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