1 /* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */
2 /* Assembler interface for targets using CGEN. -*- C -*-
3    CGEN: Cpu tools GENerator
4 
5    THIS FILE IS MACHINE GENERATED WITH CGEN.
6    - the resultant file is machine generated, cgen-asm.in isn't
7 
8    Copyright (C) 1996-2021 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 "bpf-desc.h"
36 #include "bpf-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 /* Parse a signed 64-bit immediate.  */
55 
56 static const char *
parse_imm64(CGEN_CPU_DESC cd,const char ** strp,int opindex,int64_t * valuep)57 parse_imm64 (CGEN_CPU_DESC cd,
58              const char **strp,
59              int opindex,
60              int64_t *valuep)
61 {
62   bfd_vma value;
63   enum cgen_parse_operand_result result;
64   const char *errmsg;
65 
66   errmsg = (* cd->parse_operand_fn)
67     (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
68      &result, &value);
69   if (!errmsg)
70     *valuep = value;
71 
72   return errmsg;
73 }
74 
75 /* Endianness size operands are integer immediates whose values can be
76    16, 32 or 64.  */
77 
78 static const char *
parse_endsize(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)79 parse_endsize (CGEN_CPU_DESC cd,
80                const char **strp,
81                int opindex,
82                unsigned long *valuep)
83 {
84   const char *errmsg;
85 
86   errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
87   if (errmsg)
88     return errmsg;
89 
90   switch (*valuep)
91     {
92     case 16:
93     case 32:
94     case 64:
95       break;
96     default:
97       return _("expected 16, 32 or 64 in");
98     }
99 
100   return NULL;
101 }
102 
103 /* Special check to ensure that the right instruction variant is used
104    for the given endianness induced by the ISA selected in the CPU.
105    See bpf.cpu for a discussion on how eBPF is really two instruction
106    sets.  */
107 
108 int
bpf_cgen_insn_supported(CGEN_CPU_DESC cd,const CGEN_INSN * insn)109 bpf_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
110 {
111   CGEN_BITSET isas = CGEN_INSN_BITSET_ATTR_VALUE (insn, CGEN_INSN_ISA);
112 
113   return cgen_bitset_intersect_p (&isas, cd->isas);
114 }
115 
116 
117 /* -- dis.c */
118 
119 const char * bpf_cgen_parse_operand
120   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
121 
122 /* Main entry point for operand parsing.
123 
124    This function is basically just a big switch statement.  Earlier versions
125    used tables to look up the function to use, but
126    - if the table contains both assembler and disassembler functions then
127      the disassembler contains much of the assembler and vice-versa,
128    - there's a lot of inlining possibilities as things grow,
129    - using a switch statement avoids the function call overhead.
130 
131    This function could be moved into `parse_insn_normal', but keeping it
132    separate makes clear the interface between `parse_insn_normal' and each of
133    the handlers.  */
134 
135 const char *
bpf_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)136 bpf_cgen_parse_operand (CGEN_CPU_DESC cd,
137 			   int opindex,
138 			   const char ** strp,
139 			   CGEN_FIELDS * fields)
140 {
141   const char * errmsg = NULL;
142   /* Used by scalar operands that still need to be parsed.  */
143   long junk ATTRIBUTE_UNUSED;
144 
145   switch (opindex)
146     {
147     case BPF_OPERAND_DISP16 :
148       errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_DISP16, (long *) (& fields->f_offset16));
149       break;
150     case BPF_OPERAND_DISP32 :
151       errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_DISP32, (long *) (& fields->f_imm32));
152       break;
153     case BPF_OPERAND_DSTBE :
154       errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_dstbe);
155       break;
156     case BPF_OPERAND_DSTLE :
157       errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_dstle);
158       break;
159     case BPF_OPERAND_ENDSIZE :
160       errmsg = parse_endsize (cd, strp, BPF_OPERAND_ENDSIZE, (unsigned long *) (& fields->f_imm32));
161       break;
162     case BPF_OPERAND_IMM32 :
163       errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_IMM32, (long *) (& fields->f_imm32));
164       break;
165     case BPF_OPERAND_IMM64 :
166       errmsg = parse_imm64 (cd, strp, BPF_OPERAND_IMM64, (int64_t *) (& fields->f_imm64));
167       break;
168     case BPF_OPERAND_OFFSET16 :
169       errmsg = cgen_parse_signed_integer (cd, strp, BPF_OPERAND_OFFSET16, (long *) (& fields->f_offset16));
170       break;
171     case BPF_OPERAND_SRCBE :
172       errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_srcbe);
173       break;
174     case BPF_OPERAND_SRCLE :
175       errmsg = cgen_parse_keyword (cd, strp, & bpf_cgen_opval_h_gpr, & fields->f_srcle);
176       break;
177 
178     default :
179       /* xgettext:c-format */
180       opcodes_error_handler
181 	(_("internal error: unrecognized field %d while parsing"),
182 	 opindex);
183       abort ();
184   }
185 
186   return errmsg;
187 }
188 
189 cgen_parse_fn * const bpf_cgen_parse_handlers[] =
190 {
191   parse_insn_normal,
192 };
193 
194 void
bpf_cgen_init_asm(CGEN_CPU_DESC cd)195 bpf_cgen_init_asm (CGEN_CPU_DESC cd)
196 {
197   bpf_cgen_init_opcode_table (cd);
198   bpf_cgen_init_ibld_table (cd);
199   cd->parse_handlers = & bpf_cgen_parse_handlers[0];
200   cd->parse_operand = bpf_cgen_parse_operand;
201 #ifdef CGEN_ASM_INIT_HOOK
202 CGEN_ASM_INIT_HOOK
203 #endif
204 }
205 
206 
207 
208 /* Regex construction routine.
209 
210    This translates an opcode syntax string into a regex string,
211    by replacing any non-character syntax element (such as an
212    opcode) with the pattern '.*'
213 
214    It then compiles the regex and stores it in the opcode, for
215    later use by bpf_cgen_assemble_insn
216 
217    Returns NULL for success, an error message for failure.  */
218 
219 char *
bpf_cgen_build_insn_regex(CGEN_INSN * insn)220 bpf_cgen_build_insn_regex (CGEN_INSN *insn)
221 {
222   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
223   const char *mnem = CGEN_INSN_MNEMONIC (insn);
224   char rxbuf[CGEN_MAX_RX_ELEMENTS];
225   char *rx = rxbuf;
226   const CGEN_SYNTAX_CHAR_TYPE *syn;
227   int reg_err;
228 
229   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
230 
231   /* Mnemonics come first in the syntax string.  */
232   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
233     return _("missing mnemonic in syntax string");
234   ++syn;
235 
236   /* Generate a case sensitive regular expression that emulates case
237      insensitive matching in the "C" locale.  We cannot generate a case
238      insensitive regular expression because in Turkish locales, 'i' and 'I'
239      are not equal modulo case conversion.  */
240 
241   /* Copy the literal mnemonic out of the insn.  */
242   for (; *mnem; mnem++)
243     {
244       char c = *mnem;
245 
246       if (ISALPHA (c))
247 	{
248 	  *rx++ = '[';
249 	  *rx++ = TOLOWER (c);
250 	  *rx++ = TOUPPER (c);
251 	  *rx++ = ']';
252 	}
253       else
254 	*rx++ = c;
255     }
256 
257   /* Copy any remaining literals from the syntax string into the rx.  */
258   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
259     {
260       if (CGEN_SYNTAX_CHAR_P (* syn))
261 	{
262 	  char c = CGEN_SYNTAX_CHAR (* syn);
263 
264 	  switch (c)
265 	    {
266 	      /* Escape any regex metacharacters in the syntax.  */
267 	    case '.': case '[': case '\\':
268 	    case '*': case '^': case '$':
269 
270 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
271 	    case '?': case '{': case '}':
272 	    case '(': case ')': case '*':
273 	    case '|': case '+': case ']':
274 #endif
275 	      *rx++ = '\\';
276 	      *rx++ = c;
277 	      break;
278 
279 	    default:
280 	      if (ISALPHA (c))
281 		{
282 		  *rx++ = '[';
283 		  *rx++ = TOLOWER (c);
284 		  *rx++ = TOUPPER (c);
285 		  *rx++ = ']';
286 		}
287 	      else
288 		*rx++ = c;
289 	      break;
290 	    }
291 	}
292       else
293 	{
294 	  /* Replace non-syntax fields with globs.  */
295 	  *rx++ = '.';
296 	  *rx++ = '*';
297 	}
298     }
299 
300   /* Trailing whitespace ok.  */
301   * rx++ = '[';
302   * rx++ = ' ';
303   * rx++ = '\t';
304   * rx++ = ']';
305   * rx++ = '*';
306 
307   /* But anchor it after that.  */
308   * rx++ = '$';
309   * rx = '\0';
310 
311   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
312   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
313 
314   if (reg_err == 0)
315     return NULL;
316   else
317     {
318       static char msg[80];
319 
320       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
321       regfree ((regex_t *) CGEN_INSN_RX (insn));
322       free (CGEN_INSN_RX (insn));
323       (CGEN_INSN_RX (insn)) = NULL;
324       return msg;
325     }
326 }
327 
328 
329 /* Default insn parser.
330 
331    The syntax string is scanned and operands are parsed and stored in FIELDS.
332    Relocs are queued as we go via other callbacks.
333 
334    ??? Note that this is currently an all-or-nothing parser.  If we fail to
335    parse the instruction, we return 0 and the caller will start over from
336    the beginning.  Backtracking will be necessary in parsing subexpressions,
337    but that can be handled there.  Not handling backtracking here may get
338    expensive in the case of the m68k.  Deal with later.
339 
340    Returns NULL for success, an error message for failure.  */
341 
342 static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)343 parse_insn_normal (CGEN_CPU_DESC cd,
344 		   const CGEN_INSN *insn,
345 		   const char **strp,
346 		   CGEN_FIELDS *fields)
347 {
348   /* ??? Runtime added insns not handled yet.  */
349   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
350   const char *str = *strp;
351   const char *errmsg;
352   const char *p;
353   const CGEN_SYNTAX_CHAR_TYPE * syn;
354 #ifdef CGEN_MNEMONIC_OPERANDS
355   /* FIXME: wip */
356   int past_opcode_p;
357 #endif
358 
359   /* For now we assume the mnemonic is first (there are no leading operands).
360      We can parse it without needing to set up operand parsing.
361      GAS's input scrubber will ensure mnemonics are lowercase, but we may
362      not be called from GAS.  */
363   p = CGEN_INSN_MNEMONIC (insn);
364   while (*p && TOLOWER (*p) == TOLOWER (*str))
365     ++p, ++str;
366 
367   if (* p)
368     return _("unrecognized instruction");
369 
370 #ifndef CGEN_MNEMONIC_OPERANDS
371   if (* str && ! ISSPACE (* str))
372     return _("unrecognized instruction");
373 #endif
374 
375   CGEN_INIT_PARSE (cd);
376   cgen_init_parse_operand (cd);
377 #ifdef CGEN_MNEMONIC_OPERANDS
378   past_opcode_p = 0;
379 #endif
380 
381   /* We don't check for (*str != '\0') here because we want to parse
382      any trailing fake arguments in the syntax string.  */
383   syn = CGEN_SYNTAX_STRING (syntax);
384 
385   /* Mnemonics come first for now, ensure valid string.  */
386   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
387     abort ();
388 
389   ++syn;
390 
391   while (* syn != 0)
392     {
393       /* Non operand chars must match exactly.  */
394       if (CGEN_SYNTAX_CHAR_P (* syn))
395 	{
396 	  /* FIXME: While we allow for non-GAS callers above, we assume the
397 	     first char after the mnemonic part is a space.  */
398 	  /* FIXME: We also take inappropriate advantage of the fact that
399 	     GAS's input scrubber will remove extraneous blanks.  */
400 	  if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
401 	    {
402 #ifdef CGEN_MNEMONIC_OPERANDS
403 	      if (CGEN_SYNTAX_CHAR(* syn) == ' ')
404 		past_opcode_p = 1;
405 #endif
406 	      ++ syn;
407 	      ++ str;
408 	    }
409 	  else if (*str)
410 	    {
411 	      /* Syntax char didn't match.  Can't be this insn.  */
412 	      static char msg [80];
413 
414 	      /* xgettext:c-format */
415 	      sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
416 		       CGEN_SYNTAX_CHAR(*syn), *str);
417 	      return msg;
418 	    }
419 	  else
420 	    {
421 	      /* Ran out of input.  */
422 	      static char msg [80];
423 
424 	      /* xgettext:c-format */
425 	      sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
426 		       CGEN_SYNTAX_CHAR(*syn));
427 	      return msg;
428 	    }
429 	  continue;
430 	}
431 
432 #ifdef CGEN_MNEMONIC_OPERANDS
433       (void) past_opcode_p;
434 #endif
435       /* We have an operand of some sort.  */
436       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
437       if (errmsg)
438 	return errmsg;
439 
440       /* Done with this operand, continue with next one.  */
441       ++ syn;
442     }
443 
444   /* If we're at the end of the syntax string, we're done.  */
445   if (* syn == 0)
446     {
447       /* FIXME: For the moment we assume a valid `str' can only contain
448 	 blanks now.  IE: We needn't try again with a longer version of
449 	 the insn and it is assumed that longer versions of insns appear
450 	 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
451       while (ISSPACE (* str))
452 	++ str;
453 
454       if (* str != '\0')
455 	return _("junk at end of line"); /* FIXME: would like to include `str' */
456 
457       return NULL;
458     }
459 
460   /* We couldn't parse it.  */
461   return _("unrecognized instruction");
462 }
463 
464 /* Main entry point.
465    This routine is called for each instruction to be assembled.
466    STR points to the insn to be assembled.
467    We assume all necessary tables have been initialized.
468    The assembled instruction, less any fixups, is stored in BUF.
469    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
470    still needs to be converted to target byte order, otherwise BUF is an array
471    of bytes in target byte order.
472    The result is a pointer to the insn's entry in the opcode table,
473    or NULL if an error occured (an error message will have already been
474    printed).
475 
476    Note that when processing (non-alias) macro-insns,
477    this function recurses.
478 
479    ??? It's possible to make this cpu-independent.
480    One would have to deal with a few minor things.
481    At this point in time doing so would be more of a curiosity than useful
482    [for example this file isn't _that_ big], but keeping the possibility in
483    mind helps keep the design clean.  */
484 
485 const CGEN_INSN *
bpf_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)486 bpf_cgen_assemble_insn (CGEN_CPU_DESC cd,
487 			   const char *str,
488 			   CGEN_FIELDS *fields,
489 			   CGEN_INSN_BYTES_PTR buf,
490 			   char **errmsg)
491 {
492   const char *start;
493   CGEN_INSN_LIST *ilist;
494   const char *parse_errmsg = NULL;
495   const char *insert_errmsg = NULL;
496   int recognized_mnemonic = 0;
497 
498   /* Skip leading white space.  */
499   while (ISSPACE (* str))
500     ++ str;
501 
502   /* The instructions are stored in hashed lists.
503      Get the first in the list.  */
504   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
505 
506   /* Keep looking until we find a match.  */
507   start = str;
508   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
509     {
510       const CGEN_INSN *insn = ilist->insn;
511       recognized_mnemonic = 1;
512 
513 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
514       /* Not usually needed as unsupported opcodes
515 	 shouldn't be in the hash lists.  */
516       /* Is this insn supported by the selected cpu?  */
517       if (! bpf_cgen_insn_supported (cd, insn))
518 	continue;
519 #endif
520       /* If the RELAXED attribute is set, this is an insn that shouldn't be
521 	 chosen immediately.  Instead, it is used during assembler/linker
522 	 relaxation if possible.  */
523       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
524 	continue;
525 
526       str = start;
527 
528       /* Skip this insn if str doesn't look right lexically.  */
529       if (CGEN_INSN_RX (insn) != NULL &&
530 	  regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
531 	continue;
532 
533       /* Allow parse/insert handlers to obtain length of insn.  */
534       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
535 
536       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
537       if (parse_errmsg != NULL)
538 	continue;
539 
540       /* ??? 0 is passed for `pc'.  */
541       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
542 						 (bfd_vma) 0);
543       if (insert_errmsg != NULL)
544         continue;
545 
546       /* It is up to the caller to actually output the insn and any
547          queued relocs.  */
548       return insn;
549     }
550 
551   {
552     static char errbuf[150];
553     const char *tmp_errmsg;
554 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
555 #define be_verbose 1
556 #else
557 #define be_verbose 0
558 #endif
559 
560     if (be_verbose)
561       {
562 	/* If requesting verbose error messages, use insert_errmsg.
563 	   Failing that, use parse_errmsg.  */
564 	tmp_errmsg = (insert_errmsg ? insert_errmsg :
565 		      parse_errmsg ? parse_errmsg :
566 		      recognized_mnemonic ?
567 		      _("unrecognized form of instruction") :
568 		      _("unrecognized instruction"));
569 
570 	if (strlen (start) > 50)
571 	  /* xgettext:c-format */
572 	  sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
573 	else
574 	  /* xgettext:c-format */
575 	  sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
576       }
577     else
578       {
579 	if (strlen (start) > 50)
580 	  /* xgettext:c-format */
581 	  sprintf (errbuf, _("bad instruction `%.50s...'"), start);
582 	else
583 	  /* xgettext:c-format */
584 	  sprintf (errbuf, _("bad instruction `%.50s'"), start);
585       }
586 
587     *errmsg = errbuf;
588     return NULL;
589   }
590 }
591