Home | History | Annotate | Download | only in opcodes
      1 /* Assembler interface for targets using CGEN. -*- C -*-
      2    CGEN: Cpu tools GENerator
      3 
      4    THIS FILE IS MACHINE GENERATED WITH CGEN.
      5    - the resultant file is machine generated, cgen-asm.in isn't
      6 
      7    Copyright (C) 1996-2016 Free Software Foundation, Inc.
      8 
      9    This file is part of libopcodes.
     10 
     11    This library is free software; you can redistribute it and/or modify
     12    it under the terms of the GNU General Public License as published by
     13    the Free Software Foundation; either version 3, or (at your option)
     14    any later version.
     15 
     16    It is distributed in the hope that it will be useful, but WITHOUT
     17    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     18    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     19    License for more details.
     20 
     21    You should have received a copy of the GNU General Public License
     22    along with this program; if not, write to the Free Software Foundation, Inc.,
     23    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     24 
     25 
     26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
     27    Keep that in mind.  */
     28 
     29 #include "sysdep.h"
     30 #include <stdio.h>
     31 #include "ansidecl.h"
     32 #include "bfd.h"
     33 #include "symcat.h"
     34 #include "xstormy16-desc.h"
     35 #include "xstormy16-opc.h"
     36 #include "opintl.h"
     37 #include "xregex.h"
     38 #include "libiberty.h"
     39 #include "safe-ctype.h"
     40 
     41 #undef  min
     42 #define min(a,b) ((a) < (b) ? (a) : (b))
     43 #undef  max
     44 #define max(a,b) ((a) > (b) ? (a) : (b))
     45 
     46 static const char * parse_insn_normal
     47   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
     48 
     49 /* -- assembler routines inserted here.  */
     51 
     52 /* -- asm.c */
     53 
     54 /* The machine-independent code doesn't know how to disambiguate
     55      mov (foo),r3
     56    and
     57      mov (r2),r3
     58    where 'foo' is a label.  This helps it out. */
     59 
     60 static const char *
     61 parse_mem8 (CGEN_CPU_DESC cd,
     62 	    const char **strp,
     63 	    int opindex,
     64 	    unsigned long *valuep)
     65 {
     66   if (**strp == '(')
     67     {
     68       const char *s = *strp;
     69 
     70       if (s[1] == '-' && s[2] == '-')
     71 	return _("Bad register in preincrement");
     72 
     73       while (ISALNUM (*++s))
     74 	;
     75       if (s[0] == '+' && s[1] == '+' && (s[2] == ')' || s[2] == ','))
     76 	return _("Bad register in postincrement");
     77       if (s[0] == ',' || s[0] == ')')
     78 	return _("Bad register name");
     79     }
     80   else if (cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names,
     81 			       (long *) valuep) == NULL)
     82     return _("Label conflicts with register name");
     83   else if (strncasecmp (*strp, "rx,", 3) == 0
     84 	   || strncasecmp (*strp, "rxl,", 3) == 0
     85 	   || strncasecmp (*strp, "rxh,", 3) == 0)
     86     return _("Label conflicts with `Rx'");
     87   else if (**strp == '#')
     88     return _("Bad immediate expression");
     89 
     90   return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
     91 }
     92 
     93 /* For the add and subtract instructions, there are two immediate forms,
     94    one for small operands and one for large ones.  We want to use
     95    the small one when possible, but we do not want to generate relocs
     96    of the small size.  This is somewhat tricky.  */
     97 
     98 static const char *
     99 parse_small_immediate (CGEN_CPU_DESC cd,
    100 		       const char **strp,
    101 		       int opindex,
    102 		       unsigned long *valuep)
    103 {
    104   bfd_vma value;
    105   enum cgen_parse_operand_result result;
    106   const char *errmsg;
    107 
    108   if (**strp == '@')
    109     return _("No relocation for small immediate");
    110 
    111   errmsg = (* cd->parse_operand_fn)
    112     (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
    113      & result, & value);
    114 
    115   if (errmsg)
    116     return errmsg;
    117 
    118   if (result != CGEN_PARSE_OPERAND_RESULT_NUMBER)
    119     return _("Small operand was not an immediate number");
    120 
    121   *valuep = value;
    122   return NULL;
    123 }
    124 
    125 /* Literal scan be either a normal literal, a @hi() or @lo relocation.  */
    126 
    127 static const char *
    128 parse_immediate16 (CGEN_CPU_DESC cd,
    129 		   const char **strp,
    130 		   int opindex,
    131 		   unsigned long *valuep)
    132 {
    133   const char *errmsg;
    134   enum cgen_parse_operand_result result;
    135   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
    136   bfd_vma value;
    137 
    138   if (strncmp (*strp, "@hi(", 4) == 0)
    139     {
    140       *strp += 4;
    141       code = BFD_RELOC_HI16;
    142     }
    143   else
    144   if (strncmp (*strp, "@lo(", 4) == 0)
    145     {
    146       *strp += 4;
    147       code = BFD_RELOC_LO16;
    148     }
    149 
    150   if (code == BFD_RELOC_NONE)
    151     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
    152   else
    153     {
    154       errmsg = cgen_parse_address (cd, strp, opindex, code, &result, &value);
    155       if ((errmsg == NULL) &&
    156 	  (result != CGEN_PARSE_OPERAND_RESULT_QUEUED))
    157 	errmsg = _("Operand is not a symbol");
    158 
    159       *valuep = value;
    160       if ((code == BFD_RELOC_HI16 || code == BFD_RELOC_LO16)
    161 	  && **strp == ')')
    162 	*strp += 1;
    163       else
    164         {
    165 	  errmsg = _("Syntax error: No trailing ')'");
    166 	  return errmsg;
    167 	}
    168     }
    169   return errmsg;
    170 }
    171 /* -- */
    172 
    173 const char * xstormy16_cgen_parse_operand
    174   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    175 
    176 /* Main entry point for operand parsing.
    177 
    178    This function is basically just a big switch statement.  Earlier versions
    179    used tables to look up the function to use, but
    180    - if the table contains both assembler and disassembler functions then
    181      the disassembler contains much of the assembler and vice-versa,
    182    - there's a lot of inlining possibilities as things grow,
    183    - using a switch statement avoids the function call overhead.
    184 
    185    This function could be moved into `parse_insn_normal', but keeping it
    186    separate makes clear the interface between `parse_insn_normal' and each of
    187    the handlers.  */
    188 
    189 const char *
    190 xstormy16_cgen_parse_operand (CGEN_CPU_DESC cd,
    191 			   int opindex,
    192 			   const char ** strp,
    193 			   CGEN_FIELDS * fields)
    194 {
    195   const char * errmsg = NULL;
    196   /* Used by scalar operands that still need to be parsed.  */
    197   long junk ATTRIBUTE_UNUSED;
    198 
    199   switch (opindex)
    200     {
    201     case XSTORMY16_OPERAND_RB :
    202       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_Rb_names, & fields->f_Rb);
    203       break;
    204     case XSTORMY16_OPERAND_RBJ :
    205       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_Rb_names, & fields->f_Rbj);
    206       break;
    207     case XSTORMY16_OPERAND_RD :
    208       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rd);
    209       break;
    210     case XSTORMY16_OPERAND_RDM :
    211       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rdm);
    212       break;
    213     case XSTORMY16_OPERAND_RM :
    214       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rm);
    215       break;
    216     case XSTORMY16_OPERAND_RS :
    217       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rs);
    218       break;
    219     case XSTORMY16_OPERAND_ABS24 :
    220       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_ABS24, (unsigned long *) (& fields->f_abs24));
    221       break;
    222     case XSTORMY16_OPERAND_BCOND2 :
    223       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_branchcond, & fields->f_op2);
    224       break;
    225     case XSTORMY16_OPERAND_BCOND5 :
    226       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_branchcond, & fields->f_op5);
    227       break;
    228     case XSTORMY16_OPERAND_HMEM8 :
    229       errmsg = parse_mem8 (cd, strp, XSTORMY16_OPERAND_HMEM8, (unsigned long *) (& fields->f_hmem8));
    230       break;
    231     case XSTORMY16_OPERAND_IMM12 :
    232       errmsg = cgen_parse_signed_integer (cd, strp, XSTORMY16_OPERAND_IMM12, (long *) (& fields->f_imm12));
    233       break;
    234     case XSTORMY16_OPERAND_IMM16 :
    235       errmsg = parse_immediate16 (cd, strp, XSTORMY16_OPERAND_IMM16, (unsigned long *) (& fields->f_imm16));
    236       break;
    237     case XSTORMY16_OPERAND_IMM2 :
    238       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM2, (unsigned long *) (& fields->f_imm2));
    239       break;
    240     case XSTORMY16_OPERAND_IMM3 :
    241       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM3, (unsigned long *) (& fields->f_imm3));
    242       break;
    243     case XSTORMY16_OPERAND_IMM3B :
    244       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM3B, (unsigned long *) (& fields->f_imm3b));
    245       break;
    246     case XSTORMY16_OPERAND_IMM4 :
    247       errmsg = parse_small_immediate (cd, strp, XSTORMY16_OPERAND_IMM4, (unsigned long *) (& fields->f_imm4));
    248       break;
    249     case XSTORMY16_OPERAND_IMM8 :
    250       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM8, (unsigned long *) (& fields->f_imm8));
    251       break;
    252     case XSTORMY16_OPERAND_IMM8SMALL :
    253       errmsg = parse_small_immediate (cd, strp, XSTORMY16_OPERAND_IMM8SMALL, (unsigned long *) (& fields->f_imm8));
    254       break;
    255     case XSTORMY16_OPERAND_LMEM8 :
    256       errmsg = parse_mem8 (cd, strp, XSTORMY16_OPERAND_LMEM8, (unsigned long *) (& fields->f_lmem8));
    257       break;
    258     case XSTORMY16_OPERAND_REL12 :
    259       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL12, (unsigned long *) (& fields->f_rel12));
    260       break;
    261     case XSTORMY16_OPERAND_REL12A :
    262       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL12A, (unsigned long *) (& fields->f_rel12a));
    263       break;
    264     case XSTORMY16_OPERAND_REL8_2 :
    265       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL8_2, (unsigned long *) (& fields->f_rel8_2));
    266       break;
    267     case XSTORMY16_OPERAND_REL8_4 :
    268       errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL8_4, (unsigned long *) (& fields->f_rel8_4));
    269       break;
    270     case XSTORMY16_OPERAND_WS2 :
    271       errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_wordsize, & fields->f_op2m);
    272       break;
    273 
    274     default :
    275       /* xgettext:c-format */
    276       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
    277       abort ();
    278   }
    279 
    280   return errmsg;
    281 }
    282 
    283 cgen_parse_fn * const xstormy16_cgen_parse_handlers[] =
    284 {
    285   parse_insn_normal,
    286 };
    287 
    288 void
    289 xstormy16_cgen_init_asm (CGEN_CPU_DESC cd)
    290 {
    291   xstormy16_cgen_init_opcode_table (cd);
    292   xstormy16_cgen_init_ibld_table (cd);
    293   cd->parse_handlers = & xstormy16_cgen_parse_handlers[0];
    294   cd->parse_operand = xstormy16_cgen_parse_operand;
    295 #ifdef CGEN_ASM_INIT_HOOK
    296 CGEN_ASM_INIT_HOOK
    297 #endif
    298 }
    299 
    300 
    301 
    303 /* Regex construction routine.
    304 
    305    This translates an opcode syntax string into a regex string,
    306    by replacing any non-character syntax element (such as an
    307    opcode) with the pattern '.*'
    308 
    309    It then compiles the regex and stores it in the opcode, for
    310    later use by xstormy16_cgen_assemble_insn
    311 
    312    Returns NULL for success, an error message for failure.  */
    313 
    314 char *
    315 xstormy16_cgen_build_insn_regex (CGEN_INSN *insn)
    316 {
    317   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
    318   const char *mnem = CGEN_INSN_MNEMONIC (insn);
    319   char rxbuf[CGEN_MAX_RX_ELEMENTS];
    320   char *rx = rxbuf;
    321   const CGEN_SYNTAX_CHAR_TYPE *syn;
    322   int reg_err;
    323 
    324   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
    325 
    326   /* Mnemonics come first in the syntax string.  */
    327   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    328     return _("missing mnemonic in syntax string");
    329   ++syn;
    330 
    331   /* Generate a case sensitive regular expression that emulates case
    332      insensitive matching in the "C" locale.  We cannot generate a case
    333      insensitive regular expression because in Turkish locales, 'i' and 'I'
    334      are not equal modulo case conversion.  */
    335 
    336   /* Copy the literal mnemonic out of the insn.  */
    337   for (; *mnem; mnem++)
    338     {
    339       char c = *mnem;
    340 
    341       if (ISALPHA (c))
    342 	{
    343 	  *rx++ = '[';
    344 	  *rx++ = TOLOWER (c);
    345 	  *rx++ = TOUPPER (c);
    346 	  *rx++ = ']';
    347 	}
    348       else
    349 	*rx++ = c;
    350     }
    351 
    352   /* Copy any remaining literals from the syntax string into the rx.  */
    353   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
    354     {
    355       if (CGEN_SYNTAX_CHAR_P (* syn))
    356 	{
    357 	  char c = CGEN_SYNTAX_CHAR (* syn);
    358 
    359 	  switch (c)
    360 	    {
    361 	      /* Escape any regex metacharacters in the syntax.  */
    362 	    case '.': case '[': case '\\':
    363 	    case '*': case '^': case '$':
    364 
    365 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
    366 	    case '?': case '{': case '}':
    367 	    case '(': case ')': case '*':
    368 	    case '|': case '+': case ']':
    369 #endif
    370 	      *rx++ = '\\';
    371 	      *rx++ = c;
    372 	      break;
    373 
    374 	    default:
    375 	      if (ISALPHA (c))
    376 		{
    377 		  *rx++ = '[';
    378 		  *rx++ = TOLOWER (c);
    379 		  *rx++ = TOUPPER (c);
    380 		  *rx++ = ']';
    381 		}
    382 	      else
    383 		*rx++ = c;
    384 	      break;
    385 	    }
    386 	}
    387       else
    388 	{
    389 	  /* Replace non-syntax fields with globs.  */
    390 	  *rx++ = '.';
    391 	  *rx++ = '*';
    392 	}
    393     }
    394 
    395   /* Trailing whitespace ok.  */
    396   * rx++ = '[';
    397   * rx++ = ' ';
    398   * rx++ = '\t';
    399   * rx++ = ']';
    400   * rx++ = '*';
    401 
    402   /* But anchor it after that.  */
    403   * rx++ = '$';
    404   * rx = '\0';
    405 
    406   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
    407   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
    408 
    409   if (reg_err == 0)
    410     return NULL;
    411   else
    412     {
    413       static char msg[80];
    414 
    415       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
    416       regfree ((regex_t *) CGEN_INSN_RX (insn));
    417       free (CGEN_INSN_RX (insn));
    418       (CGEN_INSN_RX (insn)) = NULL;
    419       return msg;
    420     }
    421 }
    422 
    423 
    424 /* Default insn parser.
    426 
    427    The syntax string is scanned and operands are parsed and stored in FIELDS.
    428    Relocs are queued as we go via other callbacks.
    429 
    430    ??? Note that this is currently an all-or-nothing parser.  If we fail to
    431    parse the instruction, we return 0 and the caller will start over from
    432    the beginning.  Backtracking will be necessary in parsing subexpressions,
    433    but that can be handled there.  Not handling backtracking here may get
    434    expensive in the case of the m68k.  Deal with later.
    435 
    436    Returns NULL for success, an error message for failure.  */
    437 
    438 static const char *
    439 parse_insn_normal (CGEN_CPU_DESC cd,
    440 		   const CGEN_INSN *insn,
    441 		   const char **strp,
    442 		   CGEN_FIELDS *fields)
    443 {
    444   /* ??? Runtime added insns not handled yet.  */
    445   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
    446   const char *str = *strp;
    447   const char *errmsg;
    448   const char *p;
    449   const CGEN_SYNTAX_CHAR_TYPE * syn;
    450 #ifdef CGEN_MNEMONIC_OPERANDS
    451   /* FIXME: wip */
    452   int past_opcode_p;
    453 #endif
    454 
    455   /* For now we assume the mnemonic is first (there are no leading operands).
    456      We can parse it without needing to set up operand parsing.
    457      GAS's input scrubber will ensure mnemonics are lowercase, but we may
    458      not be called from GAS.  */
    459   p = CGEN_INSN_MNEMONIC (insn);
    460   while (*p && TOLOWER (*p) == TOLOWER (*str))
    461     ++p, ++str;
    462 
    463   if (* p)
    464     return _("unrecognized instruction");
    465 
    466 #ifndef CGEN_MNEMONIC_OPERANDS
    467   if (* str && ! ISSPACE (* str))
    468     return _("unrecognized instruction");
    469 #endif
    470 
    471   CGEN_INIT_PARSE (cd);
    472   cgen_init_parse_operand (cd);
    473 #ifdef CGEN_MNEMONIC_OPERANDS
    474   past_opcode_p = 0;
    475 #endif
    476 
    477   /* We don't check for (*str != '\0') here because we want to parse
    478      any trailing fake arguments in the syntax string.  */
    479   syn = CGEN_SYNTAX_STRING (syntax);
    480 
    481   /* Mnemonics come first for now, ensure valid string.  */
    482   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    483     abort ();
    484 
    485   ++syn;
    486 
    487   while (* syn != 0)
    488     {
    489       /* Non operand chars must match exactly.  */
    490       if (CGEN_SYNTAX_CHAR_P (* syn))
    491 	{
    492 	  /* FIXME: While we allow for non-GAS callers above, we assume the
    493 	     first char after the mnemonic part is a space.  */
    494 	  /* FIXME: We also take inappropriate advantage of the fact that
    495 	     GAS's input scrubber will remove extraneous blanks.  */
    496 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
    497 	    {
    498 #ifdef CGEN_MNEMONIC_OPERANDS
    499 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
    500 		past_opcode_p = 1;
    501 #endif
    502 	      ++ syn;
    503 	      ++ str;
    504 	    }
    505 	  else if (*str)
    506 	    {
    507 	      /* Syntax char didn't match.  Can't be this insn.  */
    508 	      static char msg [80];
    509 
    510 	      /* xgettext:c-format */
    511 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
    512 		       CGEN_SYNTAX_CHAR(*syn), *str);
    513 	      return msg;
    514 	    }
    515 	  else
    516 	    {
    517 	      /* Ran out of input.  */
    518 	      static char msg [80];
    519 
    520 	      /* xgettext:c-format */
    521 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
    522 		       CGEN_SYNTAX_CHAR(*syn));
    523 	      return msg;
    524 	    }
    525 	  continue;
    526 	}
    527 
    528 #ifdef CGEN_MNEMONIC_OPERANDS
    529       (void) past_opcode_p;
    530 #endif
    531       /* We have an operand of some sort.  */
    532       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
    533       if (errmsg)
    534 	return errmsg;
    535 
    536       /* Done with this operand, continue with next one.  */
    537       ++ syn;
    538     }
    539 
    540   /* If we're at the end of the syntax string, we're done.  */
    541   if (* syn == 0)
    542     {
    543       /* FIXME: For the moment we assume a valid `str' can only contain
    544 	 blanks now.  IE: We needn't try again with a longer version of
    545 	 the insn and it is assumed that longer versions of insns appear
    546 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
    547       while (ISSPACE (* str))
    548 	++ str;
    549 
    550       if (* str != '\0')
    551 	return _("junk at end of line"); /* FIXME: would like to include `str' */
    552 
    553       return NULL;
    554     }
    555 
    556   /* We couldn't parse it.  */
    557   return _("unrecognized instruction");
    558 }
    559 
    560 /* Main entry point.
    562    This routine is called for each instruction to be assembled.
    563    STR points to the insn to be assembled.
    564    We assume all necessary tables have been initialized.
    565    The assembled instruction, less any fixups, is stored in BUF.
    566    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
    567    still needs to be converted to target byte order, otherwise BUF is an array
    568    of bytes in target byte order.
    569    The result is a pointer to the insn's entry in the opcode table,
    570    or NULL if an error occured (an error message will have already been
    571    printed).
    572 
    573    Note that when processing (non-alias) macro-insns,
    574    this function recurses.
    575 
    576    ??? It's possible to make this cpu-independent.
    577    One would have to deal with a few minor things.
    578    At this point in time doing so would be more of a curiosity than useful
    579    [for example this file isn't _that_ big], but keeping the possibility in
    580    mind helps keep the design clean.  */
    581 
    582 const CGEN_INSN *
    583 xstormy16_cgen_assemble_insn (CGEN_CPU_DESC cd,
    584 			   const char *str,
    585 			   CGEN_FIELDS *fields,
    586 			   CGEN_INSN_BYTES_PTR buf,
    587 			   char **errmsg)
    588 {
    589   const char *start;
    590   CGEN_INSN_LIST *ilist;
    591   const char *parse_errmsg = NULL;
    592   const char *insert_errmsg = NULL;
    593   int recognized_mnemonic = 0;
    594 
    595   /* Skip leading white space.  */
    596   while (ISSPACE (* str))
    597     ++ str;
    598 
    599   /* The instructions are stored in hashed lists.
    600      Get the first in the list.  */
    601   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
    602 
    603   /* Keep looking until we find a match.  */
    604   start = str;
    605   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
    606     {
    607       const CGEN_INSN *insn = ilist->insn;
    608       recognized_mnemonic = 1;
    609 
    610 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
    611       /* Not usually needed as unsupported opcodes
    612 	 shouldn't be in the hash lists.  */
    613       /* Is this insn supported by the selected cpu?  */
    614       if (! xstormy16_cgen_insn_supported (cd, insn))
    615 	continue;
    616 #endif
    617       /* If the RELAXED attribute is set, this is an insn that shouldn't be
    618 	 chosen immediately.  Instead, it is used during assembler/linker
    619 	 relaxation if possible.  */
    620       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
    621 	continue;
    622 
    623       str = start;
    624 
    625       /* Skip this insn if str doesn't look right lexically.  */
    626       if (CGEN_INSN_RX (insn) != NULL &&
    627 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
    628 	continue;
    629 
    630       /* Allow parse/insert handlers to obtain length of insn.  */
    631       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
    632 
    633       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
    634       if (parse_errmsg != NULL)
    635 	continue;
    636 
    637       /* ??? 0 is passed for `pc'.  */
    638       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
    639 						 (bfd_vma) 0);
    640       if (insert_errmsg != NULL)
    641         continue;
    642 
    643       /* It is up to the caller to actually output the insn and any
    644          queued relocs.  */
    645       return insn;
    646     }
    647 
    648   {
    649     static char errbuf[150];
    650     const char *tmp_errmsg;
    651 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
    652 #define be_verbose 1
    653 #else
    654 #define be_verbose 0
    655 #endif
    656 
    657     if (be_verbose)
    658       {
    659 	/* If requesting verbose error messages, use insert_errmsg.
    660 	   Failing that, use parse_errmsg.  */
    661 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
    662 		      parse_errmsg ? parse_errmsg :
    663 		      recognized_mnemonic ?
    664 		      _("unrecognized form of instruction") :
    665 		      _("unrecognized instruction"));
    666 
    667 	if (strlen (start) > 50)
    668 	  /* xgettext:c-format */
    669 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
    670 	else
    671 	  /* xgettext:c-format */
    672 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
    673       }
    674     else
    675       {
    676 	if (strlen (start) > 50)
    677 	  /* xgettext:c-format */
    678 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
    679 	else
    680 	  /* xgettext:c-format */
    681 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
    682       }
    683 
    684     *errmsg = errbuf;
    685     return NULL;
    686   }
    687 }
    688