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.
35 #include "m32r-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. */
53 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
55 /* Handle '#' prefixes (i.e. skip over them). */
58 parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
60 int opindex ATTRIBUTE_UNUSED,
61 long *valuep ATTRIBUTE_UNUSED)
68 /* Handle shigh(), high(). */
71 parse_hi16 (CGEN_CPU_DESC cd,
74 unsigned long *valuep)
77 enum cgen_parse_operand_result result_type;
83 if (strncasecmp (*strp, "high(", 5) == 0)
86 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO,
87 & result_type, & value);
89 return MISSING_CLOSING_PARENTHESIS;
92 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
100 else if (strncasecmp (*strp, "shigh(", 6) == 0)
103 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO,
104 & result_type, & value);
106 return MISSING_CLOSING_PARENTHESIS;
109 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
119 return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
122 /* Handle low() in a signed context. Also handle sda().
123 The signedness of the value doesn't matter to low(), but this also
124 handles the case where low() isn't present. */
127 parse_slo16 (CGEN_CPU_DESC cd,
133 enum cgen_parse_operand_result result_type;
139 if (strncasecmp (*strp, "low(", 4) == 0)
142 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
143 & result_type, & value);
145 return MISSING_CLOSING_PARENTHESIS;
148 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
149 value = ((value & 0xffff) ^ 0x8000) - 0x8000;
154 if (strncasecmp (*strp, "sda(", 4) == 0)
157 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16,
160 return MISSING_CLOSING_PARENTHESIS;
166 return cgen_parse_signed_integer (cd, strp, opindex, valuep);
169 /* Handle low() in an unsigned context.
170 The signedness of the value doesn't matter to low(), but this also
171 handles the case where low() isn't present. */
174 parse_ulo16 (CGEN_CPU_DESC cd,
177 unsigned long *valuep)
180 enum cgen_parse_operand_result result_type;
186 if (strncasecmp (*strp, "low(", 4) == 0)
189 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
190 & result_type, & value);
192 return MISSING_CLOSING_PARENTHESIS;
195 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
201 return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
206 const char * m32r_cgen_parse_operand
207 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
209 /* Main entry point for operand parsing.
211 This function is basically just a big switch statement. Earlier versions
212 used tables to look up the function to use, but
213 - if the table contains both assembler and disassembler functions then
214 the disassembler contains much of the assembler and vice-versa,
215 - there's a lot of inlining possibilities as things grow,
216 - using a switch statement avoids the function call overhead.
218 This function could be moved into `parse_insn_normal', but keeping it
219 separate makes clear the interface between `parse_insn_normal' and each of
223 m32r_cgen_parse_operand (CGEN_CPU_DESC cd,
226 CGEN_FIELDS * fields)
228 const char * errmsg = NULL;
229 /* Used by scalar operands that still need to be parsed. */
230 long junk ATTRIBUTE_UNUSED;
234 case M32R_OPERAND_ACC :
235 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_acc);
237 case M32R_OPERAND_ACCD :
238 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accd);
240 case M32R_OPERAND_ACCS :
241 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accs);
243 case M32R_OPERAND_DCR :
244 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r1);
246 case M32R_OPERAND_DISP16 :
249 errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP16, 0, NULL, & value);
250 fields->f_disp16 = value;
253 case M32R_OPERAND_DISP24 :
256 errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP24, 0, NULL, & value);
257 fields->f_disp24 = value;
260 case M32R_OPERAND_DISP8 :
263 errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP8, 0, NULL, & value);
264 fields->f_disp8 = value;
267 case M32R_OPERAND_DR :
268 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
270 case M32R_OPERAND_HASH :
271 errmsg = parse_hash (cd, strp, M32R_OPERAND_HASH, (long *) (& junk));
273 case M32R_OPERAND_HI16 :
274 errmsg = parse_hi16 (cd, strp, M32R_OPERAND_HI16, (unsigned long *) (& fields->f_hi16));
276 case M32R_OPERAND_IMM1 :
277 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_IMM1, (unsigned long *) (& fields->f_imm1));
279 case M32R_OPERAND_SCR :
280 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r2);
282 case M32R_OPERAND_SIMM16 :
283 errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM16, (long *) (& fields->f_simm16));
285 case M32R_OPERAND_SIMM8 :
286 errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM8, (long *) (& fields->f_simm8));
288 case M32R_OPERAND_SLO16 :
289 errmsg = parse_slo16 (cd, strp, M32R_OPERAND_SLO16, (long *) (& fields->f_simm16));
291 case M32R_OPERAND_SR :
292 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
294 case M32R_OPERAND_SRC1 :
295 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
297 case M32R_OPERAND_SRC2 :
298 errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
300 case M32R_OPERAND_UIMM16 :
301 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
303 case M32R_OPERAND_UIMM24 :
306 errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_UIMM24, 0, NULL, & value);
307 fields->f_uimm24 = value;
310 case M32R_OPERAND_UIMM3 :
311 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM3, (unsigned long *) (& fields->f_uimm3));
313 case M32R_OPERAND_UIMM4 :
314 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4));
316 case M32R_OPERAND_UIMM5 :
317 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM5, (unsigned long *) (& fields->f_uimm5));
319 case M32R_OPERAND_UIMM8 :
320 errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8));
322 case M32R_OPERAND_ULO16 :
323 errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, (unsigned long *) (& fields->f_uimm16));
327 /* xgettext:c-format */
328 opcodes_error_handler
329 (_("internal error: unrecognized field %d while parsing"),
337 cgen_parse_fn * const m32r_cgen_parse_handlers[] =
343 m32r_cgen_init_asm (CGEN_CPU_DESC cd)
345 m32r_cgen_init_opcode_table (cd);
346 m32r_cgen_init_ibld_table (cd);
347 cd->parse_handlers = & m32r_cgen_parse_handlers[0];
348 cd->parse_operand = m32r_cgen_parse_operand;
349 #ifdef CGEN_ASM_INIT_HOOK
356 /* Regex construction routine.
358 This translates an opcode syntax string into a regex string,
359 by replacing any non-character syntax element (such as an
360 opcode) with the pattern '.*'
362 It then compiles the regex and stores it in the opcode, for
363 later use by m32r_cgen_assemble_insn
365 Returns NULL for success, an error message for failure. */
368 m32r_cgen_build_insn_regex (CGEN_INSN *insn)
370 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
371 const char *mnem = CGEN_INSN_MNEMONIC (insn);
372 char rxbuf[CGEN_MAX_RX_ELEMENTS];
374 const CGEN_SYNTAX_CHAR_TYPE *syn;
377 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
379 /* Mnemonics come first in the syntax string. */
380 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
381 return _("missing mnemonic in syntax string");
384 /* Generate a case sensitive regular expression that emulates case
385 insensitive matching in the "C" locale. We cannot generate a case
386 insensitive regular expression because in Turkish locales, 'i' and 'I'
387 are not equal modulo case conversion. */
389 /* Copy the literal mnemonic out of the insn. */
390 for (; *mnem; mnem++)
405 /* Copy any remaining literals from the syntax string into the rx. */
406 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
408 if (CGEN_SYNTAX_CHAR_P (* syn))
410 char c = CGEN_SYNTAX_CHAR (* syn);
414 /* Escape any regex metacharacters in the syntax. */
415 case '.': case '[': case '\\':
416 case '*': case '^': case '$':
418 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
419 case '?': case '{': case '}':
420 case '(': case ')': case '*':
421 case '|': case '+': case ']':
442 /* Replace non-syntax fields with globs. */
448 /* Trailing whitespace ok. */
455 /* But anchor it after that. */
459 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
460 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
468 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
469 regfree ((regex_t *) CGEN_INSN_RX (insn));
470 free (CGEN_INSN_RX (insn));
471 (CGEN_INSN_RX (insn)) = NULL;
477 /* Default insn parser.
479 The syntax string is scanned and operands are parsed and stored in FIELDS.
480 Relocs are queued as we go via other callbacks.
482 ??? Note that this is currently an all-or-nothing parser. If we fail to
483 parse the instruction, we return 0 and the caller will start over from
484 the beginning. Backtracking will be necessary in parsing subexpressions,
485 but that can be handled there. Not handling backtracking here may get
486 expensive in the case of the m68k. Deal with later.
488 Returns NULL for success, an error message for failure. */
491 parse_insn_normal (CGEN_CPU_DESC cd,
492 const CGEN_INSN *insn,
496 /* ??? Runtime added insns not handled yet. */
497 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
498 const char *str = *strp;
501 const CGEN_SYNTAX_CHAR_TYPE * syn;
502 #ifdef CGEN_MNEMONIC_OPERANDS
507 /* For now we assume the mnemonic is first (there are no leading operands).
508 We can parse it without needing to set up operand parsing.
509 GAS's input scrubber will ensure mnemonics are lowercase, but we may
510 not be called from GAS. */
511 p = CGEN_INSN_MNEMONIC (insn);
512 while (*p && TOLOWER (*p) == TOLOWER (*str))
516 return _("unrecognized instruction");
518 #ifndef CGEN_MNEMONIC_OPERANDS
519 if (* str && ! ISSPACE (* str))
520 return _("unrecognized instruction");
523 CGEN_INIT_PARSE (cd);
524 cgen_init_parse_operand (cd);
525 #ifdef CGEN_MNEMONIC_OPERANDS
529 /* We don't check for (*str != '\0') here because we want to parse
530 any trailing fake arguments in the syntax string. */
531 syn = CGEN_SYNTAX_STRING (syntax);
533 /* Mnemonics come first for now, ensure valid string. */
534 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
541 /* Non operand chars must match exactly. */
542 if (CGEN_SYNTAX_CHAR_P (* syn))
544 /* FIXME: While we allow for non-GAS callers above, we assume the
545 first char after the mnemonic part is a space. */
546 /* FIXME: We also take inappropriate advantage of the fact that
547 GAS's input scrubber will remove extraneous blanks. */
548 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
550 #ifdef CGEN_MNEMONIC_OPERANDS
551 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
559 /* Syntax char didn't match. Can't be this insn. */
560 static char msg [80];
562 /* xgettext:c-format */
563 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
564 CGEN_SYNTAX_CHAR(*syn), *str);
569 /* Ran out of input. */
570 static char msg [80];
572 /* xgettext:c-format */
573 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
574 CGEN_SYNTAX_CHAR(*syn));
580 #ifdef CGEN_MNEMONIC_OPERANDS
581 (void) past_opcode_p;
583 /* We have an operand of some sort. */
584 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
588 /* Done with this operand, continue with next one. */
592 /* If we're at the end of the syntax string, we're done. */
595 /* FIXME: For the moment we assume a valid `str' can only contain
596 blanks now. IE: We needn't try again with a longer version of
597 the insn and it is assumed that longer versions of insns appear
598 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
599 while (ISSPACE (* str))
603 return _("junk at end of line"); /* FIXME: would like to include `str' */
608 /* We couldn't parse it. */
609 return _("unrecognized instruction");
613 This routine is called for each instruction to be assembled.
614 STR points to the insn to be assembled.
615 We assume all necessary tables have been initialized.
616 The assembled instruction, less any fixups, is stored in BUF.
617 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
618 still needs to be converted to target byte order, otherwise BUF is an array
619 of bytes in target byte order.
620 The result is a pointer to the insn's entry in the opcode table,
621 or NULL if an error occured (an error message will have already been
624 Note that when processing (non-alias) macro-insns,
625 this function recurses.
627 ??? It's possible to make this cpu-independent.
628 One would have to deal with a few minor things.
629 At this point in time doing so would be more of a curiosity than useful
630 [for example this file isn't _that_ big], but keeping the possibility in
631 mind helps keep the design clean. */
634 m32r_cgen_assemble_insn (CGEN_CPU_DESC cd,
637 CGEN_INSN_BYTES_PTR buf,
641 CGEN_INSN_LIST *ilist;
642 const char *parse_errmsg = NULL;
643 const char *insert_errmsg = NULL;
644 int recognized_mnemonic = 0;
646 /* Skip leading white space. */
647 while (ISSPACE (* str))
650 /* The instructions are stored in hashed lists.
651 Get the first in the list. */
652 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
654 /* Keep looking until we find a match. */
656 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
658 const CGEN_INSN *insn = ilist->insn;
659 recognized_mnemonic = 1;
661 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
662 /* Not usually needed as unsupported opcodes
663 shouldn't be in the hash lists. */
664 /* Is this insn supported by the selected cpu? */
665 if (! m32r_cgen_insn_supported (cd, insn))
668 /* If the RELAXED attribute is set, this is an insn that shouldn't be
669 chosen immediately. Instead, it is used during assembler/linker
670 relaxation if possible. */
671 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
676 /* Skip this insn if str doesn't look right lexically. */
677 if (CGEN_INSN_RX (insn) != NULL &&
678 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
681 /* Allow parse/insert handlers to obtain length of insn. */
682 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
684 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
685 if (parse_errmsg != NULL)
688 /* ??? 0 is passed for `pc'. */
689 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
691 if (insert_errmsg != NULL)
694 /* It is up to the caller to actually output the insn and any
700 static char errbuf[150];
701 const char *tmp_errmsg;
702 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
710 /* If requesting verbose error messages, use insert_errmsg.
711 Failing that, use parse_errmsg. */
712 tmp_errmsg = (insert_errmsg ? insert_errmsg :
713 parse_errmsg ? parse_errmsg :
714 recognized_mnemonic ?
715 _("unrecognized form of instruction") :
716 _("unrecognized instruction"));
718 if (strlen (start) > 50)
719 /* xgettext:c-format */
720 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
722 /* xgettext:c-format */
723 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
727 if (strlen (start) > 50)
728 /* xgettext:c-format */
729 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
731 /* xgettext:c-format */
732 sprintf (errbuf, _("bad instruction `%.50s'"), start);