1 /* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */
2 /* Assembler interface for targets using CGEN. -*- C -*-
3 CGEN: Cpu tools GENerator
5 THIS FILE IS MACHINE GENERATED WITH CGEN.
6 - the resultant file is machine generated, cgen-asm.in isn't
8 Copyright (C) 1996-2019 Free Software Foundation, Inc.
10 This file is part of libopcodes.
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)
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.
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. */
27 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
39 #include "libiberty.h"
40 #include "safe-ctype.h"
43 #define min(a,b) ((a) < (b) ? (a) : (b))
45 #define max(a,b) ((a) > (b) ? (a) : (b))
47 static const char * parse_insn_normal
48 (CGEN_CPU_DESC
, const CGEN_INSN
*, const char **, CGEN_FIELDS
*);
50 /* -- assembler routines inserted here. */
54 /* Parse a signed 64-bit immediate. */
57 parse_imm64 (CGEN_CPU_DESC cd
,
63 enum cgen_parse_operand_result result
;
66 errmsg
= (* cd
->parse_operand_fn
)
67 (cd
, CGEN_PARSE_OPERAND_INTEGER
, strp
, opindex
, BFD_RELOC_NONE
,
75 /* Endianness size operands are integer immediates whose values can be
79 parse_endsize (CGEN_CPU_DESC cd
,
82 unsigned long *valuep
)
86 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
97 return _("expected 16, 32 or 64 in");
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
109 bpf_cgen_insn_supported (CGEN_CPU_DESC cd
, const CGEN_INSN
*insn
)
111 CGEN_BITSET isas
= CGEN_INSN_BITSET_ATTR_VALUE (insn
, CGEN_INSN_ISA
);
113 return cgen_bitset_intersect_p (&isas
, cd
->isas
);
119 const char * bpf_cgen_parse_operand
120 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
122 /* Main entry point for operand parsing.
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.
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
136 bpf_cgen_parse_operand (CGEN_CPU_DESC cd
,
139 CGEN_FIELDS
* fields
)
141 const char * errmsg
= NULL
;
142 /* Used by scalar operands that still need to be parsed. */
143 long junk ATTRIBUTE_UNUSED
;
147 case BPF_OPERAND_DISP16
:
148 errmsg
= cgen_parse_signed_integer (cd
, strp
, BPF_OPERAND_DISP16
, (long *) (& fields
->f_offset16
));
150 case BPF_OPERAND_DISP32
:
151 errmsg
= cgen_parse_signed_integer (cd
, strp
, BPF_OPERAND_DISP32
, (long *) (& fields
->f_imm32
));
153 case BPF_OPERAND_DSTBE
:
154 errmsg
= cgen_parse_keyword (cd
, strp
, & bpf_cgen_opval_h_gpr
, & fields
->f_dstbe
);
156 case BPF_OPERAND_DSTLE
:
157 errmsg
= cgen_parse_keyword (cd
, strp
, & bpf_cgen_opval_h_gpr
, & fields
->f_dstle
);
159 case BPF_OPERAND_ENDSIZE
:
160 errmsg
= parse_endsize (cd
, strp
, BPF_OPERAND_ENDSIZE
, (unsigned long *) (& fields
->f_imm32
));
162 case BPF_OPERAND_IMM32
:
163 errmsg
= cgen_parse_signed_integer (cd
, strp
, BPF_OPERAND_IMM32
, (long *) (& fields
->f_imm32
));
165 case BPF_OPERAND_IMM64
:
166 errmsg
= parse_imm64 (cd
, strp
, BPF_OPERAND_IMM64
, (int64_t *) (& fields
->f_imm64
));
168 case BPF_OPERAND_OFFSET16
:
169 errmsg
= cgen_parse_signed_integer (cd
, strp
, BPF_OPERAND_OFFSET16
, (long *) (& fields
->f_offset16
));
171 case BPF_OPERAND_SRCBE
:
172 errmsg
= cgen_parse_keyword (cd
, strp
, & bpf_cgen_opval_h_gpr
, & fields
->f_srcbe
);
174 case BPF_OPERAND_SRCLE
:
175 errmsg
= cgen_parse_keyword (cd
, strp
, & bpf_cgen_opval_h_gpr
, & fields
->f_srcle
);
179 /* xgettext:c-format */
180 opcodes_error_handler
181 (_("internal error: unrecognized field %d while parsing"),
189 cgen_parse_fn
* const bpf_cgen_parse_handlers
[] =
195 bpf_cgen_init_asm (CGEN_CPU_DESC cd
)
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
208 /* Regex construction routine.
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 '.*'
214 It then compiles the regex and stores it in the opcode, for
215 later use by bpf_cgen_assemble_insn
217 Returns NULL for success, an error message for failure. */
220 bpf_cgen_build_insn_regex (CGEN_INSN
*insn
)
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
];
226 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
229 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
231 /* Mnemonics come first in the syntax string. */
232 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
233 return _("missing mnemonic in syntax string");
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. */
241 /* Copy the literal mnemonic out of the insn. */
242 for (; *mnem
; mnem
++)
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
)
260 if (CGEN_SYNTAX_CHAR_P (* syn
))
262 char c
= CGEN_SYNTAX_CHAR (* syn
);
266 /* Escape any regex metacharacters in the syntax. */
267 case '.': case '[': case '\\':
268 case '*': case '^': case '$':
270 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
271 case '?': case '{': case '}':
272 case '(': case ')': case '*':
273 case '|': case '+': case ']':
294 /* Replace non-syntax fields with globs. */
300 /* Trailing whitespace ok. */
307 /* But anchor it after that. */
311 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
312 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
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
;
329 /* Default insn parser.
331 The syntax string is scanned and operands are parsed and stored in FIELDS.
332 Relocs are queued as we go via other callbacks.
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.
340 Returns NULL for success, an error message for failure. */
343 parse_insn_normal (CGEN_CPU_DESC cd
,
344 const CGEN_INSN
*insn
,
348 /* ??? Runtime added insns not handled yet. */
349 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
350 const char *str
= *strp
;
353 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
354 #ifdef CGEN_MNEMONIC_OPERANDS
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
))
368 return _("unrecognized instruction");
370 #ifndef CGEN_MNEMONIC_OPERANDS
371 if (* str
&& ! ISSPACE (* str
))
372 return _("unrecognized instruction");
375 CGEN_INIT_PARSE (cd
);
376 cgen_init_parse_operand (cd
);
377 #ifdef CGEN_MNEMONIC_OPERANDS
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
);
385 /* Mnemonics come first for now, ensure valid string. */
386 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
393 /* Non operand chars must match exactly. */
394 if (CGEN_SYNTAX_CHAR_P (* syn
))
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
)))
402 #ifdef CGEN_MNEMONIC_OPERANDS
403 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
411 /* Syntax char didn't match. Can't be this insn. */
412 static char msg
[80];
414 /* xgettext:c-format */
415 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
416 CGEN_SYNTAX_CHAR(*syn
), *str
);
421 /* Ran out of input. */
422 static char msg
[80];
424 /* xgettext:c-format */
425 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
426 CGEN_SYNTAX_CHAR(*syn
));
432 #ifdef CGEN_MNEMONIC_OPERANDS
433 (void) past_opcode_p
;
435 /* We have an operand of some sort. */
436 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
), &str
, fields
);
440 /* Done with this operand, continue with next one. */
444 /* If we're at the end of the syntax string, we're done. */
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
))
455 return _("junk at end of line"); /* FIXME: would like to include `str' */
460 /* We couldn't parse it. */
461 return _("unrecognized instruction");
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
476 Note that when processing (non-alias) macro-insns,
477 this function recurses.
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. */
486 bpf_cgen_assemble_insn (CGEN_CPU_DESC cd
,
489 CGEN_INSN_BYTES_PTR buf
,
493 CGEN_INSN_LIST
*ilist
;
494 const char *parse_errmsg
= NULL
;
495 const char *insert_errmsg
= NULL
;
496 int recognized_mnemonic
= 0;
498 /* Skip leading white space. */
499 while (ISSPACE (* str
))
502 /* The instructions are stored in hashed lists.
503 Get the first in the list. */
504 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
506 /* Keep looking until we find a match. */
508 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
510 const CGEN_INSN
*insn
= ilist
->insn
;
511 recognized_mnemonic
= 1;
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
))
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)
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
)
533 /* Allow parse/insert handlers to obtain length of insn. */
534 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
536 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
537 if (parse_errmsg
!= NULL
)
540 /* ??? 0 is passed for `pc'. */
541 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
543 if (insert_errmsg
!= NULL
)
546 /* It is up to the caller to actually output the insn and any
552 static char errbuf
[150];
553 const char *tmp_errmsg
;
554 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
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"));
570 if (strlen (start
) > 50)
571 /* xgettext:c-format */
572 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
574 /* xgettext:c-format */
575 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
579 if (strlen (start
) > 50)
580 /* xgettext:c-format */
581 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
583 /* xgettext:c-format */
584 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);