Home | History | Annotate | Line # | Download | only in opcodes
      1   1.4  christos /* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */
      2   1.1      matt /* Assembler interface for targets using CGEN. -*- C -*-
      3   1.1      matt    CGEN: Cpu tools GENerator
      4   1.1      matt 
      5   1.1      matt    THIS FILE IS MACHINE GENERATED WITH CGEN.
      6   1.1      matt    - the resultant file is machine generated, cgen-asm.in isn't
      7   1.1      matt 
      8  1.10  christos    Copyright (C) 1996-2026 Free Software Foundation, Inc.
      9   1.1      matt 
     10   1.1      matt    This file is part of libopcodes.
     11   1.1      matt 
     12   1.1      matt    This library is free software; you can redistribute it and/or modify
     13   1.1      matt    it under the terms of the GNU General Public License as published by
     14   1.1      matt    the Free Software Foundation; either version 3, or (at your option)
     15   1.1      matt    any later version.
     16   1.1      matt 
     17   1.1      matt    It is distributed in the hope that it will be useful, but WITHOUT
     18   1.1      matt    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     19   1.1      matt    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     20   1.1      matt    License for more details.
     21   1.1      matt 
     22   1.1      matt    You should have received a copy of the GNU General Public License
     23   1.1      matt    along with this program; if not, write to the Free Software Foundation, Inc.,
     24   1.1      matt    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     25   1.1      matt 
     26   1.1      matt 
     27   1.1      matt /* ??? Eventually more and more of this stuff can go to cpu-independent files.
     28   1.1      matt    Keep that in mind.  */
     29   1.1      matt 
     30   1.1      matt #include "sysdep.h"
     31   1.1      matt #include <stdio.h>
     32   1.1      matt #include "ansidecl.h"
     33   1.1      matt #include "bfd.h"
     34   1.1      matt #include "symcat.h"
     35   1.1      matt #include "or1k-desc.h"
     36   1.1      matt #include "or1k-opc.h"
     37   1.1      matt #include "opintl.h"
     38   1.1      matt #include "xregex.h"
     39   1.1      matt #include "libiberty.h"
     40   1.1      matt #include "safe-ctype.h"
     41   1.1      matt 
     42   1.1      matt #undef  min
     43   1.1      matt #define min(a,b) ((a) < (b) ? (a) : (b))
     44   1.1      matt #undef  max
     45   1.1      matt #define max(a,b) ((a) > (b) ? (a) : (b))
     46   1.1      matt 
     47   1.1      matt static const char * parse_insn_normal
     48   1.1      matt   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
     49   1.1      matt 
     50   1.1      matt /* -- assembler routines inserted here.  */
     52   1.1      matt 
     53   1.1      matt /* -- asm.c */
     54   1.1      matt 
     55   1.6  christos static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
     56   1.6  christos static const char * INVALID_STORE_RELOC = N_("relocation invalid for store");
     57   1.1      matt static const char * INVALID_RELOC_TYPE = N_("internal relocation type invalid");
     58   1.1      matt 
     59   1.1      matt #define CGEN_VERBOSE_ASSEMBLER_ERRORS
     60   1.1      matt 
     61   1.1      matt static const char *
     62   1.1      matt parse_disp26 (CGEN_CPU_DESC cd,
     63   1.1      matt 	      const char ** strp,
     64   1.6  christos 	      int opindex,
     65   1.1      matt 	      int opinfo ATTRIBUTE_UNUSED,
     66   1.1      matt 	      enum cgen_parse_operand_result * resultp,
     67   1.1      matt 	      bfd_vma * valuep)
     68   1.6  christos {
     69   1.1      matt   const char *str = *strp;
     70   1.6  christos   const char *errmsg = NULL;
     71   1.1      matt   bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_REL_26;
     72   1.6  christos 
     73   1.6  christos   if (strncasecmp (str, "plta(", 5) == 0)
     74   1.6  christos     {
     75   1.6  christos       *strp = str + 5;
     76   1.6  christos       reloc = BFD_RELOC_OR1K_PLTA26;
     77   1.6  christos     }
     78   1.1      matt   else if (strncasecmp (str, "plt(", 4) == 0)
     79   1.6  christos     {
     80   1.6  christos       *strp = str + 4;
     81   1.1      matt       reloc = BFD_RELOC_OR1K_PLT26;
     82   1.1      matt     }
     83   1.6  christos 
     84   1.1      matt   errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
     85   1.6  christos 
     86   1.1      matt   if (reloc != BFD_RELOC_OR1K_REL_26)
     87   1.1      matt     {
     88   1.1      matt       if (**strp != ')')
     89   1.6  christos 	errmsg = MISSING_CLOSING_PARENTHESIS;
     90   1.6  christos       else
     91   1.1      matt 	++*strp;
     92   1.1      matt     }
     93   1.6  christos 
     94   1.6  christos   return errmsg;
     95   1.1      matt }
     96   1.6  christos 
     97   1.6  christos static const char *
     98   1.6  christos parse_disp21 (CGEN_CPU_DESC cd,
     99   1.6  christos 	      const char ** strp,
    100   1.6  christos 	      int opindex,
    101   1.6  christos 	      int opinfo ATTRIBUTE_UNUSED,
    102   1.6  christos 	      enum cgen_parse_operand_result * resultp,
    103   1.6  christos 	      bfd_vma * valuep)
    104   1.6  christos {
    105   1.6  christos   const char *str = *strp;
    106   1.6  christos   const char *errmsg = NULL;
    107   1.1      matt   bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_PCREL_PG21;
    108   1.6  christos 
    109   1.6  christos   if (strncasecmp (str, "got(", 4) == 0)
    110   1.6  christos     {
    111   1.6  christos       *strp = str + 4;
    112   1.1      matt       reloc = BFD_RELOC_OR1K_GOT_PG21;
    113   1.6  christos     }
    114   1.1      matt   else if (strncasecmp (str, "tlsgd(", 6) == 0)
    115   1.6  christos     {
    116   1.6  christos       *strp = str + 6;
    117   1.1      matt       reloc = BFD_RELOC_OR1K_TLS_GD_PG21;
    118   1.6  christos     }
    119   1.1      matt   else if (strncasecmp (str, "tlsldm(", 7) == 0)
    120   1.6  christos     {
    121   1.6  christos       *strp = str + 7;
    122   1.1      matt       reloc = BFD_RELOC_OR1K_TLS_LDM_PG21;
    123   1.6  christos     }
    124   1.1      matt   else if (strncasecmp (str, "gottp(", 6) == 0)
    125   1.6  christos     {
    126   1.6  christos       *strp = str + 6;
    127   1.1      matt       reloc = BFD_RELOC_OR1K_TLS_IE_PG21;
    128   1.1      matt     }
    129   1.6  christos 
    130   1.1      matt   errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
    131   1.6  christos 
    132   1.6  christos   if (reloc != BFD_RELOC_OR1K_PCREL_PG21)
    133   1.1      matt     {
    134   1.6  christos       if (**strp != ')')
    135   1.6  christos 	errmsg = MISSING_CLOSING_PARENTHESIS;
    136   1.6  christos       else
    137   1.1      matt 	++*strp;
    138   1.1      matt     }
    139   1.6  christos 
    140   1.6  christos   return errmsg;
    141   1.6  christos }
    142   1.6  christos 
    143   1.6  christos enum or1k_rclass
    144   1.6  christos {
    145   1.6  christos   RCLASS_DIRECT   = 0,
    146   1.6  christos   RCLASS_GOT      = 1,
    147   1.6  christos   RCLASS_GOTPC    = 2,
    148   1.6  christos   RCLASS_GOTOFF   = 3,
    149   1.6  christos   RCLASS_TLSGD    = 4,
    150   1.6  christos   RCLASS_TLSLDM   = 5,
    151   1.6  christos   RCLASS_DTPOFF   = 6,
    152   1.6  christos   RCLASS_GOTTPOFF = 7,
    153   1.6  christos   RCLASS_TPOFF    = 8,
    154   1.1      matt };
    155   1.6  christos 
    156   1.6  christos enum or1k_rtype
    157   1.6  christos {
    158   1.6  christos   RTYPE_LO = 0,
    159   1.6  christos   RTYPE_SLO = 1,
    160   1.6  christos   RTYPE_PO = 2,
    161   1.6  christos   RTYPE_SPO = 3,
    162   1.6  christos   RTYPE_HI = 4,
    163   1.6  christos   RTYPE_AHI = 5,
    164   1.1      matt };
    165   1.6  christos 
    166   1.6  christos #define RCLASS_SHIFT 3
    167   1.1      matt #define RTYPE_MASK   7
    168   1.6  christos 
    169   1.6  christos static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
    170   1.6  christos   { BFD_RELOC_LO16,
    171   1.6  christos     BFD_RELOC_OR1K_SLO16,
    172   1.6  christos     BFD_RELOC_OR1K_LO13,
    173   1.6  christos     BFD_RELOC_OR1K_SLO13,
    174   1.6  christos     BFD_RELOC_HI16,
    175   1.6  christos     BFD_RELOC_HI16_S, },
    176   1.6  christos   { BFD_RELOC_OR1K_GOT16,
    177   1.6  christos     BFD_RELOC_UNUSED,
    178   1.6  christos     BFD_RELOC_OR1K_GOT_LO13,
    179   1.6  christos     BFD_RELOC_UNUSED,
    180   1.7  christos     BFD_RELOC_UNUSED,
    181   1.6  christos     BFD_RELOC_OR1K_GOT_AHI16 },
    182   1.6  christos   { BFD_RELOC_OR1K_GOTPC_LO16,
    183   1.6  christos     BFD_RELOC_UNUSED,
    184   1.6  christos     BFD_RELOC_UNUSED,
    185   1.6  christos     BFD_RELOC_UNUSED,
    186   1.6  christos     BFD_RELOC_OR1K_GOTPC_HI16,
    187   1.6  christos     BFD_RELOC_UNUSED },
    188   1.6  christos   { BFD_RELOC_LO16_GOTOFF,
    189   1.6  christos     BFD_RELOC_OR1K_GOTOFF_SLO16,
    190   1.6  christos     BFD_RELOC_UNUSED,
    191   1.6  christos     BFD_RELOC_UNUSED,
    192   1.6  christos     BFD_RELOC_HI16_GOTOFF,
    193   1.6  christos     BFD_RELOC_HI16_S_GOTOFF },
    194   1.6  christos   { BFD_RELOC_OR1K_TLS_GD_LO16,
    195   1.6  christos     BFD_RELOC_UNUSED,
    196   1.6  christos     BFD_RELOC_OR1K_TLS_GD_LO13,
    197   1.6  christos     BFD_RELOC_UNUSED,
    198   1.6  christos     BFD_RELOC_OR1K_TLS_GD_HI16,
    199   1.6  christos     BFD_RELOC_UNUSED },
    200   1.6  christos   { BFD_RELOC_OR1K_TLS_LDM_LO16,
    201   1.6  christos     BFD_RELOC_UNUSED,
    202   1.6  christos     BFD_RELOC_OR1K_TLS_LDM_LO13,
    203   1.6  christos     BFD_RELOC_UNUSED,
    204   1.6  christos     BFD_RELOC_OR1K_TLS_LDM_HI16,
    205   1.6  christos     BFD_RELOC_UNUSED },
    206   1.6  christos   { BFD_RELOC_OR1K_TLS_LDO_LO16,
    207   1.6  christos     BFD_RELOC_UNUSED,
    208   1.6  christos     BFD_RELOC_UNUSED,
    209   1.6  christos     BFD_RELOC_UNUSED,
    210   1.6  christos     BFD_RELOC_OR1K_TLS_LDO_HI16,
    211   1.6  christos     BFD_RELOC_UNUSED },
    212   1.6  christos   { BFD_RELOC_OR1K_TLS_IE_LO16,
    213   1.6  christos     BFD_RELOC_UNUSED,
    214   1.6  christos     BFD_RELOC_OR1K_TLS_IE_LO13,
    215   1.6  christos     BFD_RELOC_UNUSED,
    216   1.6  christos     BFD_RELOC_OR1K_TLS_IE_HI16,
    217   1.6  christos     BFD_RELOC_OR1K_TLS_IE_AHI16 },
    218   1.6  christos   { BFD_RELOC_OR1K_TLS_LE_LO16,
    219   1.6  christos     BFD_RELOC_OR1K_TLS_LE_SLO16,
    220   1.6  christos     BFD_RELOC_UNUSED,
    221   1.6  christos     BFD_RELOC_UNUSED,
    222   1.6  christos     BFD_RELOC_OR1K_TLS_LE_HI16,
    223   1.6  christos     BFD_RELOC_OR1K_TLS_LE_AHI16 },
    224   1.1      matt };
    225   1.6  christos 
    226   1.6  christos static int
    227   1.6  christos parse_reloc (const char **strp)
    228   1.6  christos {
    229   1.6  christos     const char *str = *strp;
    230   1.6  christos     enum or1k_rclass cls = RCLASS_DIRECT;
    231   1.1      matt     enum or1k_rtype typ;
    232   1.6  christos 
    233   1.6  christos     if (strncasecmp (str, "got(", 4) == 0)
    234   1.6  christos       {
    235   1.6  christos 	*strp = str + 4;
    236   1.6  christos 	return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_LO;
    237   1.6  christos       }
    238   1.6  christos     if (strncasecmp (str, "gotpo(", 6) == 0)
    239   1.6  christos       {
    240   1.6  christos 	*strp = str + 6;
    241   1.6  christos 	return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_PO;
    242   1.6  christos       }
    243   1.6  christos     if (strncasecmp (str, "gottppo(", 8) == 0)
    244   1.6  christos       {
    245   1.6  christos 	*strp = str + 8;
    246   1.6  christos 	return (RCLASS_GOTTPOFF << RCLASS_SHIFT) | RTYPE_PO;
    247   1.1      matt       }
    248   1.6  christos 
    249   1.6  christos     if (strncasecmp (str, "gotpc", 5) == 0)
    250   1.6  christos       {
    251   1.6  christos 	str += 5;
    252   1.6  christos 	cls = RCLASS_GOTPC;
    253   1.6  christos       }
    254   1.6  christos     else if (strncasecmp (str, "gotoff", 6) == 0)
    255   1.6  christos       {
    256   1.6  christos 	str += 6;
    257   1.6  christos 	cls = RCLASS_GOTOFF;
    258   1.6  christos       }
    259   1.6  christos     else if (strncasecmp (str, "tlsgd", 5) == 0)
    260   1.6  christos       {
    261   1.6  christos 	str += 5;
    262   1.6  christos 	cls = RCLASS_TLSGD;
    263   1.6  christos       }
    264   1.6  christos     else if (strncasecmp (str, "tlsldm", 6) == 0)
    265   1.6  christos       {
    266   1.6  christos 	str += 6;
    267   1.6  christos 	cls = RCLASS_TLSLDM;
    268   1.6  christos       }
    269   1.6  christos     else if (strncasecmp (str, "dtpoff", 6) == 0)
    270   1.6  christos       {
    271   1.6  christos 	str += 6;
    272   1.6  christos 	cls = RCLASS_DTPOFF;
    273   1.6  christos       }
    274   1.6  christos     else if (strncasecmp (str, "gottpoff", 8) == 0)
    275   1.6  christos       {
    276   1.6  christos 	str += 8;
    277   1.6  christos 	cls = RCLASS_GOTTPOFF;
    278   1.6  christos       }
    279   1.6  christos     else if (strncasecmp (str, "tpoff", 5) == 0)
    280   1.6  christos       {
    281   1.6  christos 	str += 5;
    282   1.6  christos 	cls = RCLASS_TPOFF;
    283   1.7  christos       }
    284   1.7  christos     else if (strncasecmp (str, "got", 3) == 0)
    285   1.7  christos       {
    286   1.7  christos 	str += 3;
    287   1.7  christos 	cls = RCLASS_GOT;
    288   1.1      matt       }
    289   1.6  christos 
    290   1.6  christos     if (strncasecmp (str, "hi(", 3) == 0)
    291   1.6  christos       {
    292   1.6  christos 	str += 3;
    293   1.6  christos 	typ = RTYPE_HI;
    294   1.6  christos       }
    295   1.6  christos     else if (strncasecmp (str, "lo(", 3) == 0)
    296   1.6  christos       {
    297   1.6  christos 	str += 3;
    298   1.6  christos 	typ = RTYPE_LO;
    299   1.6  christos       }
    300   1.6  christos     else if (strncasecmp (str, "ha(", 3) == 0)
    301   1.6  christos       {
    302   1.6  christos 	str += 3;
    303   1.6  christos 	typ = RTYPE_AHI;
    304   1.6  christos       }
    305   1.6  christos     else if (strncasecmp (str, "po(", 3) == 0 && cls != RCLASS_GOTTPOFF)
    306   1.6  christos       {
    307   1.6  christos 	str += 3;
    308   1.6  christos 	typ = RTYPE_PO;
    309   1.6  christos       }
    310   1.6  christos     else
    311   1.1      matt       return -1;
    312   1.6  christos 
    313   1.6  christos     *strp = str;
    314   1.6  christos     return (cls << RCLASS_SHIFT) | typ;
    315   1.1      matt }
    316   1.6  christos 
    317   1.6  christos static const char *
    318   1.6  christos parse_imm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
    319   1.6  christos 	     long *valuep, int splitp)
    320   1.6  christos {
    321   1.6  christos   const char *errmsg;
    322   1.6  christos   enum cgen_parse_operand_result result_type;
    323   1.6  christos   bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
    324   1.6  christos   enum or1k_rtype reloc_type;
    325   1.6  christos   int reloc_code;
    326   1.1      matt   bfd_vma ret;
    327   1.6  christos 
    328   1.6  christos   if (**strp == '#')
    329   1.1      matt     ++*strp;
    330   1.6  christos 
    331   1.6  christos   reloc_code = parse_reloc (strp);
    332   1.6  christos   reloc_type = reloc_code & RTYPE_MASK;
    333   1.6  christos   if (reloc_code >= 0)
    334   1.6  christos     {
    335   1.6  christos       enum or1k_rclass reloc_class = reloc_code >> RCLASS_SHIFT;
    336   1.6  christos       if (splitp)
    337   1.6  christos 	{
    338   1.6  christos 	  if ((reloc_type == RTYPE_LO || reloc_type == RTYPE_PO)
    339   1.6  christos 	      && reloc_class != RCLASS_GOT)
    340   1.6  christos 	    /* If split we or up the type to RTYPE_SLO or RTYPE_SPO.  */
    341   1.6  christos 	    reloc_type |= 1;
    342   1.6  christos 	  else
    343   1.6  christos 	    return INVALID_STORE_RELOC;
    344   1.6  christos 	}
    345   1.1      matt       reloc = or1k_imm16_relocs[reloc_class][reloc_type];
    346   1.6  christos     }
    347   1.6  christos 
    348   1.1      matt   if (reloc != BFD_RELOC_UNUSED)
    349   1.1      matt     {
    350   1.1      matt       bfd_vma value;
    351   1.6  christos 
    352   1.1      matt       errmsg = cgen_parse_address (cd, strp, opindex, reloc,
    353   1.1      matt 				   &result_type, &value);
    354   1.6  christos       if (**strp != ')')
    355   1.1      matt 	errmsg = MISSING_CLOSING_PARENTHESIS;
    356   1.1      matt       ++*strp;
    357   1.6  christos 
    358   1.1      matt       ret = value;
    359   1.6  christos 
    360   1.6  christos       if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
    361   1.6  christos 	switch (reloc_type)
    362   1.6  christos 	  {
    363   1.6  christos 	  case RTYPE_AHI:
    364   1.6  christos 	    ret += 0x8000;
    365   1.6  christos 	    /* FALLTHRU */
    366   1.6  christos 	  case RTYPE_HI:
    367   1.6  christos 	    ret >>= 16;
    368   1.6  christos 	    /* FALLTHRU */
    369   1.6  christos 	  case RTYPE_LO:
    370   1.6  christos 	  case RTYPE_SLO:
    371   1.6  christos 	    ret &= 0xffff;
    372   1.6  christos 	    ret = (ret ^ 0x8000) - 0x8000;
    373   1.6  christos 	    break;
    374   1.6  christos 	  case RTYPE_PO:
    375   1.6  christos 	  case RTYPE_SPO:
    376   1.6  christos 	    ret &= 0x1fff;
    377   1.6  christos 	    break;
    378   1.6  christos 	  default:
    379   1.6  christos 	    errmsg = INVALID_RELOC_TYPE;
    380   1.1      matt 	  }
    381   1.1      matt     }
    382   1.1      matt   else
    383   1.1      matt     {
    384   1.1      matt       long value;
    385   1.1      matt       errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
    386   1.1      matt       ret = value;
    387   1.1      matt     }
    388   1.1      matt 
    389   1.1      matt   if (errmsg == NULL)
    390   1.1      matt     *valuep = ret;
    391   1.1      matt 
    392   1.1      matt   return errmsg;
    393   1.1      matt }
    394   1.1      matt 
    395   1.6  christos static const char *
    396   1.6  christos parse_simm16 (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
    397   1.6  christos {
    398   1.6  christos   return parse_imm16(cd, strp, opindex, (long *) valuep, 0);
    399   1.6  christos }
    400   1.6  christos 
    401   1.6  christos static const char *
    402   1.6  christos parse_simm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
    403   1.1      matt 		    long *valuep)
    404   1.6  christos {
    405   1.6  christos   return parse_imm16(cd, strp, opindex, (long *) valuep, 1);
    406   1.6  christos }
    407   1.6  christos 
    408   1.6  christos static const char *
    409   1.6  christos parse_uimm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
    410   1.6  christos 	      unsigned long *valuep)
    411   1.6  christos {
    412   1.6  christos   const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 0);
    413   1.6  christos   if (errmsg == NULL)
    414   1.6  christos     *valuep &= 0xffff;
    415   1.6  christos   return errmsg;
    416   1.1      matt }
    417   1.6  christos 
    418   1.6  christos static const char *
    419   1.6  christos parse_uimm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
    420   1.6  christos 		    unsigned long *valuep)
    421   1.6  christos {
    422   1.1      matt   const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 1);
    423   1.1      matt   if (errmsg == NULL)
    424   1.1      matt     *valuep &= 0xffff;
    425   1.1      matt   return errmsg;
    426   1.1      matt }
    427   1.6  christos 
    428   1.6  christos /* Parse register pairs with syntax rA,rB to a flag + rA value.  */
    429   1.6  christos 
    430   1.6  christos static const char *
    431   1.6  christos parse_regpair (CGEN_CPU_DESC cd, const char **strp,
    432   1.6  christos 	       int opindex ATTRIBUTE_UNUSED, unsigned long *valuep)
    433   1.6  christos {
    434   1.6  christos   long reg1_index;
    435   1.6  christos   long reg2_index;
    436   1.6  christos   const char *errmsg;
    437   1.6  christos 
    438   1.6  christos   /* The first part should just be a register.  */
    439   1.6  christos   errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
    440   1.6  christos 			       &reg1_index);
    441   1.6  christos 
    442   1.6  christos   /* If that worked skip the comma separator.  */
    443   1.6  christos   if (errmsg == NULL)
    444   1.6  christos     {
    445   1.6  christos       if (**strp == ',')
    446   1.6  christos 	++*strp;
    447   1.6  christos       else
    448   1.6  christos 	errmsg = "Unexpected character, expected ','";
    449   1.6  christos     }
    450   1.6  christos 
    451   1.6  christos   /* If that worked the next part is just another register.  */
    452   1.6  christos   if (errmsg == NULL)
    453   1.6  christos     errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
    454   1.6  christos 				 &reg2_index);
    455   1.6  christos 
    456   1.6  christos   /* Validate the register pair is valid and create the output value.  */
    457   1.6  christos   if (errmsg == NULL)
    458   1.6  christos     {
    459   1.6  christos       int regoffset = reg2_index - reg1_index;
    460   1.6  christos 
    461   1.6  christos       if (regoffset == 1 || regoffset == 2)
    462   1.6  christos 	{
    463   1.6  christos 	  unsigned short offsetmask;
    464   1.6  christos 	  unsigned short value;
    465   1.6  christos 
    466   1.6  christos 	  offsetmask = ((regoffset == 2 ? 1 : 0) << 5);
    467   1.6  christos 	  value = offsetmask | reg1_index;
    468   1.6  christos 
    469   1.6  christos 	  *valuep = value;
    470   1.6  christos 	}
    471   1.6  christos       else
    472   1.6  christos 	errmsg = "Invalid register pair, offset not 1 or 2.";
    473   1.6  christos     }
    474   1.6  christos 
    475   1.6  christos   return errmsg;
    476   1.6  christos }
    477   1.1      matt 
    478   1.1      matt /* -- */
    479   1.1      matt 
    480   1.1      matt const char * or1k_cgen_parse_operand
    481   1.1      matt   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
    482   1.1      matt 
    483   1.1      matt /* Main entry point for operand parsing.
    484   1.1      matt 
    485   1.1      matt    This function is basically just a big switch statement.  Earlier versions
    486   1.1      matt    used tables to look up the function to use, but
    487   1.1      matt    - if the table contains both assembler and disassembler functions then
    488   1.1      matt      the disassembler contains much of the assembler and vice-versa,
    489   1.1      matt    - there's a lot of inlining possibilities as things grow,
    490   1.1      matt    - using a switch statement avoids the function call overhead.
    491   1.1      matt 
    492   1.1      matt    This function could be moved into `parse_insn_normal', but keeping it
    493   1.1      matt    separate makes clear the interface between `parse_insn_normal' and each of
    494   1.1      matt    the handlers.  */
    495   1.1      matt 
    496   1.1      matt const char *
    497   1.1      matt or1k_cgen_parse_operand (CGEN_CPU_DESC cd,
    498   1.1      matt 			   int opindex,
    499   1.1      matt 			   const char ** strp,
    500   1.1      matt 			   CGEN_FIELDS * fields)
    501   1.1      matt {
    502   1.1      matt   const char * errmsg = NULL;
    503   1.1      matt   /* Used by scalar operands that still need to be parsed.  */
    504   1.1      matt   long junk ATTRIBUTE_UNUSED;
    505   1.1      matt 
    506   1.1      matt   switch (opindex)
    507   1.6  christos     {
    508   1.6  christos     case OR1K_OPERAND_DISP21 :
    509   1.6  christos       {
    510   1.6  christos         bfd_vma value = 0;
    511   1.6  christos         errmsg = parse_disp21 (cd, strp, OR1K_OPERAND_DISP21, 0, NULL,  & value);
    512   1.6  christos         fields->f_disp21 = value;
    513   1.6  christos       }
    514   1.1      matt       break;
    515   1.1      matt     case OR1K_OPERAND_DISP26 :
    516   1.1      matt       {
    517   1.1      matt         bfd_vma value = 0;
    518   1.1      matt         errmsg = parse_disp26 (cd, strp, OR1K_OPERAND_DISP26, 0, NULL,  & value);
    519   1.1      matt         fields->f_disp26 = value;
    520   1.1      matt       }
    521   1.1      matt       break;
    522   1.1      matt     case OR1K_OPERAND_RA :
    523   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r2);
    524   1.6  christos       break;
    525   1.6  christos     case OR1K_OPERAND_RAD32F :
    526   1.6  christos       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RAD32F, (unsigned long *) (& fields->f_rad32));
    527   1.6  christos       break;
    528   1.6  christos     case OR1K_OPERAND_RADI :
    529   1.1      matt       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RADI, (unsigned long *) (& fields->f_rad32));
    530   1.1      matt       break;
    531   1.1      matt     case OR1K_OPERAND_RASF :
    532   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r2);
    533   1.1      matt       break;
    534   1.1      matt     case OR1K_OPERAND_RB :
    535   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r3);
    536   1.6  christos       break;
    537   1.6  christos     case OR1K_OPERAND_RBD32F :
    538   1.6  christos       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBD32F, (unsigned long *) (& fields->f_rbd32));
    539   1.6  christos       break;
    540   1.6  christos     case OR1K_OPERAND_RBDI :
    541   1.1      matt       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBDI, (unsigned long *) (& fields->f_rbd32));
    542   1.1      matt       break;
    543   1.1      matt     case OR1K_OPERAND_RBSF :
    544   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r3);
    545   1.1      matt       break;
    546   1.1      matt     case OR1K_OPERAND_RD :
    547   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r1);
    548   1.6  christos       break;
    549   1.6  christos     case OR1K_OPERAND_RDD32F :
    550   1.6  christos       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDD32F, (unsigned long *) (& fields->f_rdd32));
    551   1.6  christos       break;
    552   1.6  christos     case OR1K_OPERAND_RDDI :
    553   1.6  christos       errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDDI, (unsigned long *) (& fields->f_rdd32));
    554   1.1      matt       break;
    555   1.1      matt     case OR1K_OPERAND_RDSF :
    556   1.1      matt       errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r1);
    557   1.1      matt       break;
    558   1.1      matt     case OR1K_OPERAND_SIMM16 :
    559   1.1      matt       errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16, (long *) (& fields->f_simm16));
    560   1.1      matt       break;
    561   1.6  christos     case OR1K_OPERAND_SIMM16_SPLIT :
    562   1.1      matt       errmsg = parse_simm16_split (cd, strp, OR1K_OPERAND_SIMM16_SPLIT, (long *) (& fields->f_simm16_split));
    563   1.1      matt       break;
    564   1.1      matt     case OR1K_OPERAND_UIMM16 :
    565   1.1      matt       errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
    566   1.1      matt       break;
    567   1.6  christos     case OR1K_OPERAND_UIMM16_SPLIT :
    568   1.1      matt       errmsg = parse_uimm16_split (cd, strp, OR1K_OPERAND_UIMM16_SPLIT, (unsigned long *) (& fields->f_uimm16_split));
    569   1.1      matt       break;
    570   1.1      matt     case OR1K_OPERAND_UIMM6 :
    571   1.1      matt       errmsg = cgen_parse_unsigned_integer (cd, strp, OR1K_OPERAND_UIMM6, (unsigned long *) (& fields->f_uimm6));
    572   1.1      matt       break;
    573   1.1      matt 
    574   1.1      matt     default :
    575   1.5  christos       /* xgettext:c-format */
    576   1.5  christos       opcodes_error_handler
    577   1.5  christos 	(_("internal error: unrecognized field %d while parsing"),
    578   1.1      matt 	 opindex);
    579   1.1      matt       abort ();
    580   1.1      matt   }
    581   1.1      matt 
    582   1.1      matt   return errmsg;
    583   1.1      matt }
    584   1.2  christos 
    585   1.1      matt cgen_parse_fn * const or1k_cgen_parse_handlers[] =
    586   1.1      matt {
    587   1.1      matt   parse_insn_normal,
    588   1.1      matt };
    589   1.1      matt 
    590   1.1      matt void
    591   1.1      matt or1k_cgen_init_asm (CGEN_CPU_DESC cd)
    592   1.1      matt {
    593   1.1      matt   or1k_cgen_init_opcode_table (cd);
    594   1.1      matt   or1k_cgen_init_ibld_table (cd);
    595   1.1      matt   cd->parse_handlers = & or1k_cgen_parse_handlers[0];
    596   1.1      matt   cd->parse_operand = or1k_cgen_parse_operand;
    597   1.1      matt #ifdef CGEN_ASM_INIT_HOOK
    598   1.1      matt CGEN_ASM_INIT_HOOK
    599   1.1      matt #endif
    600   1.1      matt }
    601   1.1      matt 
    602   1.1      matt 
    603   1.1      matt 
    605   1.1      matt /* Regex construction routine.
    606   1.1      matt 
    607   1.1      matt    This translates an opcode syntax string into a regex string,
    608   1.1      matt    by replacing any non-character syntax element (such as an
    609   1.1      matt    opcode) with the pattern '.*'
    610   1.1      matt 
    611   1.1      matt    It then compiles the regex and stores it in the opcode, for
    612   1.1      matt    later use by or1k_cgen_assemble_insn
    613   1.1      matt 
    614   1.2  christos    Returns NULL for success, an error message for failure.  */
    615   1.1      matt 
    616   1.2  christos char *
    617   1.1      matt or1k_cgen_build_insn_regex (CGEN_INSN *insn)
    618   1.1      matt {
    619   1.1      matt   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
    620   1.1      matt   const char *mnem = CGEN_INSN_MNEMONIC (insn);
    621   1.1      matt   char rxbuf[CGEN_MAX_RX_ELEMENTS];
    622   1.1      matt   char *rx = rxbuf;
    623   1.1      matt   const CGEN_SYNTAX_CHAR_TYPE *syn;
    624   1.1      matt   int reg_err;
    625   1.1      matt 
    626   1.1      matt   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
    627   1.1      matt 
    628   1.1      matt   /* Mnemonics come first in the syntax string.  */
    629   1.1      matt   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    630   1.1      matt     return _("missing mnemonic in syntax string");
    631   1.1      matt   ++syn;
    632   1.1      matt 
    633   1.1      matt   /* Generate a case sensitive regular expression that emulates case
    634   1.1      matt      insensitive matching in the "C" locale.  We cannot generate a case
    635   1.1      matt      insensitive regular expression because in Turkish locales, 'i' and 'I'
    636   1.1      matt      are not equal modulo case conversion.  */
    637   1.1      matt 
    638   1.1      matt   /* Copy the literal mnemonic out of the insn.  */
    639   1.1      matt   for (; *mnem; mnem++)
    640   1.1      matt     {
    641   1.1      matt       char c = *mnem;
    642   1.1      matt 
    643   1.1      matt       if (ISALPHA (c))
    644   1.1      matt 	{
    645   1.1      matt 	  *rx++ = '[';
    646   1.1      matt 	  *rx++ = TOLOWER (c);
    647   1.1      matt 	  *rx++ = TOUPPER (c);
    648   1.1      matt 	  *rx++ = ']';
    649   1.1      matt 	}
    650   1.1      matt       else
    651   1.1      matt 	*rx++ = c;
    652   1.1      matt     }
    653   1.1      matt 
    654   1.1      matt   /* Copy any remaining literals from the syntax string into the rx.  */
    655   1.2  christos   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
    656   1.1      matt     {
    657   1.1      matt       if (CGEN_SYNTAX_CHAR_P (* syn))
    658   1.1      matt 	{
    659   1.2  christos 	  char c = CGEN_SYNTAX_CHAR (* syn);
    660   1.1      matt 
    661   1.1      matt 	  switch (c)
    662   1.2  christos 	    {
    663   1.2  christos 	      /* Escape any regex metacharacters in the syntax.  */
    664   1.1      matt 	    case '.': case '[': case '\\':
    665   1.1      matt 	    case '*': case '^': case '$':
    666   1.2  christos 
    667   1.1      matt #ifdef CGEN_ESCAPE_EXTENDED_REGEX
    668   1.1      matt 	    case '?': case '{': case '}':
    669   1.1      matt 	    case '(': case ')': case '*':
    670   1.1      matt 	    case '|': case '+': case ']':
    671   1.1      matt #endif
    672   1.1      matt 	      *rx++ = '\\';
    673   1.1      matt 	      *rx++ = c;
    674   1.1      matt 	      break;
    675   1.1      matt 
    676   1.1      matt 	    default:
    677   1.1      matt 	      if (ISALPHA (c))
    678   1.1      matt 		{
    679   1.1      matt 		  *rx++ = '[';
    680   1.1      matt 		  *rx++ = TOLOWER (c);
    681   1.1      matt 		  *rx++ = TOUPPER (c);
    682   1.1      matt 		  *rx++ = ']';
    683   1.1      matt 		}
    684   1.1      matt 	      else
    685   1.1      matt 		*rx++ = c;
    686   1.1      matt 	      break;
    687   1.1      matt 	    }
    688   1.1      matt 	}
    689   1.1      matt       else
    690   1.1      matt 	{
    691   1.1      matt 	  /* Replace non-syntax fields with globs.  */
    692   1.1      matt 	  *rx++ = '.';
    693   1.1      matt 	  *rx++ = '*';
    694   1.1      matt 	}
    695   1.1      matt     }
    696   1.2  christos 
    697   1.2  christos   /* Trailing whitespace ok.  */
    698   1.2  christos   * rx++ = '[';
    699   1.2  christos   * rx++ = ' ';
    700   1.2  christos   * rx++ = '\t';
    701   1.1      matt   * rx++ = ']';
    702   1.1      matt   * rx++ = '*';
    703   1.2  christos 
    704   1.1      matt   /* But anchor it after that.  */
    705   1.1      matt   * rx++ = '$';
    706   1.1      matt   * rx = '\0';
    707   1.1      matt 
    708   1.1      matt   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
    709   1.2  christos   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
    710   1.1      matt 
    711   1.1      matt   if (reg_err == 0)
    712   1.1      matt     return NULL;
    713   1.1      matt   else
    714   1.1      matt     {
    715   1.1      matt       static char msg[80];
    716   1.1      matt 
    717   1.1      matt       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
    718   1.1      matt       regfree ((regex_t *) CGEN_INSN_RX (insn));
    719   1.1      matt       free (CGEN_INSN_RX (insn));
    720   1.1      matt       (CGEN_INSN_RX (insn)) = NULL;
    721   1.1      matt       return msg;
    722   1.1      matt     }
    723   1.1      matt }
    724   1.1      matt 
    725   1.1      matt 
    726   1.1      matt /* Default insn parser.
    728   1.1      matt 
    729   1.1      matt    The syntax string is scanned and operands are parsed and stored in FIELDS.
    730   1.1      matt    Relocs are queued as we go via other callbacks.
    731   1.1      matt 
    732   1.1      matt    ??? Note that this is currently an all-or-nothing parser.  If we fail to
    733   1.1      matt    parse the instruction, we return 0 and the caller will start over from
    734   1.1      matt    the beginning.  Backtracking will be necessary in parsing subexpressions,
    735   1.1      matt    but that can be handled there.  Not handling backtracking here may get
    736   1.1      matt    expensive in the case of the m68k.  Deal with later.
    737   1.1      matt 
    738   1.1      matt    Returns NULL for success, an error message for failure.  */
    739   1.1      matt 
    740   1.1      matt static const char *
    741   1.1      matt parse_insn_normal (CGEN_CPU_DESC cd,
    742   1.1      matt 		   const CGEN_INSN *insn,
    743   1.1      matt 		   const char **strp,
    744   1.1      matt 		   CGEN_FIELDS *fields)
    745   1.1      matt {
    746   1.1      matt   /* ??? Runtime added insns not handled yet.  */
    747   1.1      matt   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
    748   1.1      matt   const char *str = *strp;
    749   1.1      matt   const char *errmsg;
    750   1.1      matt   const char *p;
    751   1.1      matt   const CGEN_SYNTAX_CHAR_TYPE * syn;
    752   1.1      matt #ifdef CGEN_MNEMONIC_OPERANDS
    753   1.1      matt   /* FIXME: wip */
    754   1.1      matt   int past_opcode_p;
    755   1.1      matt #endif
    756   1.1      matt 
    757   1.1      matt   /* For now we assume the mnemonic is first (there are no leading operands).
    758   1.1      matt      We can parse it without needing to set up operand parsing.
    759   1.1      matt      GAS's input scrubber will ensure mnemonics are lowercase, but we may
    760   1.1      matt      not be called from GAS.  */
    761   1.1      matt   p = CGEN_INSN_MNEMONIC (insn);
    762   1.1      matt   while (*p && TOLOWER (*p) == TOLOWER (*str))
    763   1.1      matt     ++p, ++str;
    764   1.1      matt 
    765   1.1      matt   if (* p)
    766   1.1      matt     return _("unrecognized instruction");
    767   1.1      matt 
    768   1.1      matt #ifndef CGEN_MNEMONIC_OPERANDS
    769   1.1      matt   if (* str && ! ISSPACE (* str))
    770   1.1      matt     return _("unrecognized instruction");
    771   1.1      matt #endif
    772   1.1      matt 
    773   1.1      matt   CGEN_INIT_PARSE (cd);
    774   1.1      matt   cgen_init_parse_operand (cd);
    775   1.1      matt #ifdef CGEN_MNEMONIC_OPERANDS
    776   1.1      matt   past_opcode_p = 0;
    777   1.1      matt #endif
    778   1.1      matt 
    779   1.1      matt   /* We don't check for (*str != '\0') here because we want to parse
    780   1.1      matt      any trailing fake arguments in the syntax string.  */
    781   1.1      matt   syn = CGEN_SYNTAX_STRING (syntax);
    782   1.1      matt 
    783   1.1      matt   /* Mnemonics come first for now, ensure valid string.  */
    784   1.1      matt   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
    785   1.1      matt     abort ();
    786   1.1      matt 
    787   1.1      matt   ++syn;
    788   1.1      matt 
    789   1.1      matt   while (* syn != 0)
    790   1.1      matt     {
    791   1.1      matt       /* Non operand chars must match exactly.  */
    792   1.1      matt       if (CGEN_SYNTAX_CHAR_P (* syn))
    793   1.1      matt 	{
    794   1.1      matt 	  /* FIXME: While we allow for non-GAS callers above, we assume the
    795   1.1      matt 	     first char after the mnemonic part is a space.  */
    796   1.1      matt 	  /* FIXME: We also take inappropriate advantage of the fact that
    797   1.1      matt 	     GAS's input scrubber will remove extraneous blanks.  */
    798   1.1      matt 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
    799   1.1      matt 	    {
    800   1.1      matt #ifdef CGEN_MNEMONIC_OPERANDS
    801   1.1      matt 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
    802   1.1      matt 		past_opcode_p = 1;
    803   1.1      matt #endif
    804   1.1      matt 	      ++ syn;
    805   1.1      matt 	      ++ str;
    806   1.1      matt 	    }
    807   1.1      matt 	  else if (*str)
    808   1.1      matt 	    {
    809   1.1      matt 	      /* Syntax char didn't match.  Can't be this insn.  */
    810   1.1      matt 	      static char msg [80];
    811   1.1      matt 
    812   1.1      matt 	      /* xgettext:c-format */
    813   1.1      matt 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
    814   1.1      matt 		       CGEN_SYNTAX_CHAR(*syn), *str);
    815   1.1      matt 	      return msg;
    816   1.1      matt 	    }
    817   1.1      matt 	  else
    818   1.1      matt 	    {
    819   1.1      matt 	      /* Ran out of input.  */
    820   1.1      matt 	      static char msg [80];
    821   1.1      matt 
    822   1.1      matt 	      /* xgettext:c-format */
    823   1.1      matt 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
    824   1.1      matt 		       CGEN_SYNTAX_CHAR(*syn));
    825   1.1      matt 	      return msg;
    826   1.1      matt 	    }
    827   1.1      matt 	  continue;
    828   1.1      matt 	}
    829   1.1      matt 
    830   1.1      matt #ifdef CGEN_MNEMONIC_OPERANDS
    831   1.1      matt       (void) past_opcode_p;
    832   1.1      matt #endif
    833   1.1      matt       /* We have an operand of some sort.  */
    834   1.1      matt       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
    835   1.1      matt       if (errmsg)
    836   1.1      matt 	return errmsg;
    837   1.1      matt 
    838   1.1      matt       /* Done with this operand, continue with next one.  */
    839   1.1      matt       ++ syn;
    840   1.1      matt     }
    841   1.1      matt 
    842   1.1      matt   /* If we're at the end of the syntax string, we're done.  */
    843   1.1      matt   if (* syn == 0)
    844   1.1      matt     {
    845   1.1      matt       /* FIXME: For the moment we assume a valid `str' can only contain
    846   1.1      matt 	 blanks now.  IE: We needn't try again with a longer version of
    847   1.1      matt 	 the insn and it is assumed that longer versions of insns appear
    848   1.1      matt 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
    849   1.1      matt       while (ISSPACE (* str))
    850   1.1      matt 	++ str;
    851   1.1      matt 
    852   1.1      matt       if (* str != '\0')
    853   1.1      matt 	return _("junk at end of line"); /* FIXME: would like to include `str' */
    854   1.1      matt 
    855   1.1      matt       return NULL;
    856   1.1      matt     }
    857   1.1      matt 
    858   1.1      matt   /* We couldn't parse it.  */
    859   1.1      matt   return _("unrecognized instruction");
    860   1.1      matt }
    861   1.1      matt 
    862   1.1      matt /* Main entry point.
    864   1.1      matt    This routine is called for each instruction to be assembled.
    865   1.1      matt    STR points to the insn to be assembled.
    866   1.1      matt    We assume all necessary tables have been initialized.
    867   1.1      matt    The assembled instruction, less any fixups, is stored in BUF.
    868   1.1      matt    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
    869   1.1      matt    still needs to be converted to target byte order, otherwise BUF is an array
    870   1.1      matt    of bytes in target byte order.
    871   1.1      matt    The result is a pointer to the insn's entry in the opcode table,
    872   1.1      matt    or NULL if an error occured (an error message will have already been
    873   1.1      matt    printed).
    874   1.1      matt 
    875   1.1      matt    Note that when processing (non-alias) macro-insns,
    876   1.1      matt    this function recurses.
    877   1.1      matt 
    878   1.1      matt    ??? It's possible to make this cpu-independent.
    879   1.1      matt    One would have to deal with a few minor things.
    880   1.1      matt    At this point in time doing so would be more of a curiosity than useful
    881   1.1      matt    [for example this file isn't _that_ big], but keeping the possibility in
    882   1.1      matt    mind helps keep the design clean.  */
    883   1.1      matt 
    884   1.1      matt const CGEN_INSN *
    885   1.1      matt or1k_cgen_assemble_insn (CGEN_CPU_DESC cd,
    886   1.1      matt 			   const char *str,
    887   1.1      matt 			   CGEN_FIELDS *fields,
    888   1.1      matt 			   CGEN_INSN_BYTES_PTR buf,
    889   1.1      matt 			   char **errmsg)
    890   1.1      matt {
    891   1.1      matt   const char *start;
    892   1.1      matt   CGEN_INSN_LIST *ilist;
    893   1.1      matt   const char *parse_errmsg = NULL;
    894   1.1      matt   const char *insert_errmsg = NULL;
    895   1.1      matt   int recognized_mnemonic = 0;
    896   1.1      matt 
    897   1.1      matt   /* Skip leading white space.  */
    898   1.1      matt   while (ISSPACE (* str))
    899   1.1      matt     ++ str;
    900   1.1      matt 
    901   1.1      matt   /* The instructions are stored in hashed lists.
    902   1.1      matt      Get the first in the list.  */
    903   1.1      matt   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
    904   1.1      matt 
    905   1.1      matt   /* Keep looking until we find a match.  */
    906   1.1      matt   start = str;
    907   1.1      matt   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
    908   1.2  christos     {
    909   1.1      matt       const CGEN_INSN *insn = ilist->insn;
    910   1.1      matt       recognized_mnemonic = 1;
    911   1.1      matt 
    912   1.1      matt #ifdef CGEN_VALIDATE_INSN_SUPPORTED
    913   1.1      matt       /* Not usually needed as unsupported opcodes
    914   1.1      matt 	 shouldn't be in the hash lists.  */
    915   1.1      matt       /* Is this insn supported by the selected cpu?  */
    916   1.1      matt       if (! or1k_cgen_insn_supported (cd, insn))
    917   1.1      matt 	continue;
    918   1.1      matt #endif
    919   1.1      matt       /* If the RELAXED attribute is set, this is an insn that shouldn't be
    920   1.1      matt 	 chosen immediately.  Instead, it is used during assembler/linker
    921   1.1      matt 	 relaxation if possible.  */
    922   1.1      matt       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
    923   1.1      matt 	continue;
    924   1.1      matt 
    925   1.1      matt       str = start;
    926   1.1      matt 
    927   1.1      matt       /* Skip this insn if str doesn't look right lexically.  */
    928   1.1      matt       if (CGEN_INSN_RX (insn) != NULL &&
    929   1.1      matt 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
    930   1.1      matt 	continue;
    931   1.1      matt 
    932   1.1      matt       /* Allow parse/insert handlers to obtain length of insn.  */
    933   1.1      matt       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
    934   1.1      matt 
    935   1.1      matt       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
    936   1.1      matt       if (parse_errmsg != NULL)
    937   1.1      matt 	continue;
    938   1.1      matt 
    939   1.1      matt       /* ??? 0 is passed for `pc'.  */
    940   1.1      matt       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
    941   1.1      matt 						 (bfd_vma) 0);
    942   1.1      matt       if (insert_errmsg != NULL)
    943   1.1      matt         continue;
    944   1.1      matt 
    945   1.1      matt       /* It is up to the caller to actually output the insn and any
    946   1.1      matt          queued relocs.  */
    947   1.1      matt       return insn;
    948   1.1      matt     }
    949   1.1      matt 
    950   1.1      matt   {
    951   1.1      matt     static char errbuf[150];
    952   1.1      matt     const char *tmp_errmsg;
    953   1.1      matt #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
    954   1.1      matt #define be_verbose 1
    955   1.1      matt #else
    956   1.1      matt #define be_verbose 0
    957   1.1      matt #endif
    958   1.1      matt 
    959   1.1      matt     if (be_verbose)
    960   1.1      matt       {
    961   1.1      matt 	/* If requesting verbose error messages, use insert_errmsg.
    962   1.1      matt 	   Failing that, use parse_errmsg.  */
    963   1.1      matt 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
    964   1.1      matt 		      parse_errmsg ? parse_errmsg :
    965   1.1      matt 		      recognized_mnemonic ?
    966   1.1      matt 		      _("unrecognized form of instruction") :
    967   1.1      matt 		      _("unrecognized instruction"));
    968   1.2  christos 
    969   1.1      matt 	if (strlen (start) > 50)
    970   1.1      matt 	  /* xgettext:c-format */
    971   1.1      matt 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
    972   1.1      matt 	else
    973   1.1      matt 	  /* xgettext:c-format */
    974   1.1      matt 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
    975   1.1      matt       }
    976   1.1      matt     else
    977   1.2  christos       {
    978   1.1      matt 	if (strlen (start) > 50)
    979   1.1      matt 	  /* xgettext:c-format */
    980   1.1      matt 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
    981   1.2  christos 	else
    982   1.1      matt 	  /* xgettext:c-format */
    983   1.1      matt 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
    984   1.1      matt       }
    985   1.1      matt 
    986                     *errmsg = errbuf;
    987                     return NULL;
    988                   }
    989                 }
    990