xref: /netbsd-src/external/gpl3/binutils.old/dist/opcodes/mt-asm.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1ede78133Schristos /* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */
275fd0b74Schristos /* Assembler interface for targets using CGEN. -*- C -*-
375fd0b74Schristos    CGEN: Cpu tools GENerator
475fd0b74Schristos 
575fd0b74Schristos    THIS FILE IS MACHINE GENERATED WITH CGEN.
675fd0b74Schristos    - the resultant file is machine generated, cgen-asm.in isn't
775fd0b74Schristos 
8*e992f068Schristos    Copyright (C) 1996-2022 Free Software Foundation, Inc.
975fd0b74Schristos 
1075fd0b74Schristos    This file is part of libopcodes.
1175fd0b74Schristos 
1275fd0b74Schristos    This library is free software; you can redistribute it and/or modify
1375fd0b74Schristos    it under the terms of the GNU General Public License as published by
1475fd0b74Schristos    the Free Software Foundation; either version 3, or (at your option)
1575fd0b74Schristos    any later version.
1675fd0b74Schristos 
1775fd0b74Schristos    It is distributed in the hope that it will be useful, but WITHOUT
1875fd0b74Schristos    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1975fd0b74Schristos    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
2075fd0b74Schristos    License for more details.
2175fd0b74Schristos 
2275fd0b74Schristos    You should have received a copy of the GNU General Public License
2375fd0b74Schristos    along with this program; if not, write to the Free Software Foundation, Inc.,
2475fd0b74Schristos    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2575fd0b74Schristos 
2675fd0b74Schristos 
2775fd0b74Schristos /* ??? Eventually more and more of this stuff can go to cpu-independent files.
2875fd0b74Schristos    Keep that in mind.  */
2975fd0b74Schristos 
3075fd0b74Schristos #include "sysdep.h"
3175fd0b74Schristos #include <stdio.h>
3275fd0b74Schristos #include "ansidecl.h"
3375fd0b74Schristos #include "bfd.h"
3475fd0b74Schristos #include "symcat.h"
3575fd0b74Schristos #include "mt-desc.h"
3675fd0b74Schristos #include "mt-opc.h"
3775fd0b74Schristos #include "opintl.h"
3875fd0b74Schristos #include "xregex.h"
3975fd0b74Schristos #include "libiberty.h"
4075fd0b74Schristos #include "safe-ctype.h"
4175fd0b74Schristos 
4275fd0b74Schristos #undef  min
4375fd0b74Schristos #define min(a,b) ((a) < (b) ? (a) : (b))
4475fd0b74Schristos #undef  max
4575fd0b74Schristos #define max(a,b) ((a) > (b) ? (a) : (b))
4675fd0b74Schristos 
4775fd0b74Schristos static const char * parse_insn_normal
4875fd0b74Schristos   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
4975fd0b74Schristos 
5075fd0b74Schristos /* -- assembler routines inserted here.  */
5175fd0b74Schristos 
5275fd0b74Schristos /* -- asm.c */
5375fd0b74Schristos /* Range checking for signed numbers.  Returns 0 if acceptable
5475fd0b74Schristos    and 1 if the value is out of bounds for a signed quantity.  */
5575fd0b74Schristos 
5675fd0b74Schristos static int
signed_out_of_bounds(long val)5775fd0b74Schristos signed_out_of_bounds (long val)
5875fd0b74Schristos {
5975fd0b74Schristos   if ((val < -32768) || (val > 32767))
6075fd0b74Schristos     return 1;
6175fd0b74Schristos   return 0;
6275fd0b74Schristos }
6375fd0b74Schristos 
6475fd0b74Schristos static const char *
parse_loopsize(CGEN_CPU_DESC cd,const char ** strp,int opindex,void * arg)6575fd0b74Schristos parse_loopsize (CGEN_CPU_DESC cd,
6675fd0b74Schristos 		const char **strp,
6775fd0b74Schristos 		int opindex,
6875fd0b74Schristos 		void *arg)
6975fd0b74Schristos {
7075fd0b74Schristos   signed long * valuep = (signed long *) arg;
7175fd0b74Schristos   const char *errmsg;
7275fd0b74Schristos   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
7375fd0b74Schristos   enum cgen_parse_operand_result result_type;
7475fd0b74Schristos   bfd_vma value;
7575fd0b74Schristos 
7675fd0b74Schristos   /* Is it a control transfer instructions?  */
7775fd0b74Schristos   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_LOOPSIZE)
7875fd0b74Schristos     {
7975fd0b74Schristos       code = BFD_RELOC_MT_PCINSN8;
8075fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, code,
8175fd0b74Schristos                                    & result_type, & value);
8275fd0b74Schristos       *valuep = value;
8375fd0b74Schristos       return errmsg;
8475fd0b74Schristos     }
8575fd0b74Schristos 
8675fd0b74Schristos   abort ();
8775fd0b74Schristos }
8875fd0b74Schristos 
8975fd0b74Schristos static const char *
parse_imm16(CGEN_CPU_DESC cd,const char ** strp,int opindex,void * arg)9075fd0b74Schristos parse_imm16 (CGEN_CPU_DESC cd,
9175fd0b74Schristos 	     const char **strp,
9275fd0b74Schristos 	     int opindex,
9375fd0b74Schristos 	     void *arg)
9475fd0b74Schristos {
9575fd0b74Schristos   signed long * valuep = (signed long *) arg;
9675fd0b74Schristos   const char *errmsg;
9775fd0b74Schristos   enum cgen_parse_operand_result result_type;
9875fd0b74Schristos   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
9975fd0b74Schristos   bfd_vma value;
10075fd0b74Schristos 
10175fd0b74Schristos   /* Is it a control transfer instructions?  */
10275fd0b74Schristos   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16O)
10375fd0b74Schristos     {
10475fd0b74Schristos       code = BFD_RELOC_16_PCREL;
10575fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, code,
10675fd0b74Schristos                                    & result_type, & value);
10775fd0b74Schristos       if (errmsg == NULL)
10875fd0b74Schristos 	{
10975fd0b74Schristos 	  if (signed_out_of_bounds (value))
11075fd0b74Schristos 	    errmsg = _("Operand out of range. Must be between -32768 and 32767.");
11175fd0b74Schristos 	}
11275fd0b74Schristos       *valuep = value;
11375fd0b74Schristos       return errmsg;
11475fd0b74Schristos     }
11575fd0b74Schristos 
11675fd0b74Schristos   /* If it's not a control transfer instruction, then
11775fd0b74Schristos      we have to check for %OP relocating operators.  */
11875fd0b74Schristos   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16L)
11975fd0b74Schristos     ;
12075fd0b74Schristos   else if (strncmp (*strp, "%hi16", 5) == 0)
12175fd0b74Schristos     {
12275fd0b74Schristos       *strp += 5;
12375fd0b74Schristos       code = BFD_RELOC_HI16;
12475fd0b74Schristos     }
12575fd0b74Schristos   else if (strncmp (*strp, "%lo16", 5) == 0)
12675fd0b74Schristos     {
12775fd0b74Schristos       *strp += 5;
12875fd0b74Schristos       code = BFD_RELOC_LO16;
12975fd0b74Schristos     }
13075fd0b74Schristos 
13175fd0b74Schristos   /* If we found a %OP relocating operator, then parse it as an address.
13275fd0b74Schristos      If not, we need to parse it as an integer, either signed or unsigned
13375fd0b74Schristos      depending on which operand type we have.  */
13475fd0b74Schristos   if (code != BFD_RELOC_NONE)
13575fd0b74Schristos     {
13675fd0b74Schristos        /* %OP relocating operator found.  */
13775fd0b74Schristos        errmsg = cgen_parse_address (cd, strp, opindex, code,
13875fd0b74Schristos                                    & result_type, & value);
13975fd0b74Schristos        if (errmsg == NULL)
14075fd0b74Schristos 	 {
14175fd0b74Schristos            switch (result_type)
14275fd0b74Schristos 	     {
14375fd0b74Schristos 	     case (CGEN_PARSE_OPERAND_RESULT_NUMBER):
14475fd0b74Schristos 	       if (code == BFD_RELOC_HI16)
14575fd0b74Schristos 		 value = (value >> 16) & 0xFFFF;
14675fd0b74Schristos 	       else if (code == BFD_RELOC_LO16)
14775fd0b74Schristos 		 value = value  & 0xFFFF;
14875fd0b74Schristos 	       else
14975fd0b74Schristos 		 errmsg = _("Biiiig Trouble in parse_imm16!");
15075fd0b74Schristos 	       break;
15175fd0b74Schristos 
15275fd0b74Schristos 	     case (CGEN_PARSE_OPERAND_RESULT_QUEUED):
15375fd0b74Schristos 	       /* No special processing for this case.  */
15475fd0b74Schristos 	       break;
15575fd0b74Schristos 
15675fd0b74Schristos 	     default:
15775fd0b74Schristos 	       errmsg = _("The percent-operator's operand is not a symbol");
15875fd0b74Schristos 	       break;
15975fd0b74Schristos              }
16075fd0b74Schristos 	 }
16175fd0b74Schristos        *valuep = value;
16275fd0b74Schristos     }
16375fd0b74Schristos   else
16475fd0b74Schristos     {
16575fd0b74Schristos       /* Parse hex values like 0xffff as unsigned, and sign extend
16675fd0b74Schristos 	 them manually.  */
16775fd0b74Schristos       int parse_signed = (opindex == (CGEN_OPERAND_TYPE)MT_OPERAND_IMM16);
16875fd0b74Schristos 
16975fd0b74Schristos       if ((*strp)[0] == '0'
17075fd0b74Schristos 	  && ((*strp)[1] == 'x' || (*strp)[1] == 'X'))
17175fd0b74Schristos 	parse_signed = 0;
17275fd0b74Schristos 
17375fd0b74Schristos       /* No relocating operator.  Parse as an number.  */
17475fd0b74Schristos       if (parse_signed)
17575fd0b74Schristos 	{
17675fd0b74Schristos           /* Parse as as signed integer.  */
17775fd0b74Schristos 
17875fd0b74Schristos           errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
17975fd0b74Schristos 
18075fd0b74Schristos           if (errmsg == NULL)
18175fd0b74Schristos 	    {
18275fd0b74Schristos #if 0
18375fd0b74Schristos 	      /* Manual range checking is needed for the signed case.  */
18475fd0b74Schristos 	      if (*valuep & 0x8000)
18575fd0b74Schristos                 value = 0xffff0000 | *valuep;
18675fd0b74Schristos 	      else
18775fd0b74Schristos                 value = *valuep;
18875fd0b74Schristos 
18975fd0b74Schristos 	      if (signed_out_of_bounds (value))
19075fd0b74Schristos 	        errmsg = _("Operand out of range. Must be between -32768 and 32767.");
19175fd0b74Schristos 	      /* Truncate to 16 bits. This is necessary
19275fd0b74Schristos 		 because cgen will have sign extended *valuep.  */
19375fd0b74Schristos 	      *valuep &= 0xFFFF;
19475fd0b74Schristos #endif
19575fd0b74Schristos 	    }
19675fd0b74Schristos 	}
19775fd0b74Schristos       else
19875fd0b74Schristos 	{
19975fd0b74Schristos           /* MT_OPERAND_IMM16Z.  Parse as an unsigned integer.  */
20075fd0b74Schristos           errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) valuep);
20175fd0b74Schristos 
20275fd0b74Schristos 	  if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16
20375fd0b74Schristos 	      && *valuep >= 0x8000
20475fd0b74Schristos 	      && *valuep <= 0xffff)
20575fd0b74Schristos 	    *valuep -= 0x10000;
20675fd0b74Schristos 	}
20775fd0b74Schristos     }
20875fd0b74Schristos 
20975fd0b74Schristos   return errmsg;
21075fd0b74Schristos }
21175fd0b74Schristos 
21275fd0b74Schristos 
21375fd0b74Schristos static const char *
parse_dup(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)21475fd0b74Schristos parse_dup (CGEN_CPU_DESC cd,
21575fd0b74Schristos 	   const char **strp,
21675fd0b74Schristos 	   int opindex,
21775fd0b74Schristos 	   unsigned long *valuep)
21875fd0b74Schristos {
21975fd0b74Schristos   const char *errmsg = NULL;
22075fd0b74Schristos 
22175fd0b74Schristos   if (strncmp (*strp, "dup", 3) == 0 || strncmp (*strp, "DUP", 3) == 0)
22275fd0b74Schristos     {
22375fd0b74Schristos       *strp += 3;
22475fd0b74Schristos       *valuep = 1;
22575fd0b74Schristos     }
22675fd0b74Schristos   else if (strncmp (*strp, "xx", 2) == 0 || strncmp (*strp, "XX", 2) == 0)
22775fd0b74Schristos     {
22875fd0b74Schristos       *strp += 2;
22975fd0b74Schristos       *valuep = 0;
23075fd0b74Schristos     }
23175fd0b74Schristos   else
23275fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
23375fd0b74Schristos 
23475fd0b74Schristos   return errmsg;
23575fd0b74Schristos }
23675fd0b74Schristos 
23775fd0b74Schristos 
23875fd0b74Schristos static const char *
parse_ball(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)23975fd0b74Schristos parse_ball (CGEN_CPU_DESC cd,
24075fd0b74Schristos 	    const char **strp,
24175fd0b74Schristos 	    int opindex,
24275fd0b74Schristos 	    unsigned long *valuep)
24375fd0b74Schristos {
24475fd0b74Schristos   const char *errmsg = NULL;
24575fd0b74Schristos 
24675fd0b74Schristos   if (strncmp (*strp, "all", 3) == 0 || strncmp (*strp, "ALL", 3) == 0)
24775fd0b74Schristos     {
24875fd0b74Schristos       *strp += 3;
24975fd0b74Schristos       *valuep = 1;
25075fd0b74Schristos     }
25175fd0b74Schristos   else if (strncmp (*strp, "one", 3) == 0 || strncmp (*strp, "ONE", 3) == 0)
25275fd0b74Schristos     {
25375fd0b74Schristos       *strp += 3;
25475fd0b74Schristos       *valuep = 0;
25575fd0b74Schristos     }
25675fd0b74Schristos   else
25775fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
25875fd0b74Schristos 
25975fd0b74Schristos   return errmsg;
26075fd0b74Schristos }
26175fd0b74Schristos 
26275fd0b74Schristos static const char *
parse_xmode(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)26375fd0b74Schristos parse_xmode (CGEN_CPU_DESC cd,
26475fd0b74Schristos 	     const char **strp,
26575fd0b74Schristos 	     int opindex,
26675fd0b74Schristos 	     unsigned long *valuep)
26775fd0b74Schristos {
26875fd0b74Schristos   const char *errmsg = NULL;
26975fd0b74Schristos 
27075fd0b74Schristos   if (strncmp (*strp, "pm", 2) == 0 || strncmp (*strp, "PM", 2) == 0)
27175fd0b74Schristos     {
27275fd0b74Schristos       *strp += 2;
27375fd0b74Schristos       *valuep = 1;
27475fd0b74Schristos     }
27575fd0b74Schristos   else if (strncmp (*strp, "xm", 2) == 0 || strncmp (*strp, "XM", 2) == 0)
27675fd0b74Schristos     {
27775fd0b74Schristos       *strp += 2;
27875fd0b74Schristos       *valuep = 0;
27975fd0b74Schristos     }
28075fd0b74Schristos   else
28175fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
28275fd0b74Schristos 
28375fd0b74Schristos   return errmsg;
28475fd0b74Schristos }
28575fd0b74Schristos 
28675fd0b74Schristos static const char *
parse_rc(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)28775fd0b74Schristos parse_rc (CGEN_CPU_DESC cd,
28875fd0b74Schristos 	  const char **strp,
28975fd0b74Schristos 	  int opindex,
29075fd0b74Schristos 	  unsigned long *valuep)
29175fd0b74Schristos {
29275fd0b74Schristos   const char *errmsg = NULL;
29375fd0b74Schristos 
29475fd0b74Schristos   if (strncmp (*strp, "r", 1) == 0 || strncmp (*strp, "R", 1) == 0)
29575fd0b74Schristos     {
29675fd0b74Schristos       *strp += 1;
29775fd0b74Schristos       *valuep = 1;
29875fd0b74Schristos     }
29975fd0b74Schristos   else if (strncmp (*strp, "c", 1) == 0 || strncmp (*strp, "C", 1) == 0)
30075fd0b74Schristos     {
30175fd0b74Schristos       *strp += 1;
30275fd0b74Schristos       *valuep = 0;
30375fd0b74Schristos     }
30475fd0b74Schristos   else
30575fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
30675fd0b74Schristos 
30775fd0b74Schristos   return errmsg;
30875fd0b74Schristos }
30975fd0b74Schristos 
31075fd0b74Schristos static const char *
parse_cbrb(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)31175fd0b74Schristos parse_cbrb (CGEN_CPU_DESC cd,
31275fd0b74Schristos 	    const char **strp,
31375fd0b74Schristos 	    int opindex,
31475fd0b74Schristos 	    unsigned long *valuep)
31575fd0b74Schristos {
31675fd0b74Schristos   const char *errmsg = NULL;
31775fd0b74Schristos 
31875fd0b74Schristos   if (strncmp (*strp, "rb", 2) == 0 || strncmp (*strp, "RB", 2) == 0)
31975fd0b74Schristos     {
32075fd0b74Schristos       *strp += 2;
32175fd0b74Schristos       *valuep = 1;
32275fd0b74Schristos     }
32375fd0b74Schristos   else if (strncmp (*strp, "cb", 2) == 0 || strncmp (*strp, "CB", 2) == 0)
32475fd0b74Schristos     {
32575fd0b74Schristos       *strp += 2;
32675fd0b74Schristos       *valuep = 0;
32775fd0b74Schristos     }
32875fd0b74Schristos   else
32975fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
33075fd0b74Schristos 
33175fd0b74Schristos   return errmsg;
33275fd0b74Schristos }
33375fd0b74Schristos 
33475fd0b74Schristos static const char *
parse_rbbc(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)33575fd0b74Schristos parse_rbbc (CGEN_CPU_DESC cd,
33675fd0b74Schristos 	    const char **strp,
33775fd0b74Schristos 	    int opindex,
33875fd0b74Schristos 	    unsigned long *valuep)
33975fd0b74Schristos {
34075fd0b74Schristos   const char *errmsg = NULL;
34175fd0b74Schristos 
34275fd0b74Schristos   if (strncmp (*strp, "rt", 2) == 0 || strncmp (*strp, "RT", 2) == 0)
34375fd0b74Schristos     {
34475fd0b74Schristos       *strp += 2;
34575fd0b74Schristos       *valuep = 0;
34675fd0b74Schristos     }
34775fd0b74Schristos   else if (strncmp (*strp, "br1", 3) == 0 || strncmp (*strp, "BR1", 3) == 0)
34875fd0b74Schristos     {
34975fd0b74Schristos       *strp += 3;
35075fd0b74Schristos       *valuep = 1;
35175fd0b74Schristos     }
35275fd0b74Schristos   else if (strncmp (*strp, "br2", 3) == 0 || strncmp (*strp, "BR2", 3) == 0)
35375fd0b74Schristos     {
35475fd0b74Schristos       *strp += 3;
35575fd0b74Schristos       *valuep = 2;
35675fd0b74Schristos     }
35775fd0b74Schristos   else if (strncmp (*strp, "cs", 2) == 0 || strncmp (*strp, "CS", 2) == 0)
35875fd0b74Schristos     {
35975fd0b74Schristos       *strp += 2;
36075fd0b74Schristos       *valuep = 3;
36175fd0b74Schristos     }
36275fd0b74Schristos   else
36375fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
36475fd0b74Schristos 
36575fd0b74Schristos   return errmsg;
36675fd0b74Schristos }
36775fd0b74Schristos 
36875fd0b74Schristos static const char *
parse_type(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)36975fd0b74Schristos parse_type (CGEN_CPU_DESC cd,
37075fd0b74Schristos 	    const char **strp,
37175fd0b74Schristos 	    int opindex,
37275fd0b74Schristos 	    unsigned long *valuep)
37375fd0b74Schristos {
37475fd0b74Schristos   const char *errmsg = NULL;
37575fd0b74Schristos 
37675fd0b74Schristos   if (strncmp (*strp, "odd", 3) == 0 || strncmp (*strp, "ODD", 3) == 0)
37775fd0b74Schristos     {
37875fd0b74Schristos       *strp += 3;
37975fd0b74Schristos       *valuep = 0;
38075fd0b74Schristos     }
38175fd0b74Schristos   else if (strncmp (*strp, "even", 4) == 0 || strncmp (*strp, "EVEN", 4) == 0)
38275fd0b74Schristos     {
38375fd0b74Schristos       *strp += 4;
38475fd0b74Schristos       *valuep = 1;
38575fd0b74Schristos     }
38675fd0b74Schristos   else if (strncmp (*strp, "oe", 2) == 0 || strncmp (*strp, "OE", 2) == 0)
38775fd0b74Schristos     {
38875fd0b74Schristos       *strp += 2;
38975fd0b74Schristos       *valuep = 2;
39075fd0b74Schristos     }
39175fd0b74Schristos   else
39275fd0b74Schristos     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
39375fd0b74Schristos 
39475fd0b74Schristos  if ((errmsg == NULL) && (*valuep == 3))
39575fd0b74Schristos     errmsg = _("invalid operand.  type may have values 0,1,2 only.");
39675fd0b74Schristos 
39775fd0b74Schristos   return errmsg;
39875fd0b74Schristos }
39975fd0b74Schristos 
40075fd0b74Schristos /* -- dis.c */
40175fd0b74Schristos 
40275fd0b74Schristos const char * mt_cgen_parse_operand
40375fd0b74Schristos   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
40475fd0b74Schristos 
40575fd0b74Schristos /* Main entry point for operand parsing.
40675fd0b74Schristos 
40775fd0b74Schristos    This function is basically just a big switch statement.  Earlier versions
40875fd0b74Schristos    used tables to look up the function to use, but
40975fd0b74Schristos    - if the table contains both assembler and disassembler functions then
41075fd0b74Schristos      the disassembler contains much of the assembler and vice-versa,
41175fd0b74Schristos    - there's a lot of inlining possibilities as things grow,
41275fd0b74Schristos    - using a switch statement avoids the function call overhead.
41375fd0b74Schristos 
41475fd0b74Schristos    This function could be moved into `parse_insn_normal', but keeping it
41575fd0b74Schristos    separate makes clear the interface between `parse_insn_normal' and each of
41675fd0b74Schristos    the handlers.  */
41775fd0b74Schristos 
41875fd0b74Schristos const char *
mt_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)41975fd0b74Schristos mt_cgen_parse_operand (CGEN_CPU_DESC cd,
42075fd0b74Schristos 			   int opindex,
42175fd0b74Schristos 			   const char ** strp,
42275fd0b74Schristos 			   CGEN_FIELDS * fields)
42375fd0b74Schristos {
42475fd0b74Schristos   const char * errmsg = NULL;
42575fd0b74Schristos   /* Used by scalar operands that still need to be parsed.  */
42675fd0b74Schristos   long junk ATTRIBUTE_UNUSED;
42775fd0b74Schristos 
42875fd0b74Schristos   switch (opindex)
42975fd0b74Schristos     {
43075fd0b74Schristos     case MT_OPERAND_A23 :
43175fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_A23, (unsigned long *) (& fields->f_a23));
43275fd0b74Schristos       break;
43375fd0b74Schristos     case MT_OPERAND_BALL :
43475fd0b74Schristos       errmsg = parse_ball (cd, strp, MT_OPERAND_BALL, (unsigned long *) (& fields->f_ball));
43575fd0b74Schristos       break;
43675fd0b74Schristos     case MT_OPERAND_BALL2 :
43775fd0b74Schristos       errmsg = parse_ball (cd, strp, MT_OPERAND_BALL2, (unsigned long *) (& fields->f_ball2));
43875fd0b74Schristos       break;
43975fd0b74Schristos     case MT_OPERAND_BANKADDR :
44075fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BANKADDR, (unsigned long *) (& fields->f_bankaddr));
44175fd0b74Schristos       break;
44275fd0b74Schristos     case MT_OPERAND_BRC :
44375fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC, (unsigned long *) (& fields->f_brc));
44475fd0b74Schristos       break;
44575fd0b74Schristos     case MT_OPERAND_BRC2 :
44675fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC2, (unsigned long *) (& fields->f_brc2));
44775fd0b74Schristos       break;
44875fd0b74Schristos     case MT_OPERAND_CB1INCR :
44975fd0b74Schristos       errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB1INCR, (long *) (& fields->f_cb1incr));
45075fd0b74Schristos       break;
45175fd0b74Schristos     case MT_OPERAND_CB1SEL :
45275fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB1SEL, (unsigned long *) (& fields->f_cb1sel));
45375fd0b74Schristos       break;
45475fd0b74Schristos     case MT_OPERAND_CB2INCR :
45575fd0b74Schristos       errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB2INCR, (long *) (& fields->f_cb2incr));
45675fd0b74Schristos       break;
45775fd0b74Schristos     case MT_OPERAND_CB2SEL :
45875fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB2SEL, (unsigned long *) (& fields->f_cb2sel));
45975fd0b74Schristos       break;
46075fd0b74Schristos     case MT_OPERAND_CBRB :
46175fd0b74Schristos       errmsg = parse_cbrb (cd, strp, MT_OPERAND_CBRB, (unsigned long *) (& fields->f_cbrb));
46275fd0b74Schristos       break;
46375fd0b74Schristos     case MT_OPERAND_CBS :
46475fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBS, (unsigned long *) (& fields->f_cbs));
46575fd0b74Schristos       break;
46675fd0b74Schristos     case MT_OPERAND_CBX :
46775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBX, (unsigned long *) (& fields->f_cbx));
46875fd0b74Schristos       break;
46975fd0b74Schristos     case MT_OPERAND_CCB :
47075fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CCB, (unsigned long *) (& fields->f_ccb));
47175fd0b74Schristos       break;
47275fd0b74Schristos     case MT_OPERAND_CDB :
47375fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CDB, (unsigned long *) (& fields->f_cdb));
47475fd0b74Schristos       break;
47575fd0b74Schristos     case MT_OPERAND_CELL :
47675fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CELL, (unsigned long *) (& fields->f_cell));
47775fd0b74Schristos       break;
47875fd0b74Schristos     case MT_OPERAND_COLNUM :
47975fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_COLNUM, (unsigned long *) (& fields->f_colnum));
48075fd0b74Schristos       break;
48175fd0b74Schristos     case MT_OPERAND_CONTNUM :
48275fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CONTNUM, (unsigned long *) (& fields->f_contnum));
48375fd0b74Schristos       break;
48475fd0b74Schristos     case MT_OPERAND_CR :
48575fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CR, (unsigned long *) (& fields->f_cr));
48675fd0b74Schristos       break;
48775fd0b74Schristos     case MT_OPERAND_CTXDISP :
48875fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CTXDISP, (unsigned long *) (& fields->f_ctxdisp));
48975fd0b74Schristos       break;
49075fd0b74Schristos     case MT_OPERAND_DUP :
49175fd0b74Schristos       errmsg = parse_dup (cd, strp, MT_OPERAND_DUP, (unsigned long *) (& fields->f_dup));
49275fd0b74Schristos       break;
49375fd0b74Schristos     case MT_OPERAND_FBDISP :
49475fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBDISP, (unsigned long *) (& fields->f_fbdisp));
49575fd0b74Schristos       break;
49675fd0b74Schristos     case MT_OPERAND_FBINCR :
49775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBINCR, (unsigned long *) (& fields->f_fbincr));
49875fd0b74Schristos       break;
49975fd0b74Schristos     case MT_OPERAND_FRDR :
50075fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_dr);
50175fd0b74Schristos       break;
50275fd0b74Schristos     case MT_OPERAND_FRDRRR :
50375fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_drrr);
50475fd0b74Schristos       break;
50575fd0b74Schristos     case MT_OPERAND_FRSR1 :
50675fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr1);
50775fd0b74Schristos       break;
50875fd0b74Schristos     case MT_OPERAND_FRSR2 :
50975fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr2);
51075fd0b74Schristos       break;
51175fd0b74Schristos     case MT_OPERAND_ID :
51275fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ID, (unsigned long *) (& fields->f_id));
51375fd0b74Schristos       break;
51475fd0b74Schristos     case MT_OPERAND_IMM16 :
51575fd0b74Schristos       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16, (long *) (& fields->f_imm16s));
51675fd0b74Schristos       break;
51775fd0b74Schristos     case MT_OPERAND_IMM16L :
51875fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_IMM16L, (unsigned long *) (& fields->f_imm16l));
51975fd0b74Schristos       break;
52075fd0b74Schristos     case MT_OPERAND_IMM16O :
52175fd0b74Schristos       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16O, (unsigned long *) (& fields->f_imm16s));
52275fd0b74Schristos       break;
52375fd0b74Schristos     case MT_OPERAND_IMM16Z :
52475fd0b74Schristos       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16Z, (unsigned long *) (& fields->f_imm16u));
52575fd0b74Schristos       break;
52675fd0b74Schristos     case MT_OPERAND_INCAMT :
52775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCAMT, (unsigned long *) (& fields->f_incamt));
52875fd0b74Schristos       break;
52975fd0b74Schristos     case MT_OPERAND_INCR :
53075fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCR, (unsigned long *) (& fields->f_incr));
53175fd0b74Schristos       break;
53275fd0b74Schristos     case MT_OPERAND_LENGTH :
53375fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_LENGTH, (unsigned long *) (& fields->f_length));
53475fd0b74Schristos       break;
53575fd0b74Schristos     case MT_OPERAND_LOOPSIZE :
53675fd0b74Schristos       errmsg = parse_loopsize (cd, strp, MT_OPERAND_LOOPSIZE, (unsigned long *) (& fields->f_loopo));
53775fd0b74Schristos       break;
53875fd0b74Schristos     case MT_OPERAND_MASK :
53975fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK, (unsigned long *) (& fields->f_mask));
54075fd0b74Schristos       break;
54175fd0b74Schristos     case MT_OPERAND_MASK1 :
54275fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK1, (unsigned long *) (& fields->f_mask1));
54375fd0b74Schristos       break;
54475fd0b74Schristos     case MT_OPERAND_MODE :
54575fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MODE, (unsigned long *) (& fields->f_mode));
54675fd0b74Schristos       break;
54775fd0b74Schristos     case MT_OPERAND_PERM :
54875fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_PERM, (unsigned long *) (& fields->f_perm));
54975fd0b74Schristos       break;
55075fd0b74Schristos     case MT_OPERAND_RBBC :
55175fd0b74Schristos       errmsg = parse_rbbc (cd, strp, MT_OPERAND_RBBC, (unsigned long *) (& fields->f_rbbc));
55275fd0b74Schristos       break;
55375fd0b74Schristos     case MT_OPERAND_RC :
55475fd0b74Schristos       errmsg = parse_rc (cd, strp, MT_OPERAND_RC, (unsigned long *) (& fields->f_rc));
55575fd0b74Schristos       break;
55675fd0b74Schristos     case MT_OPERAND_RC1 :
55775fd0b74Schristos       errmsg = parse_rc (cd, strp, MT_OPERAND_RC1, (unsigned long *) (& fields->f_rc1));
55875fd0b74Schristos       break;
55975fd0b74Schristos     case MT_OPERAND_RC2 :
56075fd0b74Schristos       errmsg = parse_rc (cd, strp, MT_OPERAND_RC2, (unsigned long *) (& fields->f_rc2));
56175fd0b74Schristos       break;
56275fd0b74Schristos     case MT_OPERAND_RC3 :
56375fd0b74Schristos       errmsg = parse_rc (cd, strp, MT_OPERAND_RC3, (unsigned long *) (& fields->f_rc3));
56475fd0b74Schristos       break;
56575fd0b74Schristos     case MT_OPERAND_RCNUM :
56675fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RCNUM, (unsigned long *) (& fields->f_rcnum));
56775fd0b74Schristos       break;
56875fd0b74Schristos     case MT_OPERAND_RDA :
56975fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RDA, (unsigned long *) (& fields->f_rda));
57075fd0b74Schristos       break;
57175fd0b74Schristos     case MT_OPERAND_ROWNUM :
57275fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM, (unsigned long *) (& fields->f_rownum));
57375fd0b74Schristos       break;
57475fd0b74Schristos     case MT_OPERAND_ROWNUM1 :
57575fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM1, (unsigned long *) (& fields->f_rownum1));
57675fd0b74Schristos       break;
57775fd0b74Schristos     case MT_OPERAND_ROWNUM2 :
57875fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM2, (unsigned long *) (& fields->f_rownum2));
57975fd0b74Schristos       break;
58075fd0b74Schristos     case MT_OPERAND_SIZE :
58175fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_SIZE, (unsigned long *) (& fields->f_size));
58275fd0b74Schristos       break;
58375fd0b74Schristos     case MT_OPERAND_TYPE :
58475fd0b74Schristos       errmsg = parse_type (cd, strp, MT_OPERAND_TYPE, (unsigned long *) (& fields->f_type));
58575fd0b74Schristos       break;
58675fd0b74Schristos     case MT_OPERAND_WR :
58775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_WR, (unsigned long *) (& fields->f_wr));
58875fd0b74Schristos       break;
58975fd0b74Schristos     case MT_OPERAND_XMODE :
59075fd0b74Schristos       errmsg = parse_xmode (cd, strp, MT_OPERAND_XMODE, (unsigned long *) (& fields->f_xmode));
59175fd0b74Schristos       break;
59275fd0b74Schristos 
59375fd0b74Schristos     default :
59475fd0b74Schristos       /* xgettext:c-format */
595ede78133Schristos       opcodes_error_handler
596ede78133Schristos 	(_("internal error: unrecognized field %d while parsing"),
597ede78133Schristos 	 opindex);
59875fd0b74Schristos       abort ();
59975fd0b74Schristos   }
60075fd0b74Schristos 
60175fd0b74Schristos   return errmsg;
60275fd0b74Schristos }
60375fd0b74Schristos 
60475fd0b74Schristos cgen_parse_fn * const mt_cgen_parse_handlers[] =
60575fd0b74Schristos {
60675fd0b74Schristos   parse_insn_normal,
60775fd0b74Schristos };
60875fd0b74Schristos 
60975fd0b74Schristos void
mt_cgen_init_asm(CGEN_CPU_DESC cd)61075fd0b74Schristos mt_cgen_init_asm (CGEN_CPU_DESC cd)
61175fd0b74Schristos {
61275fd0b74Schristos   mt_cgen_init_opcode_table (cd);
61375fd0b74Schristos   mt_cgen_init_ibld_table (cd);
61475fd0b74Schristos   cd->parse_handlers = & mt_cgen_parse_handlers[0];
61575fd0b74Schristos   cd->parse_operand = mt_cgen_parse_operand;
61675fd0b74Schristos #ifdef CGEN_ASM_INIT_HOOK
61775fd0b74Schristos CGEN_ASM_INIT_HOOK
61875fd0b74Schristos #endif
61975fd0b74Schristos }
62075fd0b74Schristos 
62175fd0b74Schristos 
62275fd0b74Schristos 
62375fd0b74Schristos /* Regex construction routine.
62475fd0b74Schristos 
62575fd0b74Schristos    This translates an opcode syntax string into a regex string,
62675fd0b74Schristos    by replacing any non-character syntax element (such as an
62775fd0b74Schristos    opcode) with the pattern '.*'
62875fd0b74Schristos 
62975fd0b74Schristos    It then compiles the regex and stores it in the opcode, for
63075fd0b74Schristos    later use by mt_cgen_assemble_insn
63175fd0b74Schristos 
63275fd0b74Schristos    Returns NULL for success, an error message for failure.  */
63375fd0b74Schristos 
63475fd0b74Schristos char *
mt_cgen_build_insn_regex(CGEN_INSN * insn)63575fd0b74Schristos mt_cgen_build_insn_regex (CGEN_INSN *insn)
63675fd0b74Schristos {
63775fd0b74Schristos   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
63875fd0b74Schristos   const char *mnem = CGEN_INSN_MNEMONIC (insn);
63975fd0b74Schristos   char rxbuf[CGEN_MAX_RX_ELEMENTS];
64075fd0b74Schristos   char *rx = rxbuf;
64175fd0b74Schristos   const CGEN_SYNTAX_CHAR_TYPE *syn;
64275fd0b74Schristos   int reg_err;
64375fd0b74Schristos 
64475fd0b74Schristos   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
64575fd0b74Schristos 
64675fd0b74Schristos   /* Mnemonics come first in the syntax string.  */
64775fd0b74Schristos   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
64875fd0b74Schristos     return _("missing mnemonic in syntax string");
64975fd0b74Schristos   ++syn;
65075fd0b74Schristos 
65175fd0b74Schristos   /* Generate a case sensitive regular expression that emulates case
65275fd0b74Schristos      insensitive matching in the "C" locale.  We cannot generate a case
65375fd0b74Schristos      insensitive regular expression because in Turkish locales, 'i' and 'I'
65475fd0b74Schristos      are not equal modulo case conversion.  */
65575fd0b74Schristos 
65675fd0b74Schristos   /* Copy the literal mnemonic out of the insn.  */
65775fd0b74Schristos   for (; *mnem; mnem++)
65875fd0b74Schristos     {
65975fd0b74Schristos       char c = *mnem;
66075fd0b74Schristos 
66175fd0b74Schristos       if (ISALPHA (c))
66275fd0b74Schristos 	{
66375fd0b74Schristos 	  *rx++ = '[';
66475fd0b74Schristos 	  *rx++ = TOLOWER (c);
66575fd0b74Schristos 	  *rx++ = TOUPPER (c);
66675fd0b74Schristos 	  *rx++ = ']';
66775fd0b74Schristos 	}
66875fd0b74Schristos       else
66975fd0b74Schristos 	*rx++ = c;
67075fd0b74Schristos     }
67175fd0b74Schristos 
67275fd0b74Schristos   /* Copy any remaining literals from the syntax string into the rx.  */
67375fd0b74Schristos   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
67475fd0b74Schristos     {
67575fd0b74Schristos       if (CGEN_SYNTAX_CHAR_P (* syn))
67675fd0b74Schristos 	{
67775fd0b74Schristos 	  char c = CGEN_SYNTAX_CHAR (* syn);
67875fd0b74Schristos 
67975fd0b74Schristos 	  switch (c)
68075fd0b74Schristos 	    {
68175fd0b74Schristos 	      /* Escape any regex metacharacters in the syntax.  */
68275fd0b74Schristos 	    case '.': case '[': case '\\':
68375fd0b74Schristos 	    case '*': case '^': case '$':
68475fd0b74Schristos 
68575fd0b74Schristos #ifdef CGEN_ESCAPE_EXTENDED_REGEX
68675fd0b74Schristos 	    case '?': case '{': case '}':
68775fd0b74Schristos 	    case '(': case ')': case '*':
68875fd0b74Schristos 	    case '|': case '+': case ']':
68975fd0b74Schristos #endif
69075fd0b74Schristos 	      *rx++ = '\\';
69175fd0b74Schristos 	      *rx++ = c;
69275fd0b74Schristos 	      break;
69375fd0b74Schristos 
69475fd0b74Schristos 	    default:
69575fd0b74Schristos 	      if (ISALPHA (c))
69675fd0b74Schristos 		{
69775fd0b74Schristos 		  *rx++ = '[';
69875fd0b74Schristos 		  *rx++ = TOLOWER (c);
69975fd0b74Schristos 		  *rx++ = TOUPPER (c);
70075fd0b74Schristos 		  *rx++ = ']';
70175fd0b74Schristos 		}
70275fd0b74Schristos 	      else
70375fd0b74Schristos 		*rx++ = c;
70475fd0b74Schristos 	      break;
70575fd0b74Schristos 	    }
70675fd0b74Schristos 	}
70775fd0b74Schristos       else
70875fd0b74Schristos 	{
70975fd0b74Schristos 	  /* Replace non-syntax fields with globs.  */
71075fd0b74Schristos 	  *rx++ = '.';
71175fd0b74Schristos 	  *rx++ = '*';
71275fd0b74Schristos 	}
71375fd0b74Schristos     }
71475fd0b74Schristos 
71575fd0b74Schristos   /* Trailing whitespace ok.  */
71675fd0b74Schristos   * rx++ = '[';
71775fd0b74Schristos   * rx++ = ' ';
71875fd0b74Schristos   * rx++ = '\t';
71975fd0b74Schristos   * rx++ = ']';
72075fd0b74Schristos   * rx++ = '*';
72175fd0b74Schristos 
72275fd0b74Schristos   /* But anchor it after that.  */
72375fd0b74Schristos   * rx++ = '$';
72475fd0b74Schristos   * rx = '\0';
72575fd0b74Schristos 
72675fd0b74Schristos   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
72775fd0b74Schristos   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
72875fd0b74Schristos 
72975fd0b74Schristos   if (reg_err == 0)
73075fd0b74Schristos     return NULL;
73175fd0b74Schristos   else
73275fd0b74Schristos     {
73375fd0b74Schristos       static char msg[80];
73475fd0b74Schristos 
73575fd0b74Schristos       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
73675fd0b74Schristos       regfree ((regex_t *) CGEN_INSN_RX (insn));
73775fd0b74Schristos       free (CGEN_INSN_RX (insn));
73875fd0b74Schristos       (CGEN_INSN_RX (insn)) = NULL;
73975fd0b74Schristos       return msg;
74075fd0b74Schristos     }
74175fd0b74Schristos }
74275fd0b74Schristos 
74375fd0b74Schristos 
74475fd0b74Schristos /* Default insn parser.
74575fd0b74Schristos 
74675fd0b74Schristos    The syntax string is scanned and operands are parsed and stored in FIELDS.
74775fd0b74Schristos    Relocs are queued as we go via other callbacks.
74875fd0b74Schristos 
74975fd0b74Schristos    ??? Note that this is currently an all-or-nothing parser.  If we fail to
75075fd0b74Schristos    parse the instruction, we return 0 and the caller will start over from
75175fd0b74Schristos    the beginning.  Backtracking will be necessary in parsing subexpressions,
75275fd0b74Schristos    but that can be handled there.  Not handling backtracking here may get
75375fd0b74Schristos    expensive in the case of the m68k.  Deal with later.
75475fd0b74Schristos 
75575fd0b74Schristos    Returns NULL for success, an error message for failure.  */
75675fd0b74Schristos 
75775fd0b74Schristos static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)75875fd0b74Schristos parse_insn_normal (CGEN_CPU_DESC cd,
75975fd0b74Schristos 		   const CGEN_INSN *insn,
76075fd0b74Schristos 		   const char **strp,
76175fd0b74Schristos 		   CGEN_FIELDS *fields)
76275fd0b74Schristos {
76375fd0b74Schristos   /* ??? Runtime added insns not handled yet.  */
76475fd0b74Schristos   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
76575fd0b74Schristos   const char *str = *strp;
76675fd0b74Schristos   const char *errmsg;
76775fd0b74Schristos   const char *p;
76875fd0b74Schristos   const CGEN_SYNTAX_CHAR_TYPE * syn;
76975fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
77075fd0b74Schristos   /* FIXME: wip */
77175fd0b74Schristos   int past_opcode_p;
77275fd0b74Schristos #endif
77375fd0b74Schristos 
77475fd0b74Schristos   /* For now we assume the mnemonic is first (there are no leading operands).
77575fd0b74Schristos      We can parse it without needing to set up operand parsing.
77675fd0b74Schristos      GAS's input scrubber will ensure mnemonics are lowercase, but we may
77775fd0b74Schristos      not be called from GAS.  */
77875fd0b74Schristos   p = CGEN_INSN_MNEMONIC (insn);
77975fd0b74Schristos   while (*p && TOLOWER (*p) == TOLOWER (*str))
78075fd0b74Schristos     ++p, ++str;
78175fd0b74Schristos 
78275fd0b74Schristos   if (* p)
78375fd0b74Schristos     return _("unrecognized instruction");
78475fd0b74Schristos 
78575fd0b74Schristos #ifndef CGEN_MNEMONIC_OPERANDS
78675fd0b74Schristos   if (* str && ! ISSPACE (* str))
78775fd0b74Schristos     return _("unrecognized instruction");
78875fd0b74Schristos #endif
78975fd0b74Schristos 
79075fd0b74Schristos   CGEN_INIT_PARSE (cd);
79175fd0b74Schristos   cgen_init_parse_operand (cd);
79275fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
79375fd0b74Schristos   past_opcode_p = 0;
79475fd0b74Schristos #endif
79575fd0b74Schristos 
79675fd0b74Schristos   /* We don't check for (*str != '\0') here because we want to parse
79775fd0b74Schristos      any trailing fake arguments in the syntax string.  */
79875fd0b74Schristos   syn = CGEN_SYNTAX_STRING (syntax);
79975fd0b74Schristos 
80075fd0b74Schristos   /* Mnemonics come first for now, ensure valid string.  */
80175fd0b74Schristos   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
80275fd0b74Schristos     abort ();
80375fd0b74Schristos 
80475fd0b74Schristos   ++syn;
80575fd0b74Schristos 
80675fd0b74Schristos   while (* syn != 0)
80775fd0b74Schristos     {
80875fd0b74Schristos       /* Non operand chars must match exactly.  */
80975fd0b74Schristos       if (CGEN_SYNTAX_CHAR_P (* syn))
81075fd0b74Schristos 	{
81175fd0b74Schristos 	  /* FIXME: While we allow for non-GAS callers above, we assume the
81275fd0b74Schristos 	     first char after the mnemonic part is a space.  */
81375fd0b74Schristos 	  /* FIXME: We also take inappropriate advantage of the fact that
81475fd0b74Schristos 	     GAS's input scrubber will remove extraneous blanks.  */
81575fd0b74Schristos 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
81675fd0b74Schristos 	    {
81775fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
81875fd0b74Schristos 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
81975fd0b74Schristos 		past_opcode_p = 1;
82075fd0b74Schristos #endif
82175fd0b74Schristos 	      ++ syn;
82275fd0b74Schristos 	      ++ str;
82375fd0b74Schristos 	    }
82475fd0b74Schristos 	  else if (*str)
82575fd0b74Schristos 	    {
82675fd0b74Schristos 	      /* Syntax char didn't match.  Can't be this insn.  */
82775fd0b74Schristos 	      static char msg [80];
82875fd0b74Schristos 
82975fd0b74Schristos 	      /* xgettext:c-format */
83075fd0b74Schristos 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
83175fd0b74Schristos 		       CGEN_SYNTAX_CHAR(*syn), *str);
83275fd0b74Schristos 	      return msg;
83375fd0b74Schristos 	    }
83475fd0b74Schristos 	  else
83575fd0b74Schristos 	    {
83675fd0b74Schristos 	      /* Ran out of input.  */
83775fd0b74Schristos 	      static char msg [80];
83875fd0b74Schristos 
83975fd0b74Schristos 	      /* xgettext:c-format */
84075fd0b74Schristos 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
84175fd0b74Schristos 		       CGEN_SYNTAX_CHAR(*syn));
84275fd0b74Schristos 	      return msg;
84375fd0b74Schristos 	    }
84475fd0b74Schristos 	  continue;
84575fd0b74Schristos 	}
84675fd0b74Schristos 
84775fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
84875fd0b74Schristos       (void) past_opcode_p;
84975fd0b74Schristos #endif
85075fd0b74Schristos       /* We have an operand of some sort.  */
85175fd0b74Schristos       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
85275fd0b74Schristos       if (errmsg)
85375fd0b74Schristos 	return errmsg;
85475fd0b74Schristos 
85575fd0b74Schristos       /* Done with this operand, continue with next one.  */
85675fd0b74Schristos       ++ syn;
85775fd0b74Schristos     }
85875fd0b74Schristos 
85975fd0b74Schristos   /* If we're at the end of the syntax string, we're done.  */
86075fd0b74Schristos   if (* syn == 0)
86175fd0b74Schristos     {
86275fd0b74Schristos       /* FIXME: For the moment we assume a valid `str' can only contain
86375fd0b74Schristos 	 blanks now.  IE: We needn't try again with a longer version of
86475fd0b74Schristos 	 the insn and it is assumed that longer versions of insns appear
86575fd0b74Schristos 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
86675fd0b74Schristos       while (ISSPACE (* str))
86775fd0b74Schristos 	++ str;
86875fd0b74Schristos 
86975fd0b74Schristos       if (* str != '\0')
87075fd0b74Schristos 	return _("junk at end of line"); /* FIXME: would like to include `str' */
87175fd0b74Schristos 
87275fd0b74Schristos       return NULL;
87375fd0b74Schristos     }
87475fd0b74Schristos 
87575fd0b74Schristos   /* We couldn't parse it.  */
87675fd0b74Schristos   return _("unrecognized instruction");
87775fd0b74Schristos }
87875fd0b74Schristos 
87975fd0b74Schristos /* Main entry point.
88075fd0b74Schristos    This routine is called for each instruction to be assembled.
88175fd0b74Schristos    STR points to the insn to be assembled.
88275fd0b74Schristos    We assume all necessary tables have been initialized.
88375fd0b74Schristos    The assembled instruction, less any fixups, is stored in BUF.
88475fd0b74Schristos    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
88575fd0b74Schristos    still needs to be converted to target byte order, otherwise BUF is an array
88675fd0b74Schristos    of bytes in target byte order.
88775fd0b74Schristos    The result is a pointer to the insn's entry in the opcode table,
88875fd0b74Schristos    or NULL if an error occured (an error message will have already been
88975fd0b74Schristos    printed).
89075fd0b74Schristos 
89175fd0b74Schristos    Note that when processing (non-alias) macro-insns,
89275fd0b74Schristos    this function recurses.
89375fd0b74Schristos 
89475fd0b74Schristos    ??? It's possible to make this cpu-independent.
89575fd0b74Schristos    One would have to deal with a few minor things.
89675fd0b74Schristos    At this point in time doing so would be more of a curiosity than useful
89775fd0b74Schristos    [for example this file isn't _that_ big], but keeping the possibility in
89875fd0b74Schristos    mind helps keep the design clean.  */
89975fd0b74Schristos 
90075fd0b74Schristos const CGEN_INSN *
mt_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)90175fd0b74Schristos mt_cgen_assemble_insn (CGEN_CPU_DESC cd,
90275fd0b74Schristos 			   const char *str,
90375fd0b74Schristos 			   CGEN_FIELDS *fields,
90475fd0b74Schristos 			   CGEN_INSN_BYTES_PTR buf,
90575fd0b74Schristos 			   char **errmsg)
90675fd0b74Schristos {
90775fd0b74Schristos   const char *start;
90875fd0b74Schristos   CGEN_INSN_LIST *ilist;
90975fd0b74Schristos   const char *parse_errmsg = NULL;
91075fd0b74Schristos   const char *insert_errmsg = NULL;
91175fd0b74Schristos   int recognized_mnemonic = 0;
91275fd0b74Schristos 
91375fd0b74Schristos   /* Skip leading white space.  */
91475fd0b74Schristos   while (ISSPACE (* str))
91575fd0b74Schristos     ++ str;
91675fd0b74Schristos 
91775fd0b74Schristos   /* The instructions are stored in hashed lists.
91875fd0b74Schristos      Get the first in the list.  */
91975fd0b74Schristos   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
92075fd0b74Schristos 
92175fd0b74Schristos   /* Keep looking until we find a match.  */
92275fd0b74Schristos   start = str;
92375fd0b74Schristos   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
92475fd0b74Schristos     {
92575fd0b74Schristos       const CGEN_INSN *insn = ilist->insn;
92675fd0b74Schristos       recognized_mnemonic = 1;
92775fd0b74Schristos 
92875fd0b74Schristos #ifdef CGEN_VALIDATE_INSN_SUPPORTED
92975fd0b74Schristos       /* Not usually needed as unsupported opcodes
93075fd0b74Schristos 	 shouldn't be in the hash lists.  */
93175fd0b74Schristos       /* Is this insn supported by the selected cpu?  */
93275fd0b74Schristos       if (! mt_cgen_insn_supported (cd, insn))
93375fd0b74Schristos 	continue;
93475fd0b74Schristos #endif
93575fd0b74Schristos       /* If the RELAXED attribute is set, this is an insn that shouldn't be
93675fd0b74Schristos 	 chosen immediately.  Instead, it is used during assembler/linker
93775fd0b74Schristos 	 relaxation if possible.  */
93875fd0b74Schristos       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
93975fd0b74Schristos 	continue;
94075fd0b74Schristos 
94175fd0b74Schristos       str = start;
94275fd0b74Schristos 
94375fd0b74Schristos       /* Skip this insn if str doesn't look right lexically.  */
94475fd0b74Schristos       if (CGEN_INSN_RX (insn) != NULL &&
94575fd0b74Schristos 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
94675fd0b74Schristos 	continue;
94775fd0b74Schristos 
94875fd0b74Schristos       /* Allow parse/insert handlers to obtain length of insn.  */
94975fd0b74Schristos       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
95075fd0b74Schristos 
95175fd0b74Schristos       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
95275fd0b74Schristos       if (parse_errmsg != NULL)
95375fd0b74Schristos 	continue;
95475fd0b74Schristos 
95575fd0b74Schristos       /* ??? 0 is passed for `pc'.  */
95675fd0b74Schristos       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
95775fd0b74Schristos 						 (bfd_vma) 0);
95875fd0b74Schristos       if (insert_errmsg != NULL)
95975fd0b74Schristos         continue;
96075fd0b74Schristos 
96175fd0b74Schristos       /* It is up to the caller to actually output the insn and any
96275fd0b74Schristos          queued relocs.  */
96375fd0b74Schristos       return insn;
96475fd0b74Schristos     }
96575fd0b74Schristos 
96675fd0b74Schristos   {
96775fd0b74Schristos     static char errbuf[150];
96875fd0b74Schristos     const char *tmp_errmsg;
96975fd0b74Schristos #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
97075fd0b74Schristos #define be_verbose 1
97175fd0b74Schristos #else
97275fd0b74Schristos #define be_verbose 0
97375fd0b74Schristos #endif
97475fd0b74Schristos 
97575fd0b74Schristos     if (be_verbose)
97675fd0b74Schristos       {
97775fd0b74Schristos 	/* If requesting verbose error messages, use insert_errmsg.
97875fd0b74Schristos 	   Failing that, use parse_errmsg.  */
97975fd0b74Schristos 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
98075fd0b74Schristos 		      parse_errmsg ? parse_errmsg :
98175fd0b74Schristos 		      recognized_mnemonic ?
98275fd0b74Schristos 		      _("unrecognized form of instruction") :
98375fd0b74Schristos 		      _("unrecognized instruction"));
98475fd0b74Schristos 
98575fd0b74Schristos 	if (strlen (start) > 50)
98675fd0b74Schristos 	  /* xgettext:c-format */
98775fd0b74Schristos 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
98875fd0b74Schristos 	else
98975fd0b74Schristos 	  /* xgettext:c-format */
99075fd0b74Schristos 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
99175fd0b74Schristos       }
99275fd0b74Schristos     else
99375fd0b74Schristos       {
99475fd0b74Schristos 	if (strlen (start) > 50)
99575fd0b74Schristos 	  /* xgettext:c-format */
99675fd0b74Schristos 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
99775fd0b74Schristos 	else
99875fd0b74Schristos 	  /* xgettext:c-format */
99975fd0b74Schristos 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
100075fd0b74Schristos       }
100175fd0b74Schristos 
100275fd0b74Schristos     *errmsg = errbuf;
100375fd0b74Schristos     return NULL;
100475fd0b74Schristos   }
100575fd0b74Schristos }
1006