xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/openrisc-asm.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
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 "openrisc-desc.h"
35 #include "openrisc-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 
53 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
54 
55 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
56 
57 long
openrisc_sign_extend_16bit(long value)58 openrisc_sign_extend_16bit (long value)
59 {
60   return ((value & 0xffff) ^ 0x8000) - 0x8000;
61 }
62 
63 /* Handle hi().  */
64 
65 static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)66 parse_hi16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
67 {
68   const char *errmsg;
69   enum cgen_parse_operand_result result_type;
70   unsigned long ret;
71 
72   if (**strp == '#')
73     ++*strp;
74 
75   if (strncasecmp (*strp, "hi(", 3) == 0)
76     {
77       bfd_vma value;
78 
79       *strp += 3;
80       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
81 				   & result_type, & value);
82       if (**strp != ')')
83         return MISSING_CLOSING_PARENTHESIS;
84 
85       ++*strp;
86       if (errmsg == NULL
87           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
88         value >>= 16;
89       ret = value;
90     }
91   else
92     {
93       if (**strp == '-')
94 	{
95 	  long value;
96 
97 	  errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
98 	  ret = value;
99 	}
100       else
101 	{
102 	  unsigned long value;
103 
104 	  errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
105 	  ret = value;
106 	}
107     }
108 
109   *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
110   return errmsg;
111 }
112 
113 /* Handle lo().  */
114 
115 static const char *
parse_lo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)116 parse_lo16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
117 {
118   const char *errmsg;
119   enum cgen_parse_operand_result result_type;
120   unsigned long ret;
121 
122   if (**strp == '#')
123     ++*strp;
124 
125   if (strncasecmp (*strp, "lo(", 3) == 0)
126     {
127       bfd_vma value;
128 
129       *strp += 3;
130       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
131 				   & result_type, & value);
132       if (**strp != ')')
133         return MISSING_CLOSING_PARENTHESIS;
134 
135       ++*strp;
136       ret = value;
137     }
138   else
139     {
140       if (**strp == '-')
141 	{
142 	  long value;
143 
144 	  errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
145 	  ret = value;
146 	}
147       else
148 	{
149 	  unsigned long value;
150 
151 	  errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
152 	  ret = value;
153 	}
154     }
155 
156   *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
157   return errmsg;
158 }
159 
160 /* -- */
161 
162 const char * openrisc_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 *
openrisc_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)179 openrisc_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 OPENRISC_OPERAND_ABS_26 :
191       {
192         bfd_vma value = 0;
193         errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_ABS_26, 0, NULL,  & value);
194         fields->f_abs26 = value;
195       }
196       break;
197     case OPENRISC_OPERAND_DISP_26 :
198       {
199         bfd_vma value = 0;
200         errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_DISP_26, 0, NULL,  & value);
201         fields->f_disp26 = value;
202       }
203       break;
204     case OPENRISC_OPERAND_HI16 :
205       errmsg = parse_hi16 (cd, strp, OPENRISC_OPERAND_HI16, (long *) (& fields->f_simm16));
206       break;
207     case OPENRISC_OPERAND_LO16 :
208       errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_LO16, (long *) (& fields->f_lo16));
209       break;
210     case OPENRISC_OPERAND_OP_F_23 :
211       errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_23, (unsigned long *) (& fields->f_op4));
212       break;
213     case OPENRISC_OPERAND_OP_F_3 :
214       errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_3, (unsigned long *) (& fields->f_op5));
215       break;
216     case OPENRISC_OPERAND_RA :
217       errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r2);
218       break;
219     case OPENRISC_OPERAND_RB :
220       errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r3);
221       break;
222     case OPENRISC_OPERAND_RD :
223       errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r1);
224       break;
225     case OPENRISC_OPERAND_SIMM_16 :
226       errmsg = cgen_parse_signed_integer (cd, strp, OPENRISC_OPERAND_SIMM_16, (long *) (& fields->f_simm16));
227       break;
228     case OPENRISC_OPERAND_UI16NC :
229       errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_UI16NC, (long *) (& fields->f_i16nc));
230       break;
231     case OPENRISC_OPERAND_UIMM_16 :
232       errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_16, (unsigned long *) (& fields->f_uimm16));
233       break;
234     case OPENRISC_OPERAND_UIMM_5 :
235       errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_5, (unsigned long *) (& fields->f_uimm5));
236       break;
237 
238     default :
239       /* xgettext:c-format */
240       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
241       abort ();
242   }
243 
244   return errmsg;
245 }
246 
247 cgen_parse_fn * const openrisc_cgen_parse_handlers[] =
248 {
249   parse_insn_normal,
250 };
251 
252 void
openrisc_cgen_init_asm(CGEN_CPU_DESC cd)253 openrisc_cgen_init_asm (CGEN_CPU_DESC cd)
254 {
255   openrisc_cgen_init_opcode_table (cd);
256   openrisc_cgen_init_ibld_table (cd);
257   cd->parse_handlers = & openrisc_cgen_parse_handlers[0];
258   cd->parse_operand = openrisc_cgen_parse_operand;
259 }
260 
261 
262 
263 /* Regex construction routine.
264 
265    This translates an opcode syntax string into a regex string,
266    by replacing any non-character syntax element (such as an
267    opcode) with the pattern '.*'
268 
269    It then compiles the regex and stores it in the opcode, for
270    later use by openrisc_cgen_assemble_insn
271 
272    Returns NULL for success, an error message for failure.  */
273 
274 char *
openrisc_cgen_build_insn_regex(CGEN_INSN * insn)275 openrisc_cgen_build_insn_regex (CGEN_INSN *insn)
276 {
277   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
278   const char *mnem = CGEN_INSN_MNEMONIC (insn);
279   char rxbuf[CGEN_MAX_RX_ELEMENTS];
280   char *rx = rxbuf;
281   const CGEN_SYNTAX_CHAR_TYPE *syn;
282   int reg_err;
283 
284   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
285 
286   /* Mnemonics come first in the syntax string.  */
287   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
288     return _("missing mnemonic in syntax string");
289   ++syn;
290 
291   /* Generate a case sensitive regular expression that emulates case
292      insensitive matching in the "C" locale.  We cannot generate a case
293      insensitive regular expression because in Turkish locales, 'i' and 'I'
294      are not equal modulo case conversion.  */
295 
296   /* Copy the literal mnemonic out of the insn.  */
297   for (; *mnem; mnem++)
298     {
299       char c = *mnem;
300 
301       if (ISALPHA (c))
302 	{
303 	  *rx++ = '[';
304 	  *rx++ = TOLOWER (c);
305 	  *rx++ = TOUPPER (c);
306 	  *rx++ = ']';
307 	}
308       else
309 	*rx++ = c;
310     }
311 
312   /* Copy any remaining literals from the syntax string into the rx.  */
313   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
314     {
315       if (CGEN_SYNTAX_CHAR_P (* syn))
316 	{
317 	  char c = CGEN_SYNTAX_CHAR (* syn);
318 
319 	  switch (c)
320 	    {
321 	      /* Escape any regex metacharacters in the syntax.  */
322 	    case '.': case '[': case '\\':
323 	    case '*': case '^': case '$':
324 
325 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
326 	    case '?': case '{': case '}':
327 	    case '(': case ')': case '*':
328 	    case '|': case '+': case ']':
329 #endif
330 	      *rx++ = '\\';
331 	      *rx++ = c;
332 	      break;
333 
334 	    default:
335 	      if (ISALPHA (c))
336 		{
337 		  *rx++ = '[';
338 		  *rx++ = TOLOWER (c);
339 		  *rx++ = TOUPPER (c);
340 		  *rx++ = ']';
341 		}
342 	      else
343 		*rx++ = c;
344 	      break;
345 	    }
346 	}
347       else
348 	{
349 	  /* Replace non-syntax fields with globs.  */
350 	  *rx++ = '.';
351 	  *rx++ = '*';
352 	}
353     }
354 
355   /* Trailing whitespace ok.  */
356   * rx++ = '[';
357   * rx++ = ' ';
358   * rx++ = '\t';
359   * rx++ = ']';
360   * rx++ = '*';
361 
362   /* But anchor it after that.  */
363   * rx++ = '$';
364   * rx = '\0';
365 
366   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
367   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
368 
369   if (reg_err == 0)
370     return NULL;
371   else
372     {
373       static char msg[80];
374 
375       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
376       regfree ((regex_t *) CGEN_INSN_RX (insn));
377       free (CGEN_INSN_RX (insn));
378       (CGEN_INSN_RX (insn)) = NULL;
379       return msg;
380     }
381 }
382 
383 
384 /* Default insn parser.
385 
386    The syntax string is scanned and operands are parsed and stored in FIELDS.
387    Relocs are queued as we go via other callbacks.
388 
389    ??? Note that this is currently an all-or-nothing parser.  If we fail to
390    parse the instruction, we return 0 and the caller will start over from
391    the beginning.  Backtracking will be necessary in parsing subexpressions,
392    but that can be handled there.  Not handling backtracking here may get
393    expensive in the case of the m68k.  Deal with later.
394 
395    Returns NULL for success, an error message for failure.  */
396 
397 static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)398 parse_insn_normal (CGEN_CPU_DESC cd,
399 		   const CGEN_INSN *insn,
400 		   const char **strp,
401 		   CGEN_FIELDS *fields)
402 {
403   /* ??? Runtime added insns not handled yet.  */
404   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
405   const char *str = *strp;
406   const char *errmsg;
407   const char *p;
408   const CGEN_SYNTAX_CHAR_TYPE * syn;
409 #ifdef CGEN_MNEMONIC_OPERANDS
410   /* FIXME: wip */
411   int past_opcode_p;
412 #endif
413 
414   /* For now we assume the mnemonic is first (there are no leading operands).
415      We can parse it without needing to set up operand parsing.
416      GAS's input scrubber will ensure mnemonics are lowercase, but we may
417      not be called from GAS.  */
418   p = CGEN_INSN_MNEMONIC (insn);
419   while (*p && TOLOWER (*p) == TOLOWER (*str))
420     ++p, ++str;
421 
422   if (* p)
423     return _("unrecognized instruction");
424 
425 #ifndef CGEN_MNEMONIC_OPERANDS
426   if (* str && ! ISSPACE (* str))
427     return _("unrecognized instruction");
428 #endif
429 
430   CGEN_INIT_PARSE (cd);
431   cgen_init_parse_operand (cd);
432 #ifdef CGEN_MNEMONIC_OPERANDS
433   past_opcode_p = 0;
434 #endif
435 
436   /* We don't check for (*str != '\0') here because we want to parse
437      any trailing fake arguments in the syntax string.  */
438   syn = CGEN_SYNTAX_STRING (syntax);
439 
440   /* Mnemonics come first for now, ensure valid string.  */
441   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
442     abort ();
443 
444   ++syn;
445 
446   while (* syn != 0)
447     {
448       /* Non operand chars must match exactly.  */
449       if (CGEN_SYNTAX_CHAR_P (* syn))
450 	{
451 	  /* FIXME: While we allow for non-GAS callers above, we assume the
452 	     first char after the mnemonic part is a space.  */
453 	  /* FIXME: We also take inappropriate advantage of the fact that
454 	     GAS's input scrubber will remove extraneous blanks.  */
455 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
456 	    {
457 #ifdef CGEN_MNEMONIC_OPERANDS
458 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
459 		past_opcode_p = 1;
460 #endif
461 	      ++ syn;
462 	      ++ str;
463 	    }
464 	  else if (*str)
465 	    {
466 	      /* Syntax char didn't match.  Can't be this insn.  */
467 	      static char msg [80];
468 
469 	      /* xgettext:c-format */
470 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
471 		       CGEN_SYNTAX_CHAR(*syn), *str);
472 	      return msg;
473 	    }
474 	  else
475 	    {
476 	      /* Ran out of input.  */
477 	      static char msg [80];
478 
479 	      /* xgettext:c-format */
480 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
481 		       CGEN_SYNTAX_CHAR(*syn));
482 	      return msg;
483 	    }
484 	  continue;
485 	}
486 
487       /* We have an operand of some sort.  */
488       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
489 					  &str, fields);
490       if (errmsg)
491 	return errmsg;
492 
493       /* Done with this operand, continue with next one.  */
494       ++ syn;
495     }
496 
497   /* If we're at the end of the syntax string, we're done.  */
498   if (* syn == 0)
499     {
500       /* FIXME: For the moment we assume a valid `str' can only contain
501 	 blanks now.  IE: We needn't try again with a longer version of
502 	 the insn and it is assumed that longer versions of insns appear
503 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
504       while (ISSPACE (* str))
505 	++ str;
506 
507       if (* str != '\0')
508 	return _("junk at end of line"); /* FIXME: would like to include `str' */
509 
510       return NULL;
511     }
512 
513   /* We couldn't parse it.  */
514   return _("unrecognized instruction");
515 }
516 
517 /* Main entry point.
518    This routine is called for each instruction to be assembled.
519    STR points to the insn to be assembled.
520    We assume all necessary tables have been initialized.
521    The assembled instruction, less any fixups, is stored in BUF.
522    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
523    still needs to be converted to target byte order, otherwise BUF is an array
524    of bytes in target byte order.
525    The result is a pointer to the insn's entry in the opcode table,
526    or NULL if an error occured (an error message will have already been
527    printed).
528 
529    Note that when processing (non-alias) macro-insns,
530    this function recurses.
531 
532    ??? It's possible to make this cpu-independent.
533    One would have to deal with a few minor things.
534    At this point in time doing so would be more of a curiosity than useful
535    [for example this file isn't _that_ big], but keeping the possibility in
536    mind helps keep the design clean.  */
537 
538 const CGEN_INSN *
openrisc_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)539 openrisc_cgen_assemble_insn (CGEN_CPU_DESC cd,
540 			   const char *str,
541 			   CGEN_FIELDS *fields,
542 			   CGEN_INSN_BYTES_PTR buf,
543 			   char **errmsg)
544 {
545   const char *start;
546   CGEN_INSN_LIST *ilist;
547   const char *parse_errmsg = NULL;
548   const char *insert_errmsg = NULL;
549   int recognized_mnemonic = 0;
550 
551   /* Skip leading white space.  */
552   while (ISSPACE (* str))
553     ++ str;
554 
555   /* The instructions are stored in hashed lists.
556      Get the first in the list.  */
557   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
558 
559   /* Keep looking until we find a match.  */
560   start = str;
561   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
562     {
563       const CGEN_INSN *insn = ilist->insn;
564       recognized_mnemonic = 1;
565 
566 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
567       /* Not usually needed as unsupported opcodes
568 	 shouldn't be in the hash lists.  */
569       /* Is this insn supported by the selected cpu?  */
570       if (! openrisc_cgen_insn_supported (cd, insn))
571 	continue;
572 #endif
573       /* If the RELAXED attribute is set, this is an insn that shouldn't be
574 	 chosen immediately.  Instead, it is used during assembler/linker
575 	 relaxation if possible.  */
576       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
577 	continue;
578 
579       str = start;
580 
581       /* Skip this insn if str doesn't look right lexically.  */
582       if (CGEN_INSN_RX (insn) != NULL &&
583 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
584 	continue;
585 
586       /* Allow parse/insert handlers to obtain length of insn.  */
587       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
588 
589       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
590       if (parse_errmsg != NULL)
591 	continue;
592 
593       /* ??? 0 is passed for `pc'.  */
594       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
595 						 (bfd_vma) 0);
596       if (insert_errmsg != NULL)
597         continue;
598 
599       /* It is up to the caller to actually output the insn and any
600          queued relocs.  */
601       return insn;
602     }
603 
604   {
605     static char errbuf[150];
606 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
607     const char *tmp_errmsg;
608 
609     /* If requesting verbose error messages, use insert_errmsg.
610        Failing that, use parse_errmsg.  */
611     tmp_errmsg = (insert_errmsg ? insert_errmsg :
612 		  parse_errmsg ? parse_errmsg :
613 		  recognized_mnemonic ?
614 		  _("unrecognized form of instruction") :
615 		  _("unrecognized instruction"));
616 
617     if (strlen (start) > 50)
618       /* xgettext:c-format */
619       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
620     else
621       /* xgettext:c-format */
622       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
623 #else
624     if (strlen (start) > 50)
625       /* xgettext:c-format */
626       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
627     else
628       /* xgettext:c-format */
629       sprintf (errbuf, _("bad instruction `%.50s'"), start);
630 #endif
631 
632     *errmsg = errbuf;
633     return NULL;
634   }
635 }
636