Home | History | Annotate | Line # | Download | only in opcodes
      1 /* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */
      2 /* Assembler interface for targets using CGEN. -*- C -*-
      3    CGEN: Cpu tools GENerator
      4 
      5    THIS FILE IS MACHINE GENERATED WITH CGEN.
      6    - the resultant file is machine generated, cgen-asm.in isn't
      7 
      8    Copyright (C) 1996-2025 Free Software Foundation, Inc.
      9 
     10    This file is part of libopcodes.
     11 
     12    This library is free software; you can redistribute it and/or modify
     13    it under the terms of the GNU General Public License as published by
     14    the Free Software Foundation; either version 3, or (at your option)
     15    any later version.
     16 
     17    It is distributed in the hope that it will be useful, but WITHOUT
     18    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     19    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     20    License for more details.
     21 
     22    You should have received a copy of the GNU General Public License
     23    along with this program; if not, write to the Free Software Foundation, Inc.,
     24    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     25 
     26 
     27 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
     28    Keep that in mind.  */
     29 
     30 #include "sysdep.h"
     31 #include <stdio.h>
     32 #include "ansidecl.h"
     33 #include "bfd.h"
     34 #include "symcat.h"
     35 #include "mep-desc.h"
     36 #include "mep-opc.h"
     37 #include "opintl.h"
     38 #include "xregex.h"
     39 #include "libiberty.h"
     40 #include "safe-ctype.h"
     41 
     42 #undef  min
     43 #define min(a,b) ((a) < (b) ? (a) : (b))
     44 #undef  max
     45 #define max(a,b) ((a) > (b) ? (a) : (b))
     46 
     47 static const char * parse_insn_normal
     48   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
     49 
     50 /* -- assembler routines inserted here.  */
     52 
     53 /* -- asm.c */
     54 
     55 #include "elf/mep.h"
     56 
     57 #define CGEN_VALIDATE_INSN_SUPPORTED
     58 #define mep_cgen_insn_supported mep_cgen_insn_supported_asm
     59 
     60        const char * parse_csrn       (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
     61        const char * parse_tpreg      (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
     62        const char * parse_spreg      (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
     63        const char * parse_mep_align  (CGEN_CPU_DESC, const char **, enum cgen_operand_type, long *);
     64        const char * parse_mep_alignu (CGEN_CPU_DESC, const char **, enum cgen_operand_type, unsigned long *);
     65 static const char * parse_signed16   (CGEN_CPU_DESC, const char **, int, long *);
     66 static const char * parse_signed16_range   (CGEN_CPU_DESC, const char **, int, long *) ATTRIBUTE_UNUSED;
     67 static const char * parse_unsigned16 (CGEN_CPU_DESC, const char **, int, unsigned long *);
     68 static const char * parse_unsigned16_range (CGEN_CPU_DESC, const char **, int, unsigned long *) ATTRIBUTE_UNUSED;
     69 static const char * parse_lo16       (CGEN_CPU_DESC, const char **, int, long *, long);
     70 static const char * parse_unsigned7  (CGEN_CPU_DESC, const char **, enum cgen_operand_type, unsigned long *);
     71 static const char * parse_zero       (CGEN_CPU_DESC, const char **, int, long *);
     72 
     73 const char *
     74 parse_csrn (CGEN_CPU_DESC cd, const char **strp,
     75 	    CGEN_KEYWORD *keyword_table, long *field)
     76 {
     77   const char *err;
     78   unsigned long value;
     79 
     80   err = cgen_parse_keyword (cd, strp, keyword_table, field);
     81   if (!err)
     82     return NULL;
     83 
     84   err = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CSRN_IDX, & value);
     85   if (err)
     86     return err;
     87   *field = value;
     88   return NULL;
     89 }
     90 
     91 /* begin-cop-ip-parse-handlers */
     92 static const char *
     93 parse_ivc2_cr (CGEN_CPU_DESC,
     94 	const char **,
     95 	CGEN_KEYWORD *,
     96 	long *) ATTRIBUTE_UNUSED;
     97 static const char *
     98 parse_ivc2_cr (CGEN_CPU_DESC cd,
     99 	const char **strp,
    100 	CGEN_KEYWORD *keyword_table  ATTRIBUTE_UNUSED,
    101 	long *field)
    102 {
    103   return cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr_ivc2, field);
    104 }
    105 static const char *
    106 parse_ivc2_ccr (CGEN_CPU_DESC,
    107 	const char **,
    108 	CGEN_KEYWORD *,
    109 	long *) ATTRIBUTE_UNUSED;
    110 static const char *
    111 parse_ivc2_ccr (CGEN_CPU_DESC cd,
    112 	const char **strp,
    113 	CGEN_KEYWORD *keyword_table  ATTRIBUTE_UNUSED,
    114 	long *field)
    115 {
    116   return cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, field);
    117 }
    118 /* end-cop-ip-parse-handlers */
    119 
    120 const char *
    121 parse_tpreg (CGEN_CPU_DESC cd, const char ** strp,
    122 	     CGEN_KEYWORD *keyword_table, long *field)
    123 {
    124   const char *err;
    125 
    126   err = cgen_parse_keyword (cd, strp, keyword_table, field);
    127   if (err)
    128     return err;
    129   if (*field != 13)
    130     return _("Only $tp or $13 allowed for this opcode");
    131   return NULL;
    132 }
    133 
    134 const char *
    135 parse_spreg (CGEN_CPU_DESC cd, const char ** strp,
    136 	     CGEN_KEYWORD *keyword_table, long *field)
    137 {
    138   const char *err;
    139 
    140   err = cgen_parse_keyword (cd, strp, keyword_table, field);
    141   if (err)
    142     return err;
    143   if (*field != 15)
    144     return _("Only $sp or $15 allowed for this opcode");
    145   return NULL;
    146 }
    147 
    148 const char *
    149 parse_mep_align (CGEN_CPU_DESC cd, const char ** strp,
    150 		 enum cgen_operand_type type, long *field)
    151 {
    152   long lsbs = 0;
    153   const char *err;
    154 
    155   switch (type)
    156     {
    157     case MEP_OPERAND_PCREL8A2:
    158     case MEP_OPERAND_PCREL12A2:
    159     case MEP_OPERAND_PCREL17A2:
    160     case MEP_OPERAND_PCREL24A2:
    161       err = cgen_parse_signed_integer   (cd, strp, type, field);
    162       break;
    163     case MEP_OPERAND_PCABS24A2:
    164     case MEP_OPERAND_UDISP7:
    165     case MEP_OPERAND_UDISP7A2:
    166     case MEP_OPERAND_UDISP7A4:
    167     case MEP_OPERAND_UIMM7A4:
    168     case MEP_OPERAND_ADDR24A4:
    169       err = cgen_parse_unsigned_integer (cd, strp, type, (unsigned long *) field);
    170       break;
    171     default:
    172       abort();
    173     }
    174   if (err)
    175     return err;
    176   switch (type)
    177     {
    178     case MEP_OPERAND_UDISP7:
    179       lsbs = 0;
    180       break;
    181     case MEP_OPERAND_PCREL8A2:
    182     case MEP_OPERAND_PCREL12A2:
    183     case MEP_OPERAND_PCREL17A2:
    184     case MEP_OPERAND_PCREL24A2:
    185     case MEP_OPERAND_PCABS24A2:
    186     case MEP_OPERAND_UDISP7A2:
    187       lsbs = *field & 1;
    188       break;
    189     case MEP_OPERAND_UDISP7A4:
    190     case MEP_OPERAND_UIMM7A4:
    191     case MEP_OPERAND_ADDR24A4:
    192       lsbs = *field & 3;
    193       break;
    194       lsbs = *field & 7;
    195       break;
    196     default:
    197       /* Safe assumption?  */
    198       abort ();
    199     }
    200   if (lsbs)
    201     return "Value is not aligned enough";
    202   return NULL;
    203 }
    204 
    205 const char *
    206 parse_mep_alignu (CGEN_CPU_DESC cd, const char ** strp,
    207 		 enum cgen_operand_type type, unsigned long *field)
    208 {
    209   return parse_mep_align (cd, strp, type, (long *) field);
    210 }
    211 
    212 
    213 /* Handle %lo(), %tpoff(), %sdaoff(), %hi(), and other signed
    214    constants in a signed context.  */
    215 
    216 static const char *
    217 parse_signed16 (CGEN_CPU_DESC cd,
    218 		const char **strp,
    219 		int opindex,
    220 		long *valuep)
    221 {
    222   return parse_lo16 (cd, strp, opindex, valuep, 1);
    223 }
    224 
    225 static const char *
    226 parse_lo16 (CGEN_CPU_DESC cd,
    227 	    const char **strp,
    228 	    int opindex,
    229 	    long *valuep,
    230 	    long signedp)
    231 {
    232   const char *errmsg;
    233   enum cgen_parse_operand_result result_type;
    234   bfd_vma value;
    235 
    236   if (strncasecmp (*strp, "%lo(", 4) == 0)
    237     {
    238       *strp += 4;
    239       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_LOW16,
    240 				   & result_type, & value);
    241       if (**strp != ')')
    242 	return _("missing `)'");
    243       ++*strp;
    244       if (errmsg == NULL
    245 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    246 	value &= 0xffff;
    247       if (signedp)
    248 	*valuep = (long)(short) value;
    249       else
    250 	*valuep = value;
    251       return errmsg;
    252     }
    253 
    254   if (strncasecmp (*strp, "%hi(", 4) == 0)
    255     {
    256       *strp += 4;
    257       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16S,
    258 				   & result_type, & value);
    259       if (**strp != ')')
    260 	return _("missing `)'");
    261       ++*strp;
    262       if (errmsg == NULL
    263 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    264 	value = (value + 0x8000) >> 16;
    265       *valuep = value;
    266       return errmsg;
    267     }
    268 
    269   if (strncasecmp (*strp, "%uhi(", 5) == 0)
    270     {
    271       *strp += 5;
    272       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16U,
    273 				   & result_type, & value);
    274       if (**strp != ')')
    275 	return _("missing `)'");
    276       ++*strp;
    277       if (errmsg == NULL
    278 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    279 	value = value >> 16;
    280       *valuep = value;
    281       return errmsg;
    282     }
    283 
    284   if (strncasecmp (*strp, "%sdaoff(", 8) == 0)
    285     {
    286       *strp += 8;
    287       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_GPREL,
    288 				   NULL, & value);
    289       if (**strp != ')')
    290 	return _("missing `)'");
    291       ++*strp;
    292       *valuep = value;
    293       return errmsg;
    294     }
    295 
    296   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
    297     {
    298       *strp += 7;
    299       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_TPREL,
    300 				   NULL, & value);
    301       if (**strp != ')')
    302 	return _("missing `)'");
    303       ++*strp;
    304       *valuep = value;
    305       return errmsg;
    306     }
    307 
    308   if (**strp == '%')
    309     /* xgettext:no-c-format */
    310     return _("invalid %function() here");
    311 
    312   return cgen_parse_signed_integer (cd, strp, opindex, valuep);
    313 }
    314 
    315 static const char *
    316 parse_unsigned16 (CGEN_CPU_DESC cd,
    317 		  const char **strp,
    318 		  int opindex,
    319 		  unsigned long *valuep)
    320 {
    321   return parse_lo16 (cd, strp, opindex, (long *) valuep, 0);
    322 }
    323 
    324 static const char *
    325 parse_signed16_range (CGEN_CPU_DESC cd,
    326 		      const char **strp,
    327 		      int opindex,
    328 		      signed long *valuep)
    329 {
    330   const char *errmsg = 0;
    331   signed long value;
    332 
    333   errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
    334   if (errmsg)
    335     return errmsg;
    336 
    337   if (value < -32768 || value > 32767)
    338     return _("Immediate is out of range -32768 to 32767");
    339 
    340   *valuep = value;
    341   return 0;
    342 }
    343 
    344 static const char *
    345 parse_unsigned16_range (CGEN_CPU_DESC cd,
    346 			const char **strp,
    347 			int opindex,
    348 			unsigned long *valuep)
    349 {
    350   const char *errmsg = 0;
    351   unsigned long value;
    352 
    353   errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, & value);
    354   if (errmsg)
    355     return errmsg;
    356 
    357   if (value > 65535)
    358     return _("Immediate is out of range 0 to 65535");
    359 
    360   *valuep = value;
    361   return 0;
    362 }
    363 
    364 /* A special case of parse_signed16 which accepts only the value zero.  */
    365 
    366 static const char *
    367 parse_zero (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
    368 {
    369   const char *errmsg;
    370   enum cgen_parse_operand_result result_type;
    371   bfd_vma value;
    372 
    373   /*fprintf(stderr, "dj: signed parse opindex `%s'\n", *strp);*/
    374 
    375   /* Prevent ($ry) from being attempted as an expression on 'sw $rx,($ry)'.
    376      It will fail and cause ry to be listed as an undefined symbol in the
    377      listing.  */
    378   if (strncmp (*strp, "($", 2) == 0)
    379     return "not zero"; /* any string will do -- will never be seen.  */
    380 
    381   if (strncasecmp (*strp, "%lo(", 4) == 0)
    382     {
    383       *strp += 4;
    384       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_LOW16,
    385 				   &result_type, &value);
    386       if (**strp != ')')
    387 	return "missing `)'";
    388       ++*strp;
    389       if (errmsg == NULL
    390 	  && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    391 	return "not zero"; /* any string will do -- will never be seen.  */
    392       *valuep = value;
    393       return errmsg;
    394     }
    395 
    396   if (strncasecmp (*strp, "%hi(", 4) == 0)
    397     {
    398       *strp += 4;
    399       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16S,
    400 				   &result_type, &value);
    401       if (**strp != ')')
    402 	return "missing `)'";
    403       ++*strp;
    404       if (errmsg == NULL
    405 	  && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    406 	return "not zero"; /* any string will do -- will never be seen.  */
    407       *valuep = value;
    408       return errmsg;
    409     }
    410 
    411   if (strncasecmp (*strp, "%uhi(", 5) == 0)
    412     {
    413       *strp += 5;
    414       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16U,
    415 				   &result_type, &value);
    416       if (**strp != ')')
    417 	return "missing `)'";
    418       ++*strp;
    419       if (errmsg == NULL
    420 	  && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    421 	return "not zero"; /* any string will do -- will never be seen.  */
    422       *valuep = value;
    423       return errmsg;
    424     }
    425 
    426   if (strncasecmp (*strp, "%sdaoff(", 8) == 0)
    427     {
    428       *strp += 8;
    429       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_GPREL,
    430 				   &result_type, &value);
    431       if (**strp != ')')
    432 	return "missing `)'";
    433       ++*strp;
    434       if (errmsg == NULL
    435 	  && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    436 	return "not zero"; /* any string will do -- will never be seen.  */
    437       *valuep = value;
    438       return errmsg;
    439     }
    440 
    441   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
    442     {
    443       *strp += 7;
    444       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_TPREL,
    445 				   &result_type, &value);
    446       if (**strp != ')')
    447 	return "missing `)'";
    448       ++*strp;
    449       if (errmsg == NULL
    450 	  && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    451 	return "not zero"; /* any string will do -- will never be seen.  */
    452       *valuep = value;
    453       return errmsg;
    454     }
    455 
    456   if (**strp == '%')
    457     return "invalid %function() here";
    458 
    459   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_NONE,
    460 			       &result_type, &value);
    461   if (errmsg == NULL
    462       && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
    463     return "not zero"; /* any string will do -- will never be seen.  */
    464 
    465   return errmsg;
    466 }
    467 
    468 static const char *
    469 parse_unsigned7 (CGEN_CPU_DESC cd, const char **strp,
    470 		 enum cgen_operand_type opindex, unsigned long *valuep)
    471 {
    472   const char *errmsg;
    473   bfd_vma value;
    474 
    475   /* fprintf(stderr, "dj: unsigned7 parse `%s'\n", *strp); */
    476 
    477   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
    478     {
    479       int reloc;
    480       *strp += 7;
    481       switch (opindex)
    482 	{
    483 	case MEP_OPERAND_UDISP7:
    484 	  reloc = BFD_RELOC_MEP_TPREL7;
    485 	  break;
    486 	case MEP_OPERAND_UDISP7A2:
    487 	  reloc = BFD_RELOC_MEP_TPREL7A2;
    488 	  break;
    489 	case MEP_OPERAND_UDISP7A4:
    490 	  reloc = BFD_RELOC_MEP_TPREL7A4;
    491 	  break;
    492 	default:
    493 	  /* Safe assumption?  */
    494 	  abort ();
    495 	}
    496       errmsg = cgen_parse_address (cd, strp, opindex, reloc,
    497 				   NULL, &value);
    498       if (**strp != ')')
    499 	return "missing `)'";
    500       ++*strp;
    501       *valuep = value;
    502       return errmsg;
    503     }
    504 
    505   if (**strp == '%')
    506     /* xgettext:no-c-format */
    507     return _("invalid %function() here");
    508 
    509   return parse_mep_alignu (cd, strp, opindex, valuep);
    510 }
    511 
    512 static ATTRIBUTE_UNUSED const char *
    513 parse_cdisp10 (CGEN_CPU_DESC cd,
    514 	       const char **strp,
    515 	       int opindex,
    516 	       long *valuep)
    517 {
    518   const char *errmsg = 0;
    519   signed long value;
    520   long have_zero = 0;
    521   int wide = 0;
    522   int alignment;
    523 
    524   switch (opindex)
    525     {
    526     case MEP_OPERAND_CDISP10A4:
    527       alignment = 2;
    528       break;
    529     case MEP_OPERAND_CDISP10A2:
    530       alignment = 1;
    531       break;
    532     case MEP_OPERAND_CDISP10:
    533     default:
    534       alignment = 0;
    535       break;
    536     }
    537 
    538   if ((MEP_CPU & EF_MEP_CPU_MASK) == EF_MEP_CPU_C5)
    539     wide = 1;
    540 
    541   if (strncmp (*strp, "0x0", 3) == 0
    542       || (**strp == '0' && *(*strp + 1) != 'x'))
    543     have_zero = 1;
    544 
    545   errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
    546   if (errmsg)
    547     return errmsg;
    548 
    549   if (wide)
    550     {
    551       if (value < -512 || value > 511)
    552 	return _("Immediate is out of range -512 to 511");
    553     }
    554   else
    555     {
    556       if (value < -128 || value > 127)
    557 	return _("Immediate is out of range -128 to 127");
    558     }
    559 
    560   if (value & ((1<<alignment)-1))
    561     return _("Value is not aligned enough");
    562 
    563   /* If this field may require a relocation then use larger dsp16.  */
    564   if (! have_zero && value == 0)
    565     return (wide ? _("Immediate is out of range -512 to 511")
    566 	    : _("Immediate is out of range -128 to 127"));
    567 
    568   *valuep = value;
    569   return 0;
    570 }
    571 
    572 /* BEGIN LIGHTWEIGHT MACRO PROCESSOR.  */
    573 
    574 #define MAXARGS 9
    575 
    576 typedef struct
    577 {
    578   char *name;
    579   char *expansion;
    580 }  macro;
    581 
    582 typedef struct
    583 {
    584   const char *start;
    585   int len;
    586 } arg;
    587 
    588 static macro const macros[] =
    589 {
    590   { "sizeof", "(`1.end + (- `1))"},
    591   { "startof", "(`1 | 0)" },
    592   { "align4", "(`1&(~3))"},
    593 /*{ "hi", "(((`1+0x8000)>>16) & 0xffff)" },  */
    594 /*{ "lo", "(`1 & 0xffff)" },  */
    595 /*{ "sdaoff", "((`1-__sdabase) & 0x7f)"},  */
    596 /*{ "tpoff", "((`1-__tpbase) & 0x7f)"},  */
    597   { 0,0 }
    598 };
    599 
    600 static char  * expand_string    (const char *, int);
    601 
    602 static const char *
    603 mep_cgen_expand_macros_and_parse_operand
    604   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    605 
    606 static char *
    607 str_append (char *dest, const char *input, int len)
    608 {
    609   char *new_dest;
    610   int oldlen;
    611 
    612   if (len == 0)
    613     return dest;
    614   /* printf("str_append: <<%s>>, <<%s>>, %d\n", dest, input, len); */
    615   oldlen = (dest ? strlen(dest) : 0);
    616   new_dest = realloc (dest, oldlen + len + 1);
    617   memset (new_dest + oldlen, 0, len + 1);
    618   return strncat (new_dest, input, len);
    619 }
    620 
    621 static const macro *
    622 lookup_macro (const char *name)
    623 {
    624   const macro *m;
    625 
    626   for (m = macros; m->name; ++m)
    627     if (strncmp (m->name, name, strlen(m->name)) == 0)
    628       return m;
    629 
    630   return 0;
    631 }
    632 
    633 static char *
    634 expand_macro (arg *args, int narg, const macro *mac)
    635 {
    636   char *result = 0, *rescanned_result = 0;
    637   char *e = mac->expansion;
    638   char *mark = e;
    639   int mac_arg = 0;
    640 
    641   /*  printf("expanding macro %s with %d args\n", mac->name, narg + 1); */
    642   while (*e)
    643     {
    644       if (*e == '`'
    645 	  && (*e+1)
    646 	  && ((*(e + 1) - '1') <= MAXARGS)
    647 	  && ((*(e + 1) - '1') <= narg))
    648 	{
    649 	  result = str_append (result, mark, e - mark);
    650 	  mac_arg = (*(e + 1) - '1');
    651 	  /* printf("replacing `%d with %s\n", mac_arg+1, args[mac_arg].start); */
    652 	  result = str_append (result, args[mac_arg].start, args[mac_arg].len);
    653 	  ++e;
    654 	  mark = e+1;
    655 	}
    656       ++e;
    657     }
    658 
    659   if (mark != e)
    660     result = str_append (result, mark, e - mark);
    661 
    662   if (result)
    663     {
    664       rescanned_result = expand_string (result, 0);
    665       free (result);
    666       return rescanned_result;
    667     }
    668   else
    669     return result;
    670 }
    671 
    672 #define IN_TEXT 0
    673 #define IN_ARGS 1
    674 
    675 static char *
    676 expand_string (const char *in, int first_only)
    677 {
    678   int num_expansions = 0;
    679   int depth = 0;
    680   int narg = -1;
    681   arg args[MAXARGS];
    682   int state = IN_TEXT;
    683   const char *mark = in;
    684   const macro *pmacro = NULL;
    685   char *expansion = 0;
    686   char *result = 0;
    687 
    688   while (*in)
    689     {
    690       switch (state)
    691 	{
    692 	case IN_TEXT:
    693 	  if (*in == '%' && *(in + 1) && (!first_only || num_expansions == 0))
    694 	    {
    695 	      pmacro = lookup_macro (in + 1);
    696 	      if (pmacro)
    697 		{
    698 		  /* printf("entering state %d at '%s'...\n", state, in); */
    699 		  result = str_append (result, mark, in - mark);
    700 		  mark = in;
    701 		  in += 1 + strlen (pmacro->name);
    702 		  while (*in == ' ') ++in;
    703 		  if (*in != '(')
    704 		    {
    705 		      state = IN_TEXT;
    706 		      pmacro = NULL;
    707 		    }
    708 		  else
    709 		    {
    710 		      state = IN_ARGS;
    711 		      narg = 0;
    712 		      args[narg].start = in + 1;
    713 		      args[narg].len = 0;
    714 		      mark = in + 1;
    715 		    }
    716 		}
    717 	    }
    718 	  break;
    719 	case IN_ARGS:
    720 	  if (depth == 0)
    721 	    {
    722 	      switch (*in)
    723 		{
    724 		case ',':
    725 		  narg++;
    726 		  args[narg].start = (in + 1);
    727 		  args[narg].len = 0;
    728 		  break;
    729 		case ')':
    730 		  state = IN_TEXT;
    731 		  /* printf("entering state %d at '%s'...\n", state, in); */
    732 		  if (pmacro)
    733 		    {
    734 		      expansion = 0;
    735 		      expansion = expand_macro (args, narg, pmacro);
    736 		      num_expansions++;
    737 		      if (expansion)
    738 			{
    739 			  result = str_append (result, expansion, strlen (expansion));
    740 			  free (expansion);
    741 			}
    742 		    }
    743 		  else
    744 		    {
    745 		      result = str_append (result, mark, in - mark);
    746 		    }
    747 		  pmacro = NULL;
    748 		  mark = in + 1;
    749 		  break;
    750 		case '(':
    751 		  depth++;
    752 		  /* Fall through.  */
    753 		default:
    754 		  args[narg].len++;
    755 		  break;
    756 		}
    757 	    }
    758 	  else
    759 	    {
    760 	      if (*in == ')')
    761 		depth--;
    762 	      if (narg > -1)
    763 		args[narg].len++;
    764 	    }
    765 	}
    766       ++in;
    767     }
    768 
    769   if (mark != in)
    770     result = str_append (result, mark, in - mark);
    771 
    772   return result;
    773 }
    774 
    775 #undef IN_ARGS
    776 #undef IN_TEXT
    777 #undef MAXARGS
    778 
    779 
    780 /* END LIGHTWEIGHT MACRO PROCESSOR.  */
    781 
    782 const char * mep_cgen_parse_operand
    783   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    784 
    785 const char *
    786 mep_cgen_expand_macros_and_parse_operand (CGEN_CPU_DESC cd, int opindex,
    787 					  const char ** strp_in, CGEN_FIELDS * fields)
    788 {
    789   const char * errmsg = NULL;
    790   char *str = 0, *hold = 0;
    791   const char **strp = 0;
    792 
    793   /* Set up a new pointer to macro-expanded string.  */
    794   str = expand_string (*strp_in, 1);
    795   /* fprintf (stderr, " expanded <<%s>> to <<%s>>\n", *strp_in, str); */
    796 
    797   hold = str;
    798   strp = (const char **)(&str);
    799 
    800   errmsg = mep_cgen_parse_operand (cd, opindex, strp, fields);
    801 
    802   /* Now work out the advance.  */
    803   if (strlen (str) == 0)
    804     *strp_in += strlen (*strp_in);
    805 
    806   else
    807     {
    808       if (strstr (*strp_in, str))
    809 	/* A macro-expansion was pulled off the front.  */
    810 	*strp_in = strstr (*strp_in, str);
    811       else
    812 	/* A non-macro-expansion was pulled off the front.  */
    813 	*strp_in += (str - hold);
    814     }
    815 
    816   free (hold);
    817 
    818   return errmsg;
    819 }
    820 
    821 #define CGEN_ASM_INIT_HOOK (cd->parse_operand = mep_cgen_expand_macros_and_parse_operand);
    822 
    823 /* -- dis.c */
    824 
    825 const char * mep_cgen_parse_operand
    826   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    827 
    828 /* Main entry point for operand parsing.
    829 
    830    This function is basically just a big switch statement.  Earlier versions
    831    used tables to look up the function to use, but
    832    - if the table contains both assembler and disassembler functions then
    833      the disassembler contains much of the assembler and vice-versa,
    834    - there's a lot of inlining possibilities as things grow,
    835    - using a switch statement avoids the function call overhead.
    836 
    837    This function could be moved into `parse_insn_normal', but keeping it
    838    separate makes clear the interface between `parse_insn_normal' and each of
    839    the handlers.  */
    840 
    841 const char *
    842 mep_cgen_parse_operand (CGEN_CPU_DESC cd,
    843 			   int opindex,
    844 			   const char ** strp,
    845 			   CGEN_FIELDS * fields)
    846 {
    847   const char * errmsg = NULL;
    848   /* Used by scalar operands that still need to be parsed.  */
    849   long junk ATTRIBUTE_UNUSED;
    850 
    851   switch (opindex)
    852     {
    853     case MEP_OPERAND_ADDR24A4 :
    854       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_ADDR24A4, (unsigned long *) (& fields->f_24u8a4n));
    855       break;
    856     case MEP_OPERAND_C5RMUIMM20 :
    857       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_C5RMUIMM20, (unsigned long *) (& fields->f_c5_rmuimm20));
    858       break;
    859     case MEP_OPERAND_C5RNMUIMM24 :
    860       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_C5RNMUIMM24, (unsigned long *) (& fields->f_c5_rnmuimm24));
    861       break;
    862     case MEP_OPERAND_CALLNUM :
    863       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CALLNUM, (unsigned long *) (& fields->f_callnum));
    864       break;
    865     case MEP_OPERAND_CCCC :
    866       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CCCC, (unsigned long *) (& fields->f_rm));
    867       break;
    868     case MEP_OPERAND_CCRN :
    869       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr, & fields->f_ccrn);
    870       break;
    871     case MEP_OPERAND_CDISP10 :
    872       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10, (long *) (& fields->f_cdisp10));
    873       break;
    874     case MEP_OPERAND_CDISP10A2 :
    875       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A2, (long *) (& fields->f_cdisp10));
    876       break;
    877     case MEP_OPERAND_CDISP10A4 :
    878       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A4, (long *) (& fields->f_cdisp10));
    879       break;
    880     case MEP_OPERAND_CDISP10A8 :
    881       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A8, (long *) (& fields->f_cdisp10));
    882       break;
    883     case MEP_OPERAND_CDISP12 :
    884       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_CDISP12, (long *) (& fields->f_12s20));
    885       break;
    886     case MEP_OPERAND_CIMM4 :
    887       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CIMM4, (unsigned long *) (& fields->f_rn));
    888       break;
    889     case MEP_OPERAND_CIMM5 :
    890       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CIMM5, (unsigned long *) (& fields->f_5u24));
    891       break;
    892     case MEP_OPERAND_CODE16 :
    893       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CODE16, (unsigned long *) (& fields->f_16u16));
    894       break;
    895     case MEP_OPERAND_CODE24 :
    896       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CODE24, (unsigned long *) (& fields->f_24u4n));
    897       break;
    898     case MEP_OPERAND_CP_FLAG :
    899       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr, & junk);
    900       break;
    901     case MEP_OPERAND_CRN :
    902       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr, & fields->f_crn);
    903       break;
    904     case MEP_OPERAND_CRN64 :
    905       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_crn);
    906       break;
    907     case MEP_OPERAND_CRNX :
    908       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr, & fields->f_crnx);
    909       break;
    910     case MEP_OPERAND_CRNX64 :
    911       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_crnx);
    912       break;
    913     case MEP_OPERAND_CROC :
    914       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u7);
    915       break;
    916     case MEP_OPERAND_CROP :
    917       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u23);
    918       break;
    919     case MEP_OPERAND_CRPC :
    920       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u26);
    921       break;
    922     case MEP_OPERAND_CRPP :
    923       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u18);
    924       break;
    925     case MEP_OPERAND_CRQC :
    926       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u21);
    927       break;
    928     case MEP_OPERAND_CRQP :
    929       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u13);
    930       break;
    931     case MEP_OPERAND_CSRN :
    932       errmsg = parse_csrn (cd, strp, & mep_cgen_opval_h_csr, & fields->f_csrn);
    933       break;
    934     case MEP_OPERAND_CSRN_IDX :
    935       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CSRN_IDX, (unsigned long *) (& fields->f_csrn));
    936       break;
    937     case MEP_OPERAND_DBG :
    938       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
    939       break;
    940     case MEP_OPERAND_DEPC :
    941       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
    942       break;
    943     case MEP_OPERAND_EPC :
    944       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
    945       break;
    946     case MEP_OPERAND_EXC :
    947       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
    948       break;
    949     case MEP_OPERAND_HI :
    950       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
    951       break;
    952     case MEP_OPERAND_IMM16P0 :
    953       errmsg = parse_unsigned16_range (cd, strp, MEP_OPERAND_IMM16P0, (unsigned long *) (& fields->f_ivc2_imm16p0));
    954       break;
    955     case MEP_OPERAND_IMM3P12 :
    956       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P12, (unsigned long *) (& fields->f_ivc2_3u12));
    957       break;
    958     case MEP_OPERAND_IMM3P25 :
    959       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P25, (unsigned long *) (& fields->f_ivc2_3u25));
    960       break;
    961     case MEP_OPERAND_IMM3P4 :
    962       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P4, (unsigned long *) (& fields->f_ivc2_3u4));
    963       break;
    964     case MEP_OPERAND_IMM3P5 :
    965       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P5, (unsigned long *) (& fields->f_ivc2_3u5));
    966       break;
    967     case MEP_OPERAND_IMM3P9 :
    968       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P9, (unsigned long *) (& fields->f_ivc2_3u9));
    969       break;
    970     case MEP_OPERAND_IMM4P10 :
    971       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P10, (unsigned long *) (& fields->f_ivc2_4u10));
    972       break;
    973     case MEP_OPERAND_IMM4P4 :
    974       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P4, (unsigned long *) (& fields->f_ivc2_4u4));
    975       break;
    976     case MEP_OPERAND_IMM4P8 :
    977       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P8, (unsigned long *) (& fields->f_ivc2_4u8));
    978       break;
    979     case MEP_OPERAND_IMM5P23 :
    980       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P23, (unsigned long *) (& fields->f_ivc2_5u23));
    981       break;
    982     case MEP_OPERAND_IMM5P3 :
    983       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P3, (unsigned long *) (& fields->f_ivc2_5u3));
    984       break;
    985     case MEP_OPERAND_IMM5P7 :
    986       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P7, (unsigned long *) (& fields->f_ivc2_5u7));
    987       break;
    988     case MEP_OPERAND_IMM5P8 :
    989       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P8, (unsigned long *) (& fields->f_ivc2_5u8));
    990       break;
    991     case MEP_OPERAND_IMM6P2 :
    992       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM6P2, (unsigned long *) (& fields->f_ivc2_6u2));
    993       break;
    994     case MEP_OPERAND_IMM6P6 :
    995       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM6P6, (unsigned long *) (& fields->f_ivc2_6u6));
    996       break;
    997     case MEP_OPERAND_IMM8P0 :
    998       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P0, (unsigned long *) (& fields->f_ivc2_8u0));
    999       break;
   1000     case MEP_OPERAND_IMM8P20 :
   1001       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P20, (unsigned long *) (& fields->f_ivc2_8u20));
   1002       break;
   1003     case MEP_OPERAND_IMM8P4 :
   1004       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P4, (unsigned long *) (& fields->f_ivc2_8u4));
   1005       break;
   1006     case MEP_OPERAND_IVC_X_0_2 :
   1007       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_2, (unsigned long *) (& fields->f_ivc2_2u0));
   1008       break;
   1009     case MEP_OPERAND_IVC_X_0_3 :
   1010       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_3, (unsigned long *) (& fields->f_ivc2_3u0));
   1011       break;
   1012     case MEP_OPERAND_IVC_X_0_4 :
   1013       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_4, (unsigned long *) (& fields->f_ivc2_4u0));
   1014       break;
   1015     case MEP_OPERAND_IVC_X_0_5 :
   1016       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_5, (unsigned long *) (& fields->f_ivc2_5u0));
   1017       break;
   1018     case MEP_OPERAND_IVC_X_6_1 :
   1019       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_1, (unsigned long *) (& fields->f_ivc2_1u6));
   1020       break;
   1021     case MEP_OPERAND_IVC_X_6_2 :
   1022       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_2, (unsigned long *) (& fields->f_ivc2_2u6));
   1023       break;
   1024     case MEP_OPERAND_IVC_X_6_3 :
   1025       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_3, (unsigned long *) (& fields->f_ivc2_3u6));
   1026       break;
   1027     case MEP_OPERAND_IVC2_ACC0_0 :
   1028       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1029       break;
   1030     case MEP_OPERAND_IVC2_ACC0_1 :
   1031       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1032       break;
   1033     case MEP_OPERAND_IVC2_ACC0_2 :
   1034       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1035       break;
   1036     case MEP_OPERAND_IVC2_ACC0_3 :
   1037       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1038       break;
   1039     case MEP_OPERAND_IVC2_ACC0_4 :
   1040       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1041       break;
   1042     case MEP_OPERAND_IVC2_ACC0_5 :
   1043       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1044       break;
   1045     case MEP_OPERAND_IVC2_ACC0_6 :
   1046       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1047       break;
   1048     case MEP_OPERAND_IVC2_ACC0_7 :
   1049       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1050       break;
   1051     case MEP_OPERAND_IVC2_ACC1_0 :
   1052       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1053       break;
   1054     case MEP_OPERAND_IVC2_ACC1_1 :
   1055       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1056       break;
   1057     case MEP_OPERAND_IVC2_ACC1_2 :
   1058       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1059       break;
   1060     case MEP_OPERAND_IVC2_ACC1_3 :
   1061       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1062       break;
   1063     case MEP_OPERAND_IVC2_ACC1_4 :
   1064       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1065       break;
   1066     case MEP_OPERAND_IVC2_ACC1_5 :
   1067       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1068       break;
   1069     case MEP_OPERAND_IVC2_ACC1_6 :
   1070       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1071       break;
   1072     case MEP_OPERAND_IVC2_ACC1_7 :
   1073       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1074       break;
   1075     case MEP_OPERAND_IVC2_CC :
   1076       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1077       break;
   1078     case MEP_OPERAND_IVC2_COFA0 :
   1079       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1080       break;
   1081     case MEP_OPERAND_IVC2_COFA1 :
   1082       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1083       break;
   1084     case MEP_OPERAND_IVC2_COFR0 :
   1085       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1086       break;
   1087     case MEP_OPERAND_IVC2_COFR1 :
   1088       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1089       break;
   1090     case MEP_OPERAND_IVC2_CSAR0 :
   1091       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1092       break;
   1093     case MEP_OPERAND_IVC2_CSAR1 :
   1094       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & junk);
   1095       break;
   1096     case MEP_OPERAND_IVC2C3CCRN :
   1097       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & fields->f_ivc2_ccrn_c3);
   1098       break;
   1099     case MEP_OPERAND_IVC2CCRN :
   1100       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & fields->f_ivc2_ccrn);
   1101       break;
   1102     case MEP_OPERAND_IVC2CRN :
   1103       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_crnx);
   1104       break;
   1105     case MEP_OPERAND_IVC2RM :
   1106       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_ivc2_crm);
   1107       break;
   1108     case MEP_OPERAND_LO :
   1109       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1110       break;
   1111     case MEP_OPERAND_LP :
   1112       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1113       break;
   1114     case MEP_OPERAND_MB0 :
   1115       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1116       break;
   1117     case MEP_OPERAND_MB1 :
   1118       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1119       break;
   1120     case MEP_OPERAND_ME0 :
   1121       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1122       break;
   1123     case MEP_OPERAND_ME1 :
   1124       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1125       break;
   1126     case MEP_OPERAND_NPC :
   1127       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1128       break;
   1129     case MEP_OPERAND_OPT :
   1130       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1131       break;
   1132     case MEP_OPERAND_PCABS24A2 :
   1133       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_PCABS24A2, (unsigned long *) (& fields->f_24u5a2n));
   1134       break;
   1135     case MEP_OPERAND_PCREL12A2 :
   1136       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL12A2, (long *) (& fields->f_12s4a2));
   1137       break;
   1138     case MEP_OPERAND_PCREL17A2 :
   1139       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL17A2, (long *) (& fields->f_17s16a2));
   1140       break;
   1141     case MEP_OPERAND_PCREL24A2 :
   1142       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL24A2, (long *) (& fields->f_24s5a2n));
   1143       break;
   1144     case MEP_OPERAND_PCREL8A2 :
   1145       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL8A2, (long *) (& fields->f_8s8a2));
   1146       break;
   1147     case MEP_OPERAND_PSW :
   1148       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1149       break;
   1150     case MEP_OPERAND_R0 :
   1151       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1152       break;
   1153     case MEP_OPERAND_R1 :
   1154       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1155       break;
   1156     case MEP_OPERAND_RL :
   1157       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rl);
   1158       break;
   1159     case MEP_OPERAND_RL5 :
   1160       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rl5);
   1161       break;
   1162     case MEP_OPERAND_RM :
   1163       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rm);
   1164       break;
   1165     case MEP_OPERAND_RMA :
   1166       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rm);
   1167       break;
   1168     case MEP_OPERAND_RN :
   1169       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1170       break;
   1171     case MEP_OPERAND_RN3 :
   1172       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1173       break;
   1174     case MEP_OPERAND_RN3C :
   1175       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1176       break;
   1177     case MEP_OPERAND_RN3L :
   1178       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1179       break;
   1180     case MEP_OPERAND_RN3S :
   1181       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1182       break;
   1183     case MEP_OPERAND_RN3UC :
   1184       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1185       break;
   1186     case MEP_OPERAND_RN3UL :
   1187       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1188       break;
   1189     case MEP_OPERAND_RN3US :
   1190       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
   1191       break;
   1192     case MEP_OPERAND_RNC :
   1193       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1194       break;
   1195     case MEP_OPERAND_RNL :
   1196       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1197       break;
   1198     case MEP_OPERAND_RNS :
   1199       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1200       break;
   1201     case MEP_OPERAND_RNUC :
   1202       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1203       break;
   1204     case MEP_OPERAND_RNUL :
   1205       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1206       break;
   1207     case MEP_OPERAND_RNUS :
   1208       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
   1209       break;
   1210     case MEP_OPERAND_SAR :
   1211       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
   1212       break;
   1213     case MEP_OPERAND_SDISP16 :
   1214       errmsg = parse_signed16 (cd, strp, MEP_OPERAND_SDISP16, (long *) (& fields->f_16s16));
   1215       break;
   1216     case MEP_OPERAND_SIMM16 :
   1217       errmsg = parse_signed16 (cd, strp, MEP_OPERAND_SIMM16, (long *) (& fields->f_16s16));
   1218       break;
   1219     case MEP_OPERAND_SIMM16P0 :
   1220       errmsg = parse_signed16_range (cd, strp, MEP_OPERAND_SIMM16P0, (long *) (& fields->f_ivc2_simm16p0));
   1221       break;
   1222     case MEP_OPERAND_SIMM6 :
   1223       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM6, (long *) (& fields->f_6s8));
   1224       break;
   1225     case MEP_OPERAND_SIMM8 :
   1226       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8, (long *) (& fields->f_8s8));
   1227       break;
   1228     case MEP_OPERAND_SIMM8P0 :
   1229       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P0, (long *) (& fields->f_ivc2_8s0));
   1230       break;
   1231     case MEP_OPERAND_SIMM8P20 :
   1232       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P20, (long *) (& fields->f_ivc2_8s20));
   1233       break;
   1234     case MEP_OPERAND_SIMM8P4 :
   1235       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P4, (long *) (& fields->f_ivc2_8s4));
   1236       break;
   1237     case MEP_OPERAND_SP :
   1238       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1239       break;
   1240     case MEP_OPERAND_SPR :
   1241       errmsg = parse_spreg (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1242       break;
   1243     case MEP_OPERAND_TP :
   1244       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1245       break;
   1246     case MEP_OPERAND_TPR :
   1247       errmsg = parse_tpreg (cd, strp, & mep_cgen_opval_h_gpr, & junk);
   1248       break;
   1249     case MEP_OPERAND_UDISP2 :
   1250       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_UDISP2, (long *) (& fields->f_2u6));
   1251       break;
   1252     case MEP_OPERAND_UDISP7 :
   1253       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7, (unsigned long *) (& fields->f_7u9));
   1254       break;
   1255     case MEP_OPERAND_UDISP7A2 :
   1256       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7A2, (unsigned long *) (& fields->f_7u9a2));
   1257       break;
   1258     case MEP_OPERAND_UDISP7A4 :
   1259       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7A4, (unsigned long *) (& fields->f_7u9a4));
   1260       break;
   1261     case MEP_OPERAND_UIMM16 :
   1262       errmsg = parse_unsigned16 (cd, strp, MEP_OPERAND_UIMM16, (unsigned long *) (& fields->f_16u16));
   1263       break;
   1264     case MEP_OPERAND_UIMM2 :
   1265       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM2, (unsigned long *) (& fields->f_2u10));
   1266       break;
   1267     case MEP_OPERAND_UIMM24 :
   1268       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM24, (unsigned long *) (& fields->f_24u8n));
   1269       break;
   1270     case MEP_OPERAND_UIMM3 :
   1271       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM3, (unsigned long *) (& fields->f_3u5));
   1272       break;
   1273     case MEP_OPERAND_UIMM4 :
   1274       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM4, (unsigned long *) (& fields->f_4u8));
   1275       break;
   1276     case MEP_OPERAND_UIMM5 :
   1277       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM5, (unsigned long *) (& fields->f_5u8));
   1278       break;
   1279     case MEP_OPERAND_UIMM7A4 :
   1280       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_UIMM7A4, (unsigned long *) (& fields->f_7u9a4));
   1281       break;
   1282     case MEP_OPERAND_ZERO :
   1283       errmsg = parse_zero (cd, strp, MEP_OPERAND_ZERO, (long *) (& junk));
   1284       break;
   1285 
   1286     default :
   1287       /* xgettext:c-format */
   1288       opcodes_error_handler
   1289 	(_("internal error: unrecognized field %d while parsing"),
   1290 	 opindex);
   1291       abort ();
   1292   }
   1293 
   1294   return errmsg;
   1295 }
   1296 
   1297 cgen_parse_fn * const mep_cgen_parse_handlers[] =
   1298 {
   1299   parse_insn_normal,
   1300 };
   1301 
   1302 void
   1303 mep_cgen_init_asm (CGEN_CPU_DESC cd)
   1304 {
   1305   mep_cgen_init_opcode_table (cd);
   1306   mep_cgen_init_ibld_table (cd);
   1307   cd->parse_handlers = & mep_cgen_parse_handlers[0];
   1308   cd->parse_operand = mep_cgen_parse_operand;
   1309 #ifdef CGEN_ASM_INIT_HOOK
   1310 CGEN_ASM_INIT_HOOK
   1311 #endif
   1312 }
   1313 
   1314 
   1315 
   1317 /* Regex construction routine.
   1318 
   1319    This translates an opcode syntax string into a regex string,
   1320    by replacing any non-character syntax element (such as an
   1321    opcode) with the pattern '.*'
   1322 
   1323    It then compiles the regex and stores it in the opcode, for
   1324    later use by mep_cgen_assemble_insn
   1325 
   1326    Returns NULL for success, an error message for failure.  */
   1327 
   1328 char *
   1329 mep_cgen_build_insn_regex (CGEN_INSN *insn)
   1330 {
   1331   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
   1332   const char *mnem = CGEN_INSN_MNEMONIC (insn);
   1333   char rxbuf[CGEN_MAX_RX_ELEMENTS];
   1334   char *rx = rxbuf;
   1335   const CGEN_SYNTAX_CHAR_TYPE *syn;
   1336   int reg_err;
   1337 
   1338   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
   1339 
   1340   /* Mnemonics come first in the syntax string.  */
   1341   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
   1342     return _("missing mnemonic in syntax string");
   1343   ++syn;
   1344 
   1345   /* Generate a case sensitive regular expression that emulates case
   1346      insensitive matching in the "C" locale.  We cannot generate a case
   1347      insensitive regular expression because in Turkish locales, 'i' and 'I'
   1348      are not equal modulo case conversion.  */
   1349 
   1350   /* Copy the literal mnemonic out of the insn.  */
   1351   for (; *mnem; mnem++)
   1352     {
   1353       char c = *mnem;
   1354 
   1355       if (ISALPHA (c))
   1356 	{
   1357 	  *rx++ = '[';
   1358 	  *rx++ = TOLOWER (c);
   1359 	  *rx++ = TOUPPER (c);
   1360 	  *rx++ = ']';
   1361 	}
   1362       else
   1363 	*rx++ = c;
   1364     }
   1365 
   1366   /* Copy any remaining literals from the syntax string into the rx.  */
   1367   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
   1368     {
   1369       if (CGEN_SYNTAX_CHAR_P (* syn))
   1370 	{
   1371 	  char c = CGEN_SYNTAX_CHAR (* syn);
   1372 
   1373 	  switch (c)
   1374 	    {
   1375 	      /* Escape any regex metacharacters in the syntax.  */
   1376 	    case '.': case '[': case '\\':
   1377 	    case '*': case '^': case '$':
   1378 
   1379 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
   1380 	    case '?': case '{': case '}':
   1381 	    case '(': case ')': case '*':
   1382 	    case '|': case '+': case ']':
   1383 #endif
   1384 	      *rx++ = '\\';
   1385 	      *rx++ = c;
   1386 	      break;
   1387 
   1388 	    default:
   1389 	      if (ISALPHA (c))
   1390 		{
   1391 		  *rx++ = '[';
   1392 		  *rx++ = TOLOWER (c);
   1393 		  *rx++ = TOUPPER (c);
   1394 		  *rx++ = ']';
   1395 		}
   1396 	      else
   1397 		*rx++ = c;
   1398 	      break;
   1399 	    }
   1400 	}
   1401       else
   1402 	{
   1403 	  /* Replace non-syntax fields with globs.  */
   1404 	  *rx++ = '.';
   1405 	  *rx++ = '*';
   1406 	}
   1407     }
   1408 
   1409   /* Trailing whitespace ok.  */
   1410   * rx++ = '[';
   1411   * rx++ = ' ';
   1412   * rx++ = '\t';
   1413   * rx++ = ']';
   1414   * rx++ = '*';
   1415 
   1416   /* But anchor it after that.  */
   1417   * rx++ = '$';
   1418   * rx = '\0';
   1419 
   1420   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
   1421   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
   1422 
   1423   if (reg_err == 0)
   1424     return NULL;
   1425   else
   1426     {
   1427       static char msg[80];
   1428 
   1429       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
   1430       regfree ((regex_t *) CGEN_INSN_RX (insn));
   1431       free (CGEN_INSN_RX (insn));
   1432       (CGEN_INSN_RX (insn)) = NULL;
   1433       return msg;
   1434     }
   1435 }
   1436 
   1437 
   1438 /* Default insn parser.
   1440 
   1441    The syntax string is scanned and operands are parsed and stored in FIELDS.
   1442    Relocs are queued as we go via other callbacks.
   1443 
   1444    ??? Note that this is currently an all-or-nothing parser.  If we fail to
   1445    parse the instruction, we return 0 and the caller will start over from
   1446    the beginning.  Backtracking will be necessary in parsing subexpressions,
   1447    but that can be handled there.  Not handling backtracking here may get
   1448    expensive in the case of the m68k.  Deal with later.
   1449 
   1450    Returns NULL for success, an error message for failure.  */
   1451 
   1452 static const char *
   1453 parse_insn_normal (CGEN_CPU_DESC cd,
   1454 		   const CGEN_INSN *insn,
   1455 		   const char **strp,
   1456 		   CGEN_FIELDS *fields)
   1457 {
   1458   /* ??? Runtime added insns not handled yet.  */
   1459   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
   1460   const char *str = *strp;
   1461   const char *errmsg;
   1462   const char *p;
   1463   const CGEN_SYNTAX_CHAR_TYPE * syn;
   1464 #ifdef CGEN_MNEMONIC_OPERANDS
   1465   /* FIXME: wip */
   1466   int past_opcode_p;
   1467 #endif
   1468 
   1469   /* For now we assume the mnemonic is first (there are no leading operands).
   1470      We can parse it without needing to set up operand parsing.
   1471      GAS's input scrubber will ensure mnemonics are lowercase, but we may
   1472      not be called from GAS.  */
   1473   p = CGEN_INSN_MNEMONIC (insn);
   1474   while (*p && TOLOWER (*p) == TOLOWER (*str))
   1475     ++p, ++str;
   1476 
   1477   if (* p)
   1478     return _("unrecognized instruction");
   1479 
   1480 #ifndef CGEN_MNEMONIC_OPERANDS
   1481   if (* str && ! ISSPACE (* str))
   1482     return _("unrecognized instruction");
   1483 #endif
   1484 
   1485   CGEN_INIT_PARSE (cd);
   1486   cgen_init_parse_operand (cd);
   1487 #ifdef CGEN_MNEMONIC_OPERANDS
   1488   past_opcode_p = 0;
   1489 #endif
   1490 
   1491   /* We don't check for (*str != '\0') here because we want to parse
   1492      any trailing fake arguments in the syntax string.  */
   1493   syn = CGEN_SYNTAX_STRING (syntax);
   1494 
   1495   /* Mnemonics come first for now, ensure valid string.  */
   1496   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
   1497     abort ();
   1498 
   1499   ++syn;
   1500 
   1501   while (* syn != 0)
   1502     {
   1503       /* Non operand chars must match exactly.  */
   1504       if (CGEN_SYNTAX_CHAR_P (* syn))
   1505 	{
   1506 	  /* FIXME: While we allow for non-GAS callers above, we assume the
   1507 	     first char after the mnemonic part is a space.  */
   1508 	  /* FIXME: We also take inappropriate advantage of the fact that
   1509 	     GAS's input scrubber will remove extraneous blanks.  */
   1510 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
   1511 	    {
   1512 #ifdef CGEN_MNEMONIC_OPERANDS
   1513 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
   1514 		past_opcode_p = 1;
   1515 #endif
   1516 	      ++ syn;
   1517 	      ++ str;
   1518 	    }
   1519 	  else if (*str)
   1520 	    {
   1521 	      /* Syntax char didn't match.  Can't be this insn.  */
   1522 	      static char msg [80];
   1523 
   1524 	      /* xgettext:c-format */
   1525 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
   1526 		       CGEN_SYNTAX_CHAR(*syn), *str);
   1527 	      return msg;
   1528 	    }
   1529 	  else
   1530 	    {
   1531 	      /* Ran out of input.  */
   1532 	      static char msg [80];
   1533 
   1534 	      /* xgettext:c-format */
   1535 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
   1536 		       CGEN_SYNTAX_CHAR(*syn));
   1537 	      return msg;
   1538 	    }
   1539 	  continue;
   1540 	}
   1541 
   1542 #ifdef CGEN_MNEMONIC_OPERANDS
   1543       (void) past_opcode_p;
   1544 #endif
   1545       /* We have an operand of some sort.  */
   1546       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
   1547       if (errmsg)
   1548 	return errmsg;
   1549 
   1550       /* Done with this operand, continue with next one.  */
   1551       ++ syn;
   1552     }
   1553 
   1554   /* If we're at the end of the syntax string, we're done.  */
   1555   if (* syn == 0)
   1556     {
   1557       /* FIXME: For the moment we assume a valid `str' can only contain
   1558 	 blanks now.  IE: We needn't try again with a longer version of
   1559 	 the insn and it is assumed that longer versions of insns appear
   1560 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
   1561       while (ISSPACE (* str))
   1562 	++ str;
   1563 
   1564       if (* str != '\0')
   1565 	return _("junk at end of line"); /* FIXME: would like to include `str' */
   1566 
   1567       return NULL;
   1568     }
   1569 
   1570   /* We couldn't parse it.  */
   1571   return _("unrecognized instruction");
   1572 }
   1573 
   1574 /* Main entry point.
   1576    This routine is called for each instruction to be assembled.
   1577    STR points to the insn to be assembled.
   1578    We assume all necessary tables have been initialized.
   1579    The assembled instruction, less any fixups, is stored in BUF.
   1580    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
   1581    still needs to be converted to target byte order, otherwise BUF is an array
   1582    of bytes in target byte order.
   1583    The result is a pointer to the insn's entry in the opcode table,
   1584    or NULL if an error occured (an error message will have already been
   1585    printed).
   1586 
   1587    Note that when processing (non-alias) macro-insns,
   1588    this function recurses.
   1589 
   1590    ??? It's possible to make this cpu-independent.
   1591    One would have to deal with a few minor things.
   1592    At this point in time doing so would be more of a curiosity than useful
   1593    [for example this file isn't _that_ big], but keeping the possibility in
   1594    mind helps keep the design clean.  */
   1595 
   1596 const CGEN_INSN *
   1597 mep_cgen_assemble_insn (CGEN_CPU_DESC cd,
   1598 			   const char *str,
   1599 			   CGEN_FIELDS *fields,
   1600 			   CGEN_INSN_BYTES_PTR buf,
   1601 			   char **errmsg)
   1602 {
   1603   const char *start;
   1604   CGEN_INSN_LIST *ilist;
   1605   const char *parse_errmsg = NULL;
   1606   const char *insert_errmsg = NULL;
   1607   int recognized_mnemonic = 0;
   1608 
   1609   /* Skip leading white space.  */
   1610   while (ISSPACE (* str))
   1611     ++ str;
   1612 
   1613   /* The instructions are stored in hashed lists.
   1614      Get the first in the list.  */
   1615   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
   1616 
   1617   /* Keep looking until we find a match.  */
   1618   start = str;
   1619   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
   1620     {
   1621       const CGEN_INSN *insn = ilist->insn;
   1622       recognized_mnemonic = 1;
   1623 
   1624 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
   1625       /* Not usually needed as unsupported opcodes
   1626 	 shouldn't be in the hash lists.  */
   1627       /* Is this insn supported by the selected cpu?  */
   1628       if (! mep_cgen_insn_supported (cd, insn))
   1629 	continue;
   1630 #endif
   1631       /* If the RELAXED attribute is set, this is an insn that shouldn't be
   1632 	 chosen immediately.  Instead, it is used during assembler/linker
   1633 	 relaxation if possible.  */
   1634       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
   1635 	continue;
   1636 
   1637       str = start;
   1638 
   1639       /* Skip this insn if str doesn't look right lexically.  */
   1640       if (CGEN_INSN_RX (insn) != NULL &&
   1641 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
   1642 	continue;
   1643 
   1644       /* Allow parse/insert handlers to obtain length of insn.  */
   1645       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
   1646 
   1647       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
   1648       if (parse_errmsg != NULL)
   1649 	continue;
   1650 
   1651       /* ??? 0 is passed for `pc'.  */
   1652       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
   1653 						 (bfd_vma) 0);
   1654       if (insert_errmsg != NULL)
   1655         continue;
   1656 
   1657       /* It is up to the caller to actually output the insn and any
   1658          queued relocs.  */
   1659       return insn;
   1660     }
   1661 
   1662   {
   1663     static char errbuf[150];
   1664     const char *tmp_errmsg;
   1665 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
   1666 #define be_verbose 1
   1667 #else
   1668 #define be_verbose 0
   1669 #endif
   1670 
   1671     if (be_verbose)
   1672       {
   1673 	/* If requesting verbose error messages, use insert_errmsg.
   1674 	   Failing that, use parse_errmsg.  */
   1675 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
   1676 		      parse_errmsg ? parse_errmsg :
   1677 		      recognized_mnemonic ?
   1678 		      _("unrecognized form of instruction") :
   1679 		      _("unrecognized instruction"));
   1680 
   1681 	if (strlen (start) > 50)
   1682 	  /* xgettext:c-format */
   1683 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
   1684 	else
   1685 	  /* xgettext:c-format */
   1686 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
   1687       }
   1688     else
   1689       {
   1690 	if (strlen (start) > 50)
   1691 	  /* xgettext:c-format */
   1692 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
   1693 	else
   1694 	  /* xgettext:c-format */
   1695 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
   1696       }
   1697 
   1698     *errmsg = errbuf;
   1699     return NULL;
   1700   }
   1701 }
   1702