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