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"
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 */
51 /* Handle register lists for LDMx and STMx. */
53 static int parse_register_number
54 PARAMS ((const char **));
55 static const char * parse_register_list
56 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *, int, int));
57 static const char * parse_low_register_list_ld
58 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
59 static const char * parse_hi_register_list_ld
60 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
61 static const char * parse_low_register_list_st
62 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
63 static const char * parse_hi_register_list_st
64 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
67 parse_register_number (strp
)
71 if (**strp
< '0' || **strp
> '9')
72 return -1; /* error. */
76 if (**strp
>= '0' && **strp
<= '9')
78 regno
= regno
* 10 + (**strp
- '0');
86 parse_register_list (cd
, strp
, opindex
, valuep
, high_low
, load_store
)
87 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
89 int opindex ATTRIBUTE_UNUSED
;
90 unsigned long *valuep
;
91 int high_low
; /* 0 == high, 1 == low */
92 int load_store
; /* 0 == load, 1 == store */
97 while (**strp
&& **strp
!= ')')
99 if (**strp
!= 'R' && **strp
!= 'r')
103 regno
= parse_register_number (strp
);
105 return "Register number is not valid";
106 if (regno
> 7 && !high_low
)
107 return "Register must be between r0 and r7";
108 if (regno
< 8 && high_low
)
109 return "Register must be between r8 and r15";
114 if (load_store
) /* Mask is reversed for store. */
115 *valuep
|= 0x80 >> regno
;
117 *valuep
|= 1 << regno
;
121 if (*(*strp
+ 1) == ')')
127 if (!*strp
|| **strp
!= ')')
128 return "Register list is not valid";
134 parse_low_register_list_ld (cd
, strp
, opindex
, valuep
)
138 unsigned long *valuep
;
140 return parse_register_list (cd
, strp
, opindex
, valuep
, 0/*low*/, 0/*load*/);
144 parse_hi_register_list_ld (cd
, strp
, opindex
, valuep
)
148 unsigned long *valuep
;
150 return parse_register_list (cd
, strp
, opindex
, valuep
, 1/*high*/, 0/*load*/);
154 parse_low_register_list_st (cd
, strp
, opindex
, valuep
)
158 unsigned long *valuep
;
160 return parse_register_list (cd
, strp
, opindex
, valuep
, 0/*low*/, 1/*store*/);
164 parse_hi_register_list_st (cd
, strp
, opindex
, valuep
)
168 unsigned long *valuep
;
170 return parse_register_list (cd
, strp
, opindex
, valuep
, 1/*high*/, 1/*store*/);
175 const char * fr30_cgen_parse_operand
176 PARAMS ((CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*));
178 /* Main entry point for operand parsing.
180 This function is basically just a big switch statement. Earlier versions
181 used tables to look up the function to use, but
182 - if the table contains both assembler and disassembler functions then
183 the disassembler contains much of the assembler and vice-versa,
184 - there's a lot of inlining possibilities as things grow,
185 - using a switch statement avoids the function call overhead.
187 This function could be moved into `parse_insn_normal', but keeping it
188 separate makes clear the interface between `parse_insn_normal' and each of
193 fr30_cgen_parse_operand (cd
, opindex
, strp
, fields
)
197 CGEN_FIELDS
* fields
;
199 const char * errmsg
= NULL
;
200 /* Used by scalar operands that still need to be parsed. */
201 long junk ATTRIBUTE_UNUSED
;
205 case FR30_OPERAND_CRI
:
206 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_cr_names
, & fields
->f_CRi
);
208 case FR30_OPERAND_CRJ
:
209 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_cr_names
, & fields
->f_CRj
);
211 case FR30_OPERAND_R13
:
212 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_h_r13
, & junk
);
214 case FR30_OPERAND_R14
:
215 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_h_r14
, & junk
);
217 case FR30_OPERAND_R15
:
218 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_h_r15
, & junk
);
220 case FR30_OPERAND_RI
:
221 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_gr_names
, & fields
->f_Ri
);
223 case FR30_OPERAND_RIC
:
224 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_gr_names
, & fields
->f_Ric
);
226 case FR30_OPERAND_RJ
:
227 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_gr_names
, & fields
->f_Rj
);
229 case FR30_OPERAND_RJC
:
230 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_gr_names
, & fields
->f_Rjc
);
232 case FR30_OPERAND_RS1
:
233 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_dr_names
, & fields
->f_Rs1
);
235 case FR30_OPERAND_RS2
:
236 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_dr_names
, & fields
->f_Rs2
);
238 case FR30_OPERAND_CC
:
239 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_CC
, &fields
->f_cc
);
241 case FR30_OPERAND_CCC
:
242 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_CCC
, &fields
->f_ccc
);
244 case FR30_OPERAND_DIR10
:
245 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_DIR10
, &fields
->f_dir10
);
247 case FR30_OPERAND_DIR8
:
248 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_DIR8
, &fields
->f_dir8
);
250 case FR30_OPERAND_DIR9
:
251 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_DIR9
, &fields
->f_dir9
);
253 case FR30_OPERAND_DISP10
:
254 errmsg
= cgen_parse_signed_integer (cd
, strp
, FR30_OPERAND_DISP10
, &fields
->f_disp10
);
256 case FR30_OPERAND_DISP8
:
257 errmsg
= cgen_parse_signed_integer (cd
, strp
, FR30_OPERAND_DISP8
, &fields
->f_disp8
);
259 case FR30_OPERAND_DISP9
:
260 errmsg
= cgen_parse_signed_integer (cd
, strp
, FR30_OPERAND_DISP9
, &fields
->f_disp9
);
262 case FR30_OPERAND_I20
:
263 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_I20
, &fields
->f_i20
);
265 case FR30_OPERAND_I32
:
266 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_I32
, &fields
->f_i32
);
268 case FR30_OPERAND_I8
:
269 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_I8
, &fields
->f_i8
);
271 case FR30_OPERAND_LABEL12
:
274 errmsg
= cgen_parse_address (cd
, strp
, FR30_OPERAND_LABEL12
, 0, NULL
, & value
);
275 fields
->f_rel12
= value
;
278 case FR30_OPERAND_LABEL9
:
281 errmsg
= cgen_parse_address (cd
, strp
, FR30_OPERAND_LABEL9
, 0, NULL
, & value
);
282 fields
->f_rel9
= value
;
285 case FR30_OPERAND_M4
:
286 errmsg
= cgen_parse_signed_integer (cd
, strp
, FR30_OPERAND_M4
, &fields
->f_m4
);
288 case FR30_OPERAND_PS
:
289 errmsg
= cgen_parse_keyword (cd
, strp
, & fr30_cgen_opval_h_ps
, & junk
);
291 case FR30_OPERAND_REGLIST_HI_LD
:
292 errmsg
= parse_hi_register_list_ld (cd
, strp
, FR30_OPERAND_REGLIST_HI_LD
, &fields
->f_reglist_hi_ld
);
294 case FR30_OPERAND_REGLIST_HI_ST
:
295 errmsg
= parse_hi_register_list_st (cd
, strp
, FR30_OPERAND_REGLIST_HI_ST
, &fields
->f_reglist_hi_st
);
297 case FR30_OPERAND_REGLIST_LOW_LD
:
298 errmsg
= parse_low_register_list_ld (cd
, strp
, FR30_OPERAND_REGLIST_LOW_LD
, &fields
->f_reglist_low_ld
);
300 case FR30_OPERAND_REGLIST_LOW_ST
:
301 errmsg
= parse_low_register_list_st (cd
, strp
, FR30_OPERAND_REGLIST_LOW_ST
, &fields
->f_reglist_low_st
);
303 case FR30_OPERAND_S10
:
304 errmsg
= cgen_parse_signed_integer (cd
, strp
, FR30_OPERAND_S10
, &fields
->f_s10
);
306 case FR30_OPERAND_U10
:
307 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_U10
, &fields
->f_u10
);
309 case FR30_OPERAND_U4
:
310 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_U4
, &fields
->f_u4
);
312 case FR30_OPERAND_U4C
:
313 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_U4C
, &fields
->f_u4c
);
315 case FR30_OPERAND_U8
:
316 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_U8
, &fields
->f_u8
);
318 case FR30_OPERAND_UDISP6
:
319 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, FR30_OPERAND_UDISP6
, &fields
->f_udisp6
);
323 /* xgettext:c-format */
324 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
331 cgen_parse_fn
* const fr30_cgen_parse_handlers
[] =
337 fr30_cgen_init_asm (cd
)
340 fr30_cgen_init_opcode_table (cd
);
341 fr30_cgen_init_ibld_table (cd
);
342 cd
->parse_handlers
= & fr30_cgen_parse_handlers
[0];
343 cd
->parse_operand
= fr30_cgen_parse_operand
;
349 Regex construction routine.
351 This translates an opcode syntax string into a regex string,
352 by replacing any non-character syntax element (such as an
353 opcode) with the pattern '.*'
355 It then compiles the regex and stores it in the opcode, for
356 later use by fr30_cgen_assemble_insn
358 Returns NULL for success, an error message for failure. */
361 fr30_cgen_build_insn_regex (insn
)
364 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
365 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
367 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
369 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
372 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
374 /* Mnemonics come first in the syntax string. */
375 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
376 return _("missing mnemonic in syntax string");
379 /* Generate a case sensitive regular expression that emulates case
380 insensitive matching in the "C" locale. We cannot generate a case
381 insensitive regular expression because in Turkish locales, 'i' and 'I'
382 are not equal modulo case conversion. */
384 /* Copy the literal mnemonic out of the insn. */
385 for (; *mnem
; mnem
++)
400 /* Copy any remaining literals from the syntax string into the rx. */
401 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
403 if (CGEN_SYNTAX_CHAR_P (* syn
))
405 char c
= CGEN_SYNTAX_CHAR (* syn
);
409 /* Escape any regex metacharacters in the syntax. */
410 case '.': case '[': case '\\':
411 case '*': case '^': case '$':
413 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
414 case '?': case '{': case '}':
415 case '(': case ')': case '*':
416 case '|': case '+': case ']':
435 /* Insert syntax char into rx. */
440 /* Replace non-syntax fields with globs. */
446 /* Trailing whitespace ok. */
453 /* But anchor it after that. */
457 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
458 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
466 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
467 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
468 free (CGEN_INSN_RX (insn
));
469 (CGEN_INSN_RX (insn
)) = NULL
;
475 /* Default insn parser.
477 The syntax string is scanned and operands are parsed and stored in FIELDS.
478 Relocs are queued as we go via other callbacks.
480 ??? Note that this is currently an all-or-nothing parser. If we fail to
481 parse the instruction, we return 0 and the caller will start over from
482 the beginning. Backtracking will be necessary in parsing subexpressions,
483 but that can be handled there. Not handling backtracking here may get
484 expensive in the case of the m68k. Deal with later.
486 Returns NULL for success, an error message for failure. */
489 parse_insn_normal (cd
, insn
, strp
, fields
)
491 const CGEN_INSN
*insn
;
495 /* ??? Runtime added insns not handled yet. */
496 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
497 const char *str
= *strp
;
500 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
501 #ifdef CGEN_MNEMONIC_OPERANDS
506 /* For now we assume the mnemonic is first (there are no leading operands).
507 We can parse it without needing to set up operand parsing.
508 GAS's input scrubber will ensure mnemonics are lowercase, but we may
509 not be called from GAS. */
510 p
= CGEN_INSN_MNEMONIC (insn
);
511 while (*p
&& tolower (*p
) == tolower (*str
))
515 return _("unrecognized instruction");
517 #ifndef CGEN_MNEMONIC_OPERANDS
518 if (* str
&& !isspace (* str
))
519 return _("unrecognized instruction");
522 CGEN_INIT_PARSE (cd
);
523 cgen_init_parse_operand (cd
);
524 #ifdef CGEN_MNEMONIC_OPERANDS
528 /* We don't check for (*str != '\0') here because we want to parse
529 any trailing fake arguments in the syntax string. */
530 syn
= CGEN_SYNTAX_STRING (syntax
);
532 /* Mnemonics come first for now, ensure valid string. */
533 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
540 /* Non operand chars must match exactly. */
541 if (CGEN_SYNTAX_CHAR_P (* syn
))
543 /* FIXME: While we allow for non-GAS callers above, we assume the
544 first char after the mnemonic part is a space. */
545 /* FIXME: We also take inappropriate advantage of the fact that
546 GAS's input scrubber will remove extraneous blanks. */
547 if (tolower (*str
) == tolower (CGEN_SYNTAX_CHAR (* syn
)))
549 #ifdef CGEN_MNEMONIC_OPERANDS
550 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
558 /* Syntax char didn't match. Can't be this insn. */
559 static char msg
[80];
561 /* xgettext:c-format */
562 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
563 CGEN_SYNTAX_CHAR(*syn
), *str
);
568 /* Ran out of input. */
569 static char msg
[80];
571 /* xgettext:c-format */
572 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
573 CGEN_SYNTAX_CHAR(*syn
));
579 /* We have an operand of some sort. */
580 errmsg
= fr30_cgen_parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
585 /* Done with this operand, continue with next one. */
589 /* If we're at the end of the syntax string, we're done. */
592 /* FIXME: For the moment we assume a valid `str' can only contain
593 blanks now. IE: We needn't try again with a longer version of
594 the insn and it is assumed that longer versions of insns appear
595 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
596 while (isspace (* str
))
600 return _("junk at end of line"); /* FIXME: would like to include `str' */
605 /* We couldn't parse it. */
606 return _("unrecognized instruction");
610 This routine is called for each instruction to be assembled.
611 STR points to the insn to be assembled.
612 We assume all necessary tables have been initialized.
613 The assembled instruction, less any fixups, is stored in BUF.
614 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
615 still needs to be converted to target byte order, otherwise BUF is an array
616 of bytes in target byte order.
617 The result is a pointer to the insn's entry in the opcode table,
618 or NULL if an error occured (an error message will have already been
621 Note that when processing (non-alias) macro-insns,
622 this function recurses.
624 ??? It's possible to make this cpu-independent.
625 One would have to deal with a few minor things.
626 At this point in time doing so would be more of a curiosity than useful
627 [for example this file isn't _that_ big], but keeping the possibility in
628 mind helps keep the design clean. */
631 fr30_cgen_assemble_insn (cd
, str
, fields
, buf
, errmsg
)
635 CGEN_INSN_BYTES_PTR buf
;
639 CGEN_INSN_LIST
*ilist
;
640 const char *parse_errmsg
= NULL
;
641 const char *insert_errmsg
= NULL
;
642 int recognized_mnemonic
= 0;
644 /* Skip leading white space. */
645 while (isspace (* str
))
648 /* The instructions are stored in hashed lists.
649 Get the first in the list. */
650 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
652 /* Keep looking until we find a match. */
654 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
656 const CGEN_INSN
*insn
= ilist
->insn
;
657 recognized_mnemonic
= 1;
659 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
660 /* Not usually needed as unsupported opcodes
661 shouldn't be in the hash lists. */
662 /* Is this insn supported by the selected cpu? */
663 if (! fr30_cgen_insn_supported (cd
, insn
))
666 /* If the RELAX attribute is set, this is an insn that shouldn't be
667 chosen immediately. Instead, it is used during assembler/linker
668 relaxation if possible. */
669 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAX
) != 0)
674 /* Skip this insn if str doesn't look right lexically. */
675 if (CGEN_INSN_RX (insn
) != NULL
&&
676 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
679 /* Allow parse/insert handlers to obtain length of insn. */
680 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
682 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
683 if (parse_errmsg
!= NULL
)
686 /* ??? 0 is passed for `pc'. */
687 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
689 if (insert_errmsg
!= NULL
)
692 /* It is up to the caller to actually output the insn and any
698 static char errbuf
[150];
699 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
700 const char *tmp_errmsg
;
702 /* If requesting verbose error messages, use insert_errmsg.
703 Failing that, use parse_errmsg. */
704 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
705 parse_errmsg
? parse_errmsg
:
706 recognized_mnemonic
?
707 _("unrecognized form of instruction") :
708 _("unrecognized instruction"));
710 if (strlen (start
) > 50)
711 /* xgettext:c-format */
712 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
714 /* xgettext:c-format */
715 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
717 if (strlen (start
) > 50)
718 /* xgettext:c-format */
719 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
721 /* xgettext:c-format */
722 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
730 #if 0 /* This calls back to GAS which we can't do without care. */
732 /* Record each member of OPVALS in the assembler's symbol table.
733 This lets GAS parse registers for us.
734 ??? Interesting idea but not currently used. */
736 /* Record each member of OPVALS in the assembler's symbol table.
737 FIXME: Not currently used. */
740 fr30_cgen_asm_hash_keywords (cd
, opvals
)
742 CGEN_KEYWORD
*opvals
;
744 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
745 const CGEN_KEYWORD_ENTRY
* ke
;
747 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
749 #if 0 /* Unnecessary, should be done in the search routine. */
750 if (! fr30_cgen_opval_supported (ke
))
753 cgen_asm_record_register (cd
, ke
->name
, ke
->value
);