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, 2007, 2008, 2010
8 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 "@prefix@-desc.h"
36 #include "@prefix@-opc.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 /* Regex construction routine.
55 This translates an opcode syntax string into a regex string,
56 by replacing any non-character syntax element (such as an
57 opcode) with the pattern '.*'
59 It then compiles the regex and stores it in the opcode, for
60 later use by @arch@_cgen_assemble_insn
62 Returns NULL for success, an error message for failure. */
65 @arch@_cgen_build_insn_regex (CGEN_INSN *insn)
67 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
68 const char *mnem = CGEN_INSN_MNEMONIC (insn);
69 char rxbuf[CGEN_MAX_RX_ELEMENTS];
71 const CGEN_SYNTAX_CHAR_TYPE *syn;
74 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
76 /* Mnemonics come first in the syntax string. */
77 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
78 return _("missing mnemonic in syntax string");
81 /* Generate a case sensitive regular expression that emulates case
82 insensitive matching in the "C" locale. We cannot generate a case
83 insensitive regular expression because in Turkish locales, 'i' and 'I'
84 are not equal modulo case conversion. */
86 /* Copy the literal mnemonic out of the insn. */
102 /* Copy any remaining literals from the syntax string into the rx. */
103 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
105 if (CGEN_SYNTAX_CHAR_P (* syn))
107 char c = CGEN_SYNTAX_CHAR (* syn);
111 /* Escape any regex metacharacters in the syntax. */
112 case '.': case '[': case '\\':
113 case '*': case '^': case '$':
115 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
116 case '?': case '{': case '}':
117 case '(': case ')': case '*':
118 case '|': case '+': case ']':
139 /* Replace non-syntax fields with globs. */
145 /* Trailing whitespace ok. */
152 /* But anchor it after that. */
156 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
157 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
165 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
166 regfree ((regex_t *) CGEN_INSN_RX (insn));
167 free (CGEN_INSN_RX (insn));
168 (CGEN_INSN_RX (insn)) = NULL;
174 /* Default insn parser.
176 The syntax string is scanned and operands are parsed and stored in FIELDS.
177 Relocs are queued as we go via other callbacks.
179 ??? Note that this is currently an all-or-nothing parser. If we fail to
180 parse the instruction, we return 0 and the caller will start over from
181 the beginning. Backtracking will be necessary in parsing subexpressions,
182 but that can be handled there. Not handling backtracking here may get
183 expensive in the case of the m68k. Deal with later.
185 Returns NULL for success, an error message for failure. */
188 parse_insn_normal (CGEN_CPU_DESC cd,
189 const CGEN_INSN *insn,
193 /* ??? Runtime added insns not handled yet. */
194 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
195 const char *str = *strp;
198 const CGEN_SYNTAX_CHAR_TYPE * syn;
199 #ifdef CGEN_MNEMONIC_OPERANDS
204 /* For now we assume the mnemonic is first (there are no leading operands).
205 We can parse it without needing to set up operand parsing.
206 GAS's input scrubber will ensure mnemonics are lowercase, but we may
207 not be called from GAS. */
208 p = CGEN_INSN_MNEMONIC (insn);
209 while (*p && TOLOWER (*p) == TOLOWER (*str))
213 return _("unrecognized instruction");
215 #ifndef CGEN_MNEMONIC_OPERANDS
216 if (* str && ! ISSPACE (* str))
217 return _("unrecognized instruction");
220 CGEN_INIT_PARSE (cd);
221 cgen_init_parse_operand (cd);
222 #ifdef CGEN_MNEMONIC_OPERANDS
226 /* We don't check for (*str != '\0') here because we want to parse
227 any trailing fake arguments in the syntax string. */
228 syn = CGEN_SYNTAX_STRING (syntax);
230 /* Mnemonics come first for now, ensure valid string. */
231 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
238 /* Non operand chars must match exactly. */
239 if (CGEN_SYNTAX_CHAR_P (* syn))
241 /* FIXME: While we allow for non-GAS callers above, we assume the
242 first char after the mnemonic part is a space. */
243 /* FIXME: We also take inappropriate advantage of the fact that
244 GAS's input scrubber will remove extraneous blanks. */
245 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
247 #ifdef CGEN_MNEMONIC_OPERANDS
248 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
256 /* Syntax char didn't match. Can't be this insn. */
257 static char msg [80];
259 /* xgettext:c-format */
260 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
261 CGEN_SYNTAX_CHAR(*syn), *str);
266 /* Ran out of input. */
267 static char msg [80];
269 /* xgettext:c-format */
270 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
271 CGEN_SYNTAX_CHAR(*syn));
277 /* We have an operand of some sort. */
278 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
283 /* Done with this operand, continue with next one. */
287 /* If we're at the end of the syntax string, we're done. */
290 /* FIXME: For the moment we assume a valid `str' can only contain
291 blanks now. IE: We needn't try again with a longer version of
292 the insn and it is assumed that longer versions of insns appear
293 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
294 while (ISSPACE (* str))
298 return _("junk at end of line"); /* FIXME: would like to include `str' */
303 /* We couldn't parse it. */
304 return _("unrecognized instruction");
308 This routine is called for each instruction to be assembled.
309 STR points to the insn to be assembled.
310 We assume all necessary tables have been initialized.
311 The assembled instruction, less any fixups, is stored in BUF.
312 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
313 still needs to be converted to target byte order, otherwise BUF is an array
314 of bytes in target byte order.
315 The result is a pointer to the insn's entry in the opcode table,
316 or NULL if an error occured (an error message will have already been
319 Note that when processing (non-alias) macro-insns,
320 this function recurses.
322 ??? It's possible to make this cpu-independent.
323 One would have to deal with a few minor things.
324 At this point in time doing so would be more of a curiosity than useful
325 [for example this file isn't _that_ big], but keeping the possibility in
326 mind helps keep the design clean. */
329 @arch@_cgen_assemble_insn (CGEN_CPU_DESC cd,
332 CGEN_INSN_BYTES_PTR buf,
336 CGEN_INSN_LIST *ilist;
337 const char *parse_errmsg = NULL;
338 const char *insert_errmsg = NULL;
339 int recognized_mnemonic = 0;
341 /* Skip leading white space. */
342 while (ISSPACE (* str))
345 /* The instructions are stored in hashed lists.
346 Get the first in the list. */
347 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
349 /* Keep looking until we find a match. */
351 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
353 const CGEN_INSN *insn = ilist->insn;
354 recognized_mnemonic = 1;
356 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
357 /* Not usually needed as unsupported opcodes
358 shouldn't be in the hash lists. */
359 /* Is this insn supported by the selected cpu? */
360 if (! @arch@_cgen_insn_supported (cd, insn))
363 /* If the RELAXED attribute is set, this is an insn that shouldn't be
364 chosen immediately. Instead, it is used during assembler/linker
365 relaxation if possible. */
366 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
371 /* Skip this insn if str doesn't look right lexically. */
372 if (CGEN_INSN_RX (insn) != NULL &&
373 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
376 /* Allow parse/insert handlers to obtain length of insn. */
377 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
379 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
380 if (parse_errmsg != NULL)
383 /* ??? 0 is passed for `pc'. */
384 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
386 if (insert_errmsg != NULL)
389 /* It is up to the caller to actually output the insn and any
395 static char errbuf[150];
396 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
397 const char *tmp_errmsg;
399 /* If requesting verbose error messages, use insert_errmsg.
400 Failing that, use parse_errmsg. */
401 tmp_errmsg = (insert_errmsg ? insert_errmsg :
402 parse_errmsg ? parse_errmsg :
403 recognized_mnemonic ?
404 _("unrecognized form of instruction") :
405 _("unrecognized instruction"));
407 if (strlen (start) > 50)
408 /* xgettext:c-format */
409 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
411 /* xgettext:c-format */
412 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
414 if (strlen (start) > 50)
415 /* xgettext:c-format */
416 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
418 /* xgettext:c-format */
419 sprintf (errbuf, _("bad instruction `%.50s'"), start);