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