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-2018 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.
35 #include "lm32-desc.h"
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 /* Handle signed/unsigned literal. */
57 parse_imm (CGEN_CPU_DESC cd,
60 unsigned long *valuep)
65 errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
68 unsigned long x = value & 0xFFFF0000;
69 if (x != 0 && x != 0xFFFF0000)
70 errmsg = _("immediate value out of range");
72 *valuep = (value & 0xFFFF);
80 parse_hi16 (CGEN_CPU_DESC cd,
83 unsigned long *valuep)
85 if (strncasecmp (*strp, "hi(", 3) == 0)
87 enum cgen_parse_operand_result result_type;
92 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
93 &result_type, &value);
95 return _("missing `)'");
99 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
100 value = (value >> 16) & 0xffff;
106 return parse_imm (cd, strp, opindex, valuep);
112 parse_lo16 (CGEN_CPU_DESC cd,
115 unsigned long *valuep)
117 if (strncasecmp (*strp, "lo(", 3) == 0)
120 enum cgen_parse_operand_result result_type;
124 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
125 &result_type, &value);
127 return _("missing `)'");
130 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
136 return parse_imm (cd, strp, opindex, valuep);
142 parse_gp16 (CGEN_CPU_DESC cd,
147 if (strncasecmp (*strp, "gp(", 3) == 0)
150 enum cgen_parse_operand_result result_type;
154 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_GPREL16,
155 & result_type, & value);
157 return _("missing `)'");
160 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
166 return _("expecting gp relative address: gp(symbol)");
172 parse_got16 (CGEN_CPU_DESC cd,
177 if (strncasecmp (*strp, "got(", 4) == 0)
180 enum cgen_parse_operand_result result_type;
184 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_16_GOT,
185 & result_type, & value);
187 return _("missing `)'");
190 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
196 return _("expecting got relative address: got(symbol)");
199 /* Handle gotoffhi16() */
202 parse_gotoff_hi16 (CGEN_CPU_DESC cd,
207 if (strncasecmp (*strp, "gotoffhi16(", 11) == 0)
210 enum cgen_parse_operand_result result_type;
214 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_HI16,
215 & result_type, & value);
217 return _("missing `)'");
220 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
226 return _("expecting got relative address: gotoffhi16(symbol)");
229 /* Handle gotofflo16() */
232 parse_gotoff_lo16 (CGEN_CPU_DESC cd,
237 if (strncasecmp (*strp, "gotofflo16(", 11) == 0)
240 enum cgen_parse_operand_result result_type;
244 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_LO16,
245 &result_type, &value);
247 return _("missing `)'");
250 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
256 return _("expecting got relative address: gotofflo16(symbol)");
259 const char * lm32_cgen_parse_operand
260 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
262 /* Main entry point for operand parsing.
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.
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
276 lm32_cgen_parse_operand (CGEN_CPU_DESC cd,
279 CGEN_FIELDS * fields)
281 const char * errmsg = NULL;
282 /* Used by scalar operands that still need to be parsed. */
283 long junk ATTRIBUTE_UNUSED;
287 case LM32_OPERAND_BRANCH :
290 errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_BRANCH, 0, NULL, & value);
291 fields->f_branch = value;
294 case LM32_OPERAND_CALL :
297 errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_CALL, 0, NULL, & value);
298 fields->f_call = value;
301 case LM32_OPERAND_CSR :
302 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_csr, & fields->f_csr);
304 case LM32_OPERAND_EXCEPTION :
305 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_EXCEPTION, (unsigned long *) (& fields->f_exception));
307 case LM32_OPERAND_GOT16 :
308 errmsg = parse_got16 (cd, strp, LM32_OPERAND_GOT16, (long *) (& fields->f_imm));
310 case LM32_OPERAND_GOTOFFHI16 :
311 errmsg = parse_gotoff_hi16 (cd, strp, LM32_OPERAND_GOTOFFHI16, (long *) (& fields->f_imm));
313 case LM32_OPERAND_GOTOFFLO16 :
314 errmsg = parse_gotoff_lo16 (cd, strp, LM32_OPERAND_GOTOFFLO16, (long *) (& fields->f_imm));
316 case LM32_OPERAND_GP16 :
317 errmsg = parse_gp16 (cd, strp, LM32_OPERAND_GP16, (long *) (& fields->f_imm));
319 case LM32_OPERAND_HI16 :
320 errmsg = parse_hi16 (cd, strp, LM32_OPERAND_HI16, (unsigned long *) (& fields->f_uimm));
322 case LM32_OPERAND_IMM :
323 errmsg = cgen_parse_signed_integer (cd, strp, LM32_OPERAND_IMM, (long *) (& fields->f_imm));
325 case LM32_OPERAND_LO16 :
326 errmsg = parse_lo16 (cd, strp, LM32_OPERAND_LO16, (unsigned long *) (& fields->f_uimm));
328 case LM32_OPERAND_R0 :
329 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r0);
331 case LM32_OPERAND_R1 :
332 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r1);
334 case LM32_OPERAND_R2 :
335 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r2);
337 case LM32_OPERAND_SHIFT :
338 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_SHIFT, (unsigned long *) (& fields->f_shift));
340 case LM32_OPERAND_UIMM :
341 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_UIMM, (unsigned long *) (& fields->f_uimm));
343 case LM32_OPERAND_USER :
344 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_USER, (unsigned long *) (& fields->f_user));
348 /* xgettext:c-format */
349 opcodes_error_handler
350 (_("internal error: unrecognized field %d while parsing"),
358 cgen_parse_fn * const lm32_cgen_parse_handlers[] =
364 lm32_cgen_init_asm (CGEN_CPU_DESC cd)
366 lm32_cgen_init_opcode_table (cd);
367 lm32_cgen_init_ibld_table (cd);
368 cd->parse_handlers = & lm32_cgen_parse_handlers[0];
369 cd->parse_operand = lm32_cgen_parse_operand;
370 #ifdef CGEN_ASM_INIT_HOOK
377 /* Regex construction routine.
379 This translates an opcode syntax string into a regex string,
380 by replacing any non-character syntax element (such as an
381 opcode) with the pattern '.*'
383 It then compiles the regex and stores it in the opcode, for
384 later use by lm32_cgen_assemble_insn
386 Returns NULL for success, an error message for failure. */
389 lm32_cgen_build_insn_regex (CGEN_INSN *insn)
391 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
392 const char *mnem = CGEN_INSN_MNEMONIC (insn);
393 char rxbuf[CGEN_MAX_RX_ELEMENTS];
395 const CGEN_SYNTAX_CHAR_TYPE *syn;
398 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
400 /* Mnemonics come first in the syntax string. */
401 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
402 return _("missing mnemonic in syntax string");
405 /* Generate a case sensitive regular expression that emulates case
406 insensitive matching in the "C" locale. We cannot generate a case
407 insensitive regular expression because in Turkish locales, 'i' and 'I'
408 are not equal modulo case conversion. */
410 /* Copy the literal mnemonic out of the insn. */
411 for (; *mnem; mnem++)
426 /* Copy any remaining literals from the syntax string into the rx. */
427 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
429 if (CGEN_SYNTAX_CHAR_P (* syn))
431 char c = CGEN_SYNTAX_CHAR (* syn);
435 /* Escape any regex metacharacters in the syntax. */
436 case '.': case '[': case '\\':
437 case '*': case '^': case '$':
439 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
440 case '?': case '{': case '}':
441 case '(': case ')': case '*':
442 case '|': case '+': case ']':
463 /* Replace non-syntax fields with globs. */
469 /* Trailing whitespace ok. */
476 /* But anchor it after that. */
480 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
481 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
489 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
490 regfree ((regex_t *) CGEN_INSN_RX (insn));
491 free (CGEN_INSN_RX (insn));
492 (CGEN_INSN_RX (insn)) = NULL;
498 /* Default insn parser.
500 The syntax string is scanned and operands are parsed and stored in FIELDS.
501 Relocs are queued as we go via other callbacks.
503 ??? Note that this is currently an all-or-nothing parser. If we fail to
504 parse the instruction, we return 0 and the caller will start over from
505 the beginning. Backtracking will be necessary in parsing subexpressions,
506 but that can be handled there. Not handling backtracking here may get
507 expensive in the case of the m68k. Deal with later.
509 Returns NULL for success, an error message for failure. */
512 parse_insn_normal (CGEN_CPU_DESC cd,
513 const CGEN_INSN *insn,
517 /* ??? Runtime added insns not handled yet. */
518 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
519 const char *str = *strp;
522 const CGEN_SYNTAX_CHAR_TYPE * syn;
523 #ifdef CGEN_MNEMONIC_OPERANDS
528 /* For now we assume the mnemonic is first (there are no leading operands).
529 We can parse it without needing to set up operand parsing.
530 GAS's input scrubber will ensure mnemonics are lowercase, but we may
531 not be called from GAS. */
532 p = CGEN_INSN_MNEMONIC (insn);
533 while (*p && TOLOWER (*p) == TOLOWER (*str))
537 return _("unrecognized instruction");
539 #ifndef CGEN_MNEMONIC_OPERANDS
540 if (* str && ! ISSPACE (* str))
541 return _("unrecognized instruction");
544 CGEN_INIT_PARSE (cd);
545 cgen_init_parse_operand (cd);
546 #ifdef CGEN_MNEMONIC_OPERANDS
550 /* We don't check for (*str != '\0') here because we want to parse
551 any trailing fake arguments in the syntax string. */
552 syn = CGEN_SYNTAX_STRING (syntax);
554 /* Mnemonics come first for now, ensure valid string. */
555 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
562 /* Non operand chars must match exactly. */
563 if (CGEN_SYNTAX_CHAR_P (* syn))
565 /* FIXME: While we allow for non-GAS callers above, we assume the
566 first char after the mnemonic part is a space. */
567 /* FIXME: We also take inappropriate advantage of the fact that
568 GAS's input scrubber will remove extraneous blanks. */
569 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
571 #ifdef CGEN_MNEMONIC_OPERANDS
572 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
580 /* Syntax char didn't match. Can't be this insn. */
581 static char msg [80];
583 /* xgettext:c-format */
584 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
585 CGEN_SYNTAX_CHAR(*syn), *str);
590 /* Ran out of input. */
591 static char msg [80];
593 /* xgettext:c-format */
594 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
595 CGEN_SYNTAX_CHAR(*syn));
601 #ifdef CGEN_MNEMONIC_OPERANDS
602 (void) past_opcode_p;
604 /* We have an operand of some sort. */
605 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
609 /* Done with this operand, continue with next one. */
613 /* If we're at the end of the syntax string, we're done. */
616 /* FIXME: For the moment we assume a valid `str' can only contain
617 blanks now. IE: We needn't try again with a longer version of
618 the insn and it is assumed that longer versions of insns appear
619 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
620 while (ISSPACE (* str))
624 return _("junk at end of line"); /* FIXME: would like to include `str' */
629 /* We couldn't parse it. */
630 return _("unrecognized instruction");
634 This routine is called for each instruction to be assembled.
635 STR points to the insn to be assembled.
636 We assume all necessary tables have been initialized.
637 The assembled instruction, less any fixups, is stored in BUF.
638 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
639 still needs to be converted to target byte order, otherwise BUF is an array
640 of bytes in target byte order.
641 The result is a pointer to the insn's entry in the opcode table,
642 or NULL if an error occured (an error message will have already been
645 Note that when processing (non-alias) macro-insns,
646 this function recurses.
648 ??? It's possible to make this cpu-independent.
649 One would have to deal with a few minor things.
650 At this point in time doing so would be more of a curiosity than useful
651 [for example this file isn't _that_ big], but keeping the possibility in
652 mind helps keep the design clean. */
655 lm32_cgen_assemble_insn (CGEN_CPU_DESC cd,
658 CGEN_INSN_BYTES_PTR buf,
662 CGEN_INSN_LIST *ilist;
663 const char *parse_errmsg = NULL;
664 const char *insert_errmsg = NULL;
665 int recognized_mnemonic = 0;
667 /* Skip leading white space. */
668 while (ISSPACE (* str))
671 /* The instructions are stored in hashed lists.
672 Get the first in the list. */
673 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
675 /* Keep looking until we find a match. */
677 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
679 const CGEN_INSN *insn = ilist->insn;
680 recognized_mnemonic = 1;
682 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
683 /* Not usually needed as unsupported opcodes
684 shouldn't be in the hash lists. */
685 /* Is this insn supported by the selected cpu? */
686 if (! lm32_cgen_insn_supported (cd, insn))
689 /* If the RELAXED attribute is set, this is an insn that shouldn't be
690 chosen immediately. Instead, it is used during assembler/linker
691 relaxation if possible. */
692 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
697 /* Skip this insn if str doesn't look right lexically. */
698 if (CGEN_INSN_RX (insn) != NULL &&
699 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
702 /* Allow parse/insert handlers to obtain length of insn. */
703 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
705 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
706 if (parse_errmsg != NULL)
709 /* ??? 0 is passed for `pc'. */
710 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
712 if (insert_errmsg != NULL)
715 /* It is up to the caller to actually output the insn and any
721 static char errbuf[150];
722 const char *tmp_errmsg;
723 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
731 /* If requesting verbose error messages, use insert_errmsg.
732 Failing that, use parse_errmsg. */
733 tmp_errmsg = (insert_errmsg ? insert_errmsg :
734 parse_errmsg ? parse_errmsg :
735 recognized_mnemonic ?
736 _("unrecognized form of instruction") :
737 _("unrecognized instruction"));
739 if (strlen (start) > 50)
740 /* xgettext:c-format */
741 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
743 /* xgettext:c-format */
744 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
748 if (strlen (start) > 50)
749 /* xgettext:c-format */
750 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
752 /* xgettext:c-format */
753 sprintf (errbuf, _("bad instruction `%.50s'"), start);