Home | History | Annotate | Line # | Download | only in config
tc-cr16.c revision 1.1
      1  1.1  skrll /* tc-cr16.c -- Assembler code for the CR16 CPU core.
      2  1.1  skrll    Copyright 2007 Free Software Foundation, Inc.
      3  1.1  skrll 
      4  1.1  skrll    Contributed by M R Swami Reddy <MR.Swami.Reddy (at) nsc.com>
      5  1.1  skrll 
      6  1.1  skrll    This file is part of GAS, the GNU Assembler.
      7  1.1  skrll 
      8  1.1  skrll    GAS is free software; you can redistribute it and/or modify
      9  1.1  skrll    it under the terms of the GNU General Public License as published by
     10  1.1  skrll    the Free Software Foundation; either version 3, or (at your option)
     11  1.1  skrll    any later version.
     12  1.1  skrll 
     13  1.1  skrll    GAS is distributed in the hope that it will be useful,
     14  1.1  skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  1.1  skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  1.1  skrll    GNU General Public License for more details.
     17  1.1  skrll 
     18  1.1  skrll    You should have received a copy of the GNU General Public License
     19  1.1  skrll    along with GAS; see the file COPYING.  If not, write to the
     20  1.1  skrll    Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
     21  1.1  skrll    MA 02110-1301, USA.  */
     22  1.1  skrll 
     23  1.1  skrll #include "as.h"
     24  1.1  skrll #include "safe-ctype.h"
     25  1.1  skrll #include "dwarf2dbg.h"
     26  1.1  skrll #include "opcode/cr16.h"
     27  1.1  skrll #include "elf/cr16.h"
     28  1.1  skrll 
     29  1.1  skrll 
     30  1.1  skrll /* Word is considered here as a 16-bit unsigned short int.  */
     31  1.1  skrll #define WORD_SHIFT  16
     32  1.1  skrll 
     33  1.1  skrll /* Register is 2-byte size.  */
     34  1.1  skrll #define REG_SIZE   2
     35  1.1  skrll 
     36  1.1  skrll /* Maximum size of a single instruction (in words).  */
     37  1.1  skrll #define INSN_MAX_SIZE   3
     38  1.1  skrll 
     39  1.1  skrll /* Maximum bits which may be set in a `mask16' operand.  */
     40  1.1  skrll #define MAX_REGS_IN_MASK16  8
     41  1.1  skrll 
     42  1.1  skrll /* Assign a number NUM, shifted by SHIFT bytes, into a location
     43  1.1  skrll    pointed by index BYTE of array 'output_opcode'.  */
     44  1.1  skrll #define CR16_PRINT(BYTE, NUM, SHIFT)   output_opcode[BYTE] |= (NUM << SHIFT)
     45  1.1  skrll 
     46  1.1  skrll /* Operand errors.  */
     47  1.1  skrll typedef enum
     48  1.1  skrll   {
     49  1.1  skrll     OP_LEGAL = 0,       /* Legal operand.  */
     50  1.1  skrll     OP_OUT_OF_RANGE,    /* Operand not within permitted range.  */
     51  1.1  skrll     OP_NOT_EVEN         /* Operand is Odd number, should be even.  */
     52  1.1  skrll   }
     53  1.1  skrll op_err;
     54  1.1  skrll 
     55  1.1  skrll /* Opcode mnemonics hash table.  */
     56  1.1  skrll static struct hash_control *cr16_inst_hash;
     57  1.1  skrll /* CR16 registers hash table.  */
     58  1.1  skrll static struct hash_control *reg_hash;
     59  1.1  skrll /* CR16 register pair hash table.  */
     60  1.1  skrll static struct hash_control *regp_hash;
     61  1.1  skrll /* CR16 processor registers hash table.  */
     62  1.1  skrll static struct hash_control *preg_hash;
     63  1.1  skrll /* CR16 processor registers 32 bit hash table.  */
     64  1.1  skrll static struct hash_control *pregp_hash;
     65  1.1  skrll /* Current instruction we're assembling.  */
     66  1.1  skrll const inst *instruction;
     67  1.1  skrll 
     68  1.1  skrll 
     69  1.1  skrll static int code_label = 0;
     70  1.1  skrll 
     71  1.1  skrll /* Global variables.  */
     72  1.1  skrll 
     73  1.1  skrll /* Array to hold an instruction encoding.  */
     74  1.1  skrll long output_opcode[2];
     75  1.1  skrll 
     76  1.1  skrll /* Nonzero means a relocatable symbol.  */
     77  1.1  skrll int relocatable;
     78  1.1  skrll 
     79  1.1  skrll /* A copy of the original instruction (used in error messages).  */
     80  1.1  skrll char ins_parse[MAX_INST_LEN];
     81  1.1  skrll 
     82  1.1  skrll /* The current processed argument number.  */
     83  1.1  skrll int cur_arg_num;
     84  1.1  skrll 
     85  1.1  skrll /* Generic assembler global variables which must be defined by all targets.  */
     86  1.1  skrll 
     87  1.1  skrll /* Characters which always start a comment.  */
     88  1.1  skrll const char comment_chars[] = "#";
     89  1.1  skrll 
     90  1.1  skrll /* Characters which start a comment at the beginning of a line.  */
     91  1.1  skrll const char line_comment_chars[] = "#";
     92  1.1  skrll 
     93  1.1  skrll /* This array holds machine specific line separator characters.  */
     94  1.1  skrll const char line_separator_chars[] = ";";
     95  1.1  skrll 
     96  1.1  skrll /* Chars that can be used to separate mant from exp in floating point nums.  */
     97  1.1  skrll const char EXP_CHARS[] = "eE";
     98  1.1  skrll 
     99  1.1  skrll /* Chars that mean this number is a floating point constant as in 0f12.456  */
    100  1.1  skrll const char FLT_CHARS[] = "f'";
    101  1.1  skrll 
    102  1.1  skrll /* Target-specific multicharacter options, not const-declared at usage.  */
    103  1.1  skrll const char *md_shortopts = "";
    104  1.1  skrll struct option md_longopts[] =
    105  1.1  skrll {
    106  1.1  skrll   {NULL, no_argument, NULL, 0}
    107  1.1  skrll };
    108  1.1  skrll size_t md_longopts_size = sizeof (md_longopts);
    109  1.1  skrll 
    110  1.1  skrll static void
    111  1.1  skrll l_cons (int nbytes)
    112  1.1  skrll {
    113  1.1  skrll   int c;
    114  1.1  skrll   expressionS exp;
    115  1.1  skrll 
    116  1.1  skrll #ifdef md_flush_pending_output
    117  1.1  skrll     md_flush_pending_output ();
    118  1.1  skrll #endif
    119  1.1  skrll 
    120  1.1  skrll   if (is_it_end_of_statement ())
    121  1.1  skrll     {
    122  1.1  skrll       demand_empty_rest_of_line ();
    123  1.1  skrll       return;
    124  1.1  skrll     }
    125  1.1  skrll 
    126  1.1  skrll #ifdef TC_ADDRESS_BYTES
    127  1.1  skrll   if (nbytes == 0)
    128  1.1  skrll     nbytes = TC_ADDRESS_BYTES ();
    129  1.1  skrll #endif
    130  1.1  skrll 
    131  1.1  skrll #ifdef md_cons_align
    132  1.1  skrll   md_cons_align (nbytes);
    133  1.1  skrll #endif
    134  1.1  skrll 
    135  1.1  skrll   c = 0;
    136  1.1  skrll   do
    137  1.1  skrll     {
    138  1.1  skrll       unsigned int bits_available = BITS_PER_CHAR * nbytes;
    139  1.1  skrll       char *hold = input_line_pointer;
    140  1.1  skrll 
    141  1.1  skrll       expression (&exp);
    142  1.1  skrll 
    143  1.1  skrll       if (*input_line_pointer == ':')
    144  1.1  skrll         {
    145  1.1  skrll           /* Bitfields.  */
    146  1.1  skrll           long value = 0;
    147  1.1  skrll 
    148  1.1  skrll           for (;;)
    149  1.1  skrll             {
    150  1.1  skrll               unsigned long width;
    151  1.1  skrll 
    152  1.1  skrll               if (*input_line_pointer != ':')
    153  1.1  skrll                 {
    154  1.1  skrll                   input_line_pointer = hold;
    155  1.1  skrll                   break;
    156  1.1  skrll                 }
    157  1.1  skrll               if (exp.X_op == O_absent)
    158  1.1  skrll                 {
    159  1.1  skrll                   as_warn (_("using a bit field width of zero"));
    160  1.1  skrll                   exp.X_add_number = 0;
    161  1.1  skrll                   exp.X_op = O_constant;
    162  1.1  skrll                 }
    163  1.1  skrll 
    164  1.1  skrll               if (exp.X_op != O_constant)
    165  1.1  skrll                 {
    166  1.1  skrll                   *input_line_pointer = '\0';
    167  1.1  skrll                   as_bad (_("field width \"%s\" too complex for a bitfield"), hold);
    168  1.1  skrll                   *input_line_pointer = ':';
    169  1.1  skrll                   demand_empty_rest_of_line ();
    170  1.1  skrll                   return;
    171  1.1  skrll                 }
    172  1.1  skrll 
    173  1.1  skrll               if ((width = exp.X_add_number) >
    174  1.1  skrll                   (unsigned int)(BITS_PER_CHAR * nbytes))
    175  1.1  skrll                 {
    176  1.1  skrll                   as_warn (_("field width %lu too big to fit in %d bytes: truncated to %d bits"), width, nbytes, (BITS_PER_CHAR * nbytes));
    177  1.1  skrll                   width = BITS_PER_CHAR * nbytes;
    178  1.1  skrll                 }                   /* Too big.  */
    179  1.1  skrll 
    180  1.1  skrll 
    181  1.1  skrll               if (width > bits_available)
    182  1.1  skrll                 {
    183  1.1  skrll                   /* FIXME-SOMEDAY: backing up and reparsing is wasteful.  */
    184  1.1  skrll                   input_line_pointer = hold;
    185  1.1  skrll                   exp.X_add_number = value;
    186  1.1  skrll                   break;
    187  1.1  skrll                 }
    188  1.1  skrll 
    189  1.1  skrll               /* Skip ':'.  */
    190  1.1  skrll               hold = ++input_line_pointer;
    191  1.1  skrll 
    192  1.1  skrll               expression (&exp);
    193  1.1  skrll               if (exp.X_op != O_constant)
    194  1.1  skrll                 {
    195  1.1  skrll                   char cache = *input_line_pointer;
    196  1.1  skrll 
    197  1.1  skrll                   *input_line_pointer = '\0';
    198  1.1  skrll                   as_bad (_("field value \"%s\" too complex for a bitfield"), hold);
    199  1.1  skrll                   *input_line_pointer = cache;
    200  1.1  skrll                   demand_empty_rest_of_line ();
    201  1.1  skrll                   return;
    202  1.1  skrll                 }
    203  1.1  skrll 
    204  1.1  skrll               value |= ((~(-1 << width) & exp.X_add_number)
    205  1.1  skrll                         << ((BITS_PER_CHAR * nbytes) - bits_available));
    206  1.1  skrll 
    207  1.1  skrll               if ((bits_available -= width) == 0
    208  1.1  skrll                   || is_it_end_of_statement ()
    209  1.1  skrll                   || *input_line_pointer != ',')
    210  1.1  skrll                 break;
    211  1.1  skrll 
    212  1.1  skrll               hold = ++input_line_pointer;
    213  1.1  skrll               expression (&exp);
    214  1.1  skrll             }
    215  1.1  skrll 
    216  1.1  skrll           exp.X_add_number = value;
    217  1.1  skrll           exp.X_op = O_constant;
    218  1.1  skrll           exp.X_unsigned = 1;
    219  1.1  skrll         }
    220  1.1  skrll 
    221  1.1  skrll       if ((*(input_line_pointer) == '@') && (*(input_line_pointer +1) == 'c'))
    222  1.1  skrll         code_label = 1;
    223  1.1  skrll       emit_expr (&exp, (unsigned int) nbytes);
    224  1.1  skrll       ++c;
    225  1.1  skrll       if ((*(input_line_pointer) == '@') && (*(input_line_pointer +1) == 'c'))
    226  1.1  skrll         {
    227  1.1  skrll           input_line_pointer +=3;
    228  1.1  skrll           break;
    229  1.1  skrll         }
    230  1.1  skrll     }
    231  1.1  skrll   while ((*input_line_pointer++ == ','));
    232  1.1  skrll 
    233  1.1  skrll   /* Put terminator back into stream.  */
    234  1.1  skrll   input_line_pointer--;
    235  1.1  skrll 
    236  1.1  skrll   demand_empty_rest_of_line ();
    237  1.1  skrll }
    238  1.1  skrll 
    239  1.1  skrll 
    240  1.1  skrll /* This table describes all the machine specific pseudo-ops
    241  1.1  skrll    the assembler has to support.  The fields are:
    242  1.1  skrll    *** Pseudo-op name without dot.
    243  1.1  skrll    *** Function to call to execute this pseudo-op.
    244  1.1  skrll    *** Integer arg to pass to the function.  */
    245  1.1  skrll 
    246  1.1  skrll const pseudo_typeS md_pseudo_table[] =
    247  1.1  skrll {
    248  1.1  skrll   /* In CR16 machine, align is in bytes (not a ptwo boundary).  */
    249  1.1  skrll   {"align", s_align_bytes, 0},
    250  1.1  skrll   {"long", l_cons,  4 },
    251  1.1  skrll   {0, 0, 0}
    252  1.1  skrll };
    253  1.1  skrll 
    254  1.1  skrll /* CR16 relaxation table.  */
    255  1.1  skrll const relax_typeS md_relax_table[] =
    256  1.1  skrll {
    257  1.1  skrll   /* bCC  */
    258  1.1  skrll   {0x7f, -0x80, 2, 1},                  /*  8 */
    259  1.1  skrll   {0xfffe, -0x10000, 4, 2},             /* 16 */
    260  1.1  skrll   {0xfffffe, -0x1000000, 6, 0},         /* 24 */
    261  1.1  skrll };
    262  1.1  skrll 
    263  1.1  skrll /* Return the bit size for a given operand.  */
    264  1.1  skrll 
    265  1.1  skrll static int
    266  1.1  skrll get_opbits (operand_type op)
    267  1.1  skrll {
    268  1.1  skrll   if (op < MAX_OPRD)
    269  1.1  skrll     return cr16_optab[op].bit_size;
    270  1.1  skrll 
    271  1.1  skrll   return 0;
    272  1.1  skrll }
    273  1.1  skrll 
    274  1.1  skrll /* Return the argument type of a given operand.  */
    275  1.1  skrll 
    276  1.1  skrll static argtype
    277  1.1  skrll get_optype (operand_type op)
    278  1.1  skrll {
    279  1.1  skrll   if (op < MAX_OPRD)
    280  1.1  skrll     return cr16_optab[op].arg_type;
    281  1.1  skrll   else
    282  1.1  skrll     return nullargs;
    283  1.1  skrll }
    284  1.1  skrll 
    285  1.1  skrll /* Return the flags of a given operand.  */
    286  1.1  skrll 
    287  1.1  skrll static int
    288  1.1  skrll get_opflags (operand_type op)
    289  1.1  skrll {
    290  1.1  skrll   if (op < MAX_OPRD)
    291  1.1  skrll     return cr16_optab[op].flags;
    292  1.1  skrll 
    293  1.1  skrll   return 0;
    294  1.1  skrll }
    295  1.1  skrll 
    296  1.1  skrll /* Get the cc code.  */
    297  1.1  skrll 
    298  1.1  skrll static int
    299  1.1  skrll get_cc (char *cc_name)
    300  1.1  skrll {
    301  1.1  skrll    unsigned int i;
    302  1.1  skrll 
    303  1.1  skrll    for (i = 0; i < cr16_num_cc; i++)
    304  1.1  skrll      if (strcmp (cc_name, cr16_b_cond_tab[i]) == 0)
    305  1.1  skrll        return i;
    306  1.1  skrll 
    307  1.1  skrll    return -1;
    308  1.1  skrll }
    309  1.1  skrll 
    310  1.1  skrll /* Get the core processor register 'reg_name'.  */
    311  1.1  skrll 
    312  1.1  skrll static reg
    313  1.1  skrll get_register (char *reg_name)
    314  1.1  skrll {
    315  1.1  skrll   const reg_entry *reg;
    316  1.1  skrll 
    317  1.1  skrll   reg = (const reg_entry *) hash_find (reg_hash, reg_name);
    318  1.1  skrll 
    319  1.1  skrll   if (reg != NULL)
    320  1.1  skrll     return reg->value.reg_val;
    321  1.1  skrll 
    322  1.1  skrll   return nullregister;
    323  1.1  skrll }
    324  1.1  skrll /* Get the core processor register-pair 'reg_name'.  */
    325  1.1  skrll 
    326  1.1  skrll static reg
    327  1.1  skrll get_register_pair (char *reg_name)
    328  1.1  skrll {
    329  1.1  skrll   const reg_entry *reg;
    330  1.1  skrll   char tmp_rp[16]="\0";
    331  1.1  skrll 
    332  1.1  skrll   /* Add '(' and ')' to the reg pair, if its not present.  */
    333  1.1  skrll   if (reg_name[0] != '(')
    334  1.1  skrll     {
    335  1.1  skrll       tmp_rp[0] = '(';
    336  1.1  skrll       strcat (tmp_rp, reg_name);
    337  1.1  skrll       strcat (tmp_rp,")");
    338  1.1  skrll       reg = (const reg_entry *) hash_find (regp_hash, tmp_rp);
    339  1.1  skrll     }
    340  1.1  skrll   else
    341  1.1  skrll     reg = (const reg_entry *) hash_find (regp_hash, reg_name);
    342  1.1  skrll 
    343  1.1  skrll   if (reg != NULL)
    344  1.1  skrll     return reg->value.reg_val;
    345  1.1  skrll 
    346  1.1  skrll   return nullregister;
    347  1.1  skrll }
    348  1.1  skrll 
    349  1.1  skrll /* Get the index register 'reg_name'.  */
    350  1.1  skrll 
    351  1.1  skrll static reg
    352  1.1  skrll get_index_register (char *reg_name)
    353  1.1  skrll {
    354  1.1  skrll   const reg_entry *reg;
    355  1.1  skrll 
    356  1.1  skrll   reg = (const reg_entry *) hash_find (reg_hash, reg_name);
    357  1.1  skrll 
    358  1.1  skrll   if ((reg != NULL)
    359  1.1  skrll       && ((reg->value.reg_val == 12) || (reg->value.reg_val == 13)))
    360  1.1  skrll     return reg->value.reg_val;
    361  1.1  skrll 
    362  1.1  skrll   return nullregister;
    363  1.1  skrll }
    364  1.1  skrll /* Get the core processor index register-pair 'reg_name'.  */
    365  1.1  skrll 
    366  1.1  skrll static reg
    367  1.1  skrll get_index_register_pair (char *reg_name)
    368  1.1  skrll {
    369  1.1  skrll   const reg_entry *reg;
    370  1.1  skrll 
    371  1.1  skrll   reg = (const reg_entry *) hash_find (regp_hash, reg_name);
    372  1.1  skrll 
    373  1.1  skrll   if (reg != NULL)
    374  1.1  skrll     {
    375  1.1  skrll       if ((reg->value.reg_val != 1) || (reg->value.reg_val != 7)
    376  1.1  skrll           || (reg->value.reg_val != 9) || (reg->value.reg_val > 10))
    377  1.1  skrll         return reg->value.reg_val;
    378  1.1  skrll 
    379  1.1  skrll       as_bad (_("Unknown register pair - index relative mode: `%d'"), reg->value.reg_val);
    380  1.1  skrll     }
    381  1.1  skrll 
    382  1.1  skrll   return nullregister;
    383  1.1  skrll }
    384  1.1  skrll 
    385  1.1  skrll /* Get the processor register 'preg_name'.  */
    386  1.1  skrll 
    387  1.1  skrll static preg
    388  1.1  skrll get_pregister (char *preg_name)
    389  1.1  skrll {
    390  1.1  skrll   const reg_entry *preg;
    391  1.1  skrll 
    392  1.1  skrll   preg = (const reg_entry *) hash_find (preg_hash, preg_name);
    393  1.1  skrll 
    394  1.1  skrll   if (preg != NULL)
    395  1.1  skrll     return preg->value.preg_val;
    396  1.1  skrll 
    397  1.1  skrll   return nullpregister;
    398  1.1  skrll }
    399  1.1  skrll 
    400  1.1  skrll /* Get the processor register 'preg_name 32 bit'.  */
    401  1.1  skrll 
    402  1.1  skrll static preg
    403  1.1  skrll get_pregisterp (char *preg_name)
    404  1.1  skrll {
    405  1.1  skrll   const reg_entry *preg;
    406  1.1  skrll 
    407  1.1  skrll   preg = (const reg_entry *) hash_find (pregp_hash, preg_name);
    408  1.1  skrll 
    409  1.1  skrll   if (preg != NULL)
    410  1.1  skrll     return preg->value.preg_val;
    411  1.1  skrll 
    412  1.1  skrll   return nullpregister;
    413  1.1  skrll }
    414  1.1  skrll 
    415  1.1  skrll 
    416  1.1  skrll /* Round up a section size to the appropriate boundary.  */
    417  1.1  skrll 
    418  1.1  skrll valueT
    419  1.1  skrll md_section_align (segT seg, valueT val)
    420  1.1  skrll {
    421  1.1  skrll   /* Round .text section to a multiple of 2.  */
    422  1.1  skrll   if (seg == text_section)
    423  1.1  skrll     return (val + 1) & ~1;
    424  1.1  skrll   return val;
    425  1.1  skrll }
    426  1.1  skrll 
    427  1.1  skrll /* Parse an operand that is machine-specific (remove '*').  */
    428  1.1  skrll 
    429  1.1  skrll void
    430  1.1  skrll md_operand (expressionS * exp)
    431  1.1  skrll {
    432  1.1  skrll   char c = *input_line_pointer;
    433  1.1  skrll 
    434  1.1  skrll   switch (c)
    435  1.1  skrll     {
    436  1.1  skrll     case '*':
    437  1.1  skrll       input_line_pointer++;
    438  1.1  skrll       expression (exp);
    439  1.1  skrll       break;
    440  1.1  skrll     default:
    441  1.1  skrll       break;
    442  1.1  skrll     }
    443  1.1  skrll }
    444  1.1  skrll 
    445  1.1  skrll /* Reset global variables before parsing a new instruction.  */
    446  1.1  skrll 
    447  1.1  skrll static void
    448  1.1  skrll reset_vars (char *op)
    449  1.1  skrll {
    450  1.1  skrll   cur_arg_num = relocatable = 0;
    451  1.1  skrll   memset (& output_opcode, '\0', sizeof (output_opcode));
    452  1.1  skrll 
    453  1.1  skrll   /* Save a copy of the original OP (used in error messages).  */
    454  1.1  skrll   strncpy (ins_parse, op, sizeof ins_parse - 1);
    455  1.1  skrll   ins_parse [sizeof ins_parse - 1] = 0;
    456  1.1  skrll }
    457  1.1  skrll 
    458  1.1  skrll /* This macro decides whether a particular reloc is an entry in a
    459  1.1  skrll    switch table.  It is used when relaxing, because the linker needs
    460  1.1  skrll    to know about all such entries so that it can adjust them if
    461  1.1  skrll    necessary.  */
    462  1.1  skrll 
    463  1.1  skrll #define SWITCH_TABLE(fix)                                  \
    464  1.1  skrll   (   (fix)->fx_addsy != NULL                              \
    465  1.1  skrll    && (fix)->fx_subsy != NULL                              \
    466  1.1  skrll    && S_GET_SEGMENT ((fix)->fx_addsy) ==                   \
    467  1.1  skrll       S_GET_SEGMENT ((fix)->fx_subsy)                      \
    468  1.1  skrll    && S_GET_SEGMENT (fix->fx_addsy) != undefined_section   \
    469  1.1  skrll    && (   (fix)->fx_r_type == BFD_RELOC_CR16_NUM8          \
    470  1.1  skrll        || (fix)->fx_r_type == BFD_RELOC_CR16_NUM16         \
    471  1.1  skrll        || (fix)->fx_r_type == BFD_RELOC_CR16_NUM32         \
    472  1.1  skrll        || (fix)->fx_r_type == BFD_RELOC_CR16_NUM32a))
    473  1.1  skrll 
    474  1.1  skrll /* See whether we need to force a relocation into the output file.
    475  1.1  skrll    This is used to force out switch and PC relative relocations when
    476  1.1  skrll    relaxing.  */
    477  1.1  skrll 
    478  1.1  skrll int
    479  1.1  skrll cr16_force_relocation (fixS *fix)
    480  1.1  skrll {
    481  1.1  skrll   if (generic_force_reloc (fix) || SWITCH_TABLE (fix))
    482  1.1  skrll     return 1;
    483  1.1  skrll 
    484  1.1  skrll   return 0;
    485  1.1  skrll }
    486  1.1  skrll 
    487  1.1  skrll /* Record a fixup for a cons expression.  */
    488  1.1  skrll 
    489  1.1  skrll void
    490  1.1  skrll cr16_cons_fix_new (fragS *frag, int offset, int len, expressionS *exp)
    491  1.1  skrll {
    492  1.1  skrll   int rtype;
    493  1.1  skrll   switch (len)
    494  1.1  skrll     {
    495  1.1  skrll     default: rtype = BFD_RELOC_NONE; break;
    496  1.1  skrll     case 1: rtype = BFD_RELOC_CR16_NUM8 ; break;
    497  1.1  skrll     case 2: rtype = BFD_RELOC_CR16_NUM16; break;
    498  1.1  skrll     case 4:
    499  1.1  skrll       if (code_label)
    500  1.1  skrll         {
    501  1.1  skrll           rtype = BFD_RELOC_CR16_NUM32a;
    502  1.1  skrll           code_label = 0;
    503  1.1  skrll         }
    504  1.1  skrll       else
    505  1.1  skrll         rtype = BFD_RELOC_CR16_NUM32;
    506  1.1  skrll       break;
    507  1.1  skrll     }
    508  1.1  skrll 
    509  1.1  skrll   fix_new_exp (frag, offset, len, exp, 0, rtype);
    510  1.1  skrll }
    511  1.1  skrll 
    512  1.1  skrll /* Generate a relocation entry for a fixup.  */
    513  1.1  skrll 
    514  1.1  skrll arelent *
    515  1.1  skrll tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS * fixP)
    516  1.1  skrll {
    517  1.1  skrll   arelent * reloc;
    518  1.1  skrll 
    519  1.1  skrll   reloc = xmalloc (sizeof (arelent));
    520  1.1  skrll   reloc->sym_ptr_ptr  = xmalloc (sizeof (asymbol *));
    521  1.1  skrll   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
    522  1.1  skrll   reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
    523  1.1  skrll   reloc->addend = fixP->fx_offset;
    524  1.1  skrll 
    525  1.1  skrll   if (fixP->fx_subsy != NULL)
    526  1.1  skrll     {
    527  1.1  skrll       if (SWITCH_TABLE (fixP))
    528  1.1  skrll         {
    529  1.1  skrll           /* Keep the current difference in the addend.  */
    530  1.1  skrll           reloc->addend = (S_GET_VALUE (fixP->fx_addsy)
    531  1.1  skrll                            - S_GET_VALUE (fixP->fx_subsy) + fixP->fx_offset);
    532  1.1  skrll 
    533  1.1  skrll           switch (fixP->fx_r_type)
    534  1.1  skrll             {
    535  1.1  skrll             case BFD_RELOC_CR16_NUM8:
    536  1.1  skrll               fixP->fx_r_type = BFD_RELOC_CR16_SWITCH8;
    537  1.1  skrll               break;
    538  1.1  skrll             case BFD_RELOC_CR16_NUM16:
    539  1.1  skrll               fixP->fx_r_type = BFD_RELOC_CR16_SWITCH16;
    540  1.1  skrll               break;
    541  1.1  skrll             case BFD_RELOC_CR16_NUM32:
    542  1.1  skrll               fixP->fx_r_type = BFD_RELOC_CR16_SWITCH32;
    543  1.1  skrll               break;
    544  1.1  skrll             case BFD_RELOC_CR16_NUM32a:
    545  1.1  skrll               fixP->fx_r_type = BFD_RELOC_CR16_NUM32a;
    546  1.1  skrll               break;
    547  1.1  skrll             default:
    548  1.1  skrll               abort ();
    549  1.1  skrll               break;
    550  1.1  skrll             }
    551  1.1  skrll         }
    552  1.1  skrll       else
    553  1.1  skrll         {
    554  1.1  skrll           /* We only resolve difference expressions in the same section.  */
    555  1.1  skrll           as_bad_where (fixP->fx_file, fixP->fx_line,
    556  1.1  skrll                         _("can't resolve `%s' {%s section} - `%s' {%s section}"),
    557  1.1  skrll                         fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
    558  1.1  skrll                         segment_name (fixP->fx_addsy
    559  1.1  skrll                                       ? S_GET_SEGMENT (fixP->fx_addsy)
    560  1.1  skrll                                       : absolute_section),
    561  1.1  skrll                         S_GET_NAME (fixP->fx_subsy),
    562  1.1  skrll                         segment_name (S_GET_SEGMENT (fixP->fx_addsy)));
    563  1.1  skrll         }
    564  1.1  skrll     }
    565  1.1  skrll 
    566  1.1  skrll   assert ((int) fixP->fx_r_type > 0);
    567  1.1  skrll   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
    568  1.1  skrll 
    569  1.1  skrll   if (reloc->howto == NULL)
    570  1.1  skrll     {
    571  1.1  skrll       as_bad_where (fixP->fx_file, fixP->fx_line,
    572  1.1  skrll                     _("internal error: reloc %d (`%s') not supported by object file format"),
    573  1.1  skrll                     fixP->fx_r_type,
    574  1.1  skrll                     bfd_get_reloc_code_name (fixP->fx_r_type));
    575  1.1  skrll       return NULL;
    576  1.1  skrll     }
    577  1.1  skrll   assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
    578  1.1  skrll 
    579  1.1  skrll   return reloc;
    580  1.1  skrll }
    581  1.1  skrll 
    582  1.1  skrll /* Prepare machine-dependent frags for relaxation.  */
    583  1.1  skrll 
    584  1.1  skrll int
    585  1.1  skrll md_estimate_size_before_relax (fragS *fragp, asection *seg)
    586  1.1  skrll {
    587  1.1  skrll   /* If symbol is undefined or located in a different section,
    588  1.1  skrll      select the largest supported relocation.  */
    589  1.1  skrll   relax_substateT subtype;
    590  1.1  skrll   relax_substateT rlx_state[] = {0, 2};
    591  1.1  skrll 
    592  1.1  skrll   for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
    593  1.1  skrll     {
    594  1.1  skrll       if (fragp->fr_subtype == rlx_state[subtype]
    595  1.1  skrll           && (!S_IS_DEFINED (fragp->fr_symbol)
    596  1.1  skrll               || seg != S_GET_SEGMENT (fragp->fr_symbol)))
    597  1.1  skrll         {
    598  1.1  skrll           fragp->fr_subtype = rlx_state[subtype + 1];
    599  1.1  skrll           break;
    600  1.1  skrll         }
    601  1.1  skrll     }
    602  1.1  skrll 
    603  1.1  skrll   if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
    604  1.1  skrll     abort ();
    605  1.1  skrll 
    606  1.1  skrll   return md_relax_table[fragp->fr_subtype].rlx_length;
    607  1.1  skrll }
    608  1.1  skrll 
    609  1.1  skrll void
    610  1.1  skrll md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, fragS *fragP)
    611  1.1  skrll {
    612  1.1  skrll   /* 'opcode' points to the start of the instruction, whether
    613  1.1  skrll      we need to change the instruction's fixed encoding.  */
    614  1.1  skrll   char *opcode = fragP->fr_literal + fragP->fr_fix;
    615  1.1  skrll   bfd_reloc_code_real_type reloc;
    616  1.1  skrll 
    617  1.1  skrll   subseg_change (sec, 0);
    618  1.1  skrll 
    619  1.1  skrll   switch (fragP->fr_subtype)
    620  1.1  skrll     {
    621  1.1  skrll     case 0:
    622  1.1  skrll       reloc = BFD_RELOC_CR16_DISP8;
    623  1.1  skrll       break;
    624  1.1  skrll     case 1:
    625  1.1  skrll       /* If the subtype is not changed due to :m operand qualifier,
    626  1.1  skrll          then no need to update the opcode value.  */
    627  1.1  skrll       if ((int)opcode[1] != 0x18)
    628  1.1  skrll         {
    629  1.1  skrll           opcode[0] = (opcode[0] & 0xf0);
    630  1.1  skrll           opcode[1] = 0x18;
    631  1.1  skrll         }
    632  1.1  skrll       reloc = BFD_RELOC_CR16_DISP16;
    633  1.1  skrll       break;
    634  1.1  skrll     case 2:
    635  1.1  skrll       /* If the subtype is not changed due to :l operand qualifier,
    636  1.1  skrll          then no need to update the opcode value.  */
    637  1.1  skrll       if ((int)opcode[1] != 0)
    638  1.1  skrll         {
    639  1.1  skrll           opcode[2] = opcode[0];
    640  1.1  skrll           opcode[0] = opcode[1];
    641  1.1  skrll           opcode[1] = 0x0;
    642  1.1  skrll         }
    643  1.1  skrll       reloc = BFD_RELOC_CR16_DISP24;
    644  1.1  skrll       break;
    645  1.1  skrll     default:
    646  1.1  skrll       abort();
    647  1.1  skrll     }
    648  1.1  skrll 
    649  1.1  skrll   fix_new (fragP, fragP->fr_fix,
    650  1.1  skrll            bfd_get_reloc_size (bfd_reloc_type_lookup (stdoutput, reloc)),
    651  1.1  skrll            fragP->fr_symbol, fragP->fr_offset, 1, reloc);
    652  1.1  skrll   fragP->fr_var = 0;
    653  1.1  skrll   fragP->fr_fix += md_relax_table[fragP->fr_subtype].rlx_length;
    654  1.1  skrll }
    655  1.1  skrll 
    656  1.1  skrll /* Process machine-dependent command line options.  Called once for
    657  1.1  skrll    each option on the command line that the machine-independent part of
    658  1.1  skrll    GAS does not understand.  */
    659  1.1  skrll 
    660  1.1  skrll int
    661  1.1  skrll md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
    662  1.1  skrll {
    663  1.1  skrll   return 0;
    664  1.1  skrll }
    665  1.1  skrll 
    666  1.1  skrll /* Machine-dependent usage-output.  */
    667  1.1  skrll 
    668  1.1  skrll void
    669  1.1  skrll md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
    670  1.1  skrll {
    671  1.1  skrll   return;
    672  1.1  skrll }
    673  1.1  skrll 
    674  1.1  skrll char *
    675  1.1  skrll md_atof (int type, char *litP, int *sizeP)
    676  1.1  skrll {
    677  1.1  skrll   return ieee_md_atof (type, litP, sizeP, target_big_endian);
    678  1.1  skrll }
    679  1.1  skrll 
    680  1.1  skrll /* Apply a fixS (fixup of an instruction or data that we didn't have
    681  1.1  skrll    enough info to complete immediately) to the data in a frag.
    682  1.1  skrll    Since linkrelax is nonzero and TC_LINKRELAX_FIXUP is defined to disable
    683  1.1  skrll    relaxation of debug sections, this function is called only when
    684  1.1  skrll    fixuping relocations of debug sections.  */
    685  1.1  skrll 
    686  1.1  skrll void
    687  1.1  skrll md_apply_fix (fixS *fixP, valueT *valP, segT seg)
    688  1.1  skrll {
    689  1.1  skrll   valueT val = * valP;
    690  1.1  skrll   char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
    691  1.1  skrll   fixP->fx_offset = 0;
    692  1.1  skrll 
    693  1.1  skrll   switch (fixP->fx_r_type)
    694  1.1  skrll     {
    695  1.1  skrll       case BFD_RELOC_CR16_NUM8:
    696  1.1  skrll         bfd_put_8 (stdoutput, (unsigned char) val, buf);
    697  1.1  skrll         break;
    698  1.1  skrll       case BFD_RELOC_CR16_NUM16:
    699  1.1  skrll         bfd_put_16 (stdoutput, val, buf);
    700  1.1  skrll         break;
    701  1.1  skrll       case BFD_RELOC_CR16_NUM32:
    702  1.1  skrll         bfd_put_32 (stdoutput, val, buf);
    703  1.1  skrll         break;
    704  1.1  skrll       case BFD_RELOC_CR16_NUM32a:
    705  1.1  skrll         bfd_put_32 (stdoutput, val, buf);
    706  1.1  skrll         break;
    707  1.1  skrll       default:
    708  1.1  skrll         /* We shouldn't ever get here because linkrelax is nonzero.  */
    709  1.1  skrll         abort ();
    710  1.1  skrll         break;
    711  1.1  skrll     }
    712  1.1  skrll 
    713  1.1  skrll   fixP->fx_done = 0;
    714  1.1  skrll 
    715  1.1  skrll   if (fixP->fx_addsy == NULL
    716  1.1  skrll       && fixP->fx_pcrel == 0)
    717  1.1  skrll     fixP->fx_done = 1;
    718  1.1  skrll 
    719  1.1  skrll   if (fixP->fx_pcrel == 1
    720  1.1  skrll       && fixP->fx_addsy != NULL
    721  1.1  skrll       && S_GET_SEGMENT (fixP->fx_addsy) == seg)
    722  1.1  skrll     fixP->fx_done = 1;
    723  1.1  skrll }
    724  1.1  skrll 
    725  1.1  skrll /* The location from which a PC relative jump should be calculated,
    726  1.1  skrll    given a PC relative reloc.  */
    727  1.1  skrll 
    728  1.1  skrll long
    729  1.1  skrll md_pcrel_from (fixS *fixp)
    730  1.1  skrll {
    731  1.1  skrll   return fixp->fx_frag->fr_address + fixp->fx_where;
    732  1.1  skrll }
    733  1.1  skrll 
    734  1.1  skrll static void
    735  1.1  skrll initialise_reg_hash_table (struct hash_control ** hash_table,
    736  1.1  skrll                            const reg_entry * register_table,
    737  1.1  skrll                            const unsigned int num_entries)
    738  1.1  skrll {
    739  1.1  skrll   const reg_entry * reg;
    740  1.1  skrll   const char *hashret;
    741  1.1  skrll 
    742  1.1  skrll   if ((* hash_table = hash_new ()) == NULL)
    743  1.1  skrll     as_fatal (_("Virtual memory exhausted"));
    744  1.1  skrll 
    745  1.1  skrll   for (reg = register_table;
    746  1.1  skrll        reg < (register_table + num_entries);
    747  1.1  skrll        reg++)
    748  1.1  skrll     {
    749  1.1  skrll       hashret = hash_insert (* hash_table, reg->name, (char *) reg);
    750  1.1  skrll       if (hashret)
    751  1.1  skrll         as_fatal (_("Internal Error:  Can't hash %s: %s"),
    752  1.1  skrll                   reg->name, hashret);
    753  1.1  skrll     }
    754  1.1  skrll }
    755  1.1  skrll 
    756  1.1  skrll /* This function is called once, at assembler startup time.  This should
    757  1.1  skrll    set up all the tables, etc that the MD part of the assembler needs.  */
    758  1.1  skrll 
    759  1.1  skrll void
    760  1.1  skrll md_begin (void)
    761  1.1  skrll {
    762  1.1  skrll   int i = 0;
    763  1.1  skrll 
    764  1.1  skrll   /* Set up a hash table for the instructions.  */
    765  1.1  skrll   if ((cr16_inst_hash = hash_new ()) == NULL)
    766  1.1  skrll     as_fatal (_("Virtual memory exhausted"));
    767  1.1  skrll 
    768  1.1  skrll   while (cr16_instruction[i].mnemonic != NULL)
    769  1.1  skrll     {
    770  1.1  skrll       const char *hashret;
    771  1.1  skrll       const char *mnemonic = cr16_instruction[i].mnemonic;
    772  1.1  skrll 
    773  1.1  skrll       hashret = hash_insert (cr16_inst_hash, mnemonic,
    774  1.1  skrll                              (char *)(cr16_instruction + i));
    775  1.1  skrll 
    776  1.1  skrll       if (hashret != NULL && *hashret != '\0')
    777  1.1  skrll         as_fatal (_("Can't hash `%s': %s\n"), cr16_instruction[i].mnemonic,
    778  1.1  skrll                   *hashret == 0 ? _("(unknown reason)") : hashret);
    779  1.1  skrll 
    780  1.1  skrll       /* Insert unique names into hash table.  The CR16 instruction set
    781  1.1  skrll          has many identical opcode names that have different opcodes based
    782  1.1  skrll          on the operands.  This hash table then provides a quick index to
    783  1.1  skrll          the first opcode with a particular name in the opcode table.  */
    784  1.1  skrll       do
    785  1.1  skrll         {
    786  1.1  skrll           ++i;
    787  1.1  skrll         }
    788  1.1  skrll       while (cr16_instruction[i].mnemonic != NULL
    789  1.1  skrll              && streq (cr16_instruction[i].mnemonic, mnemonic));
    790  1.1  skrll     }
    791  1.1  skrll 
    792  1.1  skrll   /* Initialize reg_hash hash table.  */
    793  1.1  skrll   initialise_reg_hash_table (& reg_hash, cr16_regtab, NUMREGS);
    794  1.1  skrll   /* Initialize regp_hash hash table.  */
    795  1.1  skrll   initialise_reg_hash_table (& regp_hash, cr16_regptab, NUMREGPS);
    796  1.1  skrll   /* Initialize preg_hash hash table.  */
    797  1.1  skrll   initialise_reg_hash_table (& preg_hash, cr16_pregtab, NUMPREGS);
    798  1.1  skrll   /* Initialize pregp_hash hash table.  */
    799  1.1  skrll   initialise_reg_hash_table (& pregp_hash, cr16_pregptab, NUMPREGPS);
    800  1.1  skrll 
    801  1.1  skrll   /*  Set linkrelax here to avoid fixups in most sections.  */
    802  1.1  skrll   linkrelax = 1;
    803  1.1  skrll }
    804  1.1  skrll 
    805  1.1  skrll /* Process constants (immediate/absolute)
    806  1.1  skrll    and labels (jump targets/Memory locations).  */
    807  1.1  skrll 
    808  1.1  skrll static void
    809  1.1  skrll process_label_constant (char *str, ins * cr16_ins)
    810  1.1  skrll {
    811  1.1  skrll   char *saved_input_line_pointer;
    812  1.1  skrll   int symbol_with_at = 0;
    813  1.1  skrll   int symbol_with_s = 0;
    814  1.1  skrll   int symbol_with_m = 0;
    815  1.1  skrll   int symbol_with_l = 0;
    816  1.1  skrll   argument *cur_arg = cr16_ins->arg + cur_arg_num;  /* Current argument.  */
    817  1.1  skrll 
    818  1.1  skrll   saved_input_line_pointer = input_line_pointer;
    819  1.1  skrll   input_line_pointer = str;
    820  1.1  skrll 
    821  1.1  skrll   expression (&cr16_ins->exp);
    822  1.1  skrll 
    823  1.1  skrll   switch (cr16_ins->exp.X_op)
    824  1.1  skrll     {
    825  1.1  skrll     case O_big:
    826  1.1  skrll     case O_absent:
    827  1.1  skrll       /* Missing or bad expr becomes absolute 0.  */
    828  1.1  skrll       as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
    829  1.1  skrll               str);
    830  1.1  skrll       cr16_ins->exp.X_op = O_constant;
    831  1.1  skrll       cr16_ins->exp.X_add_number = 0;
    832  1.1  skrll       cr16_ins->exp.X_add_symbol = NULL;
    833  1.1  skrll       cr16_ins->exp.X_op_symbol = NULL;
    834  1.1  skrll       /* Fall through.  */
    835  1.1  skrll 
    836  1.1  skrll     case O_constant:
    837  1.1  skrll       cur_arg->X_op = O_constant;
    838  1.1  skrll       cur_arg->constant = cr16_ins->exp.X_add_number;
    839  1.1  skrll       break;
    840  1.1  skrll 
    841  1.1  skrll     case O_symbol:
    842  1.1  skrll     case O_subtract:
    843  1.1  skrll     case O_add:
    844  1.1  skrll       cur_arg->X_op = O_symbol;
    845  1.1  skrll       cr16_ins->rtype = BFD_RELOC_NONE;
    846  1.1  skrll       relocatable = 1;
    847  1.1  skrll 
    848  1.1  skrll       if (strneq (input_line_pointer, "@c", 2))
    849  1.1  skrll         symbol_with_at = 1;
    850  1.1  skrll 
    851  1.1  skrll       if (strneq (input_line_pointer, "@l", 2)
    852  1.1  skrll           || strneq (input_line_pointer, ":l", 2))
    853  1.1  skrll         symbol_with_l = 1;
    854  1.1  skrll 
    855  1.1  skrll       if (strneq (input_line_pointer, "@m", 2)
    856  1.1  skrll           || strneq (input_line_pointer, ":m", 2))
    857  1.1  skrll         symbol_with_m = 1;
    858  1.1  skrll 
    859  1.1  skrll       if (strneq (input_line_pointer, "@s", 2)
    860  1.1  skrll           || strneq (input_line_pointer, ":s", 2))
    861  1.1  skrll         symbol_with_s = 1;
    862  1.1  skrll 
    863  1.1  skrll       switch (cur_arg->type)
    864  1.1  skrll         {
    865  1.1  skrll         case arg_cr:
    866  1.1  skrll           if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
    867  1.1  skrll             {
    868  1.1  skrll               if (cur_arg->size == 20)
    869  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
    870  1.1  skrll               else
    871  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20a;
    872  1.1  skrll             }
    873  1.1  skrll           break;
    874  1.1  skrll 
    875  1.1  skrll         case arg_crp:
    876  1.1  skrll           if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
    877  1.1  skrll             switch (instruction->size)
    878  1.1  skrll               {
    879  1.1  skrll               case 1:
    880  1.1  skrll                 switch (cur_arg->size)
    881  1.1  skrll                   {
    882  1.1  skrll                   case 0:
    883  1.1  skrll                     cr16_ins->rtype = BFD_RELOC_CR16_REGREL0;
    884  1.1  skrll                     break;
    885  1.1  skrll                   case 4:
    886  1.1  skrll                     if (IS_INSN_MNEMONIC ("loadb") || IS_INSN_MNEMONIC ("storb"))
    887  1.1  skrll                       cr16_ins->rtype = BFD_RELOC_CR16_REGREL4;
    888  1.1  skrll                     else
    889  1.1  skrll                       cr16_ins->rtype = BFD_RELOC_CR16_REGREL4a;
    890  1.1  skrll                     break;
    891  1.1  skrll                   default: break;
    892  1.1  skrll                   }
    893  1.1  skrll                 break;
    894  1.1  skrll               case 2:
    895  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_REGREL16;
    896  1.1  skrll                 break;
    897  1.1  skrll               case 3:
    898  1.1  skrll                 if (cur_arg->size == 20)
    899  1.1  skrll                   cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
    900  1.1  skrll                 else
    901  1.1  skrll                   cr16_ins->rtype = BFD_RELOC_CR16_REGREL20a;
    902  1.1  skrll                 break;
    903  1.1  skrll               default:
    904  1.1  skrll                 break;
    905  1.1  skrll               }
    906  1.1  skrll           break;
    907  1.1  skrll 
    908  1.1  skrll         case arg_idxr:
    909  1.1  skrll           if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
    910  1.1  skrll             cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
    911  1.1  skrll           break;
    912  1.1  skrll 
    913  1.1  skrll         case arg_idxrp:
    914  1.1  skrll           if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
    915  1.1  skrll             switch (instruction->size)
    916  1.1  skrll               {
    917  1.1  skrll               case 1: cr16_ins->rtype = BFD_RELOC_CR16_REGREL0; break;
    918  1.1  skrll               case 2: cr16_ins->rtype = BFD_RELOC_CR16_REGREL14; break;
    919  1.1  skrll               case 3: cr16_ins->rtype = BFD_RELOC_CR16_REGREL20; break;
    920  1.1  skrll               default: break;
    921  1.1  skrll               }
    922  1.1  skrll           break;
    923  1.1  skrll 
    924  1.1  skrll         case arg_c:
    925  1.1  skrll           if (IS_INSN_MNEMONIC ("bal"))
    926  1.1  skrll             cr16_ins->rtype = BFD_RELOC_CR16_DISP24;
    927  1.1  skrll           else if (IS_INSN_TYPE (BRANCH_INS))
    928  1.1  skrll             {
    929  1.1  skrll               if (symbol_with_l)
    930  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_DISP24;
    931  1.1  skrll               else if (symbol_with_m)
    932  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_DISP16;
    933  1.1  skrll               else
    934  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_DISP8;
    935  1.1  skrll             }
    936  1.1  skrll           else if (IS_INSN_TYPE (STOR_IMM_INS) || IS_INSN_TYPE (LD_STOR_INS)
    937  1.1  skrll                    || IS_INSN_TYPE (CSTBIT_INS))
    938  1.1  skrll             {
    939  1.1  skrll               if (symbol_with_s)
    940  1.1  skrll                 as_bad (_("operand %d: illegal use expression: `%s`"), cur_arg_num + 1, str);
    941  1.1  skrll               if (symbol_with_m)
    942  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_ABS20;
    943  1.1  skrll               else /* Default to (symbol_with_l) */
    944  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_ABS24;
    945  1.1  skrll             }
    946  1.1  skrll           else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
    947  1.1  skrll             cr16_ins->rtype = BFD_RELOC_CR16_DISP4;
    948  1.1  skrll           break;
    949  1.1  skrll 
    950  1.1  skrll         case arg_ic:
    951  1.1  skrll           if (IS_INSN_TYPE (ARITH_INS))
    952  1.1  skrll             {
    953  1.1  skrll               if (symbol_with_s)
    954  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_IMM4;
    955  1.1  skrll               else if (symbol_with_m)
    956  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_IMM20;
    957  1.1  skrll               else if (symbol_with_at)
    958  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_IMM32a;
    959  1.1  skrll               else /* Default to (symbol_with_l) */
    960  1.1  skrll                 cr16_ins->rtype = BFD_RELOC_CR16_IMM32;
    961  1.1  skrll             }
    962  1.1  skrll           else if (IS_INSN_TYPE (ARITH_BYTE_INS))
    963  1.1  skrll             {
    964  1.1  skrll               cr16_ins->rtype = BFD_RELOC_CR16_IMM16;
    965  1.1  skrll             }
    966  1.1  skrll           break;
    967  1.1  skrll         default:
    968  1.1  skrll           break;
    969  1.1  skrll         }
    970  1.1  skrll       break;
    971  1.1  skrll 
    972  1.1  skrll     default:
    973  1.1  skrll       cur_arg->X_op = cr16_ins->exp.X_op;
    974  1.1  skrll       break;
    975  1.1  skrll     }
    976  1.1  skrll 
    977  1.1  skrll   input_line_pointer = saved_input_line_pointer;
    978  1.1  skrll   return;
    979  1.1  skrll }
    980  1.1  skrll 
    981  1.1  skrll /* Retrieve the opcode image of a given register.
    982  1.1  skrll    If the register is illegal for the current instruction,
    983  1.1  skrll    issue an error.  */
    984  1.1  skrll 
    985  1.1  skrll static int
    986  1.1  skrll getreg_image (reg r)
    987  1.1  skrll {
    988  1.1  skrll   const reg_entry *reg;
    989  1.1  skrll   char *reg_name;
    990  1.1  skrll   int is_procreg = 0; /* Nonzero means argument should be processor reg.  */
    991  1.1  skrll 
    992  1.1  skrll   /* Check whether the register is in registers table.  */
    993  1.1  skrll   if (r < MAX_REG)
    994  1.1  skrll     reg = cr16_regtab + r;
    995  1.1  skrll   else /* Register not found.  */
    996  1.1  skrll     {
    997  1.1  skrll       as_bad (_("Unknown register: `%d'"), r);
    998  1.1  skrll       return 0;
    999  1.1  skrll     }
   1000  1.1  skrll 
   1001  1.1  skrll   reg_name = reg->name;
   1002  1.1  skrll 
   1003  1.1  skrll /* Issue a error message when register is illegal.  */
   1004  1.1  skrll #define IMAGE_ERR \
   1005  1.1  skrll   as_bad (_("Illegal register (`%s') in Instruction: `%s'"), \
   1006  1.1  skrll             reg_name, ins_parse);                            \
   1007  1.1  skrll   break;
   1008  1.1  skrll 
   1009  1.1  skrll   switch (reg->type)
   1010  1.1  skrll     {
   1011  1.1  skrll     case CR16_R_REGTYPE:
   1012  1.1  skrll       if (! is_procreg)
   1013  1.1  skrll         return reg->image;
   1014  1.1  skrll       else
   1015  1.1  skrll         IMAGE_ERR;
   1016  1.1  skrll 
   1017  1.1  skrll     case CR16_P_REGTYPE:
   1018  1.1  skrll       return reg->image;
   1019  1.1  skrll       break;
   1020  1.1  skrll 
   1021  1.1  skrll     default:
   1022  1.1  skrll       IMAGE_ERR;
   1023  1.1  skrll     }
   1024  1.1  skrll 
   1025  1.1  skrll   return 0;
   1026  1.1  skrll }
   1027  1.1  skrll 
   1028  1.1  skrll /* Parsing different types of operands
   1029  1.1  skrll    -> constants             Immediate/Absolute/Relative numbers
   1030  1.1  skrll    -> Labels                Relocatable symbols
   1031  1.1  skrll    -> (reg pair base)       Register pair base
   1032  1.1  skrll    -> (rbase)               Register base
   1033  1.1  skrll    -> disp(rbase)           Register relative
   1034  1.1  skrll    -> [rinx]disp(reg pair)  Register index with reg pair mode
   1035  1.1  skrll    -> disp(rbase,ridx,scl)  Register index mode.  */
   1036  1.1  skrll 
   1037  1.1  skrll static void
   1038  1.1  skrll set_operand (char *operand, ins * cr16_ins)
   1039  1.1  skrll {
   1040  1.1  skrll   char *operandS; /* Pointer to start of sub-opearand.  */
   1041  1.1  skrll   char *operandE; /* Pointer to end of sub-opearand.  */
   1042  1.1  skrll 
   1043  1.1  skrll   argument *cur_arg = &cr16_ins->arg[cur_arg_num]; /* Current argument.  */
   1044  1.1  skrll 
   1045  1.1  skrll   /* Initialize pointers.  */
   1046  1.1  skrll   operandS = operandE = operand;
   1047  1.1  skrll 
   1048  1.1  skrll   switch (cur_arg->type)
   1049  1.1  skrll     {
   1050  1.1  skrll     case arg_ic:    /* Case $0x18.  */
   1051  1.1  skrll       operandS++;
   1052  1.1  skrll     case arg_c:     /* Case 0x18.  */
   1053  1.1  skrll       /* Set constant.  */
   1054  1.1  skrll       process_label_constant (operandS, cr16_ins);
   1055  1.1  skrll 
   1056  1.1  skrll       if (cur_arg->type != arg_ic)
   1057  1.1  skrll         cur_arg->type = arg_c;
   1058  1.1  skrll       break;
   1059  1.1  skrll 
   1060  1.1  skrll     case arg_icr:   /* Case $0x18(r1).  */
   1061  1.1  skrll       operandS++;
   1062  1.1  skrll     case arg_cr:    /* Case 0x18(r1).   */
   1063  1.1  skrll       /* Set displacement constant.  */
   1064  1.1  skrll       while (*operandE != '(')
   1065  1.1  skrll         operandE++;
   1066  1.1  skrll       *operandE = '\0';
   1067  1.1  skrll       process_label_constant (operandS, cr16_ins);
   1068  1.1  skrll       operandS = operandE;
   1069  1.1  skrll     case arg_rbase: /* Case (r1) or (r1,r0).  */
   1070  1.1  skrll       operandS++;
   1071  1.1  skrll       /* Set register base.  */
   1072  1.1  skrll       while (*operandE != ')')
   1073  1.1  skrll         operandE++;
   1074  1.1  skrll       *operandE = '\0';
   1075  1.1  skrll       if ((cur_arg->r = get_register (operandS)) == nullregister)
   1076  1.1  skrll          as_bad (_("Illegal register `%s' in Instruction `%s'"),
   1077  1.1  skrll               operandS, ins_parse);
   1078  1.1  skrll 
   1079  1.1  skrll       /* set the arg->rp, if reg is "r12" or "r13" or "14" or "15" */
   1080  1.1  skrll       if ((cur_arg->type != arg_rbase)
   1081  1.1  skrll           && ((getreg_image (cur_arg->r) == 12)
   1082  1.1  skrll               || (getreg_image (cur_arg->r) == 13)
   1083  1.1  skrll               || (getreg_image (cur_arg->r) == 14)
   1084  1.1  skrll               || (getreg_image (cur_arg->r) == 15)))
   1085  1.1  skrll          {
   1086  1.1  skrll            cur_arg->type = arg_crp;
   1087  1.1  skrll            cur_arg->rp = cur_arg->r;
   1088  1.1  skrll          }
   1089  1.1  skrll       break;
   1090  1.1  skrll 
   1091  1.1  skrll     case arg_crp:    /* Case 0x18(r1,r0).   */
   1092  1.1  skrll       /* Set displacement constant.  */
   1093  1.1  skrll       while (*operandE != '(')
   1094  1.1  skrll         operandE++;
   1095  1.1  skrll       *operandE = '\0';
   1096  1.1  skrll       process_label_constant (operandS, cr16_ins);
   1097  1.1  skrll       operandS = operandE;
   1098  1.1  skrll       operandS++;
   1099  1.1  skrll       /* Set register pair base.  */
   1100  1.1  skrll       while (*operandE != ')')
   1101  1.1  skrll         operandE++;
   1102  1.1  skrll       *operandE = '\0';
   1103  1.1  skrll       if ((cur_arg->rp = get_register_pair (operandS)) == nullregister)
   1104  1.1  skrll          as_bad (_("Illegal register pair `%s' in Instruction `%s'"),
   1105  1.1  skrll               operandS, ins_parse);
   1106  1.1  skrll       break;
   1107  1.1  skrll 
   1108  1.1  skrll     case arg_idxr:
   1109  1.1  skrll       /* Set register pair base.  */
   1110  1.1  skrll       if ((strchr (operandS,'(') != NULL))
   1111  1.1  skrll         {
   1112  1.1  skrll          while ((*operandE != '(') && (! ISSPACE (*operandE)))
   1113  1.1  skrll            operandE++;
   1114  1.1  skrll          if ((cur_arg->rp = get_index_register_pair (operandE)) == nullregister)
   1115  1.1  skrll               as_bad (_("Illegal register pair `%s' in Instruction `%s'"),
   1116  1.1  skrll                             operandS, ins_parse);
   1117  1.1  skrll          *operandE++ = '\0';
   1118  1.1  skrll          cur_arg->type = arg_idxrp;
   1119  1.1  skrll         }
   1120  1.1  skrll       else
   1121  1.1  skrll         cur_arg->rp = -1;
   1122  1.1  skrll 
   1123  1.1  skrll        operandE = operandS;
   1124  1.1  skrll       /* Set displacement constant.  */
   1125  1.1  skrll       while (*operandE != ']')
   1126  1.1  skrll         operandE++;
   1127  1.1  skrll       process_label_constant (++operandE, cr16_ins);
   1128  1.1  skrll       *operandE++ = '\0';
   1129  1.1  skrll       operandE = operandS;
   1130  1.1  skrll 
   1131  1.1  skrll       /* Set index register .  */
   1132  1.1  skrll       operandS = strchr (operandE,'[');
   1133  1.1  skrll       if (operandS != NULL)
   1134  1.1  skrll         { /* Eliminate '[', detach from rest of operand.  */
   1135  1.1  skrll           *operandS++ = '\0';
   1136  1.1  skrll 
   1137  1.1  skrll           operandE = strchr (operandS, ']');
   1138  1.1  skrll 
   1139  1.1  skrll           if (operandE == NULL)
   1140  1.1  skrll             as_bad (_("unmatched '['"));
   1141  1.1  skrll           else
   1142  1.1  skrll             { /* Eliminate ']' and make sure it was the last thing
   1143  1.1  skrll                  in the string.  */
   1144  1.1  skrll               *operandE = '\0';
   1145  1.1  skrll               if (*(operandE + 1) != '\0')
   1146  1.1  skrll                 as_bad (_("garbage after index spec ignored"));
   1147  1.1  skrll             }
   1148  1.1  skrll         }
   1149  1.1  skrll 
   1150  1.1  skrll       if ((cur_arg->i_r = get_index_register (operandS)) == nullregister)
   1151  1.1  skrll         as_bad (_("Illegal register `%s' in Instruction `%s'"),
   1152  1.1  skrll                 operandS, ins_parse);
   1153  1.1  skrll       *operandE = '\0';
   1154  1.1  skrll       *operandS = '\0';
   1155  1.1  skrll       break;
   1156  1.1  skrll 
   1157  1.1  skrll     default:
   1158  1.1  skrll       break;
   1159  1.1  skrll     }
   1160  1.1  skrll }
   1161  1.1  skrll 
   1162  1.1  skrll /* Parse a single operand.
   1163  1.1  skrll    operand - Current operand to parse.
   1164  1.1  skrll    cr16_ins - Current assembled instruction.  */
   1165  1.1  skrll 
   1166  1.1  skrll static void
   1167  1.1  skrll parse_operand (char *operand, ins * cr16_ins)
   1168  1.1  skrll {
   1169  1.1  skrll   int ret_val;
   1170  1.1  skrll   argument *cur_arg = cr16_ins->arg + cur_arg_num; /* Current argument.  */
   1171  1.1  skrll 
   1172  1.1  skrll   /* Initialize the type to NULL before parsing.  */
   1173  1.1  skrll   cur_arg->type = nullargs;
   1174  1.1  skrll 
   1175  1.1  skrll   /* Check whether this is a condition code .  */
   1176  1.1  skrll   if ((IS_INSN_MNEMONIC ("b")) && ((ret_val = get_cc (operand)) != -1))
   1177  1.1  skrll     {
   1178  1.1  skrll       cur_arg->type = arg_cc;
   1179  1.1  skrll       cur_arg->cc = ret_val;
   1180  1.1  skrll       cur_arg->X_op = O_register;
   1181  1.1  skrll       return;
   1182  1.1  skrll     }
   1183  1.1  skrll 
   1184  1.1  skrll   /* Check whether this is a general processor register.  */
   1185  1.1  skrll   if ((ret_val = get_register (operand)) != nullregister)
   1186  1.1  skrll     {
   1187  1.1  skrll       cur_arg->type = arg_r;
   1188  1.1  skrll       cur_arg->r = ret_val;
   1189  1.1  skrll       cur_arg->X_op = 0;
   1190  1.1  skrll       return;
   1191  1.1  skrll     }
   1192  1.1  skrll 
   1193  1.1  skrll   /* Check whether this is a general processor register pair.  */
   1194  1.1  skrll   if ((operand[0] == '(')
   1195  1.1  skrll       && ((ret_val = get_register_pair (operand)) != nullregister))
   1196  1.1  skrll     {
   1197  1.1  skrll       cur_arg->type = arg_rp;
   1198  1.1  skrll       cur_arg->rp = ret_val;
   1199  1.1  skrll       cur_arg->X_op = O_register;
   1200  1.1  skrll       return;
   1201  1.1  skrll     }
   1202  1.1  skrll 
   1203  1.1  skrll   /* Check whether the operand is a processor register.
   1204  1.1  skrll      For "lprd" and "sprd" instruction, only 32 bit
   1205  1.1  skrll      processor registers used.  */
   1206  1.1  skrll   if (!(IS_INSN_MNEMONIC ("lprd") || (IS_INSN_MNEMONIC ("sprd")))
   1207  1.1  skrll       && ((ret_val = get_pregister (operand)) != nullpregister))
   1208  1.1  skrll     {
   1209  1.1  skrll       cur_arg->type = arg_pr;
   1210  1.1  skrll       cur_arg->pr = ret_val;
   1211  1.1  skrll       cur_arg->X_op = O_register;
   1212  1.1  skrll       return;
   1213  1.1  skrll     }
   1214  1.1  skrll 
   1215  1.1  skrll   /* Check whether this is a processor register - 32 bit.  */
   1216  1.1  skrll   if ((ret_val = get_pregisterp (operand)) != nullpregister)
   1217  1.1  skrll     {
   1218  1.1  skrll       cur_arg->type = arg_prp;
   1219  1.1  skrll       cur_arg->prp = ret_val;
   1220  1.1  skrll       cur_arg->X_op = O_register;
   1221  1.1  skrll       return;
   1222  1.1  skrll     }
   1223  1.1  skrll 
   1224  1.1  skrll   /* Deal with special characters.  */
   1225  1.1  skrll   switch (operand[0])
   1226  1.1  skrll     {
   1227  1.1  skrll     case '$':
   1228  1.1  skrll       if (strchr (operand, '(') != NULL)
   1229  1.1  skrll         cur_arg->type = arg_icr;
   1230  1.1  skrll       else
   1231  1.1  skrll         cur_arg->type = arg_ic;
   1232  1.1  skrll       goto set_params;
   1233  1.1  skrll       break;
   1234  1.1  skrll 
   1235  1.1  skrll     case '(':
   1236  1.1  skrll       cur_arg->type = arg_rbase;
   1237  1.1  skrll       goto set_params;
   1238  1.1  skrll       break;
   1239  1.1  skrll 
   1240  1.1  skrll     case '[':
   1241  1.1  skrll       cur_arg->type = arg_idxr;
   1242  1.1  skrll       goto set_params;
   1243  1.1  skrll       break;
   1244  1.1  skrll 
   1245  1.1  skrll     default:
   1246  1.1  skrll       break;
   1247  1.1  skrll     }
   1248  1.1  skrll 
   1249  1.1  skrll   if (strchr (operand, '(') != NULL)
   1250  1.1  skrll     {
   1251  1.1  skrll       if (strchr (operand, ',') != NULL
   1252  1.1  skrll           && (strchr (operand, ',') > strchr (operand, '(')))
   1253  1.1  skrll         cur_arg->type = arg_crp;
   1254  1.1  skrll       else
   1255  1.1  skrll         cur_arg->type = arg_cr;
   1256  1.1  skrll     }
   1257  1.1  skrll   else
   1258  1.1  skrll     cur_arg->type = arg_c;
   1259  1.1  skrll 
   1260  1.1  skrll /* Parse an operand according to its type.  */
   1261  1.1  skrll  set_params:
   1262  1.1  skrll   cur_arg->constant = 0;
   1263  1.1  skrll   set_operand (operand, cr16_ins);
   1264  1.1  skrll }
   1265  1.1  skrll 
   1266  1.1  skrll /* Parse the various operands. Each operand is then analyzed to fillup
   1267  1.1  skrll    the fields in the cr16_ins data structure.  */
   1268  1.1  skrll 
   1269  1.1  skrll static void
   1270  1.1  skrll parse_operands (ins * cr16_ins, char *operands)
   1271  1.1  skrll {
   1272  1.1  skrll   char *operandS;            /* Operands string.  */
   1273  1.1  skrll   char *operandH, *operandT; /* Single operand head/tail pointers.  */
   1274  1.1  skrll   int allocated = 0;         /* Indicates a new operands string was allocated.*/
   1275  1.1  skrll   char *operand[MAX_OPERANDS];/* Separating the operands.  */
   1276  1.1  skrll   int op_num = 0;             /* Current operand number we are parsing.  */
   1277  1.1  skrll   int bracket_flag = 0;       /* Indicates a bracket '(' was found.  */
   1278  1.1  skrll   int sq_bracket_flag = 0;    /* Indicates a square bracket '[' was found.  */
   1279  1.1  skrll 
   1280  1.1  skrll   /* Preprocess the list of registers, if necessary.  */
   1281  1.1  skrll   operandS = operandH = operandT = operands;
   1282  1.1  skrll 
   1283  1.1  skrll   while (*operandT != '\0')
   1284  1.1  skrll     {
   1285  1.1  skrll       if (*operandT == ',' && bracket_flag != 1 && sq_bracket_flag != 1)
   1286  1.1  skrll         {
   1287  1.1  skrll           *operandT++ = '\0';
   1288  1.1  skrll           operand[op_num++] = strdup (operandH);
   1289  1.1  skrll           operandH = operandT;
   1290  1.1  skrll           continue;
   1291  1.1  skrll         }
   1292  1.1  skrll 
   1293  1.1  skrll       if (*operandT == ' ')
   1294  1.1  skrll         as_bad (_("Illegal operands (whitespace): `%s'"), ins_parse);
   1295  1.1  skrll 
   1296  1.1  skrll       if (*operandT == '(')
   1297  1.1  skrll         bracket_flag = 1;
   1298  1.1  skrll       else if (*operandT == '[')
   1299  1.1  skrll         sq_bracket_flag = 1;
   1300  1.1  skrll 
   1301  1.1  skrll       if (*operandT == ')')
   1302  1.1  skrll         {
   1303  1.1  skrll           if (bracket_flag)
   1304  1.1  skrll             bracket_flag = 0;
   1305  1.1  skrll           else
   1306  1.1  skrll             as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
   1307  1.1  skrll         }
   1308  1.1  skrll       else if (*operandT == ']')
   1309  1.1  skrll         {
   1310  1.1  skrll           if (sq_bracket_flag)
   1311  1.1  skrll             sq_bracket_flag = 0;
   1312  1.1  skrll           else
   1313  1.1  skrll             as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
   1314  1.1  skrll         }
   1315  1.1  skrll 
   1316  1.1  skrll       if (bracket_flag == 1 && *operandT == ')')
   1317  1.1  skrll         bracket_flag = 0;
   1318  1.1  skrll       else if (sq_bracket_flag == 1 && *operandT == ']')
   1319  1.1  skrll         sq_bracket_flag = 0;
   1320  1.1  skrll 
   1321  1.1  skrll       operandT++;
   1322  1.1  skrll     }
   1323  1.1  skrll 
   1324  1.1  skrll   /* Adding the last operand.  */
   1325  1.1  skrll   operand[op_num++] = strdup (operandH);
   1326  1.1  skrll   cr16_ins->nargs = op_num;
   1327  1.1  skrll 
   1328  1.1  skrll   /* Verifying correct syntax of operands (all brackets should be closed).  */
   1329  1.1  skrll   if (bracket_flag || sq_bracket_flag)
   1330  1.1  skrll     as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
   1331  1.1  skrll 
   1332  1.1  skrll   /* Now we parse each operand separately.  */
   1333  1.1  skrll   for (op_num = 0; op_num < cr16_ins->nargs; op_num++)
   1334  1.1  skrll     {
   1335  1.1  skrll       cur_arg_num = op_num;
   1336  1.1  skrll       parse_operand (operand[op_num], cr16_ins);
   1337  1.1  skrll       free (operand[op_num]);
   1338  1.1  skrll     }
   1339  1.1  skrll 
   1340  1.1  skrll   if (allocated)
   1341  1.1  skrll     free (operandS);
   1342  1.1  skrll }
   1343  1.1  skrll 
   1344  1.1  skrll /* Get the trap index in dispatch table, given its name.
   1345  1.1  skrll    This routine is used by assembling the 'excp' instruction.  */
   1346  1.1  skrll 
   1347  1.1  skrll static int
   1348  1.1  skrll gettrap (char *s)
   1349  1.1  skrll {
   1350  1.1  skrll   const trap_entry *trap;
   1351  1.1  skrll 
   1352  1.1  skrll   for (trap = cr16_traps; trap < (cr16_traps + NUMTRAPS); trap++)
   1353  1.1  skrll     if (strcasecmp (trap->name, s) == 0)
   1354  1.1  skrll       return trap->entry;
   1355  1.1  skrll 
   1356  1.1  skrll   /* To make compatable with CR16 4.1 tools, the below 3-lines of
   1357  1.1  skrll    * code added. Refer: Development Tracker item #123 */
   1358  1.1  skrll   for (trap = cr16_traps; trap < (cr16_traps + NUMTRAPS); trap++)
   1359  1.1  skrll     if (trap->entry  == (unsigned int) atoi (s))
   1360  1.1  skrll       return trap->entry;
   1361  1.1  skrll 
   1362  1.1  skrll   as_bad (_("Unknown exception: `%s'"), s);
   1363  1.1  skrll   return 0;
   1364  1.1  skrll }
   1365  1.1  skrll 
   1366  1.1  skrll /* Top level module where instruction parsing starts.
   1367  1.1  skrll    cr16_ins - data structure holds some information.
   1368  1.1  skrll    operands - holds the operands part of the whole instruction.  */
   1369  1.1  skrll 
   1370  1.1  skrll static void
   1371  1.1  skrll parse_insn (ins *insn, char *operands)
   1372  1.1  skrll {
   1373  1.1  skrll   int i;
   1374  1.1  skrll 
   1375  1.1  skrll   /* Handle instructions with no operands.  */
   1376  1.1  skrll   for (i = 0; cr16_no_op_insn[i] != NULL; i++)
   1377  1.1  skrll   {
   1378  1.1  skrll     if (streq (cr16_no_op_insn[i], instruction->mnemonic))
   1379  1.1  skrll     {
   1380  1.1  skrll       insn->nargs = 0;
   1381  1.1  skrll       return;
   1382  1.1  skrll     }
   1383  1.1  skrll   }
   1384  1.1  skrll 
   1385  1.1  skrll   /* Handle 'excp' instructions.  */
   1386  1.1  skrll   if (IS_INSN_MNEMONIC ("excp"))
   1387  1.1  skrll     {
   1388  1.1  skrll       insn->nargs = 1;
   1389  1.1  skrll       insn->arg[0].type = arg_ic;
   1390  1.1  skrll       insn->arg[0].constant = gettrap (operands);
   1391  1.1  skrll       insn->arg[0].X_op = O_constant;
   1392  1.1  skrll       return;
   1393  1.1  skrll     }
   1394  1.1  skrll 
   1395  1.1  skrll   if (operands != NULL)
   1396  1.1  skrll     parse_operands (insn, operands);
   1397  1.1  skrll }
   1398  1.1  skrll 
   1399  1.1  skrll /* bCC instruction requires special handling.  */
   1400  1.1  skrll static char *
   1401  1.1  skrll get_b_cc (char * op)
   1402  1.1  skrll {
   1403  1.1  skrll   unsigned int i;
   1404  1.1  skrll   char op1[5];
   1405  1.1  skrll 
   1406  1.1  skrll   for (i = 1; i < strlen (op); i++)
   1407  1.1  skrll      op1[i-1] = op[i];
   1408  1.1  skrll 
   1409  1.1  skrll   op1[i-1] = '\0';
   1410  1.1  skrll 
   1411  1.1  skrll   for (i = 0; i < cr16_num_cc ; i++)
   1412  1.1  skrll     if (streq (op1, cr16_b_cond_tab[i]))
   1413  1.1  skrll       return (char *) cr16_b_cond_tab[i];
   1414  1.1  skrll 
   1415  1.1  skrll    return NULL;
   1416  1.1  skrll }
   1417  1.1  skrll 
   1418  1.1  skrll /* bCC instruction requires special handling.  */
   1419  1.1  skrll static int
   1420  1.1  skrll is_bcc_insn (char * op)
   1421  1.1  skrll {
   1422  1.1  skrll   if (!(streq (op, "bal") || streq (op, "beq0b") || streq (op, "bnq0b")
   1423  1.1  skrll         || streq (op, "beq0w") || streq (op, "bnq0w")))
   1424  1.1  skrll     if ((op[0] == 'b') && (get_b_cc (op) != NULL))
   1425  1.1  skrll       return 1;
   1426  1.1  skrll   return 0;
   1427  1.1  skrll }
   1428  1.1  skrll 
   1429  1.1  skrll /* Cinv instruction requires special handling.  */
   1430  1.1  skrll 
   1431  1.1  skrll static int
   1432  1.1  skrll check_cinv_options (char * operand)
   1433  1.1  skrll {
   1434  1.1  skrll   char *p = operand;
   1435  1.1  skrll   int i_used = 0, u_used = 0, d_used = 0;
   1436  1.1  skrll 
   1437  1.1  skrll   while (*++p != ']')
   1438  1.1  skrll     {
   1439  1.1  skrll       if (*p == ',' || *p == ' ')
   1440  1.1  skrll         continue;
   1441  1.1  skrll 
   1442  1.1  skrll       else if (*p == 'i')
   1443  1.1  skrll         i_used = 1;
   1444  1.1  skrll       else if (*p == 'u')
   1445  1.1  skrll         u_used = 1;
   1446  1.1  skrll       else if (*p == 'd')
   1447  1.1  skrll         d_used = 1;
   1448  1.1  skrll       else
   1449  1.1  skrll         as_bad (_("Illegal `cinv' parameter: `%c'"), *p);
   1450  1.1  skrll     }
   1451  1.1  skrll 
   1452  1.1  skrll   return 0;
   1453  1.1  skrll }
   1454  1.1  skrll 
   1455  1.1  skrll /* Retrieve the opcode image of a given register pair.
   1456  1.1  skrll    If the register is illegal for the current instruction,
   1457  1.1  skrll    issue an error.  */
   1458  1.1  skrll 
   1459  1.1  skrll static int
   1460  1.1  skrll getregp_image (reg r)
   1461  1.1  skrll {
   1462  1.1  skrll   const reg_entry *reg;
   1463  1.1  skrll   char *reg_name;
   1464  1.1  skrll 
   1465  1.1  skrll   /* Check whether the register is in registers table.  */
   1466  1.1  skrll   if (r < MAX_REG)
   1467  1.1  skrll     reg = cr16_regptab + r;
   1468  1.1  skrll   /* Register not found.  */
   1469  1.1  skrll   else
   1470  1.1  skrll     {
   1471  1.1  skrll       as_bad (_("Unknown register pair: `%d'"), r);
   1472  1.1  skrll       return 0;
   1473  1.1  skrll     }
   1474  1.1  skrll 
   1475  1.1  skrll   reg_name = reg->name;
   1476  1.1  skrll 
   1477  1.1  skrll /* Issue a error message when register  pair is illegal.  */
   1478  1.1  skrll #define RPAIR_IMAGE_ERR \
   1479  1.1  skrll   as_bad (_("Illegal register pair (`%s') in Instruction: `%s'"), \
   1480  1.1  skrll             reg_name, ins_parse);                                 \
   1481  1.1  skrll   break;
   1482  1.1  skrll 
   1483  1.1  skrll   switch (reg->type)
   1484  1.1  skrll     {
   1485  1.1  skrll     case CR16_RP_REGTYPE:
   1486  1.1  skrll       return reg->image;
   1487  1.1  skrll     default:
   1488  1.1  skrll       RPAIR_IMAGE_ERR;
   1489  1.1  skrll     }
   1490  1.1  skrll 
   1491  1.1  skrll   return 0;
   1492  1.1  skrll }
   1493  1.1  skrll 
   1494  1.1  skrll /* Retrieve the opcode image of a given index register pair.
   1495  1.1  skrll    If the register is illegal for the current instruction,
   1496  1.1  skrll    issue an error.  */
   1497  1.1  skrll 
   1498  1.1  skrll static int
   1499  1.1  skrll getidxregp_image (reg r)
   1500  1.1  skrll {
   1501  1.1  skrll   const reg_entry *reg;
   1502  1.1  skrll   char *reg_name;
   1503  1.1  skrll 
   1504  1.1  skrll   /* Check whether the register is in registers table.  */
   1505  1.1  skrll   if (r < MAX_REG)
   1506  1.1  skrll     reg = cr16_regptab + r;
   1507  1.1  skrll   /* Register not found.  */
   1508  1.1  skrll   else
   1509  1.1  skrll     {
   1510  1.1  skrll       as_bad (_("Unknown register pair: `%d'"), r);
   1511  1.1  skrll       return 0;
   1512  1.1  skrll     }
   1513  1.1  skrll 
   1514  1.1  skrll   reg_name = reg->name;
   1515  1.1  skrll 
   1516  1.1  skrll /* Issue a error message when register  pair is illegal.  */
   1517  1.1  skrll #define IDX_RPAIR_IMAGE_ERR \
   1518  1.1  skrll   as_bad (_("Illegal index register pair (`%s') in Instruction: `%s'"), \
   1519  1.1  skrll             reg_name, ins_parse);                                       \
   1520  1.1  skrll 
   1521  1.1  skrll   if (reg->type == CR16_RP_REGTYPE)
   1522  1.1  skrll     {
   1523  1.1  skrll       switch (reg->image)
   1524  1.1  skrll         {
   1525  1.1  skrll         case 0:  return 0; break;
   1526  1.1  skrll         case 2:  return 1; break;
   1527  1.1  skrll         case 4:  return 2; break;
   1528  1.1  skrll         case 6:  return 3; break;
   1529  1.1  skrll         case 8:  return 4; break;
   1530  1.1  skrll         case 10: return 5; break;
   1531  1.1  skrll         case 3:  return 6; break;
   1532  1.1  skrll         case 5:  return 7; break;
   1533  1.1  skrll         default:
   1534  1.1  skrll           break;
   1535  1.1  skrll         }
   1536  1.1  skrll     }
   1537  1.1  skrll 
   1538  1.1  skrll   IDX_RPAIR_IMAGE_ERR;
   1539  1.1  skrll   return 0;
   1540  1.1  skrll }
   1541  1.1  skrll 
   1542  1.1  skrll /* Retrieve the opcode image of a given processort register.
   1543  1.1  skrll    If the register is illegal for the current instruction,
   1544  1.1  skrll    issue an error.  */
   1545  1.1  skrll static int
   1546  1.1  skrll getprocreg_image (reg r)
   1547  1.1  skrll {
   1548  1.1  skrll   const reg_entry *reg;
   1549  1.1  skrll   char *reg_name;
   1550  1.1  skrll 
   1551  1.1  skrll   /* Check whether the register is in registers table.  */
   1552  1.1  skrll   if (r < MAX_PREG)
   1553  1.1  skrll     reg = &cr16_pregtab[r - MAX_REG];
   1554  1.1  skrll   /* Register not found.  */
   1555  1.1  skrll   else
   1556  1.1  skrll     {
   1557  1.1  skrll       as_bad (_("Unknown processor register : `%d'"), r);
   1558  1.1  skrll       return 0;
   1559  1.1  skrll     }
   1560  1.1  skrll 
   1561  1.1  skrll   reg_name = reg->name;
   1562  1.1  skrll 
   1563  1.1  skrll /* Issue a error message when register  pair is illegal.  */
   1564  1.1  skrll #define PROCREG_IMAGE_ERR \
   1565  1.1  skrll   as_bad (_("Illegal processor register (`%s') in Instruction: `%s'"), \
   1566  1.1  skrll             reg_name, ins_parse);                                      \
   1567  1.1  skrll   break;
   1568  1.1  skrll 
   1569  1.1  skrll   switch (reg->type)
   1570  1.1  skrll     {
   1571  1.1  skrll     case CR16_P_REGTYPE:
   1572  1.1  skrll       return reg->image;
   1573  1.1  skrll     default:
   1574  1.1  skrll       PROCREG_IMAGE_ERR;
   1575  1.1  skrll     }
   1576  1.1  skrll 
   1577  1.1  skrll   return 0;
   1578  1.1  skrll }
   1579  1.1  skrll 
   1580  1.1  skrll /* Retrieve the opcode image of a given processort register.
   1581  1.1  skrll    If the register is illegal for the current instruction,
   1582  1.1  skrll    issue an error.  */
   1583  1.1  skrll static int
   1584  1.1  skrll getprocregp_image (reg r)
   1585  1.1  skrll {
   1586  1.1  skrll   const reg_entry *reg;
   1587  1.1  skrll   char *reg_name;
   1588  1.1  skrll   int pregptab_disp = 0;
   1589  1.1  skrll 
   1590  1.1  skrll   /* Check whether the register is in registers table.  */
   1591  1.1  skrll   if (r < MAX_PREG)
   1592  1.1  skrll     {
   1593  1.1  skrll       r = r - MAX_REG;
   1594  1.1  skrll       switch (r)
   1595  1.1  skrll         {
   1596  1.1  skrll         case 4: pregptab_disp = 1;  break;
   1597  1.1  skrll         case 6: pregptab_disp = 2;  break;
   1598  1.1  skrll         case 8:
   1599  1.1  skrll         case 9:
   1600  1.1  skrll         case 10:
   1601  1.1  skrll           pregptab_disp = 3;  break;
   1602  1.1  skrll         case 12:
   1603  1.1  skrll           pregptab_disp = 4;  break;
   1604  1.1  skrll         case 14:
   1605  1.1  skrll           pregptab_disp = 5;  break;
   1606  1.1  skrll         default: break;
   1607  1.1  skrll         }
   1608  1.1  skrll       reg = &cr16_pregptab[r - pregptab_disp];
   1609  1.1  skrll     }
   1610  1.1  skrll   /* Register not found.  */
   1611  1.1  skrll   else
   1612  1.1  skrll     {
   1613  1.1  skrll       as_bad (_("Unknown processor register (32 bit) : `%d'"), r);
   1614  1.1  skrll       return 0;
   1615  1.1  skrll     }
   1616  1.1  skrll 
   1617  1.1  skrll   reg_name = reg->name;
   1618  1.1  skrll 
   1619  1.1  skrll /* Issue a error message when register  pair is illegal.  */
   1620  1.1  skrll #define PROCREGP_IMAGE_ERR \
   1621  1.1  skrll   as_bad (_("Illegal 32 bit - processor register (`%s') in Instruction: `%s'"),\
   1622  1.1  skrll             reg_name, ins_parse);                                              \
   1623  1.1  skrll   break;
   1624  1.1  skrll 
   1625  1.1  skrll   switch (reg->type)
   1626  1.1  skrll     {
   1627  1.1  skrll     case CR16_P_REGTYPE:
   1628  1.1  skrll       return reg->image;
   1629  1.1  skrll     default:
   1630  1.1  skrll       PROCREGP_IMAGE_ERR;
   1631  1.1  skrll     }
   1632  1.1  skrll 
   1633  1.1  skrll   return 0;
   1634  1.1  skrll }
   1635  1.1  skrll 
   1636  1.1  skrll /* Routine used to represent integer X using NBITS bits.  */
   1637  1.1  skrll 
   1638  1.1  skrll static long
   1639  1.1  skrll getconstant (long x, int nbits)
   1640  1.1  skrll {
   1641  1.1  skrll   /* The following expression avoids overflow if
   1642  1.1  skrll      'nbits' is the number of bits in 'bfd_vma'.  */
   1643  1.1  skrll   return (x & ((((1 << (nbits - 1)) - 1) << 1) | 1));
   1644  1.1  skrll }
   1645  1.1  skrll 
   1646  1.1  skrll /* Print a constant value to 'output_opcode':
   1647  1.1  skrll    ARG holds the operand's type and value.
   1648  1.1  skrll    SHIFT represents the location of the operand to be print into.
   1649  1.1  skrll    NBITS determines the size (in bits) of the constant.  */
   1650  1.1  skrll 
   1651  1.1  skrll static void
   1652  1.1  skrll print_constant (int nbits, int shift, argument *arg)
   1653  1.1  skrll {
   1654  1.1  skrll   unsigned long mask = 0;
   1655  1.1  skrll 
   1656  1.1  skrll   long constant = getconstant (arg->constant, nbits);
   1657  1.1  skrll 
   1658  1.1  skrll   switch (nbits)
   1659  1.1  skrll     {
   1660  1.1  skrll     case 32:
   1661  1.1  skrll     case 28:
   1662  1.1  skrll       /* mask the upper part of the constant, that is, the bits
   1663  1.1  skrll          going to the lowest byte of output_opcode[0].
   1664  1.1  skrll          The upper part of output_opcode[1] is always filled,
   1665  1.1  skrll          therefore it is always masked with 0xFFFF.  */
   1666  1.1  skrll       mask = (1 << (nbits - 16)) - 1;
   1667  1.1  skrll       /* Divide the constant between two consecutive words :
   1668  1.1  skrll          0        1         2         3
   1669  1.1  skrll          +---------+---------+---------+---------+
   1670  1.1  skrll          |         | X X X X | x X x X |         |
   1671  1.1  skrll          +---------+---------+---------+---------+
   1672  1.1  skrll          output_opcode[0]    output_opcode[1]     */
   1673  1.1  skrll 
   1674  1.1  skrll       CR16_PRINT (0, (constant >> WORD_SHIFT) & mask, 0);
   1675  1.1  skrll       CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
   1676  1.1  skrll       break;
   1677  1.1  skrll 
   1678  1.1  skrll     case 21:
   1679  1.1  skrll       if ((nbits == 21) && (IS_INSN_TYPE (LD_STOR_INS))) nbits = 20;
   1680  1.1  skrll     case 24:
   1681  1.1  skrll     case 22:
   1682  1.1  skrll     case 20:
   1683  1.1  skrll       /* mask the upper part of the constant, that is, the bits
   1684  1.1  skrll          going to the lowest byte of output_opcode[0].
   1685  1.1  skrll          The upper part of output_opcode[1] is always filled,
   1686  1.1  skrll          therefore it is always masked with 0xFFFF.  */
   1687  1.1  skrll       mask = (1 << (nbits - 16)) - 1;
   1688  1.1  skrll       /* Divide the constant between two consecutive words :
   1689  1.1  skrll          0        1         2          3
   1690  1.1  skrll          +---------+---------+---------+---------+
   1691  1.1  skrll          |         | X X X X | - X - X |         |
   1692  1.1  skrll          +---------+---------+---------+---------+
   1693  1.1  skrll          output_opcode[0]    output_opcode[1]     */
   1694  1.1  skrll 
   1695  1.1  skrll       if ((instruction->size > 2) && (shift == WORD_SHIFT))
   1696  1.1  skrll         {
   1697  1.1  skrll           if (arg->type == arg_idxrp)
   1698  1.1  skrll             {
   1699  1.1  skrll               CR16_PRINT (0, ((constant >> WORD_SHIFT) & mask) << 8, 0);
   1700  1.1  skrll               CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
   1701  1.1  skrll             }
   1702  1.1  skrll           else
   1703  1.1  skrll             {
   1704  1.1  skrll               CR16_PRINT (0, (((((constant >> WORD_SHIFT) & mask) << 8) & 0x0f00) | ((((constant >> WORD_SHIFT) & mask) >> 4) & 0xf)),0);
   1705  1.1  skrll               CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
   1706  1.1  skrll             }
   1707  1.1  skrll         }
   1708  1.1  skrll       else
   1709  1.1  skrll         CR16_PRINT (0, constant, shift);
   1710  1.1  skrll       break;
   1711  1.1  skrll 
   1712  1.1  skrll     case 14:
   1713  1.1  skrll       if (arg->type == arg_idxrp)
   1714  1.1  skrll         {
   1715  1.1  skrll           if (instruction->size == 2)
   1716  1.1  skrll             {
   1717  1.1  skrll               CR16_PRINT (0, ((constant)      & 0xf), shift);        /* 0-3 bits.  */
   1718  1.1  skrll               CR16_PRINT (0, ((constant >> 4) & 0x3), (shift + 20)); /* 4-5 bits.  */
   1719  1.1  skrll               CR16_PRINT (0, ((constant >> 6) & 0x3), (shift + 14)); /* 6-7 bits.  */
   1720  1.1  skrll               CR16_PRINT (0, ((constant >> 8) & 0x3f), (shift + 8)); /* 8-13 bits.  */
   1721  1.1  skrll             }
   1722  1.1  skrll           else
   1723  1.1  skrll             CR16_PRINT (0, constant, shift);
   1724  1.1  skrll         }
   1725  1.1  skrll       break;
   1726  1.1  skrll 
   1727  1.1  skrll     case 16:
   1728  1.1  skrll     case 12:
   1729  1.1  skrll       /* When instruction size is 3 and 'shift' is 16, a 16-bit constant is
   1730  1.1  skrll          always filling the upper part of output_opcode[1]. If we mistakenly
   1731  1.1  skrll          write it to output_opcode[0], the constant prefix (that is, 'match')
   1732  1.1  skrll          will be overriden.
   1733  1.1  skrll          0        1         2         3
   1734  1.1  skrll          +---------+---------+---------+---------+
   1735  1.1  skrll          | 'match' |         | X X X X |         |
   1736  1.1  skrll          +---------+---------+---------+---------+
   1737  1.1  skrll          output_opcode[0]    output_opcode[1]     */
   1738  1.1  skrll 
   1739  1.1  skrll       if ((instruction->size > 2) && (shift == WORD_SHIFT))
   1740  1.1  skrll         CR16_PRINT (1, constant, WORD_SHIFT);
   1741  1.1  skrll       else
   1742  1.1  skrll         CR16_PRINT (0, constant, shift);
   1743  1.1  skrll       break;
   1744  1.1  skrll 
   1745  1.1  skrll     case 8:
   1746  1.1  skrll       CR16_PRINT (0, ((constant / 2) & 0xf), shift);
   1747  1.1  skrll       CR16_PRINT (0, ((constant / 2) >> 4), (shift + 8));
   1748  1.1  skrll       break;
   1749  1.1  skrll 
   1750  1.1  skrll     default:
   1751  1.1  skrll       CR16_PRINT (0, constant,  shift);
   1752  1.1  skrll       break;
   1753  1.1  skrll     }
   1754  1.1  skrll }
   1755  1.1  skrll 
   1756  1.1  skrll /* Print an operand to 'output_opcode', which later on will be
   1757  1.1  skrll    printed to the object file:
   1758  1.1  skrll    ARG holds the operand's type, size and value.
   1759  1.1  skrll    SHIFT represents the printing location of operand.
   1760  1.1  skrll    NBITS determines the size (in bits) of a constant operand.  */
   1761  1.1  skrll 
   1762  1.1  skrll static void
   1763  1.1  skrll print_operand (int nbits, int shift, argument *arg)
   1764  1.1  skrll {
   1765  1.1  skrll   switch (arg->type)
   1766  1.1  skrll     {
   1767  1.1  skrll     case arg_cc:
   1768  1.1  skrll       CR16_PRINT (0, arg->cc, shift);
   1769  1.1  skrll       break;
   1770  1.1  skrll 
   1771  1.1  skrll     case arg_r:
   1772  1.1  skrll       CR16_PRINT (0, getreg_image (arg->r), shift);
   1773  1.1  skrll       break;
   1774  1.1  skrll 
   1775  1.1  skrll     case arg_rp:
   1776  1.1  skrll       CR16_PRINT (0, getregp_image (arg->rp), shift);
   1777  1.1  skrll       break;
   1778  1.1  skrll 
   1779  1.1  skrll     case arg_pr:
   1780  1.1  skrll       CR16_PRINT (0, getprocreg_image (arg->pr), shift);
   1781  1.1  skrll       break;
   1782  1.1  skrll 
   1783  1.1  skrll     case arg_prp:
   1784  1.1  skrll       CR16_PRINT (0, getprocregp_image (arg->prp), shift);
   1785  1.1  skrll       break;
   1786  1.1  skrll 
   1787  1.1  skrll     case arg_idxrp:
   1788  1.1  skrll       /*    16      12      8    6      0
   1789  1.1  skrll             +-----------------------------+
   1790  1.1  skrll             | r_index | disp  | rp_base   |
   1791  1.1  skrll             +-----------------------------+          */
   1792  1.1  skrll 
   1793  1.1  skrll       if (instruction->size == 3)
   1794  1.1  skrll         {
   1795  1.1  skrll           CR16_PRINT (0, getidxregp_image (arg->rp), 0);
   1796  1.1  skrll           if (getreg_image (arg->i_r) == 12)
   1797  1.1  skrll             CR16_PRINT (0, 0, 3);
   1798  1.1  skrll           else
   1799  1.1  skrll             CR16_PRINT (0, 1, 3);
   1800  1.1  skrll         }
   1801  1.1  skrll       else
   1802  1.1  skrll         {
   1803  1.1  skrll           CR16_PRINT (0, getidxregp_image (arg->rp), 16);
   1804  1.1  skrll           if (getreg_image (arg->i_r) == 12)
   1805  1.1  skrll             CR16_PRINT (0, 0, 19);
   1806  1.1  skrll           else
   1807  1.1  skrll             CR16_PRINT (0, 1, 19);
   1808  1.1  skrll         }
   1809  1.1  skrll       print_constant (nbits, shift, arg);
   1810  1.1  skrll       break;
   1811  1.1  skrll 
   1812  1.1  skrll     case arg_idxr:
   1813  1.1  skrll       if (getreg_image (arg->i_r) == 12)
   1814  1.1  skrll         if (IS_INSN_MNEMONIC ("cbitb") || IS_INSN_MNEMONIC ("sbitb")
   1815  1.1  skrll             || IS_INSN_MNEMONIC ("tbitb"))
   1816  1.1  skrll           CR16_PRINT (0, 0, 23);
   1817  1.1  skrll         else CR16_PRINT (0, 0, 24);
   1818  1.1  skrll       else
   1819  1.1  skrll         if (IS_INSN_MNEMONIC ("cbitb") || IS_INSN_MNEMONIC ("sbitb")
   1820  1.1  skrll             || IS_INSN_MNEMONIC ("tbitb"))
   1821  1.1  skrll           CR16_PRINT (0, 1, 23);
   1822  1.1  skrll         else CR16_PRINT (0, 1, 24);
   1823  1.1  skrll 
   1824  1.1  skrll       print_constant (nbits, shift, arg);
   1825  1.1  skrll       break;
   1826  1.1  skrll 
   1827  1.1  skrll     case arg_ic:
   1828  1.1  skrll     case arg_c:
   1829  1.1  skrll       print_constant (nbits, shift, arg);
   1830  1.1  skrll       break;
   1831  1.1  skrll 
   1832  1.1  skrll     case arg_rbase:
   1833  1.1  skrll       CR16_PRINT (0, getreg_image (arg->r), shift);
   1834  1.1  skrll       break;
   1835  1.1  skrll 
   1836  1.1  skrll     case arg_cr:
   1837  1.1  skrll       print_constant (nbits, shift , arg);
   1838  1.1  skrll       /* Add the register argument to the output_opcode.  */
   1839  1.1  skrll       CR16_PRINT (0, getreg_image (arg->r), (shift+16));
   1840  1.1  skrll       break;
   1841  1.1  skrll 
   1842  1.1  skrll     case arg_crp:
   1843  1.1  skrll       print_constant (nbits, shift , arg);
   1844  1.1  skrll       if (instruction->size > 1)
   1845  1.1  skrll         CR16_PRINT (0, getregp_image (arg->rp), (shift + 16));
   1846  1.1  skrll       else if (IS_INSN_TYPE (LD_STOR_INS) || (IS_INSN_TYPE (CSTBIT_INS)))
   1847  1.1  skrll         {
   1848  1.1  skrll           if (instruction->size == 2)
   1849  1.1  skrll             CR16_PRINT (0, getregp_image (arg->rp), (shift - 8));
   1850  1.1  skrll           else if (instruction->size == 1)
   1851  1.1  skrll             CR16_PRINT (0, getregp_image (arg->rp), 16);
   1852  1.1  skrll         }
   1853  1.1  skrll       else
   1854  1.1  skrll         CR16_PRINT (0, getregp_image (arg->rp), shift);
   1855  1.1  skrll       break;
   1856  1.1  skrll 
   1857  1.1  skrll     default:
   1858  1.1  skrll       break;
   1859  1.1  skrll     }
   1860  1.1  skrll }
   1861  1.1  skrll 
   1862  1.1  skrll /* Retrieve the number of operands for the current assembled instruction.  */
   1863  1.1  skrll 
   1864  1.1  skrll static int
   1865  1.1  skrll get_number_of_operands (void)
   1866  1.1  skrll {
   1867  1.1  skrll   int i;
   1868  1.1  skrll 
   1869  1.1  skrll   for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
   1870  1.1  skrll     ;
   1871  1.1  skrll   return i;
   1872  1.1  skrll }
   1873  1.1  skrll 
   1874  1.1  skrll /* Verify that the number NUM can be represented in BITS bits (that is,
   1875  1.1  skrll    within its permitted range), based on the instruction's FLAGS.
   1876  1.1  skrll    If UPDATE is nonzero, update the value of NUM if necessary.
   1877  1.1  skrll    Return OP_LEGAL upon success, actual error type upon failure.  */
   1878  1.1  skrll 
   1879  1.1  skrll static op_err
   1880  1.1  skrll check_range (long *num, int bits, int unsigned flags, int update)
   1881  1.1  skrll {
   1882  1.1  skrll   long min, max;
   1883  1.1  skrll   int retval = OP_LEGAL;
   1884  1.1  skrll   long value = *num;
   1885  1.1  skrll 
   1886  1.1  skrll   if (bits == 0 && value > 0) return OP_OUT_OF_RANGE;
   1887  1.1  skrll 
   1888  1.1  skrll   /* For hosts witah longs bigger than 32-bits make sure that the top
   1889  1.1  skrll      bits of a 32-bit negative value read in by the parser are set,
   1890  1.1  skrll      so that the correct comparisons are made.  */
   1891  1.1  skrll   if (value & 0x80000000)
   1892  1.1  skrll     value |= (-1L << 31);
   1893  1.1  skrll 
   1894  1.1  skrll 
   1895  1.1  skrll   /* Verify operand value is even.  */
   1896  1.1  skrll   if (flags & OP_EVEN)
   1897  1.1  skrll     {
   1898  1.1  skrll       if (value % 2)
   1899  1.1  skrll         return OP_NOT_EVEN;
   1900  1.1  skrll     }
   1901  1.1  skrll 
   1902  1.1  skrll   if (flags & OP_DEC)
   1903  1.1  skrll     {
   1904  1.1  skrll       value -= 1;
   1905  1.1  skrll       if (update)
   1906  1.1  skrll         *num = value;
   1907  1.1  skrll     }
   1908  1.1  skrll 
   1909  1.1  skrll   if (flags & OP_SHIFT)
   1910  1.1  skrll     {
   1911  1.1  skrll       value >>= 1;
   1912  1.1  skrll       if (update)
   1913  1.1  skrll         *num = value;
   1914  1.1  skrll     }
   1915  1.1  skrll   else if (flags & OP_SHIFT_DEC)
   1916  1.1  skrll     {
   1917  1.1  skrll       value = (value >> 1) - 1;
   1918  1.1  skrll       if (update)
   1919  1.1  skrll         *num = value;
   1920  1.1  skrll     }
   1921  1.1  skrll 
   1922  1.1  skrll   if (flags & OP_ABS20)
   1923  1.1  skrll     {
   1924  1.1  skrll       if (value > 0xEFFFF)
   1925  1.1  skrll         return OP_OUT_OF_RANGE;
   1926  1.1  skrll     }
   1927  1.1  skrll 
   1928  1.1  skrll   if (flags & OP_ESC)
   1929  1.1  skrll     {
   1930  1.1  skrll       if (value == 0xB || value == 0x9)
   1931  1.1  skrll         return OP_OUT_OF_RANGE;
   1932  1.1  skrll       else if (value == -1)
   1933  1.1  skrll         {
   1934  1.1  skrll           if (update)
   1935  1.1  skrll             *num = 9;
   1936  1.1  skrll           return retval;
   1937  1.1  skrll         }
   1938  1.1  skrll     }
   1939  1.1  skrll 
   1940  1.1  skrll   if (flags & OP_ESC1)
   1941  1.1  skrll     {
   1942  1.1  skrll       if (value > 13)
   1943  1.1  skrll         return OP_OUT_OF_RANGE;
   1944  1.1  skrll     }
   1945  1.1  skrll 
   1946  1.1  skrll    if (flags & OP_SIGNED)
   1947  1.1  skrll      {
   1948  1.1  skrll        max = (1 << (bits - 1)) - 1;
   1949  1.1  skrll        min = - (1 << (bits - 1));
   1950  1.1  skrll        if ((value > max) || (value < min))
   1951  1.1  skrll          retval = OP_OUT_OF_RANGE;
   1952  1.1  skrll      }
   1953  1.1  skrll    else if (flags & OP_UNSIGNED)
   1954  1.1  skrll      {
   1955  1.1  skrll        max = ((((1 << (bits - 1)) - 1) << 1) | 1);
   1956  1.1  skrll        min = 0;
   1957  1.1  skrll        if (((unsigned long) value > (unsigned long) max)
   1958  1.1  skrll             || ((unsigned long) value < (unsigned long) min))
   1959  1.1  skrll          retval = OP_OUT_OF_RANGE;
   1960  1.1  skrll      }
   1961  1.1  skrll    else if (flags & OP_NEG)
   1962  1.1  skrll      {
   1963  1.1  skrll        max = - 1;
   1964  1.1  skrll        min = - ((1 << (bits - 1)) - 1);
   1965  1.1  skrll        if ((value > max) || (value < min))
   1966  1.1  skrll          retval = OP_OUT_OF_RANGE;
   1967  1.1  skrll      }
   1968  1.1  skrll    return retval;
   1969  1.1  skrll }
   1970  1.1  skrll 
   1971  1.1  skrll /* Bunch of error checkings.
   1972  1.1  skrll    The checks are made after a matching instruction was found.  */
   1973  1.1  skrll 
   1974  1.1  skrll static void
   1975  1.1  skrll warn_if_needed (ins *insn)
   1976  1.1  skrll {
   1977  1.1  skrll   /* If the post-increment address mode is used and the load/store
   1978  1.1  skrll      source register is the same as rbase, the result of the
   1979  1.1  skrll      instruction is undefined.  */
   1980  1.1  skrll   if (IS_INSN_TYPE (LD_STOR_INS_INC))
   1981  1.1  skrll     {
   1982  1.1  skrll       /* Enough to verify that one of the arguments is a simple reg.  */
   1983  1.1  skrll       if ((insn->arg[0].type == arg_r) || (insn->arg[1].type == arg_r))
   1984  1.1  skrll         if (insn->arg[0].r == insn->arg[1].r)
   1985  1.1  skrll           as_bad (_("Same src/dest register is used (`r%d'), result is undefined"), insn->arg[0].r);
   1986  1.1  skrll     }
   1987  1.1  skrll 
   1988  1.1  skrll   if (IS_INSN_MNEMONIC ("pop")
   1989  1.1  skrll       || IS_INSN_MNEMONIC ("push")
   1990  1.1  skrll       || IS_INSN_MNEMONIC ("popret"))
   1991  1.1  skrll     {
   1992  1.1  skrll       unsigned int count = insn->arg[0].constant, reg_val;
   1993  1.1  skrll 
   1994  1.1  skrll       /* Check if count operand caused to save/retrive the RA twice
   1995  1.1  skrll          to generate warning message.  */
   1996  1.1  skrll      if (insn->nargs > 2)
   1997  1.1  skrll        {
   1998  1.1  skrll          reg_val = getreg_image (insn->arg[1].r);
   1999  1.1  skrll 
   2000  1.1  skrll          if (   ((reg_val == 9) &&  (count > 7))
   2001  1.1  skrll              || ((reg_val == 10) && (count > 6))
   2002  1.1  skrll              || ((reg_val == 11) && (count > 5))
   2003  1.1  skrll              || ((reg_val == 12) && (count > 4))
   2004  1.1  skrll              || ((reg_val == 13) && (count > 2))
   2005  1.1  skrll              || ((reg_val == 14) && (count > 0)))
   2006  1.1  skrll            as_warn (_("RA register is saved twice."));
   2007  1.1  skrll 
   2008  1.1  skrll          /* Check if the third operand is "RA" or "ra" */
   2009  1.1  skrll          if (!(((insn->arg[2].r) == ra) || ((insn->arg[2].r) == RA)))
   2010  1.1  skrll            as_bad (_("`%s' Illegal use of registers."), ins_parse);
   2011  1.1  skrll        }
   2012  1.1  skrll 
   2013  1.1  skrll       if (insn->nargs > 1)
   2014  1.1  skrll        {
   2015  1.1  skrll          reg_val = getreg_image (insn->arg[1].r);
   2016  1.1  skrll 
   2017  1.1  skrll          /* If register is a register pair ie r12/r13/r14 in operand1, then
   2018  1.1  skrll             the count constant should be validated.  */
   2019  1.1  skrll          if (((reg_val == 11) && (count > 7))
   2020  1.1  skrll              || ((reg_val == 12) && (count > 6))
   2021  1.1  skrll              || ((reg_val == 13) && (count > 4))
   2022  1.1  skrll              || ((reg_val == 14) && (count > 2))
   2023  1.1  skrll              || ((reg_val == 15) && (count > 0)))
   2024  1.1  skrll            as_bad (_("`%s' Illegal count-register combination."), ins_parse);
   2025  1.1  skrll        }
   2026  1.1  skrll      else
   2027  1.1  skrll        {
   2028  1.1  skrll          /* Check if the operand is "RA" or "ra" */
   2029  1.1  skrll          if (!(((insn->arg[0].r) == ra) || ((insn->arg[0].r) == RA)))
   2030  1.1  skrll            as_bad (_("`%s' Illegal use of register."), ins_parse);
   2031  1.1  skrll        }
   2032  1.1  skrll     }
   2033  1.1  skrll 
   2034  1.1  skrll   /* Some instruction assume the stack pointer as rptr operand.
   2035  1.1  skrll      Issue an error when the register to be loaded is also SP.  */
   2036  1.1  skrll   if (instruction->flags & NO_SP)
   2037  1.1  skrll     {
   2038  1.1  skrll       if (getreg_image (insn->arg[1].r) == getreg_image (sp))
   2039  1.1  skrll         as_bad (_("`%s' has undefined result"), ins_parse);
   2040  1.1  skrll     }
   2041  1.1  skrll 
   2042  1.1  skrll   /* If the rptr register is specified as one of the registers to be loaded,
   2043  1.1  skrll      the final contents of rptr are undefined. Thus, we issue an error.  */
   2044  1.1  skrll   if (instruction->flags & NO_RPTR)
   2045  1.1  skrll     {
   2046  1.1  skrll       if ((1 << getreg_image (insn->arg[0].r)) & insn->arg[1].constant)
   2047  1.1  skrll         as_bad (_("Same src/dest register is used (`r%d'),result is undefined"),
   2048  1.1  skrll                   getreg_image (insn->arg[0].r));
   2049  1.1  skrll     }
   2050  1.1  skrll }
   2051  1.1  skrll 
   2052  1.1  skrll /* In some cases, we need to adjust the instruction pointer although a
   2053  1.1  skrll    match was already found. Here, we gather all these cases.
   2054  1.1  skrll    Returns 1 if instruction pointer was adjusted, otherwise 0.  */
   2055  1.1  skrll 
   2056  1.1  skrll static int
   2057  1.1  skrll adjust_if_needed (ins *insn ATTRIBUTE_UNUSED)
   2058  1.1  skrll {
   2059  1.1  skrll   int ret_value = 0;
   2060  1.1  skrll 
   2061  1.1  skrll   if ((IS_INSN_TYPE (CSTBIT_INS)) || (IS_INSN_TYPE (LD_STOR_INS)))
   2062  1.1  skrll     {
   2063  1.1  skrll       if ((instruction->operands[0].op_type == abs24)
   2064  1.1  skrll            && ((insn->arg[0].constant) > 0xF00000))
   2065  1.1  skrll         {
   2066  1.1  skrll           insn->arg[0].constant &= 0xFFFFF;
   2067  1.1  skrll           instruction--;
   2068  1.1  skrll           ret_value = 1;
   2069  1.1  skrll         }
   2070  1.1  skrll     }
   2071  1.1  skrll 
   2072  1.1  skrll   return ret_value;
   2073  1.1  skrll }
   2074  1.1  skrll 
   2075  1.1  skrll /* Assemble a single instruction:
   2076  1.1  skrll    INSN is already parsed (that is, all operand values and types are set).
   2077  1.1  skrll    For instruction to be assembled, we need to find an appropriate template in
   2078  1.1  skrll    the instruction table, meeting the following conditions:
   2079  1.1  skrll     1: Has the same number of operands.
   2080  1.1  skrll     2: Has the same operand types.
   2081  1.1  skrll     3: Each operand size is sufficient to represent the instruction's values.
   2082  1.1  skrll    Returns 1 upon success, 0 upon failure.  */
   2083  1.1  skrll 
   2084  1.1  skrll static int
   2085  1.1  skrll assemble_insn (char *mnemonic, ins *insn)
   2086  1.1  skrll {
   2087  1.1  skrll   /* Type of each operand in the current template.  */
   2088  1.1  skrll   argtype cur_type[MAX_OPERANDS];
   2089  1.1  skrll   /* Size (in bits) of each operand in the current template.  */
   2090  1.1  skrll   unsigned int cur_size[MAX_OPERANDS];
   2091  1.1  skrll   /* Flags of each operand in the current template.  */
   2092  1.1  skrll   unsigned int cur_flags[MAX_OPERANDS];
   2093  1.1  skrll   /* Instruction type to match.  */
   2094  1.1  skrll   unsigned int ins_type;
   2095  1.1  skrll   /* Boolean flag to mark whether a match was found.  */
   2096  1.1  skrll   int match = 0;
   2097  1.1  skrll   int i;
   2098  1.1  skrll   /* Nonzero if an instruction with same number of operands was found.  */
   2099  1.1  skrll   int found_same_number_of_operands = 0;
   2100  1.1  skrll   /* Nonzero if an instruction with same argument types was found.  */
   2101  1.1  skrll   int found_same_argument_types = 0;
   2102  1.1  skrll   /* Nonzero if a constant was found within the required range.  */
   2103  1.1  skrll   int found_const_within_range  = 0;
   2104  1.1  skrll   /* Argument number of an operand with invalid type.  */
   2105  1.1  skrll   int invalid_optype = -1;
   2106  1.1  skrll   /* Argument number of an operand with invalid constant value.  */
   2107  1.1  skrll   int invalid_const  = -1;
   2108  1.1  skrll   /* Operand error (used for issuing various constant error messages).  */
   2109  1.1  skrll   op_err op_error, const_err = OP_LEGAL;
   2110  1.1  skrll 
   2111  1.1  skrll /* Retrieve data (based on FUNC) for each operand of a given instruction.  */
   2112  1.1  skrll #define GET_CURRENT_DATA(FUNC, ARRAY)                           \
   2113  1.1  skrll   for (i = 0; i < insn->nargs; i++)                             \
   2114  1.1  skrll     ARRAY[i] = FUNC (instruction->operands[i].op_type)
   2115  1.1  skrll 
   2116  1.1  skrll #define GET_CURRENT_TYPE    GET_CURRENT_DATA (get_optype, cur_type)
   2117  1.1  skrll #define GET_CURRENT_SIZE    GET_CURRENT_DATA (get_opbits, cur_size)
   2118  1.1  skrll #define GET_CURRENT_FLAGS   GET_CURRENT_DATA (get_opflags, cur_flags)
   2119  1.1  skrll 
   2120  1.1  skrll   /* Instruction has no operands -> only copy the constant opcode.   */
   2121  1.1  skrll   if (insn->nargs == 0)
   2122  1.1  skrll     {
   2123  1.1  skrll       output_opcode[0] = BIN (instruction->match, instruction->match_bits);
   2124  1.1  skrll       return 1;
   2125  1.1  skrll     }
   2126  1.1  skrll 
   2127  1.1  skrll   /* In some case, same mnemonic can appear with different instruction types.
   2128  1.1  skrll      For example, 'storb' is supported with 3 different types :
   2129  1.1  skrll      LD_STOR_INS, LD_STOR_INS_INC, STOR_IMM_INS.
   2130  1.1  skrll      We assume that when reaching this point, the instruction type was
   2131  1.1  skrll      pre-determined. We need to make sure that the type stays the same
   2132  1.1  skrll      during a search for matching instruction.  */
   2133  1.1  skrll   ins_type = CR16_INS_TYPE (instruction->flags);
   2134  1.1  skrll 
   2135  1.1  skrll   while (/* Check that match is still not found.  */
   2136  1.1  skrll          match != 1
   2137  1.1  skrll          /* Check we didn't get to end of table.  */
   2138  1.1  skrll          && instruction->mnemonic != NULL
   2139  1.1  skrll          /* Check that the actual mnemonic is still available.  */
   2140  1.1  skrll          && IS_INSN_MNEMONIC (mnemonic)
   2141  1.1  skrll          /* Check that the instruction type wasn't changed.  */
   2142  1.1  skrll          && IS_INSN_TYPE (ins_type))
   2143  1.1  skrll     {
   2144  1.1  skrll       /* Check whether number of arguments is legal.  */
   2145  1.1  skrll       if (get_number_of_operands () != insn->nargs)
   2146  1.1  skrll         goto next_insn;
   2147  1.1  skrll       found_same_number_of_operands = 1;
   2148  1.1  skrll 
   2149  1.1  skrll       /* Initialize arrays with data of each operand in current template.  */
   2150  1.1  skrll       GET_CURRENT_TYPE;
   2151  1.1  skrll       GET_CURRENT_SIZE;
   2152  1.1  skrll       GET_CURRENT_FLAGS;
   2153  1.1  skrll 
   2154  1.1  skrll       /* Check for type compatibility.  */
   2155  1.1  skrll       for (i = 0; i < insn->nargs; i++)
   2156  1.1  skrll         {
   2157  1.1  skrll           if (cur_type[i] != insn->arg[i].type)
   2158  1.1  skrll             {
   2159  1.1  skrll               if (invalid_optype == -1)
   2160  1.1  skrll                 invalid_optype = i + 1;
   2161  1.1  skrll               goto next_insn;
   2162  1.1  skrll             }
   2163  1.1  skrll         }
   2164  1.1  skrll       found_same_argument_types = 1;
   2165  1.1  skrll 
   2166  1.1  skrll       for (i = 0; i < insn->nargs; i++)
   2167  1.1  skrll         {
   2168  1.1  skrll           /* If 'bal' instruction size is '2' and reg operand is not 'ra'
   2169  1.1  skrll              then goto next instruction.  */
   2170  1.1  skrll           if (IS_INSN_MNEMONIC ("bal") && (i == 0)
   2171  1.1  skrll               && (instruction->size == 2) && (insn->arg[i].rp != 14))
   2172  1.1  skrll             goto next_insn;
   2173  1.1  skrll 
   2174  1.1  skrll           /* If 'storb' instruction with 'sp' reg and 16-bit disp of
   2175  1.1  skrll            * reg-pair, leads to undifined trap, so this should use
   2176  1.1  skrll            * 20-bit disp of reg-pair.  */
   2177  1.1  skrll           if (IS_INSN_MNEMONIC ("storb") && (instruction->size == 2)
   2178  1.1  skrll               && (insn->arg[i].r == 15) && (insn->arg[i + 1].type == arg_crp))
   2179  1.1  skrll             goto next_insn;
   2180  1.1  skrll 
   2181  1.1  skrll           /* Only check range - don't update the constant's value, since the
   2182  1.1  skrll              current instruction may not be the last we try to match.
   2183  1.1  skrll              The constant's value will be updated later, right before printing
   2184  1.1  skrll              it to the object file.  */
   2185  1.1  skrll           if ((insn->arg[i].X_op == O_constant)
   2186  1.1  skrll               && (op_error = check_range (&insn->arg[i].constant, cur_size[i],
   2187  1.1  skrll                                           cur_flags[i], 0)))
   2188  1.1  skrll             {
   2189  1.1  skrll               if (invalid_const == -1)
   2190  1.1  skrll                 {
   2191  1.1  skrll                   invalid_const = i + 1;
   2192  1.1  skrll                   const_err = op_error;
   2193  1.1  skrll                 }
   2194  1.1  skrll               goto next_insn;
   2195  1.1  skrll             }
   2196  1.1  skrll           /* For symbols, we make sure the relocation size (which was already
   2197  1.1  skrll              determined) is sufficient.  */
   2198  1.1  skrll           else if ((insn->arg[i].X_op == O_symbol)
   2199  1.1  skrll                    && ((bfd_reloc_type_lookup (stdoutput, insn->rtype))->bitsize
   2200  1.1  skrll                        > cur_size[i]))
   2201  1.1  skrll                   goto next_insn;
   2202  1.1  skrll         }
   2203  1.1  skrll       found_const_within_range = 1;
   2204  1.1  skrll 
   2205  1.1  skrll       /* If we got till here -> Full match is found.  */
   2206  1.1  skrll       match = 1;
   2207  1.1  skrll       break;
   2208  1.1  skrll 
   2209  1.1  skrll /* Try again with next instruction.  */
   2210  1.1  skrll next_insn:
   2211  1.1  skrll       instruction++;
   2212  1.1  skrll     }
   2213  1.1  skrll 
   2214  1.1  skrll   if (!match)
   2215  1.1  skrll     {
   2216  1.1  skrll       /* We haven't found a match - instruction can't be assembled.  */
   2217  1.1  skrll       if (!found_same_number_of_operands)
   2218  1.1  skrll         as_bad (_("Incorrect number of operands"));
   2219  1.1  skrll       else if (!found_same_argument_types)
   2220  1.1  skrll         as_bad (_("Illegal type of operand (arg %d)"), invalid_optype);
   2221  1.1  skrll       else if (!found_const_within_range)
   2222  1.1  skrll         {
   2223  1.1  skrll           switch (const_err)
   2224  1.1  skrll             {
   2225  1.1  skrll             case OP_OUT_OF_RANGE:
   2226  1.1  skrll               as_bad (_("Operand out of range (arg %d)"), invalid_const);
   2227  1.1  skrll               break;
   2228  1.1  skrll             case OP_NOT_EVEN:
   2229  1.1  skrll               as_bad (_("Operand has odd displacement (arg %d)"), invalid_const);
   2230  1.1  skrll               break;
   2231  1.1  skrll             default:
   2232  1.1  skrll               as_bad (_("Illegal operand (arg %d)"), invalid_const);
   2233  1.1  skrll               break;
   2234  1.1  skrll             }
   2235  1.1  skrll         }
   2236  1.1  skrll 
   2237  1.1  skrll        return 0;
   2238  1.1  skrll     }
   2239  1.1  skrll   else
   2240  1.1  skrll     /* Full match - print the encoding to output file.  */
   2241  1.1  skrll     {
   2242  1.1  skrll       /* Make further checkings (such that couldn't be made earlier).
   2243  1.1  skrll          Warn the user if necessary.  */
   2244  1.1  skrll       warn_if_needed (insn);
   2245  1.1  skrll 
   2246  1.1  skrll       /* Check whether we need to adjust the instruction pointer.  */
   2247  1.1  skrll       if (adjust_if_needed (insn))
   2248  1.1  skrll         /* If instruction pointer was adjusted, we need to update
   2249  1.1  skrll            the size of the current template operands.  */
   2250  1.1  skrll         GET_CURRENT_SIZE;
   2251  1.1  skrll 
   2252  1.1  skrll       for (i = 0; i < insn->nargs; i++)
   2253  1.1  skrll         {
   2254  1.1  skrll           int j = instruction->flags & REVERSE_MATCH ?
   2255  1.1  skrll                   i == 0 ? 1 :
   2256  1.1  skrll                   i == 1 ? 0 : i :
   2257  1.1  skrll                   i;
   2258  1.1  skrll 
   2259  1.1  skrll           /* This time, update constant value before printing it.  */
   2260  1.1  skrll             if ((insn->arg[j].X_op == O_constant)
   2261  1.1  skrll                && (check_range (&insn->arg[j].constant, cur_size[j],
   2262  1.1  skrll                                 cur_flags[j], 1) != OP_LEGAL))
   2263  1.1  skrll               as_fatal (_("Illegal operand (arg %d)"), j+1);
   2264  1.1  skrll         }
   2265  1.1  skrll 
   2266  1.1  skrll       /* First, copy the instruction's opcode.  */
   2267  1.1  skrll       output_opcode[0] = BIN (instruction->match, instruction->match_bits);
   2268  1.1  skrll 
   2269  1.1  skrll       for (i = 0; i < insn->nargs; i++)
   2270  1.1  skrll         {
   2271  1.1  skrll          /* For BAL (ra),disp17 instuction only. And also set the
   2272  1.1  skrll             DISP24a relocation type.  */
   2273  1.1  skrll          if (IS_INSN_MNEMONIC ("bal") && (instruction->size == 2) && i == 0)
   2274  1.1  skrll            {
   2275  1.1  skrll              insn->rtype = BFD_RELOC_CR16_DISP24a;
   2276  1.1  skrll              continue;
   2277  1.1  skrll            }
   2278  1.1  skrll           cur_arg_num = i;
   2279  1.1  skrll           print_operand (cur_size[i], instruction->operands[i].shift,
   2280  1.1  skrll                          &insn->arg[i]);
   2281  1.1  skrll         }
   2282  1.1  skrll     }
   2283  1.1  skrll 
   2284  1.1  skrll   return 1;
   2285  1.1  skrll }
   2286  1.1  skrll 
   2287  1.1  skrll /* Print the instruction.
   2288  1.1  skrll    Handle also cases where the instruction is relaxable/relocatable.  */
   2289  1.1  skrll 
   2290  1.1  skrll static void
   2291  1.1  skrll print_insn (ins *insn)
   2292  1.1  skrll {
   2293  1.1  skrll   unsigned int i, j, insn_size;
   2294  1.1  skrll   char *this_frag;
   2295  1.1  skrll   unsigned short words[4];
   2296  1.1  skrll   int addr_mod;
   2297  1.1  skrll 
   2298  1.1  skrll   /* Arrange the insn encodings in a WORD size array.  */
   2299  1.1  skrll   for (i = 0, j = 0; i < 2; i++)
   2300  1.1  skrll     {
   2301  1.1  skrll       words[j++] = (output_opcode[i] >> 16) & 0xFFFF;
   2302  1.1  skrll       words[j++] = output_opcode[i] & 0xFFFF;
   2303  1.1  skrll     }
   2304  1.1  skrll 
   2305  1.1  skrll     /* Handle relocation.  */
   2306  1.1  skrll     if ((instruction->flags & RELAXABLE) && relocatable)
   2307  1.1  skrll       {
   2308  1.1  skrll         int relax_subtype;
   2309  1.1  skrll         /* Write the maximal instruction size supported.  */
   2310  1.1  skrll         insn_size = INSN_MAX_SIZE;
   2311  1.1  skrll 
   2312  1.1  skrll         if (IS_INSN_TYPE (BRANCH_INS))
   2313  1.1  skrll           {
   2314  1.1  skrll             switch (insn->rtype)
   2315  1.1  skrll               {
   2316  1.1  skrll               case BFD_RELOC_CR16_DISP24:
   2317  1.1  skrll                 relax_subtype = 2;
   2318  1.1  skrll                 break;
   2319  1.1  skrll               case BFD_RELOC_CR16_DISP16:
   2320  1.1  skrll                 relax_subtype = 1;
   2321  1.1  skrll                 break;
   2322  1.1  skrll               default:
   2323  1.1  skrll                 relax_subtype = 0;
   2324  1.1  skrll                 break;
   2325  1.1  skrll               }
   2326  1.1  skrll           }
   2327  1.1  skrll         else
   2328  1.1  skrll           abort ();
   2329  1.1  skrll 
   2330  1.1  skrll         this_frag = frag_var (rs_machine_dependent, insn_size *2,
   2331  1.1  skrll                               4, relax_subtype,
   2332  1.1  skrll                               insn->exp.X_add_symbol,
   2333  1.1  skrll                               insn->exp.X_add_number,
   2334  1.1  skrll                               0);
   2335  1.1  skrll       }
   2336  1.1  skrll     else
   2337  1.1  skrll       {
   2338  1.1  skrll         insn_size = instruction->size;
   2339  1.1  skrll         this_frag = frag_more (insn_size * 2);
   2340  1.1  skrll 
   2341  1.1  skrll         if ((relocatable) && (insn->rtype != BFD_RELOC_NONE))
   2342  1.1  skrll           {
   2343  1.1  skrll              reloc_howto_type *reloc_howto;
   2344  1.1  skrll              int size;
   2345  1.1  skrll 
   2346  1.1  skrll              reloc_howto = bfd_reloc_type_lookup (stdoutput, insn->rtype);
   2347  1.1  skrll 
   2348  1.1  skrll              if (!reloc_howto)
   2349  1.1  skrll                abort ();
   2350  1.1  skrll 
   2351  1.1  skrll              size = bfd_get_reloc_size (reloc_howto);
   2352  1.1  skrll 
   2353  1.1  skrll              if (size < 1 || size > 4)
   2354  1.1  skrll                abort ();
   2355  1.1  skrll 
   2356  1.1  skrll              fix_new_exp (frag_now, this_frag - frag_now->fr_literal,
   2357  1.1  skrll                           size, &insn->exp, reloc_howto->pc_relative,
   2358  1.1  skrll                           insn->rtype);
   2359  1.1  skrll           }
   2360  1.1  skrll       }
   2361  1.1  skrll 
   2362  1.1  skrll   /* Verify a 2-byte code alignment.  */
   2363  1.1  skrll   addr_mod = frag_now_fix () & 1;
   2364  1.1  skrll   if (frag_now->has_code && frag_now->insn_addr != addr_mod)
   2365  1.1  skrll     as_bad (_("instruction address is not a multiple of 2"));
   2366  1.1  skrll   frag_now->insn_addr = addr_mod;
   2367  1.1  skrll   frag_now->has_code = 1;
   2368  1.1  skrll 
   2369  1.1  skrll   /* Write the instruction encoding to frag.  */
   2370  1.1  skrll   for (i = 0; i < insn_size; i++)
   2371  1.1  skrll     {
   2372  1.1  skrll       md_number_to_chars (this_frag, (valueT) words[i], 2);
   2373  1.1  skrll       this_frag += 2;
   2374  1.1  skrll     }
   2375  1.1  skrll }
   2376  1.1  skrll 
   2377  1.1  skrll /* This is the guts of the machine-dependent assembler.  OP points to a
   2378  1.1  skrll    machine dependent instruction.  This function is supposed to emit
   2379  1.1  skrll    the frags/bytes it assembles to.  */
   2380  1.1  skrll 
   2381  1.1  skrll void
   2382  1.1  skrll md_assemble (char *op)
   2383  1.1  skrll {
   2384  1.1  skrll   ins cr16_ins;
   2385  1.1  skrll   char *param, param1[32];
   2386  1.1  skrll   char c;
   2387  1.1  skrll 
   2388  1.1  skrll   /* Reset global variables for a new instruction.  */
   2389  1.1  skrll   reset_vars (op);
   2390  1.1  skrll 
   2391  1.1  skrll   /* Strip the mnemonic.  */
   2392  1.1  skrll   for (param = op; *param != 0 && !ISSPACE (*param); param++)
   2393  1.1  skrll     ;
   2394  1.1  skrll   c = *param;
   2395  1.1  skrll   *param++ = '\0';
   2396  1.1  skrll 
   2397  1.1  skrll   /* bCC instuctions and adjust the mnemonic by adding extra white spaces.  */
   2398  1.1  skrll   if (is_bcc_insn (op))
   2399  1.1  skrll     {
   2400  1.1  skrll       strcpy (param1, get_b_cc (op));
   2401  1.1  skrll       op = "b";
   2402  1.1  skrll       strcat (param1,",");
   2403  1.1  skrll       strcat (param1, param);
   2404  1.1  skrll       param = (char *) &param1;
   2405  1.1  skrll     }
   2406  1.1  skrll 
   2407  1.1  skrll   /* Checking the cinv options and adjust the mnemonic by removing the
   2408  1.1  skrll      extra white spaces.  */
   2409  1.1  skrll   if (streq ("cinv", op))
   2410  1.1  skrll     {
   2411  1.1  skrll      /* Validate the cinv options.  */
   2412  1.1  skrll       check_cinv_options (param);
   2413  1.1  skrll       strcat (op, param);
   2414  1.1  skrll     }
   2415  1.1  skrll 
   2416  1.1  skrll   /* MAPPING - SHIFT INSN, if imm4/imm16 positive values
   2417  1.1  skrll      lsh[b/w] imm4/imm6, reg ==> ashu[b/w] imm4/imm16, reg
   2418  1.1  skrll      as CR16 core doesn't support lsh[b/w] right shift operaions.  */
   2419  1.1  skrll   if ((streq ("lshb", op) || streq ("lshw", op) || streq ("lshd", op))
   2420  1.1  skrll       && (param [0] == '$'))
   2421  1.1  skrll     {
   2422  1.1  skrll       strcpy (param1, param);
   2423  1.1  skrll       /* Find the instruction.  */
   2424  1.1  skrll       instruction = (const inst *) hash_find (cr16_inst_hash, op);
   2425  1.1  skrll        parse_operands (&cr16_ins, param1);
   2426  1.1  skrll       if (((&cr16_ins)->arg[0].type == arg_ic)
   2427  1.1  skrll           && ((&cr16_ins)->arg[0].constant >= 0))
   2428  1.1  skrll         {
   2429  1.1  skrll            if (streq ("lshb", op))
   2430  1.1  skrll              op = "ashub";
   2431  1.1  skrll            else if (streq ("lshd", op))
   2432  1.1  skrll              op = "ashud";
   2433  1.1  skrll            else
   2434  1.1  skrll              op = "ashuw";
   2435  1.1  skrll         }
   2436  1.1  skrll     }
   2437  1.1  skrll 
   2438  1.1  skrll   /* Find the instruction.  */
   2439  1.1  skrll   instruction = (const inst *) hash_find (cr16_inst_hash, op);
   2440  1.1  skrll   if (instruction == NULL)
   2441  1.1  skrll     {
   2442  1.1  skrll       as_bad (_("Unknown opcode: `%s'"), op);
   2443  1.1  skrll       return;
   2444  1.1  skrll     }
   2445  1.1  skrll 
   2446  1.1  skrll   /* Tie dwarf2 debug info to the address at the start of the insn.  */
   2447  1.1  skrll   dwarf2_emit_insn (0);
   2448  1.1  skrll 
   2449  1.1  skrll   /* Parse the instruction's operands.  */
   2450  1.1  skrll   parse_insn (&cr16_ins, param);
   2451  1.1  skrll 
   2452  1.1  skrll   /* Assemble the instruction - return upon failure.  */
   2453  1.1  skrll   if (assemble_insn (op, &cr16_ins) == 0)
   2454  1.1  skrll     return;
   2455  1.1  skrll 
   2456  1.1  skrll   /* Print the instruction.  */
   2457  1.1  skrll   print_insn (&cr16_ins);
   2458  1.1  skrll }
   2459