xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/ip2k-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 "ip2k-desc.h"
35*3d8817e4Smiod #include "ip2k-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 static const char *
parse_fr(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)54*3d8817e4Smiod parse_fr (CGEN_CPU_DESC cd,
55*3d8817e4Smiod 	  const char **strp,
56*3d8817e4Smiod 	  int opindex,
57*3d8817e4Smiod 	  unsigned long *valuep)
58*3d8817e4Smiod {
59*3d8817e4Smiod   const char *errmsg;
60*3d8817e4Smiod   const char *old_strp;
61*3d8817e4Smiod   char *afteroffset;
62*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
63*3d8817e4Smiod   bfd_vma value;
64*3d8817e4Smiod   extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
65*3d8817e4Smiod   bfd_vma tempvalue;
66*3d8817e4Smiod 
67*3d8817e4Smiod   old_strp = *strp;
68*3d8817e4Smiod   afteroffset = NULL;
69*3d8817e4Smiod 
70*3d8817e4Smiod   /* Check here to see if you're about to try parsing a w as the first arg
71*3d8817e4Smiod      and return an error if you are.  */
72*3d8817e4Smiod   if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0))
73*3d8817e4Smiod     {
74*3d8817e4Smiod       (*strp)++;
75*3d8817e4Smiod 
76*3d8817e4Smiod       if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp))
77*3d8817e4Smiod 	{
78*3d8817e4Smiod 	  /* We've been passed a w.  Return with an error message so that
79*3d8817e4Smiod 	     cgen will try the next parsing option.  */
80*3d8817e4Smiod 	  errmsg = _("W keyword invalid in FR operand slot.");
81*3d8817e4Smiod 	  return errmsg;
82*3d8817e4Smiod 	}
83*3d8817e4Smiod       *strp = old_strp;
84*3d8817e4Smiod     }
85*3d8817e4Smiod 
86*3d8817e4Smiod   /* Attempt parse as register keyword. */
87*3d8817e4Smiod   errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
88*3d8817e4Smiod 			       (long *) valuep);
89*3d8817e4Smiod   if (*strp != NULL
90*3d8817e4Smiod       && errmsg == NULL)
91*3d8817e4Smiod     return errmsg;
92*3d8817e4Smiod 
93*3d8817e4Smiod   /* Attempt to parse for "(IP)".  */
94*3d8817e4Smiod   afteroffset = strstr (*strp, "(IP)");
95*3d8817e4Smiod 
96*3d8817e4Smiod   if (afteroffset == NULL)
97*3d8817e4Smiod     /* Make sure it's not in lower case.  */
98*3d8817e4Smiod     afteroffset = strstr (*strp, "(ip)");
99*3d8817e4Smiod 
100*3d8817e4Smiod   if (afteroffset != NULL)
101*3d8817e4Smiod     {
102*3d8817e4Smiod       if (afteroffset != *strp)
103*3d8817e4Smiod 	{
104*3d8817e4Smiod 	  /* Invalid offset present.  */
105*3d8817e4Smiod 	  errmsg = _("offset(IP) is not a valid form");
106*3d8817e4Smiod 	  return errmsg;
107*3d8817e4Smiod 	}
108*3d8817e4Smiod       else
109*3d8817e4Smiod 	{
110*3d8817e4Smiod 	  *strp += 4;
111*3d8817e4Smiod 	  *valuep = 0;
112*3d8817e4Smiod 	  errmsg = NULL;
113*3d8817e4Smiod 	  return errmsg;
114*3d8817e4Smiod 	}
115*3d8817e4Smiod     }
116*3d8817e4Smiod 
117*3d8817e4Smiod   /* Attempt to parse for DP. ex: mov w, offset(DP)
118*3d8817e4Smiod                                   mov offset(DP),w   */
119*3d8817e4Smiod 
120*3d8817e4Smiod   /* Try parsing it as an address and see what comes back.  */
121*3d8817e4Smiod   afteroffset = strstr (*strp, "(DP)");
122*3d8817e4Smiod 
123*3d8817e4Smiod   if (afteroffset == NULL)
124*3d8817e4Smiod     /* Maybe it's in lower case.  */
125*3d8817e4Smiod     afteroffset = strstr (*strp, "(dp)");
126*3d8817e4Smiod 
127*3d8817e4Smiod   if (afteroffset != NULL)
128*3d8817e4Smiod     {
129*3d8817e4Smiod       if (afteroffset == *strp)
130*3d8817e4Smiod 	{
131*3d8817e4Smiod 	  /* No offset present. Use 0 by default.  */
132*3d8817e4Smiod 	  tempvalue = 0;
133*3d8817e4Smiod 	  errmsg = NULL;
134*3d8817e4Smiod 	}
135*3d8817e4Smiod       else
136*3d8817e4Smiod 	errmsg = cgen_parse_address (cd, strp, opindex,
137*3d8817e4Smiod 				     BFD_RELOC_IP2K_FR_OFFSET,
138*3d8817e4Smiod 				     & result_type, & tempvalue);
139*3d8817e4Smiod 
140*3d8817e4Smiod       if (errmsg == NULL)
141*3d8817e4Smiod 	{
142*3d8817e4Smiod 	  if (tempvalue <= 127)
143*3d8817e4Smiod 	    {
144*3d8817e4Smiod 	      /* Value is ok.  Fix up the first 2 bits and return.  */
145*3d8817e4Smiod 	      *valuep = 0x0100 | tempvalue;
146*3d8817e4Smiod 	      *strp += 4; /* Skip over the (DP) in *strp.  */
147*3d8817e4Smiod 	      return errmsg;
148*3d8817e4Smiod 	    }
149*3d8817e4Smiod 	  else
150*3d8817e4Smiod 	    {
151*3d8817e4Smiod 	      /* Found something there in front of (DP) but it's out
152*3d8817e4Smiod 		 of range.  */
153*3d8817e4Smiod 	      errmsg = _("(DP) offset out of range.");
154*3d8817e4Smiod 	      return errmsg;
155*3d8817e4Smiod 	    }
156*3d8817e4Smiod 	}
157*3d8817e4Smiod     }
158*3d8817e4Smiod 
159*3d8817e4Smiod 
160*3d8817e4Smiod   /* Attempt to parse for SP. ex: mov w, offset(SP)
161*3d8817e4Smiod                                   mov offset(SP), w.  */
162*3d8817e4Smiod   afteroffset = strstr (*strp, "(SP)");
163*3d8817e4Smiod 
164*3d8817e4Smiod   if (afteroffset == NULL)
165*3d8817e4Smiod     /* Maybe it's in lower case.  */
166*3d8817e4Smiod     afteroffset = strstr (*strp, "(sp)");
167*3d8817e4Smiod 
168*3d8817e4Smiod   if (afteroffset != NULL)
169*3d8817e4Smiod     {
170*3d8817e4Smiod       if (afteroffset == *strp)
171*3d8817e4Smiod 	{
172*3d8817e4Smiod 	  /* No offset present. Use 0 by default.  */
173*3d8817e4Smiod 	  tempvalue = 0;
174*3d8817e4Smiod 	  errmsg = NULL;
175*3d8817e4Smiod 	}
176*3d8817e4Smiod       else
177*3d8817e4Smiod 	errmsg = cgen_parse_address (cd, strp, opindex,
178*3d8817e4Smiod 				     BFD_RELOC_IP2K_FR_OFFSET,
179*3d8817e4Smiod 				     & result_type, & tempvalue);
180*3d8817e4Smiod 
181*3d8817e4Smiod       if (errmsg == NULL)
182*3d8817e4Smiod 	{
183*3d8817e4Smiod 	  if (tempvalue <= 127)
184*3d8817e4Smiod 	    {
185*3d8817e4Smiod 	      /* Value is ok.  Fix up the first 2 bits and return.  */
186*3d8817e4Smiod 	      *valuep = 0x0180 | tempvalue;
187*3d8817e4Smiod 	      *strp += 4; /* Skip over the (SP) in *strp.  */
188*3d8817e4Smiod 	      return errmsg;
189*3d8817e4Smiod 	    }
190*3d8817e4Smiod 	  else
191*3d8817e4Smiod 	    {
192*3d8817e4Smiod 	      /* Found something there in front of (SP) but it's out
193*3d8817e4Smiod 		 of range.  */
194*3d8817e4Smiod 	      errmsg = _("(SP) offset out of range.");
195*3d8817e4Smiod 	      return errmsg;
196*3d8817e4Smiod 	    }
197*3d8817e4Smiod 	}
198*3d8817e4Smiod     }
199*3d8817e4Smiod 
200*3d8817e4Smiod   /* Attempt to parse as an address.  */
201*3d8817e4Smiod   *strp = old_strp;
202*3d8817e4Smiod   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
203*3d8817e4Smiod 			       & result_type, & value);
204*3d8817e4Smiod   if (errmsg == NULL)
205*3d8817e4Smiod     {
206*3d8817e4Smiod       *valuep = value;
207*3d8817e4Smiod 
208*3d8817e4Smiod       /* If a parenthesis is found, warn about invalid form.  */
209*3d8817e4Smiod       if (**strp == '(')
210*3d8817e4Smiod 	errmsg = _("illegal use of parentheses");
211*3d8817e4Smiod 
212*3d8817e4Smiod       /* If a numeric value is specified, ensure that it is between
213*3d8817e4Smiod 	 1 and 255.  */
214*3d8817e4Smiod       else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
215*3d8817e4Smiod 	{
216*3d8817e4Smiod 	  if (value < 0x1 || value > 0xff)
217*3d8817e4Smiod 	    errmsg = _("operand out of range (not between 1 and 255)");
218*3d8817e4Smiod 	}
219*3d8817e4Smiod     }
220*3d8817e4Smiod   return errmsg;
221*3d8817e4Smiod }
222*3d8817e4Smiod 
223*3d8817e4Smiod static const char *
parse_addr16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)224*3d8817e4Smiod parse_addr16 (CGEN_CPU_DESC cd,
225*3d8817e4Smiod 	      const char **strp,
226*3d8817e4Smiod 	      int opindex,
227*3d8817e4Smiod 	      unsigned long *valuep)
228*3d8817e4Smiod {
229*3d8817e4Smiod   const char *errmsg;
230*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
231*3d8817e4Smiod   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
232*3d8817e4Smiod   bfd_vma value;
233*3d8817e4Smiod 
234*3d8817e4Smiod   if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H)
235*3d8817e4Smiod     code = BFD_RELOC_IP2K_HI8DATA;
236*3d8817e4Smiod   else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L)
237*3d8817e4Smiod     code = BFD_RELOC_IP2K_LO8DATA;
238*3d8817e4Smiod   else
239*3d8817e4Smiod     {
240*3d8817e4Smiod       /* Something is very wrong. opindex has to be one of the above.  */
241*3d8817e4Smiod       errmsg = _("parse_addr16: invalid opindex.");
242*3d8817e4Smiod       return errmsg;
243*3d8817e4Smiod     }
244*3d8817e4Smiod 
245*3d8817e4Smiod   errmsg = cgen_parse_address (cd, strp, opindex, code,
246*3d8817e4Smiod 			       & result_type, & value);
247*3d8817e4Smiod   if (errmsg == NULL)
248*3d8817e4Smiod     {
249*3d8817e4Smiod       /* We either have a relocation or a number now.  */
250*3d8817e4Smiod       if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
251*3d8817e4Smiod 	{
252*3d8817e4Smiod 	  /* We got a number back.  */
253*3d8817e4Smiod 	  if (code == BFD_RELOC_IP2K_HI8DATA)
254*3d8817e4Smiod             value >>= 8;
255*3d8817e4Smiod 	  else
256*3d8817e4Smiod 	    /* code = BFD_RELOC_IP2K_LOW8DATA.  */
257*3d8817e4Smiod 	    value &= 0x00FF;
258*3d8817e4Smiod 	}
259*3d8817e4Smiod       *valuep = value;
260*3d8817e4Smiod     }
261*3d8817e4Smiod 
262*3d8817e4Smiod   return errmsg;
263*3d8817e4Smiod }
264*3d8817e4Smiod 
265*3d8817e4Smiod static const char *
parse_addr16_cjp(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)266*3d8817e4Smiod parse_addr16_cjp (CGEN_CPU_DESC cd,
267*3d8817e4Smiod 		  const char **strp,
268*3d8817e4Smiod 		  int opindex,
269*3d8817e4Smiod 		  unsigned long *valuep)
270*3d8817e4Smiod {
271*3d8817e4Smiod   const char *errmsg;
272*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
273*3d8817e4Smiod   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
274*3d8817e4Smiod   bfd_vma value;
275*3d8817e4Smiod 
276*3d8817e4Smiod   if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
277*3d8817e4Smiod     code = BFD_RELOC_IP2K_ADDR16CJP;
278*3d8817e4Smiod   else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
279*3d8817e4Smiod     code = BFD_RELOC_IP2K_PAGE3;
280*3d8817e4Smiod 
281*3d8817e4Smiod   errmsg = cgen_parse_address (cd, strp, opindex, code,
282*3d8817e4Smiod 			       & result_type, & value);
283*3d8817e4Smiod   if (errmsg == NULL)
284*3d8817e4Smiod     {
285*3d8817e4Smiod       if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
286*3d8817e4Smiod 	{
287*3d8817e4Smiod 	  if ((value & 0x1) == 0)  /* If the address is even .... */
288*3d8817e4Smiod 	    {
289*3d8817e4Smiod 	      if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
290*3d8817e4Smiod                 *valuep = (value >> 1) & 0x1FFF;  /* Should mask be 1FFF?  */
291*3d8817e4Smiod 	      else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
292*3d8817e4Smiod                 *valuep = (value >> 14) & 0x7;
293*3d8817e4Smiod 	    }
294*3d8817e4Smiod           else
295*3d8817e4Smiod  	    errmsg = _("Byte address required. - must be even.");
296*3d8817e4Smiod 	}
297*3d8817e4Smiod       else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
298*3d8817e4Smiod 	{
299*3d8817e4Smiod 	  /* This will happen for things like (s2-s1) where s2 and s1
300*3d8817e4Smiod 	     are labels.  */
301*3d8817e4Smiod 	  *valuep = value;
302*3d8817e4Smiod 	}
303*3d8817e4Smiod       else
304*3d8817e4Smiod         errmsg = _("cgen_parse_address returned a symbol. Literal required.");
305*3d8817e4Smiod     }
306*3d8817e4Smiod   return errmsg;
307*3d8817e4Smiod }
308*3d8817e4Smiod 
309*3d8817e4Smiod static const char *
parse_lit8(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)310*3d8817e4Smiod parse_lit8 (CGEN_CPU_DESC cd,
311*3d8817e4Smiod 	    const char **strp,
312*3d8817e4Smiod 	    int opindex,
313*3d8817e4Smiod 	    long *valuep)
314*3d8817e4Smiod {
315*3d8817e4Smiod   const char *errmsg;
316*3d8817e4Smiod   enum cgen_parse_operand_result result_type;
317*3d8817e4Smiod   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
318*3d8817e4Smiod   bfd_vma value;
319*3d8817e4Smiod 
320*3d8817e4Smiod   /* Parse %OP relocating operators.  */
321*3d8817e4Smiod   if (strncmp (*strp, "%bank", 5) == 0)
322*3d8817e4Smiod     {
323*3d8817e4Smiod       *strp += 5;
324*3d8817e4Smiod       code = BFD_RELOC_IP2K_BANK;
325*3d8817e4Smiod     }
326*3d8817e4Smiod   else if (strncmp (*strp, "%lo8data", 8) == 0)
327*3d8817e4Smiod     {
328*3d8817e4Smiod       *strp += 8;
329*3d8817e4Smiod       code = BFD_RELOC_IP2K_LO8DATA;
330*3d8817e4Smiod     }
331*3d8817e4Smiod   else if (strncmp (*strp, "%hi8data", 8) == 0)
332*3d8817e4Smiod     {
333*3d8817e4Smiod       *strp += 8;
334*3d8817e4Smiod       code = BFD_RELOC_IP2K_HI8DATA;
335*3d8817e4Smiod     }
336*3d8817e4Smiod   else if (strncmp (*strp, "%ex8data", 8) == 0)
337*3d8817e4Smiod     {
338*3d8817e4Smiod       *strp += 8;
339*3d8817e4Smiod       code = BFD_RELOC_IP2K_EX8DATA;
340*3d8817e4Smiod     }
341*3d8817e4Smiod   else if (strncmp (*strp, "%lo8insn", 8) == 0)
342*3d8817e4Smiod     {
343*3d8817e4Smiod       *strp += 8;
344*3d8817e4Smiod       code = BFD_RELOC_IP2K_LO8INSN;
345*3d8817e4Smiod     }
346*3d8817e4Smiod   else if (strncmp (*strp, "%hi8insn", 8) == 0)
347*3d8817e4Smiod     {
348*3d8817e4Smiod       *strp += 8;
349*3d8817e4Smiod       code = BFD_RELOC_IP2K_HI8INSN;
350*3d8817e4Smiod     }
351*3d8817e4Smiod 
352*3d8817e4Smiod   /* Parse %op operand.  */
353*3d8817e4Smiod   if (code != BFD_RELOC_NONE)
354*3d8817e4Smiod     {
355*3d8817e4Smiod       errmsg = cgen_parse_address (cd, strp, opindex, code,
356*3d8817e4Smiod 				   & result_type, & value);
357*3d8817e4Smiod       if ((errmsg == NULL) &&
358*3d8817e4Smiod 	  (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
359*3d8817e4Smiod 	errmsg = _("percent-operator operand is not a symbol");
360*3d8817e4Smiod 
361*3d8817e4Smiod       *valuep = value;
362*3d8817e4Smiod     }
363*3d8817e4Smiod   /* Parse as a number.  */
364*3d8817e4Smiod   else
365*3d8817e4Smiod     {
366*3d8817e4Smiod       errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
367*3d8817e4Smiod 
368*3d8817e4Smiod       /* Truncate to eight bits to accept both signed and unsigned input.  */
369*3d8817e4Smiod       if (errmsg == NULL)
370*3d8817e4Smiod 	*valuep &= 0xFF;
371*3d8817e4Smiod     }
372*3d8817e4Smiod 
373*3d8817e4Smiod   return errmsg;
374*3d8817e4Smiod }
375*3d8817e4Smiod 
376*3d8817e4Smiod static const char *
parse_bit3(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)377*3d8817e4Smiod parse_bit3 (CGEN_CPU_DESC cd,
378*3d8817e4Smiod 	    const char **strp,
379*3d8817e4Smiod 	    int opindex,
380*3d8817e4Smiod 	    unsigned long *valuep)
381*3d8817e4Smiod {
382*3d8817e4Smiod   const char *errmsg;
383*3d8817e4Smiod   char mode = 0;
384*3d8817e4Smiod   long count = 0;
385*3d8817e4Smiod   unsigned long value;
386*3d8817e4Smiod 
387*3d8817e4Smiod   if (strncmp (*strp, "%bit", 4) == 0)
388*3d8817e4Smiod     {
389*3d8817e4Smiod       *strp += 4;
390*3d8817e4Smiod       mode = 1;
391*3d8817e4Smiod     }
392*3d8817e4Smiod   else if (strncmp (*strp, "%msbbit", 7) == 0)
393*3d8817e4Smiod     {
394*3d8817e4Smiod       *strp += 7;
395*3d8817e4Smiod       mode = 1;
396*3d8817e4Smiod     }
397*3d8817e4Smiod   else if (strncmp (*strp, "%lsbbit", 7) == 0)
398*3d8817e4Smiod     {
399*3d8817e4Smiod       *strp += 7;
400*3d8817e4Smiod       mode = 2;
401*3d8817e4Smiod     }
402*3d8817e4Smiod 
403*3d8817e4Smiod   errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
404*3d8817e4Smiod   if (errmsg)
405*3d8817e4Smiod     return errmsg;
406*3d8817e4Smiod 
407*3d8817e4Smiod   if (mode)
408*3d8817e4Smiod     {
409*3d8817e4Smiod       value = * valuep;
410*3d8817e4Smiod       if (value == 0)
411*3d8817e4Smiod 	{
412*3d8817e4Smiod 	  errmsg = _("Attempt to find bit index of 0");
413*3d8817e4Smiod 	  return errmsg;
414*3d8817e4Smiod 	}
415*3d8817e4Smiod 
416*3d8817e4Smiod       if (mode == 1)
417*3d8817e4Smiod 	{
418*3d8817e4Smiod 	  count = 31;
419*3d8817e4Smiod 	  while ((value & 0x80000000) == 0)
420*3d8817e4Smiod 	    {
421*3d8817e4Smiod 	      count--;
422*3d8817e4Smiod 	      value <<= 1;
423*3d8817e4Smiod 	    }
424*3d8817e4Smiod 	}
425*3d8817e4Smiod       else if (mode == 2)
426*3d8817e4Smiod 	{
427*3d8817e4Smiod 	  count = 0;
428*3d8817e4Smiod 	  while ((value & 0x00000001) == 0)
429*3d8817e4Smiod 	    {
430*3d8817e4Smiod 	      count++;
431*3d8817e4Smiod 	      value >>= 1;
432*3d8817e4Smiod 	    }
433*3d8817e4Smiod 	}
434*3d8817e4Smiod 
435*3d8817e4Smiod       *valuep = count;
436*3d8817e4Smiod     }
437*3d8817e4Smiod 
438*3d8817e4Smiod   return errmsg;
439*3d8817e4Smiod }
440*3d8817e4Smiod 
441*3d8817e4Smiod /* -- dis.c */
442*3d8817e4Smiod 
443*3d8817e4Smiod const char * ip2k_cgen_parse_operand
444*3d8817e4Smiod   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
445*3d8817e4Smiod 
446*3d8817e4Smiod /* Main entry point for operand parsing.
447*3d8817e4Smiod 
448*3d8817e4Smiod    This function is basically just a big switch statement.  Earlier versions
449*3d8817e4Smiod    used tables to look up the function to use, but
450*3d8817e4Smiod    - if the table contains both assembler and disassembler functions then
451*3d8817e4Smiod      the disassembler contains much of the assembler and vice-versa,
452*3d8817e4Smiod    - there's a lot of inlining possibilities as things grow,
453*3d8817e4Smiod    - using a switch statement avoids the function call overhead.
454*3d8817e4Smiod 
455*3d8817e4Smiod    This function could be moved into `parse_insn_normal', but keeping it
456*3d8817e4Smiod    separate makes clear the interface between `parse_insn_normal' and each of
457*3d8817e4Smiod    the handlers.  */
458*3d8817e4Smiod 
459*3d8817e4Smiod const char *
ip2k_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)460*3d8817e4Smiod ip2k_cgen_parse_operand (CGEN_CPU_DESC cd,
461*3d8817e4Smiod 			   int opindex,
462*3d8817e4Smiod 			   const char ** strp,
463*3d8817e4Smiod 			   CGEN_FIELDS * fields)
464*3d8817e4Smiod {
465*3d8817e4Smiod   const char * errmsg = NULL;
466*3d8817e4Smiod   /* Used by scalar operands that still need to be parsed.  */
467*3d8817e4Smiod   long junk ATTRIBUTE_UNUSED;
468*3d8817e4Smiod 
469*3d8817e4Smiod   switch (opindex)
470*3d8817e4Smiod     {
471*3d8817e4Smiod     case IP2K_OPERAND_ADDR16CJP :
472*3d8817e4Smiod       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, (unsigned long *) (& fields->f_addr16cjp));
473*3d8817e4Smiod       break;
474*3d8817e4Smiod     case IP2K_OPERAND_ADDR16H :
475*3d8817e4Smiod       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, (unsigned long *) (& fields->f_imm8));
476*3d8817e4Smiod       break;
477*3d8817e4Smiod     case IP2K_OPERAND_ADDR16L :
478*3d8817e4Smiod       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, (unsigned long *) (& fields->f_imm8));
479*3d8817e4Smiod       break;
480*3d8817e4Smiod     case IP2K_OPERAND_ADDR16P :
481*3d8817e4Smiod       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, (unsigned long *) (& fields->f_page3));
482*3d8817e4Smiod       break;
483*3d8817e4Smiod     case IP2K_OPERAND_BITNO :
484*3d8817e4Smiod       errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, (unsigned long *) (& fields->f_bitno));
485*3d8817e4Smiod       break;
486*3d8817e4Smiod     case IP2K_OPERAND_CBIT :
487*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, (unsigned long *) (& junk));
488*3d8817e4Smiod       break;
489*3d8817e4Smiod     case IP2K_OPERAND_DCBIT :
490*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, (unsigned long *) (& junk));
491*3d8817e4Smiod       break;
492*3d8817e4Smiod     case IP2K_OPERAND_FR :
493*3d8817e4Smiod       errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, (unsigned long *) (& fields->f_reg));
494*3d8817e4Smiod       break;
495*3d8817e4Smiod     case IP2K_OPERAND_LIT8 :
496*3d8817e4Smiod       errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, (long *) (& fields->f_imm8));
497*3d8817e4Smiod       break;
498*3d8817e4Smiod     case IP2K_OPERAND_PABITS :
499*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, (unsigned long *) (& junk));
500*3d8817e4Smiod       break;
501*3d8817e4Smiod     case IP2K_OPERAND_RETI3 :
502*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, (unsigned long *) (& fields->f_reti3));
503*3d8817e4Smiod       break;
504*3d8817e4Smiod     case IP2K_OPERAND_ZBIT :
505*3d8817e4Smiod       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, (unsigned long *) (& junk));
506*3d8817e4Smiod       break;
507*3d8817e4Smiod 
508*3d8817e4Smiod     default :
509*3d8817e4Smiod       /* xgettext:c-format */
510*3d8817e4Smiod       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
511*3d8817e4Smiod       abort ();
512*3d8817e4Smiod   }
513*3d8817e4Smiod 
514*3d8817e4Smiod   return errmsg;
515*3d8817e4Smiod }
516*3d8817e4Smiod 
517*3d8817e4Smiod cgen_parse_fn * const ip2k_cgen_parse_handlers[] =
518*3d8817e4Smiod {
519*3d8817e4Smiod   parse_insn_normal,
520*3d8817e4Smiod };
521*3d8817e4Smiod 
522*3d8817e4Smiod void
ip2k_cgen_init_asm(CGEN_CPU_DESC cd)523*3d8817e4Smiod ip2k_cgen_init_asm (CGEN_CPU_DESC cd)
524*3d8817e4Smiod {
525*3d8817e4Smiod   ip2k_cgen_init_opcode_table (cd);
526*3d8817e4Smiod   ip2k_cgen_init_ibld_table (cd);
527*3d8817e4Smiod   cd->parse_handlers = & ip2k_cgen_parse_handlers[0];
528*3d8817e4Smiod   cd->parse_operand = ip2k_cgen_parse_operand;
529*3d8817e4Smiod }
530*3d8817e4Smiod 
531*3d8817e4Smiod 
532*3d8817e4Smiod 
533*3d8817e4Smiod /* Regex construction routine.
534*3d8817e4Smiod 
535*3d8817e4Smiod    This translates an opcode syntax string into a regex string,
536*3d8817e4Smiod    by replacing any non-character syntax element (such as an
537*3d8817e4Smiod    opcode) with the pattern '.*'
538*3d8817e4Smiod 
539*3d8817e4Smiod    It then compiles the regex and stores it in the opcode, for
540*3d8817e4Smiod    later use by ip2k_cgen_assemble_insn
541*3d8817e4Smiod 
542*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
543*3d8817e4Smiod 
544*3d8817e4Smiod char *
ip2k_cgen_build_insn_regex(CGEN_INSN * insn)545*3d8817e4Smiod ip2k_cgen_build_insn_regex (CGEN_INSN *insn)
546*3d8817e4Smiod {
547*3d8817e4Smiod   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
548*3d8817e4Smiod   const char *mnem = CGEN_INSN_MNEMONIC (insn);
549*3d8817e4Smiod   char rxbuf[CGEN_MAX_RX_ELEMENTS];
550*3d8817e4Smiod   char *rx = rxbuf;
551*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE *syn;
552*3d8817e4Smiod   int reg_err;
553*3d8817e4Smiod 
554*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
555*3d8817e4Smiod 
556*3d8817e4Smiod   /* Mnemonics come first in the syntax string.  */
557*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
558*3d8817e4Smiod     return _("missing mnemonic in syntax string");
559*3d8817e4Smiod   ++syn;
560*3d8817e4Smiod 
561*3d8817e4Smiod   /* Generate a case sensitive regular expression that emulates case
562*3d8817e4Smiod      insensitive matching in the "C" locale.  We cannot generate a case
563*3d8817e4Smiod      insensitive regular expression because in Turkish locales, 'i' and 'I'
564*3d8817e4Smiod      are not equal modulo case conversion.  */
565*3d8817e4Smiod 
566*3d8817e4Smiod   /* Copy the literal mnemonic out of the insn.  */
567*3d8817e4Smiod   for (; *mnem; mnem++)
568*3d8817e4Smiod     {
569*3d8817e4Smiod       char c = *mnem;
570*3d8817e4Smiod 
571*3d8817e4Smiod       if (ISALPHA (c))
572*3d8817e4Smiod 	{
573*3d8817e4Smiod 	  *rx++ = '[';
574*3d8817e4Smiod 	  *rx++ = TOLOWER (c);
575*3d8817e4Smiod 	  *rx++ = TOUPPER (c);
576*3d8817e4Smiod 	  *rx++ = ']';
577*3d8817e4Smiod 	}
578*3d8817e4Smiod       else
579*3d8817e4Smiod 	*rx++ = c;
580*3d8817e4Smiod     }
581*3d8817e4Smiod 
582*3d8817e4Smiod   /* Copy any remaining literals from the syntax string into the rx.  */
583*3d8817e4Smiod   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
584*3d8817e4Smiod     {
585*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
586*3d8817e4Smiod 	{
587*3d8817e4Smiod 	  char c = CGEN_SYNTAX_CHAR (* syn);
588*3d8817e4Smiod 
589*3d8817e4Smiod 	  switch (c)
590*3d8817e4Smiod 	    {
591*3d8817e4Smiod 	      /* Escape any regex metacharacters in the syntax.  */
592*3d8817e4Smiod 	    case '.': case '[': case '\\':
593*3d8817e4Smiod 	    case '*': case '^': case '$':
594*3d8817e4Smiod 
595*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
596*3d8817e4Smiod 	    case '?': case '{': case '}':
597*3d8817e4Smiod 	    case '(': case ')': case '*':
598*3d8817e4Smiod 	    case '|': case '+': case ']':
599*3d8817e4Smiod #endif
600*3d8817e4Smiod 	      *rx++ = '\\';
601*3d8817e4Smiod 	      *rx++ = c;
602*3d8817e4Smiod 	      break;
603*3d8817e4Smiod 
604*3d8817e4Smiod 	    default:
605*3d8817e4Smiod 	      if (ISALPHA (c))
606*3d8817e4Smiod 		{
607*3d8817e4Smiod 		  *rx++ = '[';
608*3d8817e4Smiod 		  *rx++ = TOLOWER (c);
609*3d8817e4Smiod 		  *rx++ = TOUPPER (c);
610*3d8817e4Smiod 		  *rx++ = ']';
611*3d8817e4Smiod 		}
612*3d8817e4Smiod 	      else
613*3d8817e4Smiod 		*rx++ = c;
614*3d8817e4Smiod 	      break;
615*3d8817e4Smiod 	    }
616*3d8817e4Smiod 	}
617*3d8817e4Smiod       else
618*3d8817e4Smiod 	{
619*3d8817e4Smiod 	  /* Replace non-syntax fields with globs.  */
620*3d8817e4Smiod 	  *rx++ = '.';
621*3d8817e4Smiod 	  *rx++ = '*';
622*3d8817e4Smiod 	}
623*3d8817e4Smiod     }
624*3d8817e4Smiod 
625*3d8817e4Smiod   /* Trailing whitespace ok.  */
626*3d8817e4Smiod   * rx++ = '[';
627*3d8817e4Smiod   * rx++ = ' ';
628*3d8817e4Smiod   * rx++ = '\t';
629*3d8817e4Smiod   * rx++ = ']';
630*3d8817e4Smiod   * rx++ = '*';
631*3d8817e4Smiod 
632*3d8817e4Smiod   /* But anchor it after that.  */
633*3d8817e4Smiod   * rx++ = '$';
634*3d8817e4Smiod   * rx = '\0';
635*3d8817e4Smiod 
636*3d8817e4Smiod   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
637*3d8817e4Smiod   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
638*3d8817e4Smiod 
639*3d8817e4Smiod   if (reg_err == 0)
640*3d8817e4Smiod     return NULL;
641*3d8817e4Smiod   else
642*3d8817e4Smiod     {
643*3d8817e4Smiod       static char msg[80];
644*3d8817e4Smiod 
645*3d8817e4Smiod       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
646*3d8817e4Smiod       regfree ((regex_t *) CGEN_INSN_RX (insn));
647*3d8817e4Smiod       free (CGEN_INSN_RX (insn));
648*3d8817e4Smiod       (CGEN_INSN_RX (insn)) = NULL;
649*3d8817e4Smiod       return msg;
650*3d8817e4Smiod     }
651*3d8817e4Smiod }
652*3d8817e4Smiod 
653*3d8817e4Smiod 
654*3d8817e4Smiod /* Default insn parser.
655*3d8817e4Smiod 
656*3d8817e4Smiod    The syntax string is scanned and operands are parsed and stored in FIELDS.
657*3d8817e4Smiod    Relocs are queued as we go via other callbacks.
658*3d8817e4Smiod 
659*3d8817e4Smiod    ??? Note that this is currently an all-or-nothing parser.  If we fail to
660*3d8817e4Smiod    parse the instruction, we return 0 and the caller will start over from
661*3d8817e4Smiod    the beginning.  Backtracking will be necessary in parsing subexpressions,
662*3d8817e4Smiod    but that can be handled there.  Not handling backtracking here may get
663*3d8817e4Smiod    expensive in the case of the m68k.  Deal with later.
664*3d8817e4Smiod 
665*3d8817e4Smiod    Returns NULL for success, an error message for failure.  */
666*3d8817e4Smiod 
667*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)668*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
669*3d8817e4Smiod 		   const CGEN_INSN *insn,
670*3d8817e4Smiod 		   const char **strp,
671*3d8817e4Smiod 		   CGEN_FIELDS *fields)
672*3d8817e4Smiod {
673*3d8817e4Smiod   /* ??? Runtime added insns not handled yet.  */
674*3d8817e4Smiod   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
675*3d8817e4Smiod   const char *str = *strp;
676*3d8817e4Smiod   const char *errmsg;
677*3d8817e4Smiod   const char *p;
678*3d8817e4Smiod   const CGEN_SYNTAX_CHAR_TYPE * syn;
679*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
680*3d8817e4Smiod   /* FIXME: wip */
681*3d8817e4Smiod   int past_opcode_p;
682*3d8817e4Smiod #endif
683*3d8817e4Smiod 
684*3d8817e4Smiod   /* For now we assume the mnemonic is first (there are no leading operands).
685*3d8817e4Smiod      We can parse it without needing to set up operand parsing.
686*3d8817e4Smiod      GAS's input scrubber will ensure mnemonics are lowercase, but we may
687*3d8817e4Smiod      not be called from GAS.  */
688*3d8817e4Smiod   p = CGEN_INSN_MNEMONIC (insn);
689*3d8817e4Smiod   while (*p && TOLOWER (*p) == TOLOWER (*str))
690*3d8817e4Smiod     ++p, ++str;
691*3d8817e4Smiod 
692*3d8817e4Smiod   if (* p)
693*3d8817e4Smiod     return _("unrecognized instruction");
694*3d8817e4Smiod 
695*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
696*3d8817e4Smiod   if (* str && ! ISSPACE (* str))
697*3d8817e4Smiod     return _("unrecognized instruction");
698*3d8817e4Smiod #endif
699*3d8817e4Smiod 
700*3d8817e4Smiod   CGEN_INIT_PARSE (cd);
701*3d8817e4Smiod   cgen_init_parse_operand (cd);
702*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
703*3d8817e4Smiod   past_opcode_p = 0;
704*3d8817e4Smiod #endif
705*3d8817e4Smiod 
706*3d8817e4Smiod   /* We don't check for (*str != '\0') here because we want to parse
707*3d8817e4Smiod      any trailing fake arguments in the syntax string.  */
708*3d8817e4Smiod   syn = CGEN_SYNTAX_STRING (syntax);
709*3d8817e4Smiod 
710*3d8817e4Smiod   /* Mnemonics come first for now, ensure valid string.  */
711*3d8817e4Smiod   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
712*3d8817e4Smiod     abort ();
713*3d8817e4Smiod 
714*3d8817e4Smiod   ++syn;
715*3d8817e4Smiod 
716*3d8817e4Smiod   while (* syn != 0)
717*3d8817e4Smiod     {
718*3d8817e4Smiod       /* Non operand chars must match exactly.  */
719*3d8817e4Smiod       if (CGEN_SYNTAX_CHAR_P (* syn))
720*3d8817e4Smiod 	{
721*3d8817e4Smiod 	  /* FIXME: While we allow for non-GAS callers above, we assume the
722*3d8817e4Smiod 	     first char after the mnemonic part is a space.  */
723*3d8817e4Smiod 	  /* FIXME: We also take inappropriate advantage of the fact that
724*3d8817e4Smiod 	     GAS's input scrubber will remove extraneous blanks.  */
725*3d8817e4Smiod 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
726*3d8817e4Smiod 	    {
727*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
728*3d8817e4Smiod 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
729*3d8817e4Smiod 		past_opcode_p = 1;
730*3d8817e4Smiod #endif
731*3d8817e4Smiod 	      ++ syn;
732*3d8817e4Smiod 	      ++ str;
733*3d8817e4Smiod 	    }
734*3d8817e4Smiod 	  else if (*str)
735*3d8817e4Smiod 	    {
736*3d8817e4Smiod 	      /* Syntax char didn't match.  Can't be this insn.  */
737*3d8817e4Smiod 	      static char msg [80];
738*3d8817e4Smiod 
739*3d8817e4Smiod 	      /* xgettext:c-format */
740*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
741*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn), *str);
742*3d8817e4Smiod 	      return msg;
743*3d8817e4Smiod 	    }
744*3d8817e4Smiod 	  else
745*3d8817e4Smiod 	    {
746*3d8817e4Smiod 	      /* Ran out of input.  */
747*3d8817e4Smiod 	      static char msg [80];
748*3d8817e4Smiod 
749*3d8817e4Smiod 	      /* xgettext:c-format */
750*3d8817e4Smiod 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
751*3d8817e4Smiod 		       CGEN_SYNTAX_CHAR(*syn));
752*3d8817e4Smiod 	      return msg;
753*3d8817e4Smiod 	    }
754*3d8817e4Smiod 	  continue;
755*3d8817e4Smiod 	}
756*3d8817e4Smiod 
757*3d8817e4Smiod       /* We have an operand of some sort.  */
758*3d8817e4Smiod       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
759*3d8817e4Smiod 					  &str, fields);
760*3d8817e4Smiod       if (errmsg)
761*3d8817e4Smiod 	return errmsg;
762*3d8817e4Smiod 
763*3d8817e4Smiod       /* Done with this operand, continue with next one.  */
764*3d8817e4Smiod       ++ syn;
765*3d8817e4Smiod     }
766*3d8817e4Smiod 
767*3d8817e4Smiod   /* If we're at the end of the syntax string, we're done.  */
768*3d8817e4Smiod   if (* syn == 0)
769*3d8817e4Smiod     {
770*3d8817e4Smiod       /* FIXME: For the moment we assume a valid `str' can only contain
771*3d8817e4Smiod 	 blanks now.  IE: We needn't try again with a longer version of
772*3d8817e4Smiod 	 the insn and it is assumed that longer versions of insns appear
773*3d8817e4Smiod 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
774*3d8817e4Smiod       while (ISSPACE (* str))
775*3d8817e4Smiod 	++ str;
776*3d8817e4Smiod 
777*3d8817e4Smiod       if (* str != '\0')
778*3d8817e4Smiod 	return _("junk at end of line"); /* FIXME: would like to include `str' */
779*3d8817e4Smiod 
780*3d8817e4Smiod       return NULL;
781*3d8817e4Smiod     }
782*3d8817e4Smiod 
783*3d8817e4Smiod   /* We couldn't parse it.  */
784*3d8817e4Smiod   return _("unrecognized instruction");
785*3d8817e4Smiod }
786*3d8817e4Smiod 
787*3d8817e4Smiod /* Main entry point.
788*3d8817e4Smiod    This routine is called for each instruction to be assembled.
789*3d8817e4Smiod    STR points to the insn to be assembled.
790*3d8817e4Smiod    We assume all necessary tables have been initialized.
791*3d8817e4Smiod    The assembled instruction, less any fixups, is stored in BUF.
792*3d8817e4Smiod    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
793*3d8817e4Smiod    still needs to be converted to target byte order, otherwise BUF is an array
794*3d8817e4Smiod    of bytes in target byte order.
795*3d8817e4Smiod    The result is a pointer to the insn's entry in the opcode table,
796*3d8817e4Smiod    or NULL if an error occured (an error message will have already been
797*3d8817e4Smiod    printed).
798*3d8817e4Smiod 
799*3d8817e4Smiod    Note that when processing (non-alias) macro-insns,
800*3d8817e4Smiod    this function recurses.
801*3d8817e4Smiod 
802*3d8817e4Smiod    ??? It's possible to make this cpu-independent.
803*3d8817e4Smiod    One would have to deal with a few minor things.
804*3d8817e4Smiod    At this point in time doing so would be more of a curiosity than useful
805*3d8817e4Smiod    [for example this file isn't _that_ big], but keeping the possibility in
806*3d8817e4Smiod    mind helps keep the design clean.  */
807*3d8817e4Smiod 
808*3d8817e4Smiod const CGEN_INSN *
ip2k_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)809*3d8817e4Smiod ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd,
810*3d8817e4Smiod 			   const char *str,
811*3d8817e4Smiod 			   CGEN_FIELDS *fields,
812*3d8817e4Smiod 			   CGEN_INSN_BYTES_PTR buf,
813*3d8817e4Smiod 			   char **errmsg)
814*3d8817e4Smiod {
815*3d8817e4Smiod   const char *start;
816*3d8817e4Smiod   CGEN_INSN_LIST *ilist;
817*3d8817e4Smiod   const char *parse_errmsg = NULL;
818*3d8817e4Smiod   const char *insert_errmsg = NULL;
819*3d8817e4Smiod   int recognized_mnemonic = 0;
820*3d8817e4Smiod 
821*3d8817e4Smiod   /* Skip leading white space.  */
822*3d8817e4Smiod   while (ISSPACE (* str))
823*3d8817e4Smiod     ++ str;
824*3d8817e4Smiod 
825*3d8817e4Smiod   /* The instructions are stored in hashed lists.
826*3d8817e4Smiod      Get the first in the list.  */
827*3d8817e4Smiod   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
828*3d8817e4Smiod 
829*3d8817e4Smiod   /* Keep looking until we find a match.  */
830*3d8817e4Smiod   start = str;
831*3d8817e4Smiod   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
832*3d8817e4Smiod     {
833*3d8817e4Smiod       const CGEN_INSN *insn = ilist->insn;
834*3d8817e4Smiod       recognized_mnemonic = 1;
835*3d8817e4Smiod 
836*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
837*3d8817e4Smiod       /* Not usually needed as unsupported opcodes
838*3d8817e4Smiod 	 shouldn't be in the hash lists.  */
839*3d8817e4Smiod       /* Is this insn supported by the selected cpu?  */
840*3d8817e4Smiod       if (! ip2k_cgen_insn_supported (cd, insn))
841*3d8817e4Smiod 	continue;
842*3d8817e4Smiod #endif
843*3d8817e4Smiod       /* If the RELAXED attribute is set, this is an insn that shouldn't be
844*3d8817e4Smiod 	 chosen immediately.  Instead, it is used during assembler/linker
845*3d8817e4Smiod 	 relaxation if possible.  */
846*3d8817e4Smiod       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
847*3d8817e4Smiod 	continue;
848*3d8817e4Smiod 
849*3d8817e4Smiod       str = start;
850*3d8817e4Smiod 
851*3d8817e4Smiod       /* Skip this insn if str doesn't look right lexically.  */
852*3d8817e4Smiod       if (CGEN_INSN_RX (insn) != NULL &&
853*3d8817e4Smiod 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
854*3d8817e4Smiod 	continue;
855*3d8817e4Smiod 
856*3d8817e4Smiod       /* Allow parse/insert handlers to obtain length of insn.  */
857*3d8817e4Smiod       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
858*3d8817e4Smiod 
859*3d8817e4Smiod       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
860*3d8817e4Smiod       if (parse_errmsg != NULL)
861*3d8817e4Smiod 	continue;
862*3d8817e4Smiod 
863*3d8817e4Smiod       /* ??? 0 is passed for `pc'.  */
864*3d8817e4Smiod       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
865*3d8817e4Smiod 						 (bfd_vma) 0);
866*3d8817e4Smiod       if (insert_errmsg != NULL)
867*3d8817e4Smiod         continue;
868*3d8817e4Smiod 
869*3d8817e4Smiod       /* It is up to the caller to actually output the insn and any
870*3d8817e4Smiod          queued relocs.  */
871*3d8817e4Smiod       return insn;
872*3d8817e4Smiod     }
873*3d8817e4Smiod 
874*3d8817e4Smiod   {
875*3d8817e4Smiod     static char errbuf[150];
876*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
877*3d8817e4Smiod     const char *tmp_errmsg;
878*3d8817e4Smiod 
879*3d8817e4Smiod     /* If requesting verbose error messages, use insert_errmsg.
880*3d8817e4Smiod        Failing that, use parse_errmsg.  */
881*3d8817e4Smiod     tmp_errmsg = (insert_errmsg ? insert_errmsg :
882*3d8817e4Smiod 		  parse_errmsg ? parse_errmsg :
883*3d8817e4Smiod 		  recognized_mnemonic ?
884*3d8817e4Smiod 		  _("unrecognized form of instruction") :
885*3d8817e4Smiod 		  _("unrecognized instruction"));
886*3d8817e4Smiod 
887*3d8817e4Smiod     if (strlen (start) > 50)
888*3d8817e4Smiod       /* xgettext:c-format */
889*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
890*3d8817e4Smiod     else
891*3d8817e4Smiod       /* xgettext:c-format */
892*3d8817e4Smiod       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
893*3d8817e4Smiod #else
894*3d8817e4Smiod     if (strlen (start) > 50)
895*3d8817e4Smiod       /* xgettext:c-format */
896*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
897*3d8817e4Smiod     else
898*3d8817e4Smiod       /* xgettext:c-format */
899*3d8817e4Smiod       sprintf (errbuf, _("bad instruction `%.50s'"), start);
900*3d8817e4Smiod #endif
901*3d8817e4Smiod 
902*3d8817e4Smiod     *errmsg = errbuf;
903*3d8817e4Smiod     return NULL;
904*3d8817e4Smiod   }
905*3d8817e4Smiod }
906