xref: /netbsd-src/external/gpl3/binutils.old/dist/opcodes/m32r-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 "m32r-desc.h"
3675fd0b74Schristos #include "m32r-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 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
5475fd0b74Schristos 
5575fd0b74Schristos /* Handle '#' prefixes (i.e. skip over them).  */
5675fd0b74Schristos 
5775fd0b74Schristos static const char *
parse_hash(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,const char ** strp,int opindex ATTRIBUTE_UNUSED,long * valuep ATTRIBUTE_UNUSED)5875fd0b74Schristos parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
5975fd0b74Schristos 	    const char **strp,
6075fd0b74Schristos 	    int opindex ATTRIBUTE_UNUSED,
6175fd0b74Schristos 	    long *valuep ATTRIBUTE_UNUSED)
6275fd0b74Schristos {
6375fd0b74Schristos   if (**strp == '#')
6475fd0b74Schristos     ++*strp;
6575fd0b74Schristos   return NULL;
6675fd0b74Schristos }
6775fd0b74Schristos 
6875fd0b74Schristos /* Handle shigh(), high().  */
6975fd0b74Schristos 
7075fd0b74Schristos static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)7175fd0b74Schristos parse_hi16 (CGEN_CPU_DESC cd,
7275fd0b74Schristos 	    const char **strp,
7375fd0b74Schristos 	    int opindex,
7475fd0b74Schristos 	    unsigned long *valuep)
7575fd0b74Schristos {
7675fd0b74Schristos   const char *errmsg;
7775fd0b74Schristos   enum cgen_parse_operand_result result_type;
7875fd0b74Schristos   bfd_vma value;
7975fd0b74Schristos 
8075fd0b74Schristos   if (**strp == '#')
8175fd0b74Schristos     ++*strp;
8275fd0b74Schristos 
8375fd0b74Schristos   if (strncasecmp (*strp, "high(", 5) == 0)
8475fd0b74Schristos     {
8575fd0b74Schristos       *strp += 5;
8675fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO,
8775fd0b74Schristos 				   & result_type, & value);
8875fd0b74Schristos       if (**strp != ')')
8975fd0b74Schristos 	return MISSING_CLOSING_PARENTHESIS;
9075fd0b74Schristos       ++*strp;
9175fd0b74Schristos       if (errmsg == NULL
9275fd0b74Schristos 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
9375fd0b74Schristos 	{
9475fd0b74Schristos 	  value >>= 16;
9575fd0b74Schristos 	  value &= 0xffff;
9675fd0b74Schristos 	}
9775fd0b74Schristos       *valuep = value;
9875fd0b74Schristos       return errmsg;
9975fd0b74Schristos     }
10075fd0b74Schristos   else if (strncasecmp (*strp, "shigh(", 6) == 0)
10175fd0b74Schristos     {
10275fd0b74Schristos       *strp += 6;
10375fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO,
10475fd0b74Schristos 				   & result_type, & value);
10575fd0b74Schristos       if (**strp != ')')
10675fd0b74Schristos 	return MISSING_CLOSING_PARENTHESIS;
10775fd0b74Schristos       ++*strp;
10875fd0b74Schristos       if (errmsg == NULL
10975fd0b74Schristos 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
11075fd0b74Schristos 	{
11175fd0b74Schristos 	  value += 0x8000;
11275fd0b74Schristos 	  value >>= 16;
11375fd0b74Schristos 	  value &= 0xffff;
11475fd0b74Schristos 	}
11575fd0b74Schristos       *valuep = value;
11675fd0b74Schristos       return errmsg;
11775fd0b74Schristos     }
11875fd0b74Schristos 
11975fd0b74Schristos   return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
12075fd0b74Schristos }
12175fd0b74Schristos 
12275fd0b74Schristos /* Handle low() in a signed context.  Also handle sda().
12375fd0b74Schristos    The signedness of the value doesn't matter to low(), but this also
12475fd0b74Schristos    handles the case where low() isn't present.  */
12575fd0b74Schristos 
12675fd0b74Schristos static const char *
parse_slo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)12775fd0b74Schristos parse_slo16 (CGEN_CPU_DESC cd,
12875fd0b74Schristos 	     const char ** strp,
12975fd0b74Schristos 	     int opindex,
13075fd0b74Schristos 	     long * valuep)
13175fd0b74Schristos {
13275fd0b74Schristos   const char *errmsg;
13375fd0b74Schristos   enum cgen_parse_operand_result result_type;
13475fd0b74Schristos   bfd_vma value;
13575fd0b74Schristos 
13675fd0b74Schristos   if (**strp == '#')
13775fd0b74Schristos     ++*strp;
13875fd0b74Schristos 
13975fd0b74Schristos   if (strncasecmp (*strp, "low(", 4) == 0)
14075fd0b74Schristos     {
14175fd0b74Schristos       *strp += 4;
14275fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
14375fd0b74Schristos 				   & result_type, & value);
14475fd0b74Schristos       if (**strp != ')')
14575fd0b74Schristos 	return MISSING_CLOSING_PARENTHESIS;
14675fd0b74Schristos       ++*strp;
14775fd0b74Schristos       if (errmsg == NULL
14875fd0b74Schristos 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
14975fd0b74Schristos 	value = ((value & 0xffff) ^ 0x8000) - 0x8000;
15075fd0b74Schristos       *valuep = value;
15175fd0b74Schristos       return errmsg;
15275fd0b74Schristos     }
15375fd0b74Schristos 
15475fd0b74Schristos   if (strncasecmp (*strp, "sda(", 4) == 0)
15575fd0b74Schristos     {
15675fd0b74Schristos       *strp += 4;
15775fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16,
15875fd0b74Schristos 				   NULL, & value);
15975fd0b74Schristos       if (**strp != ')')
16075fd0b74Schristos 	return MISSING_CLOSING_PARENTHESIS;
16175fd0b74Schristos       ++*strp;
16275fd0b74Schristos       *valuep = value;
16375fd0b74Schristos       return errmsg;
16475fd0b74Schristos     }
16575fd0b74Schristos 
16675fd0b74Schristos   return cgen_parse_signed_integer (cd, strp, opindex, valuep);
16775fd0b74Schristos }
16875fd0b74Schristos 
16975fd0b74Schristos /* Handle low() in an unsigned context.
17075fd0b74Schristos    The signedness of the value doesn't matter to low(), but this also
17175fd0b74Schristos    handles the case where low() isn't present.  */
17275fd0b74Schristos 
17375fd0b74Schristos static const char *
parse_ulo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)17475fd0b74Schristos parse_ulo16 (CGEN_CPU_DESC cd,
17575fd0b74Schristos 	     const char **strp,
17675fd0b74Schristos 	     int opindex,
17775fd0b74Schristos 	     unsigned long *valuep)
17875fd0b74Schristos {
17975fd0b74Schristos   const char *errmsg;
18075fd0b74Schristos   enum cgen_parse_operand_result result_type;
18175fd0b74Schristos   bfd_vma value;
18275fd0b74Schristos 
18375fd0b74Schristos   if (**strp == '#')
18475fd0b74Schristos     ++*strp;
18575fd0b74Schristos 
18675fd0b74Schristos   if (strncasecmp (*strp, "low(", 4) == 0)
18775fd0b74Schristos     {
18875fd0b74Schristos       *strp += 4;
18975fd0b74Schristos       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
19075fd0b74Schristos 				   & result_type, & value);
19175fd0b74Schristos       if (**strp != ')')
19275fd0b74Schristos 	return MISSING_CLOSING_PARENTHESIS;
19375fd0b74Schristos       ++*strp;
19475fd0b74Schristos       if (errmsg == NULL
19575fd0b74Schristos 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
19675fd0b74Schristos 	value &= 0xffff;
19775fd0b74Schristos       *valuep = value;
19875fd0b74Schristos       return errmsg;
19975fd0b74Schristos     }
20075fd0b74Schristos 
20175fd0b74Schristos   return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
20275fd0b74Schristos }
20375fd0b74Schristos 
20475fd0b74Schristos /* -- */
20575fd0b74Schristos 
20675fd0b74Schristos const char * m32r_cgen_parse_operand
20775fd0b74Schristos   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
20875fd0b74Schristos 
20975fd0b74Schristos /* Main entry point for operand parsing.
21075fd0b74Schristos 
21175fd0b74Schristos    This function is basically just a big switch statement.  Earlier versions
21275fd0b74Schristos    used tables to look up the function to use, but
21375fd0b74Schristos    - if the table contains both assembler and disassembler functions then
21475fd0b74Schristos      the disassembler contains much of the assembler and vice-versa,
21575fd0b74Schristos    - there's a lot of inlining possibilities as things grow,
21675fd0b74Schristos    - using a switch statement avoids the function call overhead.
21775fd0b74Schristos 
21875fd0b74Schristos    This function could be moved into `parse_insn_normal', but keeping it
21975fd0b74Schristos    separate makes clear the interface between `parse_insn_normal' and each of
22075fd0b74Schristos    the handlers.  */
22175fd0b74Schristos 
22275fd0b74Schristos const char *
m32r_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)22375fd0b74Schristos m32r_cgen_parse_operand (CGEN_CPU_DESC cd,
22475fd0b74Schristos 			   int opindex,
22575fd0b74Schristos 			   const char ** strp,
22675fd0b74Schristos 			   CGEN_FIELDS * fields)
22775fd0b74Schristos {
22875fd0b74Schristos   const char * errmsg = NULL;
22975fd0b74Schristos   /* Used by scalar operands that still need to be parsed.  */
23075fd0b74Schristos   long junk ATTRIBUTE_UNUSED;
23175fd0b74Schristos 
23275fd0b74Schristos   switch (opindex)
23375fd0b74Schristos     {
23475fd0b74Schristos     case M32R_OPERAND_ACC :
23575fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_acc);
23675fd0b74Schristos       break;
23775fd0b74Schristos     case M32R_OPERAND_ACCD :
23875fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accd);
23975fd0b74Schristos       break;
24075fd0b74Schristos     case M32R_OPERAND_ACCS :
24175fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accs);
24275fd0b74Schristos       break;
24375fd0b74Schristos     case M32R_OPERAND_DCR :
24475fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r1);
24575fd0b74Schristos       break;
24675fd0b74Schristos     case M32R_OPERAND_DISP16 :
24775fd0b74Schristos       {
24875fd0b74Schristos         bfd_vma value = 0;
24975fd0b74Schristos         errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP16, 0, NULL,  & value);
25075fd0b74Schristos         fields->f_disp16 = value;
25175fd0b74Schristos       }
25275fd0b74Schristos       break;
25375fd0b74Schristos     case M32R_OPERAND_DISP24 :
25475fd0b74Schristos       {
25575fd0b74Schristos         bfd_vma value = 0;
25675fd0b74Schristos         errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP24, 0, NULL,  & value);
25775fd0b74Schristos         fields->f_disp24 = value;
25875fd0b74Schristos       }
25975fd0b74Schristos       break;
26075fd0b74Schristos     case M32R_OPERAND_DISP8 :
26175fd0b74Schristos       {
26275fd0b74Schristos         bfd_vma value = 0;
26375fd0b74Schristos         errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP8, 0, NULL,  & value);
26475fd0b74Schristos         fields->f_disp8 = value;
26575fd0b74Schristos       }
26675fd0b74Schristos       break;
26775fd0b74Schristos     case M32R_OPERAND_DR :
26875fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
26975fd0b74Schristos       break;
27075fd0b74Schristos     case M32R_OPERAND_HASH :
27175fd0b74Schristos       errmsg = parse_hash (cd, strp, M32R_OPERAND_HASH, (long *) (& junk));
27275fd0b74Schristos       break;
27375fd0b74Schristos     case M32R_OPERAND_HI16 :
27475fd0b74Schristos       errmsg = parse_hi16 (cd, strp, M32R_OPERAND_HI16, (unsigned long *) (& fields->f_hi16));
27575fd0b74Schristos       break;
27675fd0b74Schristos     case M32R_OPERAND_IMM1 :
27775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_IMM1, (unsigned long *) (& fields->f_imm1));
27875fd0b74Schristos       break;
27975fd0b74Schristos     case M32R_OPERAND_SCR :
28075fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r2);
28175fd0b74Schristos       break;
28275fd0b74Schristos     case M32R_OPERAND_SIMM16 :
28375fd0b74Schristos       errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM16, (long *) (& fields->f_simm16));
28475fd0b74Schristos       break;
28575fd0b74Schristos     case M32R_OPERAND_SIMM8 :
28675fd0b74Schristos       errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM8, (long *) (& fields->f_simm8));
28775fd0b74Schristos       break;
28875fd0b74Schristos     case M32R_OPERAND_SLO16 :
28975fd0b74Schristos       errmsg = parse_slo16 (cd, strp, M32R_OPERAND_SLO16, (long *) (& fields->f_simm16));
29075fd0b74Schristos       break;
29175fd0b74Schristos     case M32R_OPERAND_SR :
29275fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
29375fd0b74Schristos       break;
29475fd0b74Schristos     case M32R_OPERAND_SRC1 :
29575fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
29675fd0b74Schristos       break;
29775fd0b74Schristos     case M32R_OPERAND_SRC2 :
29875fd0b74Schristos       errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
29975fd0b74Schristos       break;
30075fd0b74Schristos     case M32R_OPERAND_UIMM16 :
30175fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
30275fd0b74Schristos       break;
30375fd0b74Schristos     case M32R_OPERAND_UIMM24 :
30475fd0b74Schristos       {
30575fd0b74Schristos         bfd_vma value = 0;
30675fd0b74Schristos         errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_UIMM24, 0, NULL,  & value);
30775fd0b74Schristos         fields->f_uimm24 = value;
30875fd0b74Schristos       }
30975fd0b74Schristos       break;
31075fd0b74Schristos     case M32R_OPERAND_UIMM3 :
31175fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM3, (unsigned long *) (& fields->f_uimm3));
31275fd0b74Schristos       break;
31375fd0b74Schristos     case M32R_OPERAND_UIMM4 :
31475fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4));
31575fd0b74Schristos       break;
31675fd0b74Schristos     case M32R_OPERAND_UIMM5 :
31775fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM5, (unsigned long *) (& fields->f_uimm5));
31875fd0b74Schristos       break;
31975fd0b74Schristos     case M32R_OPERAND_UIMM8 :
32075fd0b74Schristos       errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8));
32175fd0b74Schristos       break;
32275fd0b74Schristos     case M32R_OPERAND_ULO16 :
32375fd0b74Schristos       errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, (unsigned long *) (& fields->f_uimm16));
32475fd0b74Schristos       break;
32575fd0b74Schristos 
32675fd0b74Schristos     default :
32775fd0b74Schristos       /* xgettext:c-format */
328ede78133Schristos       opcodes_error_handler
329ede78133Schristos 	(_("internal error: unrecognized field %d while parsing"),
330ede78133Schristos 	 opindex);
33175fd0b74Schristos       abort ();
33275fd0b74Schristos   }
33375fd0b74Schristos 
33475fd0b74Schristos   return errmsg;
33575fd0b74Schristos }
33675fd0b74Schristos 
33775fd0b74Schristos cgen_parse_fn * const m32r_cgen_parse_handlers[] =
33875fd0b74Schristos {
33975fd0b74Schristos   parse_insn_normal,
34075fd0b74Schristos };
34175fd0b74Schristos 
34275fd0b74Schristos void
m32r_cgen_init_asm(CGEN_CPU_DESC cd)34375fd0b74Schristos m32r_cgen_init_asm (CGEN_CPU_DESC cd)
34475fd0b74Schristos {
34575fd0b74Schristos   m32r_cgen_init_opcode_table (cd);
34675fd0b74Schristos   m32r_cgen_init_ibld_table (cd);
34775fd0b74Schristos   cd->parse_handlers = & m32r_cgen_parse_handlers[0];
34875fd0b74Schristos   cd->parse_operand = m32r_cgen_parse_operand;
34975fd0b74Schristos #ifdef CGEN_ASM_INIT_HOOK
35075fd0b74Schristos CGEN_ASM_INIT_HOOK
35175fd0b74Schristos #endif
35275fd0b74Schristos }
35375fd0b74Schristos 
35475fd0b74Schristos 
35575fd0b74Schristos 
35675fd0b74Schristos /* Regex construction routine.
35775fd0b74Schristos 
35875fd0b74Schristos    This translates an opcode syntax string into a regex string,
35975fd0b74Schristos    by replacing any non-character syntax element (such as an
36075fd0b74Schristos    opcode) with the pattern '.*'
36175fd0b74Schristos 
36275fd0b74Schristos    It then compiles the regex and stores it in the opcode, for
36375fd0b74Schristos    later use by m32r_cgen_assemble_insn
36475fd0b74Schristos 
36575fd0b74Schristos    Returns NULL for success, an error message for failure.  */
36675fd0b74Schristos 
36775fd0b74Schristos char *
m32r_cgen_build_insn_regex(CGEN_INSN * insn)36875fd0b74Schristos m32r_cgen_build_insn_regex (CGEN_INSN *insn)
36975fd0b74Schristos {
37075fd0b74Schristos   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
37175fd0b74Schristos   const char *mnem = CGEN_INSN_MNEMONIC (insn);
37275fd0b74Schristos   char rxbuf[CGEN_MAX_RX_ELEMENTS];
37375fd0b74Schristos   char *rx = rxbuf;
37475fd0b74Schristos   const CGEN_SYNTAX_CHAR_TYPE *syn;
37575fd0b74Schristos   int reg_err;
37675fd0b74Schristos 
37775fd0b74Schristos   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
37875fd0b74Schristos 
37975fd0b74Schristos   /* Mnemonics come first in the syntax string.  */
38075fd0b74Schristos   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
38175fd0b74Schristos     return _("missing mnemonic in syntax string");
38275fd0b74Schristos   ++syn;
38375fd0b74Schristos 
38475fd0b74Schristos   /* Generate a case sensitive regular expression that emulates case
38575fd0b74Schristos      insensitive matching in the "C" locale.  We cannot generate a case
38675fd0b74Schristos      insensitive regular expression because in Turkish locales, 'i' and 'I'
38775fd0b74Schristos      are not equal modulo case conversion.  */
38875fd0b74Schristos 
38975fd0b74Schristos   /* Copy the literal mnemonic out of the insn.  */
39075fd0b74Schristos   for (; *mnem; mnem++)
39175fd0b74Schristos     {
39275fd0b74Schristos       char c = *mnem;
39375fd0b74Schristos 
39475fd0b74Schristos       if (ISALPHA (c))
39575fd0b74Schristos 	{
39675fd0b74Schristos 	  *rx++ = '[';
39775fd0b74Schristos 	  *rx++ = TOLOWER (c);
39875fd0b74Schristos 	  *rx++ = TOUPPER (c);
39975fd0b74Schristos 	  *rx++ = ']';
40075fd0b74Schristos 	}
40175fd0b74Schristos       else
40275fd0b74Schristos 	*rx++ = c;
40375fd0b74Schristos     }
40475fd0b74Schristos 
40575fd0b74Schristos   /* Copy any remaining literals from the syntax string into the rx.  */
40675fd0b74Schristos   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
40775fd0b74Schristos     {
40875fd0b74Schristos       if (CGEN_SYNTAX_CHAR_P (* syn))
40975fd0b74Schristos 	{
41075fd0b74Schristos 	  char c = CGEN_SYNTAX_CHAR (* syn);
41175fd0b74Schristos 
41275fd0b74Schristos 	  switch (c)
41375fd0b74Schristos 	    {
41475fd0b74Schristos 	      /* Escape any regex metacharacters in the syntax.  */
41575fd0b74Schristos 	    case '.': case '[': case '\\':
41675fd0b74Schristos 	    case '*': case '^': case '$':
41775fd0b74Schristos 
41875fd0b74Schristos #ifdef CGEN_ESCAPE_EXTENDED_REGEX
41975fd0b74Schristos 	    case '?': case '{': case '}':
42075fd0b74Schristos 	    case '(': case ')': case '*':
42175fd0b74Schristos 	    case '|': case '+': case ']':
42275fd0b74Schristos #endif
42375fd0b74Schristos 	      *rx++ = '\\';
42475fd0b74Schristos 	      *rx++ = c;
42575fd0b74Schristos 	      break;
42675fd0b74Schristos 
42775fd0b74Schristos 	    default:
42875fd0b74Schristos 	      if (ISALPHA (c))
42975fd0b74Schristos 		{
43075fd0b74Schristos 		  *rx++ = '[';
43175fd0b74Schristos 		  *rx++ = TOLOWER (c);
43275fd0b74Schristos 		  *rx++ = TOUPPER (c);
43375fd0b74Schristos 		  *rx++ = ']';
43475fd0b74Schristos 		}
43575fd0b74Schristos 	      else
43675fd0b74Schristos 		*rx++ = c;
43775fd0b74Schristos 	      break;
43875fd0b74Schristos 	    }
43975fd0b74Schristos 	}
44075fd0b74Schristos       else
44175fd0b74Schristos 	{
44275fd0b74Schristos 	  /* Replace non-syntax fields with globs.  */
44375fd0b74Schristos 	  *rx++ = '.';
44475fd0b74Schristos 	  *rx++ = '*';
44575fd0b74Schristos 	}
44675fd0b74Schristos     }
44775fd0b74Schristos 
44875fd0b74Schristos   /* Trailing whitespace ok.  */
44975fd0b74Schristos   * rx++ = '[';
45075fd0b74Schristos   * rx++ = ' ';
45175fd0b74Schristos   * rx++ = '\t';
45275fd0b74Schristos   * rx++ = ']';
45375fd0b74Schristos   * rx++ = '*';
45475fd0b74Schristos 
45575fd0b74Schristos   /* But anchor it after that.  */
45675fd0b74Schristos   * rx++ = '$';
45775fd0b74Schristos   * rx = '\0';
45875fd0b74Schristos 
45975fd0b74Schristos   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
46075fd0b74Schristos   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
46175fd0b74Schristos 
46275fd0b74Schristos   if (reg_err == 0)
46375fd0b74Schristos     return NULL;
46475fd0b74Schristos   else
46575fd0b74Schristos     {
46675fd0b74Schristos       static char msg[80];
46775fd0b74Schristos 
46875fd0b74Schristos       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
46975fd0b74Schristos       regfree ((regex_t *) CGEN_INSN_RX (insn));
47075fd0b74Schristos       free (CGEN_INSN_RX (insn));
47175fd0b74Schristos       (CGEN_INSN_RX (insn)) = NULL;
47275fd0b74Schristos       return msg;
47375fd0b74Schristos     }
47475fd0b74Schristos }
47575fd0b74Schristos 
47675fd0b74Schristos 
47775fd0b74Schristos /* Default insn parser.
47875fd0b74Schristos 
47975fd0b74Schristos    The syntax string is scanned and operands are parsed and stored in FIELDS.
48075fd0b74Schristos    Relocs are queued as we go via other callbacks.
48175fd0b74Schristos 
48275fd0b74Schristos    ??? Note that this is currently an all-or-nothing parser.  If we fail to
48375fd0b74Schristos    parse the instruction, we return 0 and the caller will start over from
48475fd0b74Schristos    the beginning.  Backtracking will be necessary in parsing subexpressions,
48575fd0b74Schristos    but that can be handled there.  Not handling backtracking here may get
48675fd0b74Schristos    expensive in the case of the m68k.  Deal with later.
48775fd0b74Schristos 
48875fd0b74Schristos    Returns NULL for success, an error message for failure.  */
48975fd0b74Schristos 
49075fd0b74Schristos static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)49175fd0b74Schristos parse_insn_normal (CGEN_CPU_DESC cd,
49275fd0b74Schristos 		   const CGEN_INSN *insn,
49375fd0b74Schristos 		   const char **strp,
49475fd0b74Schristos 		   CGEN_FIELDS *fields)
49575fd0b74Schristos {
49675fd0b74Schristos   /* ??? Runtime added insns not handled yet.  */
49775fd0b74Schristos   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
49875fd0b74Schristos   const char *str = *strp;
49975fd0b74Schristos   const char *errmsg;
50075fd0b74Schristos   const char *p;
50175fd0b74Schristos   const CGEN_SYNTAX_CHAR_TYPE * syn;
50275fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
50375fd0b74Schristos   /* FIXME: wip */
50475fd0b74Schristos   int past_opcode_p;
50575fd0b74Schristos #endif
50675fd0b74Schristos 
50775fd0b74Schristos   /* For now we assume the mnemonic is first (there are no leading operands).
50875fd0b74Schristos      We can parse it without needing to set up operand parsing.
50975fd0b74Schristos      GAS's input scrubber will ensure mnemonics are lowercase, but we may
51075fd0b74Schristos      not be called from GAS.  */
51175fd0b74Schristos   p = CGEN_INSN_MNEMONIC (insn);
51275fd0b74Schristos   while (*p && TOLOWER (*p) == TOLOWER (*str))
51375fd0b74Schristos     ++p, ++str;
51475fd0b74Schristos 
51575fd0b74Schristos   if (* p)
51675fd0b74Schristos     return _("unrecognized instruction");
51775fd0b74Schristos 
51875fd0b74Schristos #ifndef CGEN_MNEMONIC_OPERANDS
51975fd0b74Schristos   if (* str && ! ISSPACE (* str))
52075fd0b74Schristos     return _("unrecognized instruction");
52175fd0b74Schristos #endif
52275fd0b74Schristos 
52375fd0b74Schristos   CGEN_INIT_PARSE (cd);
52475fd0b74Schristos   cgen_init_parse_operand (cd);
52575fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
52675fd0b74Schristos   past_opcode_p = 0;
52775fd0b74Schristos #endif
52875fd0b74Schristos 
52975fd0b74Schristos   /* We don't check for (*str != '\0') here because we want to parse
53075fd0b74Schristos      any trailing fake arguments in the syntax string.  */
53175fd0b74Schristos   syn = CGEN_SYNTAX_STRING (syntax);
53275fd0b74Schristos 
53375fd0b74Schristos   /* Mnemonics come first for now, ensure valid string.  */
53475fd0b74Schristos   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
53575fd0b74Schristos     abort ();
53675fd0b74Schristos 
53775fd0b74Schristos   ++syn;
53875fd0b74Schristos 
53975fd0b74Schristos   while (* syn != 0)
54075fd0b74Schristos     {
54175fd0b74Schristos       /* Non operand chars must match exactly.  */
54275fd0b74Schristos       if (CGEN_SYNTAX_CHAR_P (* syn))
54375fd0b74Schristos 	{
54475fd0b74Schristos 	  /* FIXME: While we allow for non-GAS callers above, we assume the
54575fd0b74Schristos 	     first char after the mnemonic part is a space.  */
54675fd0b74Schristos 	  /* FIXME: We also take inappropriate advantage of the fact that
54775fd0b74Schristos 	     GAS's input scrubber will remove extraneous blanks.  */
54875fd0b74Schristos 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
54975fd0b74Schristos 	    {
55075fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
55175fd0b74Schristos 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
55275fd0b74Schristos 		past_opcode_p = 1;
55375fd0b74Schristos #endif
55475fd0b74Schristos 	      ++ syn;
55575fd0b74Schristos 	      ++ str;
55675fd0b74Schristos 	    }
55775fd0b74Schristos 	  else if (*str)
55875fd0b74Schristos 	    {
55975fd0b74Schristos 	      /* Syntax char didn't match.  Can't be this insn.  */
56075fd0b74Schristos 	      static char msg [80];
56175fd0b74Schristos 
56275fd0b74Schristos 	      /* xgettext:c-format */
56375fd0b74Schristos 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
56475fd0b74Schristos 		       CGEN_SYNTAX_CHAR(*syn), *str);
56575fd0b74Schristos 	      return msg;
56675fd0b74Schristos 	    }
56775fd0b74Schristos 	  else
56875fd0b74Schristos 	    {
56975fd0b74Schristos 	      /* Ran out of input.  */
57075fd0b74Schristos 	      static char msg [80];
57175fd0b74Schristos 
57275fd0b74Schristos 	      /* xgettext:c-format */
57375fd0b74Schristos 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
57475fd0b74Schristos 		       CGEN_SYNTAX_CHAR(*syn));
57575fd0b74Schristos 	      return msg;
57675fd0b74Schristos 	    }
57775fd0b74Schristos 	  continue;
57875fd0b74Schristos 	}
57975fd0b74Schristos 
58075fd0b74Schristos #ifdef CGEN_MNEMONIC_OPERANDS
58175fd0b74Schristos       (void) past_opcode_p;
58275fd0b74Schristos #endif
58375fd0b74Schristos       /* We have an operand of some sort.  */
58475fd0b74Schristos       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
58575fd0b74Schristos       if (errmsg)
58675fd0b74Schristos 	return errmsg;
58775fd0b74Schristos 
58875fd0b74Schristos       /* Done with this operand, continue with next one.  */
58975fd0b74Schristos       ++ syn;
59075fd0b74Schristos     }
59175fd0b74Schristos 
59275fd0b74Schristos   /* If we're at the end of the syntax string, we're done.  */
59375fd0b74Schristos   if (* syn == 0)
59475fd0b74Schristos     {
59575fd0b74Schristos       /* FIXME: For the moment we assume a valid `str' can only contain
59675fd0b74Schristos 	 blanks now.  IE: We needn't try again with a longer version of
59775fd0b74Schristos 	 the insn and it is assumed that longer versions of insns appear
59875fd0b74Schristos 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
59975fd0b74Schristos       while (ISSPACE (* str))
60075fd0b74Schristos 	++ str;
60175fd0b74Schristos 
60275fd0b74Schristos       if (* str != '\0')
60375fd0b74Schristos 	return _("junk at end of line"); /* FIXME: would like to include `str' */
60475fd0b74Schristos 
60575fd0b74Schristos       return NULL;
60675fd0b74Schristos     }
60775fd0b74Schristos 
60875fd0b74Schristos   /* We couldn't parse it.  */
60975fd0b74Schristos   return _("unrecognized instruction");
61075fd0b74Schristos }
61175fd0b74Schristos 
61275fd0b74Schristos /* Main entry point.
61375fd0b74Schristos    This routine is called for each instruction to be assembled.
61475fd0b74Schristos    STR points to the insn to be assembled.
61575fd0b74Schristos    We assume all necessary tables have been initialized.
61675fd0b74Schristos    The assembled instruction, less any fixups, is stored in BUF.
61775fd0b74Schristos    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
61875fd0b74Schristos    still needs to be converted to target byte order, otherwise BUF is an array
61975fd0b74Schristos    of bytes in target byte order.
62075fd0b74Schristos    The result is a pointer to the insn's entry in the opcode table,
62175fd0b74Schristos    or NULL if an error occured (an error message will have already been
62275fd0b74Schristos    printed).
62375fd0b74Schristos 
62475fd0b74Schristos    Note that when processing (non-alias) macro-insns,
62575fd0b74Schristos    this function recurses.
62675fd0b74Schristos 
62775fd0b74Schristos    ??? It's possible to make this cpu-independent.
62875fd0b74Schristos    One would have to deal with a few minor things.
62975fd0b74Schristos    At this point in time doing so would be more of a curiosity than useful
63075fd0b74Schristos    [for example this file isn't _that_ big], but keeping the possibility in
63175fd0b74Schristos    mind helps keep the design clean.  */
63275fd0b74Schristos 
63375fd0b74Schristos const CGEN_INSN *
m32r_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)63475fd0b74Schristos m32r_cgen_assemble_insn (CGEN_CPU_DESC cd,
63575fd0b74Schristos 			   const char *str,
63675fd0b74Schristos 			   CGEN_FIELDS *fields,
63775fd0b74Schristos 			   CGEN_INSN_BYTES_PTR buf,
63875fd0b74Schristos 			   char **errmsg)
63975fd0b74Schristos {
64075fd0b74Schristos   const char *start;
64175fd0b74Schristos   CGEN_INSN_LIST *ilist;
64275fd0b74Schristos   const char *parse_errmsg = NULL;
64375fd0b74Schristos   const char *insert_errmsg = NULL;
64475fd0b74Schristos   int recognized_mnemonic = 0;
64575fd0b74Schristos 
64675fd0b74Schristos   /* Skip leading white space.  */
64775fd0b74Schristos   while (ISSPACE (* str))
64875fd0b74Schristos     ++ str;
64975fd0b74Schristos 
65075fd0b74Schristos   /* The instructions are stored in hashed lists.
65175fd0b74Schristos      Get the first in the list.  */
65275fd0b74Schristos   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
65375fd0b74Schristos 
65475fd0b74Schristos   /* Keep looking until we find a match.  */
65575fd0b74Schristos   start = str;
65675fd0b74Schristos   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
65775fd0b74Schristos     {
65875fd0b74Schristos       const CGEN_INSN *insn = ilist->insn;
65975fd0b74Schristos       recognized_mnemonic = 1;
66075fd0b74Schristos 
66175fd0b74Schristos #ifdef CGEN_VALIDATE_INSN_SUPPORTED
66275fd0b74Schristos       /* Not usually needed as unsupported opcodes
66375fd0b74Schristos 	 shouldn't be in the hash lists.  */
66475fd0b74Schristos       /* Is this insn supported by the selected cpu?  */
66575fd0b74Schristos       if (! m32r_cgen_insn_supported (cd, insn))
66675fd0b74Schristos 	continue;
66775fd0b74Schristos #endif
66875fd0b74Schristos       /* If the RELAXED attribute is set, this is an insn that shouldn't be
66975fd0b74Schristos 	 chosen immediately.  Instead, it is used during assembler/linker
67075fd0b74Schristos 	 relaxation if possible.  */
67175fd0b74Schristos       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
67275fd0b74Schristos 	continue;
67375fd0b74Schristos 
67475fd0b74Schristos       str = start;
67575fd0b74Schristos 
67675fd0b74Schristos       /* Skip this insn if str doesn't look right lexically.  */
67775fd0b74Schristos       if (CGEN_INSN_RX (insn) != NULL &&
67875fd0b74Schristos 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
67975fd0b74Schristos 	continue;
68075fd0b74Schristos 
68175fd0b74Schristos       /* Allow parse/insert handlers to obtain length of insn.  */
68275fd0b74Schristos       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
68375fd0b74Schristos 
68475fd0b74Schristos       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
68575fd0b74Schristos       if (parse_errmsg != NULL)
68675fd0b74Schristos 	continue;
68775fd0b74Schristos 
68875fd0b74Schristos       /* ??? 0 is passed for `pc'.  */
68975fd0b74Schristos       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
69075fd0b74Schristos 						 (bfd_vma) 0);
69175fd0b74Schristos       if (insert_errmsg != NULL)
69275fd0b74Schristos         continue;
69375fd0b74Schristos 
69475fd0b74Schristos       /* It is up to the caller to actually output the insn and any
69575fd0b74Schristos          queued relocs.  */
69675fd0b74Schristos       return insn;
69775fd0b74Schristos     }
69875fd0b74Schristos 
69975fd0b74Schristos   {
70075fd0b74Schristos     static char errbuf[150];
70175fd0b74Schristos     const char *tmp_errmsg;
70275fd0b74Schristos #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
70375fd0b74Schristos #define be_verbose 1
70475fd0b74Schristos #else
70575fd0b74Schristos #define be_verbose 0
70675fd0b74Schristos #endif
70775fd0b74Schristos 
70875fd0b74Schristos     if (be_verbose)
70975fd0b74Schristos       {
71075fd0b74Schristos 	/* If requesting verbose error messages, use insert_errmsg.
71175fd0b74Schristos 	   Failing that, use parse_errmsg.  */
71275fd0b74Schristos 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
71375fd0b74Schristos 		      parse_errmsg ? parse_errmsg :
71475fd0b74Schristos 		      recognized_mnemonic ?
71575fd0b74Schristos 		      _("unrecognized form of instruction") :
71675fd0b74Schristos 		      _("unrecognized instruction"));
71775fd0b74Schristos 
71875fd0b74Schristos 	if (strlen (start) > 50)
71975fd0b74Schristos 	  /* xgettext:c-format */
72075fd0b74Schristos 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
72175fd0b74Schristos 	else
72275fd0b74Schristos 	  /* xgettext:c-format */
72375fd0b74Schristos 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
72475fd0b74Schristos       }
72575fd0b74Schristos     else
72675fd0b74Schristos       {
72775fd0b74Schristos 	if (strlen (start) > 50)
72875fd0b74Schristos 	  /* xgettext:c-format */
72975fd0b74Schristos 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
73075fd0b74Schristos 	else
73175fd0b74Schristos 	  /* xgettext:c-format */
73275fd0b74Schristos 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
73375fd0b74Schristos       }
73475fd0b74Schristos 
73575fd0b74Schristos     *errmsg = errbuf;
73675fd0b74Schristos     return NULL;
73775fd0b74Schristos   }
73875fd0b74Schristos }
739