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 "fr30-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. */
53 /* Handle register lists for LDMx and STMx. */
56 parse_register_number (const char **strp)
60 if (**strp < '0' || **strp > '9')
61 return -1; /* Error. */
65 if (**strp >= '0' && **strp <= '9')
67 regno = regno * 10 + (**strp - '0');
75 parse_register_list (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
77 int opindex ATTRIBUTE_UNUSED,
78 unsigned long *valuep,
79 int high_low, /* 0 == high, 1 == low. */
80 int load_store) /* 0 == load, 1 == store. */
83 while (**strp && **strp != ')')
87 if (**strp != 'R' && **strp != 'r')
91 regno = parse_register_number (strp);
93 return _("Register number is not valid");
94 if (regno > 7 && !high_low)
95 return _("Register must be between r0 and r7");
96 if (regno < 8 && high_low)
97 return _("Register must be between r8 and r15");
102 if (load_store) /* Mask is reversed for store. */
103 *valuep |= 0x80 >> regno;
105 *valuep |= 1 << regno;
109 if (*(*strp + 1) == ')')
115 if (!*strp || **strp != ')')
116 return _("Register list is not valid");
122 parse_low_register_list_ld (CGEN_CPU_DESC cd,
125 unsigned long *valuep)
127 return parse_register_list (cd, strp, opindex, valuep,
128 0 /* Low. */, 0 /* Load. */);
132 parse_hi_register_list_ld (CGEN_CPU_DESC cd,
135 unsigned long *valuep)
137 return parse_register_list (cd, strp, opindex, valuep,
138 1 /* High. */, 0 /* Load. */);
142 parse_low_register_list_st (CGEN_CPU_DESC cd,
145 unsigned long *valuep)
147 return parse_register_list (cd, strp, opindex, valuep,
148 0 /* Low. */, 1 /* Store. */);
152 parse_hi_register_list_st (CGEN_CPU_DESC cd,
155 unsigned long *valuep)
157 return parse_register_list (cd, strp, opindex, valuep,
158 1 /* High. */, 1 /* Store. */);
163 const char * fr30_cgen_parse_operand
164 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
166 /* Main entry point for operand parsing.
168 This function is basically just a big switch statement. Earlier versions
169 used tables to look up the function to use, but
170 - if the table contains both assembler and disassembler functions then
171 the disassembler contains much of the assembler and vice-versa,
172 - there's a lot of inlining possibilities as things grow,
173 - using a switch statement avoids the function call overhead.
175 This function could be moved into `parse_insn_normal', but keeping it
176 separate makes clear the interface between `parse_insn_normal' and each of
180 fr30_cgen_parse_operand (CGEN_CPU_DESC cd,
183 CGEN_FIELDS * fields)
185 const char * errmsg = NULL;
186 /* Used by scalar operands that still need to be parsed. */
187 long junk ATTRIBUTE_UNUSED;
191 case FR30_OPERAND_CRI :
192 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi);
194 case FR30_OPERAND_CRJ :
195 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj);
197 case FR30_OPERAND_R13 :
198 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk);
200 case FR30_OPERAND_R14 :
201 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk);
203 case FR30_OPERAND_R15 :
204 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk);
206 case FR30_OPERAND_RI :
207 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri);
209 case FR30_OPERAND_RIC :
210 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric);
212 case FR30_OPERAND_RJ :
213 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj);
215 case FR30_OPERAND_RJC :
216 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc);
218 case FR30_OPERAND_RS1 :
219 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1);
221 case FR30_OPERAND_RS2 :
222 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2);
224 case FR30_OPERAND_CC :
225 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, (unsigned long *) (& fields->f_cc));
227 case FR30_OPERAND_CCC :
228 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, (unsigned long *) (& fields->f_ccc));
230 case FR30_OPERAND_DIR10 :
231 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, (unsigned long *) (& fields->f_dir10));
233 case FR30_OPERAND_DIR8 :
234 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, (unsigned long *) (& fields->f_dir8));
236 case FR30_OPERAND_DIR9 :
237 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, (unsigned long *) (& fields->f_dir9));
239 case FR30_OPERAND_DISP10 :
240 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, (long *) (& fields->f_disp10));
242 case FR30_OPERAND_DISP8 :
243 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, (long *) (& fields->f_disp8));
245 case FR30_OPERAND_DISP9 :
246 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, (long *) (& fields->f_disp9));
248 case FR30_OPERAND_I20 :
249 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, (unsigned long *) (& fields->f_i20));
251 case FR30_OPERAND_I32 :
252 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, (unsigned long *) (& fields->f_i32));
254 case FR30_OPERAND_I8 :
255 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, (unsigned long *) (& fields->f_i8));
257 case FR30_OPERAND_LABEL12 :
260 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL, & value);
261 fields->f_rel12 = value;
264 case FR30_OPERAND_LABEL9 :
267 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL, & value);
268 fields->f_rel9 = value;
271 case FR30_OPERAND_M4 :
272 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, (long *) (& fields->f_m4));
274 case FR30_OPERAND_PS :
275 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk);
277 case FR30_OPERAND_REGLIST_HI_LD :
278 errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, (unsigned long *) (& fields->f_reglist_hi_ld));
280 case FR30_OPERAND_REGLIST_HI_ST :
281 errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, (unsigned long *) (& fields->f_reglist_hi_st));
283 case FR30_OPERAND_REGLIST_LOW_LD :
284 errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, (unsigned long *) (& fields->f_reglist_low_ld));
286 case FR30_OPERAND_REGLIST_LOW_ST :
287 errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, (unsigned long *) (& fields->f_reglist_low_st));
289 case FR30_OPERAND_S10 :
290 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, (long *) (& fields->f_s10));
292 case FR30_OPERAND_U10 :
293 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, (unsigned long *) (& fields->f_u10));
295 case FR30_OPERAND_U4 :
296 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, (unsigned long *) (& fields->f_u4));
298 case FR30_OPERAND_U4C :
299 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, (unsigned long *) (& fields->f_u4c));
301 case FR30_OPERAND_U8 :
302 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, (unsigned long *) (& fields->f_u8));
304 case FR30_OPERAND_UDISP6 :
305 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, (unsigned long *) (& fields->f_udisp6));
309 /* xgettext:c-format */
310 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
317 cgen_parse_fn * const fr30_cgen_parse_handlers[] =
323 fr30_cgen_init_asm (CGEN_CPU_DESC cd)
325 fr30_cgen_init_opcode_table (cd);
326 fr30_cgen_init_ibld_table (cd);
327 cd->parse_handlers = & fr30_cgen_parse_handlers[0];
328 cd->parse_operand = fr30_cgen_parse_operand;
329 #ifdef CGEN_ASM_INIT_HOOK
336 /* Regex construction routine.
338 This translates an opcode syntax string into a regex string,
339 by replacing any non-character syntax element (such as an
340 opcode) with the pattern '.*'
342 It then compiles the regex and stores it in the opcode, for
343 later use by fr30_cgen_assemble_insn
345 Returns NULL for success, an error message for failure. */
348 fr30_cgen_build_insn_regex (CGEN_INSN *insn)
350 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
351 const char *mnem = CGEN_INSN_MNEMONIC (insn);
352 char rxbuf[CGEN_MAX_RX_ELEMENTS];
354 const CGEN_SYNTAX_CHAR_TYPE *syn;
357 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
359 /* Mnemonics come first in the syntax string. */
360 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
361 return _("missing mnemonic in syntax string");
364 /* Generate a case sensitive regular expression that emulates case
365 insensitive matching in the "C" locale. We cannot generate a case
366 insensitive regular expression because in Turkish locales, 'i' and 'I'
367 are not equal modulo case conversion. */
369 /* Copy the literal mnemonic out of the insn. */
370 for (; *mnem; mnem++)
385 /* Copy any remaining literals from the syntax string into the rx. */
386 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
388 if (CGEN_SYNTAX_CHAR_P (* syn))
390 char c = CGEN_SYNTAX_CHAR (* syn);
394 /* Escape any regex metacharacters in the syntax. */
395 case '.': case '[': case '\\':
396 case '*': case '^': case '$':
398 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
399 case '?': case '{': case '}':
400 case '(': case ')': case '*':
401 case '|': case '+': case ']':
422 /* Replace non-syntax fields with globs. */
428 /* Trailing whitespace ok. */
435 /* But anchor it after that. */
439 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
440 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
448 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
449 regfree ((regex_t *) CGEN_INSN_RX (insn));
450 free (CGEN_INSN_RX (insn));
451 (CGEN_INSN_RX (insn)) = NULL;
457 /* Default insn parser.
459 The syntax string is scanned and operands are parsed and stored in FIELDS.
460 Relocs are queued as we go via other callbacks.
462 ??? Note that this is currently an all-or-nothing parser. If we fail to
463 parse the instruction, we return 0 and the caller will start over from
464 the beginning. Backtracking will be necessary in parsing subexpressions,
465 but that can be handled there. Not handling backtracking here may get
466 expensive in the case of the m68k. Deal with later.
468 Returns NULL for success, an error message for failure. */
471 parse_insn_normal (CGEN_CPU_DESC cd,
472 const CGEN_INSN *insn,
476 /* ??? Runtime added insns not handled yet. */
477 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
478 const char *str = *strp;
481 const CGEN_SYNTAX_CHAR_TYPE * syn;
482 #ifdef CGEN_MNEMONIC_OPERANDS
487 /* For now we assume the mnemonic is first (there are no leading operands).
488 We can parse it without needing to set up operand parsing.
489 GAS's input scrubber will ensure mnemonics are lowercase, but we may
490 not be called from GAS. */
491 p = CGEN_INSN_MNEMONIC (insn);
492 while (*p && TOLOWER (*p) == TOLOWER (*str))
496 return _("unrecognized instruction");
498 #ifndef CGEN_MNEMONIC_OPERANDS
499 if (* str && ! ISSPACE (* str))
500 return _("unrecognized instruction");
503 CGEN_INIT_PARSE (cd);
504 cgen_init_parse_operand (cd);
505 #ifdef CGEN_MNEMONIC_OPERANDS
509 /* We don't check for (*str != '\0') here because we want to parse
510 any trailing fake arguments in the syntax string. */
511 syn = CGEN_SYNTAX_STRING (syntax);
513 /* Mnemonics come first for now, ensure valid string. */
514 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
521 /* Non operand chars must match exactly. */
522 if (CGEN_SYNTAX_CHAR_P (* syn))
524 /* FIXME: While we allow for non-GAS callers above, we assume the
525 first char after the mnemonic part is a space. */
526 /* FIXME: We also take inappropriate advantage of the fact that
527 GAS's input scrubber will remove extraneous blanks. */
528 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
530 #ifdef CGEN_MNEMONIC_OPERANDS
531 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
539 /* Syntax char didn't match. Can't be this insn. */
540 static char msg [80];
542 /* xgettext:c-format */
543 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
544 CGEN_SYNTAX_CHAR(*syn), *str);
549 /* Ran out of input. */
550 static char msg [80];
552 /* xgettext:c-format */
553 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
554 CGEN_SYNTAX_CHAR(*syn));
560 #ifdef CGEN_MNEMONIC_OPERANDS
561 (void) past_opcode_p;
563 /* We have an operand of some sort. */
564 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
568 /* Done with this operand, continue with next one. */
572 /* If we're at the end of the syntax string, we're done. */
575 /* FIXME: For the moment we assume a valid `str' can only contain
576 blanks now. IE: We needn't try again with a longer version of
577 the insn and it is assumed that longer versions of insns appear
578 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
579 while (ISSPACE (* str))
583 return _("junk at end of line"); /* FIXME: would like to include `str' */
588 /* We couldn't parse it. */
589 return _("unrecognized instruction");
593 This routine is called for each instruction to be assembled.
594 STR points to the insn to be assembled.
595 We assume all necessary tables have been initialized.
596 The assembled instruction, less any fixups, is stored in BUF.
597 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
598 still needs to be converted to target byte order, otherwise BUF is an array
599 of bytes in target byte order.
600 The result is a pointer to the insn's entry in the opcode table,
601 or NULL if an error occured (an error message will have already been
604 Note that when processing (non-alias) macro-insns,
605 this function recurses.
607 ??? It's possible to make this cpu-independent.
608 One would have to deal with a few minor things.
609 At this point in time doing so would be more of a curiosity than useful
610 [for example this file isn't _that_ big], but keeping the possibility in
611 mind helps keep the design clean. */
614 fr30_cgen_assemble_insn (CGEN_CPU_DESC cd,
617 CGEN_INSN_BYTES_PTR buf,
621 CGEN_INSN_LIST *ilist;
622 const char *parse_errmsg = NULL;
623 const char *insert_errmsg = NULL;
624 int recognized_mnemonic = 0;
626 /* Skip leading white space. */
627 while (ISSPACE (* str))
630 /* The instructions are stored in hashed lists.
631 Get the first in the list. */
632 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
634 /* Keep looking until we find a match. */
636 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
638 const CGEN_INSN *insn = ilist->insn;
639 recognized_mnemonic = 1;
641 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
642 /* Not usually needed as unsupported opcodes
643 shouldn't be in the hash lists. */
644 /* Is this insn supported by the selected cpu? */
645 if (! fr30_cgen_insn_supported (cd, insn))
648 /* If the RELAXED attribute is set, this is an insn that shouldn't be
649 chosen immediately. Instead, it is used during assembler/linker
650 relaxation if possible. */
651 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
656 /* Skip this insn if str doesn't look right lexically. */
657 if (CGEN_INSN_RX (insn) != NULL &&
658 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
661 /* Allow parse/insert handlers to obtain length of insn. */
662 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
664 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
665 if (parse_errmsg != NULL)
668 /* ??? 0 is passed for `pc'. */
669 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
671 if (insert_errmsg != NULL)
674 /* It is up to the caller to actually output the insn and any
680 static char errbuf[150];
681 const char *tmp_errmsg;
682 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
690 /* If requesting verbose error messages, use insert_errmsg.
691 Failing that, use parse_errmsg. */
692 tmp_errmsg = (insert_errmsg ? insert_errmsg :
693 parse_errmsg ? parse_errmsg :
694 recognized_mnemonic ?
695 _("unrecognized form of instruction") :
696 _("unrecognized instruction"));
698 if (strlen (start) > 50)
699 /* xgettext:c-format */
700 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
702 /* xgettext:c-format */
703 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
707 if (strlen (start) > 50)
708 /* xgettext:c-format */
709 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
711 /* xgettext:c-format */
712 sprintf (errbuf, _("bad instruction `%.50s'"), start);