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