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 "@prefix (at) -desc.h"
     35 #include "@prefix (at) -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 
     54 /* Regex construction routine.
     55 
     56    This translates an opcode syntax string into a regex string,
     57    by replacing any non-character syntax element (such as an
     58    opcode) with the pattern '.*'
     59 
     60    It then compiles the regex and stores it in the opcode, for
     61    later use by @arch@_cgen_assemble_insn
     62 
     63    Returns NULL for success, an error message for failure.  */
     64 
     65 char *
     66 @arch@_cgen_build_insn_regex (CGEN_INSN *insn)
     67 {
     68   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
     69   const char *mnem = CGEN_INSN_MNEMONIC (insn);
     70   char rxbuf[CGEN_MAX_RX_ELEMENTS];
     71   char *rx = rxbuf;
     72   const CGEN_SYNTAX_CHAR_TYPE *syn;
     73   int reg_err;
     74 
     75   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
     76 
     77   /* Mnemonics come first in the syntax string.  */
     78   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
     79     return _("missing mnemonic in syntax string");
     80   ++syn;
     81 
     82   /* Generate a case sensitive regular expression that emulates case
     83      insensitive matching in the "C" locale.  We cannot generate a case
     84      insensitive regular expression because in Turkish locales, 'i' and 'I'
     85      are not equal modulo case conversion.  */
     86 
     87   /* Copy the literal mnemonic out of the insn.  */
     88   for (; *mnem; mnem++)
     89     {
     90       char c = *mnem;
     91 
     92       if (ISALPHA (c))
     93 	{
     94 	  *rx++ = '[';
     95 	  *rx++ = TOLOWER (c);
     96 	  *rx++ = TOUPPER (c);
     97 	  *rx++ = ']';
     98 	}
     99       else
    100 	*rx++ = c;
    101     }
    102 
    103   /* Copy any remaining literals from the syntax string into the rx.  */
    104   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
    105     {
    106       if (CGEN_SYNTAX_CHAR_P (* syn))
    107 	{
    108 	  char c = CGEN_SYNTAX_CHAR (* syn);
    109 
    110 	  switch (c)
    111 	    {
    112 	      /* Escape any regex metacharacters in the syntax.  */
    113 	    case '.': case '[': case '\\':
    114 	    case '*': case '^': case '$':
    115 
    116 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
    117 	    case '?': case '{': case '}':
    118 	    case '(': case ')': case '*':
    119 	    case '|': case '+': case ']':
    120 #endif
    121 	      *rx++ = '\\';
    122 	      *rx++ = c;
    123 	      break;
    124 
    125 	    default:
    126 	      if (ISALPHA (c))
    127 		{
    128 		  *rx++ = '[';
    129 		  *rx++ = TOLOWER (c);
    130 		  *rx++ = TOUPPER (c);
    131 		  *rx++ = ']';
    132 		}
    133 	      else
    134 		*rx++ = c;
    135 	      break;
    136 	    }
    137 	}
    138       else
    139 	{
    140 	  /* Replace non-syntax fields with globs.  */
    141 	  *rx++ = '.';
    142 	  *rx++ = '*';
    143 	}
    144     }
    145 
    146   /* Trailing whitespace ok.  */
    147   * rx++ = '[';
    148   * rx++ = ' ';
    149   * rx++ = '\t';
    150   * rx++ = ']';
    151   * rx++ = '*';
    152 
    153   /* But anchor it after that.  */
    154   * rx++ = '$';
    155   * rx = '\0';
    156 
    157   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
    158   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
    159 
    160   if (reg_err == 0)
    161     return NULL;
    162   else
    163     {
    164       static char msg[80];
    165 
    166       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
    167       regfree ((regex_t *) CGEN_INSN_RX (insn));
    168       free (CGEN_INSN_RX (insn));
    169       (CGEN_INSN_RX (insn)) = NULL;
    170       return msg;
    171     }
    172 }
    173 
    174 
    175 /* Default insn parser.
    177 
    178    The syntax string is scanned and operands are parsed and stored in FIELDS.
    179    Relocs are queued as we go via other callbacks.
    180 
    181    ??? Note that this is currently an all-or-nothing parser.  If we fail to
    182    parse the instruction, we return 0 and the caller will start over from
    183    the beginning.  Backtracking will be necessary in parsing subexpressions,
    184    but that can be handled there.  Not handling backtracking here may get
    185    expensive in the case of the m68k.  Deal with later.
    186 
    187    Returns NULL for success, an error message for failure.  */
    188 
    189 static const char *
    190 parse_insn_normal (CGEN_CPU_DESC cd,
    191 		   const CGEN_INSN *insn,
    192 		   const char **strp,
    193 		   CGEN_FIELDS *fields)
    194 {
    195   /* ??? Runtime added insns not handled yet.  */
    196   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
    197   const char *str = *strp;
    198   const char *errmsg;
    199   const char *p;
    200   const CGEN_SYNTAX_CHAR_TYPE * syn;
    201 #ifdef CGEN_MNEMONIC_OPERANDS
    202   /* FIXME: wip */
    203   int past_opcode_p;
    204 #endif
    205 
    206   /* For now we assume the mnemonic is first (there are no leading operands).
    207      We can parse it without needing to set up operand parsing.
    208      GAS's input scrubber will ensure mnemonics are lowercase, but we may
    209      not be called from GAS.  */
    210   p = CGEN_INSN_MNEMONIC (insn);
    211   while (*p && TOLOWER (*p) == TOLOWER (*str))
    212     ++p, ++str;
    213 
    214   if (* p)
    215     return _("unrecognized instruction");
    216 
    217 #ifndef CGEN_MNEMONIC_OPERANDS
    218   if (* str && ! ISSPACE (* str))
    219     return _("unrecognized instruction");
    220 #endif
    221 
    222   CGEN_INIT_PARSE (cd);
    223   cgen_init_parse_operand (cd);
    224 #ifdef CGEN_MNEMONIC_OPERANDS
    225   past_opcode_p = 0;
    226 #endif
    227 
    228   /* We don't check for (*str != '\0') here because we want to parse
    229      any trailing fake arguments in the syntax string.  */
    230   syn = CGEN_SYNTAX_STRING (syntax);
    231 
    232   /* Mnemonics come first for now, ensure valid string.  */
    233   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    234     abort ();
    235 
    236   ++syn;
    237 
    238   while (* syn != 0)
    239     {
    240       /* Non operand chars must match exactly.  */
    241       if (CGEN_SYNTAX_CHAR_P (* syn))
    242 	{
    243 	  /* FIXME: While we allow for non-GAS callers above, we assume the
    244 	     first char after the mnemonic part is a space.  */
    245 	  /* FIXME: We also take inappropriate advantage of the fact that
    246 	     GAS's input scrubber will remove extraneous blanks.  */
    247 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
    248 	    {
    249 #ifdef CGEN_MNEMONIC_OPERANDS
    250 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
    251 		past_opcode_p = 1;
    252 #endif
    253 	      ++ syn;
    254 	      ++ str;
    255 	    }
    256 	  else if (*str)
    257 	    {
    258 	      /* Syntax char didn't match.  Can't be this insn.  */
    259 	      static char msg [80];
    260 
    261 	      /* xgettext:c-format */
    262 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
    263 		       CGEN_SYNTAX_CHAR(*syn), *str);
    264 	      return msg;
    265 	    }
    266 	  else
    267 	    {
    268 	      /* Ran out of input.  */
    269 	      static char msg [80];
    270 
    271 	      /* xgettext:c-format */
    272 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
    273 		       CGEN_SYNTAX_CHAR(*syn));
    274 	      return msg;
    275 	    }
    276 	  continue;
    277 	}
    278 
    279 #ifdef CGEN_MNEMONIC_OPERANDS
    280       (void) past_opcode_p;
    281 #endif
    282       /* We have an operand of some sort.  */
    283       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
    284       if (errmsg)
    285 	return errmsg;
    286 
    287       /* Done with this operand, continue with next one.  */
    288       ++ syn;
    289     }
    290 
    291   /* If we're at the end of the syntax string, we're done.  */
    292   if (* syn == 0)
    293     {
    294       /* FIXME: For the moment we assume a valid `str' can only contain
    295 	 blanks now.  IE: We needn't try again with a longer version of
    296 	 the insn and it is assumed that longer versions of insns appear
    297 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
    298       while (ISSPACE (* str))
    299 	++ str;
    300 
    301       if (* str != '\0')
    302 	return _("junk at end of line"); /* FIXME: would like to include `str' */
    303 
    304       return NULL;
    305     }
    306 
    307   /* We couldn't parse it.  */
    308   return _("unrecognized instruction");
    309 }
    310 
    311 /* Main entry point.
    313    This routine is called for each instruction to be assembled.
    314    STR points to the insn to be assembled.
    315    We assume all necessary tables have been initialized.
    316    The assembled instruction, less any fixups, is stored in BUF.
    317    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
    318    still needs to be converted to target byte order, otherwise BUF is an array
    319    of bytes in target byte order.
    320    The result is a pointer to the insn's entry in the opcode table,
    321    or NULL if an error occured (an error message will have already been
    322    printed).
    323 
    324    Note that when processing (non-alias) macro-insns,
    325    this function recurses.
    326 
    327    ??? It's possible to make this cpu-independent.
    328    One would have to deal with a few minor things.
    329    At this point in time doing so would be more of a curiosity than useful
    330    [for example this file isn't _that_ big], but keeping the possibility in
    331    mind helps keep the design clean.  */
    332 
    333 const CGEN_INSN *
    334 @arch@_cgen_assemble_insn (CGEN_CPU_DESC cd,
    335 			   const char *str,
    336 			   CGEN_FIELDS *fields,
    337 			   CGEN_INSN_BYTES_PTR buf,
    338 			   char **errmsg)
    339 {
    340   const char *start;
    341   CGEN_INSN_LIST *ilist;
    342   const char *parse_errmsg = NULL;
    343   const char *insert_errmsg = NULL;
    344   int recognized_mnemonic = 0;
    345 
    346   /* Skip leading white space.  */
    347   while (ISSPACE (* str))
    348     ++ str;
    349 
    350   /* The instructions are stored in hashed lists.
    351      Get the first in the list.  */
    352   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
    353 
    354   /* Keep looking until we find a match.  */
    355   start = str;
    356   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
    357     {
    358       const CGEN_INSN *insn = ilist->insn;
    359       recognized_mnemonic = 1;
    360 
    361 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
    362       /* Not usually needed as unsupported opcodes
    363 	 shouldn't be in the hash lists.  */
    364       /* Is this insn supported by the selected cpu?  */
    365       if (! @arch@_cgen_insn_supported (cd, insn))
    366 	continue;
    367 #endif
    368       /* If the RELAXED attribute is set, this is an insn that shouldn't be
    369 	 chosen immediately.  Instead, it is used during assembler/linker
    370 	 relaxation if possible.  */
    371       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
    372 	continue;
    373 
    374       str = start;
    375 
    376       /* Skip this insn if str doesn't look right lexically.  */
    377       if (CGEN_INSN_RX (insn) != NULL &&
    378 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
    379 	continue;
    380 
    381       /* Allow parse/insert handlers to obtain length of insn.  */
    382       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
    383 
    384       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
    385       if (parse_errmsg != NULL)
    386 	continue;
    387 
    388       /* ??? 0 is passed for `pc'.  */
    389       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
    390 						 (bfd_vma) 0);
    391       if (insert_errmsg != NULL)
    392         continue;
    393 
    394       /* It is up to the caller to actually output the insn and any
    395          queued relocs.  */
    396       return insn;
    397     }
    398 
    399   {
    400     static char errbuf[150];
    401     const char *tmp_errmsg;
    402 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
    403 #define be_verbose 1
    404 #else
    405 #define be_verbose 0
    406 #endif
    407 
    408     if (be_verbose)
    409       {
    410 	/* If requesting verbose error messages, use insert_errmsg.
    411 	   Failing that, use parse_errmsg.  */
    412 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
    413 		      parse_errmsg ? parse_errmsg :
    414 		      recognized_mnemonic ?
    415 		      _("unrecognized form of instruction") :
    416 		      _("unrecognized instruction"));
    417 
    418 	if (strlen (start) > 50)
    419 	  /* xgettext:c-format */
    420 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
    421 	else
    422 	  /* xgettext:c-format */
    423 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
    424       }
    425     else
    426       {
    427 	if (strlen (start) > 50)
    428 	  /* xgettext:c-format */
    429 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
    430 	else
    431 	  /* xgettext:c-format */
    432 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
    433       }
    434 
    435     *errmsg = errbuf;
    436     return NULL;
    437   }
    438 }
    439