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, 2007, 2008, 2010
8    Free Software Foundation, Inc.
9 
10    This file is part of libopcodes.
11 
12    This library 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 3, or (at your option)
15    any later version.
16 
17    It is distributed in the hope that it will be useful, but WITHOUT
18    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
20    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 
27 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
28    Keep that in mind.  */
29 
30 #include "sysdep.h"
31 #include <stdio.h>
32 #include "ansidecl.h"
33 #include "bfd.h"
34 #include "symcat.h"
35 #include "lm32-desc.h"
36 #include "lm32-opc.h"
37 #include "opintl.h"
38 #include "xregex.h"
39 #include "libiberty.h"
40 #include "safe-ctype.h"
41 
42 #undef  min
43 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #undef  max
45 #define max(a,b) ((a) > (b) ? (a) : (b))
46 
47 static const char * parse_insn_normal
48   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
49 
50 /* -- assembler routines inserted here.  */
51 
52 /* -- asm.c */
53 
54 /* Handle signed/unsigned literal.  */
55 
56 static const char *
parse_imm(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)57 parse_imm (CGEN_CPU_DESC cd,
58 	   const char **strp,
59 	   int opindex,
60 	   unsigned long *valuep)
61 {
62   const char *errmsg;
63   signed long value;
64 
65   errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
66   if (errmsg == NULL)
67     {
68       unsigned long x = value & 0xFFFF0000;
69       if (x != 0 && x != 0xFFFF0000)
70         errmsg = _("immediate value out of range");
71       else
72         *valuep = (value & 0xFFFF);
73     }
74   return errmsg;
75 }
76 
77 /* Handle hi() */
78 
79 static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)80 parse_hi16 (CGEN_CPU_DESC cd,
81 	    const char **strp,
82 	    int opindex,
83 	    unsigned long *valuep)
84 {
85   if (strncasecmp (*strp, "hi(", 3) == 0)
86     {
87       enum cgen_parse_operand_result result_type;
88       bfd_vma value;
89       const char *errmsg;
90 
91       *strp += 3;
92       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
93                                    &result_type, &value);
94       if (**strp != ')')
95         return _("missing `)'");
96 
97       ++*strp;
98       if (errmsg == NULL
99           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
100         value = (value >> 16) & 0xffff;
101       *valuep = value;
102 
103       return errmsg;
104     }
105 
106   return parse_imm (cd, strp, opindex, valuep);
107 }
108 
109 /* Handle lo() */
110 
111 static const char *
parse_lo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)112 parse_lo16 (CGEN_CPU_DESC cd,
113 	    const char **strp,
114 	    int opindex,
115 	    unsigned long *valuep)
116 {
117   if (strncasecmp (*strp, "lo(", 3) == 0)
118     {
119       const char *errmsg;
120       enum cgen_parse_operand_result result_type;
121       bfd_vma value;
122 
123       *strp += 3;
124       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
125                                    &result_type, &value);
126       if (**strp != ')')
127         return _("missing `)'");
128       ++*strp;
129       if (errmsg == NULL
130           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
131         value &= 0xffff;
132       *valuep = value;
133       return errmsg;
134     }
135 
136   return parse_imm (cd, strp, opindex, valuep);
137 }
138 
139 /* Handle gp() */
140 
141 static const char *
parse_gp16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)142 parse_gp16 (CGEN_CPU_DESC cd,
143 	    const char **strp,
144 	    int opindex,
145 	    long *valuep)
146 {
147   if (strncasecmp (*strp, "gp(", 3) == 0)
148     {
149       const char *errmsg;
150       enum cgen_parse_operand_result result_type;
151       bfd_vma value;
152 
153       *strp += 3;
154       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_GPREL16,
155                                    & result_type, & value);
156       if (**strp != ')')
157         return _("missing `)'");
158       ++*strp;
159       if (errmsg == NULL
160           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
161         value &= 0xffff;
162       *valuep = value;
163       return errmsg;
164     }
165 
166   return _("expecting gp relative address: gp(symbol)");
167 }
168 
169 /* Handle got() */
170 
171 static const char *
parse_got16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)172 parse_got16 (CGEN_CPU_DESC cd,
173 	     const char **strp,
174 	     int opindex,
175 	     long *valuep)
176 {
177   if (strncasecmp (*strp, "got(", 4) == 0)
178     {
179       const char *errmsg;
180       enum cgen_parse_operand_result result_type;
181       bfd_vma value;
182 
183       *strp += 4;
184       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_16_GOT,
185                                    & result_type, & value);
186       if (**strp != ')')
187         return _("missing `)'");
188       ++*strp;
189       if (errmsg == NULL
190           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
191         value &= 0xffff;
192       *valuep = value;
193       return errmsg;
194     }
195 
196   return _("expecting got relative address: got(symbol)");
197 }
198 
199 /* Handle gotoffhi16() */
200 
201 static const char *
parse_gotoff_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)202 parse_gotoff_hi16 (CGEN_CPU_DESC cd,
203 		   const char **strp,
204 		   int opindex,
205 		   long *valuep)
206 {
207   if (strncasecmp (*strp, "gotoffhi16(", 11) == 0)
208     {
209       const char *errmsg;
210       enum cgen_parse_operand_result result_type;
211       bfd_vma value;
212 
213       *strp += 11;
214       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_HI16,
215                                    & result_type, & value);
216       if (**strp != ')')
217         return _("missing `)'");
218       ++*strp;
219       if (errmsg == NULL
220           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
221         value &= 0xffff;
222       *valuep = value;
223       return errmsg;
224     }
225 
226   return _("expecting got relative address: gotoffhi16(symbol)");
227 }
228 
229 /* Handle gotofflo16() */
230 
231 static const char *
parse_gotoff_lo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)232 parse_gotoff_lo16 (CGEN_CPU_DESC cd,
233 		   const char **strp,
234 		   int opindex,
235 		   long *valuep)
236 {
237   if (strncasecmp (*strp, "gotofflo16(", 11) == 0)
238     {
239       const char *errmsg;
240       enum cgen_parse_operand_result result_type;
241       bfd_vma value;
242 
243       *strp += 11;
244       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_LO16,
245                                    &result_type, &value);
246       if (**strp != ')')
247         return _("missing `)'");
248       ++*strp;
249       if (errmsg == NULL
250           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
251         value &= 0xffff;
252       *valuep = value;
253       return errmsg;
254     }
255 
256   return _("expecting got relative address: gotofflo16(symbol)");
257 }
258 
259 const char * lm32_cgen_parse_operand
260   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
261 
262 /* Main entry point for operand parsing.
263 
264    This function is basically just a big switch statement.  Earlier versions
265    used tables to look up the function to use, but
266    - if the table contains both assembler and disassembler functions then
267      the disassembler contains much of the assembler and vice-versa,
268    - there's a lot of inlining possibilities as things grow,
269    - using a switch statement avoids the function call overhead.
270 
271    This function could be moved into `parse_insn_normal', but keeping it
272    separate makes clear the interface between `parse_insn_normal' and each of
273    the handlers.  */
274 
275 const char *
lm32_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)276 lm32_cgen_parse_operand (CGEN_CPU_DESC cd,
277 			   int opindex,
278 			   const char ** strp,
279 			   CGEN_FIELDS * fields)
280 {
281   const char * errmsg = NULL;
282   /* Used by scalar operands that still need to be parsed.  */
283   long junk ATTRIBUTE_UNUSED;
284 
285   switch (opindex)
286     {
287     case LM32_OPERAND_BRANCH :
288       {
289         bfd_vma value = 0;
290         errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_BRANCH, 0, NULL,  & value);
291         fields->f_branch = value;
292       }
293       break;
294     case LM32_OPERAND_CALL :
295       {
296         bfd_vma value = 0;
297         errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_CALL, 0, NULL,  & value);
298         fields->f_call = value;
299       }
300       break;
301     case LM32_OPERAND_CSR :
302       errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_csr, & fields->f_csr);
303       break;
304     case LM32_OPERAND_EXCEPTION :
305       errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_EXCEPTION, (unsigned long *) (& fields->f_exception));
306       break;
307     case LM32_OPERAND_GOT16 :
308       errmsg = parse_got16 (cd, strp, LM32_OPERAND_GOT16, (long *) (& fields->f_imm));
309       break;
310     case LM32_OPERAND_GOTOFFHI16 :
311       errmsg = parse_gotoff_hi16 (cd, strp, LM32_OPERAND_GOTOFFHI16, (long *) (& fields->f_imm));
312       break;
313     case LM32_OPERAND_GOTOFFLO16 :
314       errmsg = parse_gotoff_lo16 (cd, strp, LM32_OPERAND_GOTOFFLO16, (long *) (& fields->f_imm));
315       break;
316     case LM32_OPERAND_GP16 :
317       errmsg = parse_gp16 (cd, strp, LM32_OPERAND_GP16, (long *) (& fields->f_imm));
318       break;
319     case LM32_OPERAND_HI16 :
320       errmsg = parse_hi16 (cd, strp, LM32_OPERAND_HI16, (unsigned long *) (& fields->f_uimm));
321       break;
322     case LM32_OPERAND_IMM :
323       errmsg = cgen_parse_signed_integer (cd, strp, LM32_OPERAND_IMM, (long *) (& fields->f_imm));
324       break;
325     case LM32_OPERAND_LO16 :
326       errmsg = parse_lo16 (cd, strp, LM32_OPERAND_LO16, (unsigned long *) (& fields->f_uimm));
327       break;
328     case LM32_OPERAND_R0 :
329       errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r0);
330       break;
331     case LM32_OPERAND_R1 :
332       errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r1);
333       break;
334     case LM32_OPERAND_R2 :
335       errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r2);
336       break;
337     case LM32_OPERAND_SHIFT :
338       errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_SHIFT, (unsigned long *) (& fields->f_shift));
339       break;
340     case LM32_OPERAND_UIMM :
341       errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_UIMM, (unsigned long *) (& fields->f_uimm));
342       break;
343     case LM32_OPERAND_USER :
344       errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_USER, (unsigned long *) (& fields->f_user));
345       break;
346 
347     default :
348       /* xgettext:c-format */
349       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
350       abort ();
351   }
352 
353   return errmsg;
354 }
355 
356 cgen_parse_fn * const lm32_cgen_parse_handlers[] =
357 {
358   parse_insn_normal,
359 };
360 
361 void
lm32_cgen_init_asm(CGEN_CPU_DESC cd)362 lm32_cgen_init_asm (CGEN_CPU_DESC cd)
363 {
364   lm32_cgen_init_opcode_table (cd);
365   lm32_cgen_init_ibld_table (cd);
366   cd->parse_handlers = & lm32_cgen_parse_handlers[0];
367   cd->parse_operand = lm32_cgen_parse_operand;
368 #ifdef CGEN_ASM_INIT_HOOK
369 CGEN_ASM_INIT_HOOK
370 #endif
371 }
372 
373 
374 
375 /* Regex construction routine.
376 
377    This translates an opcode syntax string into a regex string,
378    by replacing any non-character syntax element (such as an
379    opcode) with the pattern '.*'
380 
381    It then compiles the regex and stores it in the opcode, for
382    later use by lm32_cgen_assemble_insn
383 
384    Returns NULL for success, an error message for failure.  */
385 
386 char *
lm32_cgen_build_insn_regex(CGEN_INSN * insn)387 lm32_cgen_build_insn_regex (CGEN_INSN *insn)
388 {
389   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
390   const char *mnem = CGEN_INSN_MNEMONIC (insn);
391   char rxbuf[CGEN_MAX_RX_ELEMENTS];
392   char *rx = rxbuf;
393   const CGEN_SYNTAX_CHAR_TYPE *syn;
394   int reg_err;
395 
396   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
397 
398   /* Mnemonics come first in the syntax string.  */
399   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
400     return _("missing mnemonic in syntax string");
401   ++syn;
402 
403   /* Generate a case sensitive regular expression that emulates case
404      insensitive matching in the "C" locale.  We cannot generate a case
405      insensitive regular expression because in Turkish locales, 'i' and 'I'
406      are not equal modulo case conversion.  */
407 
408   /* Copy the literal mnemonic out of the insn.  */
409   for (; *mnem; mnem++)
410     {
411       char c = *mnem;
412 
413       if (ISALPHA (c))
414 	{
415 	  *rx++ = '[';
416 	  *rx++ = TOLOWER (c);
417 	  *rx++ = TOUPPER (c);
418 	  *rx++ = ']';
419 	}
420       else
421 	*rx++ = c;
422     }
423 
424   /* Copy any remaining literals from the syntax string into the rx.  */
425   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
426     {
427       if (CGEN_SYNTAX_CHAR_P (* syn))
428 	{
429 	  char c = CGEN_SYNTAX_CHAR (* syn);
430 
431 	  switch (c)
432 	    {
433 	      /* Escape any regex metacharacters in the syntax.  */
434 	    case '.': case '[': case '\\':
435 	    case '*': case '^': case '$':
436 
437 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
438 	    case '?': case '{': case '}':
439 	    case '(': case ')': case '*':
440 	    case '|': case '+': case ']':
441 #endif
442 	      *rx++ = '\\';
443 	      *rx++ = c;
444 	      break;
445 
446 	    default:
447 	      if (ISALPHA (c))
448 		{
449 		  *rx++ = '[';
450 		  *rx++ = TOLOWER (c);
451 		  *rx++ = TOUPPER (c);
452 		  *rx++ = ']';
453 		}
454 	      else
455 		*rx++ = c;
456 	      break;
457 	    }
458 	}
459       else
460 	{
461 	  /* Replace non-syntax fields with globs.  */
462 	  *rx++ = '.';
463 	  *rx++ = '*';
464 	}
465     }
466 
467   /* Trailing whitespace ok.  */
468   * rx++ = '[';
469   * rx++ = ' ';
470   * rx++ = '\t';
471   * rx++ = ']';
472   * rx++ = '*';
473 
474   /* But anchor it after that.  */
475   * rx++ = '$';
476   * rx = '\0';
477 
478   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
479   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
480 
481   if (reg_err == 0)
482     return NULL;
483   else
484     {
485       static char msg[80];
486 
487       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
488       regfree ((regex_t *) CGEN_INSN_RX (insn));
489       free (CGEN_INSN_RX (insn));
490       (CGEN_INSN_RX (insn)) = NULL;
491       return msg;
492     }
493 }
494 
495 
496 /* Default insn parser.
497 
498    The syntax string is scanned and operands are parsed and stored in FIELDS.
499    Relocs are queued as we go via other callbacks.
500 
501    ??? Note that this is currently an all-or-nothing parser.  If we fail to
502    parse the instruction, we return 0 and the caller will start over from
503    the beginning.  Backtracking will be necessary in parsing subexpressions,
504    but that can be handled there.  Not handling backtracking here may get
505    expensive in the case of the m68k.  Deal with later.
506 
507    Returns NULL for success, an error message for failure.  */
508 
509 static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)510 parse_insn_normal (CGEN_CPU_DESC cd,
511 		   const CGEN_INSN *insn,
512 		   const char **strp,
513 		   CGEN_FIELDS *fields)
514 {
515   /* ??? Runtime added insns not handled yet.  */
516   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
517   const char *str = *strp;
518   const char *errmsg;
519   const char *p;
520   const CGEN_SYNTAX_CHAR_TYPE * syn;
521 #ifdef CGEN_MNEMONIC_OPERANDS
522   /* FIXME: wip */
523   int past_opcode_p;
524 #endif
525 
526   /* For now we assume the mnemonic is first (there are no leading operands).
527      We can parse it without needing to set up operand parsing.
528      GAS's input scrubber will ensure mnemonics are lowercase, but we may
529      not be called from GAS.  */
530   p = CGEN_INSN_MNEMONIC (insn);
531   while (*p && TOLOWER (*p) == TOLOWER (*str))
532     ++p, ++str;
533 
534   if (* p)
535     return _("unrecognized instruction");
536 
537 #ifndef CGEN_MNEMONIC_OPERANDS
538   if (* str && ! ISSPACE (* str))
539     return _("unrecognized instruction");
540 #endif
541 
542   CGEN_INIT_PARSE (cd);
543   cgen_init_parse_operand (cd);
544 #ifdef CGEN_MNEMONIC_OPERANDS
545   past_opcode_p = 0;
546 #endif
547 
548   /* We don't check for (*str != '\0') here because we want to parse
549      any trailing fake arguments in the syntax string.  */
550   syn = CGEN_SYNTAX_STRING (syntax);
551 
552   /* Mnemonics come first for now, ensure valid string.  */
553   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
554     abort ();
555 
556   ++syn;
557 
558   while (* syn != 0)
559     {
560       /* Non operand chars must match exactly.  */
561       if (CGEN_SYNTAX_CHAR_P (* syn))
562 	{
563 	  /* FIXME: While we allow for non-GAS callers above, we assume the
564 	     first char after the mnemonic part is a space.  */
565 	  /* FIXME: We also take inappropriate advantage of the fact that
566 	     GAS's input scrubber will remove extraneous blanks.  */
567 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
568 	    {
569 #ifdef CGEN_MNEMONIC_OPERANDS
570 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
571 		past_opcode_p = 1;
572 #endif
573 	      ++ syn;
574 	      ++ str;
575 	    }
576 	  else if (*str)
577 	    {
578 	      /* Syntax char didn't match.  Can't be this insn.  */
579 	      static char msg [80];
580 
581 	      /* xgettext:c-format */
582 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
583 		       CGEN_SYNTAX_CHAR(*syn), *str);
584 	      return msg;
585 	    }
586 	  else
587 	    {
588 	      /* Ran out of input.  */
589 	      static char msg [80];
590 
591 	      /* xgettext:c-format */
592 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
593 		       CGEN_SYNTAX_CHAR(*syn));
594 	      return msg;
595 	    }
596 	  continue;
597 	}
598 
599 #ifdef CGEN_MNEMONIC_OPERANDS
600       (void) past_opcode_p;
601 #endif
602       /* We have an operand of some sort.  */
603       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
604       if (errmsg)
605 	return errmsg;
606 
607       /* Done with this operand, continue with next one.  */
608       ++ syn;
609     }
610 
611   /* If we're at the end of the syntax string, we're done.  */
612   if (* syn == 0)
613     {
614       /* FIXME: For the moment we assume a valid `str' can only contain
615 	 blanks now.  IE: We needn't try again with a longer version of
616 	 the insn and it is assumed that longer versions of insns appear
617 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
618       while (ISSPACE (* str))
619 	++ str;
620 
621       if (* str != '\0')
622 	return _("junk at end of line"); /* FIXME: would like to include `str' */
623 
624       return NULL;
625     }
626 
627   /* We couldn't parse it.  */
628   return _("unrecognized instruction");
629 }
630 
631 /* Main entry point.
632    This routine is called for each instruction to be assembled.
633    STR points to the insn to be assembled.
634    We assume all necessary tables have been initialized.
635    The assembled instruction, less any fixups, is stored in BUF.
636    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
637    still needs to be converted to target byte order, otherwise BUF is an array
638    of bytes in target byte order.
639    The result is a pointer to the insn's entry in the opcode table,
640    or NULL if an error occured (an error message will have already been
641    printed).
642 
643    Note that when processing (non-alias) macro-insns,
644    this function recurses.
645 
646    ??? It's possible to make this cpu-independent.
647    One would have to deal with a few minor things.
648    At this point in time doing so would be more of a curiosity than useful
649    [for example this file isn't _that_ big], but keeping the possibility in
650    mind helps keep the design clean.  */
651 
652 const CGEN_INSN *
lm32_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)653 lm32_cgen_assemble_insn (CGEN_CPU_DESC cd,
654 			   const char *str,
655 			   CGEN_FIELDS *fields,
656 			   CGEN_INSN_BYTES_PTR buf,
657 			   char **errmsg)
658 {
659   const char *start;
660   CGEN_INSN_LIST *ilist;
661   const char *parse_errmsg = NULL;
662   const char *insert_errmsg = NULL;
663   int recognized_mnemonic = 0;
664 
665   /* Skip leading white space.  */
666   while (ISSPACE (* str))
667     ++ str;
668 
669   /* The instructions are stored in hashed lists.
670      Get the first in the list.  */
671   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
672 
673   /* Keep looking until we find a match.  */
674   start = str;
675   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
676     {
677       const CGEN_INSN *insn = ilist->insn;
678       recognized_mnemonic = 1;
679 
680 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
681       /* Not usually needed as unsupported opcodes
682 	 shouldn't be in the hash lists.  */
683       /* Is this insn supported by the selected cpu?  */
684       if (! lm32_cgen_insn_supported (cd, insn))
685 	continue;
686 #endif
687       /* If the RELAXED attribute is set, this is an insn that shouldn't be
688 	 chosen immediately.  Instead, it is used during assembler/linker
689 	 relaxation if possible.  */
690       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
691 	continue;
692 
693       str = start;
694 
695       /* Skip this insn if str doesn't look right lexically.  */
696       if (CGEN_INSN_RX (insn) != NULL &&
697 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
698 	continue;
699 
700       /* Allow parse/insert handlers to obtain length of insn.  */
701       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
702 
703       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
704       if (parse_errmsg != NULL)
705 	continue;
706 
707       /* ??? 0 is passed for `pc'.  */
708       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
709 						 (bfd_vma) 0);
710       if (insert_errmsg != NULL)
711         continue;
712 
713       /* It is up to the caller to actually output the insn and any
714          queued relocs.  */
715       return insn;
716     }
717 
718   {
719     static char errbuf[150];
720     const char *tmp_errmsg;
721 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
722 #define be_verbose 1
723 #else
724 #define be_verbose 0
725 #endif
726 
727     if (be_verbose)
728       {
729 	/* If requesting verbose error messages, use insert_errmsg.
730 	   Failing that, use parse_errmsg.  */
731 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
732 		      parse_errmsg ? parse_errmsg :
733 		      recognized_mnemonic ?
734 		      _("unrecognized form of instruction") :
735 		      _("unrecognized instruction"));
736 
737 	if (strlen (start) > 50)
738 	  /* xgettext:c-format */
739 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
740 	else
741 	  /* xgettext:c-format */
742 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
743       }
744     else
745       {
746 	if (strlen (start) > 50)
747 	  /* xgettext:c-format */
748 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
749 	else
750 	  /* xgettext:c-format */
751 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
752       }
753 
754     *errmsg = errbuf;
755     return NULL;
756   }
757 }
758