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 "or1k-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 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
55 static const char * INVALID_STORE_RELOC = N_("relocation invalid for store");
56 static const char * INVALID_RELOC_TYPE = N_("internal relocation type invalid");
58 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
61 parse_disp26 (CGEN_CPU_DESC cd,
65 enum cgen_parse_operand_result * resultp,
68 const char *errmsg = NULL;
69 enum cgen_parse_operand_result result_type;
71 if (strncasecmp (*strp, "plt(", 4) == 0)
76 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_OR1K_PLT26,
77 & result_type, & value);
79 return MISSING_CLOSING_PARENTHESIS;
82 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
83 value = (value >> 2) & 0xffff;
87 return cgen_parse_address (cd, strp, opindex, opinfo, resultp, valuep);
97 RTYPE_GOTPC = (2 << 2),
98 RTYPE_GOTOFF = (3 << 2),
99 RTYPE_TLSGD = (4 << 2),
100 RTYPE_TLSLDM = (5 << 2),
101 RTYPE_DTPOFF = (6 << 2),
102 RTYPE_GOTTPOFF = (7 << 2),
103 RTYPE_TPOFF = (8 << 2),
106 static const bfd_reloc_code_real_type or1k_imm16_relocs[][4] = {
110 BFD_RELOC_OR1K_SLO16 },
111 { BFD_RELOC_OR1K_GOT16,
115 { BFD_RELOC_OR1K_GOTPC_LO16,
116 BFD_RELOC_OR1K_GOTPC_HI16,
119 { BFD_RELOC_LO16_GOTOFF,
120 BFD_RELOC_HI16_GOTOFF,
121 BFD_RELOC_HI16_S_GOTOFF,
122 BFD_RELOC_OR1K_GOTOFF_SLO16 },
123 { BFD_RELOC_OR1K_TLS_GD_LO16,
124 BFD_RELOC_OR1K_TLS_GD_HI16,
127 { BFD_RELOC_OR1K_TLS_LDM_LO16,
128 BFD_RELOC_OR1K_TLS_LDM_HI16,
131 { BFD_RELOC_OR1K_TLS_LDO_LO16,
132 BFD_RELOC_OR1K_TLS_LDO_HI16,
135 { BFD_RELOC_OR1K_TLS_IE_LO16,
136 BFD_RELOC_OR1K_TLS_IE_HI16,
137 BFD_RELOC_OR1K_TLS_IE_AHI16,
139 { BFD_RELOC_OR1K_TLS_LE_LO16,
140 BFD_RELOC_OR1K_TLS_LE_HI16,
141 BFD_RELOC_OR1K_TLS_LE_AHI16,
142 BFD_RELOC_OR1K_TLS_LE_SLO16 }
146 parse_reloc(const char **strp)
148 const char *str = *strp;
151 if (strncasecmp (str, "got(", 4) == 0)
154 return RTYPE_GOT | RTYPE_LO;
157 if (strncasecmp (str, "gotpc", 5) == 0)
162 else if (strncasecmp (str, "gotoff", 6) == 0)
167 else if (strncasecmp (str, "tlsgd", 5) == 0)
172 else if (strncasecmp (str, "tlsldm", 6) == 0)
177 else if (strncasecmp (str, "dtpoff", 6) == 0)
182 else if (strncasecmp (str, "gottpoff", 8) == 0)
185 ret = RTYPE_GOTTPOFF;
187 else if (strncasecmp (str, "tpoff", 5) == 0)
193 if (strncasecmp (str, "hi(", 3) == 0)
198 else if (strncasecmp (str, "lo(", 3) == 0)
203 else if (strncasecmp (str, "ha(", 3) == 0)
216 parse_imm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
217 long *valuep, int splitp)
220 enum cgen_parse_operand_result result_type;
221 bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
228 reloc_type = parse_reloc (strp);
233 if ((reloc_type & 3) == RTYPE_LO && reloc_type != RTYPE_GOT)
234 reloc_type |= RTYPE_SLO;
236 return INVALID_STORE_RELOC;
238 reloc = or1k_imm16_relocs[reloc_type >> 2][reloc_type & 3];
241 if (reloc != BFD_RELOC_UNUSED)
245 errmsg = cgen_parse_address (cd, strp, opindex, reloc,
246 &result_type, &value);
248 errmsg = MISSING_CLOSING_PARENTHESIS;
253 if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
254 switch (reloc_type & 3)
265 ret = (ret ^ 0x8000) - 0x8000;
268 errmsg = INVALID_RELOC_TYPE;
274 errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
285 parse_simm16 (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
287 return parse_imm16(cd, strp, opindex, (long *) valuep, 0);
291 parse_simm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
294 return parse_imm16(cd, strp, opindex, (long *) valuep, 1);
298 parse_uimm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
299 unsigned long *valuep)
301 const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 0);
308 parse_uimm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
309 unsigned long *valuep)
311 const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 1);
319 const char * or1k_cgen_parse_operand
320 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
322 /* Main entry point for operand parsing.
324 This function is basically just a big switch statement. Earlier versions
325 used tables to look up the function to use, but
326 - if the table contains both assembler and disassembler functions then
327 the disassembler contains much of the assembler and vice-versa,
328 - there's a lot of inlining possibilities as things grow,
329 - using a switch statement avoids the function call overhead.
331 This function could be moved into `parse_insn_normal', but keeping it
332 separate makes clear the interface between `parse_insn_normal' and each of
336 or1k_cgen_parse_operand (CGEN_CPU_DESC cd,
339 CGEN_FIELDS * fields)
341 const char * errmsg = NULL;
342 /* Used by scalar operands that still need to be parsed. */
343 long junk ATTRIBUTE_UNUSED;
347 case OR1K_OPERAND_DISP26 :
350 errmsg = parse_disp26 (cd, strp, OR1K_OPERAND_DISP26, 0, NULL, & value);
351 fields->f_disp26 = value;
354 case OR1K_OPERAND_RA :
355 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r2);
357 case OR1K_OPERAND_RADF :
358 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
360 case OR1K_OPERAND_RASF :
361 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r2);
363 case OR1K_OPERAND_RB :
364 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r3);
366 case OR1K_OPERAND_RBDF :
367 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
369 case OR1K_OPERAND_RBSF :
370 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r3);
372 case OR1K_OPERAND_RD :
373 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r1);
375 case OR1K_OPERAND_RDDF :
376 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
378 case OR1K_OPERAND_RDSF :
379 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r1);
381 case OR1K_OPERAND_SIMM16 :
382 errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16, (long *) (& fields->f_simm16));
384 case OR1K_OPERAND_SIMM16_SPLIT :
385 errmsg = parse_simm16_split (cd, strp, OR1K_OPERAND_SIMM16_SPLIT, (long *) (& fields->f_simm16_split));
387 case OR1K_OPERAND_UIMM16 :
388 errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
390 case OR1K_OPERAND_UIMM16_SPLIT :
391 errmsg = parse_uimm16_split (cd, strp, OR1K_OPERAND_UIMM16_SPLIT, (unsigned long *) (& fields->f_uimm16_split));
393 case OR1K_OPERAND_UIMM6 :
394 errmsg = cgen_parse_unsigned_integer (cd, strp, OR1K_OPERAND_UIMM6, (unsigned long *) (& fields->f_uimm6));
398 /* xgettext:c-format */
399 opcodes_error_handler
400 (_("internal error: unrecognized field %d while parsing"),
408 cgen_parse_fn * const or1k_cgen_parse_handlers[] =
414 or1k_cgen_init_asm (CGEN_CPU_DESC cd)
416 or1k_cgen_init_opcode_table (cd);
417 or1k_cgen_init_ibld_table (cd);
418 cd->parse_handlers = & or1k_cgen_parse_handlers[0];
419 cd->parse_operand = or1k_cgen_parse_operand;
420 #ifdef CGEN_ASM_INIT_HOOK
427 /* Regex construction routine.
429 This translates an opcode syntax string into a regex string,
430 by replacing any non-character syntax element (such as an
431 opcode) with the pattern '.*'
433 It then compiles the regex and stores it in the opcode, for
434 later use by or1k_cgen_assemble_insn
436 Returns NULL for success, an error message for failure. */
439 or1k_cgen_build_insn_regex (CGEN_INSN *insn)
441 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
442 const char *mnem = CGEN_INSN_MNEMONIC (insn);
443 char rxbuf[CGEN_MAX_RX_ELEMENTS];
445 const CGEN_SYNTAX_CHAR_TYPE *syn;
448 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
450 /* Mnemonics come first in the syntax string. */
451 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
452 return _("missing mnemonic in syntax string");
455 /* Generate a case sensitive regular expression that emulates case
456 insensitive matching in the "C" locale. We cannot generate a case
457 insensitive regular expression because in Turkish locales, 'i' and 'I'
458 are not equal modulo case conversion. */
460 /* Copy the literal mnemonic out of the insn. */
461 for (; *mnem; mnem++)
476 /* Copy any remaining literals from the syntax string into the rx. */
477 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
479 if (CGEN_SYNTAX_CHAR_P (* syn))
481 char c = CGEN_SYNTAX_CHAR (* syn);
485 /* Escape any regex metacharacters in the syntax. */
486 case '.': case '[': case '\\':
487 case '*': case '^': case '$':
489 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
490 case '?': case '{': case '}':
491 case '(': case ')': case '*':
492 case '|': case '+': case ']':
513 /* Replace non-syntax fields with globs. */
519 /* Trailing whitespace ok. */
526 /* But anchor it after that. */
530 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
531 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
539 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
540 regfree ((regex_t *) CGEN_INSN_RX (insn));
541 free (CGEN_INSN_RX (insn));
542 (CGEN_INSN_RX (insn)) = NULL;
548 /* Default insn parser.
550 The syntax string is scanned and operands are parsed and stored in FIELDS.
551 Relocs are queued as we go via other callbacks.
553 ??? Note that this is currently an all-or-nothing parser. If we fail to
554 parse the instruction, we return 0 and the caller will start over from
555 the beginning. Backtracking will be necessary in parsing subexpressions,
556 but that can be handled there. Not handling backtracking here may get
557 expensive in the case of the m68k. Deal with later.
559 Returns NULL for success, an error message for failure. */
562 parse_insn_normal (CGEN_CPU_DESC cd,
563 const CGEN_INSN *insn,
567 /* ??? Runtime added insns not handled yet. */
568 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
569 const char *str = *strp;
572 const CGEN_SYNTAX_CHAR_TYPE * syn;
573 #ifdef CGEN_MNEMONIC_OPERANDS
578 /* For now we assume the mnemonic is first (there are no leading operands).
579 We can parse it without needing to set up operand parsing.
580 GAS's input scrubber will ensure mnemonics are lowercase, but we may
581 not be called from GAS. */
582 p = CGEN_INSN_MNEMONIC (insn);
583 while (*p && TOLOWER (*p) == TOLOWER (*str))
587 return _("unrecognized instruction");
589 #ifndef CGEN_MNEMONIC_OPERANDS
590 if (* str && ! ISSPACE (* str))
591 return _("unrecognized instruction");
594 CGEN_INIT_PARSE (cd);
595 cgen_init_parse_operand (cd);
596 #ifdef CGEN_MNEMONIC_OPERANDS
600 /* We don't check for (*str != '\0') here because we want to parse
601 any trailing fake arguments in the syntax string. */
602 syn = CGEN_SYNTAX_STRING (syntax);
604 /* Mnemonics come first for now, ensure valid string. */
605 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
612 /* Non operand chars must match exactly. */
613 if (CGEN_SYNTAX_CHAR_P (* syn))
615 /* FIXME: While we allow for non-GAS callers above, we assume the
616 first char after the mnemonic part is a space. */
617 /* FIXME: We also take inappropriate advantage of the fact that
618 GAS's input scrubber will remove extraneous blanks. */
619 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
621 #ifdef CGEN_MNEMONIC_OPERANDS
622 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
630 /* Syntax char didn't match. Can't be this insn. */
631 static char msg [80];
633 /* xgettext:c-format */
634 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
635 CGEN_SYNTAX_CHAR(*syn), *str);
640 /* Ran out of input. */
641 static char msg [80];
643 /* xgettext:c-format */
644 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
645 CGEN_SYNTAX_CHAR(*syn));
651 #ifdef CGEN_MNEMONIC_OPERANDS
652 (void) past_opcode_p;
654 /* We have an operand of some sort. */
655 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
659 /* Done with this operand, continue with next one. */
663 /* If we're at the end of the syntax string, we're done. */
666 /* FIXME: For the moment we assume a valid `str' can only contain
667 blanks now. IE: We needn't try again with a longer version of
668 the insn and it is assumed that longer versions of insns appear
669 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
670 while (ISSPACE (* str))
674 return _("junk at end of line"); /* FIXME: would like to include `str' */
679 /* We couldn't parse it. */
680 return _("unrecognized instruction");
684 This routine is called for each instruction to be assembled.
685 STR points to the insn to be assembled.
686 We assume all necessary tables have been initialized.
687 The assembled instruction, less any fixups, is stored in BUF.
688 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
689 still needs to be converted to target byte order, otherwise BUF is an array
690 of bytes in target byte order.
691 The result is a pointer to the insn's entry in the opcode table,
692 or NULL if an error occured (an error message will have already been
695 Note that when processing (non-alias) macro-insns,
696 this function recurses.
698 ??? It's possible to make this cpu-independent.
699 One would have to deal with a few minor things.
700 At this point in time doing so would be more of a curiosity than useful
701 [for example this file isn't _that_ big], but keeping the possibility in
702 mind helps keep the design clean. */
705 or1k_cgen_assemble_insn (CGEN_CPU_DESC cd,
708 CGEN_INSN_BYTES_PTR buf,
712 CGEN_INSN_LIST *ilist;
713 const char *parse_errmsg = NULL;
714 const char *insert_errmsg = NULL;
715 int recognized_mnemonic = 0;
717 /* Skip leading white space. */
718 while (ISSPACE (* str))
721 /* The instructions are stored in hashed lists.
722 Get the first in the list. */
723 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
725 /* Keep looking until we find a match. */
727 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
729 const CGEN_INSN *insn = ilist->insn;
730 recognized_mnemonic = 1;
732 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
733 /* Not usually needed as unsupported opcodes
734 shouldn't be in the hash lists. */
735 /* Is this insn supported by the selected cpu? */
736 if (! or1k_cgen_insn_supported (cd, insn))
739 /* If the RELAXED attribute is set, this is an insn that shouldn't be
740 chosen immediately. Instead, it is used during assembler/linker
741 relaxation if possible. */
742 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
747 /* Skip this insn if str doesn't look right lexically. */
748 if (CGEN_INSN_RX (insn) != NULL &&
749 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
752 /* Allow parse/insert handlers to obtain length of insn. */
753 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
755 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
756 if (parse_errmsg != NULL)
759 /* ??? 0 is passed for `pc'. */
760 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
762 if (insert_errmsg != NULL)
765 /* It is up to the caller to actually output the insn and any
771 static char errbuf[150];
772 const char *tmp_errmsg;
773 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
781 /* If requesting verbose error messages, use insert_errmsg.
782 Failing that, use parse_errmsg. */
783 tmp_errmsg = (insert_errmsg ? insert_errmsg :
784 parse_errmsg ? parse_errmsg :
785 recognized_mnemonic ?
786 _("unrecognized form of instruction") :
787 _("unrecognized instruction"));
789 if (strlen (start) > 50)
790 /* xgettext:c-format */
791 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
793 /* xgettext:c-format */
794 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
798 if (strlen (start) > 50)
799 /* xgettext:c-format */
800 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
802 /* xgettext:c-format */
803 sprintf (errbuf, _("bad instruction `%.50s'"), start);