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.
33 #include "m32r-desc.h"
37 #include "libiberty.h"
38 #include "safe-ctype.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 (CGEN_CPU_DESC
, const CGEN_INSN
*, const char **, CGEN_FIELDS
*);
48 /* -- assembler routines inserted here. */
51 static const char * parse_hash
52 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
53 static const char * parse_hi16
54 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
55 static const char * parse_slo16
56 PARAMS ((CGEN_CPU_DESC
, const char **, int, long *));
57 static const char * parse_ulo16
58 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
60 /* Handle '#' prefixes (i.e. skip over them). */
63 parse_hash (cd
, strp
, opindex
, valuep
)
64 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
66 int opindex ATTRIBUTE_UNUSED
;
67 unsigned long *valuep ATTRIBUTE_UNUSED
;
74 /* Handle shigh(), high(). */
77 parse_hi16 (cd
, strp
, opindex
, valuep
)
81 unsigned long *valuep
;
84 enum cgen_parse_operand_result result_type
;
90 if (strncasecmp (*strp
, "high(", 5) == 0)
93 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_HI16_ULO
,
94 &result_type
, &value
);
99 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
104 else if (strncasecmp (*strp
, "shigh(", 6) == 0)
107 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_HI16_SLO
,
108 &result_type
, &value
);
110 return "missing `)'";
113 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
114 value
= (value
>> 16) + (value
& 0x8000 ? 1 : 0);
119 return cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
122 /* Handle low() in a signed context. Also handle sda().
123 The signedness of the value doesn't matter to low(), but this also
124 handles the case where low() isn't present. */
127 parse_slo16 (cd
, strp
, opindex
, valuep
)
134 enum cgen_parse_operand_result result_type
;
140 if (strncasecmp (*strp
, "low(", 4) == 0)
143 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_LO16
,
144 &result_type
, &value
);
146 return "missing `)'";
149 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
155 if (strncasecmp (*strp
, "sda(", 4) == 0)
158 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_SDA16
,
161 return "missing `)'";
167 return cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
170 /* Handle low() in an unsigned context.
171 The signedness of the value doesn't matter to low(), but this also
172 handles the case where low() isn't present. */
175 parse_ulo16 (cd
, strp
, opindex
, valuep
)
179 unsigned long *valuep
;
182 enum cgen_parse_operand_result result_type
;
188 if (strncasecmp (*strp
, "low(", 4) == 0)
191 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_LO16
,
192 &result_type
, &value
);
194 return "missing `)'";
197 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
203 return cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
208 const char * m32r_cgen_parse_operand
209 PARAMS ((CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*));
211 /* Main entry point for operand parsing.
213 This function is basically just a big switch statement. Earlier versions
214 used tables to look up the function to use, but
215 - if the table contains both assembler and disassembler functions then
216 the disassembler contains much of the assembler and vice-versa,
217 - there's a lot of inlining possibilities as things grow,
218 - using a switch statement avoids the function call overhead.
220 This function could be moved into `parse_insn_normal', but keeping it
221 separate makes clear the interface between `parse_insn_normal' and each of
225 m32r_cgen_parse_operand (cd
, opindex
, strp
, fields
)
229 CGEN_FIELDS
* fields
;
231 const char * errmsg
= NULL
;
232 /* Used by scalar operands that still need to be parsed. */
233 long junk ATTRIBUTE_UNUSED
;
237 case M32R_OPERAND_ACC
:
238 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_acc
);
240 case M32R_OPERAND_ACCD
:
241 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_accd
);
243 case M32R_OPERAND_ACCS
:
244 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_accs
);
246 case M32R_OPERAND_DCR
:
247 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_cr_names
, & fields
->f_r1
);
249 case M32R_OPERAND_DISP16
:
252 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP16
, 0, NULL
, & value
);
253 fields
->f_disp16
= value
;
256 case M32R_OPERAND_DISP24
:
259 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP24
, 0, NULL
, & value
);
260 fields
->f_disp24
= value
;
263 case M32R_OPERAND_DISP8
:
266 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP8
, 0, NULL
, & value
);
267 fields
->f_disp8
= value
;
270 case M32R_OPERAND_DR
:
271 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r1
);
273 case M32R_OPERAND_HASH
:
274 errmsg
= parse_hash (cd
, strp
, M32R_OPERAND_HASH
, &junk
);
276 case M32R_OPERAND_HI16
:
277 errmsg
= parse_hi16 (cd
, strp
, M32R_OPERAND_HI16
, &fields
->f_hi16
);
279 case M32R_OPERAND_IMM1
:
280 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_IMM1
, &fields
->f_imm1
);
282 case M32R_OPERAND_SCR
:
283 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_cr_names
, & fields
->f_r2
);
285 case M32R_OPERAND_SIMM16
:
286 errmsg
= cgen_parse_signed_integer (cd
, strp
, M32R_OPERAND_SIMM16
, &fields
->f_simm16
);
288 case M32R_OPERAND_SIMM8
:
289 errmsg
= cgen_parse_signed_integer (cd
, strp
, M32R_OPERAND_SIMM8
, &fields
->f_simm8
);
291 case M32R_OPERAND_SLO16
:
292 errmsg
= parse_slo16 (cd
, strp
, M32R_OPERAND_SLO16
, &fields
->f_simm16
);
294 case M32R_OPERAND_SR
:
295 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r2
);
297 case M32R_OPERAND_SRC1
:
298 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r1
);
300 case M32R_OPERAND_SRC2
:
301 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r2
);
303 case M32R_OPERAND_UIMM16
:
304 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM16
, &fields
->f_uimm16
);
306 case M32R_OPERAND_UIMM24
:
309 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_UIMM24
, 0, NULL
, & value
);
310 fields
->f_uimm24
= value
;
313 case M32R_OPERAND_UIMM4
:
314 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM4
, &fields
->f_uimm4
);
316 case M32R_OPERAND_UIMM5
:
317 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM5
, &fields
->f_uimm5
);
319 case M32R_OPERAND_ULO16
:
320 errmsg
= parse_ulo16 (cd
, strp
, M32R_OPERAND_ULO16
, &fields
->f_uimm16
);
324 /* xgettext:c-format */
325 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
332 cgen_parse_fn
* const m32r_cgen_parse_handlers
[] =
338 m32r_cgen_init_asm (cd
)
341 m32r_cgen_init_opcode_table (cd
);
342 m32r_cgen_init_ibld_table (cd
);
343 cd
->parse_handlers
= & m32r_cgen_parse_handlers
[0];
344 cd
->parse_operand
= m32r_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 m32r_cgen_assemble_insn
358 Returns NULL for success, an error message for failure. */
361 m32r_cgen_build_insn_regex (CGEN_INSN
*insn
)
363 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
364 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
365 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
367 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
370 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
372 /* Mnemonics come first in the syntax string. */
373 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
374 return _("missing mnemonic in syntax string");
377 /* Generate a case sensitive regular expression that emulates case
378 insensitive matching in the "C" locale. We cannot generate a case
379 insensitive regular expression because in Turkish locales, 'i' and 'I'
380 are not equal modulo case conversion. */
382 /* Copy the literal mnemonic out of the insn. */
383 for (; *mnem
; mnem
++)
398 /* Copy any remaining literals from the syntax string into the rx. */
399 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
401 if (CGEN_SYNTAX_CHAR_P (* syn
))
403 char c
= CGEN_SYNTAX_CHAR (* syn
);
407 /* Escape any regex metacharacters in the syntax. */
408 case '.': case '[': case '\\':
409 case '*': case '^': case '$':
411 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
412 case '?': case '{': case '}':
413 case '(': case ')': case '*':
414 case '|': case '+': case ']':
435 /* Replace non-syntax fields with globs. */
441 /* Trailing whitespace ok. */
448 /* But anchor it after that. */
452 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
453 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
461 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
462 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
463 free (CGEN_INSN_RX (insn
));
464 (CGEN_INSN_RX (insn
)) = NULL
;
470 /* Default insn parser.
472 The syntax string is scanned and operands are parsed and stored in FIELDS.
473 Relocs are queued as we go via other callbacks.
475 ??? Note that this is currently an all-or-nothing parser. If we fail to
476 parse the instruction, we return 0 and the caller will start over from
477 the beginning. Backtracking will be necessary in parsing subexpressions,
478 but that can be handled there. Not handling backtracking here may get
479 expensive in the case of the m68k. Deal with later.
481 Returns NULL for success, an error message for failure. */
484 parse_insn_normal (CGEN_CPU_DESC cd
,
485 const CGEN_INSN
*insn
,
489 /* ??? Runtime added insns not handled yet. */
490 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
491 const char *str
= *strp
;
494 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
495 #ifdef CGEN_MNEMONIC_OPERANDS
500 /* For now we assume the mnemonic is first (there are no leading operands).
501 We can parse it without needing to set up operand parsing.
502 GAS's input scrubber will ensure mnemonics are lowercase, but we may
503 not be called from GAS. */
504 p
= CGEN_INSN_MNEMONIC (insn
);
505 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
509 return _("unrecognized instruction");
511 #ifndef CGEN_MNEMONIC_OPERANDS
512 if (* str
&& ! ISSPACE (* str
))
513 return _("unrecognized instruction");
516 CGEN_INIT_PARSE (cd
);
517 cgen_init_parse_operand (cd
);
518 #ifdef CGEN_MNEMONIC_OPERANDS
522 /* We don't check for (*str != '\0') here because we want to parse
523 any trailing fake arguments in the syntax string. */
524 syn
= CGEN_SYNTAX_STRING (syntax
);
526 /* Mnemonics come first for now, ensure valid string. */
527 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
534 /* Non operand chars must match exactly. */
535 if (CGEN_SYNTAX_CHAR_P (* syn
))
537 /* FIXME: While we allow for non-GAS callers above, we assume the
538 first char after the mnemonic part is a space. */
539 /* FIXME: We also take inappropriate advantage of the fact that
540 GAS's input scrubber will remove extraneous blanks. */
541 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
543 #ifdef CGEN_MNEMONIC_OPERANDS
544 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
552 /* Syntax char didn't match. Can't be this insn. */
553 static char msg
[80];
555 /* xgettext:c-format */
556 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
557 CGEN_SYNTAX_CHAR(*syn
), *str
);
562 /* Ran out of input. */
563 static char msg
[80];
565 /* xgettext:c-format */
566 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
567 CGEN_SYNTAX_CHAR(*syn
));
573 /* We have an operand of some sort. */
574 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
579 /* Done with this operand, continue with next one. */
583 /* If we're at the end of the syntax string, we're done. */
586 /* FIXME: For the moment we assume a valid `str' can only contain
587 blanks now. IE: We needn't try again with a longer version of
588 the insn and it is assumed that longer versions of insns appear
589 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
590 while (ISSPACE (* str
))
594 return _("junk at end of line"); /* FIXME: would like to include `str' */
599 /* We couldn't parse it. */
600 return _("unrecognized instruction");
604 This routine is called for each instruction to be assembled.
605 STR points to the insn to be assembled.
606 We assume all necessary tables have been initialized.
607 The assembled instruction, less any fixups, is stored in BUF.
608 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
609 still needs to be converted to target byte order, otherwise BUF is an array
610 of bytes in target byte order.
611 The result is a pointer to the insn's entry in the opcode table,
612 or NULL if an error occured (an error message will have already been
615 Note that when processing (non-alias) macro-insns,
616 this function recurses.
618 ??? It's possible to make this cpu-independent.
619 One would have to deal with a few minor things.
620 At this point in time doing so would be more of a curiosity than useful
621 [for example this file isn't _that_ big], but keeping the possibility in
622 mind helps keep the design clean. */
625 m32r_cgen_assemble_insn (CGEN_CPU_DESC cd
,
628 CGEN_INSN_BYTES_PTR buf
,
632 CGEN_INSN_LIST
*ilist
;
633 const char *parse_errmsg
= NULL
;
634 const char *insert_errmsg
= NULL
;
635 int recognized_mnemonic
= 0;
637 /* Skip leading white space. */
638 while (ISSPACE (* str
))
641 /* The instructions are stored in hashed lists.
642 Get the first in the list. */
643 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
645 /* Keep looking until we find a match. */
647 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
649 const CGEN_INSN
*insn
= ilist
->insn
;
650 recognized_mnemonic
= 1;
652 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
653 /* Not usually needed as unsupported opcodes
654 shouldn't be in the hash lists. */
655 /* Is this insn supported by the selected cpu? */
656 if (! m32r_cgen_insn_supported (cd
, insn
))
659 /* If the RELAXED attribute is set, this is an insn that shouldn't be
660 chosen immediately. Instead, it is used during assembler/linker
661 relaxation if possible. */
662 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
667 /* Skip this insn if str doesn't look right lexically. */
668 if (CGEN_INSN_RX (insn
) != NULL
&&
669 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
672 /* Allow parse/insert handlers to obtain length of insn. */
673 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
675 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
676 if (parse_errmsg
!= NULL
)
679 /* ??? 0 is passed for `pc'. */
680 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
682 if (insert_errmsg
!= NULL
)
685 /* It is up to the caller to actually output the insn and any
691 static char errbuf
[150];
692 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
693 const char *tmp_errmsg
;
695 /* If requesting verbose error messages, use insert_errmsg.
696 Failing that, use parse_errmsg. */
697 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
698 parse_errmsg
? parse_errmsg
:
699 recognized_mnemonic
?
700 _("unrecognized form of instruction") :
701 _("unrecognized instruction"));
703 if (strlen (start
) > 50)
704 /* xgettext:c-format */
705 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
707 /* xgettext:c-format */
708 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
710 if (strlen (start
) > 50)
711 /* xgettext:c-format */
712 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
714 /* xgettext:c-format */
715 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
723 #if 0 /* This calls back to GAS which we can't do without care. */
725 /* Record each member of OPVALS in the assembler's symbol table.
726 This lets GAS parse registers for us.
727 ??? Interesting idea but not currently used. */
729 /* Record each member of OPVALS in the assembler's symbol table.
730 FIXME: Not currently used. */
733 m32r_cgen_asm_hash_keywords (CGEN_CPU_DESC cd
, CGEN_KEYWORD
*opvals
)
735 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
736 const CGEN_KEYWORD_ENTRY
* ke
;
738 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
740 #if 0 /* Unnecessary, should be done in the search routine. */
741 if (! m32r_cgen_opval_supported (ke
))
744 cgen_asm_record_register (cd
, ke
->name
, ke
->value
);