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