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 "fr30-desc.h"
40 #define min(a,b) ((a) < (b) ? (a) : (b))
42 #define max(a,b) ((a) > (b) ? (a) : (b))
44 static const char * parse_insn_normal
45 PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *));
47 /* -- assembler routines inserted here */
50 /* Handle register lists for LDMx and STMx */
53 parse_register_number (strp)
57 if (**strp < '0' || **strp > '9')
58 return -1; /* error */
62 if (**strp >= '0' && **strp <= '9')
64 regno = regno * 10 + (**strp - '0');
72 parse_register_list (cd, strp, opindex, valuep, high_low, load_store)
76 unsigned long *valuep;
77 int high_low; /* 0 == high, 1 == low */
78 int load_store; /* 0 == load, 1 == store */
82 while (**strp && **strp != ')')
84 if (**strp != 'R' && **strp != 'r')
88 regno = parse_register_number (strp);
90 return "Register number is not valid";
91 if (regno > 7 && !high_low)
92 return "Register must be between r0 and r7";
93 if (regno < 8 && high_low)
94 return "Register must be between r8 and r15";
99 if (load_store) /* mask is reversed for store */
100 *valuep |= 0x80 >> regno;
102 *valuep |= 1 << regno;
106 if (*(*strp + 1) == ')')
112 if (!*strp || **strp != ')')
113 return "Register list is not valid";
119 parse_low_register_list_ld (cd, strp, opindex, valuep)
123 unsigned long *valuep;
125 return parse_register_list (cd, strp, opindex, valuep, 0/*low*/, 0/*load*/);
129 parse_hi_register_list_ld (cd, strp, opindex, valuep)
133 unsigned long *valuep;
135 return parse_register_list (cd, strp, opindex, valuep, 1/*high*/, 0/*load*/);
139 parse_low_register_list_st (cd, strp, opindex, valuep)
143 unsigned long *valuep;
145 return parse_register_list (cd, strp, opindex, valuep, 0/*low*/, 1/*store*/);
149 parse_hi_register_list_st (cd, strp, opindex, valuep)
153 unsigned long *valuep;
155 return parse_register_list (cd, strp, opindex, valuep, 1/*high*/, 1/*store*/);
160 /* Main entry point for operand parsing.
162 This function is basically just a big switch statement. Earlier versions
163 used tables to look up the function to use, but
164 - if the table contains both assembler and disassembler functions then
165 the disassembler contains much of the assembler and vice-versa,
166 - there's a lot of inlining possibilities as things grow,
167 - using a switch statement avoids the function call overhead.
169 This function could be moved into `parse_insn_normal', but keeping it
170 separate makes clear the interface between `parse_insn_normal' and each of
175 fr30_cgen_parse_operand (cd, opindex, strp, fields)
179 CGEN_FIELDS * fields;
181 const char * errmsg = NULL;
182 /* Used by scalar operands that still need to be parsed. */
187 case FR30_OPERAND_CRI :
188 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi);
190 case FR30_OPERAND_CRJ :
191 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj);
193 case FR30_OPERAND_R13 :
194 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk);
196 case FR30_OPERAND_R14 :
197 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk);
199 case FR30_OPERAND_R15 :
200 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk);
202 case FR30_OPERAND_RI :
203 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri);
205 case FR30_OPERAND_RIC :
206 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric);
208 case FR30_OPERAND_RJ :
209 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj);
211 case FR30_OPERAND_RJC :
212 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc);
214 case FR30_OPERAND_RS1 :
215 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1);
217 case FR30_OPERAND_RS2 :
218 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2);
220 case FR30_OPERAND_CC :
221 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, &fields->f_cc);
223 case FR30_OPERAND_CCC :
224 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, &fields->f_ccc);
226 case FR30_OPERAND_DIR10 :
227 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, &fields->f_dir10);
229 case FR30_OPERAND_DIR8 :
230 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, &fields->f_dir8);
232 case FR30_OPERAND_DIR9 :
233 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, &fields->f_dir9);
235 case FR30_OPERAND_DISP10 :
236 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, &fields->f_disp10);
238 case FR30_OPERAND_DISP8 :
239 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, &fields->f_disp8);
241 case FR30_OPERAND_DISP9 :
242 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, &fields->f_disp9);
244 case FR30_OPERAND_I20 :
245 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, &fields->f_i20);
247 case FR30_OPERAND_I32 :
248 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, &fields->f_i32);
250 case FR30_OPERAND_I8 :
251 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, &fields->f_i8);
253 case FR30_OPERAND_LABEL12 :
256 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL, & value);
257 fields->f_rel12 = value;
260 case FR30_OPERAND_LABEL9 :
263 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL, & value);
264 fields->f_rel9 = value;
267 case FR30_OPERAND_M4 :
268 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, &fields->f_m4);
270 case FR30_OPERAND_PS :
271 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk);
273 case FR30_OPERAND_REGLIST_HI_LD :
274 errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, &fields->f_reglist_hi_ld);
276 case FR30_OPERAND_REGLIST_HI_ST :
277 errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, &fields->f_reglist_hi_st);
279 case FR30_OPERAND_REGLIST_LOW_LD :
280 errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, &fields->f_reglist_low_ld);
282 case FR30_OPERAND_REGLIST_LOW_ST :
283 errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, &fields->f_reglist_low_st);
285 case FR30_OPERAND_S10 :
286 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, &fields->f_s10);
288 case FR30_OPERAND_U10 :
289 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, &fields->f_u10);
291 case FR30_OPERAND_U4 :
292 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, &fields->f_u4);
294 case FR30_OPERAND_U4C :
295 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, &fields->f_u4c);
297 case FR30_OPERAND_U8 :
298 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, &fields->f_u8);
300 case FR30_OPERAND_UDISP6 :
301 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, &fields->f_udisp6);
305 /* xgettext:c-format */
306 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
313 cgen_parse_fn * const fr30_cgen_parse_handlers[] =
319 fr30_cgen_init_asm (cd)
322 fr30_cgen_init_opcode_table (cd);
323 fr30_cgen_init_ibld_table (cd);
324 cd->parse_handlers = & fr30_cgen_parse_handlers[0];
325 cd->parse_operand = fr30_cgen_parse_operand;
331 Regex construction routine.
333 This translates an opcode syntax string into a regex string,
334 by replacing any non-character syntax element (such as an
335 opcode) with the pattern '.*'
337 It then compiles the regex and stores it in the opcode, for
338 later use by fr30_cgen_assemble_insn
340 returns NULL for success, an error message for failure
344 fr30_cgen_build_insn_regex (insn)
347 CGEN_OPCODE *opc = CGEN_INSN_OPCODE (insn);
348 const char *mnem = CGEN_INSN_MNEMONIC (insn);
350 char rxbuf[CGEN_MAX_RX_ELEMENTS];
352 const CGEN_SYNTAX_CHAR_TYPE *syn;
355 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
357 /* Mnemonics come first in the syntax string */
358 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) return "missing mnemonic in syntax string";
361 /* copy the literal mnemonic out of the insn */
362 memset (rx, 0, CGEN_MAX_RX_ELEMENTS);
363 mnem_len = strlen(mnem);
364 memcpy (rx, mnem, mnem_len);
367 /* copy any remaining literals from the syntax string into the rx */
368 for(; * syn != 0 && rx < rxbuf + (CGEN_MAX_RX_ELEMENTS - 9); ++syn, ++rx)
370 if (CGEN_SYNTAX_CHAR_P (* syn))
372 char tmp = CGEN_SYNTAX_CHAR (* syn);
375 /* escape any regex metacharacters in the syntax */
376 case '.': case '[': case '\\':
377 case '*': case '^': case '$':
379 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
380 case '?': case '{': case '}':
381 case '(': case ')': case '*':
382 case '|': case '+': case ']':
388 /* insert syntax char into rx */
393 /* replace non-syntax fields with globs */
399 /* trailing whitespace ok */
406 /* but anchor it after that */
410 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
411 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB|REG_ICASE);
418 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
419 regfree ((regex_t *) CGEN_INSN_RX (insn));
420 free (CGEN_INSN_RX (insn));
421 (CGEN_INSN_RX (insn)) = NULL;
427 /* Default insn parser.
429 The syntax string is scanned and operands are parsed and stored in FIELDS.
430 Relocs are queued as we go via other callbacks.
432 ??? Note that this is currently an all-or-nothing parser. If we fail to
433 parse the instruction, we return 0 and the caller will start over from
434 the beginning. Backtracking will be necessary in parsing subexpressions,
435 but that can be handled there. Not handling backtracking here may get
436 expensive in the case of the m68k. Deal with later.
438 Returns NULL for success, an error message for failure.
442 parse_insn_normal (cd, insn, strp, fields)
444 const CGEN_INSN *insn;
448 /* ??? Runtime added insns not handled yet. */
449 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
450 const char *str = *strp;
453 const CGEN_SYNTAX_CHAR_TYPE * syn;
454 #ifdef CGEN_MNEMONIC_OPERANDS
459 /* For now we assume the mnemonic is first (there are no leading operands).
460 We can parse it without needing to set up operand parsing.
461 GAS's input scrubber will ensure mnemonics are lowercase, but we may
462 not be called from GAS. */
463 p = CGEN_INSN_MNEMONIC (insn);
464 while (*p && tolower (*p) == tolower (*str))
468 return _("unrecognized instruction");
470 #ifndef CGEN_MNEMONIC_OPERANDS
471 if (* str && !isspace (* str))
472 return _("unrecognized instruction");
475 CGEN_INIT_PARSE (cd);
476 cgen_init_parse_operand (cd);
477 #ifdef CGEN_MNEMONIC_OPERANDS
481 /* We don't check for (*str != '\0') here because we want to parse
482 any trailing fake arguments in the syntax string. */
483 syn = CGEN_SYNTAX_STRING (syntax);
485 /* Mnemonics come first for now, ensure valid string. */
486 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
493 /* Non operand chars must match exactly. */
494 if (CGEN_SYNTAX_CHAR_P (* syn))
496 /* FIXME: While we allow for non-GAS callers above, we assume the
497 first char after the mnemonic part is a space. */
498 /* FIXME: We also take inappropriate advantage of the fact that
499 GAS's input scrubber will remove extraneous blanks. */
500 if (tolower (*str) == tolower (CGEN_SYNTAX_CHAR (* syn)))
502 #ifdef CGEN_MNEMONIC_OPERANDS
503 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
511 /* Syntax char didn't match. Can't be this insn. */
512 static char msg [80];
513 /* xgettext:c-format */
514 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
515 CGEN_SYNTAX_CHAR(*syn), *str);
520 /* Ran out of input. */
521 static char msg [80];
522 /* xgettext:c-format */
523 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
524 CGEN_SYNTAX_CHAR(*syn));
530 /* We have an operand of some sort. */
531 errmsg = fr30_cgen_parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
536 /* Done with this operand, continue with next one. */
540 /* If we're at the end of the syntax string, we're done. */
543 /* FIXME: For the moment we assume a valid `str' can only contain
544 blanks now. IE: We needn't try again with a longer version of
545 the insn and it is assumed that longer versions of insns appear
546 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
547 while (isspace (* str))
551 return _("junk at end of line"); /* FIXME: would like to include `str' */
556 /* We couldn't parse it. */
557 return _("unrecognized instruction");
561 This routine is called for each instruction to be assembled.
562 STR points to the insn to be assembled.
563 We assume all necessary tables have been initialized.
564 The assembled instruction, less any fixups, is stored in BUF.
565 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
566 still needs to be converted to target byte order, otherwise BUF is an array
567 of bytes in target byte order.
568 The result is a pointer to the insn's entry in the opcode table,
569 or NULL if an error occured (an error message will have already been
572 Note that when processing (non-alias) macro-insns,
573 this function recurses.
575 ??? It's possible to make this cpu-independent.
576 One would have to deal with a few minor things.
577 At this point in time doing so would be more of a curiosity than useful
578 [for example this file isn't _that_ big], but keeping the possibility in
579 mind helps keep the design clean. */
582 fr30_cgen_assemble_insn (cd, str, fields, buf, errmsg)
586 CGEN_INSN_BYTES_PTR buf;
590 CGEN_INSN_LIST *ilist;
591 const char *parse_errmsg = NULL;
592 const char *insert_errmsg = NULL;
593 int recognized_mnemonic = 0;
595 /* Skip leading white space. */
596 while (isspace (* str))
599 /* The instructions are stored in hashed lists.
600 Get the first in the list. */
601 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
603 /* Keep looking until we find a match. */
606 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
608 const CGEN_INSN *insn = ilist->insn;
609 recognized_mnemonic = 1;
611 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
612 /* not usually needed as unsupported opcodes shouldn't be in the hash lists */
613 /* Is this insn supported by the selected cpu? */
614 if (! fr30_cgen_insn_supported (cd, insn))
618 /* If the RELAX attribute is set, this is an insn that shouldn't be
619 chosen immediately. Instead, it is used during assembler/linker
620 relaxation if possible. */
621 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0)
626 /* skip this insn if str doesn't look right lexically */
627 if (CGEN_INSN_RX (insn) != NULL &&
628 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
631 /* Allow parse/insert handlers to obtain length of insn. */
632 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
634 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
635 if (parse_errmsg != NULL)
638 /* ??? 0 is passed for `pc' */
639 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
641 if (insert_errmsg != NULL)
644 /* It is up to the caller to actually output the insn and any
650 static char errbuf[150];
651 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
652 const char *tmp_errmsg;
654 /* If requesting verbose error messages, use insert_errmsg.
655 Failing that, use parse_errmsg */
656 tmp_errmsg = (insert_errmsg ? insert_errmsg :
657 parse_errmsg ? parse_errmsg :
658 recognized_mnemonic ? _("unrecognized form of instruction") :
659 _("unrecognized instruction"));
661 if (strlen (start) > 50)
662 /* xgettext:c-format */
663 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
665 /* xgettext:c-format */
666 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
668 if (strlen (start) > 50)
669 /* xgettext:c-format */
670 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
672 /* xgettext:c-format */
673 sprintf (errbuf, _("bad instruction `%.50s'"), start);
681 #if 0 /* This calls back to GAS which we can't do without care. */
683 /* Record each member of OPVALS in the assembler's symbol table.
684 This lets GAS parse registers for us.
685 ??? Interesting idea but not currently used. */
687 /* Record each member of OPVALS in the assembler's symbol table.
688 FIXME: Not currently used. */
691 fr30_cgen_asm_hash_keywords (cd, opvals)
693 CGEN_KEYWORD *opvals;
695 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
696 const CGEN_KEYWORD_ENTRY * ke;
698 while ((ke = cgen_keyword_search_next (& search)) != NULL)
700 #if 0 /* Unnecessary, should be done in the search routine. */
701 if (! fr30_cgen_opval_supported (ke))
704 cgen_asm_record_register (cd, ke->name, ke->value);