Home | History | Annotate | Line # | Download | only in config
tc-mn10300.c revision 1.1
      1  1.1  christos /* tc-mn10300.c -- Assembler code for the Matsushita 10300
      2  1.1  christos    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
      3  1.1  christos    2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GAS, the GNU Assembler.
      6  1.1  christos 
      7  1.1  christos    GAS is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3, or (at your option)
     10  1.1  christos    any later version.
     11  1.1  christos 
     12  1.1  christos    GAS is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with GAS; see the file COPYING.  If not, write to
     19  1.1  christos    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
     20  1.1  christos    Boston, MA 02110-1301, USA.  */
     21  1.1  christos 
     22  1.1  christos #include "as.h"
     23  1.1  christos #include "safe-ctype.h"
     24  1.1  christos #include "subsegs.h"
     25  1.1  christos #include "opcode/mn10300.h"
     26  1.1  christos #include "dwarf2dbg.h"
     27  1.1  christos #include "libiberty.h"
     28  1.1  christos 
     29  1.1  christos /* Structure to hold information about predefined registers.  */
     31  1.1  christos struct reg_name
     32  1.1  christos {
     33  1.1  christos   const char *name;
     34  1.1  christos   int value;
     35  1.1  christos };
     36  1.1  christos 
     37  1.1  christos /* Generic assembler global variables which must be defined by all
     38  1.1  christos    targets.  */
     39  1.1  christos 
     40  1.1  christos /* Characters which always start a comment.  */
     41  1.1  christos const char comment_chars[] = "#";
     42  1.1  christos 
     43  1.1  christos /* Characters which start a comment at the beginning of a line.  */
     44  1.1  christos const char line_comment_chars[] = ";#";
     45  1.1  christos 
     46  1.1  christos /* Characters which may be used to separate multiple commands on a
     47  1.1  christos    single line.  */
     48  1.1  christos const char line_separator_chars[] = ";";
     49  1.1  christos 
     50  1.1  christos /* Characters which are used to indicate an exponent in a floating
     51  1.1  christos    point number.  */
     52  1.1  christos const char EXP_CHARS[] = "eE";
     53  1.1  christos 
     54  1.1  christos /* Characters which mean that a number is a floating point constant,
     55  1.1  christos    as in 0d1.0.  */
     56  1.1  christos const char FLT_CHARS[] = "dD";
     57  1.1  christos 
     58  1.1  christos const relax_typeS md_relax_table[] =
     60  1.1  christos {
     61  1.1  christos   /* The plus values for the bCC and fBCC instructions in the table below
     62  1.1  christos      are because the branch instruction is translated into a jump
     63  1.1  christos      instruction that is now +2 or +3 bytes further on in memory, and the
     64  1.1  christos      correct size of jump instruction must be selected.  */
     65  1.1  christos   /* bCC relaxing.  */
     66  1.1  christos   {0x7f, -0x80, 2, 1},
     67  1.1  christos   {0x7fff + 2, -0x8000 + 2, 5, 2},
     68  1.1  christos   {0x7fffffff, -0x80000000, 7, 0},
     69  1.1  christos 
     70  1.1  christos   /* bCC relaxing (uncommon cases for 3byte length instructions)  */
     71  1.1  christos   {0x7f, -0x80, 3, 4},
     72  1.1  christos   {0x7fff + 3, -0x8000 + 3, 6, 5},
     73  1.1  christos   {0x7fffffff, -0x80000000, 8, 0},
     74  1.1  christos 
     75  1.1  christos   /* call relaxing.  */
     76  1.1  christos   {0x7fff, -0x8000, 5, 7},
     77  1.1  christos   {0x7fffffff, -0x80000000, 7, 0},
     78  1.1  christos 
     79  1.1  christos   /* calls relaxing.  */
     80  1.1  christos   {0x7fff, -0x8000, 4, 9},
     81  1.1  christos   {0x7fffffff, -0x80000000, 6, 0},
     82  1.1  christos 
     83  1.1  christos   /* jmp relaxing.  */
     84  1.1  christos   {0x7f, -0x80, 2, 11},
     85  1.1  christos   {0x7fff, -0x8000, 3, 12},
     86  1.1  christos   {0x7fffffff, -0x80000000, 5, 0},
     87  1.1  christos 
     88  1.1  christos   /* fbCC relaxing.  */
     89  1.1  christos   {0x7f, -0x80, 3, 14},
     90  1.1  christos   {0x7fff + 3, -0x8000 + 3, 6, 15},
     91  1.1  christos   {0x7fffffff, -0x80000000, 8, 0},
     92  1.1  christos 
     93  1.1  christos };
     94  1.1  christos 
     95  1.1  christos /*  Set linkrelax here to avoid fixups in most sections.  */
     96  1.1  christos int linkrelax = 1;
     97  1.1  christos 
     98  1.1  christos static int current_machine;
     99  1.1  christos 
    100  1.1  christos /* Fixups.  */
    101  1.1  christos #define MAX_INSN_FIXUPS 5
    102  1.1  christos 
    103  1.1  christos struct mn10300_fixup
    104  1.1  christos {
    105  1.1  christos   expressionS exp;
    106  1.1  christos   int opindex;
    107  1.1  christos   bfd_reloc_code_real_type reloc;
    108  1.1  christos };
    109  1.1  christos struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
    110  1.1  christos static int fc;
    111  1.1  christos 
    112  1.1  christos /* We must store the value of each register operand so that we can
    113  1.1  christos    verify that certain registers do not match.  */
    114  1.1  christos int mn10300_reg_operands[MN10300_MAX_OPERANDS];
    115  1.1  christos 
    116  1.1  christos const char *md_shortopts = "";
    118  1.1  christos 
    119  1.1  christos struct option md_longopts[] =
    120  1.1  christos {
    121  1.1  christos   {NULL, no_argument, NULL, 0}
    122  1.1  christos };
    123  1.1  christos 
    124  1.1  christos size_t md_longopts_size = sizeof (md_longopts);
    125  1.1  christos 
    126  1.1  christos #define HAVE_AM33_2 (current_machine == AM33_2)
    127  1.1  christos #define HAVE_AM33   (current_machine == AM33 || HAVE_AM33_2)
    128  1.1  christos #define HAVE_AM30   (current_machine == AM30)
    129  1.1  christos 
    130  1.1  christos /* Opcode hash table.  */
    131  1.1  christos static struct hash_control *mn10300_hash;
    132  1.1  christos 
    133  1.1  christos /* This table is sorted. Suitable for searching by a binary search.  */
    134  1.1  christos static const struct reg_name data_registers[] =
    135  1.1  christos {
    136  1.1  christos   { "d0", 0 },
    137  1.1  christos   { "d1", 1 },
    138  1.1  christos   { "d2", 2 },
    139  1.1  christos   { "d3", 3 },
    140  1.1  christos };
    141  1.1  christos 
    142  1.1  christos static const struct reg_name address_registers[] =
    143  1.1  christos {
    144  1.1  christos   { "a0", 0 },
    145  1.1  christos   { "a1", 1 },
    146  1.1  christos   { "a2", 2 },
    147  1.1  christos   { "a3", 3 },
    148  1.1  christos };
    149  1.1  christos 
    150  1.1  christos static const struct reg_name r_registers[] =
    151  1.1  christos {
    152  1.1  christos   { "a0", 8 },
    153  1.1  christos   { "a1", 9 },
    154  1.1  christos   { "a2", 10 },
    155  1.1  christos   { "a3", 11 },
    156  1.1  christos   { "d0", 12 },
    157  1.1  christos   { "d1", 13 },
    158  1.1  christos   { "d2", 14 },
    159  1.1  christos   { "d3", 15 },
    160  1.1  christos   { "e0", 0 },
    161  1.1  christos   { "e1", 1 },
    162  1.1  christos   { "e10", 10 },
    163  1.1  christos   { "e11", 11 },
    164  1.1  christos   { "e12", 12 },
    165  1.1  christos   { "e13", 13 },
    166  1.1  christos   { "e14", 14 },
    167  1.1  christos   { "e15", 15 },
    168  1.1  christos   { "e2", 2 },
    169  1.1  christos   { "e3", 3 },
    170  1.1  christos   { "e4", 4 },
    171  1.1  christos   { "e5", 5 },
    172  1.1  christos   { "e6", 6 },
    173  1.1  christos   { "e7", 7 },
    174  1.1  christos   { "e8", 8 },
    175  1.1  christos   { "e9", 9 },
    176  1.1  christos   { "r0", 0 },
    177  1.1  christos   { "r1", 1 },
    178  1.1  christos   { "r10", 10 },
    179  1.1  christos   { "r11", 11 },
    180  1.1  christos   { "r12", 12 },
    181  1.1  christos   { "r13", 13 },
    182  1.1  christos   { "r14", 14 },
    183  1.1  christos   { "r15", 15 },
    184  1.1  christos   { "r2", 2 },
    185  1.1  christos   { "r3", 3 },
    186  1.1  christos   { "r4", 4 },
    187  1.1  christos   { "r5", 5 },
    188  1.1  christos   { "r6", 6 },
    189  1.1  christos   { "r7", 7 },
    190  1.1  christos   { "r8", 8 },
    191  1.1  christos   { "r9", 9 },
    192  1.1  christos };
    193  1.1  christos 
    194  1.1  christos static const struct reg_name xr_registers[] =
    195  1.1  christos {
    196  1.1  christos   { "mcrh", 2 },
    197  1.1  christos   { "mcrl", 3 },
    198  1.1  christos   { "mcvf", 4 },
    199  1.1  christos   { "mdrq", 1 },
    200  1.1  christos   { "sp", 0 },
    201  1.1  christos   { "xr0", 0 },
    202  1.1  christos   { "xr1", 1 },
    203  1.1  christos   { "xr10", 10 },
    204  1.1  christos   { "xr11", 11 },
    205  1.1  christos   { "xr12", 12 },
    206  1.1  christos   { "xr13", 13 },
    207  1.1  christos   { "xr14", 14 },
    208  1.1  christos   { "xr15", 15 },
    209  1.1  christos   { "xr2", 2 },
    210  1.1  christos   { "xr3", 3 },
    211  1.1  christos   { "xr4", 4 },
    212  1.1  christos   { "xr5", 5 },
    213  1.1  christos   { "xr6", 6 },
    214  1.1  christos   { "xr7", 7 },
    215  1.1  christos   { "xr8", 8 },
    216  1.1  christos   { "xr9", 9 },
    217  1.1  christos };
    218  1.1  christos 
    219  1.1  christos static const struct reg_name float_registers[] =
    220  1.1  christos {
    221  1.1  christos   { "fs0", 0 },
    222  1.1  christos   { "fs1", 1 },
    223  1.1  christos   { "fs10", 10 },
    224  1.1  christos   { "fs11", 11 },
    225  1.1  christos   { "fs12", 12 },
    226  1.1  christos   { "fs13", 13 },
    227  1.1  christos   { "fs14", 14 },
    228  1.1  christos   { "fs15", 15 },
    229  1.1  christos   { "fs16", 16 },
    230  1.1  christos   { "fs17", 17 },
    231  1.1  christos   { "fs18", 18 },
    232  1.1  christos   { "fs19", 19 },
    233  1.1  christos   { "fs2",   2 },
    234  1.1  christos   { "fs20", 20 },
    235  1.1  christos   { "fs21", 21 },
    236  1.1  christos   { "fs22", 22 },
    237  1.1  christos   { "fs23", 23 },
    238  1.1  christos   { "fs24", 24 },
    239  1.1  christos   { "fs25", 25 },
    240  1.1  christos   { "fs26", 26 },
    241  1.1  christos   { "fs27", 27 },
    242  1.1  christos   { "fs28", 28 },
    243  1.1  christos   { "fs29", 29 },
    244  1.1  christos   { "fs3",   3 },
    245  1.1  christos   { "fs30", 30 },
    246  1.1  christos   { "fs31", 31 },
    247  1.1  christos   { "fs4",   4 },
    248  1.1  christos   { "fs5",   5 },
    249  1.1  christos   { "fs6",   6 },
    250  1.1  christos   { "fs7",   7 },
    251  1.1  christos   { "fs8",   8 },
    252  1.1  christos   { "fs9",   9 },
    253  1.1  christos };
    254  1.1  christos 
    255  1.1  christos static const struct reg_name double_registers[] =
    256  1.1  christos {
    257  1.1  christos   { "fd0",   0 },
    258  1.1  christos   { "fd10", 10 },
    259  1.1  christos   { "fd12", 12 },
    260  1.1  christos   { "fd14", 14 },
    261  1.1  christos   { "fd16", 16 },
    262  1.1  christos   { "fd18", 18 },
    263  1.1  christos   { "fd2",   2 },
    264  1.1  christos   { "fd20", 20 },
    265  1.1  christos   { "fd22", 22 },
    266  1.1  christos   { "fd24", 24 },
    267  1.1  christos   { "fd26", 26 },
    268  1.1  christos   { "fd28", 28 },
    269  1.1  christos   { "fd30", 30 },
    270  1.1  christos   { "fd4",   4 },
    271  1.1  christos   { "fd6",   6 },
    272  1.1  christos   { "fd8",   8 },
    273  1.1  christos };
    274  1.1  christos 
    275  1.1  christos /* We abuse the `value' field, that would be otherwise unused, to
    276  1.1  christos    encode the architecture on which (access to) the register was
    277  1.1  christos    introduced.  FIXME: we should probably warn when we encounter a
    278  1.1  christos    register name when assembling for an architecture that doesn't
    279  1.1  christos    support it, before parsing it as a symbol name.  */
    280  1.1  christos static const struct reg_name other_registers[] =
    281  1.1  christos {
    282  1.1  christos   { "epsw", AM33 },
    283  1.1  christos   { "mdr", 0 },
    284  1.1  christos   { "pc", AM33 },
    285  1.1  christos   { "psw", 0 },
    286  1.1  christos   { "sp", 0 },
    287  1.1  christos   { "ssp", 0 },
    288  1.1  christos   { "usp", 0 },
    289  1.1  christos };
    290  1.1  christos 
    291  1.1  christos #define OTHER_REG_NAME_CNT	ARRAY_SIZE (other_registers)
    292  1.1  christos 
    293  1.1  christos /* Perform a binary search of the given register table REGS to see
    294  1.1  christos    if NAME is a valid regiter name.  Returns the register number from
    295  1.1  christos    the array on success, or -1 on failure.  */
    296  1.1  christos 
    297  1.1  christos static int
    298  1.1  christos reg_name_search (const struct reg_name *regs,
    299  1.1  christos 		 int regcount,
    300  1.1  christos 		 const char *name)
    301  1.1  christos {
    302  1.1  christos   int low, high;
    303  1.1  christos 
    304  1.1  christos   low = 0;
    305  1.1  christos   high = regcount - 1;
    306  1.1  christos 
    307  1.1  christos   do
    308  1.1  christos     {
    309  1.1  christos       int cmp, middle;
    310  1.1  christos 
    311  1.1  christos       middle = (low + high) / 2;
    312  1.1  christos       cmp = strcasecmp (name, regs[middle].name);
    313  1.1  christos       if (cmp < 0)
    314  1.1  christos 	high = middle - 1;
    315  1.1  christos       else if (cmp > 0)
    316  1.1  christos 	low = middle + 1;
    317  1.1  christos       else
    318  1.1  christos 	return regs[middle].value;
    319  1.1  christos     }
    320  1.1  christos   while (low <= high);
    321  1.1  christos 
    322  1.1  christos   return -1;
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos /* Looks at the current position in the input line to see if it is
    326  1.1  christos    the name of a register in TABLE.  If it is, then the name is
    327  1.1  christos    converted into an expression returned in EXPRESSIONP (with X_op
    328  1.1  christos    set to O_register and X_add_number set to the register number), the
    329  1.1  christos    input pointer is left pointing at the first non-blank character after
    330  1.1  christos    the name and the function returns TRUE.  Otherwise the input pointer
    331  1.1  christos    is left alone and the function returns FALSE.  */
    332  1.1  christos 
    333  1.1  christos static bfd_boolean
    334  1.1  christos get_register_name (expressionS *           expressionP,
    335  1.1  christos 		   const struct reg_name * table,
    336  1.1  christos 		   size_t                  table_length)
    337  1.1  christos {
    338  1.1  christos   int reg_number;
    339  1.1  christos   char *name;
    340  1.1  christos   char *start;
    341  1.1  christos   char c;
    342  1.1  christos 
    343  1.1  christos   /* Find the spelling of the operand.  */
    344  1.1  christos   start = name = input_line_pointer;
    345  1.1  christos 
    346  1.1  christos   c = get_symbol_end ();
    347  1.1  christos   reg_number = reg_name_search (table, table_length, name);
    348  1.1  christos 
    349  1.1  christos   /* Put back the delimiting char.  */
    350  1.1  christos   *input_line_pointer = c;
    351  1.1  christos 
    352  1.1  christos   /* Look to see if it's in the register table.  */
    353  1.1  christos   if (reg_number >= 0)
    354  1.1  christos     {
    355  1.1  christos       expressionP->X_op = O_register;
    356  1.1  christos       expressionP->X_add_number = reg_number;
    357  1.1  christos 
    358  1.1  christos       /* Make the rest nice.  */
    359  1.1  christos       expressionP->X_add_symbol = NULL;
    360  1.1  christos       expressionP->X_op_symbol = NULL;
    361  1.1  christos 
    362  1.1  christos       return TRUE;
    363  1.1  christos     }
    364  1.1  christos 
    365  1.1  christos   /* Reset the line as if we had not done anything.  */
    366  1.1  christos   input_line_pointer = start;
    367  1.1  christos   return FALSE;
    368  1.1  christos }
    369  1.1  christos 
    370  1.1  christos static bfd_boolean
    371  1.1  christos r_register_name (expressionS *expressionP)
    372  1.1  christos {
    373  1.1  christos   return get_register_name (expressionP, r_registers, ARRAY_SIZE (r_registers));
    374  1.1  christos }
    375  1.1  christos 
    376  1.1  christos 
    377  1.1  christos static bfd_boolean
    378  1.1  christos xr_register_name (expressionS *expressionP)
    379  1.1  christos {
    380  1.1  christos   return get_register_name (expressionP, xr_registers, ARRAY_SIZE (xr_registers));
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos static bfd_boolean
    384  1.1  christos data_register_name (expressionS *expressionP)
    385  1.1  christos {
    386  1.1  christos   return get_register_name (expressionP, data_registers, ARRAY_SIZE (data_registers));
    387  1.1  christos }
    388  1.1  christos 
    389  1.1  christos static bfd_boolean
    390  1.1  christos address_register_name (expressionS *expressionP)
    391  1.1  christos {
    392  1.1  christos   return get_register_name (expressionP, address_registers, ARRAY_SIZE (address_registers));
    393  1.1  christos }
    394  1.1  christos 
    395  1.1  christos static bfd_boolean
    396  1.1  christos float_register_name (expressionS *expressionP)
    397  1.1  christos {
    398  1.1  christos   return get_register_name (expressionP, float_registers, ARRAY_SIZE (float_registers));
    399  1.1  christos }
    400  1.1  christos 
    401  1.1  christos static bfd_boolean
    402  1.1  christos double_register_name (expressionS *expressionP)
    403  1.1  christos {
    404  1.1  christos   return get_register_name (expressionP, double_registers, ARRAY_SIZE (double_registers));
    405  1.1  christos }
    406  1.1  christos 
    407  1.1  christos static bfd_boolean
    408  1.1  christos other_register_name (expressionS *expressionP)
    409  1.1  christos {
    410  1.1  christos   int reg_number;
    411  1.1  christos   char *name;
    412  1.1  christos   char *start;
    413  1.1  christos   char c;
    414  1.1  christos 
    415  1.1  christos   /* Find the spelling of the operand.  */
    416  1.1  christos   start = name = input_line_pointer;
    417  1.1  christos 
    418  1.1  christos   c = get_symbol_end ();
    419  1.1  christos   reg_number = reg_name_search (other_registers, ARRAY_SIZE (other_registers), name);
    420  1.1  christos 
    421  1.1  christos   /* Put back the delimiting char.  */
    422  1.1  christos   *input_line_pointer = c;
    423  1.1  christos 
    424  1.1  christos   /* Look to see if it's in the register table.  */
    425  1.1  christos   if (reg_number == 0
    426  1.1  christos       || (reg_number == AM33 && HAVE_AM33))
    427  1.1  christos     {
    428  1.1  christos       expressionP->X_op = O_register;
    429  1.1  christos       expressionP->X_add_number = 0;
    430  1.1  christos 
    431  1.1  christos       /* Make the rest nice.  */
    432  1.1  christos       expressionP->X_add_symbol = NULL;
    433  1.1  christos       expressionP->X_op_symbol = NULL;
    434  1.1  christos 
    435  1.1  christos       return TRUE;
    436  1.1  christos     }
    437  1.1  christos 
    438  1.1  christos   /* Reset the line as if we had not done anything.  */
    439  1.1  christos   input_line_pointer = start;
    440  1.1  christos   return FALSE;
    441  1.1  christos }
    442  1.1  christos 
    443  1.1  christos void
    444  1.1  christos md_show_usage (FILE *stream)
    445  1.1  christos {
    446  1.1  christos   fprintf (stream, _("MN10300 assembler options:\n\
    447  1.1  christos none yet\n"));
    448  1.1  christos }
    449  1.1  christos 
    450  1.1  christos int
    451  1.1  christos md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
    452  1.1  christos {
    453  1.1  christos   return 0;
    454  1.1  christos }
    455  1.1  christos 
    456  1.1  christos symbolS *
    457  1.1  christos md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
    458  1.1  christos {
    459  1.1  christos   return 0;
    460  1.1  christos }
    461  1.1  christos 
    462  1.1  christos char *
    463  1.1  christos md_atof (int type, char *litp, int *sizep)
    464  1.1  christos {
    465  1.1  christos   return ieee_md_atof (type, litp, sizep, FALSE);
    466  1.1  christos }
    467  1.1  christos 
    468  1.1  christos void
    469  1.1  christos md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
    470  1.1  christos 		 asection *sec,
    471  1.1  christos 		 fragS *fragP)
    472  1.1  christos {
    473  1.1  christos   static unsigned long label_count = 0;
    474  1.1  christos   char buf[40];
    475  1.1  christos 
    476  1.1  christos   subseg_change (sec, 0);
    477  1.1  christos   if (fragP->fr_subtype == 0)
    478  1.1  christos     {
    479  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
    480  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
    481  1.1  christos       fragP->fr_var = 0;
    482  1.1  christos       fragP->fr_fix += 2;
    483  1.1  christos     }
    484  1.1  christos   else if (fragP->fr_subtype == 1)
    485  1.1  christos     {
    486  1.1  christos       /* Reverse the condition of the first branch.  */
    487  1.1  christos       int offset = fragP->fr_fix;
    488  1.1  christos       int opcode = fragP->fr_literal[offset] & 0xff;
    489  1.1  christos 
    490  1.1  christos       switch (opcode)
    491  1.1  christos 	{
    492  1.1  christos 	case 0xc8:
    493  1.1  christos 	  opcode = 0xc9;
    494  1.1  christos 	  break;
    495  1.1  christos 	case 0xc9:
    496  1.1  christos 	  opcode = 0xc8;
    497  1.1  christos 	  break;
    498  1.1  christos 	case 0xc0:
    499  1.1  christos 	  opcode = 0xc2;
    500  1.1  christos 	  break;
    501  1.1  christos 	case 0xc2:
    502  1.1  christos 	  opcode = 0xc0;
    503  1.1  christos 	  break;
    504  1.1  christos 	case 0xc3:
    505  1.1  christos 	  opcode = 0xc1;
    506  1.1  christos 	  break;
    507  1.1  christos 	case 0xc1:
    508  1.1  christos 	  opcode = 0xc3;
    509  1.1  christos 	  break;
    510  1.1  christos 	case 0xc4:
    511  1.1  christos 	  opcode = 0xc6;
    512  1.1  christos 	  break;
    513  1.1  christos 	case 0xc6:
    514  1.1  christos 	  opcode = 0xc4;
    515  1.1  christos 	  break;
    516  1.1  christos 	case 0xc7:
    517  1.1  christos 	  opcode = 0xc5;
    518  1.1  christos 	  break;
    519  1.1  christos 	case 0xc5:
    520  1.1  christos 	  opcode = 0xc7;
    521  1.1  christos 	  break;
    522  1.1  christos 	default:
    523  1.1  christos 	  abort ();
    524  1.1  christos 	}
    525  1.1  christos       fragP->fr_literal[offset] = opcode;
    526  1.1  christos 
    527  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    528  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    529  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 1,
    530  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    531  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
    532  1.1  christos 
    533  1.1  christos       /* Now create the unconditional branch + fixup to the
    534  1.1  christos 	 final target.  */
    535  1.1  christos       fragP->fr_literal[offset + 2] = 0xcc;
    536  1.1  christos       fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
    537  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
    538  1.1  christos       fragP->fr_var = 0;
    539  1.1  christos       fragP->fr_fix += 5;
    540  1.1  christos     }
    541  1.1  christos   else if (fragP->fr_subtype == 2)
    542  1.1  christos     {
    543  1.1  christos       /* Reverse the condition of the first branch.  */
    544  1.1  christos       int offset = fragP->fr_fix;
    545  1.1  christos       int opcode = fragP->fr_literal[offset] & 0xff;
    546  1.1  christos 
    547  1.1  christos       switch (opcode)
    548  1.1  christos 	{
    549  1.1  christos 	case 0xc8:
    550  1.1  christos 	  opcode = 0xc9;
    551  1.1  christos 	  break;
    552  1.1  christos 	case 0xc9:
    553  1.1  christos 	  opcode = 0xc8;
    554  1.1  christos 	  break;
    555  1.1  christos 	case 0xc0:
    556  1.1  christos 	  opcode = 0xc2;
    557  1.1  christos 	  break;
    558  1.1  christos 	case 0xc2:
    559  1.1  christos 	  opcode = 0xc0;
    560  1.1  christos 	  break;
    561  1.1  christos 	case 0xc3:
    562  1.1  christos 	  opcode = 0xc1;
    563  1.1  christos 	  break;
    564  1.1  christos 	case 0xc1:
    565  1.1  christos 	  opcode = 0xc3;
    566  1.1  christos 	  break;
    567  1.1  christos 	case 0xc4:
    568  1.1  christos 	  opcode = 0xc6;
    569  1.1  christos 	  break;
    570  1.1  christos 	case 0xc6:
    571  1.1  christos 	  opcode = 0xc4;
    572  1.1  christos 	  break;
    573  1.1  christos 	case 0xc7:
    574  1.1  christos 	  opcode = 0xc5;
    575  1.1  christos 	  break;
    576  1.1  christos 	case 0xc5:
    577  1.1  christos 	  opcode = 0xc7;
    578  1.1  christos 	  break;
    579  1.1  christos 	default:
    580  1.1  christos 	  abort ();
    581  1.1  christos 	}
    582  1.1  christos       fragP->fr_literal[offset] = opcode;
    583  1.1  christos 
    584  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    585  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    586  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 1,
    587  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    588  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
    589  1.1  christos 
    590  1.1  christos       /* Now create the unconditional branch + fixup to the
    591  1.1  christos 	 final target.  */
    592  1.1  christos       fragP->fr_literal[offset + 2] = 0xdc;
    593  1.1  christos       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
    594  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
    595  1.1  christos       fragP->fr_var = 0;
    596  1.1  christos       fragP->fr_fix += 7;
    597  1.1  christos     }
    598  1.1  christos   else if (fragP->fr_subtype == 3)
    599  1.1  christos     {
    600  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
    601  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    602  1.1  christos       fragP->fr_var = 0;
    603  1.1  christos       fragP->fr_fix += 3;
    604  1.1  christos     }
    605  1.1  christos   else if (fragP->fr_subtype == 4)
    606  1.1  christos     {
    607  1.1  christos       /* Reverse the condition of the first branch.  */
    608  1.1  christos       int offset = fragP->fr_fix;
    609  1.1  christos       int opcode = fragP->fr_literal[offset + 1] & 0xff;
    610  1.1  christos 
    611  1.1  christos       switch (opcode)
    612  1.1  christos 	{
    613  1.1  christos 	case 0xe8:
    614  1.1  christos 	  opcode = 0xe9;
    615  1.1  christos 	  break;
    616  1.1  christos 	case 0xe9:
    617  1.1  christos 	  opcode = 0xe8;
    618  1.1  christos 	  break;
    619  1.1  christos 	case 0xea:
    620  1.1  christos 	  opcode = 0xeb;
    621  1.1  christos 	  break;
    622  1.1  christos 	case 0xeb:
    623  1.1  christos 	  opcode = 0xea;
    624  1.1  christos 	  break;
    625  1.1  christos 	default:
    626  1.1  christos 	  abort ();
    627  1.1  christos 	}
    628  1.1  christos       fragP->fr_literal[offset + 1] = opcode;
    629  1.1  christos 
    630  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    631  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    632  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1,
    633  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    634  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    635  1.1  christos 
    636  1.1  christos       /* Now create the unconditional branch + fixup to the
    637  1.1  christos 	 final target.  */
    638  1.1  christos       fragP->fr_literal[offset + 3] = 0xcc;
    639  1.1  christos       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
    640  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
    641  1.1  christos       fragP->fr_var = 0;
    642  1.1  christos       fragP->fr_fix += 6;
    643  1.1  christos     }
    644  1.1  christos   else if (fragP->fr_subtype == 5)
    645  1.1  christos     {
    646  1.1  christos       /* Reverse the condition of the first branch.  */
    647  1.1  christos       int offset = fragP->fr_fix;
    648  1.1  christos       int opcode = fragP->fr_literal[offset + 1] & 0xff;
    649  1.1  christos 
    650  1.1  christos       switch (opcode)
    651  1.1  christos 	{
    652  1.1  christos 	case 0xe8:
    653  1.1  christos 	  opcode = 0xe9;
    654  1.1  christos 	  break;
    655  1.1  christos 	case 0xea:
    656  1.1  christos 	  opcode = 0xeb;
    657  1.1  christos 	  break;
    658  1.1  christos 	case 0xeb:
    659  1.1  christos 	  opcode = 0xea;
    660  1.1  christos 	  break;
    661  1.1  christos 	default:
    662  1.1  christos 	  abort ();
    663  1.1  christos 	}
    664  1.1  christos       fragP->fr_literal[offset + 1] = opcode;
    665  1.1  christos 
    666  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    667  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    668  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1,
    669  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    670  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    671  1.1  christos 
    672  1.1  christos       /* Now create the unconditional branch + fixup to the
    673  1.1  christos 	 final target.  */
    674  1.1  christos       fragP->fr_literal[offset + 3] = 0xdc;
    675  1.1  christos       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
    676  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
    677  1.1  christos       fragP->fr_var = 0;
    678  1.1  christos       fragP->fr_fix += 8;
    679  1.1  christos     }
    680  1.1  christos   else if (fragP->fr_subtype == 6)
    681  1.1  christos     {
    682  1.1  christos       int offset = fragP->fr_fix;
    683  1.1  christos 
    684  1.1  christos       fragP->fr_literal[offset] = 0xcd;
    685  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
    686  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
    687  1.1  christos       fragP->fr_var = 0;
    688  1.1  christos       fragP->fr_fix += 5;
    689  1.1  christos     }
    690  1.1  christos   else if (fragP->fr_subtype == 7)
    691  1.1  christos     {
    692  1.1  christos       int offset = fragP->fr_fix;
    693  1.1  christos 
    694  1.1  christos       fragP->fr_literal[offset] = 0xdd;
    695  1.1  christos       fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
    696  1.1  christos       fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
    697  1.1  christos       fragP->fr_literal[offset + 3] = 0;
    698  1.1  christos       fragP->fr_literal[offset + 4] = 0;
    699  1.1  christos 
    700  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
    701  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
    702  1.1  christos       fragP->fr_var = 0;
    703  1.1  christos       fragP->fr_fix += 7;
    704  1.1  christos     }
    705  1.1  christos   else if (fragP->fr_subtype == 8)
    706  1.1  christos     {
    707  1.1  christos       int offset = fragP->fr_fix;
    708  1.1  christos 
    709  1.1  christos       fragP->fr_literal[offset] = 0xfa;
    710  1.1  christos       fragP->fr_literal[offset + 1] = 0xff;
    711  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
    712  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
    713  1.1  christos       fragP->fr_var = 0;
    714  1.1  christos       fragP->fr_fix += 4;
    715  1.1  christos     }
    716  1.1  christos   else if (fragP->fr_subtype == 9)
    717  1.1  christos     {
    718  1.1  christos       int offset = fragP->fr_fix;
    719  1.1  christos 
    720  1.1  christos       fragP->fr_literal[offset] = 0xfc;
    721  1.1  christos       fragP->fr_literal[offset + 1] = 0xff;
    722  1.1  christos 
    723  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
    724  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
    725  1.1  christos       fragP->fr_var = 0;
    726  1.1  christos       fragP->fr_fix += 6;
    727  1.1  christos     }
    728  1.1  christos   else if (fragP->fr_subtype == 10)
    729  1.1  christos     {
    730  1.1  christos       fragP->fr_literal[fragP->fr_fix] = 0xca;
    731  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
    732  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
    733  1.1  christos       fragP->fr_var = 0;
    734  1.1  christos       fragP->fr_fix += 2;
    735  1.1  christos     }
    736  1.1  christos   else if (fragP->fr_subtype == 11)
    737  1.1  christos     {
    738  1.1  christos       int offset = fragP->fr_fix;
    739  1.1  christos 
    740  1.1  christos       fragP->fr_literal[offset] = 0xcc;
    741  1.1  christos 
    742  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
    743  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
    744  1.1  christos       fragP->fr_var = 0;
    745  1.1  christos       fragP->fr_fix += 3;
    746  1.1  christos     }
    747  1.1  christos   else if (fragP->fr_subtype == 12)
    748  1.1  christos     {
    749  1.1  christos       int offset = fragP->fr_fix;
    750  1.1  christos 
    751  1.1  christos       fragP->fr_literal[offset] = 0xdc;
    752  1.1  christos 
    753  1.1  christos       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
    754  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
    755  1.1  christos       fragP->fr_var = 0;
    756  1.1  christos       fragP->fr_fix += 5;
    757  1.1  christos     }
    758  1.1  christos   else if (fragP->fr_subtype == 13)
    759  1.1  christos     {
    760  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
    761  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    762  1.1  christos       fragP->fr_var = 0;
    763  1.1  christos       fragP->fr_fix += 3;
    764  1.1  christos     }
    765  1.1  christos   else if (fragP->fr_subtype == 14)
    766  1.1  christos     {
    767  1.1  christos       /* Reverse the condition of the first branch.  */
    768  1.1  christos       int offset = fragP->fr_fix;
    769  1.1  christos       int opcode = fragP->fr_literal[offset + 1] & 0xff;
    770  1.1  christos 
    771  1.1  christos       switch (opcode)
    772  1.1  christos 	{
    773  1.1  christos 	case 0xd0:
    774  1.1  christos 	  opcode = 0xd1;
    775  1.1  christos 	  break;
    776  1.1  christos 	case 0xd1:
    777  1.1  christos 	  opcode = 0xd0;
    778  1.1  christos 	  break;
    779  1.1  christos 	case 0xd2:
    780  1.1  christos 	  opcode = 0xdc;
    781  1.1  christos 	  break;
    782  1.1  christos 	case 0xd3:
    783  1.1  christos 	  opcode = 0xdb;
    784  1.1  christos 	  break;
    785  1.1  christos 	case 0xd4:
    786  1.1  christos 	  opcode = 0xda;
    787  1.1  christos 	  break;
    788  1.1  christos 	case 0xd5:
    789  1.1  christos 	  opcode = 0xd9;
    790  1.1  christos 	  break;
    791  1.1  christos 	case 0xd6:
    792  1.1  christos 	  opcode = 0xd8;
    793  1.1  christos 	  break;
    794  1.1  christos 	case 0xd7:
    795  1.1  christos 	  opcode = 0xdd;
    796  1.1  christos 	  break;
    797  1.1  christos 	case 0xd8:
    798  1.1  christos 	  opcode = 0xd6;
    799  1.1  christos 	  break;
    800  1.1  christos 	case 0xd9:
    801  1.1  christos 	  opcode = 0xd5;
    802  1.1  christos 	  break;
    803  1.1  christos 	case 0xda:
    804  1.1  christos 	  opcode = 0xd4;
    805  1.1  christos 	  break;
    806  1.1  christos 	case 0xdb:
    807  1.1  christos 	  opcode = 0xd3;
    808  1.1  christos 	  break;
    809  1.1  christos 	case 0xdc:
    810  1.1  christos 	  opcode = 0xd2;
    811  1.1  christos 	  break;
    812  1.1  christos 	case 0xdd:
    813  1.1  christos 	  opcode = 0xd7;
    814  1.1  christos 	  break;
    815  1.1  christos 	default:
    816  1.1  christos 	  abort ();
    817  1.1  christos 	}
    818  1.1  christos       fragP->fr_literal[offset + 1] = opcode;
    819  1.1  christos 
    820  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    821  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    822  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1,
    823  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    824  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    825  1.1  christos 
    826  1.1  christos       /* Now create the unconditional branch + fixup to the
    827  1.1  christos 	 final target.  */
    828  1.1  christos       fragP->fr_literal[offset + 3] = 0xcc;
    829  1.1  christos       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
    830  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
    831  1.1  christos       fragP->fr_var = 0;
    832  1.1  christos       fragP->fr_fix += 6;
    833  1.1  christos     }
    834  1.1  christos   else if (fragP->fr_subtype == 15)
    835  1.1  christos     {
    836  1.1  christos       /* Reverse the condition of the first branch.  */
    837  1.1  christos       int offset = fragP->fr_fix;
    838  1.1  christos       int opcode = fragP->fr_literal[offset + 1] & 0xff;
    839  1.1  christos 
    840  1.1  christos       switch (opcode)
    841  1.1  christos 	{
    842  1.1  christos 	case 0xd0:
    843  1.1  christos 	  opcode = 0xd1;
    844  1.1  christos 	  break;
    845  1.1  christos 	case 0xd1:
    846  1.1  christos 	  opcode = 0xd0;
    847  1.1  christos 	  break;
    848  1.1  christos 	case 0xd2:
    849  1.1  christos 	  opcode = 0xdc;
    850  1.1  christos 	  break;
    851  1.1  christos 	case 0xd3:
    852  1.1  christos 	  opcode = 0xdb;
    853  1.1  christos 	  break;
    854  1.1  christos 	case 0xd4:
    855  1.1  christos 	  opcode = 0xda;
    856  1.1  christos 	  break;
    857  1.1  christos 	case 0xd5:
    858  1.1  christos 	  opcode = 0xd9;
    859  1.1  christos 	  break;
    860  1.1  christos 	case 0xd6:
    861  1.1  christos 	  opcode = 0xd8;
    862  1.1  christos 	  break;
    863  1.1  christos 	case 0xd7:
    864  1.1  christos 	  opcode = 0xdd;
    865  1.1  christos 	  break;
    866  1.1  christos 	case 0xd8:
    867  1.1  christos 	  opcode = 0xd6;
    868  1.1  christos 	  break;
    869  1.1  christos 	case 0xd9:
    870  1.1  christos 	  opcode = 0xd5;
    871  1.1  christos 	  break;
    872  1.1  christos 	case 0xda:
    873  1.1  christos 	  opcode = 0xd4;
    874  1.1  christos 	  break;
    875  1.1  christos 	case 0xdb:
    876  1.1  christos 	  opcode = 0xd3;
    877  1.1  christos 	  break;
    878  1.1  christos 	case 0xdc:
    879  1.1  christos 	  opcode = 0xd2;
    880  1.1  christos 	  break;
    881  1.1  christos 	case 0xdd:
    882  1.1  christos 	  opcode = 0xd7;
    883  1.1  christos 	  break;
    884  1.1  christos 	default:
    885  1.1  christos 	  abort ();
    886  1.1  christos 	}
    887  1.1  christos       fragP->fr_literal[offset + 1] = opcode;
    888  1.1  christos 
    889  1.1  christos       /* Create a fixup for the reversed conditional branch.  */
    890  1.1  christos       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
    891  1.1  christos       fix_new (fragP, fragP->fr_fix + 2, 1,
    892  1.1  christos 	       symbol_new (buf, sec, 0, fragP->fr_next),
    893  1.1  christos 	       fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
    894  1.1  christos 
    895  1.1  christos       /* Now create the unconditional branch + fixup to the
    896  1.1  christos 	 final target.  */
    897  1.1  christos       fragP->fr_literal[offset + 3] = 0xdc;
    898  1.1  christos       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
    899  1.1  christos 	       fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
    900  1.1  christos       fragP->fr_var = 0;
    901  1.1  christos       fragP->fr_fix += 8;
    902  1.1  christos     }
    903  1.1  christos   else
    904  1.1  christos     abort ();
    905  1.1  christos }
    906  1.1  christos 
    907  1.1  christos valueT
    908  1.1  christos md_section_align (asection *seg, valueT addr)
    909  1.1  christos {
    910  1.1  christos   int align = bfd_get_section_alignment (stdoutput, seg);
    911  1.1  christos 
    912  1.1  christos   return ((addr + (1 << align) - 1) & (-1 << align));
    913  1.1  christos }
    914  1.1  christos 
    915  1.1  christos void
    916  1.1  christos md_begin (void)
    917  1.1  christos {
    918  1.1  christos   char *prev_name = "";
    919  1.1  christos   const struct mn10300_opcode *op;
    920  1.1  christos 
    921  1.1  christos   mn10300_hash = hash_new ();
    922  1.1  christos 
    923  1.1  christos   /* Insert unique names into hash table.  The MN10300 instruction set
    924  1.1  christos      has many identical opcode names that have different opcodes based
    925  1.1  christos      on the operands.  This hash table then provides a quick index to
    926  1.1  christos      the first opcode with a particular name in the opcode table.  */
    927  1.1  christos 
    928  1.1  christos   op = mn10300_opcodes;
    929  1.1  christos   while (op->name)
    930  1.1  christos     {
    931  1.1  christos       if (strcmp (prev_name, op->name))
    932  1.1  christos 	{
    933  1.1  christos 	  prev_name = (char *) op->name;
    934  1.1  christos 	  hash_insert (mn10300_hash, op->name, (char *) op);
    935  1.1  christos 	}
    936  1.1  christos       op++;
    937  1.1  christos     }
    938  1.1  christos 
    939  1.1  christos   /* Set the default machine type.  */
    940  1.1  christos #ifdef TE_LINUX
    941  1.1  christos   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, AM33_2))
    942  1.1  christos     as_warn (_("could not set architecture and machine"));
    943  1.1  christos 
    944  1.1  christos   current_machine = AM33_2;
    945  1.1  christos #else
    946  1.1  christos   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, MN103))
    947  1.1  christos     as_warn (_("could not set architecture and machine"));
    948  1.1  christos 
    949  1.1  christos   current_machine = MN103;
    950  1.1  christos #endif
    951  1.1  christos }
    952  1.1  christos 
    953  1.1  christos static symbolS *GOT_symbol;
    954  1.1  christos 
    955  1.1  christos static inline int
    956  1.1  christos mn10300_PIC_related_p (symbolS *sym)
    957  1.1  christos {
    958  1.1  christos   expressionS *exp;
    959  1.1  christos 
    960  1.1  christos   if (! sym)
    961  1.1  christos     return 0;
    962  1.1  christos 
    963  1.1  christos   if (sym == GOT_symbol)
    964  1.1  christos     return 1;
    965  1.1  christos 
    966  1.1  christos   exp = symbol_get_value_expression (sym);
    967  1.1  christos 
    968  1.1  christos   return (exp->X_op == O_PIC_reloc
    969  1.1  christos 	  || mn10300_PIC_related_p (exp->X_add_symbol)
    970  1.1  christos 	  || mn10300_PIC_related_p (exp->X_op_symbol));
    971  1.1  christos }
    972  1.1  christos 
    973  1.1  christos static inline int
    974  1.1  christos mn10300_check_fixup (struct mn10300_fixup *fixup)
    975  1.1  christos {
    976  1.1  christos   expressionS *exp = &fixup->exp;
    977  1.1  christos 
    978  1.1  christos  repeat:
    979  1.1  christos   switch (exp->X_op)
    980  1.1  christos     {
    981  1.1  christos     case O_add:
    982  1.1  christos     case O_subtract: /* If we're sufficiently unlucky that the label
    983  1.1  christos 			and the expression that references it happen
    984  1.1  christos 			to end up in different frags, the subtract
    985  1.1  christos 			won't be simplified within expression().  */
    986  1.1  christos       /* The PIC-related operand must be the first operand of a sum.  */
    987  1.1  christos       if (exp != &fixup->exp || mn10300_PIC_related_p (exp->X_op_symbol))
    988  1.1  christos 	return 1;
    989  1.1  christos 
    990  1.1  christos       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
    991  1.1  christos 	fixup->reloc = BFD_RELOC_32_GOT_PCREL;
    992  1.1  christos 
    993  1.1  christos       exp = symbol_get_value_expression (exp->X_add_symbol);
    994  1.1  christos       goto repeat;
    995  1.1  christos 
    996  1.1  christos     case O_symbol:
    997  1.1  christos       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
    998  1.1  christos 	fixup->reloc = BFD_RELOC_32_GOT_PCREL;
    999  1.1  christos       break;
   1000  1.1  christos 
   1001  1.1  christos     case O_PIC_reloc:
   1002  1.1  christos       fixup->reloc = exp->X_md;
   1003  1.1  christos       exp->X_op = O_symbol;
   1004  1.1  christos       if (fixup->reloc == BFD_RELOC_32_PLT_PCREL
   1005  1.1  christos 	  && fixup->opindex >= 0
   1006  1.1  christos 	  && (mn10300_operands[fixup->opindex].flags
   1007  1.1  christos 	      & MN10300_OPERAND_RELAX))
   1008  1.1  christos 	return 1;
   1009  1.1  christos       break;
   1010  1.1  christos 
   1011  1.1  christos     default:
   1012  1.1  christos       return (mn10300_PIC_related_p (exp->X_add_symbol)
   1013  1.1  christos 	      || mn10300_PIC_related_p (exp->X_op_symbol));
   1014  1.1  christos     }
   1015  1.1  christos 
   1016  1.1  christos   return 0;
   1017  1.1  christos }
   1018  1.1  christos 
   1019  1.1  christos void
   1020  1.1  christos mn10300_cons_fix_new (fragS *frag, int off, int size, expressionS *exp)
   1021  1.1  christos {
   1022  1.1  christos   struct mn10300_fixup fixup;
   1023  1.1  christos 
   1024  1.1  christos   fixup.opindex = -1;
   1025  1.1  christos   fixup.exp = *exp;
   1026  1.1  christos   fixup.reloc = BFD_RELOC_UNUSED;
   1027  1.1  christos 
   1028  1.1  christos   mn10300_check_fixup (&fixup);
   1029  1.1  christos 
   1030  1.1  christos   if (fixup.reloc == BFD_RELOC_MN10300_GOT32)
   1031  1.1  christos     switch (size)
   1032  1.1  christos       {
   1033  1.1  christos       case 2:
   1034  1.1  christos 	fixup.reloc = BFD_RELOC_MN10300_GOT16;
   1035  1.1  christos 	break;
   1036  1.1  christos 
   1037  1.1  christos       case 3:
   1038  1.1  christos 	fixup.reloc = BFD_RELOC_MN10300_GOT24;
   1039  1.1  christos 	break;
   1040  1.1  christos 
   1041  1.1  christos       case 4:
   1042  1.1  christos 	break;
   1043  1.1  christos 
   1044  1.1  christos       default:
   1045  1.1  christos 	goto error;
   1046  1.1  christos       }
   1047  1.1  christos   else if (fixup.reloc == BFD_RELOC_UNUSED)
   1048  1.1  christos     switch (size)
   1049  1.1  christos       {
   1050  1.1  christos       case 1:
   1051  1.1  christos 	fixup.reloc = BFD_RELOC_8;
   1052  1.1  christos 	break;
   1053  1.1  christos 
   1054  1.1  christos       case 2:
   1055  1.1  christos 	fixup.reloc = BFD_RELOC_16;
   1056  1.1  christos 	break;
   1057  1.1  christos 
   1058  1.1  christos       case 3:
   1059  1.1  christos 	fixup.reloc = BFD_RELOC_24;
   1060  1.1  christos 	break;
   1061  1.1  christos 
   1062  1.1  christos       case 4:
   1063  1.1  christos 	fixup.reloc = BFD_RELOC_32;
   1064  1.1  christos 	break;
   1065  1.1  christos 
   1066  1.1  christos       default:
   1067  1.1  christos 	goto error;
   1068  1.1  christos       }
   1069  1.1  christos   else if (size != 4)
   1070  1.1  christos     {
   1071  1.1  christos     error:
   1072  1.1  christos       as_bad (_("unsupported BFD relocation size %u"), size);
   1073  1.1  christos       fixup.reloc = BFD_RELOC_UNUSED;
   1074  1.1  christos     }
   1075  1.1  christos 
   1076  1.1  christos   fix_new_exp (frag, off, size, &fixup.exp, 0, fixup.reloc);
   1077  1.1  christos }
   1078  1.1  christos 
   1079  1.1  christos static bfd_boolean
   1080  1.1  christos check_operand (const struct mn10300_operand *operand,
   1081  1.1  christos 	       offsetT val)
   1082  1.1  christos {
   1083  1.1  christos   /* No need to check 32bit operands for a bit.  Note that
   1084  1.1  christos      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
   1085  1.1  christos   if (operand->bits != 32
   1086  1.1  christos       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
   1087  1.1  christos     {
   1088  1.1  christos       long min, max;
   1089  1.1  christos       offsetT test;
   1090  1.1  christos       int bits;
   1091  1.1  christos 
   1092  1.1  christos       bits = operand->bits;
   1093  1.1  christos       if (operand->flags & MN10300_OPERAND_24BIT)
   1094  1.1  christos 	bits = 24;
   1095  1.1  christos 
   1096  1.1  christos       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
   1097  1.1  christos 	{
   1098  1.1  christos 	  max = (1 << (bits - 1)) - 1;
   1099  1.1  christos 	  min = - (1 << (bits - 1));
   1100  1.1  christos 	}
   1101  1.1  christos       else
   1102  1.1  christos 	{
   1103  1.1  christos 	  max = (1 << bits) - 1;
   1104  1.1  christos 	  min = 0;
   1105  1.1  christos 	}
   1106  1.1  christos 
   1107  1.1  christos       test = val;
   1108  1.1  christos 
   1109  1.1  christos       if (test < (offsetT) min || test > (offsetT) max)
   1110  1.1  christos 	return FALSE;
   1111  1.1  christos     }
   1112  1.1  christos   return TRUE;
   1113  1.1  christos }
   1114  1.1  christos 
   1115  1.1  christos /* Insert an operand value into an instruction.  */
   1116  1.1  christos 
   1117  1.1  christos static void
   1118  1.1  christos mn10300_insert_operand (unsigned long *insnp,
   1119  1.1  christos 			unsigned long *extensionp,
   1120  1.1  christos 			const struct mn10300_operand *operand,
   1121  1.1  christos 			offsetT val,
   1122  1.1  christos 			char *file,
   1123  1.1  christos 			unsigned int line,
   1124  1.1  christos 			unsigned int shift)
   1125  1.1  christos {
   1126  1.1  christos   /* No need to check 32bit operands for a bit.  Note that
   1127  1.1  christos      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
   1128  1.1  christos   if (operand->bits != 32
   1129  1.1  christos       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
   1130  1.1  christos     {
   1131  1.1  christos       long min, max;
   1132  1.1  christos       offsetT test;
   1133  1.1  christos       int bits;
   1134  1.1  christos 
   1135  1.1  christos       bits = operand->bits;
   1136  1.1  christos       if (operand->flags & MN10300_OPERAND_24BIT)
   1137  1.1  christos 	bits = 24;
   1138  1.1  christos 
   1139  1.1  christos       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
   1140  1.1  christos 	{
   1141  1.1  christos 	  max = (1 << (bits - 1)) - 1;
   1142  1.1  christos 	  min = - (1 << (bits - 1));
   1143  1.1  christos 	}
   1144  1.1  christos       else
   1145  1.1  christos 	{
   1146  1.1  christos 	  max = (1 << bits) - 1;
   1147  1.1  christos 	  min = 0;
   1148  1.1  christos 	}
   1149  1.1  christos 
   1150  1.1  christos       test = val;
   1151  1.1  christos 
   1152  1.1  christos       if (test < (offsetT) min || test > (offsetT) max)
   1153  1.1  christos 	as_warn_value_out_of_range (_("operand"), test, (offsetT) min, (offsetT) max, file, line);
   1154  1.1  christos     }
   1155  1.1  christos 
   1156  1.1  christos   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
   1157  1.1  christos     {
   1158  1.1  christos       *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
   1159  1.1  christos       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
   1160  1.1  christos 		      << operand->shift);
   1161  1.1  christos     }
   1162  1.1  christos   else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
   1163  1.1  christos     {
   1164  1.1  christos       *insnp |= (val >> (24 - operand->bits)) & ((1 << operand->bits) - 1);
   1165  1.1  christos       *extensionp |= ((val & ((1 << (24 - operand->bits)) - 1))
   1166  1.1  christos 		      << operand->shift);
   1167  1.1  christos     }
   1168  1.1  christos   else if ((operand->flags & (MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG)))
   1169  1.1  christos     {
   1170  1.1  christos       /* See devo/opcodes/m10300-opc.c just before #define FSM0 for an
   1171  1.1  christos          explanation of these variables.  Note that FMT-implied shifts
   1172  1.1  christos         are not taken into account for FP registers.  */
   1173  1.1  christos       unsigned long mask_low, mask_high;
   1174  1.1  christos       int shl_low, shr_high, shl_high;
   1175  1.1  christos 
   1176  1.1  christos       switch (operand->bits)
   1177  1.1  christos 	{
   1178  1.1  christos 	case 5:
   1179  1.1  christos 	  /* Handle regular FP registers.  */
   1180  1.1  christos 	  if (operand->shift >= 0)
   1181  1.1  christos 	    {
   1182  1.1  christos 	      /* This is an `m' register.  */
   1183  1.1  christos 	      shl_low = operand->shift;
   1184  1.1  christos 	      shl_high = 8 + (8 & shl_low) + (shl_low & 4) / 4;
   1185  1.1  christos 	    }
   1186  1.1  christos 	  else
   1187  1.1  christos 	    {
   1188  1.1  christos 	      /* This is an `n' register.  */
   1189  1.1  christos 	      shl_low = -operand->shift;
   1190  1.1  christos 	      shl_high = shl_low / 4;
   1191  1.1  christos 	    }
   1192  1.1  christos 
   1193  1.1  christos 	  mask_low = 0x0f;
   1194  1.1  christos 	  mask_high = 0x10;
   1195  1.1  christos 	  shr_high = 4;
   1196  1.1  christos 	  break;
   1197  1.1  christos 
   1198  1.1  christos 	case 3:
   1199  1.1  christos 	  /* Handle accumulators.  */
   1200  1.1  christos 	  shl_low = -operand->shift;
   1201  1.1  christos 	  shl_high = 0;
   1202  1.1  christos 	  mask_low = 0x03;
   1203  1.1  christos 	  mask_high = 0x04;
   1204  1.1  christos 	  shr_high = 2;
   1205  1.1  christos 	  break;
   1206  1.1  christos 
   1207  1.1  christos 	default:
   1208  1.1  christos 	  abort ();
   1209  1.1  christos 	}
   1210  1.1  christos       *insnp |= ((((val & mask_high) >> shr_high) << shl_high)
   1211  1.1  christos 		 | ((val & mask_low) << shl_low));
   1212  1.1  christos     }
   1213  1.1  christos   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
   1214  1.1  christos     {
   1215  1.1  christos       *insnp |= (((long) val & ((1 << operand->bits) - 1))
   1216  1.1  christos 		 << (operand->shift + shift));
   1217  1.1  christos 
   1218  1.1  christos       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
   1219  1.1  christos 	*insnp |= (((long) val & ((1 << operand->bits) - 1))
   1220  1.1  christos 		   << (operand->shift + shift + operand->bits));
   1221  1.1  christos     }
   1222  1.1  christos   else
   1223  1.1  christos     {
   1224  1.1  christos       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
   1225  1.1  christos 		      << (operand->shift + shift));
   1226  1.1  christos 
   1227  1.1  christos       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
   1228  1.1  christos 	*extensionp |= (((long) val & ((1 << operand->bits) - 1))
   1229  1.1  christos 			<< (operand->shift + shift + operand->bits));
   1230  1.1  christos     }
   1231  1.1  christos }
   1232  1.1  christos 
   1233  1.1  christos void
   1234  1.1  christos md_assemble (char *str)
   1235  1.1  christos {
   1236  1.1  christos   char *s;
   1237  1.1  christos   struct mn10300_opcode *opcode;
   1238  1.1  christos   struct mn10300_opcode *next_opcode;
   1239  1.1  christos   const unsigned char *opindex_ptr;
   1240  1.1  christos   int next_opindex, relaxable;
   1241  1.1  christos   unsigned long insn, extension, size = 0;
   1242  1.1  christos   char *f;
   1243  1.1  christos   int i;
   1244  1.1  christos   int match;
   1245  1.1  christos 
   1246  1.1  christos   /* Get the opcode.  */
   1247  1.1  christos   for (s = str; *s != '\0' && !ISSPACE (*s); s++)
   1248  1.1  christos     ;
   1249  1.1  christos   if (*s != '\0')
   1250  1.1  christos     *s++ = '\0';
   1251  1.1  christos 
   1252  1.1  christos   /* Find the first opcode with the proper name.  */
   1253  1.1  christos   opcode = (struct mn10300_opcode *) hash_find (mn10300_hash, str);
   1254  1.1  christos   if (opcode == NULL)
   1255  1.1  christos     {
   1256  1.1  christos       as_bad (_("Unrecognized opcode: `%s'"), str);
   1257  1.1  christos       return;
   1258  1.1  christos     }
   1259  1.1  christos 
   1260  1.1  christos   str = s;
   1261  1.1  christos   while (ISSPACE (*str))
   1262  1.1  christos     ++str;
   1263  1.1  christos 
   1264  1.1  christos   input_line_pointer = str;
   1265  1.1  christos 
   1266  1.1  christos   for (;;)
   1267  1.1  christos     {
   1268  1.1  christos       const char *errmsg;
   1269  1.1  christos       int op_idx;
   1270  1.1  christos       char *hold;
   1271  1.1  christos       int extra_shift = 0;
   1272  1.1  christos 
   1273  1.1  christos       errmsg = _("Invalid opcode/operands");
   1274  1.1  christos 
   1275  1.1  christos       /* Reset the array of register operands.  */
   1276  1.1  christos       memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
   1277  1.1  christos 
   1278  1.1  christos       relaxable = 0;
   1279  1.1  christos       fc = 0;
   1280  1.1  christos       match = 0;
   1281  1.1  christos       next_opindex = 0;
   1282  1.1  christos       insn = opcode->opcode;
   1283  1.1  christos       extension = 0;
   1284  1.1  christos 
   1285  1.1  christos       /* If the instruction is not available on the current machine
   1286  1.1  christos 	 then it can not possibly match.  */
   1287  1.1  christos       if (opcode->machine
   1288  1.1  christos 	  && !(opcode->machine == AM33_2 && HAVE_AM33_2)
   1289  1.1  christos 	  && !(opcode->machine == AM33 && HAVE_AM33)
   1290  1.1  christos 	  && !(opcode->machine == AM30 && HAVE_AM30))
   1291  1.1  christos 	goto error;
   1292  1.1  christos 
   1293  1.1  christos       for (op_idx = 1, opindex_ptr = opcode->operands;
   1294  1.1  christos 	   *opindex_ptr != 0;
   1295  1.1  christos 	   opindex_ptr++, op_idx++)
   1296  1.1  christos 	{
   1297  1.1  christos 	  const struct mn10300_operand *operand;
   1298  1.1  christos 	  expressionS ex;
   1299  1.1  christos 
   1300  1.1  christos 	  if (next_opindex == 0)
   1301  1.1  christos 	    {
   1302  1.1  christos 	      operand = &mn10300_operands[*opindex_ptr];
   1303  1.1  christos 	    }
   1304  1.1  christos 	  else
   1305  1.1  christos 	    {
   1306  1.1  christos 	      operand = &mn10300_operands[next_opindex];
   1307  1.1  christos 	      next_opindex = 0;
   1308  1.1  christos 	    }
   1309  1.1  christos 
   1310  1.1  christos 	  while (*str == ' ' || *str == ',')
   1311  1.1  christos 	    ++str;
   1312  1.1  christos 
   1313  1.1  christos 	  if (operand->flags & MN10300_OPERAND_RELAX)
   1314  1.1  christos 	    relaxable = 1;
   1315  1.1  christos 
   1316  1.1  christos 	  /* Gather the operand.  */
   1317  1.1  christos 	  hold = input_line_pointer;
   1318  1.1  christos 	  input_line_pointer = str;
   1319  1.1  christos 
   1320  1.1  christos 	  if (operand->flags & MN10300_OPERAND_PAREN)
   1321  1.1  christos 	    {
   1322  1.1  christos 	      if (*input_line_pointer != ')' && *input_line_pointer != '(')
   1323  1.1  christos 		{
   1324  1.1  christos 		  input_line_pointer = hold;
   1325  1.1  christos 		  str = hold;
   1326  1.1  christos 		  goto error;
   1327  1.1  christos 		}
   1328  1.1  christos 	      input_line_pointer++;
   1329  1.1  christos 	      goto keep_going;
   1330  1.1  christos 	    }
   1331  1.1  christos 	  /* See if we can match the operands.  */
   1332  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_DREG)
   1333  1.1  christos 	    {
   1334  1.1  christos 	      if (!data_register_name (&ex))
   1335  1.1  christos 		{
   1336  1.1  christos 		  input_line_pointer = hold;
   1337  1.1  christos 		  str = hold;
   1338  1.1  christos 		  goto error;
   1339  1.1  christos 		}
   1340  1.1  christos 	    }
   1341  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_AREG)
   1342  1.1  christos 	    {
   1343  1.1  christos 	      if (!address_register_name (&ex))
   1344  1.1  christos 		{
   1345  1.1  christos 		  input_line_pointer = hold;
   1346  1.1  christos 		  str = hold;
   1347  1.1  christos 		  goto error;
   1348  1.1  christos 		}
   1349  1.1  christos 	    }
   1350  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_SP)
   1351  1.1  christos 	    {
   1352  1.1  christos 	      char *start = input_line_pointer;
   1353  1.1  christos 	      char c = get_symbol_end ();
   1354  1.1  christos 
   1355  1.1  christos 	      if (strcasecmp (start, "sp") != 0)
   1356  1.1  christos 		{
   1357  1.1  christos 		  *input_line_pointer = c;
   1358  1.1  christos 		  input_line_pointer = hold;
   1359  1.1  christos 		  str = hold;
   1360  1.1  christos 		  goto error;
   1361  1.1  christos 		}
   1362  1.1  christos 	      *input_line_pointer = c;
   1363  1.1  christos 	      goto keep_going;
   1364  1.1  christos 	    }
   1365  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_RREG)
   1366  1.1  christos 	    {
   1367  1.1  christos 	      if (!r_register_name (&ex))
   1368  1.1  christos 		{
   1369  1.1  christos 		  input_line_pointer = hold;
   1370  1.1  christos 		  str = hold;
   1371  1.1  christos 		  goto error;
   1372  1.1  christos 		}
   1373  1.1  christos 	    }
   1374  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_XRREG)
   1375  1.1  christos 	    {
   1376  1.1  christos 	      if (!xr_register_name (&ex))
   1377  1.1  christos 		{
   1378  1.1  christos 		  input_line_pointer = hold;
   1379  1.1  christos 		  str = hold;
   1380  1.1  christos 		  goto error;
   1381  1.1  christos 		}
   1382  1.1  christos 	    }
   1383  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_FSREG)
   1384  1.1  christos 	    {
   1385  1.1  christos 	      if (!float_register_name (&ex))
   1386  1.1  christos 		{
   1387  1.1  christos 		  input_line_pointer = hold;
   1388  1.1  christos 		  str = hold;
   1389  1.1  christos 		  goto error;
   1390  1.1  christos 		}
   1391  1.1  christos 	    }
   1392  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_FDREG)
   1393  1.1  christos 	    {
   1394  1.1  christos 	      if (!double_register_name (&ex))
   1395  1.1  christos 		{
   1396  1.1  christos 		  input_line_pointer = hold;
   1397  1.1  christos 		  str = hold;
   1398  1.1  christos 		  goto error;
   1399  1.1  christos 		}
   1400  1.1  christos 	    }
   1401  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_FPCR)
   1402  1.1  christos 	    {
   1403  1.1  christos 	      char *start = input_line_pointer;
   1404  1.1  christos 	      char c = get_symbol_end ();
   1405  1.1  christos 
   1406  1.1  christos 	      if (strcasecmp (start, "fpcr") != 0)
   1407  1.1  christos 		{
   1408  1.1  christos 		  *input_line_pointer = c;
   1409  1.1  christos 		  input_line_pointer = hold;
   1410  1.1  christos 		  str = hold;
   1411  1.1  christos 		  goto error;
   1412  1.1  christos 		}
   1413  1.1  christos 	      *input_line_pointer = c;
   1414  1.1  christos 	      goto keep_going;
   1415  1.1  christos 	    }
   1416  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_USP)
   1417  1.1  christos 	    {
   1418  1.1  christos 	      char *start = input_line_pointer;
   1419  1.1  christos 	      char c = get_symbol_end ();
   1420  1.1  christos 
   1421  1.1  christos 	      if (strcasecmp (start, "usp") != 0)
   1422  1.1  christos 		{
   1423  1.1  christos 		  *input_line_pointer = c;
   1424  1.1  christos 		  input_line_pointer = hold;
   1425  1.1  christos 		  str = hold;
   1426  1.1  christos 		  goto error;
   1427  1.1  christos 		}
   1428  1.1  christos 	      *input_line_pointer = c;
   1429  1.1  christos 	      goto keep_going;
   1430  1.1  christos 	    }
   1431  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_SSP)
   1432  1.1  christos 	    {
   1433  1.1  christos 	      char *start = input_line_pointer;
   1434  1.1  christos 	      char c = get_symbol_end ();
   1435  1.1  christos 
   1436  1.1  christos 	      if (strcasecmp (start, "ssp") != 0)
   1437  1.1  christos 		{
   1438  1.1  christos 		  *input_line_pointer = c;
   1439  1.1  christos 		  input_line_pointer = hold;
   1440  1.1  christos 		  str = hold;
   1441  1.1  christos 		  goto error;
   1442  1.1  christos 		}
   1443  1.1  christos 	      *input_line_pointer = c;
   1444  1.1  christos 	      goto keep_going;
   1445  1.1  christos 	    }
   1446  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_MSP)
   1447  1.1  christos 	    {
   1448  1.1  christos 	      char *start = input_line_pointer;
   1449  1.1  christos 	      char c = get_symbol_end ();
   1450  1.1  christos 
   1451  1.1  christos 	      if (strcasecmp (start, "msp") != 0)
   1452  1.1  christos 		{
   1453  1.1  christos 		  *input_line_pointer = c;
   1454  1.1  christos 		  input_line_pointer = hold;
   1455  1.1  christos 		  str = hold;
   1456  1.1  christos 		  goto error;
   1457  1.1  christos 		}
   1458  1.1  christos 	      *input_line_pointer = c;
   1459  1.1  christos 	      goto keep_going;
   1460  1.1  christos 	    }
   1461  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_PC)
   1462  1.1  christos 	    {
   1463  1.1  christos 	      char *start = input_line_pointer;
   1464  1.1  christos 	      char c = get_symbol_end ();
   1465  1.1  christos 
   1466  1.1  christos 	      if (strcasecmp (start, "pc") != 0)
   1467  1.1  christos 		{
   1468  1.1  christos 		  *input_line_pointer = c;
   1469  1.1  christos 		  input_line_pointer = hold;
   1470  1.1  christos 		  str = hold;
   1471  1.1  christos 		  goto error;
   1472  1.1  christos 		}
   1473  1.1  christos 	      *input_line_pointer = c;
   1474  1.1  christos 	      goto keep_going;
   1475  1.1  christos 	    }
   1476  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_EPSW)
   1477  1.1  christos 	    {
   1478  1.1  christos 	      char *start = input_line_pointer;
   1479  1.1  christos 	      char c = get_symbol_end ();
   1480  1.1  christos 
   1481  1.1  christos 	      if (strcasecmp (start, "epsw") != 0)
   1482  1.1  christos 		{
   1483  1.1  christos 		  *input_line_pointer = c;
   1484  1.1  christos 		  input_line_pointer = hold;
   1485  1.1  christos 		  str = hold;
   1486  1.1  christos 		  goto error;
   1487  1.1  christos 		}
   1488  1.1  christos 	      *input_line_pointer = c;
   1489  1.1  christos 	      goto keep_going;
   1490  1.1  christos 	    }
   1491  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_PLUS)
   1492  1.1  christos 	    {
   1493  1.1  christos 	      if (*input_line_pointer != '+')
   1494  1.1  christos 		{
   1495  1.1  christos 		  input_line_pointer = hold;
   1496  1.1  christos 		  str = hold;
   1497  1.1  christos 		  goto error;
   1498  1.1  christos 		}
   1499  1.1  christos 	      input_line_pointer++;
   1500  1.1  christos 	      goto keep_going;
   1501  1.1  christos 	    }
   1502  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_PSW)
   1503  1.1  christos 	    {
   1504  1.1  christos 	      char *start = input_line_pointer;
   1505  1.1  christos 	      char c = get_symbol_end ();
   1506  1.1  christos 
   1507  1.1  christos 	      if (strcasecmp (start, "psw") != 0)
   1508  1.1  christos 		{
   1509  1.1  christos 		  *input_line_pointer = c;
   1510  1.1  christos 		  input_line_pointer = hold;
   1511  1.1  christos 		  str = hold;
   1512  1.1  christos 		  goto error;
   1513  1.1  christos 		}
   1514  1.1  christos 	      *input_line_pointer = c;
   1515  1.1  christos 	      goto keep_going;
   1516  1.1  christos 	    }
   1517  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_MDR)
   1518  1.1  christos 	    {
   1519  1.1  christos 	      char *start = input_line_pointer;
   1520  1.1  christos 	      char c = get_symbol_end ();
   1521  1.1  christos 
   1522  1.1  christos 	      if (strcasecmp (start, "mdr") != 0)
   1523  1.1  christos 		{
   1524  1.1  christos 		  *input_line_pointer = c;
   1525  1.1  christos 		  input_line_pointer = hold;
   1526  1.1  christos 		  str = hold;
   1527  1.1  christos 		  goto error;
   1528  1.1  christos 		}
   1529  1.1  christos 	      *input_line_pointer = c;
   1530  1.1  christos 	      goto keep_going;
   1531  1.1  christos 	    }
   1532  1.1  christos 	  else if (operand->flags & MN10300_OPERAND_REG_LIST)
   1533  1.1  christos 	    {
   1534  1.1  christos 	      unsigned int value = 0;
   1535  1.1  christos 	      if (*input_line_pointer != '[')
   1536  1.1  christos 		{
   1537  1.1  christos 		  input_line_pointer = hold;
   1538  1.1  christos 		  str = hold;
   1539  1.1  christos 		  goto error;
   1540  1.1  christos 		}
   1541  1.1  christos 
   1542  1.1  christos 	      /* Eat the '['.  */
   1543  1.1  christos 	      input_line_pointer++;
   1544  1.1  christos 
   1545  1.1  christos 	      /* We used to reject a null register list here; however,
   1546  1.1  christos 		 we accept it now so the compiler can emit "call"
   1547  1.1  christos 		 instructions for all calls to named functions.
   1548  1.1  christos 
   1549  1.1  christos 		 The linker can then fill in the appropriate bits for the
   1550  1.1  christos 		 register list and stack size or change the instruction
   1551  1.1  christos 		 into a "calls" if using "call" is not profitable.  */
   1552  1.1  christos 	      while (*input_line_pointer != ']')
   1553  1.1  christos 		{
   1554  1.1  christos 		  char *start;
   1555  1.1  christos 		  char c;
   1556  1.1  christos 
   1557  1.1  christos 		  if (*input_line_pointer == ',')
   1558  1.1  christos 		    input_line_pointer++;
   1559  1.1  christos 
   1560  1.1  christos 		  start = input_line_pointer;
   1561  1.1  christos 		  c = get_symbol_end ();
   1562  1.1  christos 
   1563  1.1  christos 		  if (strcasecmp (start, "d2") == 0)
   1564  1.1  christos 		    {
   1565  1.1  christos 		      value |= 0x80;
   1566  1.1  christos 		      *input_line_pointer = c;
   1567  1.1  christos 		    }
   1568  1.1  christos 		  else if (strcasecmp (start, "d3") == 0)
   1569  1.1  christos 		    {
   1570  1.1  christos 		      value |= 0x40;
   1571  1.1  christos 		      *input_line_pointer = c;
   1572  1.1  christos 		    }
   1573  1.1  christos 		  else if (strcasecmp (start, "a2") == 0)
   1574  1.1  christos 		    {
   1575  1.1  christos 		      value |= 0x20;
   1576  1.1  christos 		      *input_line_pointer = c;
   1577  1.1  christos 		    }
   1578  1.1  christos 		  else if (strcasecmp (start, "a3") == 0)
   1579  1.1  christos 		    {
   1580  1.1  christos 		      value |= 0x10;
   1581  1.1  christos 		      *input_line_pointer = c;
   1582  1.1  christos 		    }
   1583  1.1  christos 		  else if (strcasecmp (start, "other") == 0)
   1584  1.1  christos 		    {
   1585  1.1  christos 		      value |= 0x08;
   1586  1.1  christos 		      *input_line_pointer = c;
   1587  1.1  christos 		    }
   1588  1.1  christos 		  else if (HAVE_AM33
   1589  1.1  christos 			   && strcasecmp (start, "exreg0") == 0)
   1590  1.1  christos 		    {
   1591  1.1  christos 		      value |= 0x04;
   1592  1.1  christos 		      *input_line_pointer = c;
   1593  1.1  christos 		    }
   1594  1.1  christos 		  else if (HAVE_AM33
   1595  1.1  christos 			   && strcasecmp (start, "exreg1") == 0)
   1596  1.1  christos 		    {
   1597  1.1  christos 		      value |= 0x02;
   1598  1.1  christos 		      *input_line_pointer = c;
   1599  1.1  christos 		    }
   1600  1.1  christos 		  else if (HAVE_AM33
   1601  1.1  christos 			   && strcasecmp (start, "exother") == 0)
   1602  1.1  christos 		    {
   1603  1.1  christos 		      value |= 0x01;
   1604  1.1  christos 		      *input_line_pointer = c;
   1605  1.1  christos 		    }
   1606  1.1  christos 		  else if (HAVE_AM33
   1607  1.1  christos 			   && strcasecmp (start, "all") == 0)
   1608  1.1  christos 		    {
   1609  1.1  christos 		      value |= 0xff;
   1610  1.1  christos 		      *input_line_pointer = c;
   1611  1.1  christos 		    }
   1612  1.1  christos 		  else
   1613  1.1  christos 		    {
   1614  1.1  christos 		      input_line_pointer = hold;
   1615  1.1  christos 		      str = hold;
   1616  1.1  christos 		      goto error;
   1617  1.1  christos 		    }
   1618  1.1  christos 		}
   1619  1.1  christos 	      input_line_pointer++;
   1620  1.1  christos               mn10300_insert_operand (& insn, & extension, operand,
   1621  1.1  christos                                       value, NULL, 0, 0);
   1622  1.1  christos 	      goto keep_going;
   1623  1.1  christos 
   1624  1.1  christos 	    }
   1625  1.1  christos 	  else if (data_register_name (&ex))
   1626  1.1  christos 	    {
   1627  1.1  christos 	      input_line_pointer = hold;
   1628  1.1  christos 	      str = hold;
   1629  1.1  christos 	      goto error;
   1630  1.1  christos 	    }
   1631  1.1  christos 	  else if (address_register_name (&ex))
   1632  1.1  christos 	    {
   1633  1.1  christos 	      input_line_pointer = hold;
   1634  1.1  christos 	      str = hold;
   1635  1.1  christos 	      goto error;
   1636  1.1  christos 	    }
   1637  1.1  christos 	  else if (other_register_name (&ex))
   1638  1.1  christos 	    {
   1639  1.1  christos 	      input_line_pointer = hold;
   1640  1.1  christos 	      str = hold;
   1641  1.1  christos 	      goto error;
   1642  1.1  christos 	    }
   1643  1.1  christos 	  else if (HAVE_AM33 && r_register_name (&ex))
   1644  1.1  christos 	    {
   1645  1.1  christos 	      input_line_pointer = hold;
   1646  1.1  christos 	      str = hold;
   1647  1.1  christos 	      goto error;
   1648  1.1  christos 	    }
   1649  1.1  christos 	  else if (HAVE_AM33 && xr_register_name (&ex))
   1650  1.1  christos 	    {
   1651  1.1  christos 	      input_line_pointer = hold;
   1652  1.1  christos 	      str = hold;
   1653  1.1  christos 	      goto error;
   1654  1.1  christos 	    }
   1655  1.1  christos 	  else if (HAVE_AM33_2 && float_register_name (&ex))
   1656  1.1  christos 	    {
   1657  1.1  christos 	      input_line_pointer = hold;
   1658  1.1  christos 	      str = hold;
   1659  1.1  christos 	      goto error;
   1660  1.1  christos 	    }
   1661  1.1  christos 	  else if (HAVE_AM33_2 && double_register_name (&ex))
   1662  1.1  christos 	    {
   1663  1.1  christos 	      input_line_pointer = hold;
   1664  1.1  christos 	      str = hold;
   1665  1.1  christos 	      goto error;
   1666  1.1  christos 	    }
   1667  1.1  christos 	  else if (*str == ')' || *str == '(')
   1668  1.1  christos 	    {
   1669  1.1  christos 	      input_line_pointer = hold;
   1670  1.1  christos 	      str = hold;
   1671  1.1  christos 	      goto error;
   1672  1.1  christos 	    }
   1673  1.1  christos 	  else
   1674  1.1  christos 	    {
   1675  1.1  christos 	      expression (&ex);
   1676  1.1  christos 	    }
   1677  1.1  christos 
   1678  1.1  christos 	  switch (ex.X_op)
   1679  1.1  christos 	    {
   1680  1.1  christos 	    case O_illegal:
   1681  1.1  christos 	      errmsg = _("illegal operand");
   1682  1.1  christos 	      goto error;
   1683  1.1  christos 	    case O_absent:
   1684  1.1  christos 	      errmsg = _("missing operand");
   1685  1.1  christos 	      goto error;
   1686  1.1  christos 	    case O_register:
   1687  1.1  christos 	      {
   1688  1.1  christos 		int mask;
   1689  1.1  christos 
   1690  1.1  christos 		mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
   1691  1.1  christos 		if (HAVE_AM33)
   1692  1.1  christos 		  mask |= MN10300_OPERAND_RREG | MN10300_OPERAND_XRREG;
   1693  1.1  christos 		if (HAVE_AM33_2)
   1694  1.1  christos 		  mask |= MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG;
   1695  1.1  christos 		if ((operand->flags & mask) == 0)
   1696  1.1  christos 		  {
   1697  1.1  christos 		    input_line_pointer = hold;
   1698  1.1  christos 		    str = hold;
   1699  1.1  christos 		    goto error;
   1700  1.1  christos 		  }
   1701  1.1  christos 
   1702  1.1  christos 		if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
   1703  1.1  christos 		  extra_shift = 8;
   1704  1.1  christos 		else if (opcode->format == FMT_D2
   1705  1.1  christos 			 || opcode->format == FMT_D4
   1706  1.1  christos 			 || opcode->format == FMT_S2
   1707  1.1  christos 			 || opcode->format == FMT_S4
   1708  1.1  christos 			 || opcode->format == FMT_S6
   1709  1.1  christos 			 || opcode->format == FMT_D5)
   1710  1.1  christos 		  extra_shift = 16;
   1711  1.1  christos 		else if (opcode->format == FMT_D7)
   1712  1.1  christos 		  extra_shift = 8;
   1713  1.1  christos 		else if (opcode->format == FMT_D8 || opcode->format == FMT_D9)
   1714  1.1  christos 		  extra_shift = 8;
   1715  1.1  christos 		else
   1716  1.1  christos 		  extra_shift = 0;
   1717  1.1  christos 
   1718  1.1  christos 		mn10300_insert_operand (& insn, & extension, operand,
   1719  1.1  christos 					ex.X_add_number, NULL,
   1720  1.1  christos 					0, extra_shift);
   1721  1.1  christos 
   1722  1.1  christos 		/* And note the register number in the register array.  */
   1723  1.1  christos 		mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
   1724  1.1  christos 		break;
   1725  1.1  christos 	      }
   1726  1.1  christos 
   1727  1.1  christos 	    case O_constant:
   1728  1.1  christos 	      /* If this operand can be promoted, and it doesn't
   1729  1.1  christos 		 fit into the allocated bitfield for this insn,
   1730  1.1  christos 		 then promote it (ie this opcode does not match).  */
   1731  1.1  christos 	      if (operand->flags
   1732  1.1  christos 		  & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
   1733  1.1  christos 		  && !check_operand (operand, ex.X_add_number))
   1734  1.1  christos 		{
   1735  1.1  christos 		  input_line_pointer = hold;
   1736  1.1  christos 		  str = hold;
   1737  1.1  christos 		  goto error;
   1738  1.1  christos 		}
   1739  1.1  christos 
   1740  1.1  christos 	      mn10300_insert_operand (& insn, & extension, operand,
   1741  1.1  christos 				      ex.X_add_number, NULL, 0, 0);
   1742  1.1  christos 	      break;
   1743  1.1  christos 
   1744  1.1  christos 	    default:
   1745  1.1  christos 	      /* If this operand can be promoted, then this opcode didn't
   1746  1.1  christos 		 match since we can't know if it needed promotion!  */
   1747  1.1  christos 	      if (operand->flags & MN10300_OPERAND_PROMOTE)
   1748  1.1  christos 		{
   1749  1.1  christos 		  input_line_pointer = hold;
   1750  1.1  christos 		  str = hold;
   1751  1.1  christos 		  goto error;
   1752  1.1  christos 		}
   1753  1.1  christos 
   1754  1.1  christos 	      /* We need to generate a fixup for this expression.  */
   1755  1.1  christos 	      if (fc >= MAX_INSN_FIXUPS)
   1756  1.1  christos 		as_fatal (_("too many fixups"));
   1757  1.1  christos 	      fixups[fc].exp = ex;
   1758  1.1  christos 	      fixups[fc].opindex = *opindex_ptr;
   1759  1.1  christos 	      fixups[fc].reloc = BFD_RELOC_UNUSED;
   1760  1.1  christos 	      if (mn10300_check_fixup (& fixups[fc]))
   1761  1.1  christos 		goto error;
   1762  1.1  christos 	      ++fc;
   1763  1.1  christos 	      break;
   1764  1.1  christos 	    }
   1765  1.1  christos 
   1766  1.1  christos keep_going:
   1767  1.1  christos 	  str = input_line_pointer;
   1768  1.1  christos 	  input_line_pointer = hold;
   1769  1.1  christos 
   1770  1.1  christos 	  while (*str == ' ' || *str == ',')
   1771  1.1  christos 	    ++str;
   1772  1.1  christos 	}
   1773  1.1  christos 
   1774  1.1  christos       /* Make sure we used all the operands!  */
   1775  1.1  christos       if (*str != ',')
   1776  1.1  christos 	match = 1;
   1777  1.1  christos 
   1778  1.1  christos       /* If this instruction has registers that must not match, verify
   1779  1.1  christos 	 that they do indeed not match.  */
   1780  1.1  christos       if (opcode->no_match_operands)
   1781  1.1  christos 	{
   1782  1.1  christos 	  /* Look at each operand to see if it's marked.  */
   1783  1.1  christos 	  for (i = 0; i < MN10300_MAX_OPERANDS; i++)
   1784  1.1  christos 	    {
   1785  1.1  christos 	      if ((1 << i) & opcode->no_match_operands)
   1786  1.1  christos 		{
   1787  1.1  christos 		  int j;
   1788  1.1  christos 
   1789  1.1  christos 		  /* operand I is marked.  Check that it does not match any
   1790  1.1  christos 		     operands > I which are marked.  */
   1791  1.1  christos 		  for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
   1792  1.1  christos 		    {
   1793  1.1  christos 		      if (((1 << j) & opcode->no_match_operands)
   1794  1.1  christos 			  && mn10300_reg_operands[i] == mn10300_reg_operands[j])
   1795  1.1  christos 			{
   1796  1.1  christos 			  errmsg = _("Invalid register specification.");
   1797  1.1  christos 			  match = 0;
   1798  1.1  christos 			  goto error;
   1799  1.1  christos 			}
   1800  1.1  christos 		    }
   1801  1.1  christos 		}
   1802  1.1  christos 	    }
   1803  1.1  christos 	}
   1804  1.1  christos 
   1805  1.1  christos     error:
   1806  1.1  christos       if (match == 0)
   1807  1.1  christos 	{
   1808  1.1  christos 	  next_opcode = opcode + 1;
   1809  1.1  christos 	  if (!strcmp (next_opcode->name, opcode->name))
   1810  1.1  christos 	    {
   1811  1.1  christos 	      opcode = next_opcode;
   1812  1.1  christos 	      continue;
   1813  1.1  christos 	    }
   1814  1.1  christos 
   1815  1.1  christos 	  as_bad ("%s", errmsg);
   1816  1.1  christos 	  return;
   1817  1.1  christos 	}
   1818  1.1  christos       break;
   1819  1.1  christos     }
   1820  1.1  christos 
   1821  1.1  christos   while (ISSPACE (*str))
   1822  1.1  christos     ++str;
   1823  1.1  christos 
   1824  1.1  christos   if (*str != '\0')
   1825  1.1  christos     as_bad (_("junk at end of line: `%s'"), str);
   1826  1.1  christos 
   1827  1.1  christos   input_line_pointer = str;
   1828  1.1  christos 
   1829  1.1  christos   /* Determine the size of the instruction.  */
   1830  1.1  christos   if (opcode->format == FMT_S0)
   1831  1.1  christos     size = 1;
   1832  1.1  christos 
   1833  1.1  christos   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
   1834  1.1  christos     size = 2;
   1835  1.1  christos 
   1836  1.1  christos   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
   1837  1.1  christos     size = 3;
   1838  1.1  christos 
   1839  1.1  christos   if (opcode->format == FMT_D6)
   1840  1.1  christos     size = 3;
   1841  1.1  christos 
   1842  1.1  christos   if (opcode->format == FMT_D7 || opcode->format == FMT_D10)
   1843  1.1  christos     size = 4;
   1844  1.1  christos 
   1845  1.1  christos   if (opcode->format == FMT_D8)
   1846  1.1  christos     size = 6;
   1847  1.1  christos 
   1848  1.1  christos   if (opcode->format == FMT_D9)
   1849  1.1  christos     size = 7;
   1850  1.1  christos 
   1851  1.1  christos   if (opcode->format == FMT_S4)
   1852  1.1  christos     size = 5;
   1853  1.1  christos 
   1854  1.1  christos   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
   1855  1.1  christos     size = 7;
   1856  1.1  christos 
   1857  1.1  christos   if (opcode->format == FMT_D2)
   1858  1.1  christos     size = 4;
   1859  1.1  christos 
   1860  1.1  christos   if (opcode->format == FMT_D3)
   1861  1.1  christos     size = 5;
   1862  1.1  christos 
   1863  1.1  christos   if (opcode->format == FMT_D4)
   1864  1.1  christos     size = 6;
   1865  1.1  christos 
   1866  1.1  christos   if (relaxable && fc > 0)
   1867  1.1  christos     {
   1868  1.1  christos       /* On a 64-bit host the size of an 'int' is not the same
   1869  1.1  christos 	 as the size of a pointer, so we need a union to convert
   1870  1.1  christos 	 the opindex field of the fr_cgen structure into a char *
   1871  1.1  christos 	 so that it can be stored in the frag.  We do not have
   1872  1.1  christos 	 to worry about loosing accuracy as we are not going to
   1873  1.1  christos 	 be even close to the 32bit limit of the int.  */
   1874  1.1  christos       union
   1875  1.1  christos       {
   1876  1.1  christos 	int opindex;
   1877  1.1  christos 	char * ptr;
   1878  1.1  christos       }
   1879  1.1  christos       opindex_converter;
   1880  1.1  christos       int type;
   1881  1.1  christos 
   1882  1.1  christos       /* We want to anchor the line info to the previous frag (if
   1883  1.1  christos 	 there isn't one, create it), so that, when the insn is
   1884  1.1  christos 	 resized, we still get the right address for the beginning of
   1885  1.1  christos 	 the region.  */
   1886  1.1  christos       f = frag_more (0);
   1887  1.1  christos       dwarf2_emit_insn (0);
   1888  1.1  christos 
   1889  1.1  christos       /* bCC  */
   1890  1.1  christos       if (size == 2)
   1891  1.1  christos 	{
   1892  1.1  christos 	  /* Handle bra specially.  Basically treat it like jmp so
   1893  1.1  christos 	     that we automatically handle 8, 16 and 32 bit offsets
   1894  1.1  christos 	     correctly as well as jumps to an undefined address.
   1895  1.1  christos 
   1896  1.1  christos 	     It is also important to not treat it like other bCC
   1897  1.1  christos 	     instructions since the long forms of bra is different
   1898  1.1  christos 	     from other bCC instructions.  */
   1899  1.1  christos 	  if (opcode->opcode == 0xca00)
   1900  1.1  christos 	    type = 10;
   1901  1.1  christos 	  else
   1902  1.1  christos 	    type = 0;
   1903  1.1  christos 	}
   1904  1.1  christos       /* call  */
   1905  1.1  christos       else if (size == 5)
   1906  1.1  christos 	type = 6;
   1907  1.1  christos       /* calls  */
   1908  1.1  christos       else if (size == 4)
   1909  1.1  christos 	type = 8;
   1910  1.1  christos       /* jmp  */
   1911  1.1  christos       else if (size == 3 && opcode->opcode == 0xcc0000)
   1912  1.1  christos 	type = 10;
   1913  1.1  christos       else if (size == 3 && (opcode->opcode & 0xfff000) == 0xf8d000)
   1914  1.1  christos 	type = 13;
   1915  1.1  christos       /* bCC (uncommon cases)  */
   1916  1.1  christos       else
   1917  1.1  christos 	type = 3;
   1918  1.1  christos 
   1919  1.1  christos       opindex_converter.opindex = fixups[0].opindex;
   1920  1.1  christos       f = frag_var (rs_machine_dependent, 8, 8 - size, type,
   1921  1.1  christos 		    fixups[0].exp.X_add_symbol,
   1922  1.1  christos 		    fixups[0].exp.X_add_number,
   1923  1.1  christos 		    opindex_converter.ptr);
   1924  1.1  christos 
   1925  1.1  christos       /* This is pretty hokey.  We basically just care about the
   1926  1.1  christos 	 opcode, so we have to write out the first word big endian.
   1927  1.1  christos 
   1928  1.1  christos 	 The exception is "call", which has two operands that we
   1929  1.1  christos 	 care about.
   1930  1.1  christos 
   1931  1.1  christos 	 The first operand (the register list) happens to be in the
   1932  1.1  christos 	 first instruction word, and will be in the right place if
   1933  1.1  christos 	 we output the first word in big endian mode.
   1934  1.1  christos 
   1935  1.1  christos 	 The second operand (stack size) is in the extension word,
   1936  1.1  christos 	 and we want it to appear as the first character in the extension
   1937  1.1  christos 	 word (as it appears in memory).  Luckily, writing the extension
   1938  1.1  christos 	 word in big endian format will do what we want.  */
   1939  1.1  christos       number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
   1940  1.1  christos       if (size > 8)
   1941  1.1  christos 	{
   1942  1.1  christos 	  number_to_chars_bigendian (f + 4, extension, 4);
   1943  1.1  christos 	  number_to_chars_bigendian (f + 8, 0, size - 8);
   1944  1.1  christos 	}
   1945  1.1  christos       else if (size > 4)
   1946  1.1  christos 	number_to_chars_bigendian (f + 4, extension, size - 4);
   1947  1.1  christos     }
   1948  1.1  christos   else
   1949  1.1  christos     {
   1950  1.1  christos       /* Allocate space for the instruction.  */
   1951  1.1  christos       f = frag_more (size);
   1952  1.1  christos 
   1953  1.1  christos       /* Fill in bytes for the instruction.  Note that opcode fields
   1954  1.1  christos 	 are written big-endian, 16 & 32bit immediates are written
   1955  1.1  christos 	 little endian.  Egad.  */
   1956  1.1  christos       if (opcode->format == FMT_S0
   1957  1.1  christos 	  || opcode->format == FMT_S1
   1958  1.1  christos 	  || opcode->format == FMT_D0
   1959  1.1  christos 	  || opcode->format == FMT_D6
   1960  1.1  christos 	  || opcode->format == FMT_D7
   1961  1.1  christos 	  || opcode->format == FMT_D10
   1962  1.1  christos 	  || opcode->format == FMT_D1)
   1963  1.1  christos 	{
   1964  1.1  christos 	  number_to_chars_bigendian (f, insn, size);
   1965  1.1  christos 	}
   1966  1.1  christos       else if (opcode->format == FMT_S2
   1967  1.1  christos 	       && opcode->opcode != 0xdf0000
   1968  1.1  christos 	       && opcode->opcode != 0xde0000)
   1969  1.1  christos 	{
   1970  1.1  christos 	  /* A format S2 instruction that is _not_ "ret" and "retf".  */
   1971  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
   1972  1.1  christos 	  number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
   1973  1.1  christos 	}
   1974  1.1  christos       else if (opcode->format == FMT_S2)
   1975  1.1  christos 	{
   1976  1.1  christos 	  /* This must be a ret or retf, which is written entirely in
   1977  1.1  christos 	     big-endian format.  */
   1978  1.1  christos 	  number_to_chars_bigendian (f, insn, 3);
   1979  1.1  christos 	}
   1980  1.1  christos       else if (opcode->format == FMT_S4
   1981  1.1  christos 	       && opcode->opcode != 0xdc000000)
   1982  1.1  christos 	{
   1983  1.1  christos 	  /* This must be a format S4 "call" instruction.  What a pain.  */
   1984  1.1  christos 	  unsigned long temp = (insn >> 8) & 0xffff;
   1985  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
   1986  1.1  christos 	  number_to_chars_littleendian (f + 1, temp, 2);
   1987  1.1  christos 	  number_to_chars_bigendian (f + 3, insn & 0xff, 1);
   1988  1.1  christos 	  number_to_chars_bigendian (f + 4, extension & 0xff, 1);
   1989  1.1  christos 	}
   1990  1.1  christos       else if (opcode->format == FMT_S4)
   1991  1.1  christos 	{
   1992  1.1  christos 	  /* This must be a format S4 "jmp" instruction.  */
   1993  1.1  christos 	  unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
   1994  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
   1995  1.1  christos 	  number_to_chars_littleendian (f + 1, temp, 4);
   1996  1.1  christos 	}
   1997  1.1  christos       else if (opcode->format == FMT_S6)
   1998  1.1  christos 	{
   1999  1.1  christos 	  unsigned long temp = ((insn & 0xffffff) << 8)
   2000  1.1  christos 	    | ((extension >> 16) & 0xff);
   2001  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
   2002  1.1  christos 	  number_to_chars_littleendian (f + 1, temp, 4);
   2003  1.1  christos 	  number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
   2004  1.1  christos 	  number_to_chars_bigendian (f + 6, extension & 0xff, 1);
   2005  1.1  christos 	}
   2006  1.1  christos       else if (opcode->format == FMT_D2
   2007  1.1  christos 	       && opcode->opcode != 0xfaf80000
   2008  1.1  christos 	       && opcode->opcode != 0xfaf00000
   2009  1.1  christos 	       && opcode->opcode != 0xfaf40000)
   2010  1.1  christos 	{
   2011  1.1  christos 	  /* A format D2 instruction where the 16bit immediate is
   2012  1.1  christos 	     really a single 16bit value, not two 8bit values.  */
   2013  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
   2014  1.1  christos 	  number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
   2015  1.1  christos 	}
   2016  1.1  christos       else if (opcode->format == FMT_D2)
   2017  1.1  christos 	{
   2018  1.1  christos 	  /* A format D2 instruction where the 16bit immediate
   2019  1.1  christos 	     is really two 8bit immediates.  */
   2020  1.1  christos 	  number_to_chars_bigendian (f, insn, 4);
   2021  1.1  christos 	}
   2022  1.1  christos       else if (opcode->format == FMT_D3)
   2023  1.1  christos 	{
   2024  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
   2025  1.1  christos 	  number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
   2026  1.1  christos 	  number_to_chars_bigendian (f + 4, extension & 0xff, 1);
   2027  1.1  christos 	}
   2028  1.1  christos       else if (opcode->format == FMT_D4)
   2029  1.1  christos 	{
   2030  1.1  christos 	  unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
   2031  1.1  christos 
   2032  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
   2033  1.1  christos 	  number_to_chars_littleendian (f + 2, temp, 4);
   2034  1.1  christos 	}
   2035  1.1  christos       else if (opcode->format == FMT_D5)
   2036  1.1  christos 	{
   2037  1.1  christos 	  unsigned long temp = (((insn & 0xffff) << 16)
   2038  1.1  christos 				| ((extension >> 8) & 0xffff));
   2039  1.1  christos 
   2040  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
   2041  1.1  christos 	  number_to_chars_littleendian (f + 2, temp, 4);
   2042  1.1  christos 	  number_to_chars_bigendian (f + 6, extension & 0xff, 1);
   2043  1.1  christos 	}
   2044  1.1  christos       else if (opcode->format == FMT_D8)
   2045  1.1  christos 	{
   2046  1.1  christos 	  unsigned long temp = ((insn & 0xff) << 16) | (extension & 0xffff);
   2047  1.1  christos 
   2048  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
   2049  1.1  christos 	  number_to_chars_bigendian (f + 3, (temp & 0xff), 1);
   2050  1.1  christos 	  number_to_chars_littleendian (f + 4, temp >> 8, 2);
   2051  1.1  christos 	}
   2052  1.1  christos       else if (opcode->format == FMT_D9)
   2053  1.1  christos 	{
   2054  1.1  christos 	  unsigned long temp = ((insn & 0xff) << 24) | (extension & 0xffffff);
   2055  1.1  christos 
   2056  1.1  christos 	  number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
   2057  1.1  christos 	  number_to_chars_littleendian (f + 3, temp, 4);
   2058  1.1  christos 	}
   2059  1.1  christos 
   2060  1.1  christos       /* Create any fixups.  */
   2061  1.1  christos       for (i = 0; i < fc; i++)
   2062  1.1  christos 	{
   2063  1.1  christos 	  const struct mn10300_operand *operand;
   2064  1.1  christos 	  int reloc_size;
   2065  1.1  christos 
   2066  1.1  christos 	  operand = &mn10300_operands[fixups[i].opindex];
   2067  1.1  christos 	  if (fixups[i].reloc != BFD_RELOC_UNUSED
   2068  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_32_GOT_PCREL
   2069  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_32_GOTOFF
   2070  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_32_PLT_PCREL
   2071  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_GD
   2072  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_LD
   2073  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_LDO
   2074  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_GOTIE
   2075  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_IE
   2076  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_TLS_LE
   2077  1.1  christos 	      && fixups[i].reloc != BFD_RELOC_MN10300_GOT32)
   2078  1.1  christos 	    {
   2079  1.1  christos 	      reloc_howto_type *reloc_howto;
   2080  1.1  christos 	      int offset;
   2081  1.1  christos 
   2082  1.1  christos 	      reloc_howto = bfd_reloc_type_lookup (stdoutput,
   2083  1.1  christos 						   fixups[i].reloc);
   2084  1.1  christos 
   2085  1.1  christos 	      if (!reloc_howto)
   2086  1.1  christos 		abort ();
   2087  1.1  christos 
   2088  1.1  christos 	      reloc_size = bfd_get_reloc_size (reloc_howto);
   2089  1.1  christos 
   2090  1.1  christos 	      if (reloc_size < 1 || reloc_size > 4)
   2091  1.1  christos 		abort ();
   2092  1.1  christos 
   2093  1.1  christos 	      offset = 4 - size;
   2094  1.1  christos 	      fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
   2095  1.1  christos 			   reloc_size, &fixups[i].exp,
   2096  1.1  christos 			   reloc_howto->pc_relative,
   2097  1.1  christos 			   fixups[i].reloc);
   2098  1.1  christos 	    }
   2099  1.1  christos 	  else
   2100  1.1  christos 	    {
   2101  1.1  christos 	      int reloc, pcrel, offset;
   2102  1.1  christos 	      fixS *fixP;
   2103  1.1  christos 
   2104  1.1  christos 	      reloc = BFD_RELOC_NONE;
   2105  1.1  christos 	      if (fixups[i].reloc != BFD_RELOC_UNUSED)
   2106  1.1  christos 		reloc = fixups[i].reloc;
   2107  1.1  christos 	      /* How big is the reloc?  Remember SPLIT relocs are
   2108  1.1  christos 		 implicitly 32bits.  */
   2109  1.1  christos 	      if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
   2110  1.1  christos 		reloc_size = 32;
   2111  1.1  christos 	      else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
   2112  1.1  christos 		reloc_size = 24;
   2113  1.1  christos 	      else
   2114  1.1  christos 		reloc_size = operand->bits;
   2115  1.1  christos 
   2116  1.1  christos 	      /* Is the reloc pc-relative?  */
   2117  1.1  christos 	      pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
   2118  1.1  christos 	      if (reloc != BFD_RELOC_NONE)
   2119  1.1  christos 		pcrel = bfd_reloc_type_lookup (stdoutput, reloc)->pc_relative;
   2120  1.1  christos 
   2121  1.1  christos 	      offset = size - (reloc_size + operand->shift) / 8;
   2122  1.1  christos 
   2123  1.1  christos 	      /* Choose a proper BFD relocation type.  */
   2124  1.1  christos 	      if (reloc != BFD_RELOC_NONE)
   2125  1.1  christos 		;
   2126  1.1  christos 	      else if (pcrel)
   2127  1.1  christos 		{
   2128  1.1  christos 		  if (reloc_size == 32)
   2129  1.1  christos 		    reloc = BFD_RELOC_32_PCREL;
   2130  1.1  christos 		  else if (reloc_size == 16)
   2131  1.1  christos 		    reloc = BFD_RELOC_16_PCREL;
   2132  1.1  christos 		  else if (reloc_size == 8)
   2133  1.1  christos 		    reloc = BFD_RELOC_8_PCREL;
   2134  1.1  christos 		  else
   2135  1.1  christos 		    abort ();
   2136  1.1  christos 		}
   2137  1.1  christos 	      else
   2138  1.1  christos 		{
   2139  1.1  christos 		  if (reloc_size == 32)
   2140  1.1  christos 		    reloc = BFD_RELOC_32;
   2141  1.1  christos 		  else if (reloc_size == 16)
   2142  1.1  christos 		    reloc = BFD_RELOC_16;
   2143  1.1  christos 		  else if (reloc_size == 8)
   2144  1.1  christos 		    reloc = BFD_RELOC_8;
   2145  1.1  christos 		  else
   2146  1.1  christos 		    abort ();
   2147  1.1  christos 		}
   2148  1.1  christos 
   2149  1.1  christos 	      fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
   2150  1.1  christos 				  reloc_size / 8, &fixups[i].exp, pcrel,
   2151  1.1  christos 				  ((bfd_reloc_code_real_type) reloc));
   2152  1.1  christos 
   2153  1.1  christos 	      if (pcrel)
   2154  1.1  christos 		fixP->fx_offset += offset;
   2155  1.1  christos 	    }
   2156  1.1  christos 	}
   2157  1.1  christos 
   2158  1.1  christos       dwarf2_emit_insn (size);
   2159  1.1  christos     }
   2160  1.1  christos 
   2161  1.1  christos   /* Label this frag as one that contains instructions.  */
   2162  1.1  christos   frag_now->tc_frag_data = TRUE;
   2163  1.1  christos }
   2164  1.1  christos 
   2165  1.1  christos /* If while processing a fixup, a reloc really needs to be created
   2166  1.1  christos    then it is done here.  */
   2167  1.1  christos 
   2168  1.1  christos arelent **
   2169  1.1  christos tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
   2170  1.1  christos {
   2171  1.1  christos   static arelent * no_relocs = NULL;
   2172  1.1  christos   static arelent * relocs[MAX_RELOC_EXPANSION + 1];
   2173  1.1  christos   arelent *reloc;
   2174  1.1  christos 
   2175  1.1  christos   reloc = xmalloc (sizeof (arelent));
   2176  1.1  christos 
   2177  1.1  christos   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
   2178  1.1  christos   if (reloc->howto == NULL)
   2179  1.1  christos     {
   2180  1.1  christos       as_bad_where (fixp->fx_file, fixp->fx_line,
   2181  1.1  christos 		    _("reloc %d not supported by object file format"),
   2182  1.1  christos 		    (int) fixp->fx_r_type);
   2183  1.1  christos       free (reloc);
   2184  1.1  christos       return & no_relocs;
   2185  1.1  christos     }
   2186  1.1  christos 
   2187  1.1  christos   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   2188  1.1  christos   relocs[0] = reloc;
   2189  1.1  christos   relocs[1] = NULL;
   2190  1.1  christos 
   2191  1.1  christos   if (fixp->fx_subsy
   2192  1.1  christos       && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
   2193  1.1  christos     {
   2194  1.1  christos       fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
   2195  1.1  christos       fixp->fx_subsy = NULL;
   2196  1.1  christos     }
   2197  1.1  christos 
   2198  1.1  christos   if (fixp->fx_addsy && fixp->fx_subsy)
   2199  1.1  christos     {
   2200  1.1  christos       asection *asec, *ssec;
   2201  1.1  christos 
   2202  1.1  christos       asec = S_GET_SEGMENT (fixp->fx_addsy);
   2203  1.1  christos       ssec = S_GET_SEGMENT (fixp->fx_subsy);
   2204  1.1  christos 
   2205  1.1  christos       /* If we have a difference between two (non-absolute) symbols we must
   2206  1.1  christos 	 generate two relocs (one for each symbol) and allow the linker to
   2207  1.1  christos 	 resolve them - relaxation may change the distances between symbols,
   2208  1.1  christos 	 even local symbols defined in the same section.  */
   2209  1.1  christos       if (ssec != absolute_section || asec != absolute_section)
   2210  1.1  christos 	{
   2211  1.1  christos 	  arelent * reloc2 = xmalloc (sizeof * reloc);
   2212  1.1  christos 
   2213  1.1  christos 	  relocs[0] = reloc2;
   2214  1.1  christos 	  relocs[1] = reloc;
   2215  1.1  christos 
   2216  1.1  christos 	  reloc2->address = reloc->address;
   2217  1.1  christos 	  reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_MN10300_SYM_DIFF);
   2218  1.1  christos 	  reloc2->addend = - S_GET_VALUE (fixp->fx_subsy);
   2219  1.1  christos 	  reloc2->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
   2220  1.1  christos 	  *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
   2221  1.1  christos 
   2222  1.1  christos 	  reloc->addend = fixp->fx_offset;
   2223  1.1  christos 	  if (asec == absolute_section)
   2224  1.1  christos 	    {
   2225  1.1  christos 	      reloc->addend += S_GET_VALUE (fixp->fx_addsy);
   2226  1.1  christos 	      reloc->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   2227  1.1  christos 	    }
   2228  1.1  christos 	  else
   2229  1.1  christos 	    {
   2230  1.1  christos 	      reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
   2231  1.1  christos 	      *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   2232  1.1  christos 	    }
   2233  1.1  christos 
   2234  1.1  christos 	  fixp->fx_pcrel = 0;
   2235  1.1  christos 	  fixp->fx_done = 1;
   2236  1.1  christos 	  return relocs;
   2237  1.1  christos 	}
   2238  1.1  christos       else
   2239  1.1  christos 	{
   2240  1.1  christos 	  char *fixpos = fixp->fx_where + fixp->fx_frag->fr_literal;
   2241  1.1  christos 
   2242  1.1  christos 	  reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
   2243  1.1  christos 			   - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
   2244  1.1  christos 
   2245  1.1  christos 	  switch (fixp->fx_r_type)
   2246  1.1  christos 	    {
   2247  1.1  christos 	    case BFD_RELOC_8:
   2248  1.1  christos 	      md_number_to_chars (fixpos, reloc->addend, 1);
   2249  1.1  christos 	      break;
   2250  1.1  christos 
   2251  1.1  christos 	    case BFD_RELOC_16:
   2252  1.1  christos 	      md_number_to_chars (fixpos, reloc->addend, 2);
   2253  1.1  christos 	      break;
   2254  1.1  christos 
   2255  1.1  christos 	    case BFD_RELOC_24:
   2256  1.1  christos 	      md_number_to_chars (fixpos, reloc->addend, 3);
   2257  1.1  christos 	      break;
   2258  1.1  christos 
   2259  1.1  christos 	    case BFD_RELOC_32:
   2260  1.1  christos 	      md_number_to_chars (fixpos, reloc->addend, 4);
   2261  1.1  christos 	      break;
   2262  1.1  christos 
   2263  1.1  christos 	    default:
   2264  1.1  christos 	      reloc->sym_ptr_ptr
   2265  1.1  christos 		= (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
   2266  1.1  christos 	      return relocs;
   2267  1.1  christos 	    }
   2268  1.1  christos 
   2269  1.1  christos 	  free (reloc);
   2270  1.1  christos 	  return & no_relocs;
   2271  1.1  christos 	}
   2272  1.1  christos     }
   2273  1.1  christos   else
   2274  1.1  christos     {
   2275  1.1  christos       reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
   2276  1.1  christos       *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   2277  1.1  christos       reloc->addend = fixp->fx_offset;
   2278  1.1  christos     }
   2279  1.1  christos   return relocs;
   2280  1.1  christos }
   2281  1.1  christos 
   2282  1.1  christos /* Returns true iff the symbol attached to the frag is at a known location
   2283  1.1  christos    in the given section, (and hence the relocation to it can be relaxed by
   2284  1.1  christos    the assembler).  */
   2285  1.1  christos static inline bfd_boolean
   2286  1.1  christos has_known_symbol_location (fragS * fragp, asection * sec)
   2287  1.1  christos {
   2288  1.1  christos   symbolS * sym = fragp->fr_symbol;
   2289  1.1  christos 
   2290  1.1  christos   return sym != NULL
   2291  1.1  christos     && S_IS_DEFINED (sym)
   2292  1.1  christos     && ! S_IS_WEAK (sym)
   2293  1.1  christos     && S_GET_SEGMENT (sym) == sec;
   2294  1.1  christos }
   2295  1.1  christos 
   2296  1.1  christos int
   2297  1.1  christos md_estimate_size_before_relax (fragS *fragp, asection *seg)
   2298  1.1  christos {
   2299  1.1  christos   if (fragp->fr_subtype == 6
   2300  1.1  christos       && ! has_known_symbol_location (fragp, seg))
   2301  1.1  christos     fragp->fr_subtype = 7;
   2302  1.1  christos   else if (fragp->fr_subtype == 8
   2303  1.1  christos 	   && ! has_known_symbol_location (fragp, seg))
   2304  1.1  christos     fragp->fr_subtype = 9;
   2305  1.1  christos   else if (fragp->fr_subtype == 10
   2306  1.1  christos 	   && ! has_known_symbol_location (fragp, seg))
   2307  1.1  christos     fragp->fr_subtype = 12;
   2308  1.1  christos 
   2309  1.1  christos   if (fragp->fr_subtype == 13)
   2310  1.1  christos     return 3;
   2311  1.1  christos 
   2312  1.1  christos   if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
   2313  1.1  christos     abort ();
   2314  1.1  christos 
   2315  1.1  christos   return md_relax_table[fragp->fr_subtype].rlx_length;
   2316  1.1  christos }
   2317  1.1  christos 
   2318  1.1  christos long
   2319  1.1  christos md_pcrel_from (fixS *fixp)
   2320  1.1  christos {
   2321  1.1  christos   if (fixp->fx_addsy != (symbolS *) NULL
   2322  1.1  christos       && (!S_IS_DEFINED (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
   2323  1.1  christos     /* The symbol is undefined or weak.  Let the linker figure it out.  */
   2324  1.1  christos     return 0;
   2325  1.1  christos 
   2326  1.1  christos   return fixp->fx_frag->fr_address + fixp->fx_where;
   2327  1.1  christos }
   2328  1.1  christos 
   2329  1.1  christos void
   2330  1.1  christos md_apply_fix (fixS * fixP, valueT * valP, segT seg)
   2331  1.1  christos {
   2332  1.1  christos   char * fixpos = fixP->fx_where + fixP->fx_frag->fr_literal;
   2333  1.1  christos   int size = 0;
   2334  1.1  christos   int value = (int) * valP;
   2335  1.1  christos 
   2336  1.1  christos   gas_assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
   2337  1.1  christos 
   2338  1.1  christos   /* This should never happen.  */
   2339  1.1  christos   if (seg->flags & SEC_ALLOC)
   2340  1.1  christos     abort ();
   2341  1.1  christos 
   2342  1.1  christos   /* The value we are passed in *valuep includes the symbol values.
   2343  1.1  christos      If we are doing this relocation the code in write.c is going to
   2344  1.1  christos      call bfd_install_relocation, which is also going to use the symbol
   2345  1.1  christos      value.  That means that if the reloc is fully resolved we want to
   2346  1.1  christos      use *valuep since bfd_install_relocation is not being used.
   2347  1.1  christos 
   2348  1.1  christos      However, if the reloc is not fully resolved we do not want to use
   2349  1.1  christos      *valuep, and must use fx_offset instead.  However, if the reloc
   2350  1.1  christos      is PC relative, we do want to use *valuep since it includes the
   2351  1.1  christos      result of md_pcrel_from.  */
   2352  1.1  christos   if (fixP->fx_addsy != NULL && ! fixP->fx_pcrel)
   2353  1.1  christos     value = fixP->fx_offset;
   2354  1.1  christos 
   2355  1.1  christos   /* If the fix is relative to a symbol which is not defined, or not
   2356  1.1  christos      in the same segment as the fix, we cannot resolve it here.  */
   2357  1.1  christos   if (fixP->fx_addsy != NULL
   2358  1.1  christos       && (! S_IS_DEFINED (fixP->fx_addsy)
   2359  1.1  christos 	  || (S_GET_SEGMENT (fixP->fx_addsy) != seg)))
   2360  1.1  christos     {
   2361  1.1  christos       fixP->fx_done = 0;
   2362  1.1  christos       return;
   2363  1.1  christos     }
   2364  1.1  christos 
   2365  1.1  christos   switch (fixP->fx_r_type)
   2366  1.1  christos     {
   2367  1.1  christos     case BFD_RELOC_8:
   2368  1.1  christos     case BFD_RELOC_8_PCREL:
   2369  1.1  christos       size = 1;
   2370  1.1  christos       break;
   2371  1.1  christos 
   2372  1.1  christos     case BFD_RELOC_16:
   2373  1.1  christos     case BFD_RELOC_16_PCREL:
   2374  1.1  christos       size = 2;
   2375  1.1  christos       break;
   2376  1.1  christos 
   2377  1.1  christos     case BFD_RELOC_32:
   2378  1.1  christos     case BFD_RELOC_32_PCREL:
   2379  1.1  christos       size = 4;
   2380  1.1  christos       break;
   2381  1.1  christos 
   2382  1.1  christos     case BFD_RELOC_VTABLE_INHERIT:
   2383  1.1  christos     case BFD_RELOC_VTABLE_ENTRY:
   2384  1.1  christos       fixP->fx_done = 0;
   2385  1.1  christos       return;
   2386  1.1  christos 
   2387  1.1  christos     case BFD_RELOC_MN10300_ALIGN:
   2388  1.1  christos       fixP->fx_done = 1;
   2389  1.1  christos       return;
   2390  1.1  christos 
   2391  1.1  christos     case BFD_RELOC_NONE:
   2392  1.1  christos     default:
   2393  1.1  christos       as_bad_where (fixP->fx_file, fixP->fx_line,
   2394  1.1  christos                    _("Bad relocation fixup type (%d)"), fixP->fx_r_type);
   2395  1.1  christos     }
   2396  1.1  christos 
   2397  1.1  christos   md_number_to_chars (fixpos, value, size);
   2398  1.1  christos 
   2399  1.1  christos   /* If a symbol remains, pass the fixup, as a reloc, onto the linker.  */
   2400  1.1  christos   if (fixP->fx_addsy == NULL)
   2401  1.1  christos     fixP->fx_done = 1;
   2402  1.1  christos }
   2403  1.1  christos 
   2404  1.1  christos /* Return zero if the fixup in fixp should be left alone and not
   2405  1.1  christos    adjusted.  */
   2406  1.1  christos 
   2407  1.1  christos bfd_boolean
   2408  1.1  christos mn10300_fix_adjustable (struct fix *fixp)
   2409  1.1  christos {
   2410  1.1  christos   if (fixp->fx_pcrel)
   2411  1.1  christos     {
   2412  1.1  christos       if (TC_FORCE_RELOCATION_LOCAL (fixp))
   2413  1.1  christos 	return FALSE;
   2414  1.1  christos     }
   2415  1.1  christos   /* Non-relative relocs can (and must) be adjusted if they do
   2416  1.1  christos      not meet the criteria below, or the generic criteria.  */
   2417  1.1  christos   else if (TC_FORCE_RELOCATION (fixp))
   2418  1.1  christos     return FALSE;
   2419  1.1  christos 
   2420  1.1  christos   /* Do not adjust relocations involving symbols in code sections,
   2421  1.1  christos      because it breaks linker relaxations.  This could be fixed in the
   2422  1.1  christos      linker, but this fix is simpler, and it pretty much only affects
   2423  1.1  christos      object size a little bit.  */
   2424  1.1  christos   if (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_CODE)
   2425  1.1  christos     return FALSE;
   2426  1.1  christos 
   2427  1.1  christos   /* Likewise, do not adjust symbols that won't be merged, or debug
   2428  1.1  christos      symbols, because they too break relaxation.  We do want to adjust
   2429  1.1  christos      other mergable symbols, like .rodata, because code relaxations
   2430  1.1  christos      need section-relative symbols to properly relax them.  */
   2431  1.1  christos   if (! (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE))
   2432  1.1  christos     return FALSE;
   2433  1.1  christos 
   2434  1.1  christos   if (strncmp (S_GET_SEGMENT (fixp->fx_addsy)->name, ".debug", 6) == 0)
   2435  1.1  christos     return FALSE;
   2436  1.1  christos 
   2437  1.1  christos   return TRUE;
   2438  1.1  christos }
   2439  1.1  christos 
   2440  1.1  christos static void
   2441  1.1  christos set_arch_mach (int mach)
   2442  1.1  christos {
   2443  1.1  christos   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
   2444  1.1  christos     as_warn (_("could not set architecture and machine"));
   2445  1.1  christos 
   2446  1.1  christos   current_machine = mach;
   2447  1.1  christos }
   2448  1.1  christos 
   2449  1.1  christos static inline char *
   2450  1.1  christos mn10300_end_of_match (char *cont, char *what)
   2451  1.1  christos {
   2452  1.1  christos   int len = strlen (what);
   2453  1.1  christos 
   2454  1.1  christos   if (strncmp (cont, what, strlen (what)) == 0
   2455  1.1  christos       && ! is_part_of_name (cont[len]))
   2456  1.1  christos     return cont + len;
   2457  1.1  christos 
   2458  1.1  christos   return NULL;
   2459  1.1  christos }
   2460  1.1  christos 
   2461  1.1  christos int
   2462  1.1  christos mn10300_parse_name (char const *name,
   2463  1.1  christos 		    expressionS *exprP,
   2464  1.1  christos 		    enum expr_mode mode,
   2465  1.1  christos 		    char *nextcharP)
   2466  1.1  christos {
   2467  1.1  christos   char *next = input_line_pointer;
   2468  1.1  christos   char *next_end;
   2469  1.1  christos   int reloc_type;
   2470  1.1  christos   segT segment;
   2471  1.1  christos 
   2472  1.1  christos   exprP->X_op_symbol = NULL;
   2473  1.1  christos 
   2474  1.1  christos   if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
   2475  1.1  christos     {
   2476  1.1  christos       if (! GOT_symbol)
   2477  1.1  christos 	GOT_symbol = symbol_find_or_make (name);
   2478  1.1  christos 
   2479  1.1  christos       exprP->X_add_symbol = GOT_symbol;
   2480  1.1  christos     no_suffix:
   2481  1.1  christos       /* If we have an absolute symbol or a reg,
   2482  1.1  christos 	 then we know its value now.  */
   2483  1.1  christos       segment = S_GET_SEGMENT (exprP->X_add_symbol);
   2484  1.1  christos       if (mode != expr_defer && segment == absolute_section)
   2485  1.1  christos 	{
   2486  1.1  christos 	  exprP->X_op = O_constant;
   2487  1.1  christos 	  exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
   2488  1.1  christos 	  exprP->X_add_symbol = NULL;
   2489  1.1  christos 	}
   2490  1.1  christos       else if (mode != expr_defer && segment == reg_section)
   2491  1.1  christos 	{
   2492  1.1  christos 	  exprP->X_op = O_register;
   2493  1.1  christos 	  exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
   2494  1.1  christos 	  exprP->X_add_symbol = NULL;
   2495  1.1  christos 	}
   2496  1.1  christos       else
   2497  1.1  christos 	{
   2498  1.1  christos 	  exprP->X_op = O_symbol;
   2499  1.1  christos 	  exprP->X_add_number = 0;
   2500  1.1  christos 	}
   2501  1.1  christos 
   2502  1.1  christos       return 1;
   2503  1.1  christos     }
   2504  1.1  christos 
   2505  1.1  christos   exprP->X_add_symbol = symbol_find_or_make (name);
   2506  1.1  christos 
   2507  1.1  christos   if (*nextcharP != '@')
   2508  1.1  christos     goto no_suffix;
   2509  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "GOTOFF")))
   2510  1.1  christos     reloc_type = BFD_RELOC_32_GOTOFF;
   2511  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "GOT")))
   2512  1.1  christos     reloc_type = BFD_RELOC_MN10300_GOT32;
   2513  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "PLT")))
   2514  1.1  christos     reloc_type = BFD_RELOC_32_PLT_PCREL;
   2515  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "tlsgd")))
   2516  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_GD;
   2517  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "tlsldm")))
   2518  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_LD;
   2519  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "dtpoff")))
   2520  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_LDO;
   2521  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "gotntpoff")))
   2522  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_GOTIE;
   2523  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "indntpoff")))
   2524  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_IE;
   2525  1.1  christos   else if ((next_end = mn10300_end_of_match (next + 1, "tpoff")))
   2526  1.1  christos     reloc_type = BFD_RELOC_MN10300_TLS_LE;
   2527  1.1  christos   else
   2528  1.1  christos     goto no_suffix;
   2529  1.1  christos 
   2530  1.1  christos   *input_line_pointer = *nextcharP;
   2531  1.1  christos   input_line_pointer = next_end;
   2532  1.1  christos   *nextcharP = *input_line_pointer;
   2533  1.1  christos   *input_line_pointer = '\0';
   2534  1.1  christos 
   2535  1.1  christos   exprP->X_op = O_PIC_reloc;
   2536  1.1  christos   exprP->X_add_number = 0;
   2537  1.1  christos   exprP->X_md = reloc_type;
   2538  1.1  christos 
   2539  1.1  christos   return 1;
   2540  1.1  christos }
   2541  1.1  christos 
   2542  1.1  christos /* The target specific pseudo-ops which we support.  */
   2543  1.1  christos const pseudo_typeS md_pseudo_table[] =
   2544  1.1  christos {
   2545  1.1  christos   { "am30",	set_arch_mach,	AM30 },
   2546  1.1  christos   { "am33",	set_arch_mach,	AM33 },
   2547  1.1  christos   { "am33_2",	set_arch_mach,	AM33_2 },
   2548  1.1  christos   { "mn10300",	set_arch_mach,	MN103 },
   2549  1.1  christos   {NULL, 0, 0}
   2550  1.1  christos };
   2551  1.1  christos 
   2552  1.1  christos /* Returns FALSE if there is some mn10300 specific reason why the
   2553  1.1  christos    subtraction of two same-section symbols cannot be computed by
   2554  1.1  christos    the assembler.  */
   2555  1.1  christos 
   2556  1.1  christos bfd_boolean
   2557  1.1  christos mn10300_allow_local_subtract (expressionS * left, expressionS * right, segT section)
   2558  1.1  christos {
   2559  1.1  christos   bfd_boolean result;
   2560  1.1  christos   fragS * left_frag;
   2561  1.1  christos   fragS * right_frag;
   2562  1.1  christos   fragS * frag;
   2563  1.1  christos 
   2564  1.1  christos   /* If we are not performing linker relaxation then we have nothing
   2565  1.1  christos      to worry about.  */
   2566  1.1  christos   if (linkrelax == 0)
   2567  1.1  christos     return TRUE;
   2568  1.1  christos 
   2569  1.1  christos   /* If the symbols are not in a code section then they are OK.  */
   2570  1.1  christos   if ((section->flags & SEC_CODE) == 0)
   2571  1.1  christos     return TRUE;
   2572  1.1  christos 
   2573  1.1  christos   /* Otherwise we have to scan the fragments between the two symbols.
   2574  1.1  christos      If any instructions are found then we have to assume that linker
   2575  1.1  christos      relaxation may change their size and so we must delay resolving
   2576  1.1  christos      the subtraction until the final link.  */
   2577  1.1  christos   left_frag = symbol_get_frag (left->X_add_symbol);
   2578  1.1  christos   right_frag = symbol_get_frag (right->X_add_symbol);
   2579  1.1  christos 
   2580  1.1  christos   if (left_frag == right_frag)
   2581  1.1  christos     return ! left_frag->tc_frag_data;
   2582  1.1  christos 
   2583  1.1  christos   result = TRUE;
   2584  1.1  christos   for (frag = left_frag; frag != NULL; frag = frag->fr_next)
   2585  1.1  christos     {
   2586  1.1  christos       if (frag->tc_frag_data)
   2587  1.1  christos 	result = FALSE;
   2588  1.1  christos       if (frag == right_frag)
   2589  1.1  christos 	break;
   2590  1.1  christos     }
   2591  1.1  christos 
   2592  1.1  christos   if (frag == NULL)
   2593  1.1  christos     for (frag = right_frag; frag != NULL; frag = frag->fr_next)
   2594  1.1  christos       {
   2595  1.1  christos 	if (frag->tc_frag_data)
   2596  1.1  christos 	  result = FALSE;
   2597  1.1  christos 	if (frag == left_frag)
   2598  1.1  christos 	  break;
   2599  1.1  christos       }
   2600  1.1  christos 
   2601  1.1  christos   if (frag == NULL)
   2602  1.1  christos     /* The two symbols are on disjoint fragment chains
   2603  1.1  christos        - we cannot possibly compute their difference.  */
   2604  1.1  christos     return FALSE;
   2605  1.1  christos 
   2606  1.1  christos   return result;
   2607  1.1  christos }
   2608  1.1  christos 
   2609  1.1  christos /* When relaxing, we need to output a reloc for any .align directive
   2610  1.1  christos    that requests alignment to a two byte boundary or larger.  */
   2611  1.1  christos 
   2612  1.1  christos void
   2613  1.1  christos mn10300_handle_align (fragS *frag)
   2614  1.1  christos {
   2615  1.1  christos   if (linkrelax
   2616  1.1  christos       && (frag->fr_type == rs_align
   2617  1.1  christos 	  || frag->fr_type == rs_align_code)
   2618  1.1  christos       && frag->fr_address + frag->fr_fix > 0
   2619  1.1  christos       && frag->fr_offset > 1
   2620  1.1  christos       && now_seg != bss_section
   2621  1.1  christos       /* Do not create relocs for the merging sections - such
   2622  1.1  christos 	 relocs will prevent the contents from being merged.  */
   2623  1.1  christos       && (bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE) == 0)
   2624  1.1  christos     /* Create a new fixup to record the alignment request.  The symbol is
   2625  1.1  christos        irrelevent but must be present so we use the absolute section symbol.
   2626  1.1  christos        The offset from the symbol is used to record the power-of-two alignment
   2627  1.1  christos        value.  The size is set to 0 because the frag may already be aligned,
   2628  1.1  christos        thus causing cvt_frag_to_fill to reduce the size of the frag to zero.  */
   2629  1.1  christos     fix_new (frag, frag->fr_fix, 0, & abs_symbol, frag->fr_offset, FALSE,
   2630  1.1  christos 	     BFD_RELOC_MN10300_ALIGN);
   2631  1.1  christos }
   2632  1.1  christos 
   2633  1.1  christos bfd_boolean
   2634  1.1  christos mn10300_force_relocation (struct fix * fixp)
   2635  1.1  christos {
   2636  1.1  christos   if (linkrelax
   2637  1.1  christos       && (fixp->fx_pcrel
   2638  1.1  christos 	  || fixp->fx_r_type == BFD_RELOC_MN10300_ALIGN))
   2639  1.1  christos     return TRUE;
   2640                
   2641                  return generic_force_reloc (fixp);
   2642                }
   2643