185815Sobrien/* Assembler interface for targets using CGEN. -*- C -*- 285815Sobrien CGEN: Cpu tools GENerator 385815Sobrien 4218822Sdim THIS FILE IS MACHINE GENERATED WITH CGEN. 5218822Sdim - the resultant file is machine generated, cgen-asm.in isn't 685815Sobrien 7218822Sdim Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005 8218822Sdim Free Software Foundation, Inc. 985815Sobrien 10218822Sdim This file is part of the GNU Binutils and GDB, the GNU debugger. 1185815Sobrien 12218822Sdim This program is free software; you can redistribute it and/or modify 13218822Sdim it under the terms of the GNU General Public License as published by 14218822Sdim the Free Software Foundation; either version 2, or (at your option) 15218822Sdim any later version. 1685815Sobrien 17218822Sdim This program is distributed in the hope that it will be useful, 18218822Sdim but WITHOUT ANY WARRANTY; without even the implied warranty of 19218822Sdim MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20218822Sdim GNU General Public License for more details. 2185815Sobrien 22218822Sdim You should have received a copy of the GNU General Public License 23218822Sdim along with this program; if not, write to the Free Software Foundation, Inc., 24218822Sdim 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 2585815Sobrien 2685815Sobrien/* ??? Eventually more and more of this stuff can go to cpu-independent files. 2785815Sobrien Keep that in mind. */ 2885815Sobrien 2985815Sobrien#include "sysdep.h" 3085815Sobrien#include <stdio.h> 3185815Sobrien#include "ansidecl.h" 3285815Sobrien#include "bfd.h" 3385815Sobrien#include "symcat.h" 3485815Sobrien#include "@prefix@-desc.h" 3585815Sobrien#include "@prefix@-opc.h" 3685815Sobrien#include "opintl.h" 3789857Sobrien#include "xregex.h" 3889857Sobrien#include "libiberty.h" 3989857Sobrien#include "safe-ctype.h" 4085815Sobrien 4189857Sobrien#undef min 4285815Sobrien#define min(a,b) ((a) < (b) ? (a) : (b)) 4389857Sobrien#undef max 4485815Sobrien#define max(a,b) ((a) > (b) ? (a) : (b)) 4585815Sobrien 4685815Sobrienstatic const char * parse_insn_normal 47130561Sobrien (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); 4885815Sobrien 4989857Sobrien/* -- assembler routines inserted here. */ 5085815Sobrien 5189857Sobrien 5289857Sobrien/* Regex construction routine. 5389857Sobrien 5489857Sobrien This translates an opcode syntax string into a regex string, 5589857Sobrien by replacing any non-character syntax element (such as an 5689857Sobrien opcode) with the pattern '.*' 5789857Sobrien 5889857Sobrien It then compiles the regex and stores it in the opcode, for 5989857Sobrien later use by @arch@_cgen_assemble_insn 6089857Sobrien 6189857Sobrien Returns NULL for success, an error message for failure. */ 6289857Sobrien 6389857Sobrienchar * 64130561Sobrien@arch@_cgen_build_insn_regex (CGEN_INSN *insn) 6589857Sobrien{ 6689857Sobrien CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); 6789857Sobrien const char *mnem = CGEN_INSN_MNEMONIC (insn); 6889857Sobrien char rxbuf[CGEN_MAX_RX_ELEMENTS]; 6989857Sobrien char *rx = rxbuf; 7089857Sobrien const CGEN_SYNTAX_CHAR_TYPE *syn; 7189857Sobrien int reg_err; 7289857Sobrien 7389857Sobrien syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); 7489857Sobrien 7589857Sobrien /* Mnemonics come first in the syntax string. */ 7689857Sobrien if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 7789857Sobrien return _("missing mnemonic in syntax string"); 7889857Sobrien ++syn; 7989857Sobrien 8089857Sobrien /* Generate a case sensitive regular expression that emulates case 8189857Sobrien insensitive matching in the "C" locale. We cannot generate a case 8289857Sobrien insensitive regular expression because in Turkish locales, 'i' and 'I' 8389857Sobrien are not equal modulo case conversion. */ 8489857Sobrien 8589857Sobrien /* Copy the literal mnemonic out of the insn. */ 8689857Sobrien for (; *mnem; mnem++) 8789857Sobrien { 8889857Sobrien char c = *mnem; 8989857Sobrien 9089857Sobrien if (ISALPHA (c)) 9189857Sobrien { 9289857Sobrien *rx++ = '['; 9389857Sobrien *rx++ = TOLOWER (c); 9489857Sobrien *rx++ = TOUPPER (c); 9589857Sobrien *rx++ = ']'; 9689857Sobrien } 9789857Sobrien else 9889857Sobrien *rx++ = c; 9989857Sobrien } 10089857Sobrien 10189857Sobrien /* Copy any remaining literals from the syntax string into the rx. */ 10289857Sobrien for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) 10389857Sobrien { 10489857Sobrien if (CGEN_SYNTAX_CHAR_P (* syn)) 10589857Sobrien { 10689857Sobrien char c = CGEN_SYNTAX_CHAR (* syn); 10789857Sobrien 10889857Sobrien switch (c) 10989857Sobrien { 11089857Sobrien /* Escape any regex metacharacters in the syntax. */ 11189857Sobrien case '.': case '[': case '\\': 11289857Sobrien case '*': case '^': case '$': 11389857Sobrien 11489857Sobrien#ifdef CGEN_ESCAPE_EXTENDED_REGEX 11589857Sobrien case '?': case '{': case '}': 11689857Sobrien case '(': case ')': case '*': 11789857Sobrien case '|': case '+': case ']': 11889857Sobrien#endif 11989857Sobrien *rx++ = '\\'; 12089857Sobrien *rx++ = c; 12189857Sobrien break; 12289857Sobrien 12389857Sobrien default: 12489857Sobrien if (ISALPHA (c)) 12589857Sobrien { 12689857Sobrien *rx++ = '['; 12789857Sobrien *rx++ = TOLOWER (c); 12889857Sobrien *rx++ = TOUPPER (c); 12989857Sobrien *rx++ = ']'; 13089857Sobrien } 13189857Sobrien else 13289857Sobrien *rx++ = c; 13389857Sobrien break; 13489857Sobrien } 13589857Sobrien } 13689857Sobrien else 13789857Sobrien { 13889857Sobrien /* Replace non-syntax fields with globs. */ 13989857Sobrien *rx++ = '.'; 14089857Sobrien *rx++ = '*'; 14189857Sobrien } 14289857Sobrien } 14389857Sobrien 14489857Sobrien /* Trailing whitespace ok. */ 14589857Sobrien * rx++ = '['; 14689857Sobrien * rx++ = ' '; 14789857Sobrien * rx++ = '\t'; 14889857Sobrien * rx++ = ']'; 14989857Sobrien * rx++ = '*'; 15089857Sobrien 15189857Sobrien /* But anchor it after that. */ 15289857Sobrien * rx++ = '$'; 15389857Sobrien * rx = '\0'; 15489857Sobrien 15589857Sobrien CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); 15689857Sobrien reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); 15789857Sobrien 15889857Sobrien if (reg_err == 0) 15989857Sobrien return NULL; 16089857Sobrien else 16189857Sobrien { 16289857Sobrien static char msg[80]; 16389857Sobrien 16489857Sobrien regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); 16589857Sobrien regfree ((regex_t *) CGEN_INSN_RX (insn)); 16689857Sobrien free (CGEN_INSN_RX (insn)); 16789857Sobrien (CGEN_INSN_RX (insn)) = NULL; 16889857Sobrien return msg; 16989857Sobrien } 17089857Sobrien} 17189857Sobrien 17289857Sobrien 17385815Sobrien/* Default insn parser. 17485815Sobrien 17585815Sobrien The syntax string is scanned and operands are parsed and stored in FIELDS. 17685815Sobrien Relocs are queued as we go via other callbacks. 17785815Sobrien 17885815Sobrien ??? Note that this is currently an all-or-nothing parser. If we fail to 17985815Sobrien parse the instruction, we return 0 and the caller will start over from 18085815Sobrien the beginning. Backtracking will be necessary in parsing subexpressions, 18185815Sobrien but that can be handled there. Not handling backtracking here may get 18285815Sobrien expensive in the case of the m68k. Deal with later. 18385815Sobrien 18489857Sobrien Returns NULL for success, an error message for failure. */ 18585815Sobrien 18685815Sobrienstatic const char * 187130561Sobrienparse_insn_normal (CGEN_CPU_DESC cd, 188130561Sobrien const CGEN_INSN *insn, 189130561Sobrien const char **strp, 190130561Sobrien CGEN_FIELDS *fields) 19185815Sobrien{ 19285815Sobrien /* ??? Runtime added insns not handled yet. */ 19385815Sobrien const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); 19485815Sobrien const char *str = *strp; 19585815Sobrien const char *errmsg; 19685815Sobrien const char *p; 19785815Sobrien const CGEN_SYNTAX_CHAR_TYPE * syn; 19885815Sobrien#ifdef CGEN_MNEMONIC_OPERANDS 19985815Sobrien /* FIXME: wip */ 20085815Sobrien int past_opcode_p; 20185815Sobrien#endif 20285815Sobrien 20385815Sobrien /* For now we assume the mnemonic is first (there are no leading operands). 20485815Sobrien We can parse it without needing to set up operand parsing. 20585815Sobrien GAS's input scrubber will ensure mnemonics are lowercase, but we may 20685815Sobrien not be called from GAS. */ 20785815Sobrien p = CGEN_INSN_MNEMONIC (insn); 20889857Sobrien while (*p && TOLOWER (*p) == TOLOWER (*str)) 20985815Sobrien ++p, ++str; 21085815Sobrien 21185815Sobrien if (* p) 21285815Sobrien return _("unrecognized instruction"); 21385815Sobrien 21485815Sobrien#ifndef CGEN_MNEMONIC_OPERANDS 21589857Sobrien if (* str && ! ISSPACE (* str)) 21685815Sobrien return _("unrecognized instruction"); 21785815Sobrien#endif 21885815Sobrien 21985815Sobrien CGEN_INIT_PARSE (cd); 22085815Sobrien cgen_init_parse_operand (cd); 22185815Sobrien#ifdef CGEN_MNEMONIC_OPERANDS 22285815Sobrien past_opcode_p = 0; 22385815Sobrien#endif 22485815Sobrien 22585815Sobrien /* We don't check for (*str != '\0') here because we want to parse 22685815Sobrien any trailing fake arguments in the syntax string. */ 22785815Sobrien syn = CGEN_SYNTAX_STRING (syntax); 22885815Sobrien 22985815Sobrien /* Mnemonics come first for now, ensure valid string. */ 23085815Sobrien if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 23185815Sobrien abort (); 23285815Sobrien 23385815Sobrien ++syn; 23485815Sobrien 23585815Sobrien while (* syn != 0) 23685815Sobrien { 23785815Sobrien /* Non operand chars must match exactly. */ 23885815Sobrien if (CGEN_SYNTAX_CHAR_P (* syn)) 23985815Sobrien { 24085815Sobrien /* FIXME: While we allow for non-GAS callers above, we assume the 24185815Sobrien first char after the mnemonic part is a space. */ 24285815Sobrien /* FIXME: We also take inappropriate advantage of the fact that 24385815Sobrien GAS's input scrubber will remove extraneous blanks. */ 24489857Sobrien if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) 24585815Sobrien { 24685815Sobrien#ifdef CGEN_MNEMONIC_OPERANDS 24785815Sobrien if (CGEN_SYNTAX_CHAR(* syn) == ' ') 24885815Sobrien past_opcode_p = 1; 24985815Sobrien#endif 25085815Sobrien ++ syn; 25185815Sobrien ++ str; 25285815Sobrien } 25385815Sobrien else if (*str) 25485815Sobrien { 25585815Sobrien /* Syntax char didn't match. Can't be this insn. */ 25685815Sobrien static char msg [80]; 25789857Sobrien 25885815Sobrien /* xgettext:c-format */ 25985815Sobrien sprintf (msg, _("syntax error (expected char `%c', found `%c')"), 26085815Sobrien CGEN_SYNTAX_CHAR(*syn), *str); 26185815Sobrien return msg; 26285815Sobrien } 26385815Sobrien else 26485815Sobrien { 26585815Sobrien /* Ran out of input. */ 26685815Sobrien static char msg [80]; 26789857Sobrien 26885815Sobrien /* xgettext:c-format */ 26985815Sobrien sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), 27085815Sobrien CGEN_SYNTAX_CHAR(*syn)); 27185815Sobrien return msg; 27285815Sobrien } 27385815Sobrien continue; 27485815Sobrien } 27585815Sobrien 27685815Sobrien /* We have an operand of some sort. */ 277104834Sobrien errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), 27885815Sobrien &str, fields); 27985815Sobrien if (errmsg) 28085815Sobrien return errmsg; 28185815Sobrien 28285815Sobrien /* Done with this operand, continue with next one. */ 28385815Sobrien ++ syn; 28485815Sobrien } 28585815Sobrien 28685815Sobrien /* If we're at the end of the syntax string, we're done. */ 28785815Sobrien if (* syn == 0) 28885815Sobrien { 28985815Sobrien /* FIXME: For the moment we assume a valid `str' can only contain 29085815Sobrien blanks now. IE: We needn't try again with a longer version of 29185815Sobrien the insn and it is assumed that longer versions of insns appear 29285815Sobrien before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ 29389857Sobrien while (ISSPACE (* str)) 29485815Sobrien ++ str; 29585815Sobrien 29685815Sobrien if (* str != '\0') 29785815Sobrien return _("junk at end of line"); /* FIXME: would like to include `str' */ 29885815Sobrien 29985815Sobrien return NULL; 30085815Sobrien } 30185815Sobrien 30285815Sobrien /* We couldn't parse it. */ 30385815Sobrien return _("unrecognized instruction"); 30485815Sobrien} 30585815Sobrien 30685815Sobrien/* Main entry point. 30785815Sobrien This routine is called for each instruction to be assembled. 30885815Sobrien STR points to the insn to be assembled. 30985815Sobrien We assume all necessary tables have been initialized. 31085815Sobrien The assembled instruction, less any fixups, is stored in BUF. 31185815Sobrien Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value 31285815Sobrien still needs to be converted to target byte order, otherwise BUF is an array 31385815Sobrien of bytes in target byte order. 31485815Sobrien The result is a pointer to the insn's entry in the opcode table, 31585815Sobrien or NULL if an error occured (an error message will have already been 31685815Sobrien printed). 31785815Sobrien 31885815Sobrien Note that when processing (non-alias) macro-insns, 31985815Sobrien this function recurses. 32085815Sobrien 32185815Sobrien ??? It's possible to make this cpu-independent. 32285815Sobrien One would have to deal with a few minor things. 32385815Sobrien At this point in time doing so would be more of a curiosity than useful 32485815Sobrien [for example this file isn't _that_ big], but keeping the possibility in 32585815Sobrien mind helps keep the design clean. */ 32685815Sobrien 32785815Sobrienconst CGEN_INSN * 328130561Sobrien@arch@_cgen_assemble_insn (CGEN_CPU_DESC cd, 329130561Sobrien const char *str, 330130561Sobrien CGEN_FIELDS *fields, 331130561Sobrien CGEN_INSN_BYTES_PTR buf, 332130561Sobrien char **errmsg) 33385815Sobrien{ 33485815Sobrien const char *start; 33585815Sobrien CGEN_INSN_LIST *ilist; 33685815Sobrien const char *parse_errmsg = NULL; 33785815Sobrien const char *insert_errmsg = NULL; 33889857Sobrien int recognized_mnemonic = 0; 33985815Sobrien 34085815Sobrien /* Skip leading white space. */ 34189857Sobrien while (ISSPACE (* str)) 34285815Sobrien ++ str; 34385815Sobrien 34485815Sobrien /* The instructions are stored in hashed lists. 34585815Sobrien Get the first in the list. */ 34685815Sobrien ilist = CGEN_ASM_LOOKUP_INSN (cd, str); 34785815Sobrien 34885815Sobrien /* Keep looking until we find a match. */ 34985815Sobrien start = str; 35085815Sobrien for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) 35185815Sobrien { 35285815Sobrien const CGEN_INSN *insn = ilist->insn; 35389857Sobrien recognized_mnemonic = 1; 35485815Sobrien 35585815Sobrien#ifdef CGEN_VALIDATE_INSN_SUPPORTED 35689857Sobrien /* Not usually needed as unsupported opcodes 35789857Sobrien shouldn't be in the hash lists. */ 35885815Sobrien /* Is this insn supported by the selected cpu? */ 35985815Sobrien if (! @arch@_cgen_insn_supported (cd, insn)) 36085815Sobrien continue; 36185815Sobrien#endif 362130561Sobrien /* If the RELAXED attribute is set, this is an insn that shouldn't be 36385815Sobrien chosen immediately. Instead, it is used during assembler/linker 36485815Sobrien relaxation if possible. */ 365130561Sobrien if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) 36685815Sobrien continue; 36785815Sobrien 36885815Sobrien str = start; 36985815Sobrien 37089857Sobrien /* Skip this insn if str doesn't look right lexically. */ 37189857Sobrien if (CGEN_INSN_RX (insn) != NULL && 37289857Sobrien regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) 37389857Sobrien continue; 37489857Sobrien 37585815Sobrien /* Allow parse/insert handlers to obtain length of insn. */ 37685815Sobrien CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); 37785815Sobrien 37885815Sobrien parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); 37985815Sobrien if (parse_errmsg != NULL) 38085815Sobrien continue; 38185815Sobrien 38289857Sobrien /* ??? 0 is passed for `pc'. */ 38385815Sobrien insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, 38485815Sobrien (bfd_vma) 0); 38585815Sobrien if (insert_errmsg != NULL) 38685815Sobrien continue; 38785815Sobrien 38885815Sobrien /* It is up to the caller to actually output the insn and any 38985815Sobrien queued relocs. */ 39085815Sobrien return insn; 39185815Sobrien } 39285815Sobrien 39385815Sobrien { 39485815Sobrien static char errbuf[150]; 39589857Sobrien#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS 39685815Sobrien const char *tmp_errmsg; 39785815Sobrien 39885815Sobrien /* If requesting verbose error messages, use insert_errmsg. 39989857Sobrien Failing that, use parse_errmsg. */ 40085815Sobrien tmp_errmsg = (insert_errmsg ? insert_errmsg : 40185815Sobrien parse_errmsg ? parse_errmsg : 40289857Sobrien recognized_mnemonic ? 40389857Sobrien _("unrecognized form of instruction") : 40485815Sobrien _("unrecognized instruction")); 40585815Sobrien 40685815Sobrien if (strlen (start) > 50) 40785815Sobrien /* xgettext:c-format */ 40885815Sobrien sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); 40985815Sobrien else 41085815Sobrien /* xgettext:c-format */ 41185815Sobrien sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); 41285815Sobrien#else 41385815Sobrien if (strlen (start) > 50) 41485815Sobrien /* xgettext:c-format */ 41585815Sobrien sprintf (errbuf, _("bad instruction `%.50s...'"), start); 41685815Sobrien else 41785815Sobrien /* xgettext:c-format */ 41885815Sobrien sprintf (errbuf, _("bad instruction `%.50s'"), start); 41985815Sobrien#endif 42085815Sobrien 42185815Sobrien *errmsg = errbuf; 42285815Sobrien return NULL; 42385815Sobrien } 42485815Sobrien} 425