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 "fr30-desc.h"
35*3d8817e4Smiod #include "fr30-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 /* Handle register lists for LDMx and STMx.  */
53*3d8817e4Smiod 
54*3d8817e4Smiod static int
parse_register_number(const char ** strp)55*3d8817e4Smiod parse_register_number (const char **strp)
56*3d8817e4Smiod {
57*3d8817e4Smiod   int regno;
58*3d8817e4Smiod 
59*3d8817e4Smiod   if (**strp < '0' || **strp > '9')
60*3d8817e4Smiod     return -1; /* Error.  */
61*3d8817e4Smiod   regno = **strp - '0';
62*3d8817e4Smiod   ++*strp;
63*3d8817e4Smiod 
64*3d8817e4Smiod   if (**strp >= '0' && **strp <= '9')
65*3d8817e4Smiod     {
66*3d8817e4Smiod       regno = regno * 10 + (**strp - '0');
67*3d8817e4Smiod       ++*strp;
68*3d8817e4Smiod     }
69*3d8817e4Smiod 
70*3d8817e4Smiod   return regno;
71*3d8817e4Smiod }
72*3d8817e4Smiod 
73*3d8817e4Smiod static const char *
parse_register_list(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,const char ** strp,int opindex ATTRIBUTE_UNUSED,unsigned long * valuep,int high_low,int load_store)74*3d8817e4Smiod parse_register_list (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
75*3d8817e4Smiod 		     const char **strp,
76*3d8817e4Smiod 		     int opindex ATTRIBUTE_UNUSED,
77*3d8817e4Smiod 		     unsigned long *valuep,
78*3d8817e4Smiod 		     int high_low,   /* 0 == high, 1 == low.  */
79*3d8817e4Smiod 		     int load_store) /* 0 == load, 1 == store.  */
80*3d8817e4Smiod {
81*3d8817e4Smiod   *valuep = 0;
82*3d8817e4Smiod   while (**strp && **strp != ')')
83*3d8817e4Smiod     {
84*3d8817e4Smiod       int regno;
85*3d8817e4Smiod 
86*3d8817e4Smiod       if (**strp != 'R' && **strp != 'r')
87*3d8817e4Smiod 	break;
88*3d8817e4Smiod       ++*strp;
89*3d8817e4Smiod 
90*3d8817e4Smiod       regno = parse_register_number (strp);
91*3d8817e4Smiod       if (regno == -1)
92*3d8817e4Smiod 	return _("Register number is not valid");
93*3d8817e4Smiod       if (regno > 7 && !high_low)
94*3d8817e4Smiod 	return _("Register must be between r0 and r7");
95*3d8817e4Smiod       if (regno < 8 && high_low)
96*3d8817e4Smiod 	return _("Register must be between r8 and r15");
97*3d8817e4Smiod 
98*3d8817e4Smiod       if (high_low)
99*3d8817e4Smiod 	regno -= 8;
100*3d8817e4Smiod 
101*3d8817e4Smiod       if (load_store) /* Mask is reversed for store.  */
102*3d8817e4Smiod 	*valuep |= 0x80 >> regno;
103*3d8817e4Smiod       else
104*3d8817e4Smiod 	*valuep |= 1 << regno;
105*3d8817e4Smiod 
106*3d8817e4Smiod       if (**strp == ',')
107*3d8817e4Smiod 	{
108*3d8817e4Smiod 	  if (*(*strp + 1) == ')')
109*3d8817e4Smiod 	    break;
110*3d8817e4Smiod 	  ++*strp;
111*3d8817e4Smiod 	}
112*3d8817e4Smiod     }
113*3d8817e4Smiod 
114*3d8817e4Smiod   if (!*strp || **strp != ')')
115*3d8817e4Smiod     return _("Register list is not valid");
116*3d8817e4Smiod 
117*3d8817e4Smiod   return NULL;
118*3d8817e4Smiod }
119*3d8817e4Smiod 
120*3d8817e4Smiod static const char *
parse_low_register_list_ld(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)121*3d8817e4Smiod parse_low_register_list_ld (CGEN_CPU_DESC cd,
122*3d8817e4Smiod 			    const char **strp,
123*3d8817e4Smiod 			    int opindex,
124*3d8817e4Smiod 			    unsigned long *valuep)
125*3d8817e4Smiod {
126*3d8817e4Smiod   return parse_register_list (cd, strp, opindex, valuep,
127*3d8817e4Smiod 			      0 /* Low.  */, 0 /* Load.  */);
128*3d8817e4Smiod }
129*3d8817e4Smiod 
130*3d8817e4Smiod static const char *
parse_hi_register_list_ld(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)131*3d8817e4Smiod parse_hi_register_list_ld (CGEN_CPU_DESC cd,
132*3d8817e4Smiod 			   const char **strp,
133*3d8817e4Smiod 			   int opindex,
134*3d8817e4Smiod 			   unsigned long *valuep)
135*3d8817e4Smiod {
136*3d8817e4Smiod   return parse_register_list (cd, strp, opindex, valuep,
137*3d8817e4Smiod 			      1 /* High.  */, 0 /* Load.  */);
138*3d8817e4Smiod }
139*3d8817e4Smiod 
140*3d8817e4Smiod static const char *
parse_low_register_list_st(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)141*3d8817e4Smiod parse_low_register_list_st (CGEN_CPU_DESC cd,
142*3d8817e4Smiod 			    const char **strp,
143*3d8817e4Smiod 			    int opindex,
144*3d8817e4Smiod 			    unsigned long *valuep)
145*3d8817e4Smiod {
146*3d8817e4Smiod   return parse_register_list (cd, strp, opindex, valuep,
147*3d8817e4Smiod 			      0 /* Low.  */, 1 /* Store.  */);
148*3d8817e4Smiod }
149*3d8817e4Smiod 
150*3d8817e4Smiod static const char *
parse_hi_register_list_st(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)151*3d8817e4Smiod parse_hi_register_list_st (CGEN_CPU_DESC cd,
152*3d8817e4Smiod 			   const char **strp,
153*3d8817e4Smiod 			   int opindex,
154*3d8817e4Smiod 			   unsigned long *valuep)
155*3d8817e4Smiod {
156*3d8817e4Smiod   return parse_register_list (cd, strp, opindex, valuep,
157*3d8817e4Smiod 			      1 /* High.  */, 1 /* Store.  */);
158*3d8817e4Smiod }
159*3d8817e4Smiod 
160*3d8817e4Smiod /* -- */
161*3d8817e4Smiod 
162*3d8817e4Smiod const char * fr30_cgen_parse_operand
163*3d8817e4Smiod   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
164*3d8817e4Smiod 
165*3d8817e4Smiod /* Main entry point for operand parsing.
166*3d8817e4Smiod 
167*3d8817e4Smiod    This function is basically just a big switch statement.  Earlier versions
168*3d8817e4Smiod    used tables to look up the function to use, but
169*3d8817e4Smiod    - if the table contains both assembler and disassembler functions then
170*3d8817e4Smiod      the disassembler contains much of the assembler and vice-versa,
171*3d8817e4Smiod    - there's a lot of inlining possibilities as things grow,
172*3d8817e4Smiod    - using a switch statement avoids the function call overhead.
173*3d8817e4Smiod 
174*3d8817e4Smiod    This function could be moved into `parse_insn_normal', but keeping it
175*3d8817e4Smiod    separate makes clear the interface between `parse_insn_normal' and each of
176*3d8817e4Smiod    the handlers.  */
177*3d8817e4Smiod 
178*3d8817e4Smiod const char *
fr30_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)179*3d8817e4Smiod fr30_cgen_parse_operand (CGEN_CPU_DESC cd,
180*3d8817e4Smiod 			   int opindex,
181*3d8817e4Smiod 			   const char ** strp,
182*3d8817e4Smiod 			   CGEN_FIELDS * fields)
183*3d8817e4Smiod {
184*3d8817e4Smiod   const char * errmsg = NULL;
185*3d8817e4Smiod   /* Used by scalar operands that still need to be parsed.  */
186*3d8817e4Smiod   long junk ATTRIBUTE_UNUSED;
187*3d8817e4Smiod 
188*3d8817e4Smiod   switch (opindex)
189*3d8817e4Smiod     {
190*3d8817e4Smiod     case FR30_OPERAND_CRI :
191*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi);
192*3d8817e4Smiod       break;
193*3d8817e4Smiod     case FR30_OPERAND_CRJ :
194*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj);
195*3d8817e4Smiod       break;
196*3d8817e4Smiod     case FR30_OPERAND_R13 :
197*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk);
198*3d8817e4Smiod       break;
199*3d8817e4Smiod     case FR30_OPERAND_R14 :
200*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk);
201*3d8817e4Smiod       break;
202*3d8817e4Smiod     case FR30_OPERAND_R15 :
203*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk);
204*3d8817e4Smiod       break;
205*3d8817e4Smiod     case FR30_OPERAND_RI :
206*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri);
207*3d8817e4Smiod       break;
208*3d8817e4Smiod     case FR30_OPERAND_RIC :
209*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric);
210*3d8817e4Smiod       break;
211*3d8817e4Smiod     case FR30_OPERAND_RJ :
212*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj);
213*3d8817e4Smiod       break;
214*3d8817e4Smiod     case FR30_OPERAND_RJC :
215*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc);
216*3d8817e4Smiod       break;
217*3d8817e4Smiod     case FR30_OPERAND_RS1 :
218*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1);
219*3d8817e4Smiod       break;
220*3d8817e4Smiod     case FR30_OPERAND_RS2 :
221*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2);
222*3d8817e4Smiod       break;
223*3d8817e4Smiod     case FR30_OPERAND_CC :
224*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, (unsigned long *) (& fields->f_cc));
225*3d8817e4Smiod       break;
226*3d8817e4Smiod     case FR30_OPERAND_CCC :
227*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, (unsigned long *) (& fields->f_ccc));
228*3d8817e4Smiod       break;
229*3d8817e4Smiod     case FR30_OPERAND_DIR10 :
230*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, (unsigned long *) (& fields->f_dir10));
231*3d8817e4Smiod       break;
232*3d8817e4Smiod     case FR30_OPERAND_DIR8 :
233*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, (unsigned long *) (& fields->f_dir8));
234*3d8817e4Smiod       break;
235*3d8817e4Smiod     case FR30_OPERAND_DIR9 :
236*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, (unsigned long *) (& fields->f_dir9));
237*3d8817e4Smiod       break;
238*3d8817e4Smiod     case FR30_OPERAND_DISP10 :
239*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, (long *) (& fields->f_disp10));
240*3d8817e4Smiod       break;
241*3d8817e4Smiod     case FR30_OPERAND_DISP8 :
242*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, (long *) (& fields->f_disp8));
243*3d8817e4Smiod       break;
244*3d8817e4Smiod     case FR30_OPERAND_DISP9 :
245*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, (long *) (& fields->f_disp9));
246*3d8817e4Smiod       break;
247*3d8817e4Smiod     case FR30_OPERAND_I20 :
248*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, (unsigned long *) (& fields->f_i20));
249*3d8817e4Smiod       break;
250*3d8817e4Smiod     case FR30_OPERAND_I32 :
251*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, (unsigned long *) (& fields->f_i32));
252*3d8817e4Smiod       break;
253*3d8817e4Smiod     case FR30_OPERAND_I8 :
254*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, (unsigned long *) (& fields->f_i8));
255*3d8817e4Smiod       break;
256*3d8817e4Smiod     case FR30_OPERAND_LABEL12 :
257*3d8817e4Smiod       {
258*3d8817e4Smiod         bfd_vma value = 0;
259*3d8817e4Smiod         errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL,  & value);
260*3d8817e4Smiod         fields->f_rel12 = value;
261*3d8817e4Smiod       }
262*3d8817e4Smiod       break;
263*3d8817e4Smiod     case FR30_OPERAND_LABEL9 :
264*3d8817e4Smiod       {
265*3d8817e4Smiod         bfd_vma value = 0;
266*3d8817e4Smiod         errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL,  & value);
267*3d8817e4Smiod         fields->f_rel9 = value;
268*3d8817e4Smiod       }
269*3d8817e4Smiod       break;
270*3d8817e4Smiod     case FR30_OPERAND_M4 :
271*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, (long *) (& fields->f_m4));
272*3d8817e4Smiod       break;
273*3d8817e4Smiod     case FR30_OPERAND_PS :
274*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk);
275*3d8817e4Smiod       break;
276*3d8817e4Smiod     case FR30_OPERAND_REGLIST_HI_LD :
277*3d8817e4Smiod       errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, (unsigned long *) (& fields->f_reglist_hi_ld));
278*3d8817e4Smiod       break;
279*3d8817e4Smiod     case FR30_OPERAND_REGLIST_HI_ST :
280*3d8817e4Smiod       errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, (unsigned long *) (& fields->f_reglist_hi_st));
281*3d8817e4Smiod       break;
282*3d8817e4Smiod     case FR30_OPERAND_REGLIST_LOW_LD :
283*3d8817e4Smiod       errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, (unsigned long *) (& fields->f_reglist_low_ld));
284*3d8817e4Smiod       break;
285*3d8817e4Smiod     case FR30_OPERAND_REGLIST_LOW_ST :
286*3d8817e4Smiod       errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, (unsigned long *) (& fields->f_reglist_low_st));
287*3d8817e4Smiod       break;
288*3d8817e4Smiod     case FR30_OPERAND_S10 :
289*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, (long *) (& fields->f_s10));
290*3d8817e4Smiod       break;
291*3d8817e4Smiod     case FR30_OPERAND_U10 :
292*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, (unsigned long *) (& fields->f_u10));
293*3d8817e4Smiod       break;
294*3d8817e4Smiod     case FR30_OPERAND_U4 :
295*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, (unsigned long *) (& fields->f_u4));
296*3d8817e4Smiod       break;
297*3d8817e4Smiod     case FR30_OPERAND_U4C :
298*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, (unsigned long *) (& fields->f_u4c));
299*3d8817e4Smiod       break;
300*3d8817e4Smiod     case FR30_OPERAND_U8 :
301*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, (unsigned long *) (& fields->f_u8));
302*3d8817e4Smiod       break;
303*3d8817e4Smiod     case FR30_OPERAND_UDISP6 :
304*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, (unsigned long *) (& fields->f_udisp6));
305*3d8817e4Smiod       break;
306*3d8817e4Smiod 
307*3d8817e4Smiod     default :
308*3d8817e4Smiod       /* xgettext:c-format */
309*3d8817e4Smiod       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
310*3d8817e4Smiod       abort ();
311*3d8817e4Smiod   }
312*3d8817e4Smiod 
313*3d8817e4Smiod   return errmsg;
314*3d8817e4Smiod }
315*3d8817e4Smiod 
316*3d8817e4Smiod cgen_parse_fn * const fr30_cgen_parse_handlers[] =
317*3d8817e4Smiod {
318*3d8817e4Smiod   parse_insn_normal,
319*3d8817e4Smiod };
320*3d8817e4Smiod 
321*3d8817e4Smiod void
fr30_cgen_init_asm(CGEN_CPU_DESC cd)322*3d8817e4Smiod fr30_cgen_init_asm (CGEN_CPU_DESC cd)
323*3d8817e4Smiod {
324*3d8817e4Smiod   fr30_cgen_init_opcode_table (cd);
325*3d8817e4Smiod   fr30_cgen_init_ibld_table (cd);
326*3d8817e4Smiod   cd->parse_handlers = & fr30_cgen_parse_handlers[0];
327*3d8817e4Smiod   cd->parse_operand = fr30_cgen_parse_operand;
328*3d8817e4Smiod }
329*3d8817e4Smiod 
330*3d8817e4Smiod 
331*3d8817e4Smiod 
332*3d8817e4Smiod /* Regex construction routine.
333*3d8817e4Smiod 
334*3d8817e4Smiod    This translates an opcode syntax string into a regex string,
335*3d8817e4Smiod    by replacing any non-character syntax element (such as an
336*3d8817e4Smiod    opcode) with the pattern '.*'
337*3d8817e4Smiod 
338*3d8817e4Smiod    It then compiles the regex and stores it in the opcode, for
339*3d8817e4Smiod    later use by fr30_cgen_assemble_insn
340*3d8817e4Smiod 
341*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
342*3d8817e4Smiod 
343*3d8817e4Smiod char *
fr30_cgen_build_insn_regex(CGEN_INSN * insn)344*3d8817e4Smiod fr30_cgen_build_insn_regex (CGEN_INSN *insn)
345*3d8817e4Smiod {
346*3d8817e4Smiod   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
347*3d8817e4Smiod   const char *mnem = CGEN_INSN_MNEMONIC (insn);
348*3d8817e4Smiod   char rxbuf[CGEN_MAX_RX_ELEMENTS];
349*3d8817e4Smiod   char *rx = rxbuf;
350*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE *syn;
351*3d8817e4Smiod   int reg_err;
352*3d8817e4Smiod 
353*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
354*3d8817e4Smiod 
355*3d8817e4Smiod   /* Mnemonics come first in the syntax string.  */
356*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
357*3d8817e4Smiod     return _("missing mnemonic in syntax string");
358*3d8817e4Smiod   ++syn;
359*3d8817e4Smiod 
360*3d8817e4Smiod   /* Generate a case sensitive regular expression that emulates case
361*3d8817e4Smiod      insensitive matching in the "C" locale.  We cannot generate a case
362*3d8817e4Smiod      insensitive regular expression because in Turkish locales, 'i' and 'I'
363*3d8817e4Smiod      are not equal modulo case conversion.  */
364*3d8817e4Smiod 
365*3d8817e4Smiod   /* Copy the literal mnemonic out of the insn.  */
366*3d8817e4Smiod   for (; *mnem; mnem++)
367*3d8817e4Smiod     {
368*3d8817e4Smiod       char c = *mnem;
369*3d8817e4Smiod 
370*3d8817e4Smiod       if (ISALPHA (c))
371*3d8817e4Smiod 	{
372*3d8817e4Smiod 	  *rx++ = '[';
373*3d8817e4Smiod 	  *rx++ = TOLOWER (c);
374*3d8817e4Smiod 	  *rx++ = TOUPPER (c);
375*3d8817e4Smiod 	  *rx++ = ']';
376*3d8817e4Smiod 	}
377*3d8817e4Smiod       else
378*3d8817e4Smiod 	*rx++ = c;
379*3d8817e4Smiod     }
380*3d8817e4Smiod 
381*3d8817e4Smiod   /* Copy any remaining literals from the syntax string into the rx.  */
382*3d8817e4Smiod   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
383*3d8817e4Smiod     {
384*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
385*3d8817e4Smiod 	{
386*3d8817e4Smiod 	  char c = CGEN_SYNTAX_CHAR (* syn);
387*3d8817e4Smiod 
388*3d8817e4Smiod 	  switch (c)
389*3d8817e4Smiod 	    {
390*3d8817e4Smiod 	      /* Escape any regex metacharacters in the syntax.  */
391*3d8817e4Smiod 	    case '.': case '[': case '\\':
392*3d8817e4Smiod 	    case '*': case '^': case '$':
393*3d8817e4Smiod 
394*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
395*3d8817e4Smiod 	    case '?': case '{': case '}':
396*3d8817e4Smiod 	    case '(': case ')': case '*':
397*3d8817e4Smiod 	    case '|': case '+': case ']':
398*3d8817e4Smiod #endif
399*3d8817e4Smiod 	      *rx++ = '\\';
400*3d8817e4Smiod 	      *rx++ = c;
401*3d8817e4Smiod 	      break;
402*3d8817e4Smiod 
403*3d8817e4Smiod 	    default:
404*3d8817e4Smiod 	      if (ISALPHA (c))
405*3d8817e4Smiod 		{
406*3d8817e4Smiod 		  *rx++ = '[';
407*3d8817e4Smiod 		  *rx++ = TOLOWER (c);
408*3d8817e4Smiod 		  *rx++ = TOUPPER (c);
409*3d8817e4Smiod 		  *rx++ = ']';
410*3d8817e4Smiod 		}
411*3d8817e4Smiod 	      else
412*3d8817e4Smiod 		*rx++ = c;
413*3d8817e4Smiod 	      break;
414*3d8817e4Smiod 	    }
415*3d8817e4Smiod 	}
416*3d8817e4Smiod       else
417*3d8817e4Smiod 	{
418*3d8817e4Smiod 	  /* Replace non-syntax fields with globs.  */
419*3d8817e4Smiod 	  *rx++ = '.';
420*3d8817e4Smiod 	  *rx++ = '*';
421*3d8817e4Smiod 	}
422*3d8817e4Smiod     }
423*3d8817e4Smiod 
424*3d8817e4Smiod   /* Trailing whitespace ok.  */
425*3d8817e4Smiod   * rx++ = '[';
426*3d8817e4Smiod   * rx++ = ' ';
427*3d8817e4Smiod   * rx++ = '\t';
428*3d8817e4Smiod   * rx++ = ']';
429*3d8817e4Smiod   * rx++ = '*';
430*3d8817e4Smiod 
431*3d8817e4Smiod   /* But anchor it after that.  */
432*3d8817e4Smiod   * rx++ = '$';
433*3d8817e4Smiod   * rx = '\0';
434*3d8817e4Smiod 
435*3d8817e4Smiod   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
436*3d8817e4Smiod   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
437*3d8817e4Smiod 
438*3d8817e4Smiod   if (reg_err == 0)
439*3d8817e4Smiod     return NULL;
440*3d8817e4Smiod   else
441*3d8817e4Smiod     {
442*3d8817e4Smiod       static char msg[80];
443*3d8817e4Smiod 
444*3d8817e4Smiod       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
445*3d8817e4Smiod       regfree ((regex_t *) CGEN_INSN_RX (insn));
446*3d8817e4Smiod       free (CGEN_INSN_RX (insn));
447*3d8817e4Smiod       (CGEN_INSN_RX (insn)) = NULL;
448*3d8817e4Smiod       return msg;
449*3d8817e4Smiod     }
450*3d8817e4Smiod }
451*3d8817e4Smiod 
452*3d8817e4Smiod 
453*3d8817e4Smiod /* Default insn parser.
454*3d8817e4Smiod 
455*3d8817e4Smiod    The syntax string is scanned and operands are parsed and stored in FIELDS.
456*3d8817e4Smiod    Relocs are queued as we go via other callbacks.
457*3d8817e4Smiod 
458*3d8817e4Smiod    ??? Note that this is currently an all-or-nothing parser.  If we fail to
459*3d8817e4Smiod    parse the instruction, we return 0 and the caller will start over from
460*3d8817e4Smiod    the beginning.  Backtracking will be necessary in parsing subexpressions,
461*3d8817e4Smiod    but that can be handled there.  Not handling backtracking here may get
462*3d8817e4Smiod    expensive in the case of the m68k.  Deal with later.
463*3d8817e4Smiod 
464*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
465*3d8817e4Smiod 
466*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)467*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
468*3d8817e4Smiod 		   const CGEN_INSN *insn,
469*3d8817e4Smiod 		   const char **strp,
470*3d8817e4Smiod 		   CGEN_FIELDS *fields)
471*3d8817e4Smiod {
472*3d8817e4Smiod   /* ??? Runtime added insns not handled yet.  */
473*3d8817e4Smiod   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
474*3d8817e4Smiod   const char *str = *strp;
475*3d8817e4Smiod   const char *errmsg;
476*3d8817e4Smiod   const char *p;
477*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE * syn;
478*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
479*3d8817e4Smiod   /* FIXME: wip */
480*3d8817e4Smiod   int past_opcode_p;
481*3d8817e4Smiod #endif
482*3d8817e4Smiod 
483*3d8817e4Smiod   /* For now we assume the mnemonic is first (there are no leading operands).
484*3d8817e4Smiod      We can parse it without needing to set up operand parsing.
485*3d8817e4Smiod      GAS's input scrubber will ensure mnemonics are lowercase, but we may
486*3d8817e4Smiod      not be called from GAS.  */
487*3d8817e4Smiod   p = CGEN_INSN_MNEMONIC (insn);
488*3d8817e4Smiod   while (*p && TOLOWER (*p) == TOLOWER (*str))
489*3d8817e4Smiod     ++p, ++str;
490*3d8817e4Smiod 
491*3d8817e4Smiod   if (* p)
492*3d8817e4Smiod     return _("unrecognized instruction");
493*3d8817e4Smiod 
494*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
495*3d8817e4Smiod   if (* str && ! ISSPACE (* str))
496*3d8817e4Smiod     return _("unrecognized instruction");
497*3d8817e4Smiod #endif
498*3d8817e4Smiod 
499*3d8817e4Smiod   CGEN_INIT_PARSE (cd);
500*3d8817e4Smiod   cgen_init_parse_operand (cd);
501*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
502*3d8817e4Smiod   past_opcode_p = 0;
503*3d8817e4Smiod #endif
504*3d8817e4Smiod 
505*3d8817e4Smiod   /* We don't check for (*str != '\0') here because we want to parse
506*3d8817e4Smiod      any trailing fake arguments in the syntax string.  */
507*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (syntax);
508*3d8817e4Smiod 
509*3d8817e4Smiod   /* Mnemonics come first for now, ensure valid string.  */
510*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
511*3d8817e4Smiod     abort ();
512*3d8817e4Smiod 
513*3d8817e4Smiod   ++syn;
514*3d8817e4Smiod 
515*3d8817e4Smiod   while (* syn != 0)
516*3d8817e4Smiod     {
517*3d8817e4Smiod       /* Non operand chars must match exactly.  */
518*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
519*3d8817e4Smiod 	{
520*3d8817e4Smiod 	  /* FIXME: While we allow for non-GAS callers above, we assume the
521*3d8817e4Smiod 	     first char after the mnemonic part is a space.  */
522*3d8817e4Smiod 	  /* FIXME: We also take inappropriate advantage of the fact that
523*3d8817e4Smiod 	     GAS's input scrubber will remove extraneous blanks.  */
524*3d8817e4Smiod 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
525*3d8817e4Smiod 	    {
526*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
527*3d8817e4Smiod 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
528*3d8817e4Smiod 		past_opcode_p = 1;
529*3d8817e4Smiod #endif
530*3d8817e4Smiod 	      ++ syn;
531*3d8817e4Smiod 	      ++ str;
532*3d8817e4Smiod 	    }
533*3d8817e4Smiod 	  else if (*str)
534*3d8817e4Smiod 	    {
535*3d8817e4Smiod 	      /* Syntax char didn't match.  Can't be this insn.  */
536*3d8817e4Smiod 	      static char msg [80];
537*3d8817e4Smiod 
538*3d8817e4Smiod 	      /* xgettext:c-format */
539*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
540*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn), *str);
541*3d8817e4Smiod 	      return msg;
542*3d8817e4Smiod 	    }
543*3d8817e4Smiod 	  else
544*3d8817e4Smiod 	    {
545*3d8817e4Smiod 	      /* Ran out of input.  */
546*3d8817e4Smiod 	      static char msg [80];
547*3d8817e4Smiod 
548*3d8817e4Smiod 	      /* xgettext:c-format */
549*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
550*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn));
551*3d8817e4Smiod 	      return msg;
552*3d8817e4Smiod 	    }
553*3d8817e4Smiod 	  continue;
554*3d8817e4Smiod 	}
555*3d8817e4Smiod 
556*3d8817e4Smiod       /* We have an operand of some sort.  */
557*3d8817e4Smiod       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
558*3d8817e4Smiod 					  &str, fields);
559*3d8817e4Smiod       if (errmsg)
560*3d8817e4Smiod 	return errmsg;
561*3d8817e4Smiod 
562*3d8817e4Smiod       /* Done with this operand, continue with next one.  */
563*3d8817e4Smiod       ++ syn;
564*3d8817e4Smiod     }
565*3d8817e4Smiod 
566*3d8817e4Smiod   /* If we're at the end of the syntax string, we're done.  */
567*3d8817e4Smiod   if (* syn == 0)
568*3d8817e4Smiod     {
569*3d8817e4Smiod       /* FIXME: For the moment we assume a valid `str' can only contain
570*3d8817e4Smiod 	 blanks now.  IE: We needn't try again with a longer version of
571*3d8817e4Smiod 	 the insn and it is assumed that longer versions of insns appear
572*3d8817e4Smiod 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
573*3d8817e4Smiod       while (ISSPACE (* str))
574*3d8817e4Smiod 	++ str;
575*3d8817e4Smiod 
576*3d8817e4Smiod       if (* str != '\0')
577*3d8817e4Smiod 	return _("junk at end of line"); /* FIXME: would like to include `str' */
578*3d8817e4Smiod 
579*3d8817e4Smiod       return NULL;
580*3d8817e4Smiod     }
581*3d8817e4Smiod 
582*3d8817e4Smiod   /* We couldn't parse it.  */
583*3d8817e4Smiod   return _("unrecognized instruction");
584*3d8817e4Smiod }
585*3d8817e4Smiod 
586*3d8817e4Smiod /* Main entry point.
587*3d8817e4Smiod    This routine is called for each instruction to be assembled.
588*3d8817e4Smiod    STR points to the insn to be assembled.
589*3d8817e4Smiod    We assume all necessary tables have been initialized.
590*3d8817e4Smiod    The assembled instruction, less any fixups, is stored in BUF.
591*3d8817e4Smiod    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
592*3d8817e4Smiod    still needs to be converted to target byte order, otherwise BUF is an array
593*3d8817e4Smiod    of bytes in target byte order.
594*3d8817e4Smiod    The result is a pointer to the insn's entry in the opcode table,
595*3d8817e4Smiod    or NULL if an error occured (an error message will have already been
596*3d8817e4Smiod    printed).
597*3d8817e4Smiod 
598*3d8817e4Smiod    Note that when processing (non-alias) macro-insns,
599*3d8817e4Smiod    this function recurses.
600*3d8817e4Smiod 
601*3d8817e4Smiod    ??? It's possible to make this cpu-independent.
602*3d8817e4Smiod    One would have to deal with a few minor things.
603*3d8817e4Smiod    At this point in time doing so would be more of a curiosity than useful
604*3d8817e4Smiod    [for example this file isn't _that_ big], but keeping the possibility in
605*3d8817e4Smiod    mind helps keep the design clean.  */
606*3d8817e4Smiod 
607*3d8817e4Smiod const CGEN_INSN *
fr30_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)608*3d8817e4Smiod fr30_cgen_assemble_insn (CGEN_CPU_DESC cd,
609*3d8817e4Smiod 			   const char *str,
610*3d8817e4Smiod 			   CGEN_FIELDS *fields,
611*3d8817e4Smiod 			   CGEN_INSN_BYTES_PTR buf,
612*3d8817e4Smiod 			   char **errmsg)
613*3d8817e4Smiod {
614*3d8817e4Smiod   const char *start;
615*3d8817e4Smiod   CGEN_INSN_LIST *ilist;
616*3d8817e4Smiod   const char *parse_errmsg = NULL;
617*3d8817e4Smiod   const char *insert_errmsg = NULL;
618*3d8817e4Smiod   int recognized_mnemonic = 0;
619*3d8817e4Smiod 
620*3d8817e4Smiod   /* Skip leading white space.  */
621*3d8817e4Smiod   while (ISSPACE (* str))
622*3d8817e4Smiod     ++ str;
623*3d8817e4Smiod 
624*3d8817e4Smiod   /* The instructions are stored in hashed lists.
625*3d8817e4Smiod      Get the first in the list.  */
626*3d8817e4Smiod   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
627*3d8817e4Smiod 
628*3d8817e4Smiod   /* Keep looking until we find a match.  */
629*3d8817e4Smiod   start = str;
630*3d8817e4Smiod   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
631*3d8817e4Smiod     {
632*3d8817e4Smiod       const CGEN_INSN *insn = ilist->insn;
633*3d8817e4Smiod       recognized_mnemonic = 1;
634*3d8817e4Smiod 
635*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
636*3d8817e4Smiod       /* Not usually needed as unsupported opcodes
637*3d8817e4Smiod 	 shouldn't be in the hash lists.  */
638*3d8817e4Smiod       /* Is this insn supported by the selected cpu?  */
639*3d8817e4Smiod       if (! fr30_cgen_insn_supported (cd, insn))
640*3d8817e4Smiod 	continue;
641*3d8817e4Smiod #endif
642*3d8817e4Smiod       /* If the RELAXED attribute is set, this is an insn that shouldn't be
643*3d8817e4Smiod 	 chosen immediately.  Instead, it is used during assembler/linker
644*3d8817e4Smiod 	 relaxation if possible.  */
645*3d8817e4Smiod       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
646*3d8817e4Smiod 	continue;
647*3d8817e4Smiod 
648*3d8817e4Smiod       str = start;
649*3d8817e4Smiod 
650*3d8817e4Smiod       /* Skip this insn if str doesn't look right lexically.  */
651*3d8817e4Smiod       if (CGEN_INSN_RX (insn) != NULL &&
652*3d8817e4Smiod 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
653*3d8817e4Smiod 	continue;
654*3d8817e4Smiod 
655*3d8817e4Smiod       /* Allow parse/insert handlers to obtain length of insn.  */
656*3d8817e4Smiod       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
657*3d8817e4Smiod 
658*3d8817e4Smiod       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
659*3d8817e4Smiod       if (parse_errmsg != NULL)
660*3d8817e4Smiod 	continue;
661*3d8817e4Smiod 
662*3d8817e4Smiod       /* ??? 0 is passed for `pc'.  */
663*3d8817e4Smiod       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
664*3d8817e4Smiod 						 (bfd_vma) 0);
665*3d8817e4Smiod       if (insert_errmsg != NULL)
666*3d8817e4Smiod         continue;
667*3d8817e4Smiod 
668*3d8817e4Smiod       /* It is up to the caller to actually output the insn and any
669*3d8817e4Smiod          queued relocs.  */
670*3d8817e4Smiod       return insn;
671*3d8817e4Smiod     }
672*3d8817e4Smiod 
673*3d8817e4Smiod   {
674*3d8817e4Smiod     static char errbuf[150];
675*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
676*3d8817e4Smiod     const char *tmp_errmsg;
677*3d8817e4Smiod 
678*3d8817e4Smiod     /* If requesting verbose error messages, use insert_errmsg.
679*3d8817e4Smiod        Failing that, use parse_errmsg.  */
680*3d8817e4Smiod     tmp_errmsg = (insert_errmsg ? insert_errmsg :
681*3d8817e4Smiod 		  parse_errmsg ? parse_errmsg :
682*3d8817e4Smiod 		  recognized_mnemonic ?
683*3d8817e4Smiod 		  _("unrecognized form of instruction") :
684*3d8817e4Smiod 		  _("unrecognized instruction"));
685*3d8817e4Smiod 
686*3d8817e4Smiod     if (strlen (start) > 50)
687*3d8817e4Smiod       /* xgettext:c-format */
688*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
689*3d8817e4Smiod     else
690*3d8817e4Smiod       /* xgettext:c-format */
691*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
692*3d8817e4Smiod #else
693*3d8817e4Smiod     if (strlen (start) > 50)
694*3d8817e4Smiod       /* xgettext:c-format */
695*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
696*3d8817e4Smiod     else
697*3d8817e4Smiod       /* xgettext:c-format */
698*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s'"), start);
699*3d8817e4Smiod #endif
700*3d8817e4Smiod 
701*3d8817e4Smiod     *errmsg = errbuf;
702*3d8817e4Smiod     return NULL;
703*3d8817e4Smiod   }
704*3d8817e4Smiod }
705