xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/mt-asm.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
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 "mt-desc.h"
35*3d8817e4Smiod #include "mt-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 /* Range checking for signed numbers.  Returns 0 if acceptable
53*3d8817e4Smiod    and 1 if the value is out of bounds for a signed quantity.  */
54*3d8817e4Smiod 
55*3d8817e4Smiod static int
signed_out_of_bounds(long val)56*3d8817e4Smiod signed_out_of_bounds (long val)
57*3d8817e4Smiod {
58*3d8817e4Smiod   if ((val < -32768) || (val > 32767))
59*3d8817e4Smiod     return 1;
60*3d8817e4Smiod   return 0;
61*3d8817e4Smiod }
62*3d8817e4Smiod 
63*3d8817e4Smiod static const char *
parse_loopsize(CGEN_CPU_DESC cd,const char ** strp,int opindex,void * arg)64*3d8817e4Smiod parse_loopsize (CGEN_CPU_DESC cd,
65*3d8817e4Smiod 		const char **strp,
66*3d8817e4Smiod 		int opindex,
67*3d8817e4Smiod 		void *arg)
68*3d8817e4Smiod {
69*3d8817e4Smiod   signed long * valuep = (signed long *) arg;
70*3d8817e4Smiod   const char *errmsg;
71*3d8817e4Smiod   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
72*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
73*3d8817e4Smiod   bfd_vma value;
74*3d8817e4Smiod 
75*3d8817e4Smiod   /* Is it a control transfer instructions?  */
76*3d8817e4Smiod   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_LOOPSIZE)
77*3d8817e4Smiod     {
78*3d8817e4Smiod       code = BFD_RELOC_MT_PCINSN8;
79*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, code,
80*3d8817e4Smiod                                    & result_type, & value);
81*3d8817e4Smiod       *valuep = value;
82*3d8817e4Smiod       return errmsg;
83*3d8817e4Smiod     }
84*3d8817e4Smiod 
85*3d8817e4Smiod   abort ();
86*3d8817e4Smiod }
87*3d8817e4Smiod 
88*3d8817e4Smiod static const char *
parse_imm16(CGEN_CPU_DESC cd,const char ** strp,int opindex,void * arg)89*3d8817e4Smiod parse_imm16 (CGEN_CPU_DESC cd,
90*3d8817e4Smiod 	     const char **strp,
91*3d8817e4Smiod 	     int opindex,
92*3d8817e4Smiod 	     void *arg)
93*3d8817e4Smiod {
94*3d8817e4Smiod   signed long * valuep = (signed long *) arg;
95*3d8817e4Smiod   const char *errmsg;
96*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
97*3d8817e4Smiod   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
98*3d8817e4Smiod   bfd_vma value;
99*3d8817e4Smiod 
100*3d8817e4Smiod   /* Is it a control transfer instructions?  */
101*3d8817e4Smiod   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16O)
102*3d8817e4Smiod     {
103*3d8817e4Smiod       code = BFD_RELOC_16_PCREL;
104*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, code,
105*3d8817e4Smiod                                    & result_type, & value);
106*3d8817e4Smiod       if (errmsg == NULL)
107*3d8817e4Smiod 	{
108*3d8817e4Smiod 	  if (signed_out_of_bounds (value))
109*3d8817e4Smiod 	    errmsg = _("Operand out of range. Must be between -32768 and 32767.");
110*3d8817e4Smiod 	}
111*3d8817e4Smiod       *valuep = value;
112*3d8817e4Smiod       return errmsg;
113*3d8817e4Smiod     }
114*3d8817e4Smiod 
115*3d8817e4Smiod   /* If it's not a control transfer instruction, then
116*3d8817e4Smiod      we have to check for %OP relocating operators.  */
117*3d8817e4Smiod   if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16L)
118*3d8817e4Smiod     ;
119*3d8817e4Smiod   else if (strncmp (*strp, "%hi16", 5) == 0)
120*3d8817e4Smiod     {
121*3d8817e4Smiod       *strp += 5;
122*3d8817e4Smiod       code = BFD_RELOC_HI16;
123*3d8817e4Smiod     }
124*3d8817e4Smiod   else if (strncmp (*strp, "%lo16", 5) == 0)
125*3d8817e4Smiod     {
126*3d8817e4Smiod       *strp += 5;
127*3d8817e4Smiod       code = BFD_RELOC_LO16;
128*3d8817e4Smiod     }
129*3d8817e4Smiod 
130*3d8817e4Smiod   /* If we found a %OP relocating operator, then parse it as an address.
131*3d8817e4Smiod      If not, we need to parse it as an integer, either signed or unsigned
132*3d8817e4Smiod      depending on which operand type we have.  */
133*3d8817e4Smiod   if (code != BFD_RELOC_NONE)
134*3d8817e4Smiod     {
135*3d8817e4Smiod        /* %OP relocating operator found.  */
136*3d8817e4Smiod        errmsg = cgen_parse_address (cd, strp, opindex, code,
137*3d8817e4Smiod                                    & result_type, & value);
138*3d8817e4Smiod        if (errmsg == NULL)
139*3d8817e4Smiod 	 {
140*3d8817e4Smiod            switch (result_type)
141*3d8817e4Smiod 	     {
142*3d8817e4Smiod 	     case (CGEN_PARSE_OPERAND_RESULT_NUMBER):
143*3d8817e4Smiod 	       if (code == BFD_RELOC_HI16)
144*3d8817e4Smiod 		 value = (value >> 16) & 0xFFFF;
145*3d8817e4Smiod 	       else if (code == BFD_RELOC_LO16)
146*3d8817e4Smiod 		 value = value  & 0xFFFF;
147*3d8817e4Smiod 	       else
148*3d8817e4Smiod 		 errmsg = _("Biiiig Trouble in parse_imm16!");
149*3d8817e4Smiod 	       break;
150*3d8817e4Smiod 
151*3d8817e4Smiod 	     case (CGEN_PARSE_OPERAND_RESULT_QUEUED):
152*3d8817e4Smiod 	       /* No special processing for this case.  */
153*3d8817e4Smiod 	       break;
154*3d8817e4Smiod 
155*3d8817e4Smiod 	     default:
156*3d8817e4Smiod 	       errmsg = _("%operator operand is not a symbol");
157*3d8817e4Smiod 	       break;
158*3d8817e4Smiod              }
159*3d8817e4Smiod 	 }
160*3d8817e4Smiod        *valuep = value;
161*3d8817e4Smiod     }
162*3d8817e4Smiod   else
163*3d8817e4Smiod     {
164*3d8817e4Smiod       /* Parse hex values like 0xffff as unsigned, and sign extend
165*3d8817e4Smiod 	 them manually.  */
166*3d8817e4Smiod       int parse_signed = (opindex == (CGEN_OPERAND_TYPE)MT_OPERAND_IMM16);
167*3d8817e4Smiod 
168*3d8817e4Smiod       if ((*strp)[0] == '0'
169*3d8817e4Smiod 	  && ((*strp)[1] == 'x' || (*strp)[1] == 'X'))
170*3d8817e4Smiod 	parse_signed = 0;
171*3d8817e4Smiod 
172*3d8817e4Smiod       /* No relocating operator.  Parse as an number.  */
173*3d8817e4Smiod       if (parse_signed)
174*3d8817e4Smiod 	{
175*3d8817e4Smiod           /* Parse as as signed integer.  */
176*3d8817e4Smiod 
177*3d8817e4Smiod           errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
178*3d8817e4Smiod 
179*3d8817e4Smiod           if (errmsg == NULL)
180*3d8817e4Smiod 	    {
181*3d8817e4Smiod #if 0
182*3d8817e4Smiod 	      /* Manual range checking is needed for the signed case.  */
183*3d8817e4Smiod 	      if (*valuep & 0x8000)
184*3d8817e4Smiod                 value = 0xffff0000 | *valuep;
185*3d8817e4Smiod 	      else
186*3d8817e4Smiod                 value = *valuep;
187*3d8817e4Smiod 
188*3d8817e4Smiod 	      if (signed_out_of_bounds (value))
189*3d8817e4Smiod 	        errmsg = _("Operand out of range. Must be between -32768 and 32767.");
190*3d8817e4Smiod 	      /* Truncate to 16 bits. This is necessary
191*3d8817e4Smiod 		 because cgen will have sign extended *valuep.  */
192*3d8817e4Smiod 	      *valuep &= 0xFFFF;
193*3d8817e4Smiod #endif
194*3d8817e4Smiod 	    }
195*3d8817e4Smiod 	}
196*3d8817e4Smiod       else
197*3d8817e4Smiod 	{
198*3d8817e4Smiod           /* MT_OPERAND_IMM16Z.  Parse as an unsigned integer.  */
199*3d8817e4Smiod           errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) valuep);
200*3d8817e4Smiod 
201*3d8817e4Smiod 	  if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16
202*3d8817e4Smiod 	      && *valuep >= 0x8000
203*3d8817e4Smiod 	      && *valuep <= 0xffff)
204*3d8817e4Smiod 	    *valuep -= 0x10000;
205*3d8817e4Smiod 	}
206*3d8817e4Smiod     }
207*3d8817e4Smiod 
208*3d8817e4Smiod   return errmsg;
209*3d8817e4Smiod }
210*3d8817e4Smiod 
211*3d8817e4Smiod 
212*3d8817e4Smiod static const char *
parse_dup(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)213*3d8817e4Smiod parse_dup (CGEN_CPU_DESC cd,
214*3d8817e4Smiod 	   const char **strp,
215*3d8817e4Smiod 	   int opindex,
216*3d8817e4Smiod 	   unsigned long *valuep)
217*3d8817e4Smiod {
218*3d8817e4Smiod   const char *errmsg = NULL;
219*3d8817e4Smiod 
220*3d8817e4Smiod   if (strncmp (*strp, "dup", 3) == 0 || strncmp (*strp, "DUP", 3) == 0)
221*3d8817e4Smiod     {
222*3d8817e4Smiod       *strp += 3;
223*3d8817e4Smiod       *valuep = 1;
224*3d8817e4Smiod     }
225*3d8817e4Smiod   else if (strncmp (*strp, "xx", 2) == 0 || strncmp (*strp, "XX", 2) == 0)
226*3d8817e4Smiod     {
227*3d8817e4Smiod       *strp += 2;
228*3d8817e4Smiod       *valuep = 0;
229*3d8817e4Smiod     }
230*3d8817e4Smiod   else
231*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
232*3d8817e4Smiod 
233*3d8817e4Smiod   return errmsg;
234*3d8817e4Smiod }
235*3d8817e4Smiod 
236*3d8817e4Smiod 
237*3d8817e4Smiod static const char *
parse_ball(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)238*3d8817e4Smiod parse_ball (CGEN_CPU_DESC cd,
239*3d8817e4Smiod 	    const char **strp,
240*3d8817e4Smiod 	    int opindex,
241*3d8817e4Smiod 	    unsigned long *valuep)
242*3d8817e4Smiod {
243*3d8817e4Smiod   const char *errmsg = NULL;
244*3d8817e4Smiod 
245*3d8817e4Smiod   if (strncmp (*strp, "all", 3) == 0 || strncmp (*strp, "ALL", 3) == 0)
246*3d8817e4Smiod     {
247*3d8817e4Smiod       *strp += 3;
248*3d8817e4Smiod       *valuep = 1;
249*3d8817e4Smiod     }
250*3d8817e4Smiod   else if (strncmp (*strp, "one", 3) == 0 || strncmp (*strp, "ONE", 3) == 0)
251*3d8817e4Smiod     {
252*3d8817e4Smiod       *strp += 3;
253*3d8817e4Smiod       *valuep = 0;
254*3d8817e4Smiod     }
255*3d8817e4Smiod   else
256*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
257*3d8817e4Smiod 
258*3d8817e4Smiod   return errmsg;
259*3d8817e4Smiod }
260*3d8817e4Smiod 
261*3d8817e4Smiod static const char *
parse_xmode(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)262*3d8817e4Smiod parse_xmode (CGEN_CPU_DESC cd,
263*3d8817e4Smiod 	     const char **strp,
264*3d8817e4Smiod 	     int opindex,
265*3d8817e4Smiod 	     unsigned long *valuep)
266*3d8817e4Smiod {
267*3d8817e4Smiod   const char *errmsg = NULL;
268*3d8817e4Smiod 
269*3d8817e4Smiod   if (strncmp (*strp, "pm", 2) == 0 || strncmp (*strp, "PM", 2) == 0)
270*3d8817e4Smiod     {
271*3d8817e4Smiod       *strp += 2;
272*3d8817e4Smiod       *valuep = 1;
273*3d8817e4Smiod     }
274*3d8817e4Smiod   else if (strncmp (*strp, "xm", 2) == 0 || strncmp (*strp, "XM", 2) == 0)
275*3d8817e4Smiod     {
276*3d8817e4Smiod       *strp += 2;
277*3d8817e4Smiod       *valuep = 0;
278*3d8817e4Smiod     }
279*3d8817e4Smiod   else
280*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
281*3d8817e4Smiod 
282*3d8817e4Smiod   return errmsg;
283*3d8817e4Smiod }
284*3d8817e4Smiod 
285*3d8817e4Smiod static const char *
parse_rc(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)286*3d8817e4Smiod parse_rc (CGEN_CPU_DESC cd,
287*3d8817e4Smiod 	  const char **strp,
288*3d8817e4Smiod 	  int opindex,
289*3d8817e4Smiod 	  unsigned long *valuep)
290*3d8817e4Smiod {
291*3d8817e4Smiod   const char *errmsg = NULL;
292*3d8817e4Smiod 
293*3d8817e4Smiod   if (strncmp (*strp, "r", 1) == 0 || strncmp (*strp, "R", 1) == 0)
294*3d8817e4Smiod     {
295*3d8817e4Smiod       *strp += 1;
296*3d8817e4Smiod       *valuep = 1;
297*3d8817e4Smiod     }
298*3d8817e4Smiod   else if (strncmp (*strp, "c", 1) == 0 || strncmp (*strp, "C", 1) == 0)
299*3d8817e4Smiod     {
300*3d8817e4Smiod       *strp += 1;
301*3d8817e4Smiod       *valuep = 0;
302*3d8817e4Smiod     }
303*3d8817e4Smiod   else
304*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
305*3d8817e4Smiod 
306*3d8817e4Smiod   return errmsg;
307*3d8817e4Smiod }
308*3d8817e4Smiod 
309*3d8817e4Smiod static const char *
parse_cbrb(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)310*3d8817e4Smiod parse_cbrb (CGEN_CPU_DESC cd,
311*3d8817e4Smiod 	    const char **strp,
312*3d8817e4Smiod 	    int opindex,
313*3d8817e4Smiod 	    unsigned long *valuep)
314*3d8817e4Smiod {
315*3d8817e4Smiod   const char *errmsg = NULL;
316*3d8817e4Smiod 
317*3d8817e4Smiod   if (strncmp (*strp, "rb", 2) == 0 || strncmp (*strp, "RB", 2) == 0)
318*3d8817e4Smiod     {
319*3d8817e4Smiod       *strp += 2;
320*3d8817e4Smiod       *valuep = 1;
321*3d8817e4Smiod     }
322*3d8817e4Smiod   else if (strncmp (*strp, "cb", 2) == 0 || strncmp (*strp, "CB", 2) == 0)
323*3d8817e4Smiod     {
324*3d8817e4Smiod       *strp += 2;
325*3d8817e4Smiod       *valuep = 0;
326*3d8817e4Smiod     }
327*3d8817e4Smiod   else
328*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
329*3d8817e4Smiod 
330*3d8817e4Smiod   return errmsg;
331*3d8817e4Smiod }
332*3d8817e4Smiod 
333*3d8817e4Smiod static const char *
parse_rbbc(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)334*3d8817e4Smiod parse_rbbc (CGEN_CPU_DESC cd,
335*3d8817e4Smiod 	    const char **strp,
336*3d8817e4Smiod 	    int opindex,
337*3d8817e4Smiod 	    unsigned long *valuep)
338*3d8817e4Smiod {
339*3d8817e4Smiod   const char *errmsg = NULL;
340*3d8817e4Smiod 
341*3d8817e4Smiod   if (strncmp (*strp, "rt", 2) == 0 || strncmp (*strp, "RT", 2) == 0)
342*3d8817e4Smiod     {
343*3d8817e4Smiod       *strp += 2;
344*3d8817e4Smiod       *valuep = 0;
345*3d8817e4Smiod     }
346*3d8817e4Smiod   else if (strncmp (*strp, "br1", 3) == 0 || strncmp (*strp, "BR1", 3) == 0)
347*3d8817e4Smiod     {
348*3d8817e4Smiod       *strp += 3;
349*3d8817e4Smiod       *valuep = 1;
350*3d8817e4Smiod     }
351*3d8817e4Smiod   else if (strncmp (*strp, "br2", 3) == 0 || strncmp (*strp, "BR2", 3) == 0)
352*3d8817e4Smiod     {
353*3d8817e4Smiod       *strp += 3;
354*3d8817e4Smiod       *valuep = 2;
355*3d8817e4Smiod     }
356*3d8817e4Smiod   else if (strncmp (*strp, "cs", 2) == 0 || strncmp (*strp, "CS", 2) == 0)
357*3d8817e4Smiod     {
358*3d8817e4Smiod       *strp += 2;
359*3d8817e4Smiod       *valuep = 3;
360*3d8817e4Smiod     }
361*3d8817e4Smiod   else
362*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
363*3d8817e4Smiod 
364*3d8817e4Smiod   return errmsg;
365*3d8817e4Smiod }
366*3d8817e4Smiod 
367*3d8817e4Smiod static const char *
parse_type(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)368*3d8817e4Smiod parse_type (CGEN_CPU_DESC cd,
369*3d8817e4Smiod 	    const char **strp,
370*3d8817e4Smiod 	    int opindex,
371*3d8817e4Smiod 	    unsigned long *valuep)
372*3d8817e4Smiod {
373*3d8817e4Smiod   const char *errmsg = NULL;
374*3d8817e4Smiod 
375*3d8817e4Smiod   if (strncmp (*strp, "odd", 3) == 0 || strncmp (*strp, "ODD", 3) == 0)
376*3d8817e4Smiod     {
377*3d8817e4Smiod       *strp += 3;
378*3d8817e4Smiod       *valuep = 0;
379*3d8817e4Smiod     }
380*3d8817e4Smiod   else if (strncmp (*strp, "even", 4) == 0 || strncmp (*strp, "EVEN", 4) == 0)
381*3d8817e4Smiod     {
382*3d8817e4Smiod       *strp += 4;
383*3d8817e4Smiod       *valuep = 1;
384*3d8817e4Smiod     }
385*3d8817e4Smiod   else if (strncmp (*strp, "oe", 2) == 0 || strncmp (*strp, "OE", 2) == 0)
386*3d8817e4Smiod     {
387*3d8817e4Smiod       *strp += 2;
388*3d8817e4Smiod       *valuep = 2;
389*3d8817e4Smiod     }
390*3d8817e4Smiod   else
391*3d8817e4Smiod     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
392*3d8817e4Smiod 
393*3d8817e4Smiod  if ((errmsg == NULL) && (*valuep == 3))
394*3d8817e4Smiod     errmsg = _("invalid operand.  type may have values 0,1,2 only.");
395*3d8817e4Smiod 
396*3d8817e4Smiod   return errmsg;
397*3d8817e4Smiod }
398*3d8817e4Smiod 
399*3d8817e4Smiod /* -- dis.c */
400*3d8817e4Smiod 
401*3d8817e4Smiod const char * mt_cgen_parse_operand
402*3d8817e4Smiod   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
403*3d8817e4Smiod 
404*3d8817e4Smiod /* Main entry point for operand parsing.
405*3d8817e4Smiod 
406*3d8817e4Smiod    This function is basically just a big switch statement.  Earlier versions
407*3d8817e4Smiod    used tables to look up the function to use, but
408*3d8817e4Smiod    - if the table contains both assembler and disassembler functions then
409*3d8817e4Smiod      the disassembler contains much of the assembler and vice-versa,
410*3d8817e4Smiod    - there's a lot of inlining possibilities as things grow,
411*3d8817e4Smiod    - using a switch statement avoids the function call overhead.
412*3d8817e4Smiod 
413*3d8817e4Smiod    This function could be moved into `parse_insn_normal', but keeping it
414*3d8817e4Smiod    separate makes clear the interface between `parse_insn_normal' and each of
415*3d8817e4Smiod    the handlers.  */
416*3d8817e4Smiod 
417*3d8817e4Smiod const char *
mt_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)418*3d8817e4Smiod mt_cgen_parse_operand (CGEN_CPU_DESC cd,
419*3d8817e4Smiod 			   int opindex,
420*3d8817e4Smiod 			   const char ** strp,
421*3d8817e4Smiod 			   CGEN_FIELDS * fields)
422*3d8817e4Smiod {
423*3d8817e4Smiod   const char * errmsg = NULL;
424*3d8817e4Smiod   /* Used by scalar operands that still need to be parsed.  */
425*3d8817e4Smiod   long junk ATTRIBUTE_UNUSED;
426*3d8817e4Smiod 
427*3d8817e4Smiod   switch (opindex)
428*3d8817e4Smiod     {
429*3d8817e4Smiod     case MT_OPERAND_A23 :
430*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_A23, (unsigned long *) (& fields->f_a23));
431*3d8817e4Smiod       break;
432*3d8817e4Smiod     case MT_OPERAND_BALL :
433*3d8817e4Smiod       errmsg = parse_ball (cd, strp, MT_OPERAND_BALL, (unsigned long *) (& fields->f_ball));
434*3d8817e4Smiod       break;
435*3d8817e4Smiod     case MT_OPERAND_BALL2 :
436*3d8817e4Smiod       errmsg = parse_ball (cd, strp, MT_OPERAND_BALL2, (unsigned long *) (& fields->f_ball2));
437*3d8817e4Smiod       break;
438*3d8817e4Smiod     case MT_OPERAND_BANKADDR :
439*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BANKADDR, (unsigned long *) (& fields->f_bankaddr));
440*3d8817e4Smiod       break;
441*3d8817e4Smiod     case MT_OPERAND_BRC :
442*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC, (unsigned long *) (& fields->f_brc));
443*3d8817e4Smiod       break;
444*3d8817e4Smiod     case MT_OPERAND_BRC2 :
445*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC2, (unsigned long *) (& fields->f_brc2));
446*3d8817e4Smiod       break;
447*3d8817e4Smiod     case MT_OPERAND_CB1INCR :
448*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB1INCR, (long *) (& fields->f_cb1incr));
449*3d8817e4Smiod       break;
450*3d8817e4Smiod     case MT_OPERAND_CB1SEL :
451*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB1SEL, (unsigned long *) (& fields->f_cb1sel));
452*3d8817e4Smiod       break;
453*3d8817e4Smiod     case MT_OPERAND_CB2INCR :
454*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB2INCR, (long *) (& fields->f_cb2incr));
455*3d8817e4Smiod       break;
456*3d8817e4Smiod     case MT_OPERAND_CB2SEL :
457*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB2SEL, (unsigned long *) (& fields->f_cb2sel));
458*3d8817e4Smiod       break;
459*3d8817e4Smiod     case MT_OPERAND_CBRB :
460*3d8817e4Smiod       errmsg = parse_cbrb (cd, strp, MT_OPERAND_CBRB, (unsigned long *) (& fields->f_cbrb));
461*3d8817e4Smiod       break;
462*3d8817e4Smiod     case MT_OPERAND_CBS :
463*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBS, (unsigned long *) (& fields->f_cbs));
464*3d8817e4Smiod       break;
465*3d8817e4Smiod     case MT_OPERAND_CBX :
466*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBX, (unsigned long *) (& fields->f_cbx));
467*3d8817e4Smiod       break;
468*3d8817e4Smiod     case MT_OPERAND_CCB :
469*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CCB, (unsigned long *) (& fields->f_ccb));
470*3d8817e4Smiod       break;
471*3d8817e4Smiod     case MT_OPERAND_CDB :
472*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CDB, (unsigned long *) (& fields->f_cdb));
473*3d8817e4Smiod       break;
474*3d8817e4Smiod     case MT_OPERAND_CELL :
475*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CELL, (unsigned long *) (& fields->f_cell));
476*3d8817e4Smiod       break;
477*3d8817e4Smiod     case MT_OPERAND_COLNUM :
478*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_COLNUM, (unsigned long *) (& fields->f_colnum));
479*3d8817e4Smiod       break;
480*3d8817e4Smiod     case MT_OPERAND_CONTNUM :
481*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CONTNUM, (unsigned long *) (& fields->f_contnum));
482*3d8817e4Smiod       break;
483*3d8817e4Smiod     case MT_OPERAND_CR :
484*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CR, (unsigned long *) (& fields->f_cr));
485*3d8817e4Smiod       break;
486*3d8817e4Smiod     case MT_OPERAND_CTXDISP :
487*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CTXDISP, (unsigned long *) (& fields->f_ctxdisp));
488*3d8817e4Smiod       break;
489*3d8817e4Smiod     case MT_OPERAND_DUP :
490*3d8817e4Smiod       errmsg = parse_dup (cd, strp, MT_OPERAND_DUP, (unsigned long *) (& fields->f_dup));
491*3d8817e4Smiod       break;
492*3d8817e4Smiod     case MT_OPERAND_FBDISP :
493*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBDISP, (unsigned long *) (& fields->f_fbdisp));
494*3d8817e4Smiod       break;
495*3d8817e4Smiod     case MT_OPERAND_FBINCR :
496*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBINCR, (unsigned long *) (& fields->f_fbincr));
497*3d8817e4Smiod       break;
498*3d8817e4Smiod     case MT_OPERAND_FRDR :
499*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_dr);
500*3d8817e4Smiod       break;
501*3d8817e4Smiod     case MT_OPERAND_FRDRRR :
502*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_drrr);
503*3d8817e4Smiod       break;
504*3d8817e4Smiod     case MT_OPERAND_FRSR1 :
505*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr1);
506*3d8817e4Smiod       break;
507*3d8817e4Smiod     case MT_OPERAND_FRSR2 :
508*3d8817e4Smiod       errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr2);
509*3d8817e4Smiod       break;
510*3d8817e4Smiod     case MT_OPERAND_ID :
511*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ID, (unsigned long *) (& fields->f_id));
512*3d8817e4Smiod       break;
513*3d8817e4Smiod     case MT_OPERAND_IMM16 :
514*3d8817e4Smiod       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16, (long *) (& fields->f_imm16s));
515*3d8817e4Smiod       break;
516*3d8817e4Smiod     case MT_OPERAND_IMM16L :
517*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_IMM16L, (unsigned long *) (& fields->f_imm16l));
518*3d8817e4Smiod       break;
519*3d8817e4Smiod     case MT_OPERAND_IMM16O :
520*3d8817e4Smiod       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16O, (unsigned long *) (& fields->f_imm16s));
521*3d8817e4Smiod       break;
522*3d8817e4Smiod     case MT_OPERAND_IMM16Z :
523*3d8817e4Smiod       errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16Z, (unsigned long *) (& fields->f_imm16u));
524*3d8817e4Smiod       break;
525*3d8817e4Smiod     case MT_OPERAND_INCAMT :
526*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCAMT, (unsigned long *) (& fields->f_incamt));
527*3d8817e4Smiod       break;
528*3d8817e4Smiod     case MT_OPERAND_INCR :
529*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCR, (unsigned long *) (& fields->f_incr));
530*3d8817e4Smiod       break;
531*3d8817e4Smiod     case MT_OPERAND_LENGTH :
532*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_LENGTH, (unsigned long *) (& fields->f_length));
533*3d8817e4Smiod       break;
534*3d8817e4Smiod     case MT_OPERAND_LOOPSIZE :
535*3d8817e4Smiod       errmsg = parse_loopsize (cd, strp, MT_OPERAND_LOOPSIZE, (unsigned long *) (& fields->f_loopo));
536*3d8817e4Smiod       break;
537*3d8817e4Smiod     case MT_OPERAND_MASK :
538*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK, (unsigned long *) (& fields->f_mask));
539*3d8817e4Smiod       break;
540*3d8817e4Smiod     case MT_OPERAND_MASK1 :
541*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK1, (unsigned long *) (& fields->f_mask1));
542*3d8817e4Smiod       break;
543*3d8817e4Smiod     case MT_OPERAND_MODE :
544*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MODE, (unsigned long *) (& fields->f_mode));
545*3d8817e4Smiod       break;
546*3d8817e4Smiod     case MT_OPERAND_PERM :
547*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_PERM, (unsigned long *) (& fields->f_perm));
548*3d8817e4Smiod       break;
549*3d8817e4Smiod     case MT_OPERAND_RBBC :
550*3d8817e4Smiod       errmsg = parse_rbbc (cd, strp, MT_OPERAND_RBBC, (unsigned long *) (& fields->f_rbbc));
551*3d8817e4Smiod       break;
552*3d8817e4Smiod     case MT_OPERAND_RC :
553*3d8817e4Smiod       errmsg = parse_rc (cd, strp, MT_OPERAND_RC, (unsigned long *) (& fields->f_rc));
554*3d8817e4Smiod       break;
555*3d8817e4Smiod     case MT_OPERAND_RC1 :
556*3d8817e4Smiod       errmsg = parse_rc (cd, strp, MT_OPERAND_RC1, (unsigned long *) (& fields->f_rc1));
557*3d8817e4Smiod       break;
558*3d8817e4Smiod     case MT_OPERAND_RC2 :
559*3d8817e4Smiod       errmsg = parse_rc (cd, strp, MT_OPERAND_RC2, (unsigned long *) (& fields->f_rc2));
560*3d8817e4Smiod       break;
561*3d8817e4Smiod     case MT_OPERAND_RC3 :
562*3d8817e4Smiod       errmsg = parse_rc (cd, strp, MT_OPERAND_RC3, (unsigned long *) (& fields->f_rc3));
563*3d8817e4Smiod       break;
564*3d8817e4Smiod     case MT_OPERAND_RCNUM :
565*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RCNUM, (unsigned long *) (& fields->f_rcnum));
566*3d8817e4Smiod       break;
567*3d8817e4Smiod     case MT_OPERAND_RDA :
568*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RDA, (unsigned long *) (& fields->f_rda));
569*3d8817e4Smiod       break;
570*3d8817e4Smiod     case MT_OPERAND_ROWNUM :
571*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM, (unsigned long *) (& fields->f_rownum));
572*3d8817e4Smiod       break;
573*3d8817e4Smiod     case MT_OPERAND_ROWNUM1 :
574*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM1, (unsigned long *) (& fields->f_rownum1));
575*3d8817e4Smiod       break;
576*3d8817e4Smiod     case MT_OPERAND_ROWNUM2 :
577*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM2, (unsigned long *) (& fields->f_rownum2));
578*3d8817e4Smiod       break;
579*3d8817e4Smiod     case MT_OPERAND_SIZE :
580*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_SIZE, (unsigned long *) (& fields->f_size));
581*3d8817e4Smiod       break;
582*3d8817e4Smiod     case MT_OPERAND_TYPE :
583*3d8817e4Smiod       errmsg = parse_type (cd, strp, MT_OPERAND_TYPE, (unsigned long *) (& fields->f_type));
584*3d8817e4Smiod       break;
585*3d8817e4Smiod     case MT_OPERAND_WR :
586*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_WR, (unsigned long *) (& fields->f_wr));
587*3d8817e4Smiod       break;
588*3d8817e4Smiod     case MT_OPERAND_XMODE :
589*3d8817e4Smiod       errmsg = parse_xmode (cd, strp, MT_OPERAND_XMODE, (unsigned long *) (& fields->f_xmode));
590*3d8817e4Smiod       break;
591*3d8817e4Smiod 
592*3d8817e4Smiod     default :
593*3d8817e4Smiod       /* xgettext:c-format */
594*3d8817e4Smiod       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
595*3d8817e4Smiod       abort ();
596*3d8817e4Smiod   }
597*3d8817e4Smiod 
598*3d8817e4Smiod   return errmsg;
599*3d8817e4Smiod }
600*3d8817e4Smiod 
601*3d8817e4Smiod cgen_parse_fn * const mt_cgen_parse_handlers[] =
602*3d8817e4Smiod {
603*3d8817e4Smiod   parse_insn_normal,
604*3d8817e4Smiod };
605*3d8817e4Smiod 
606*3d8817e4Smiod void
mt_cgen_init_asm(CGEN_CPU_DESC cd)607*3d8817e4Smiod mt_cgen_init_asm (CGEN_CPU_DESC cd)
608*3d8817e4Smiod {
609*3d8817e4Smiod   mt_cgen_init_opcode_table (cd);
610*3d8817e4Smiod   mt_cgen_init_ibld_table (cd);
611*3d8817e4Smiod   cd->parse_handlers = & mt_cgen_parse_handlers[0];
612*3d8817e4Smiod   cd->parse_operand = mt_cgen_parse_operand;
613*3d8817e4Smiod }
614*3d8817e4Smiod 
615*3d8817e4Smiod 
616*3d8817e4Smiod 
617*3d8817e4Smiod /* Regex construction routine.
618*3d8817e4Smiod 
619*3d8817e4Smiod    This translates an opcode syntax string into a regex string,
620*3d8817e4Smiod    by replacing any non-character syntax element (such as an
621*3d8817e4Smiod    opcode) with the pattern '.*'
622*3d8817e4Smiod 
623*3d8817e4Smiod    It then compiles the regex and stores it in the opcode, for
624*3d8817e4Smiod    later use by mt_cgen_assemble_insn
625*3d8817e4Smiod 
626*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
627*3d8817e4Smiod 
628*3d8817e4Smiod char *
mt_cgen_build_insn_regex(CGEN_INSN * insn)629*3d8817e4Smiod mt_cgen_build_insn_regex (CGEN_INSN *insn)
630*3d8817e4Smiod {
631*3d8817e4Smiod   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
632*3d8817e4Smiod   const char *mnem = CGEN_INSN_MNEMONIC (insn);
633*3d8817e4Smiod   char rxbuf[CGEN_MAX_RX_ELEMENTS];
634*3d8817e4Smiod   char *rx = rxbuf;
635*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE *syn;
636*3d8817e4Smiod   int reg_err;
637*3d8817e4Smiod 
638*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
639*3d8817e4Smiod 
640*3d8817e4Smiod   /* Mnemonics come first in the syntax string.  */
641*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
642*3d8817e4Smiod     return _("missing mnemonic in syntax string");
643*3d8817e4Smiod   ++syn;
644*3d8817e4Smiod 
645*3d8817e4Smiod   /* Generate a case sensitive regular expression that emulates case
646*3d8817e4Smiod      insensitive matching in the "C" locale.  We cannot generate a case
647*3d8817e4Smiod      insensitive regular expression because in Turkish locales, 'i' and 'I'
648*3d8817e4Smiod      are not equal modulo case conversion.  */
649*3d8817e4Smiod 
650*3d8817e4Smiod   /* Copy the literal mnemonic out of the insn.  */
651*3d8817e4Smiod   for (; *mnem; mnem++)
652*3d8817e4Smiod     {
653*3d8817e4Smiod       char c = *mnem;
654*3d8817e4Smiod 
655*3d8817e4Smiod       if (ISALPHA (c))
656*3d8817e4Smiod 	{
657*3d8817e4Smiod 	  *rx++ = '[';
658*3d8817e4Smiod 	  *rx++ = TOLOWER (c);
659*3d8817e4Smiod 	  *rx++ = TOUPPER (c);
660*3d8817e4Smiod 	  *rx++ = ']';
661*3d8817e4Smiod 	}
662*3d8817e4Smiod       else
663*3d8817e4Smiod 	*rx++ = c;
664*3d8817e4Smiod     }
665*3d8817e4Smiod 
666*3d8817e4Smiod   /* Copy any remaining literals from the syntax string into the rx.  */
667*3d8817e4Smiod   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
668*3d8817e4Smiod     {
669*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
670*3d8817e4Smiod 	{
671*3d8817e4Smiod 	  char c = CGEN_SYNTAX_CHAR (* syn);
672*3d8817e4Smiod 
673*3d8817e4Smiod 	  switch (c)
674*3d8817e4Smiod 	    {
675*3d8817e4Smiod 	      /* Escape any regex metacharacters in the syntax.  */
676*3d8817e4Smiod 	    case '.': case '[': case '\\':
677*3d8817e4Smiod 	    case '*': case '^': case '$':
678*3d8817e4Smiod 
679*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
680*3d8817e4Smiod 	    case '?': case '{': case '}':
681*3d8817e4Smiod 	    case '(': case ')': case '*':
682*3d8817e4Smiod 	    case '|': case '+': case ']':
683*3d8817e4Smiod #endif
684*3d8817e4Smiod 	      *rx++ = '\\';
685*3d8817e4Smiod 	      *rx++ = c;
686*3d8817e4Smiod 	      break;
687*3d8817e4Smiod 
688*3d8817e4Smiod 	    default:
689*3d8817e4Smiod 	      if (ISALPHA (c))
690*3d8817e4Smiod 		{
691*3d8817e4Smiod 		  *rx++ = '[';
692*3d8817e4Smiod 		  *rx++ = TOLOWER (c);
693*3d8817e4Smiod 		  *rx++ = TOUPPER (c);
694*3d8817e4Smiod 		  *rx++ = ']';
695*3d8817e4Smiod 		}
696*3d8817e4Smiod 	      else
697*3d8817e4Smiod 		*rx++ = c;
698*3d8817e4Smiod 	      break;
699*3d8817e4Smiod 	    }
700*3d8817e4Smiod 	}
701*3d8817e4Smiod       else
702*3d8817e4Smiod 	{
703*3d8817e4Smiod 	  /* Replace non-syntax fields with globs.  */
704*3d8817e4Smiod 	  *rx++ = '.';
705*3d8817e4Smiod 	  *rx++ = '*';
706*3d8817e4Smiod 	}
707*3d8817e4Smiod     }
708*3d8817e4Smiod 
709*3d8817e4Smiod   /* Trailing whitespace ok.  */
710*3d8817e4Smiod   * rx++ = '[';
711*3d8817e4Smiod   * rx++ = ' ';
712*3d8817e4Smiod   * rx++ = '\t';
713*3d8817e4Smiod   * rx++ = ']';
714*3d8817e4Smiod   * rx++ = '*';
715*3d8817e4Smiod 
716*3d8817e4Smiod   /* But anchor it after that.  */
717*3d8817e4Smiod   * rx++ = '$';
718*3d8817e4Smiod   * rx = '\0';
719*3d8817e4Smiod 
720*3d8817e4Smiod   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
721*3d8817e4Smiod   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
722*3d8817e4Smiod 
723*3d8817e4Smiod   if (reg_err == 0)
724*3d8817e4Smiod     return NULL;
725*3d8817e4Smiod   else
726*3d8817e4Smiod     {
727*3d8817e4Smiod       static char msg[80];
728*3d8817e4Smiod 
729*3d8817e4Smiod       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
730*3d8817e4Smiod       regfree ((regex_t *) CGEN_INSN_RX (insn));
731*3d8817e4Smiod       free (CGEN_INSN_RX (insn));
732*3d8817e4Smiod       (CGEN_INSN_RX (insn)) = NULL;
733*3d8817e4Smiod       return msg;
734*3d8817e4Smiod     }
735*3d8817e4Smiod }
736*3d8817e4Smiod 
737*3d8817e4Smiod 
738*3d8817e4Smiod /* Default insn parser.
739*3d8817e4Smiod 
740*3d8817e4Smiod    The syntax string is scanned and operands are parsed and stored in FIELDS.
741*3d8817e4Smiod    Relocs are queued as we go via other callbacks.
742*3d8817e4Smiod 
743*3d8817e4Smiod    ??? Note that this is currently an all-or-nothing parser.  If we fail to
744*3d8817e4Smiod    parse the instruction, we return 0 and the caller will start over from
745*3d8817e4Smiod    the beginning.  Backtracking will be necessary in parsing subexpressions,
746*3d8817e4Smiod    but that can be handled there.  Not handling backtracking here may get
747*3d8817e4Smiod    expensive in the case of the m68k.  Deal with later.
748*3d8817e4Smiod 
749*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
750*3d8817e4Smiod 
751*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)752*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
753*3d8817e4Smiod 		   const CGEN_INSN *insn,
754*3d8817e4Smiod 		   const char **strp,
755*3d8817e4Smiod 		   CGEN_FIELDS *fields)
756*3d8817e4Smiod {
757*3d8817e4Smiod   /* ??? Runtime added insns not handled yet.  */
758*3d8817e4Smiod   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
759*3d8817e4Smiod   const char *str = *strp;
760*3d8817e4Smiod   const char *errmsg;
761*3d8817e4Smiod   const char *p;
762*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE * syn;
763*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
764*3d8817e4Smiod   /* FIXME: wip */
765*3d8817e4Smiod   int past_opcode_p;
766*3d8817e4Smiod #endif
767*3d8817e4Smiod 
768*3d8817e4Smiod   /* For now we assume the mnemonic is first (there are no leading operands).
769*3d8817e4Smiod      We can parse it without needing to set up operand parsing.
770*3d8817e4Smiod      GAS's input scrubber will ensure mnemonics are lowercase, but we may
771*3d8817e4Smiod      not be called from GAS.  */
772*3d8817e4Smiod   p = CGEN_INSN_MNEMONIC (insn);
773*3d8817e4Smiod   while (*p && TOLOWER (*p) == TOLOWER (*str))
774*3d8817e4Smiod     ++p, ++str;
775*3d8817e4Smiod 
776*3d8817e4Smiod   if (* p)
777*3d8817e4Smiod     return _("unrecognized instruction");
778*3d8817e4Smiod 
779*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
780*3d8817e4Smiod   if (* str && ! ISSPACE (* str))
781*3d8817e4Smiod     return _("unrecognized instruction");
782*3d8817e4Smiod #endif
783*3d8817e4Smiod 
784*3d8817e4Smiod   CGEN_INIT_PARSE (cd);
785*3d8817e4Smiod   cgen_init_parse_operand (cd);
786*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
787*3d8817e4Smiod   past_opcode_p = 0;
788*3d8817e4Smiod #endif
789*3d8817e4Smiod 
790*3d8817e4Smiod   /* We don't check for (*str != '\0') here because we want to parse
791*3d8817e4Smiod      any trailing fake arguments in the syntax string.  */
792*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (syntax);
793*3d8817e4Smiod 
794*3d8817e4Smiod   /* Mnemonics come first for now, ensure valid string.  */
795*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
796*3d8817e4Smiod     abort ();
797*3d8817e4Smiod 
798*3d8817e4Smiod   ++syn;
799*3d8817e4Smiod 
800*3d8817e4Smiod   while (* syn != 0)
801*3d8817e4Smiod     {
802*3d8817e4Smiod       /* Non operand chars must match exactly.  */
803*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
804*3d8817e4Smiod 	{
805*3d8817e4Smiod 	  /* FIXME: While we allow for non-GAS callers above, we assume the
806*3d8817e4Smiod 	     first char after the mnemonic part is a space.  */
807*3d8817e4Smiod 	  /* FIXME: We also take inappropriate advantage of the fact that
808*3d8817e4Smiod 	     GAS's input scrubber will remove extraneous blanks.  */
809*3d8817e4Smiod 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
810*3d8817e4Smiod 	    {
811*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
812*3d8817e4Smiod 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
813*3d8817e4Smiod 		past_opcode_p = 1;
814*3d8817e4Smiod #endif
815*3d8817e4Smiod 	      ++ syn;
816*3d8817e4Smiod 	      ++ str;
817*3d8817e4Smiod 	    }
818*3d8817e4Smiod 	  else if (*str)
819*3d8817e4Smiod 	    {
820*3d8817e4Smiod 	      /* Syntax char didn't match.  Can't be this insn.  */
821*3d8817e4Smiod 	      static char msg [80];
822*3d8817e4Smiod 
823*3d8817e4Smiod 	      /* xgettext:c-format */
824*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
825*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn), *str);
826*3d8817e4Smiod 	      return msg;
827*3d8817e4Smiod 	    }
828*3d8817e4Smiod 	  else
829*3d8817e4Smiod 	    {
830*3d8817e4Smiod 	      /* Ran out of input.  */
831*3d8817e4Smiod 	      static char msg [80];
832*3d8817e4Smiod 
833*3d8817e4Smiod 	      /* xgettext:c-format */
834*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
835*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn));
836*3d8817e4Smiod 	      return msg;
837*3d8817e4Smiod 	    }
838*3d8817e4Smiod 	  continue;
839*3d8817e4Smiod 	}
840*3d8817e4Smiod 
841*3d8817e4Smiod       /* We have an operand of some sort.  */
842*3d8817e4Smiod       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
843*3d8817e4Smiod 					  &str, fields);
844*3d8817e4Smiod       if (errmsg)
845*3d8817e4Smiod 	return errmsg;
846*3d8817e4Smiod 
847*3d8817e4Smiod       /* Done with this operand, continue with next one.  */
848*3d8817e4Smiod       ++ syn;
849*3d8817e4Smiod     }
850*3d8817e4Smiod 
851*3d8817e4Smiod   /* If we're at the end of the syntax string, we're done.  */
852*3d8817e4Smiod   if (* syn == 0)
853*3d8817e4Smiod     {
854*3d8817e4Smiod       /* FIXME: For the moment we assume a valid `str' can only contain
855*3d8817e4Smiod 	 blanks now.  IE: We needn't try again with a longer version of
856*3d8817e4Smiod 	 the insn and it is assumed that longer versions of insns appear
857*3d8817e4Smiod 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
858*3d8817e4Smiod       while (ISSPACE (* str))
859*3d8817e4Smiod 	++ str;
860*3d8817e4Smiod 
861*3d8817e4Smiod       if (* str != '\0')
862*3d8817e4Smiod 	return _("junk at end of line"); /* FIXME: would like to include `str' */
863*3d8817e4Smiod 
864*3d8817e4Smiod       return NULL;
865*3d8817e4Smiod     }
866*3d8817e4Smiod 
867*3d8817e4Smiod   /* We couldn't parse it.  */
868*3d8817e4Smiod   return _("unrecognized instruction");
869*3d8817e4Smiod }
870*3d8817e4Smiod 
871*3d8817e4Smiod /* Main entry point.
872*3d8817e4Smiod    This routine is called for each instruction to be assembled.
873*3d8817e4Smiod    STR points to the insn to be assembled.
874*3d8817e4Smiod    We assume all necessary tables have been initialized.
875*3d8817e4Smiod    The assembled instruction, less any fixups, is stored in BUF.
876*3d8817e4Smiod    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
877*3d8817e4Smiod    still needs to be converted to target byte order, otherwise BUF is an array
878*3d8817e4Smiod    of bytes in target byte order.
879*3d8817e4Smiod    The result is a pointer to the insn's entry in the opcode table,
880*3d8817e4Smiod    or NULL if an error occured (an error message will have already been
881*3d8817e4Smiod    printed).
882*3d8817e4Smiod 
883*3d8817e4Smiod    Note that when processing (non-alias) macro-insns,
884*3d8817e4Smiod    this function recurses.
885*3d8817e4Smiod 
886*3d8817e4Smiod    ??? It's possible to make this cpu-independent.
887*3d8817e4Smiod    One would have to deal with a few minor things.
888*3d8817e4Smiod    At this point in time doing so would be more of a curiosity than useful
889*3d8817e4Smiod    [for example this file isn't _that_ big], but keeping the possibility in
890*3d8817e4Smiod    mind helps keep the design clean.  */
891*3d8817e4Smiod 
892*3d8817e4Smiod const CGEN_INSN *
mt_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)893*3d8817e4Smiod mt_cgen_assemble_insn (CGEN_CPU_DESC cd,
894*3d8817e4Smiod 			   const char *str,
895*3d8817e4Smiod 			   CGEN_FIELDS *fields,
896*3d8817e4Smiod 			   CGEN_INSN_BYTES_PTR buf,
897*3d8817e4Smiod 			   char **errmsg)
898*3d8817e4Smiod {
899*3d8817e4Smiod   const char *start;
900*3d8817e4Smiod   CGEN_INSN_LIST *ilist;
901*3d8817e4Smiod   const char *parse_errmsg = NULL;
902*3d8817e4Smiod   const char *insert_errmsg = NULL;
903*3d8817e4Smiod   int recognized_mnemonic = 0;
904*3d8817e4Smiod 
905*3d8817e4Smiod   /* Skip leading white space.  */
906*3d8817e4Smiod   while (ISSPACE (* str))
907*3d8817e4Smiod     ++ str;
908*3d8817e4Smiod 
909*3d8817e4Smiod   /* The instructions are stored in hashed lists.
910*3d8817e4Smiod      Get the first in the list.  */
911*3d8817e4Smiod   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
912*3d8817e4Smiod 
913*3d8817e4Smiod   /* Keep looking until we find a match.  */
914*3d8817e4Smiod   start = str;
915*3d8817e4Smiod   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
916*3d8817e4Smiod     {
917*3d8817e4Smiod       const CGEN_INSN *insn = ilist->insn;
918*3d8817e4Smiod       recognized_mnemonic = 1;
919*3d8817e4Smiod 
920*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
921*3d8817e4Smiod       /* Not usually needed as unsupported opcodes
922*3d8817e4Smiod 	 shouldn't be in the hash lists.  */
923*3d8817e4Smiod       /* Is this insn supported by the selected cpu?  */
924*3d8817e4Smiod       if (! mt_cgen_insn_supported (cd, insn))
925*3d8817e4Smiod 	continue;
926*3d8817e4Smiod #endif
927*3d8817e4Smiod       /* If the RELAXED attribute is set, this is an insn that shouldn't be
928*3d8817e4Smiod 	 chosen immediately.  Instead, it is used during assembler/linker
929*3d8817e4Smiod 	 relaxation if possible.  */
930*3d8817e4Smiod       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
931*3d8817e4Smiod 	continue;
932*3d8817e4Smiod 
933*3d8817e4Smiod       str = start;
934*3d8817e4Smiod 
935*3d8817e4Smiod       /* Skip this insn if str doesn't look right lexically.  */
936*3d8817e4Smiod       if (CGEN_INSN_RX (insn) != NULL &&
937*3d8817e4Smiod 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
938*3d8817e4Smiod 	continue;
939*3d8817e4Smiod 
940*3d8817e4Smiod       /* Allow parse/insert handlers to obtain length of insn.  */
941*3d8817e4Smiod       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
942*3d8817e4Smiod 
943*3d8817e4Smiod       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
944*3d8817e4Smiod       if (parse_errmsg != NULL)
945*3d8817e4Smiod 	continue;
946*3d8817e4Smiod 
947*3d8817e4Smiod       /* ??? 0 is passed for `pc'.  */
948*3d8817e4Smiod       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
949*3d8817e4Smiod 						 (bfd_vma) 0);
950*3d8817e4Smiod       if (insert_errmsg != NULL)
951*3d8817e4Smiod         continue;
952*3d8817e4Smiod 
953*3d8817e4Smiod       /* It is up to the caller to actually output the insn and any
954*3d8817e4Smiod          queued relocs.  */
955*3d8817e4Smiod       return insn;
956*3d8817e4Smiod     }
957*3d8817e4Smiod 
958*3d8817e4Smiod   {
959*3d8817e4Smiod     static char errbuf[150];
960*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
961*3d8817e4Smiod     const char *tmp_errmsg;
962*3d8817e4Smiod 
963*3d8817e4Smiod     /* If requesting verbose error messages, use insert_errmsg.
964*3d8817e4Smiod        Failing that, use parse_errmsg.  */
965*3d8817e4Smiod     tmp_errmsg = (insert_errmsg ? insert_errmsg :
966*3d8817e4Smiod 		  parse_errmsg ? parse_errmsg :
967*3d8817e4Smiod 		  recognized_mnemonic ?
968*3d8817e4Smiod 		  _("unrecognized form of instruction") :
969*3d8817e4Smiod 		  _("unrecognized instruction"));
970*3d8817e4Smiod 
971*3d8817e4Smiod     if (strlen (start) > 50)
972*3d8817e4Smiod       /* xgettext:c-format */
973*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
974*3d8817e4Smiod     else
975*3d8817e4Smiod       /* xgettext:c-format */
976*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
977*3d8817e4Smiod #else
978*3d8817e4Smiod     if (strlen (start) > 50)
979*3d8817e4Smiod       /* xgettext:c-format */
980*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
981*3d8817e4Smiod     else
982*3d8817e4Smiod       /* xgettext:c-format */
983*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s'"), start);
984*3d8817e4Smiod #endif
985*3d8817e4Smiod 
986*3d8817e4Smiod     *errmsg = errbuf;
987*3d8817e4Smiod     return NULL;
988*3d8817e4Smiod   }
989*3d8817e4Smiod }
990