1*3d8817e4Smiod /* Assembler interface for targets using CGEN. -*- C -*-
2*3d8817e4Smiod    CGEN: Cpu tools GENerator
3*3d8817e4Smiod 
4*3d8817e4Smiod    THIS FILE IS MACHINE GENERATED WITH CGEN.
5*3d8817e4Smiod    - the resultant file is machine generated, cgen-asm.in isn't
6*3d8817e4Smiod 
7*3d8817e4Smiod    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005
8*3d8817e4Smiod    Free Software Foundation, Inc.
9*3d8817e4Smiod 
10*3d8817e4Smiod    This file is part of the GNU Binutils and GDB, the GNU debugger.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
13*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
14*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
15*3d8817e4Smiod    any later version.
16*3d8817e4Smiod 
17*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
18*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
19*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20*3d8817e4Smiod    GNU General Public License for more details.
21*3d8817e4Smiod 
22*3d8817e4Smiod    You should have received a copy of the GNU General Public License
23*3d8817e4Smiod    along with this program; if not, write to the Free Software Foundation, Inc.,
24*3d8817e4Smiod    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
25*3d8817e4Smiod 
26*3d8817e4Smiod /* ??? Eventually more and more of this stuff can go to cpu-independent files.
27*3d8817e4Smiod    Keep that in mind.  */
28*3d8817e4Smiod 
29*3d8817e4Smiod #include "sysdep.h"
30*3d8817e4Smiod #include <stdio.h>
31*3d8817e4Smiod #include "ansidecl.h"
32*3d8817e4Smiod #include "bfd.h"
33*3d8817e4Smiod #include "symcat.h"
34*3d8817e4Smiod #include "iq2000-desc.h"
35*3d8817e4Smiod #include "iq2000-opc.h"
36*3d8817e4Smiod #include "opintl.h"
37*3d8817e4Smiod #include "xregex.h"
38*3d8817e4Smiod #include "libiberty.h"
39*3d8817e4Smiod #include "safe-ctype.h"
40*3d8817e4Smiod 
41*3d8817e4Smiod #undef  min
42*3d8817e4Smiod #define min(a,b) ((a) < (b) ? (a) : (b))
43*3d8817e4Smiod #undef  max
44*3d8817e4Smiod #define max(a,b) ((a) > (b) ? (a) : (b))
45*3d8817e4Smiod 
46*3d8817e4Smiod static const char * parse_insn_normal
47*3d8817e4Smiod   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
48*3d8817e4Smiod 
49*3d8817e4Smiod /* -- assembler routines inserted here.  */
50*3d8817e4Smiod 
51*3d8817e4Smiod /* -- asm.c */
52*3d8817e4Smiod 
53*3d8817e4Smiod #include "safe-ctype.h"
54*3d8817e4Smiod 
55*3d8817e4Smiod static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
56*3d8817e4Smiod 
57*3d8817e4Smiod /* Special check to ensure that instruction exists for given machine.  */
58*3d8817e4Smiod 
59*3d8817e4Smiod int
iq2000_cgen_insn_supported(CGEN_CPU_DESC cd,const CGEN_INSN * insn)60*3d8817e4Smiod iq2000_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
61*3d8817e4Smiod {
62*3d8817e4Smiod   int machs = cd->machs;
63*3d8817e4Smiod 
64*3d8817e4Smiod   return (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_MACH) & machs) != 0;
65*3d8817e4Smiod }
66*3d8817e4Smiod 
67*3d8817e4Smiod static int
iq2000_cgen_isa_register(const char ** strp)68*3d8817e4Smiod iq2000_cgen_isa_register (const char **strp)
69*3d8817e4Smiod {
70*3d8817e4Smiod   int len;
71*3d8817e4Smiod   int ch1, ch2;
72*3d8817e4Smiod 
73*3d8817e4Smiod   if (**strp == 'r' || **strp == 'R')
74*3d8817e4Smiod     {
75*3d8817e4Smiod       len = strlen (*strp);
76*3d8817e4Smiod       if (len == 2)
77*3d8817e4Smiod         {
78*3d8817e4Smiod           ch1 = (*strp)[1];
79*3d8817e4Smiod           if ('0' <= ch1 && ch1 <= '9')
80*3d8817e4Smiod             return 1;
81*3d8817e4Smiod         }
82*3d8817e4Smiod       else if (len == 3)
83*3d8817e4Smiod         {
84*3d8817e4Smiod 	  ch1 = (*strp)[1];
85*3d8817e4Smiod           ch2 = (*strp)[2];
86*3d8817e4Smiod           if (('1' <= ch1 && ch1 <= '2') && ('0' <= ch2 && ch2 <= '9'))
87*3d8817e4Smiod             return 1;
88*3d8817e4Smiod           if ('3' == ch1 && (ch2 == '0' || ch2 == '1'))
89*3d8817e4Smiod             return 1;
90*3d8817e4Smiod         }
91*3d8817e4Smiod     }
92*3d8817e4Smiod   if (**strp == '%'
93*3d8817e4Smiod       && TOLOWER ((*strp)[1]) != 'l'
94*3d8817e4Smiod       && TOLOWER ((*strp)[1]) != 'h')
95*3d8817e4Smiod     return 1;
96*3d8817e4Smiod   return 0;
97*3d8817e4Smiod }
98*3d8817e4Smiod 
99*3d8817e4Smiod /* Handle negated literal.  */
100*3d8817e4Smiod 
101*3d8817e4Smiod static const char *
parse_mimm(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)102*3d8817e4Smiod parse_mimm (CGEN_CPU_DESC cd,
103*3d8817e4Smiod 	    const char **strp,
104*3d8817e4Smiod 	    int opindex,
105*3d8817e4Smiod 	    unsigned long *valuep)
106*3d8817e4Smiod {
107*3d8817e4Smiod   const char *errmsg;
108*3d8817e4Smiod 
109*3d8817e4Smiod   /* Verify this isn't a register.  */
110*3d8817e4Smiod   if (iq2000_cgen_isa_register (strp))
111*3d8817e4Smiod     errmsg = _("immediate value cannot be register");
112*3d8817e4Smiod   else
113*3d8817e4Smiod     {
114*3d8817e4Smiod       long value;
115*3d8817e4Smiod 
116*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
117*3d8817e4Smiod       if (errmsg == NULL)
118*3d8817e4Smiod 	{
119*3d8817e4Smiod 	  long x = (-value) & 0xFFFF0000;
120*3d8817e4Smiod 
121*3d8817e4Smiod 	  if (x != 0 && x != (long) 0xFFFF0000)
122*3d8817e4Smiod 	    errmsg = _("immediate value out of range");
123*3d8817e4Smiod 	  else
124*3d8817e4Smiod 	    *valuep = (-value & 0xFFFF);
125*3d8817e4Smiod 	}
126*3d8817e4Smiod     }
127*3d8817e4Smiod   return errmsg;
128*3d8817e4Smiod }
129*3d8817e4Smiod 
130*3d8817e4Smiod /* Handle signed/unsigned literal.  */
131*3d8817e4Smiod 
132*3d8817e4Smiod static const char *
parse_imm(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)133*3d8817e4Smiod parse_imm (CGEN_CPU_DESC cd,
134*3d8817e4Smiod 	   const char **strp,
135*3d8817e4Smiod 	   int opindex,
136*3d8817e4Smiod 	   unsigned long *valuep)
137*3d8817e4Smiod {
138*3d8817e4Smiod   const char *errmsg;
139*3d8817e4Smiod 
140*3d8817e4Smiod   if (iq2000_cgen_isa_register (strp))
141*3d8817e4Smiod     errmsg = _("immediate value cannot be register");
142*3d8817e4Smiod   else
143*3d8817e4Smiod     {
144*3d8817e4Smiod       long value;
145*3d8817e4Smiod 
146*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
147*3d8817e4Smiod       if (errmsg == NULL)
148*3d8817e4Smiod 	{
149*3d8817e4Smiod 	  long x = value & 0xFFFF0000;
150*3d8817e4Smiod 
151*3d8817e4Smiod 	  if (x != 0 && x != (long) 0xFFFF0000)
152*3d8817e4Smiod 	    errmsg = _("immediate value out of range");
153*3d8817e4Smiod 	  else
154*3d8817e4Smiod 	    *valuep = (value & 0xFFFF);
155*3d8817e4Smiod 	}
156*3d8817e4Smiod     }
157*3d8817e4Smiod   return errmsg;
158*3d8817e4Smiod }
159*3d8817e4Smiod 
160*3d8817e4Smiod /* Handle iq10 21-bit jmp offset.  */
161*3d8817e4Smiod 
162*3d8817e4Smiod 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)163*3d8817e4Smiod parse_jtargq10 (CGEN_CPU_DESC cd,
164*3d8817e4Smiod 		const char **strp,
165*3d8817e4Smiod 		int opindex,
166*3d8817e4Smiod 		int reloc ATTRIBUTE_UNUSED,
167*3d8817e4Smiod 		enum cgen_parse_operand_result *type_addr ATTRIBUTE_UNUSED,
168*3d8817e4Smiod 		bfd_vma *valuep)
169*3d8817e4Smiod {
170*3d8817e4Smiod   const char *errmsg;
171*3d8817e4Smiod   bfd_vma value;
172*3d8817e4Smiod   enum cgen_parse_operand_result result_type = CGEN_PARSE_OPERAND_RESULT_NUMBER;
173*3d8817e4Smiod 
174*3d8817e4Smiod   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IQ2000_OFFSET_21,
175*3d8817e4Smiod 			       & result_type, & value);
176*3d8817e4Smiod   if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
177*3d8817e4Smiod     {
178*3d8817e4Smiod       /* Check value is within 23-bits
179*3d8817e4Smiod 	 (remembering that 2-bit shift right will occur).  */
180*3d8817e4Smiod       if (value > 0x7fffff)
181*3d8817e4Smiod         return _("21-bit offset out of range");
182*3d8817e4Smiod     }
183*3d8817e4Smiod   *valuep = (value & 0x7FFFFF);
184*3d8817e4Smiod   return errmsg;
185*3d8817e4Smiod }
186*3d8817e4Smiod 
187*3d8817e4Smiod /* Handle high().  */
188*3d8817e4Smiod 
189*3d8817e4Smiod static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)190*3d8817e4Smiod parse_hi16 (CGEN_CPU_DESC cd,
191*3d8817e4Smiod 	    const char **strp,
192*3d8817e4Smiod 	    int opindex,
193*3d8817e4Smiod 	    unsigned long *valuep)
194*3d8817e4Smiod {
195*3d8817e4Smiod   if (strncasecmp (*strp, "%hi(", 4) == 0)
196*3d8817e4Smiod     {
197*3d8817e4Smiod       enum cgen_parse_operand_result result_type;
198*3d8817e4Smiod       bfd_vma value;
199*3d8817e4Smiod       const char *errmsg;
200*3d8817e4Smiod 
201*3d8817e4Smiod       *strp += 4;
202*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
203*3d8817e4Smiod 				   & result_type, & value);
204*3d8817e4Smiod       if (**strp != ')')
205*3d8817e4Smiod 	return MISSING_CLOSING_PARENTHESIS;
206*3d8817e4Smiod 
207*3d8817e4Smiod       ++*strp;
208*3d8817e4Smiod       if (errmsg == NULL
209*3d8817e4Smiod   	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
210*3d8817e4Smiod 	{
211*3d8817e4Smiod 	  /* If value has top-bit of %lo on, then it will
212*3d8817e4Smiod 	     sign-propagate and so we compensate by adding
213*3d8817e4Smiod 	     1 to the resultant %hi value.  */
214*3d8817e4Smiod 	  if (value & 0x8000)
215*3d8817e4Smiod 	    value += 0x10000;
216*3d8817e4Smiod 	  value >>= 16;
217*3d8817e4Smiod 	  value &= 0xffff;
218*3d8817e4Smiod 	}
219*3d8817e4Smiod       *valuep = value;
220*3d8817e4Smiod 
221*3d8817e4Smiod       return errmsg;
222*3d8817e4Smiod     }
223*3d8817e4Smiod 
224*3d8817e4Smiod   /* We add %uhi in case a user just wants the high 16-bits or is using
225*3d8817e4Smiod      an insn like ori for %lo which does not sign-propagate.  */
226*3d8817e4Smiod   if (strncasecmp (*strp, "%uhi(", 5) == 0)
227*3d8817e4Smiod     {
228*3d8817e4Smiod       enum cgen_parse_operand_result result_type;
229*3d8817e4Smiod       bfd_vma value;
230*3d8817e4Smiod       const char *errmsg;
231*3d8817e4Smiod 
232*3d8817e4Smiod       *strp += 5;
233*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IQ2000_UHI16,
234*3d8817e4Smiod 				   & result_type, & value);
235*3d8817e4Smiod       if (**strp != ')')
236*3d8817e4Smiod 	return MISSING_CLOSING_PARENTHESIS;
237*3d8817e4Smiod 
238*3d8817e4Smiod       ++*strp;
239*3d8817e4Smiod       if (errmsg == NULL
240*3d8817e4Smiod   	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
241*3d8817e4Smiod 	value >>= 16;
242*3d8817e4Smiod 
243*3d8817e4Smiod       value &= 0xffff;
244*3d8817e4Smiod       *valuep = value;
245*3d8817e4Smiod 
246*3d8817e4Smiod       return errmsg;
247*3d8817e4Smiod     }
248*3d8817e4Smiod 
249*3d8817e4Smiod   return parse_imm (cd, strp, opindex, valuep);
250*3d8817e4Smiod }
251*3d8817e4Smiod 
252*3d8817e4Smiod /* Handle %lo in a signed context.
253*3d8817e4Smiod    The signedness of the value doesn't matter to %lo(), but this also
254*3d8817e4Smiod    handles the case where %lo() isn't present.  */
255*3d8817e4Smiod 
256*3d8817e4Smiod static const char *
parse_lo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)257*3d8817e4Smiod parse_lo16 (CGEN_CPU_DESC cd,
258*3d8817e4Smiod 	    const char **strp,
259*3d8817e4Smiod 	    int opindex,
260*3d8817e4Smiod 	    unsigned long *valuep)
261*3d8817e4Smiod {
262*3d8817e4Smiod   if (strncasecmp (*strp, "%lo(", 4) == 0)
263*3d8817e4Smiod     {
264*3d8817e4Smiod       const char *errmsg;
265*3d8817e4Smiod       enum cgen_parse_operand_result result_type;
266*3d8817e4Smiod       bfd_vma value;
267*3d8817e4Smiod 
268*3d8817e4Smiod       *strp += 4;
269*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
270*3d8817e4Smiod 				   & result_type, & value);
271*3d8817e4Smiod       if (**strp != ')')
272*3d8817e4Smiod 	return MISSING_CLOSING_PARENTHESIS;
273*3d8817e4Smiod       ++*strp;
274*3d8817e4Smiod       if (errmsg == NULL
275*3d8817e4Smiod 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
276*3d8817e4Smiod 	value &= 0xffff;
277*3d8817e4Smiod       *valuep = value;
278*3d8817e4Smiod       return errmsg;
279*3d8817e4Smiod     }
280*3d8817e4Smiod 
281*3d8817e4Smiod   return parse_imm (cd, strp, opindex, valuep);
282*3d8817e4Smiod }
283*3d8817e4Smiod 
284*3d8817e4Smiod /* Handle %lo in a negated signed context.
285*3d8817e4Smiod    The signedness of the value doesn't matter to %lo(), but this also
286*3d8817e4Smiod    handles the case where %lo() isn't present.  */
287*3d8817e4Smiod 
288*3d8817e4Smiod static const char *
parse_mlo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)289*3d8817e4Smiod parse_mlo16 (CGEN_CPU_DESC cd,
290*3d8817e4Smiod 	     const char **strp,
291*3d8817e4Smiod 	     int opindex,
292*3d8817e4Smiod 	     unsigned long *valuep)
293*3d8817e4Smiod {
294*3d8817e4Smiod   if (strncasecmp (*strp, "%lo(", 4) == 0)
295*3d8817e4Smiod     {
296*3d8817e4Smiod       const char *errmsg;
297*3d8817e4Smiod       enum cgen_parse_operand_result result_type;
298*3d8817e4Smiod       bfd_vma value;
299*3d8817e4Smiod 
300*3d8817e4Smiod       *strp += 4;
301*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
302*3d8817e4Smiod 				   & result_type, & value);
303*3d8817e4Smiod       if (**strp != ')')
304*3d8817e4Smiod 	return MISSING_CLOSING_PARENTHESIS;
305*3d8817e4Smiod       ++*strp;
306*3d8817e4Smiod       if (errmsg == NULL
307*3d8817e4Smiod 	  && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
308*3d8817e4Smiod 	value = (-value) & 0xffff;
309*3d8817e4Smiod       *valuep = value;
310*3d8817e4Smiod       return errmsg;
311*3d8817e4Smiod     }
312*3d8817e4Smiod 
313*3d8817e4Smiod   return parse_mimm (cd, strp, opindex, valuep);
314*3d8817e4Smiod }
315*3d8817e4Smiod 
316*3d8817e4Smiod /* -- */
317*3d8817e4Smiod 
318*3d8817e4Smiod const char * iq2000_cgen_parse_operand
319*3d8817e4Smiod   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
320*3d8817e4Smiod 
321*3d8817e4Smiod /* Main entry point for operand parsing.
322*3d8817e4Smiod 
323*3d8817e4Smiod    This function is basically just a big switch statement.  Earlier versions
324*3d8817e4Smiod    used tables to look up the function to use, but
325*3d8817e4Smiod    - if the table contains both assembler and disassembler functions then
326*3d8817e4Smiod      the disassembler contains much of the assembler and vice-versa,
327*3d8817e4Smiod    - there's a lot of inlining possibilities as things grow,
328*3d8817e4Smiod    - using a switch statement avoids the function call overhead.
329*3d8817e4Smiod 
330*3d8817e4Smiod    This function could be moved into `parse_insn_normal', but keeping it
331*3d8817e4Smiod    separate makes clear the interface between `parse_insn_normal' and each of
332*3d8817e4Smiod    the handlers.  */
333*3d8817e4Smiod 
334*3d8817e4Smiod const char *
iq2000_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)335*3d8817e4Smiod iq2000_cgen_parse_operand (CGEN_CPU_DESC cd,
336*3d8817e4Smiod 			   int opindex,
337*3d8817e4Smiod 			   const char ** strp,
338*3d8817e4Smiod 			   CGEN_FIELDS * fields)
339*3d8817e4Smiod {
340*3d8817e4Smiod   const char * errmsg = NULL;
341*3d8817e4Smiod   /* Used by scalar operands that still need to be parsed.  */
342*3d8817e4Smiod   long junk ATTRIBUTE_UNUSED;
343*3d8817e4Smiod 
344*3d8817e4Smiod   switch (opindex)
345*3d8817e4Smiod     {
346*3d8817e4Smiod     case IQ2000_OPERAND__INDEX :
347*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND__INDEX, (unsigned long *) (& fields->f_index));
348*3d8817e4Smiod       break;
349*3d8817e4Smiod     case IQ2000_OPERAND_BASE :
350*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rs);
351*3d8817e4Smiod       break;
352*3d8817e4Smiod     case IQ2000_OPERAND_BASEOFF :
353*3d8817e4Smiod       {
354*3d8817e4Smiod         bfd_vma value = 0;
355*3d8817e4Smiod         errmsg = cgen_parse_address (cd, strp, IQ2000_OPERAND_BASEOFF, 0, NULL,  & value);
356*3d8817e4Smiod         fields->f_imm = value;
357*3d8817e4Smiod       }
358*3d8817e4Smiod       break;
359*3d8817e4Smiod     case IQ2000_OPERAND_BITNUM :
360*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_BITNUM, (unsigned long *) (& fields->f_rt));
361*3d8817e4Smiod       break;
362*3d8817e4Smiod     case IQ2000_OPERAND_BYTECOUNT :
363*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_BYTECOUNT, (unsigned long *) (& fields->f_bytecount));
364*3d8817e4Smiod       break;
365*3d8817e4Smiod     case IQ2000_OPERAND_CAM_Y :
366*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CAM_Y, (unsigned long *) (& fields->f_cam_y));
367*3d8817e4Smiod       break;
368*3d8817e4Smiod     case IQ2000_OPERAND_CAM_Z :
369*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CAM_Z, (unsigned long *) (& fields->f_cam_z));
370*3d8817e4Smiod       break;
371*3d8817e4Smiod     case IQ2000_OPERAND_CM_3FUNC :
372*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CM_3FUNC, (unsigned long *) (& fields->f_cm_3func));
373*3d8817e4Smiod       break;
374*3d8817e4Smiod     case IQ2000_OPERAND_CM_3Z :
375*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CM_3Z, (unsigned long *) (& fields->f_cm_3z));
376*3d8817e4Smiod       break;
377*3d8817e4Smiod     case IQ2000_OPERAND_CM_4FUNC :
378*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CM_4FUNC, (unsigned long *) (& fields->f_cm_4func));
379*3d8817e4Smiod       break;
380*3d8817e4Smiod     case IQ2000_OPERAND_CM_4Z :
381*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_CM_4Z, (unsigned long *) (& fields->f_cm_4z));
382*3d8817e4Smiod       break;
383*3d8817e4Smiod     case IQ2000_OPERAND_COUNT :
384*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_COUNT, (unsigned long *) (& fields->f_count));
385*3d8817e4Smiod       break;
386*3d8817e4Smiod     case IQ2000_OPERAND_EXECODE :
387*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_EXECODE, (unsigned long *) (& fields->f_excode));
388*3d8817e4Smiod       break;
389*3d8817e4Smiod     case IQ2000_OPERAND_HI16 :
390*3d8817e4Smiod       errmsg = parse_hi16 (cd, strp, IQ2000_OPERAND_HI16, (unsigned long *) (& fields->f_imm));
391*3d8817e4Smiod       break;
392*3d8817e4Smiod     case IQ2000_OPERAND_IMM :
393*3d8817e4Smiod       errmsg = parse_imm (cd, strp, IQ2000_OPERAND_IMM, (unsigned long *) (& fields->f_imm));
394*3d8817e4Smiod       break;
395*3d8817e4Smiod     case IQ2000_OPERAND_JMPTARG :
396*3d8817e4Smiod       {
397*3d8817e4Smiod         bfd_vma value = 0;
398*3d8817e4Smiod         errmsg = cgen_parse_address (cd, strp, IQ2000_OPERAND_JMPTARG, 0, NULL,  & value);
399*3d8817e4Smiod         fields->f_jtarg = value;
400*3d8817e4Smiod       }
401*3d8817e4Smiod       break;
402*3d8817e4Smiod     case IQ2000_OPERAND_JMPTARGQ10 :
403*3d8817e4Smiod       {
404*3d8817e4Smiod         bfd_vma value = 0;
405*3d8817e4Smiod         errmsg = parse_jtargq10 (cd, strp, IQ2000_OPERAND_JMPTARGQ10, 0, NULL,  & value);
406*3d8817e4Smiod         fields->f_jtargq10 = value;
407*3d8817e4Smiod       }
408*3d8817e4Smiod       break;
409*3d8817e4Smiod     case IQ2000_OPERAND_LO16 :
410*3d8817e4Smiod       errmsg = parse_lo16 (cd, strp, IQ2000_OPERAND_LO16, (unsigned long *) (& fields->f_imm));
411*3d8817e4Smiod       break;
412*3d8817e4Smiod     case IQ2000_OPERAND_MASK :
413*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_MASK, (unsigned long *) (& fields->f_mask));
414*3d8817e4Smiod       break;
415*3d8817e4Smiod     case IQ2000_OPERAND_MASKL :
416*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_MASKL, (unsigned long *) (& fields->f_maskl));
417*3d8817e4Smiod       break;
418*3d8817e4Smiod     case IQ2000_OPERAND_MASKQ10 :
419*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_MASKQ10, (unsigned long *) (& fields->f_maskq10));
420*3d8817e4Smiod       break;
421*3d8817e4Smiod     case IQ2000_OPERAND_MASKR :
422*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_MASKR, (unsigned long *) (& fields->f_rs));
423*3d8817e4Smiod       break;
424*3d8817e4Smiod     case IQ2000_OPERAND_MLO16 :
425*3d8817e4Smiod       errmsg = parse_mlo16 (cd, strp, IQ2000_OPERAND_MLO16, (unsigned long *) (& fields->f_imm));
426*3d8817e4Smiod       break;
427*3d8817e4Smiod     case IQ2000_OPERAND_OFFSET :
428*3d8817e4Smiod       {
429*3d8817e4Smiod         bfd_vma value = 0;
430*3d8817e4Smiod         errmsg = cgen_parse_address (cd, strp, IQ2000_OPERAND_OFFSET, 0, NULL,  & value);
431*3d8817e4Smiod         fields->f_offset = value;
432*3d8817e4Smiod       }
433*3d8817e4Smiod       break;
434*3d8817e4Smiod     case IQ2000_OPERAND_RD :
435*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rd);
436*3d8817e4Smiod       break;
437*3d8817e4Smiod     case IQ2000_OPERAND_RD_RS :
438*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rd_rs);
439*3d8817e4Smiod       break;
440*3d8817e4Smiod     case IQ2000_OPERAND_RD_RT :
441*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rd_rt);
442*3d8817e4Smiod       break;
443*3d8817e4Smiod     case IQ2000_OPERAND_RS :
444*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rs);
445*3d8817e4Smiod       break;
446*3d8817e4Smiod     case IQ2000_OPERAND_RT :
447*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rt);
448*3d8817e4Smiod       break;
449*3d8817e4Smiod     case IQ2000_OPERAND_RT_RS :
450*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & iq2000_cgen_opval_gr_names, & fields->f_rt_rs);
451*3d8817e4Smiod       break;
452*3d8817e4Smiod     case IQ2000_OPERAND_SHAMT :
453*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IQ2000_OPERAND_SHAMT, (unsigned long *) (& fields->f_shamt));
454*3d8817e4Smiod       break;
455*3d8817e4Smiod 
456*3d8817e4Smiod     default :
457*3d8817e4Smiod       /* xgettext:c-format */
458*3d8817e4Smiod       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
459*3d8817e4Smiod       abort ();
460*3d8817e4Smiod   }
461*3d8817e4Smiod 
462*3d8817e4Smiod   return errmsg;
463*3d8817e4Smiod }
464*3d8817e4Smiod 
465*3d8817e4Smiod cgen_parse_fn * const iq2000_cgen_parse_handlers[] =
466*3d8817e4Smiod {
467*3d8817e4Smiod   parse_insn_normal,
468*3d8817e4Smiod };
469*3d8817e4Smiod 
470*3d8817e4Smiod void
iq2000_cgen_init_asm(CGEN_CPU_DESC cd)471*3d8817e4Smiod iq2000_cgen_init_asm (CGEN_CPU_DESC cd)
472*3d8817e4Smiod {
473*3d8817e4Smiod   iq2000_cgen_init_opcode_table (cd);
474*3d8817e4Smiod   iq2000_cgen_init_ibld_table (cd);
475*3d8817e4Smiod   cd->parse_handlers = & iq2000_cgen_parse_handlers[0];
476*3d8817e4Smiod   cd->parse_operand = iq2000_cgen_parse_operand;
477*3d8817e4Smiod }
478*3d8817e4Smiod 
479*3d8817e4Smiod 
480*3d8817e4Smiod 
481*3d8817e4Smiod /* Regex construction routine.
482*3d8817e4Smiod 
483*3d8817e4Smiod    This translates an opcode syntax string into a regex string,
484*3d8817e4Smiod    by replacing any non-character syntax element (such as an
485*3d8817e4Smiod    opcode) with the pattern '.*'
486*3d8817e4Smiod 
487*3d8817e4Smiod    It then compiles the regex and stores it in the opcode, for
488*3d8817e4Smiod    later use by iq2000_cgen_assemble_insn
489*3d8817e4Smiod 
490*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
491*3d8817e4Smiod 
492*3d8817e4Smiod char *
iq2000_cgen_build_insn_regex(CGEN_INSN * insn)493*3d8817e4Smiod iq2000_cgen_build_insn_regex (CGEN_INSN *insn)
494*3d8817e4Smiod {
495*3d8817e4Smiod   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
496*3d8817e4Smiod   const char *mnem = CGEN_INSN_MNEMONIC (insn);
497*3d8817e4Smiod   char rxbuf[CGEN_MAX_RX_ELEMENTS];
498*3d8817e4Smiod   char *rx = rxbuf;
499*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE *syn;
500*3d8817e4Smiod   int reg_err;
501*3d8817e4Smiod 
502*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
503*3d8817e4Smiod 
504*3d8817e4Smiod   /* Mnemonics come first in the syntax string.  */
505*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
506*3d8817e4Smiod     return _("missing mnemonic in syntax string");
507*3d8817e4Smiod   ++syn;
508*3d8817e4Smiod 
509*3d8817e4Smiod   /* Generate a case sensitive regular expression that emulates case
510*3d8817e4Smiod      insensitive matching in the "C" locale.  We cannot generate a case
511*3d8817e4Smiod      insensitive regular expression because in Turkish locales, 'i' and 'I'
512*3d8817e4Smiod      are not equal modulo case conversion.  */
513*3d8817e4Smiod 
514*3d8817e4Smiod   /* Copy the literal mnemonic out of the insn.  */
515*3d8817e4Smiod   for (; *mnem; mnem++)
516*3d8817e4Smiod     {
517*3d8817e4Smiod       char c = *mnem;
518*3d8817e4Smiod 
519*3d8817e4Smiod       if (ISALPHA (c))
520*3d8817e4Smiod 	{
521*3d8817e4Smiod 	  *rx++ = '[';
522*3d8817e4Smiod 	  *rx++ = TOLOWER (c);
523*3d8817e4Smiod 	  *rx++ = TOUPPER (c);
524*3d8817e4Smiod 	  *rx++ = ']';
525*3d8817e4Smiod 	}
526*3d8817e4Smiod       else
527*3d8817e4Smiod 	*rx++ = c;
528*3d8817e4Smiod     }
529*3d8817e4Smiod 
530*3d8817e4Smiod   /* Copy any remaining literals from the syntax string into the rx.  */
531*3d8817e4Smiod   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
532*3d8817e4Smiod     {
533*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
534*3d8817e4Smiod 	{
535*3d8817e4Smiod 	  char c = CGEN_SYNTAX_CHAR (* syn);
536*3d8817e4Smiod 
537*3d8817e4Smiod 	  switch (c)
538*3d8817e4Smiod 	    {
539*3d8817e4Smiod 	      /* Escape any regex metacharacters in the syntax.  */
540*3d8817e4Smiod 	    case '.': case '[': case '\\':
541*3d8817e4Smiod 	    case '*': case '^': case '$':
542*3d8817e4Smiod 
543*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
544*3d8817e4Smiod 	    case '?': case '{': case '}':
545*3d8817e4Smiod 	    case '(': case ')': case '*':
546*3d8817e4Smiod 	    case '|': case '+': case ']':
547*3d8817e4Smiod #endif
548*3d8817e4Smiod 	      *rx++ = '\\';
549*3d8817e4Smiod 	      *rx++ = c;
550*3d8817e4Smiod 	      break;
551*3d8817e4Smiod 
552*3d8817e4Smiod 	    default:
553*3d8817e4Smiod 	      if (ISALPHA (c))
554*3d8817e4Smiod 		{
555*3d8817e4Smiod 		  *rx++ = '[';
556*3d8817e4Smiod 		  *rx++ = TOLOWER (c);
557*3d8817e4Smiod 		  *rx++ = TOUPPER (c);
558*3d8817e4Smiod 		  *rx++ = ']';
559*3d8817e4Smiod 		}
560*3d8817e4Smiod 	      else
561*3d8817e4Smiod 		*rx++ = c;
562*3d8817e4Smiod 	      break;
563*3d8817e4Smiod 	    }
564*3d8817e4Smiod 	}
565*3d8817e4Smiod       else
566*3d8817e4Smiod 	{
567*3d8817e4Smiod 	  /* Replace non-syntax fields with globs.  */
568*3d8817e4Smiod 	  *rx++ = '.';
569*3d8817e4Smiod 	  *rx++ = '*';
570*3d8817e4Smiod 	}
571*3d8817e4Smiod     }
572*3d8817e4Smiod 
573*3d8817e4Smiod   /* Trailing whitespace ok.  */
574*3d8817e4Smiod   * rx++ = '[';
575*3d8817e4Smiod   * rx++ = ' ';
576*3d8817e4Smiod   * rx++ = '\t';
577*3d8817e4Smiod   * rx++ = ']';
578*3d8817e4Smiod   * rx++ = '*';
579*3d8817e4Smiod 
580*3d8817e4Smiod   /* But anchor it after that.  */
581*3d8817e4Smiod   * rx++ = '$';
582*3d8817e4Smiod   * rx = '\0';
583*3d8817e4Smiod 
584*3d8817e4Smiod   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
585*3d8817e4Smiod   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
586*3d8817e4Smiod 
587*3d8817e4Smiod   if (reg_err == 0)
588*3d8817e4Smiod     return NULL;
589*3d8817e4Smiod   else
590*3d8817e4Smiod     {
591*3d8817e4Smiod       static char msg[80];
592*3d8817e4Smiod 
593*3d8817e4Smiod       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
594*3d8817e4Smiod       regfree ((regex_t *) CGEN_INSN_RX (insn));
595*3d8817e4Smiod       free (CGEN_INSN_RX (insn));
596*3d8817e4Smiod       (CGEN_INSN_RX (insn)) = NULL;
597*3d8817e4Smiod       return msg;
598*3d8817e4Smiod     }
599*3d8817e4Smiod }
600*3d8817e4Smiod 
601*3d8817e4Smiod 
602*3d8817e4Smiod /* Default insn parser.
603*3d8817e4Smiod 
604*3d8817e4Smiod    The syntax string is scanned and operands are parsed and stored in FIELDS.
605*3d8817e4Smiod    Relocs are queued as we go via other callbacks.
606*3d8817e4Smiod 
607*3d8817e4Smiod    ??? Note that this is currently an all-or-nothing parser.  If we fail to
608*3d8817e4Smiod    parse the instruction, we return 0 and the caller will start over from
609*3d8817e4Smiod    the beginning.  Backtracking will be necessary in parsing subexpressions,
610*3d8817e4Smiod    but that can be handled there.  Not handling backtracking here may get
611*3d8817e4Smiod    expensive in the case of the m68k.  Deal with later.
612*3d8817e4Smiod 
613*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
614*3d8817e4Smiod 
615*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)616*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
617*3d8817e4Smiod 		   const CGEN_INSN *insn,
618*3d8817e4Smiod 		   const char **strp,
619*3d8817e4Smiod 		   CGEN_FIELDS *fields)
620*3d8817e4Smiod {
621*3d8817e4Smiod   /* ??? Runtime added insns not handled yet.  */
622*3d8817e4Smiod   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
623*3d8817e4Smiod   const char *str = *strp;
624*3d8817e4Smiod   const char *errmsg;
625*3d8817e4Smiod   const char *p;
626*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE * syn;
627*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
628*3d8817e4Smiod   /* FIXME: wip */
629*3d8817e4Smiod   int past_opcode_p;
630*3d8817e4Smiod #endif
631*3d8817e4Smiod 
632*3d8817e4Smiod   /* For now we assume the mnemonic is first (there are no leading operands).
633*3d8817e4Smiod      We can parse it without needing to set up operand parsing.
634*3d8817e4Smiod      GAS's input scrubber will ensure mnemonics are lowercase, but we may
635*3d8817e4Smiod      not be called from GAS.  */
636*3d8817e4Smiod   p = CGEN_INSN_MNEMONIC (insn);
637*3d8817e4Smiod   while (*p && TOLOWER (*p) == TOLOWER (*str))
638*3d8817e4Smiod     ++p, ++str;
639*3d8817e4Smiod 
640*3d8817e4Smiod   if (* p)
641*3d8817e4Smiod     return _("unrecognized instruction");
642*3d8817e4Smiod 
643*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
644*3d8817e4Smiod   if (* str && ! ISSPACE (* str))
645*3d8817e4Smiod     return _("unrecognized instruction");
646*3d8817e4Smiod #endif
647*3d8817e4Smiod 
648*3d8817e4Smiod   CGEN_INIT_PARSE (cd);
649*3d8817e4Smiod   cgen_init_parse_operand (cd);
650*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
651*3d8817e4Smiod   past_opcode_p = 0;
652*3d8817e4Smiod #endif
653*3d8817e4Smiod 
654*3d8817e4Smiod   /* We don't check for (*str != '\0') here because we want to parse
655*3d8817e4Smiod      any trailing fake arguments in the syntax string.  */
656*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (syntax);
657*3d8817e4Smiod 
658*3d8817e4Smiod   /* Mnemonics come first for now, ensure valid string.  */
659*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
660*3d8817e4Smiod     abort ();
661*3d8817e4Smiod 
662*3d8817e4Smiod   ++syn;
663*3d8817e4Smiod 
664*3d8817e4Smiod   while (* syn != 0)
665*3d8817e4Smiod     {
666*3d8817e4Smiod       /* Non operand chars must match exactly.  */
667*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
668*3d8817e4Smiod 	{
669*3d8817e4Smiod 	  /* FIXME: While we allow for non-GAS callers above, we assume the
670*3d8817e4Smiod 	     first char after the mnemonic part is a space.  */
671*3d8817e4Smiod 	  /* FIXME: We also take inappropriate advantage of the fact that
672*3d8817e4Smiod 	     GAS's input scrubber will remove extraneous blanks.  */
673*3d8817e4Smiod 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
674*3d8817e4Smiod 	    {
675*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
676*3d8817e4Smiod 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
677*3d8817e4Smiod 		past_opcode_p = 1;
678*3d8817e4Smiod #endif
679*3d8817e4Smiod 	      ++ syn;
680*3d8817e4Smiod 	      ++ str;
681*3d8817e4Smiod 	    }
682*3d8817e4Smiod 	  else if (*str)
683*3d8817e4Smiod 	    {
684*3d8817e4Smiod 	      /* Syntax char didn't match.  Can't be this insn.  */
685*3d8817e4Smiod 	      static char msg [80];
686*3d8817e4Smiod 
687*3d8817e4Smiod 	      /* xgettext:c-format */
688*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
689*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn), *str);
690*3d8817e4Smiod 	      return msg;
691*3d8817e4Smiod 	    }
692*3d8817e4Smiod 	  else
693*3d8817e4Smiod 	    {
694*3d8817e4Smiod 	      /* Ran out of input.  */
695*3d8817e4Smiod 	      static char msg [80];
696*3d8817e4Smiod 
697*3d8817e4Smiod 	      /* xgettext:c-format */
698*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
699*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn));
700*3d8817e4Smiod 	      return msg;
701*3d8817e4Smiod 	    }
702*3d8817e4Smiod 	  continue;
703*3d8817e4Smiod 	}
704*3d8817e4Smiod 
705*3d8817e4Smiod       /* We have an operand of some sort.  */
706*3d8817e4Smiod       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
707*3d8817e4Smiod 					  &str, fields);
708*3d8817e4Smiod       if (errmsg)
709*3d8817e4Smiod 	return errmsg;
710*3d8817e4Smiod 
711*3d8817e4Smiod       /* Done with this operand, continue with next one.  */
712*3d8817e4Smiod       ++ syn;
713*3d8817e4Smiod     }
714*3d8817e4Smiod 
715*3d8817e4Smiod   /* If we're at the end of the syntax string, we're done.  */
716*3d8817e4Smiod   if (* syn == 0)
717*3d8817e4Smiod     {
718*3d8817e4Smiod       /* FIXME: For the moment we assume a valid `str' can only contain
719*3d8817e4Smiod 	 blanks now.  IE: We needn't try again with a longer version of
720*3d8817e4Smiod 	 the insn and it is assumed that longer versions of insns appear
721*3d8817e4Smiod 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
722*3d8817e4Smiod       while (ISSPACE (* str))
723*3d8817e4Smiod 	++ str;
724*3d8817e4Smiod 
725*3d8817e4Smiod       if (* str != '\0')
726*3d8817e4Smiod 	return _("junk at end of line"); /* FIXME: would like to include `str' */
727*3d8817e4Smiod 
728*3d8817e4Smiod       return NULL;
729*3d8817e4Smiod     }
730*3d8817e4Smiod 
731*3d8817e4Smiod   /* We couldn't parse it.  */
732*3d8817e4Smiod   return _("unrecognized instruction");
733*3d8817e4Smiod }
734*3d8817e4Smiod 
735*3d8817e4Smiod /* Main entry point.
736*3d8817e4Smiod    This routine is called for each instruction to be assembled.
737*3d8817e4Smiod    STR points to the insn to be assembled.
738*3d8817e4Smiod    We assume all necessary tables have been initialized.
739*3d8817e4Smiod    The assembled instruction, less any fixups, is stored in BUF.
740*3d8817e4Smiod    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
741*3d8817e4Smiod    still needs to be converted to target byte order, otherwise BUF is an array
742*3d8817e4Smiod    of bytes in target byte order.
743*3d8817e4Smiod    The result is a pointer to the insn's entry in the opcode table,
744*3d8817e4Smiod    or NULL if an error occured (an error message will have already been
745*3d8817e4Smiod    printed).
746*3d8817e4Smiod 
747*3d8817e4Smiod    Note that when processing (non-alias) macro-insns,
748*3d8817e4Smiod    this function recurses.
749*3d8817e4Smiod 
750*3d8817e4Smiod    ??? It's possible to make this cpu-independent.
751*3d8817e4Smiod    One would have to deal with a few minor things.
752*3d8817e4Smiod    At this point in time doing so would be more of a curiosity than useful
753*3d8817e4Smiod    [for example this file isn't _that_ big], but keeping the possibility in
754*3d8817e4Smiod    mind helps keep the design clean.  */
755*3d8817e4Smiod 
756*3d8817e4Smiod const CGEN_INSN *
iq2000_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)757*3d8817e4Smiod iq2000_cgen_assemble_insn (CGEN_CPU_DESC cd,
758*3d8817e4Smiod 			   const char *str,
759*3d8817e4Smiod 			   CGEN_FIELDS *fields,
760*3d8817e4Smiod 			   CGEN_INSN_BYTES_PTR buf,
761*3d8817e4Smiod 			   char **errmsg)
762*3d8817e4Smiod {
763*3d8817e4Smiod   const char *start;
764*3d8817e4Smiod   CGEN_INSN_LIST *ilist;
765*3d8817e4Smiod   const char *parse_errmsg = NULL;
766*3d8817e4Smiod   const char *insert_errmsg = NULL;
767*3d8817e4Smiod   int recognized_mnemonic = 0;
768*3d8817e4Smiod 
769*3d8817e4Smiod   /* Skip leading white space.  */
770*3d8817e4Smiod   while (ISSPACE (* str))
771*3d8817e4Smiod     ++ str;
772*3d8817e4Smiod 
773*3d8817e4Smiod   /* The instructions are stored in hashed lists.
774*3d8817e4Smiod      Get the first in the list.  */
775*3d8817e4Smiod   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
776*3d8817e4Smiod 
777*3d8817e4Smiod   /* Keep looking until we find a match.  */
778*3d8817e4Smiod   start = str;
779*3d8817e4Smiod   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
780*3d8817e4Smiod     {
781*3d8817e4Smiod       const CGEN_INSN *insn = ilist->insn;
782*3d8817e4Smiod       recognized_mnemonic = 1;
783*3d8817e4Smiod 
784*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
785*3d8817e4Smiod       /* Not usually needed as unsupported opcodes
786*3d8817e4Smiod 	 shouldn't be in the hash lists.  */
787*3d8817e4Smiod       /* Is this insn supported by the selected cpu?  */
788*3d8817e4Smiod       if (! iq2000_cgen_insn_supported (cd, insn))
789*3d8817e4Smiod 	continue;
790*3d8817e4Smiod #endif
791*3d8817e4Smiod       /* If the RELAXED attribute is set, this is an insn that shouldn't be
792*3d8817e4Smiod 	 chosen immediately.  Instead, it is used during assembler/linker
793*3d8817e4Smiod 	 relaxation if possible.  */
794*3d8817e4Smiod       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
795*3d8817e4Smiod 	continue;
796*3d8817e4Smiod 
797*3d8817e4Smiod       str = start;
798*3d8817e4Smiod 
799*3d8817e4Smiod       /* Skip this insn if str doesn't look right lexically.  */
800*3d8817e4Smiod       if (CGEN_INSN_RX (insn) != NULL &&
801*3d8817e4Smiod 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
802*3d8817e4Smiod 	continue;
803*3d8817e4Smiod 
804*3d8817e4Smiod       /* Allow parse/insert handlers to obtain length of insn.  */
805*3d8817e4Smiod       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
806*3d8817e4Smiod 
807*3d8817e4Smiod       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
808*3d8817e4Smiod       if (parse_errmsg != NULL)
809*3d8817e4Smiod 	continue;
810*3d8817e4Smiod 
811*3d8817e4Smiod       /* ??? 0 is passed for `pc'.  */
812*3d8817e4Smiod       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
813*3d8817e4Smiod 						 (bfd_vma) 0);
814*3d8817e4Smiod       if (insert_errmsg != NULL)
815*3d8817e4Smiod         continue;
816*3d8817e4Smiod 
817*3d8817e4Smiod       /* It is up to the caller to actually output the insn and any
818*3d8817e4Smiod          queued relocs.  */
819*3d8817e4Smiod       return insn;
820*3d8817e4Smiod     }
821*3d8817e4Smiod 
822*3d8817e4Smiod   {
823*3d8817e4Smiod     static char errbuf[150];
824*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
825*3d8817e4Smiod     const char *tmp_errmsg;
826*3d8817e4Smiod 
827*3d8817e4Smiod     /* If requesting verbose error messages, use insert_errmsg.
828*3d8817e4Smiod        Failing that, use parse_errmsg.  */
829*3d8817e4Smiod     tmp_errmsg = (insert_errmsg ? insert_errmsg :
830*3d8817e4Smiod 		  parse_errmsg ? parse_errmsg :
831*3d8817e4Smiod 		  recognized_mnemonic ?
832*3d8817e4Smiod 		  _("unrecognized form of instruction") :
833*3d8817e4Smiod 		  _("unrecognized instruction"));
834*3d8817e4Smiod 
835*3d8817e4Smiod     if (strlen (start) > 50)
836*3d8817e4Smiod       /* xgettext:c-format */
837*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
838*3d8817e4Smiod     else
839*3d8817e4Smiod       /* xgettext:c-format */
840*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
841*3d8817e4Smiod #else
842*3d8817e4Smiod     if (strlen (start) > 50)
843*3d8817e4Smiod       /* xgettext:c-format */
844*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
845*3d8817e4Smiod     else
846*3d8817e4Smiod       /* xgettext:c-format */
847*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s'"), start);
848*3d8817e4Smiod #endif
849*3d8817e4Smiod 
850*3d8817e4Smiod     *errmsg = errbuf;
851*3d8817e4Smiod     return NULL;
852*3d8817e4Smiod   }
853*3d8817e4Smiod }
854