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 "m32r-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 */
51 /* Handle '#' prefixes (i.e. skip over them). */
54 parse_hash (cd
, strp
, opindex
, valuep
)
58 unsigned long *valuep
;
65 /* Handle shigh(), high(). */
68 parse_hi16 (cd
, strp
, opindex
, valuep
)
72 unsigned long *valuep
;
75 enum cgen_parse_operand_result result_type
;
81 if (strncasecmp (*strp
, "high(", 5) == 0)
84 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_HI16_ULO
,
85 &result_type
, &value
);
90 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
95 else if (strncasecmp (*strp
, "shigh(", 6) == 0)
98 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_HI16_SLO
,
99 &result_type
, &value
);
101 return "missing `)'";
104 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
105 value
= (value
>> 16) + (value
& 0x8000 ? 1 : 0);
110 return cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
113 /* Handle low() in a signed context. Also handle sda().
114 The signedness of the value doesn't matter to low(), but this also
115 handles the case where low() isn't present. */
118 parse_slo16 (cd
, strp
, opindex
, valuep
)
125 enum cgen_parse_operand_result result_type
;
131 if (strncasecmp (*strp
, "low(", 4) == 0)
134 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_LO16
,
135 &result_type
, &value
);
137 return "missing `)'";
140 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
146 if (strncasecmp (*strp
, "sda(", 4) == 0)
149 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_SDA16
,
152 return "missing `)'";
158 return cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
161 /* Handle low() in an unsigned context.
162 The signedness of the value doesn't matter to low(), but this also
163 handles the case where low() isn't present. */
166 parse_ulo16 (cd
, strp
, opindex
, valuep
)
170 unsigned long *valuep
;
173 enum cgen_parse_operand_result result_type
;
179 if (strncasecmp (*strp
, "low(", 4) == 0)
182 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_M32R_LO16
,
183 &result_type
, &value
);
185 return "missing `)'";
188 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
194 return cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
199 /* Main entry point for operand parsing.
201 This function is basically just a big switch statement. Earlier versions
202 used tables to look up the function to use, but
203 - if the table contains both assembler and disassembler functions then
204 the disassembler contains much of the assembler and vice-versa,
205 - there's a lot of inlining possibilities as things grow,
206 - using a switch statement avoids the function call overhead.
208 This function could be moved into `parse_insn_normal', but keeping it
209 separate makes clear the interface between `parse_insn_normal' and each of
214 m32r_cgen_parse_operand (cd
, opindex
, strp
, fields
)
218 CGEN_FIELDS
* fields
;
220 const char * errmsg
= NULL
;
221 /* Used by scalar operands that still need to be parsed. */
226 case M32R_OPERAND_ACC
:
227 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_acc
);
229 case M32R_OPERAND_ACCD
:
230 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_accd
);
232 case M32R_OPERAND_ACCS
:
233 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_h_accums
, & fields
->f_accs
);
235 case M32R_OPERAND_DCR
:
236 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_cr_names
, & fields
->f_r1
);
238 case M32R_OPERAND_DISP16
:
241 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP16
, 0, NULL
, & value
);
242 fields
->f_disp16
= value
;
245 case M32R_OPERAND_DISP24
:
248 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP24
, 0, NULL
, & value
);
249 fields
->f_disp24
= value
;
252 case M32R_OPERAND_DISP8
:
255 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_DISP8
, 0, NULL
, & value
);
256 fields
->f_disp8
= value
;
259 case M32R_OPERAND_DR
:
260 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r1
);
262 case M32R_OPERAND_HASH
:
263 errmsg
= parse_hash (cd
, strp
, M32R_OPERAND_HASH
, &junk
);
265 case M32R_OPERAND_HI16
:
266 errmsg
= parse_hi16 (cd
, strp
, M32R_OPERAND_HI16
, &fields
->f_hi16
);
268 case M32R_OPERAND_IMM1
:
269 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_IMM1
, &fields
->f_imm1
);
271 case M32R_OPERAND_SCR
:
272 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_cr_names
, & fields
->f_r2
);
274 case M32R_OPERAND_SIMM16
:
275 errmsg
= cgen_parse_signed_integer (cd
, strp
, M32R_OPERAND_SIMM16
, &fields
->f_simm16
);
277 case M32R_OPERAND_SIMM8
:
278 errmsg
= cgen_parse_signed_integer (cd
, strp
, M32R_OPERAND_SIMM8
, &fields
->f_simm8
);
280 case M32R_OPERAND_SLO16
:
281 errmsg
= parse_slo16 (cd
, strp
, M32R_OPERAND_SLO16
, &fields
->f_simm16
);
283 case M32R_OPERAND_SR
:
284 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r2
);
286 case M32R_OPERAND_SRC1
:
287 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r1
);
289 case M32R_OPERAND_SRC2
:
290 errmsg
= cgen_parse_keyword (cd
, strp
, & m32r_cgen_opval_gr_names
, & fields
->f_r2
);
292 case M32R_OPERAND_UIMM16
:
293 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM16
, &fields
->f_uimm16
);
295 case M32R_OPERAND_UIMM24
:
298 errmsg
= cgen_parse_address (cd
, strp
, M32R_OPERAND_UIMM24
, 0, NULL
, & value
);
299 fields
->f_uimm24
= value
;
302 case M32R_OPERAND_UIMM4
:
303 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM4
, &fields
->f_uimm4
);
305 case M32R_OPERAND_UIMM5
:
306 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, M32R_OPERAND_UIMM5
, &fields
->f_uimm5
);
308 case M32R_OPERAND_ULO16
:
309 errmsg
= parse_ulo16 (cd
, strp
, M32R_OPERAND_ULO16
, &fields
->f_uimm16
);
313 /* xgettext:c-format */
314 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
321 cgen_parse_fn
* const m32r_cgen_parse_handlers
[] =
327 m32r_cgen_init_asm (cd
)
330 m32r_cgen_init_opcode_table (cd
);
331 m32r_cgen_init_ibld_table (cd
);
332 cd
->parse_handlers
= & m32r_cgen_parse_handlers
[0];
333 cd
->parse_operand
= m32r_cgen_parse_operand
;
339 Regex construction routine.
341 This translates an opcode syntax string into a regex string,
342 by replacing any non-character syntax element (such as an
343 opcode) with the pattern '.*'
345 It then compiles the regex and stores it in the opcode, for
346 later use by m32r_cgen_assemble_insn
348 returns NULL for success, an error message for failure
352 m32r_cgen_build_insn_regex (insn
)
355 CGEN_OPCODE
*opc
= CGEN_INSN_OPCODE (insn
);
356 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
358 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
360 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
363 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
365 /* Mnemonics come first in the syntax string */
366 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
)) return "missing mnemonic in syntax string";
369 /* copy the literal mnemonic out of the insn */
370 memset (rx
, 0, CGEN_MAX_RX_ELEMENTS
);
371 mnem_len
= strlen(mnem
);
372 memcpy (rx
, mnem
, mnem_len
);
375 /* copy any remaining literals from the syntax string into the rx */
376 for(; * syn
!= 0 && rx
< rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 9); ++syn
, ++rx
)
378 if (CGEN_SYNTAX_CHAR_P (* syn
))
380 char tmp
= CGEN_SYNTAX_CHAR (* syn
);
383 /* escape any regex metacharacters in the syntax */
384 case '.': case '[': case '\\':
385 case '*': case '^': case '$':
387 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
388 case '?': case '{': case '}':
389 case '(': case ')': case '*':
390 case '|': case '+': case ']':
396 /* insert syntax char into rx */
401 /* replace non-syntax fields with globs */
407 /* trailing whitespace ok */
414 /* but anchor it after that */
418 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
419 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
|REG_ICASE
);
426 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
427 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
428 free (CGEN_INSN_RX (insn
));
429 (CGEN_INSN_RX (insn
)) = NULL
;
435 /* Default insn parser.
437 The syntax string is scanned and operands are parsed and stored in FIELDS.
438 Relocs are queued as we go via other callbacks.
440 ??? Note that this is currently an all-or-nothing parser. If we fail to
441 parse the instruction, we return 0 and the caller will start over from
442 the beginning. Backtracking will be necessary in parsing subexpressions,
443 but that can be handled there. Not handling backtracking here may get
444 expensive in the case of the m68k. Deal with later.
446 Returns NULL for success, an error message for failure.
450 parse_insn_normal (cd
, insn
, strp
, fields
)
452 const CGEN_INSN
*insn
;
456 /* ??? Runtime added insns not handled yet. */
457 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
458 const char *str
= *strp
;
461 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
462 #ifdef CGEN_MNEMONIC_OPERANDS
467 /* For now we assume the mnemonic is first (there are no leading operands).
468 We can parse it without needing to set up operand parsing.
469 GAS's input scrubber will ensure mnemonics are lowercase, but we may
470 not be called from GAS. */
471 p
= CGEN_INSN_MNEMONIC (insn
);
472 while (*p
&& tolower (*p
) == tolower (*str
))
476 return _("unrecognized instruction");
478 #ifndef CGEN_MNEMONIC_OPERANDS
479 if (* str
&& !isspace (* str
))
480 return _("unrecognized instruction");
483 CGEN_INIT_PARSE (cd
);
484 cgen_init_parse_operand (cd
);
485 #ifdef CGEN_MNEMONIC_OPERANDS
489 /* We don't check for (*str != '\0') here because we want to parse
490 any trailing fake arguments in the syntax string. */
491 syn
= CGEN_SYNTAX_STRING (syntax
);
493 /* Mnemonics come first for now, ensure valid string. */
494 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
501 /* Non operand chars must match exactly. */
502 if (CGEN_SYNTAX_CHAR_P (* syn
))
504 /* FIXME: While we allow for non-GAS callers above, we assume the
505 first char after the mnemonic part is a space. */
506 /* FIXME: We also take inappropriate advantage of the fact that
507 GAS's input scrubber will remove extraneous blanks. */
508 if (tolower (*str
) == tolower (CGEN_SYNTAX_CHAR (* syn
)))
510 #ifdef CGEN_MNEMONIC_OPERANDS
511 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
519 /* Syntax char didn't match. Can't be this insn. */
520 static char msg
[80];
521 /* xgettext:c-format */
522 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
523 CGEN_SYNTAX_CHAR(*syn
), *str
);
528 /* Ran out of input. */
529 static char msg
[80];
530 /* xgettext:c-format */
531 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
532 CGEN_SYNTAX_CHAR(*syn
));
538 /* We have an operand of some sort. */
539 errmsg
= m32r_cgen_parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
544 /* Done with this operand, continue with next one. */
548 /* If we're at the end of the syntax string, we're done. */
551 /* FIXME: For the moment we assume a valid `str' can only contain
552 blanks now. IE: We needn't try again with a longer version of
553 the insn and it is assumed that longer versions of insns appear
554 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
555 while (isspace (* str
))
559 return _("junk at end of line"); /* FIXME: would like to include `str' */
564 /* We couldn't parse it. */
565 return _("unrecognized instruction");
569 This routine is called for each instruction to be assembled.
570 STR points to the insn to be assembled.
571 We assume all necessary tables have been initialized.
572 The assembled instruction, less any fixups, is stored in BUF.
573 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
574 still needs to be converted to target byte order, otherwise BUF is an array
575 of bytes in target byte order.
576 The result is a pointer to the insn's entry in the opcode table,
577 or NULL if an error occured (an error message will have already been
580 Note that when processing (non-alias) macro-insns,
581 this function recurses.
583 ??? It's possible to make this cpu-independent.
584 One would have to deal with a few minor things.
585 At this point in time doing so would be more of a curiosity than useful
586 [for example this file isn't _that_ big], but keeping the possibility in
587 mind helps keep the design clean. */
590 m32r_cgen_assemble_insn (cd
, str
, fields
, buf
, errmsg
)
594 CGEN_INSN_BYTES_PTR buf
;
598 CGEN_INSN_LIST
*ilist
;
599 const char *parse_errmsg
= NULL
;
600 const char *insert_errmsg
= NULL
;
601 int recognized_mnemonic
= 0;
603 /* Skip leading white space. */
604 while (isspace (* str
))
607 /* The instructions are stored in hashed lists.
608 Get the first in the list. */
609 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
611 /* Keep looking until we find a match. */
614 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
616 const CGEN_INSN
*insn
= ilist
->insn
;
617 recognized_mnemonic
= 1;
619 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
620 /* not usually needed as unsupported opcodes shouldn't be in the hash lists */
621 /* Is this insn supported by the selected cpu? */
622 if (! m32r_cgen_insn_supported (cd
, insn
))
626 /* If the RELAX attribute is set, this is an insn that shouldn't be
627 chosen immediately. Instead, it is used during assembler/linker
628 relaxation if possible. */
629 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAX
) != 0)
634 /* skip this insn if str doesn't look right lexically */
635 if (CGEN_INSN_RX (insn
) != NULL
&&
636 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
639 /* Allow parse/insert handlers to obtain length of insn. */
640 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
642 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
643 if (parse_errmsg
!= NULL
)
646 /* ??? 0 is passed for `pc' */
647 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
649 if (insert_errmsg
!= NULL
)
652 /* It is up to the caller to actually output the insn and any
658 static char errbuf
[150];
659 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
660 const char *tmp_errmsg
;
662 /* If requesting verbose error messages, use insert_errmsg.
663 Failing that, use parse_errmsg */
664 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
665 parse_errmsg
? parse_errmsg
:
666 recognized_mnemonic
? _("unrecognized form of instruction") :
667 _("unrecognized instruction"));
669 if (strlen (start
) > 50)
670 /* xgettext:c-format */
671 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
673 /* xgettext:c-format */
674 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
676 if (strlen (start
) > 50)
677 /* xgettext:c-format */
678 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
680 /* xgettext:c-format */
681 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
689 #if 0 /* This calls back to GAS which we can't do without care. */
691 /* Record each member of OPVALS in the assembler's symbol table.
692 This lets GAS parse registers for us.
693 ??? Interesting idea but not currently used. */
695 /* Record each member of OPVALS in the assembler's symbol table.
696 FIXME: Not currently used. */
699 m32r_cgen_asm_hash_keywords (cd
, opvals
)
701 CGEN_KEYWORD
*opvals
;
703 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
704 const CGEN_KEYWORD_ENTRY
* ke
;
706 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
708 #if 0 /* Unnecessary, should be done in the search routine. */
709 if (! m32r_cgen_opval_supported (ke
))
712 cgen_asm_record_register (cd
, ke
->name
, ke
->value
);