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-2014 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 "ip2k-desc.h"
     35 #include "ip2k-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 static const char *
     55 parse_fr (CGEN_CPU_DESC cd,
     56 	  const char **strp,
     57 	  int opindex,
     58 	  unsigned long *valuep)
     59 {
     60   const char *errmsg;
     61   const char *old_strp;
     62   char *afteroffset;
     63   enum cgen_parse_operand_result result_type;
     64   bfd_vma value;
     65   extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
     66   bfd_vma tempvalue;
     67 
     68   old_strp = *strp;
     69   afteroffset = NULL;
     70 
     71   /* Check here to see if you're about to try parsing a w as the first arg
     72      and return an error if you are.  */
     73   if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0))
     74     {
     75       (*strp)++;
     76 
     77       if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp))
     78 	{
     79 	  /* We've been passed a w.  Return with an error message so that
     80 	     cgen will try the next parsing option.  */
     81 	  errmsg = _("W keyword invalid in FR operand slot.");
     82 	  return errmsg;
     83 	}
     84       *strp = old_strp;
     85     }
     86 
     87   /* Attempt parse as register keyword. */
     88   errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
     89 			       (long *) valuep);
     90   if (*strp != NULL
     91       && errmsg == NULL)
     92     return errmsg;
     93 
     94   /* Attempt to parse for "(IP)".  */
     95   afteroffset = strstr (*strp, "(IP)");
     96 
     97   if (afteroffset == NULL)
     98     /* Make sure it's not in lower case.  */
     99     afteroffset = strstr (*strp, "(ip)");
    100 
    101   if (afteroffset != NULL)
    102     {
    103       if (afteroffset != *strp)
    104 	{
    105 	  /* Invalid offset present.  */
    106 	  errmsg = _("offset(IP) is not a valid form");
    107 	  return errmsg;
    108 	}
    109       else
    110 	{
    111 	  *strp += 4;
    112 	  *valuep = 0;
    113 	  errmsg = NULL;
    114 	  return errmsg;
    115 	}
    116     }
    117 
    118   /* Attempt to parse for DP. ex: mov w, offset(DP)
    119                                   mov offset(DP),w   */
    120 
    121   /* Try parsing it as an address and see what comes back.  */
    122   afteroffset = strstr (*strp, "(DP)");
    123 
    124   if (afteroffset == NULL)
    125     /* Maybe it's in lower case.  */
    126     afteroffset = strstr (*strp, "(dp)");
    127 
    128   if (afteroffset != NULL)
    129     {
    130       if (afteroffset == *strp)
    131 	{
    132 	  /* No offset present. Use 0 by default.  */
    133 	  tempvalue = 0;
    134 	  errmsg = NULL;
    135 	}
    136       else
    137 	errmsg = cgen_parse_address (cd, strp, opindex,
    138 				     BFD_RELOC_IP2K_FR_OFFSET,
    139 				     & result_type, & tempvalue);
    140 
    141       if (errmsg == NULL)
    142 	{
    143 	  if (tempvalue <= 127)
    144 	    {
    145 	      /* Value is ok.  Fix up the first 2 bits and return.  */
    146 	      *valuep = 0x0100 | tempvalue;
    147 	      *strp += 4; /* Skip over the (DP) in *strp.  */
    148 	      return errmsg;
    149 	    }
    150 	  else
    151 	    {
    152 	      /* Found something there in front of (DP) but it's out
    153 		 of range.  */
    154 	      errmsg = _("(DP) offset out of range.");
    155 	      return errmsg;
    156 	    }
    157 	}
    158     }
    159 
    160 
    161   /* Attempt to parse for SP. ex: mov w, offset(SP)
    162                                   mov offset(SP), w.  */
    163   afteroffset = strstr (*strp, "(SP)");
    164 
    165   if (afteroffset == NULL)
    166     /* Maybe it's in lower case.  */
    167     afteroffset = strstr (*strp, "(sp)");
    168 
    169   if (afteroffset != NULL)
    170     {
    171       if (afteroffset == *strp)
    172 	{
    173 	  /* No offset present. Use 0 by default.  */
    174 	  tempvalue = 0;
    175 	  errmsg = NULL;
    176 	}
    177       else
    178 	errmsg = cgen_parse_address (cd, strp, opindex,
    179 				     BFD_RELOC_IP2K_FR_OFFSET,
    180 				     & result_type, & tempvalue);
    181 
    182       if (errmsg == NULL)
    183 	{
    184 	  if (tempvalue <= 127)
    185 	    {
    186 	      /* Value is ok.  Fix up the first 2 bits and return.  */
    187 	      *valuep = 0x0180 | tempvalue;
    188 	      *strp += 4; /* Skip over the (SP) in *strp.  */
    189 	      return errmsg;
    190 	    }
    191 	  else
    192 	    {
    193 	      /* Found something there in front of (SP) but it's out
    194 		 of range.  */
    195 	      errmsg = _("(SP) offset out of range.");
    196 	      return errmsg;
    197 	    }
    198 	}
    199     }
    200 
    201   /* Attempt to parse as an address.  */
    202   *strp = old_strp;
    203   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
    204 			       & result_type, & value);
    205   if (errmsg == NULL)
    206     {
    207       *valuep = value;
    208 
    209       /* If a parenthesis is found, warn about invalid form.  */
    210       if (**strp == '(')
    211 	errmsg = _("illegal use of parentheses");
    212 
    213       /* If a numeric value is specified, ensure that it is between
    214 	 1 and 255.  */
    215       else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    216 	{
    217 	  if (value < 0x1 || value > 0xff)
    218 	    errmsg = _("operand out of range (not between 1 and 255)");
    219 	}
    220     }
    221   return errmsg;
    222 }
    223 
    224 static const char *
    225 parse_addr16 (CGEN_CPU_DESC cd,
    226 	      const char **strp,
    227 	      int opindex,
    228 	      unsigned long *valuep)
    229 {
    230   const char *errmsg;
    231   enum cgen_parse_operand_result result_type;
    232   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
    233   bfd_vma value;
    234 
    235   if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H)
    236     code = BFD_RELOC_IP2K_HI8DATA;
    237   else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L)
    238     code = BFD_RELOC_IP2K_LO8DATA;
    239   else
    240     {
    241       /* Something is very wrong. opindex has to be one of the above.  */
    242       errmsg = _("parse_addr16: invalid opindex.");
    243       return errmsg;
    244     }
    245 
    246   errmsg = cgen_parse_address (cd, strp, opindex, code,
    247 			       & result_type, & value);
    248   if (errmsg == NULL)
    249     {
    250       /* We either have a relocation or a number now.  */
    251       if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    252 	{
    253 	  /* We got a number back.  */
    254 	  if (code == BFD_RELOC_IP2K_HI8DATA)
    255             value >>= 8;
    256 	  else
    257 	    /* code = BFD_RELOC_IP2K_LOW8DATA.  */
    258 	    value &= 0x00FF;
    259 	}
    260       *valuep = value;
    261     }
    262 
    263   return errmsg;
    264 }
    265 
    266 static const char *
    267 parse_addr16_cjp (CGEN_CPU_DESC cd,
    268 		  const char **strp,
    269 		  int opindex,
    270 		  unsigned long *valuep)
    271 {
    272   const char *errmsg;
    273   enum cgen_parse_operand_result result_type;
    274   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
    275   bfd_vma value;
    276 
    277   if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
    278     code = BFD_RELOC_IP2K_ADDR16CJP;
    279   else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
    280     code = BFD_RELOC_IP2K_PAGE3;
    281 
    282   errmsg = cgen_parse_address (cd, strp, opindex, code,
    283 			       & result_type, & value);
    284   if (errmsg == NULL)
    285     {
    286       if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    287 	{
    288 	  if ((value & 0x1) == 0)  /* If the address is even .... */
    289 	    {
    290 	      if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
    291                 *valuep = (value >> 1) & 0x1FFF;  /* Should mask be 1FFF?  */
    292 	      else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
    293                 *valuep = (value >> 14) & 0x7;
    294 	    }
    295           else
    296  	    errmsg = _("Byte address required. - must be even.");
    297 	}
    298       else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
    299 	{
    300 	  /* This will happen for things like (s2-s1) where s2 and s1
    301 	     are labels.  */
    302 	  *valuep = value;
    303 	}
    304       else
    305         errmsg = _("cgen_parse_address returned a symbol. Literal required.");
    306     }
    307   return errmsg;
    308 }
    309 
    310 static const char *
    311 parse_lit8 (CGEN_CPU_DESC cd,
    312 	    const char **strp,
    313 	    int opindex,
    314 	    long *valuep)
    315 {
    316   const char *errmsg;
    317   enum cgen_parse_operand_result result_type;
    318   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
    319   bfd_vma value;
    320 
    321   /* Parse %OP relocating operators.  */
    322   if (strncmp (*strp, "%bank", 5) == 0)
    323     {
    324       *strp += 5;
    325       code = BFD_RELOC_IP2K_BANK;
    326     }
    327   else if (strncmp (*strp, "%lo8data", 8) == 0)
    328     {
    329       *strp += 8;
    330       code = BFD_RELOC_IP2K_LO8DATA;
    331     }
    332   else if (strncmp (*strp, "%hi8data", 8) == 0)
    333     {
    334       *strp += 8;
    335       code = BFD_RELOC_IP2K_HI8DATA;
    336     }
    337   else if (strncmp (*strp, "%ex8data", 8) == 0)
    338     {
    339       *strp += 8;
    340       code = BFD_RELOC_IP2K_EX8DATA;
    341     }
    342   else if (strncmp (*strp, "%lo8insn", 8) == 0)
    343     {
    344       *strp += 8;
    345       code = BFD_RELOC_IP2K_LO8INSN;
    346     }
    347   else if (strncmp (*strp, "%hi8insn", 8) == 0)
    348     {
    349       *strp += 8;
    350       code = BFD_RELOC_IP2K_HI8INSN;
    351     }
    352 
    353   /* Parse %op operand.  */
    354   if (code != BFD_RELOC_NONE)
    355     {
    356       errmsg = cgen_parse_address (cd, strp, opindex, code,
    357 				   & result_type, & value);
    358       if ((errmsg == NULL) &&
    359 	  (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
    360 	errmsg = _("percent-operator operand is not a symbol");
    361 
    362       *valuep = value;
    363     }
    364   /* Parse as a number.  */
    365   else
    366     {
    367       errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
    368 
    369       /* Truncate to eight bits to accept both signed and unsigned input.  */
    370       if (errmsg == NULL)
    371 	*valuep &= 0xFF;
    372     }
    373 
    374   return errmsg;
    375 }
    376 
    377 static const char *
    378 parse_bit3 (CGEN_CPU_DESC cd,
    379 	    const char **strp,
    380 	    int opindex,
    381 	    unsigned long *valuep)
    382 {
    383   const char *errmsg;
    384   char mode = 0;
    385   long count = 0;
    386   unsigned long value;
    387 
    388   if (strncmp (*strp, "%bit", 4) == 0)
    389     {
    390       *strp += 4;
    391       mode = 1;
    392     }
    393   else if (strncmp (*strp, "%msbbit", 7) == 0)
    394     {
    395       *strp += 7;
    396       mode = 1;
    397     }
    398   else if (strncmp (*strp, "%lsbbit", 7) == 0)
    399     {
    400       *strp += 7;
    401       mode = 2;
    402     }
    403 
    404   errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
    405   if (errmsg)
    406     return errmsg;
    407 
    408   if (mode)
    409     {
    410       value = * valuep;
    411       if (value == 0)
    412 	{
    413 	  errmsg = _("Attempt to find bit index of 0");
    414 	  return errmsg;
    415 	}
    416 
    417       if (mode == 1)
    418 	{
    419 	  count = 31;
    420 	  while ((value & 0x80000000) == 0)
    421 	    {
    422 	      count--;
    423 	      value <<= 1;
    424 	    }
    425 	}
    426       else if (mode == 2)
    427 	{
    428 	  count = 0;
    429 	  while ((value & 0x00000001) == 0)
    430 	    {
    431 	      count++;
    432 	      value >>= 1;
    433 	    }
    434 	}
    435 
    436       *valuep = count;
    437     }
    438 
    439   return errmsg;
    440 }
    441 
    442 /* -- dis.c */
    443 
    444 const char * ip2k_cgen_parse_operand
    445   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    446 
    447 /* Main entry point for operand parsing.
    448 
    449    This function is basically just a big switch statement.  Earlier versions
    450    used tables to look up the function to use, but
    451    - if the table contains both assembler and disassembler functions then
    452      the disassembler contains much of the assembler and vice-versa,
    453    - there's a lot of inlining possibilities as things grow,
    454    - using a switch statement avoids the function call overhead.
    455 
    456    This function could be moved into `parse_insn_normal', but keeping it
    457    separate makes clear the interface between `parse_insn_normal' and each of
    458    the handlers.  */
    459 
    460 const char *
    461 ip2k_cgen_parse_operand (CGEN_CPU_DESC cd,
    462 			   int opindex,
    463 			   const char ** strp,
    464 			   CGEN_FIELDS * fields)
    465 {
    466   const char * errmsg = NULL;
    467   /* Used by scalar operands that still need to be parsed.  */
    468   long junk ATTRIBUTE_UNUSED;
    469 
    470   switch (opindex)
    471     {
    472     case IP2K_OPERAND_ADDR16CJP :
    473       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, (unsigned long *) (& fields->f_addr16cjp));
    474       break;
    475     case IP2K_OPERAND_ADDR16H :
    476       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, (unsigned long *) (& fields->f_imm8));
    477       break;
    478     case IP2K_OPERAND_ADDR16L :
    479       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, (unsigned long *) (& fields->f_imm8));
    480       break;
    481     case IP2K_OPERAND_ADDR16P :
    482       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, (unsigned long *) (& fields->f_page3));
    483       break;
    484     case IP2K_OPERAND_BITNO :
    485       errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, (unsigned long *) (& fields->f_bitno));
    486       break;
    487     case IP2K_OPERAND_CBIT :
    488       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, (unsigned long *) (& junk));
    489       break;
    490     case IP2K_OPERAND_DCBIT :
    491       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, (unsigned long *) (& junk));
    492       break;
    493     case IP2K_OPERAND_FR :
    494       errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, (unsigned long *) (& fields->f_reg));
    495       break;
    496     case IP2K_OPERAND_LIT8 :
    497       errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, (long *) (& fields->f_imm8));
    498       break;
    499     case IP2K_OPERAND_PABITS :
    500       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, (unsigned long *) (& junk));
    501       break;
    502     case IP2K_OPERAND_RETI3 :
    503       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, (unsigned long *) (& fields->f_reti3));
    504       break;
    505     case IP2K_OPERAND_ZBIT :
    506       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, (unsigned long *) (& junk));
    507       break;
    508 
    509     default :
    510       /* xgettext:c-format */
    511       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
    512       abort ();
    513   }
    514 
    515   return errmsg;
    516 }
    517 
    518 cgen_parse_fn * const ip2k_cgen_parse_handlers[] =
    519 {
    520   parse_insn_normal,
    521 };
    522 
    523 void
    524 ip2k_cgen_init_asm (CGEN_CPU_DESC cd)
    525 {
    526   ip2k_cgen_init_opcode_table (cd);
    527   ip2k_cgen_init_ibld_table (cd);
    528   cd->parse_handlers = & ip2k_cgen_parse_handlers[0];
    529   cd->parse_operand = ip2k_cgen_parse_operand;
    530 #ifdef CGEN_ASM_INIT_HOOK
    531 CGEN_ASM_INIT_HOOK
    532 #endif
    533 }
    534 
    535 
    536 
    538 /* Regex construction routine.
    539 
    540    This translates an opcode syntax string into a regex string,
    541    by replacing any non-character syntax element (such as an
    542    opcode) with the pattern '.*'
    543 
    544    It then compiles the regex and stores it in the opcode, for
    545    later use by ip2k_cgen_assemble_insn
    546 
    547    Returns NULL for success, an error message for failure.  */
    548 
    549 char *
    550 ip2k_cgen_build_insn_regex (CGEN_INSN *insn)
    551 {
    552   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
    553   const char *mnem = CGEN_INSN_MNEMONIC (insn);
    554   char rxbuf[CGEN_MAX_RX_ELEMENTS];
    555   char *rx = rxbuf;
    556   const CGEN_SYNTAX_CHAR_TYPE *syn;
    557   int reg_err;
    558 
    559   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
    560 
    561   /* Mnemonics come first in the syntax string.  */
    562   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    563     return _("missing mnemonic in syntax string");
    564   ++syn;
    565 
    566   /* Generate a case sensitive regular expression that emulates case
    567      insensitive matching in the "C" locale.  We cannot generate a case
    568      insensitive regular expression because in Turkish locales, 'i' and 'I'
    569      are not equal modulo case conversion.  */
    570 
    571   /* Copy the literal mnemonic out of the insn.  */
    572   for (; *mnem; mnem++)
    573     {
    574       char c = *mnem;
    575 
    576       if (ISALPHA (c))
    577 	{
    578 	  *rx++ = '[';
    579 	  *rx++ = TOLOWER (c);
    580 	  *rx++ = TOUPPER (c);
    581 	  *rx++ = ']';
    582 	}
    583       else
    584 	*rx++ = c;
    585     }
    586 
    587   /* Copy any remaining literals from the syntax string into the rx.  */
    588   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
    589     {
    590       if (CGEN_SYNTAX_CHAR_P (* syn))
    591 	{
    592 	  char c = CGEN_SYNTAX_CHAR (* syn);
    593 
    594 	  switch (c)
    595 	    {
    596 	      /* Escape any regex metacharacters in the syntax.  */
    597 	    case '.': case '[': case '\\':
    598 	    case '*': case '^': case '$':
    599 
    600 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
    601 	    case '?': case '{': case '}':
    602 	    case '(': case ')': case '*':
    603 	    case '|': case '+': case ']':
    604 #endif
    605 	      *rx++ = '\\';
    606 	      *rx++ = c;
    607 	      break;
    608 
    609 	    default:
    610 	      if (ISALPHA (c))
    611 		{
    612 		  *rx++ = '[';
    613 		  *rx++ = TOLOWER (c);
    614 		  *rx++ = TOUPPER (c);
    615 		  *rx++ = ']';
    616 		}
    617 	      else
    618 		*rx++ = c;
    619 	      break;
    620 	    }
    621 	}
    622       else
    623 	{
    624 	  /* Replace non-syntax fields with globs.  */
    625 	  *rx++ = '.';
    626 	  *rx++ = '*';
    627 	}
    628     }
    629 
    630   /* Trailing whitespace ok.  */
    631   * rx++ = '[';
    632   * rx++ = ' ';
    633   * rx++ = '\t';
    634   * rx++ = ']';
    635   * rx++ = '*';
    636 
    637   /* But anchor it after that.  */
    638   * rx++ = '$';
    639   * rx = '\0';
    640 
    641   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
    642   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
    643 
    644   if (reg_err == 0)
    645     return NULL;
    646   else
    647     {
    648       static char msg[80];
    649 
    650       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
    651       regfree ((regex_t *) CGEN_INSN_RX (insn));
    652       free (CGEN_INSN_RX (insn));
    653       (CGEN_INSN_RX (insn)) = NULL;
    654       return msg;
    655     }
    656 }
    657 
    658 
    659 /* Default insn parser.
    661 
    662    The syntax string is scanned and operands are parsed and stored in FIELDS.
    663    Relocs are queued as we go via other callbacks.
    664 
    665    ??? Note that this is currently an all-or-nothing parser.  If we fail to
    666    parse the instruction, we return 0 and the caller will start over from
    667    the beginning.  Backtracking will be necessary in parsing subexpressions,
    668    but that can be handled there.  Not handling backtracking here may get
    669    expensive in the case of the m68k.  Deal with later.
    670 
    671    Returns NULL for success, an error message for failure.  */
    672 
    673 static const char *
    674 parse_insn_normal (CGEN_CPU_DESC cd,
    675 		   const CGEN_INSN *insn,
    676 		   const char **strp,
    677 		   CGEN_FIELDS *fields)
    678 {
    679   /* ??? Runtime added insns not handled yet.  */
    680   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
    681   const char *str = *strp;
    682   const char *errmsg;
    683   const char *p;
    684   const CGEN_SYNTAX_CHAR_TYPE * syn;
    685 #ifdef CGEN_MNEMONIC_OPERANDS
    686   /* FIXME: wip */
    687   int past_opcode_p;
    688 #endif
    689 
    690   /* For now we assume the mnemonic is first (there are no leading operands).
    691      We can parse it without needing to set up operand parsing.
    692      GAS's input scrubber will ensure mnemonics are lowercase, but we may
    693      not be called from GAS.  */
    694   p = CGEN_INSN_MNEMONIC (insn);
    695   while (*p && TOLOWER (*p) == TOLOWER (*str))
    696     ++p, ++str;
    697 
    698   if (* p)
    699     return _("unrecognized instruction");
    700 
    701 #ifndef CGEN_MNEMONIC_OPERANDS
    702   if (* str && ! ISSPACE (* str))
    703     return _("unrecognized instruction");
    704 #endif
    705 
    706   CGEN_INIT_PARSE (cd);
    707   cgen_init_parse_operand (cd);
    708 #ifdef CGEN_MNEMONIC_OPERANDS
    709   past_opcode_p = 0;
    710 #endif
    711 
    712   /* We don't check for (*str != '\0') here because we want to parse
    713      any trailing fake arguments in the syntax string.  */
    714   syn = CGEN_SYNTAX_STRING (syntax);
    715 
    716   /* Mnemonics come first for now, ensure valid string.  */
    717   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    718     abort ();
    719 
    720   ++syn;
    721 
    722   while (* syn != 0)
    723     {
    724       /* Non operand chars must match exactly.  */
    725       if (CGEN_SYNTAX_CHAR_P (* syn))
    726 	{
    727 	  /* FIXME: While we allow for non-GAS callers above, we assume the
    728 	     first char after the mnemonic part is a space.  */
    729 	  /* FIXME: We also take inappropriate advantage of the fact that
    730 	     GAS's input scrubber will remove extraneous blanks.  */
    731 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
    732 	    {
    733 #ifdef CGEN_MNEMONIC_OPERANDS
    734 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
    735 		past_opcode_p = 1;
    736 #endif
    737 	      ++ syn;
    738 	      ++ str;
    739 	    }
    740 	  else if (*str)
    741 	    {
    742 	      /* Syntax char didn't match.  Can't be this insn.  */
    743 	      static char msg [80];
    744 
    745 	      /* xgettext:c-format */
    746 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
    747 		       CGEN_SYNTAX_CHAR(*syn), *str);
    748 	      return msg;
    749 	    }
    750 	  else
    751 	    {
    752 	      /* Ran out of input.  */
    753 	      static char msg [80];
    754 
    755 	      /* xgettext:c-format */
    756 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
    757 		       CGEN_SYNTAX_CHAR(*syn));
    758 	      return msg;
    759 	    }
    760 	  continue;
    761 	}
    762 
    763 #ifdef CGEN_MNEMONIC_OPERANDS
    764       (void) past_opcode_p;
    765 #endif
    766       /* We have an operand of some sort.  */
    767       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
    768       if (errmsg)
    769 	return errmsg;
    770 
    771       /* Done with this operand, continue with next one.  */
    772       ++ syn;
    773     }
    774 
    775   /* If we're at the end of the syntax string, we're done.  */
    776   if (* syn == 0)
    777     {
    778       /* FIXME: For the moment we assume a valid `str' can only contain
    779 	 blanks now.  IE: We needn't try again with a longer version of
    780 	 the insn and it is assumed that longer versions of insns appear
    781 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
    782       while (ISSPACE (* str))
    783 	++ str;
    784 
    785       if (* str != '\0')
    786 	return _("junk at end of line"); /* FIXME: would like to include `str' */
    787 
    788       return NULL;
    789     }
    790 
    791   /* We couldn't parse it.  */
    792   return _("unrecognized instruction");
    793 }
    794 
    795 /* Main entry point.
    797    This routine is called for each instruction to be assembled.
    798    STR points to the insn to be assembled.
    799    We assume all necessary tables have been initialized.
    800    The assembled instruction, less any fixups, is stored in BUF.
    801    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
    802    still needs to be converted to target byte order, otherwise BUF is an array
    803    of bytes in target byte order.
    804    The result is a pointer to the insn's entry in the opcode table,
    805    or NULL if an error occured (an error message will have already been
    806    printed).
    807 
    808    Note that when processing (non-alias) macro-insns,
    809    this function recurses.
    810 
    811    ??? It's possible to make this cpu-independent.
    812    One would have to deal with a few minor things.
    813    At this point in time doing so would be more of a curiosity than useful
    814    [for example this file isn't _that_ big], but keeping the possibility in
    815    mind helps keep the design clean.  */
    816 
    817 const CGEN_INSN *
    818 ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd,
    819 			   const char *str,
    820 			   CGEN_FIELDS *fields,
    821 			   CGEN_INSN_BYTES_PTR buf,
    822 			   char **errmsg)
    823 {
    824   const char *start;
    825   CGEN_INSN_LIST *ilist;
    826   const char *parse_errmsg = NULL;
    827   const char *insert_errmsg = NULL;
    828   int recognized_mnemonic = 0;
    829 
    830   /* Skip leading white space.  */
    831   while (ISSPACE (* str))
    832     ++ str;
    833 
    834   /* The instructions are stored in hashed lists.
    835      Get the first in the list.  */
    836   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
    837 
    838   /* Keep looking until we find a match.  */
    839   start = str;
    840   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
    841     {
    842       const CGEN_INSN *insn = ilist->insn;
    843       recognized_mnemonic = 1;
    844 
    845 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
    846       /* Not usually needed as unsupported opcodes
    847 	 shouldn't be in the hash lists.  */
    848       /* Is this insn supported by the selected cpu?  */
    849       if (! ip2k_cgen_insn_supported (cd, insn))
    850 	continue;
    851 #endif
    852       /* If the RELAXED attribute is set, this is an insn that shouldn't be
    853 	 chosen immediately.  Instead, it is used during assembler/linker
    854 	 relaxation if possible.  */
    855       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
    856 	continue;
    857 
    858       str = start;
    859 
    860       /* Skip this insn if str doesn't look right lexically.  */
    861       if (CGEN_INSN_RX (insn) != NULL &&
    862 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
    863 	continue;
    864 
    865       /* Allow parse/insert handlers to obtain length of insn.  */
    866       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
    867 
    868       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
    869       if (parse_errmsg != NULL)
    870 	continue;
    871 
    872       /* ??? 0 is passed for `pc'.  */
    873       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
    874 						 (bfd_vma) 0);
    875       if (insert_errmsg != NULL)
    876         continue;
    877 
    878       /* It is up to the caller to actually output the insn and any
    879          queued relocs.  */
    880       return insn;
    881     }
    882 
    883   {
    884     static char errbuf[150];
    885     const char *tmp_errmsg;
    886 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
    887 #define be_verbose 1
    888 #else
    889 #define be_verbose 0
    890 #endif
    891 
    892     if (be_verbose)
    893       {
    894 	/* If requesting verbose error messages, use insert_errmsg.
    895 	   Failing that, use parse_errmsg.  */
    896 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
    897 		      parse_errmsg ? parse_errmsg :
    898 		      recognized_mnemonic ?
    899 		      _("unrecognized form of instruction") :
    900 		      _("unrecognized instruction"));
    901 
    902 	if (strlen (start) > 50)
    903 	  /* xgettext:c-format */
    904 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
    905 	else
    906 	  /* xgettext:c-format */
    907 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
    908       }
    909     else
    910       {
    911 	if (strlen (start) > 50)
    912 	  /* xgettext:c-format */
    913 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
    914 	else
    915 	  /* xgettext:c-format */
    916 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
    917       }
    918 
    919     *errmsg = errbuf;
    920     return NULL;
    921   }
    922 }
    923