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 Free Software Foundation, Inc.
9 This file is part of the GNU Binutils and GDB, the GNU debugger.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* ??? 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"
41 #define min(a,b) ((a) < (b) ? (a) : (b))
43 #define max(a,b) ((a) > (b) ? (a) : (b))
45 static const char * parse_insn_normal
46 PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *));
48 /* -- 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 (insn)
67 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
68 const char *mnem = CGEN_INSN_MNEMONIC (insn);
70 char rxbuf[CGEN_MAX_RX_ELEMENTS];
72 const CGEN_SYNTAX_CHAR_TYPE *syn;
75 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
77 /* Mnemonics come first in the syntax string. */
78 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
79 return _("missing mnemonic in syntax string");
82 /* Generate a case sensitive regular expression that emulates case
83 insensitive matching in the "C" locale. We cannot generate a case
84 insensitive regular expression because in Turkish locales, 'i' and 'I'
85 are not equal modulo case conversion. */
87 /* Copy the literal mnemonic out of the insn. */
103 /* Copy any remaining literals from the syntax string into the rx. */
104 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
106 if (CGEN_SYNTAX_CHAR_P (* syn))
108 char c = CGEN_SYNTAX_CHAR (* syn);
112 /* Escape any regex metacharacters in the syntax. */
113 case '.': case '[': case '\\':
114 case '*': case '^': case '$':
116 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
117 case '?': case '{': case '}':
118 case '(': case ')': case '*':
119 case '|': case '+': case ']':
138 /* Insert syntax char into rx. */
143 /* Replace non-syntax fields with globs. */
149 /* Trailing whitespace ok. */
156 /* But anchor it after that. */
160 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
161 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
169 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
170 regfree ((regex_t *) CGEN_INSN_RX (insn));
171 free (CGEN_INSN_RX (insn));
172 (CGEN_INSN_RX (insn)) = NULL;
178 /* Default insn parser.
180 The syntax string is scanned and operands are parsed and stored in FIELDS.
181 Relocs are queued as we go via other callbacks.
183 ??? Note that this is currently an all-or-nothing parser. If we fail to
184 parse the instruction, we return 0 and the caller will start over from
185 the beginning. Backtracking will be necessary in parsing subexpressions,
186 but that can be handled there. Not handling backtracking here may get
187 expensive in the case of the m68k. Deal with later.
189 Returns NULL for success, an error message for failure. */
192 parse_insn_normal (cd, insn, strp, fields)
194 const CGEN_INSN *insn;
198 /* ??? Runtime added insns not handled yet. */
199 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
200 const char *str = *strp;
203 const CGEN_SYNTAX_CHAR_TYPE * syn;
204 #ifdef CGEN_MNEMONIC_OPERANDS
209 /* For now we assume the mnemonic is first (there are no leading operands).
210 We can parse it without needing to set up operand parsing.
211 GAS's input scrubber will ensure mnemonics are lowercase, but we may
212 not be called from GAS. */
213 p = CGEN_INSN_MNEMONIC (insn);
214 while (*p && tolower (*p) == tolower (*str))
218 return _("unrecognized instruction");
220 #ifndef CGEN_MNEMONIC_OPERANDS
221 if (* str && !isspace (* str))
222 return _("unrecognized instruction");
225 CGEN_INIT_PARSE (cd);
226 cgen_init_parse_operand (cd);
227 #ifdef CGEN_MNEMONIC_OPERANDS
231 /* We don't check for (*str != '\0') here because we want to parse
232 any trailing fake arguments in the syntax string. */
233 syn = CGEN_SYNTAX_STRING (syntax);
235 /* Mnemonics come first for now, ensure valid string. */
236 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
243 /* Non operand chars must match exactly. */
244 if (CGEN_SYNTAX_CHAR_P (* syn))
246 /* FIXME: While we allow for non-GAS callers above, we assume the
247 first char after the mnemonic part is a space. */
248 /* FIXME: We also take inappropriate advantage of the fact that
249 GAS's input scrubber will remove extraneous blanks. */
250 if (tolower (*str) == tolower (CGEN_SYNTAX_CHAR (* syn)))
252 #ifdef CGEN_MNEMONIC_OPERANDS
253 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
261 /* Syntax char didn't match. Can't be this insn. */
262 static char msg [80];
264 /* xgettext:c-format */
265 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
266 CGEN_SYNTAX_CHAR(*syn), *str);
271 /* Ran out of input. */
272 static char msg [80];
274 /* xgettext:c-format */
275 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
276 CGEN_SYNTAX_CHAR(*syn));
282 /* We have an operand of some sort. */
283 errmsg = @arch@_cgen_parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
288 /* Done with this operand, continue with next one. */
292 /* If we're at the end of the syntax string, we're done. */
295 /* FIXME: For the moment we assume a valid `str' can only contain
296 blanks now. IE: We needn't try again with a longer version of
297 the insn and it is assumed that longer versions of insns appear
298 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
299 while (isspace (* str))
303 return _("junk at end of line"); /* FIXME: would like to include `str' */
308 /* We couldn't parse it. */
309 return _("unrecognized instruction");
313 This routine is called for each instruction to be assembled.
314 STR points to the insn to be assembled.
315 We assume all necessary tables have been initialized.
316 The assembled instruction, less any fixups, is stored in BUF.
317 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
318 still needs to be converted to target byte order, otherwise BUF is an array
319 of bytes in target byte order.
320 The result is a pointer to the insn's entry in the opcode table,
321 or NULL if an error occured (an error message will have already been
324 Note that when processing (non-alias) macro-insns,
325 this function recurses.
327 ??? It's possible to make this cpu-independent.
328 One would have to deal with a few minor things.
329 At this point in time doing so would be more of a curiosity than useful
330 [for example this file isn't _that_ big], but keeping the possibility in
331 mind helps keep the design clean. */
334 @arch@_cgen_assemble_insn (cd, str, fields, buf, errmsg)
338 CGEN_INSN_BYTES_PTR buf;
342 CGEN_INSN_LIST *ilist;
343 const char *parse_errmsg = NULL;
344 const char *insert_errmsg = NULL;
345 int recognized_mnemonic = 0;
347 /* Skip leading white space. */
348 while (isspace (* str))
351 /* The instructions are stored in hashed lists.
352 Get the first in the list. */
353 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
355 /* Keep looking until we find a match. */
357 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
359 const CGEN_INSN *insn = ilist->insn;
360 recognized_mnemonic = 1;
362 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
363 /* Not usually needed as unsupported opcodes
364 shouldn't be in the hash lists. */
365 /* Is this insn supported by the selected cpu? */
366 if (! @arch@_cgen_insn_supported (cd, insn))
369 /* If the RELAX attribute is set, this is an insn that shouldn't be
370 chosen immediately. Instead, it is used during assembler/linker
371 relaxation if possible. */
372 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0)
377 /* Skip this insn if str doesn't look right lexically. */
378 if (CGEN_INSN_RX (insn) != NULL &&
379 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
382 /* Allow parse/insert handlers to obtain length of insn. */
383 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
385 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
386 if (parse_errmsg != NULL)
389 /* ??? 0 is passed for `pc'. */
390 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
392 if (insert_errmsg != NULL)
395 /* It is up to the caller to actually output the insn and any
401 static char errbuf[150];
402 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
403 const char *tmp_errmsg;
405 /* If requesting verbose error messages, use insert_errmsg.
406 Failing that, use parse_errmsg. */
407 tmp_errmsg = (insert_errmsg ? insert_errmsg :
408 parse_errmsg ? parse_errmsg :
409 recognized_mnemonic ?
410 _("unrecognized form of instruction") :
411 _("unrecognized instruction"));
413 if (strlen (start) > 50)
414 /* xgettext:c-format */
415 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
417 /* xgettext:c-format */
418 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
420 if (strlen (start) > 50)
421 /* xgettext:c-format */
422 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
424 /* xgettext:c-format */
425 sprintf (errbuf, _("bad instruction `%.50s'"), start);
433 #if 0 /* This calls back to GAS which we can't do without care. */
435 /* Record each member of OPVALS in the assembler's symbol table.
436 This lets GAS parse registers for us.
437 ??? Interesting idea but not currently used. */
439 /* Record each member of OPVALS in the assembler's symbol table.
440 FIXME: Not currently used. */
443 @arch@_cgen_asm_hash_keywords (cd, opvals)
445 CGEN_KEYWORD *opvals;
447 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
448 const CGEN_KEYWORD_ENTRY * ke;
450 while ((ke = cgen_keyword_search_next (& search)) != NULL)
452 #if 0 /* Unnecessary, should be done in the search routine. */
453 if (! @arch@_cgen_opval_supported (ke))
456 cgen_asm_record_register (cd, ke->name, ke->value);