1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS MACHINE GENERATED WITH CGEN.
5 - the resultant file is machine generated, cgen-asm.in isn't
7 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005
8 Free Software Foundation, Inc.
10 This file is part of the GNU Binutils and GDB, the GNU debugger.
12 This program 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 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public 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. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
34 #include "@prefix@-desc.h"
35 #include "@prefix@-opc.h"
38 #include "libiberty.h"
39 #include "safe-ctype.h"
42 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #define max(a,b) ((a) > (b) ? (a) : (b))
46 static const char * parse_insn_normal
47 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
49 /* -- assembler routines inserted here. */
52 /* Regex construction routine.
54 This translates an opcode syntax string into a regex string,
55 by replacing any non-character syntax element (such as an
56 opcode) with the pattern '.*'
58 It then compiles the regex and stores it in the opcode, for
59 later use by @arch@_cgen_assemble_insn
61 Returns NULL for success, an error message for failure. */
64 @arch@_cgen_build_insn_regex (CGEN_INSN *insn)
66 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
67 const char *mnem = CGEN_INSN_MNEMONIC (insn);
68 char rxbuf[CGEN_MAX_RX_ELEMENTS];
70 const CGEN_SYNTAX_CHAR_TYPE *syn;
73 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
75 /* Mnemonics come first in the syntax string. */
76 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
77 return _("missing mnemonic in syntax string");
80 /* Generate a case sensitive regular expression that emulates case
81 insensitive matching in the "C" locale. We cannot generate a case
82 insensitive regular expression because in Turkish locales, 'i' and 'I'
83 are not equal modulo case conversion. */
85 /* Copy the literal mnemonic out of the insn. */
101 /* Copy any remaining literals from the syntax string into the rx. */
102 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
104 if (CGEN_SYNTAX_CHAR_P (* syn))
106 char c = CGEN_SYNTAX_CHAR (* syn);
110 /* Escape any regex metacharacters in the syntax. */
111 case '.': case '[': case '\\':
112 case '*': case '^': case '$':
114 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
115 case '?': case '{': case '}':
116 case '(': case ')': case '*':
117 case '|': case '+': case ']':
138 /* Replace non-syntax fields with globs. */
144 /* Trailing whitespace ok. */
151 /* But anchor it after that. */
155 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
156 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
164 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
165 regfree ((regex_t *) CGEN_INSN_RX (insn));
166 free (CGEN_INSN_RX (insn));
167 (CGEN_INSN_RX (insn)) = NULL;
173 /* Default insn parser.
175 The syntax string is scanned and operands are parsed and stored in FIELDS.
176 Relocs are queued as we go via other callbacks.
178 ??? Note that this is currently an all-or-nothing parser. If we fail to
179 parse the instruction, we return 0 and the caller will start over from
180 the beginning. Backtracking will be necessary in parsing subexpressions,
181 but that can be handled there. Not handling backtracking here may get
182 expensive in the case of the m68k. Deal with later.
184 Returns NULL for success, an error message for failure. */
187 parse_insn_normal (CGEN_CPU_DESC cd,
188 const CGEN_INSN *insn,
192 /* ??? Runtime added insns not handled yet. */
193 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
194 const char *str = *strp;
197 const CGEN_SYNTAX_CHAR_TYPE * syn;
198 #ifdef CGEN_MNEMONIC_OPERANDS
203 /* For now we assume the mnemonic is first (there are no leading operands).
204 We can parse it without needing to set up operand parsing.
205 GAS's input scrubber will ensure mnemonics are lowercase, but we may
206 not be called from GAS. */
207 p = CGEN_INSN_MNEMONIC (insn);
208 while (*p && TOLOWER (*p) == TOLOWER (*str))
212 return _("unrecognized instruction");
214 #ifndef CGEN_MNEMONIC_OPERANDS
215 if (* str && ! ISSPACE (* str))
216 return _("unrecognized instruction");
219 CGEN_INIT_PARSE (cd);
220 cgen_init_parse_operand (cd);
221 #ifdef CGEN_MNEMONIC_OPERANDS
225 /* We don't check for (*str != '\0') here because we want to parse
226 any trailing fake arguments in the syntax string. */
227 syn = CGEN_SYNTAX_STRING (syntax);
229 /* Mnemonics come first for now, ensure valid string. */
230 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
237 /* Non operand chars must match exactly. */
238 if (CGEN_SYNTAX_CHAR_P (* syn))
240 /* FIXME: While we allow for non-GAS callers above, we assume the
241 first char after the mnemonic part is a space. */
242 /* FIXME: We also take inappropriate advantage of the fact that
243 GAS's input scrubber will remove extraneous blanks. */
244 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
246 #ifdef CGEN_MNEMONIC_OPERANDS
247 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
255 /* Syntax char didn't match. Can't be this insn. */
256 static char msg [80];
258 /* xgettext:c-format */
259 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
260 CGEN_SYNTAX_CHAR(*syn), *str);
265 /* Ran out of input. */
266 static char msg [80];
268 /* xgettext:c-format */
269 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
270 CGEN_SYNTAX_CHAR(*syn));
276 /* We have an operand of some sort. */
277 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
282 /* Done with this operand, continue with next one. */
286 /* If we're at the end of the syntax string, we're done. */
289 /* FIXME: For the moment we assume a valid `str' can only contain
290 blanks now. IE: We needn't try again with a longer version of
291 the insn and it is assumed that longer versions of insns appear
292 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
293 while (ISSPACE (* str))
297 return _("junk at end of line"); /* FIXME: would like to include `str' */
302 /* We couldn't parse it. */
303 return _("unrecognized instruction");
307 This routine is called for each instruction to be assembled.
308 STR points to the insn to be assembled.
309 We assume all necessary tables have been initialized.
310 The assembled instruction, less any fixups, is stored in BUF.
311 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
312 still needs to be converted to target byte order, otherwise BUF is an array
313 of bytes in target byte order.
314 The result is a pointer to the insn's entry in the opcode table,
315 or NULL if an error occured (an error message will have already been
318 Note that when processing (non-alias) macro-insns,
319 this function recurses.
321 ??? It's possible to make this cpu-independent.
322 One would have to deal with a few minor things.
323 At this point in time doing so would be more of a curiosity than useful
324 [for example this file isn't _that_ big], but keeping the possibility in
325 mind helps keep the design clean. */
328 @arch@_cgen_assemble_insn (CGEN_CPU_DESC cd,
331 CGEN_INSN_BYTES_PTR buf,
335 CGEN_INSN_LIST *ilist;
336 const char *parse_errmsg = NULL;
337 const char *insert_errmsg = NULL;
338 int recognized_mnemonic = 0;
340 /* Skip leading white space. */
341 while (ISSPACE (* str))
344 /* The instructions are stored in hashed lists.
345 Get the first in the list. */
346 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
348 /* Keep looking until we find a match. */
350 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
352 const CGEN_INSN *insn = ilist->insn;
353 recognized_mnemonic = 1;
355 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
356 /* Not usually needed as unsupported opcodes
357 shouldn't be in the hash lists. */
358 /* Is this insn supported by the selected cpu? */
359 if (! @arch@_cgen_insn_supported (cd, insn))
362 /* If the RELAXED attribute is set, this is an insn that shouldn't be
363 chosen immediately. Instead, it is used during assembler/linker
364 relaxation if possible. */
365 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
370 /* Skip this insn if str doesn't look right lexically. */
371 if (CGEN_INSN_RX (insn) != NULL &&
372 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
375 /* Allow parse/insert handlers to obtain length of insn. */
376 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
378 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
379 if (parse_errmsg != NULL)
382 /* ??? 0 is passed for `pc'. */
383 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
385 if (insert_errmsg != NULL)
388 /* It is up to the caller to actually output the insn and any
394 static char errbuf[150];
395 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
396 const char *tmp_errmsg;
398 /* If requesting verbose error messages, use insert_errmsg.
399 Failing that, use parse_errmsg. */
400 tmp_errmsg = (insert_errmsg ? insert_errmsg :
401 parse_errmsg ? parse_errmsg :
402 recognized_mnemonic ?
403 _("unrecognized form of instruction") :
404 _("unrecognized instruction"));
406 if (strlen (start) > 50)
407 /* xgettext:c-format */
408 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
410 /* xgettext:c-format */
411 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
413 if (strlen (start) > 50)
414 /* xgettext:c-format */
415 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
417 /* xgettext:c-format */
418 sprintf (errbuf, _("bad instruction `%.50s'"), start);