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
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.
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 /* Range checking for signed numbers. Returns 0 if acceptable
54 and 1 if the value is out of bounds for a signed quantity. */
57 signed_out_of_bounds (long val
)
59 if ((val
< -32768) || (val
> 32767))
65 parse_loopsize (CGEN_CPU_DESC cd
,
70 signed long * valuep
= (signed long *) arg
;
72 bfd_reloc_code_real_type code
= BFD_RELOC_NONE
;
73 enum cgen_parse_operand_result result_type
;
76 /* Is it a control transfer instructions? */
77 if (opindex
== (CGEN_OPERAND_TYPE
) MT_OPERAND_LOOPSIZE
)
79 code
= BFD_RELOC_MT_PCINSN8
;
80 errmsg
= cgen_parse_address (cd
, strp
, opindex
, code
,
81 & result_type
, & value
);
90 parse_imm16 (CGEN_CPU_DESC cd
,
95 signed long * valuep
= (signed long *) arg
;
97 enum cgen_parse_operand_result result_type
;
98 bfd_reloc_code_real_type code
= BFD_RELOC_NONE
;
101 /* Is it a control transfer instructions? */
102 if (opindex
== (CGEN_OPERAND_TYPE
) MT_OPERAND_IMM16O
)
104 code
= BFD_RELOC_16_PCREL
;
105 errmsg
= cgen_parse_address (cd
, strp
, opindex
, code
,
106 & result_type
, & value
);
109 if (signed_out_of_bounds (value
))
110 errmsg
= _("Operand out of range. Must be between -32768 and 32767.");
116 /* If it's not a control transfer instruction, then
117 we have to check for %OP relocating operators. */
118 if (opindex
== (CGEN_OPERAND_TYPE
) MT_OPERAND_IMM16L
)
120 else if (strncmp (*strp
, "%hi16", 5) == 0)
123 code
= BFD_RELOC_HI16
;
125 else if (strncmp (*strp
, "%lo16", 5) == 0)
128 code
= BFD_RELOC_LO16
;
131 /* If we found a %OP relocating operator, then parse it as an address.
132 If not, we need to parse it as an integer, either signed or unsigned
133 depending on which operand type we have. */
134 if (code
!= BFD_RELOC_NONE
)
136 /* %OP relocating operator found. */
137 errmsg
= cgen_parse_address (cd
, strp
, opindex
, code
,
138 & result_type
, & value
);
143 case (CGEN_PARSE_OPERAND_RESULT_NUMBER
):
144 if (code
== BFD_RELOC_HI16
)
145 value
= (value
>> 16) & 0xFFFF;
146 else if (code
== BFD_RELOC_LO16
)
147 value
= value
& 0xFFFF;
149 errmsg
= _("Biiiig Trouble in parse_imm16!");
152 case (CGEN_PARSE_OPERAND_RESULT_QUEUED
):
153 /* No special processing for this case. */
157 errmsg
= _("%operator operand is not a symbol");
165 /* Parse hex values like 0xffff as unsigned, and sign extend
167 int parse_signed
= (opindex
== (CGEN_OPERAND_TYPE
)MT_OPERAND_IMM16
);
169 if ((*strp
)[0] == '0'
170 && ((*strp
)[1] == 'x' || (*strp
)[1] == 'X'))
173 /* No relocating operator. Parse as an number. */
176 /* Parse as as signed integer. */
178 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
183 /* Manual range checking is needed for the signed case. */
184 if (*valuep
& 0x8000)
185 value
= 0xffff0000 | *valuep
;
189 if (signed_out_of_bounds (value
))
190 errmsg
= _("Operand out of range. Must be between -32768 and 32767.");
191 /* Truncate to 16 bits. This is necessary
192 because cgen will have sign extended *valuep. */
199 /* MT_OPERAND_IMM16Z. Parse as an unsigned integer. */
200 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, (unsigned long *) valuep
);
202 if (opindex
== (CGEN_OPERAND_TYPE
) MT_OPERAND_IMM16
204 && *valuep
<= 0xffff)
214 parse_dup (CGEN_CPU_DESC cd
,
217 unsigned long *valuep
)
219 const char *errmsg
= NULL
;
221 if (strncmp (*strp
, "dup", 3) == 0 || strncmp (*strp
, "DUP", 3) == 0)
226 else if (strncmp (*strp
, "xx", 2) == 0 || strncmp (*strp
, "XX", 2) == 0)
232 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
239 parse_ball (CGEN_CPU_DESC cd
,
242 unsigned long *valuep
)
244 const char *errmsg
= NULL
;
246 if (strncmp (*strp
, "all", 3) == 0 || strncmp (*strp
, "ALL", 3) == 0)
251 else if (strncmp (*strp
, "one", 3) == 0 || strncmp (*strp
, "ONE", 3) == 0)
257 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
263 parse_xmode (CGEN_CPU_DESC cd
,
266 unsigned long *valuep
)
268 const char *errmsg
= NULL
;
270 if (strncmp (*strp
, "pm", 2) == 0 || strncmp (*strp
, "PM", 2) == 0)
275 else if (strncmp (*strp
, "xm", 2) == 0 || strncmp (*strp
, "XM", 2) == 0)
281 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
287 parse_rc (CGEN_CPU_DESC cd
,
290 unsigned long *valuep
)
292 const char *errmsg
= NULL
;
294 if (strncmp (*strp
, "r", 1) == 0 || strncmp (*strp
, "R", 1) == 0)
299 else if (strncmp (*strp
, "c", 1) == 0 || strncmp (*strp
, "C", 1) == 0)
305 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
311 parse_cbrb (CGEN_CPU_DESC cd
,
314 unsigned long *valuep
)
316 const char *errmsg
= NULL
;
318 if (strncmp (*strp
, "rb", 2) == 0 || strncmp (*strp
, "RB", 2) == 0)
323 else if (strncmp (*strp
, "cb", 2) == 0 || strncmp (*strp
, "CB", 2) == 0)
329 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
335 parse_rbbc (CGEN_CPU_DESC cd
,
338 unsigned long *valuep
)
340 const char *errmsg
= NULL
;
342 if (strncmp (*strp
, "rt", 2) == 0 || strncmp (*strp
, "RT", 2) == 0)
347 else if (strncmp (*strp
, "br1", 3) == 0 || strncmp (*strp
, "BR1", 3) == 0)
352 else if (strncmp (*strp
, "br2", 3) == 0 || strncmp (*strp
, "BR2", 3) == 0)
357 else if (strncmp (*strp
, "cs", 2) == 0 || strncmp (*strp
, "CS", 2) == 0)
363 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
369 parse_type (CGEN_CPU_DESC cd
,
372 unsigned long *valuep
)
374 const char *errmsg
= NULL
;
376 if (strncmp (*strp
, "odd", 3) == 0 || strncmp (*strp
, "ODD", 3) == 0)
381 else if (strncmp (*strp
, "even", 4) == 0 || strncmp (*strp
, "EVEN", 4) == 0)
386 else if (strncmp (*strp
, "oe", 2) == 0 || strncmp (*strp
, "OE", 2) == 0)
392 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, valuep
);
394 if ((errmsg
== NULL
) && (*valuep
== 3))
395 errmsg
= _("invalid operand. type may have values 0,1,2 only.");
402 const char * mt_cgen_parse_operand
403 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
405 /* Main entry point for operand parsing.
407 This function is basically just a big switch statement. Earlier versions
408 used tables to look up the function to use, but
409 - if the table contains both assembler and disassembler functions then
410 the disassembler contains much of the assembler and vice-versa,
411 - there's a lot of inlining possibilities as things grow,
412 - using a switch statement avoids the function call overhead.
414 This function could be moved into `parse_insn_normal', but keeping it
415 separate makes clear the interface between `parse_insn_normal' and each of
419 mt_cgen_parse_operand (CGEN_CPU_DESC cd
,
422 CGEN_FIELDS
* fields
)
424 const char * errmsg
= NULL
;
425 /* Used by scalar operands that still need to be parsed. */
426 long junk ATTRIBUTE_UNUSED
;
430 case MT_OPERAND_A23
:
431 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_A23
, (unsigned long *) (& fields
->f_a23
));
433 case MT_OPERAND_BALL
:
434 errmsg
= parse_ball (cd
, strp
, MT_OPERAND_BALL
, (unsigned long *) (& fields
->f_ball
));
436 case MT_OPERAND_BALL2
:
437 errmsg
= parse_ball (cd
, strp
, MT_OPERAND_BALL2
, (unsigned long *) (& fields
->f_ball2
));
439 case MT_OPERAND_BANKADDR
:
440 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_BANKADDR
, (unsigned long *) (& fields
->f_bankaddr
));
442 case MT_OPERAND_BRC
:
443 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_BRC
, (unsigned long *) (& fields
->f_brc
));
445 case MT_OPERAND_BRC2
:
446 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_BRC2
, (unsigned long *) (& fields
->f_brc2
));
448 case MT_OPERAND_CB1INCR
:
449 errmsg
= cgen_parse_signed_integer (cd
, strp
, MT_OPERAND_CB1INCR
, (long *) (& fields
->f_cb1incr
));
451 case MT_OPERAND_CB1SEL
:
452 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CB1SEL
, (unsigned long *) (& fields
->f_cb1sel
));
454 case MT_OPERAND_CB2INCR
:
455 errmsg
= cgen_parse_signed_integer (cd
, strp
, MT_OPERAND_CB2INCR
, (long *) (& fields
->f_cb2incr
));
457 case MT_OPERAND_CB2SEL
:
458 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CB2SEL
, (unsigned long *) (& fields
->f_cb2sel
));
460 case MT_OPERAND_CBRB
:
461 errmsg
= parse_cbrb (cd
, strp
, MT_OPERAND_CBRB
, (unsigned long *) (& fields
->f_cbrb
));
463 case MT_OPERAND_CBS
:
464 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CBS
, (unsigned long *) (& fields
->f_cbs
));
466 case MT_OPERAND_CBX
:
467 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CBX
, (unsigned long *) (& fields
->f_cbx
));
469 case MT_OPERAND_CCB
:
470 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CCB
, (unsigned long *) (& fields
->f_ccb
));
472 case MT_OPERAND_CDB
:
473 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CDB
, (unsigned long *) (& fields
->f_cdb
));
475 case MT_OPERAND_CELL
:
476 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CELL
, (unsigned long *) (& fields
->f_cell
));
478 case MT_OPERAND_COLNUM
:
479 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_COLNUM
, (unsigned long *) (& fields
->f_colnum
));
481 case MT_OPERAND_CONTNUM
:
482 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CONTNUM
, (unsigned long *) (& fields
->f_contnum
));
485 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CR
, (unsigned long *) (& fields
->f_cr
));
487 case MT_OPERAND_CTXDISP
:
488 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_CTXDISP
, (unsigned long *) (& fields
->f_ctxdisp
));
490 case MT_OPERAND_DUP
:
491 errmsg
= parse_dup (cd
, strp
, MT_OPERAND_DUP
, (unsigned long *) (& fields
->f_dup
));
493 case MT_OPERAND_FBDISP
:
494 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_FBDISP
, (unsigned long *) (& fields
->f_fbdisp
));
496 case MT_OPERAND_FBINCR
:
497 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_FBINCR
, (unsigned long *) (& fields
->f_fbincr
));
499 case MT_OPERAND_FRDR
:
500 errmsg
= cgen_parse_keyword (cd
, strp
, & mt_cgen_opval_h_spr
, & fields
->f_dr
);
502 case MT_OPERAND_FRDRRR
:
503 errmsg
= cgen_parse_keyword (cd
, strp
, & mt_cgen_opval_h_spr
, & fields
->f_drrr
);
505 case MT_OPERAND_FRSR1
:
506 errmsg
= cgen_parse_keyword (cd
, strp
, & mt_cgen_opval_h_spr
, & fields
->f_sr1
);
508 case MT_OPERAND_FRSR2
:
509 errmsg
= cgen_parse_keyword (cd
, strp
, & mt_cgen_opval_h_spr
, & fields
->f_sr2
);
512 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_ID
, (unsigned long *) (& fields
->f_id
));
514 case MT_OPERAND_IMM16
:
515 errmsg
= parse_imm16 (cd
, strp
, MT_OPERAND_IMM16
, (long *) (& fields
->f_imm16s
));
517 case MT_OPERAND_IMM16L
:
518 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_IMM16L
, (unsigned long *) (& fields
->f_imm16l
));
520 case MT_OPERAND_IMM16O
:
521 errmsg
= parse_imm16 (cd
, strp
, MT_OPERAND_IMM16O
, (unsigned long *) (& fields
->f_imm16s
));
523 case MT_OPERAND_IMM16Z
:
524 errmsg
= parse_imm16 (cd
, strp
, MT_OPERAND_IMM16Z
, (unsigned long *) (& fields
->f_imm16u
));
526 case MT_OPERAND_INCAMT
:
527 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_INCAMT
, (unsigned long *) (& fields
->f_incamt
));
529 case MT_OPERAND_INCR
:
530 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_INCR
, (unsigned long *) (& fields
->f_incr
));
532 case MT_OPERAND_LENGTH
:
533 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_LENGTH
, (unsigned long *) (& fields
->f_length
));
535 case MT_OPERAND_LOOPSIZE
:
536 errmsg
= parse_loopsize (cd
, strp
, MT_OPERAND_LOOPSIZE
, (unsigned long *) (& fields
->f_loopo
));
538 case MT_OPERAND_MASK
:
539 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_MASK
, (unsigned long *) (& fields
->f_mask
));
541 case MT_OPERAND_MASK1
:
542 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_MASK1
, (unsigned long *) (& fields
->f_mask1
));
544 case MT_OPERAND_MODE
:
545 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_MODE
, (unsigned long *) (& fields
->f_mode
));
547 case MT_OPERAND_PERM
:
548 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_PERM
, (unsigned long *) (& fields
->f_perm
));
550 case MT_OPERAND_RBBC
:
551 errmsg
= parse_rbbc (cd
, strp
, MT_OPERAND_RBBC
, (unsigned long *) (& fields
->f_rbbc
));
554 errmsg
= parse_rc (cd
, strp
, MT_OPERAND_RC
, (unsigned long *) (& fields
->f_rc
));
556 case MT_OPERAND_RC1
:
557 errmsg
= parse_rc (cd
, strp
, MT_OPERAND_RC1
, (unsigned long *) (& fields
->f_rc1
));
559 case MT_OPERAND_RC2
:
560 errmsg
= parse_rc (cd
, strp
, MT_OPERAND_RC2
, (unsigned long *) (& fields
->f_rc2
));
562 case MT_OPERAND_RC3
:
563 errmsg
= parse_rc (cd
, strp
, MT_OPERAND_RC3
, (unsigned long *) (& fields
->f_rc3
));
565 case MT_OPERAND_RCNUM
:
566 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_RCNUM
, (unsigned long *) (& fields
->f_rcnum
));
568 case MT_OPERAND_RDA
:
569 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_RDA
, (unsigned long *) (& fields
->f_rda
));
571 case MT_OPERAND_ROWNUM
:
572 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_ROWNUM
, (unsigned long *) (& fields
->f_rownum
));
574 case MT_OPERAND_ROWNUM1
:
575 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_ROWNUM1
, (unsigned long *) (& fields
->f_rownum1
));
577 case MT_OPERAND_ROWNUM2
:
578 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_ROWNUM2
, (unsigned long *) (& fields
->f_rownum2
));
580 case MT_OPERAND_SIZE
:
581 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_SIZE
, (unsigned long *) (& fields
->f_size
));
583 case MT_OPERAND_TYPE
:
584 errmsg
= parse_type (cd
, strp
, MT_OPERAND_TYPE
, (unsigned long *) (& fields
->f_type
));
587 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MT_OPERAND_WR
, (unsigned long *) (& fields
->f_wr
));
589 case MT_OPERAND_XMODE
:
590 errmsg
= parse_xmode (cd
, strp
, MT_OPERAND_XMODE
, (unsigned long *) (& fields
->f_xmode
));
594 /* xgettext:c-format */
595 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
602 cgen_parse_fn
* const mt_cgen_parse_handlers
[] =
608 mt_cgen_init_asm (CGEN_CPU_DESC cd
)
610 mt_cgen_init_opcode_table (cd
);
611 mt_cgen_init_ibld_table (cd
);
612 cd
->parse_handlers
= & mt_cgen_parse_handlers
[0];
613 cd
->parse_operand
= mt_cgen_parse_operand
;
614 #ifdef CGEN_ASM_INIT_HOOK
621 /* Regex construction routine.
623 This translates an opcode syntax string into a regex string,
624 by replacing any non-character syntax element (such as an
625 opcode) with the pattern '.*'
627 It then compiles the regex and stores it in the opcode, for
628 later use by mt_cgen_assemble_insn
630 Returns NULL for success, an error message for failure. */
633 mt_cgen_build_insn_regex (CGEN_INSN
*insn
)
635 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
636 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
637 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
639 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
642 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
644 /* Mnemonics come first in the syntax string. */
645 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
646 return _("missing mnemonic in syntax string");
649 /* Generate a case sensitive regular expression that emulates case
650 insensitive matching in the "C" locale. We cannot generate a case
651 insensitive regular expression because in Turkish locales, 'i' and 'I'
652 are not equal modulo case conversion. */
654 /* Copy the literal mnemonic out of the insn. */
655 for (; *mnem
; mnem
++)
670 /* Copy any remaining literals from the syntax string into the rx. */
671 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
673 if (CGEN_SYNTAX_CHAR_P (* syn
))
675 char c
= CGEN_SYNTAX_CHAR (* syn
);
679 /* Escape any regex metacharacters in the syntax. */
680 case '.': case '[': case '\\':
681 case '*': case '^': case '$':
683 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
684 case '?': case '{': case '}':
685 case '(': case ')': case '*':
686 case '|': case '+': case ']':
707 /* Replace non-syntax fields with globs. */
713 /* Trailing whitespace ok. */
720 /* But anchor it after that. */
724 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
725 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
733 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
734 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
735 free (CGEN_INSN_RX (insn
));
736 (CGEN_INSN_RX (insn
)) = NULL
;
742 /* Default insn parser.
744 The syntax string is scanned and operands are parsed and stored in FIELDS.
745 Relocs are queued as we go via other callbacks.
747 ??? Note that this is currently an all-or-nothing parser. If we fail to
748 parse the instruction, we return 0 and the caller will start over from
749 the beginning. Backtracking will be necessary in parsing subexpressions,
750 but that can be handled there. Not handling backtracking here may get
751 expensive in the case of the m68k. Deal with later.
753 Returns NULL for success, an error message for failure. */
756 parse_insn_normal (CGEN_CPU_DESC cd
,
757 const CGEN_INSN
*insn
,
761 /* ??? Runtime added insns not handled yet. */
762 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
763 const char *str
= *strp
;
766 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
767 #ifdef CGEN_MNEMONIC_OPERANDS
772 /* For now we assume the mnemonic is first (there are no leading operands).
773 We can parse it without needing to set up operand parsing.
774 GAS's input scrubber will ensure mnemonics are lowercase, but we may
775 not be called from GAS. */
776 p
= CGEN_INSN_MNEMONIC (insn
);
777 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
781 return _("unrecognized instruction");
783 #ifndef CGEN_MNEMONIC_OPERANDS
784 if (* str
&& ! ISSPACE (* str
))
785 return _("unrecognized instruction");
788 CGEN_INIT_PARSE (cd
);
789 cgen_init_parse_operand (cd
);
790 #ifdef CGEN_MNEMONIC_OPERANDS
794 /* We don't check for (*str != '\0') here because we want to parse
795 any trailing fake arguments in the syntax string. */
796 syn
= CGEN_SYNTAX_STRING (syntax
);
798 /* Mnemonics come first for now, ensure valid string. */
799 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
806 /* Non operand chars must match exactly. */
807 if (CGEN_SYNTAX_CHAR_P (* syn
))
809 /* FIXME: While we allow for non-GAS callers above, we assume the
810 first char after the mnemonic part is a space. */
811 /* FIXME: We also take inappropriate advantage of the fact that
812 GAS's input scrubber will remove extraneous blanks. */
813 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
815 #ifdef CGEN_MNEMONIC_OPERANDS
816 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
824 /* Syntax char didn't match. Can't be this insn. */
825 static char msg
[80];
827 /* xgettext:c-format */
828 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
829 CGEN_SYNTAX_CHAR(*syn
), *str
);
834 /* Ran out of input. */
835 static char msg
[80];
837 /* xgettext:c-format */
838 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
839 CGEN_SYNTAX_CHAR(*syn
));
845 /* We have an operand of some sort. */
846 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
851 /* Done with this operand, continue with next one. */
855 /* If we're at the end of the syntax string, we're done. */
858 /* FIXME: For the moment we assume a valid `str' can only contain
859 blanks now. IE: We needn't try again with a longer version of
860 the insn and it is assumed that longer versions of insns appear
861 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
862 while (ISSPACE (* str
))
866 return _("junk at end of line"); /* FIXME: would like to include `str' */
871 /* We couldn't parse it. */
872 return _("unrecognized instruction");
876 This routine is called for each instruction to be assembled.
877 STR points to the insn to be assembled.
878 We assume all necessary tables have been initialized.
879 The assembled instruction, less any fixups, is stored in BUF.
880 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
881 still needs to be converted to target byte order, otherwise BUF is an array
882 of bytes in target byte order.
883 The result is a pointer to the insn's entry in the opcode table,
884 or NULL if an error occured (an error message will have already been
887 Note that when processing (non-alias) macro-insns,
888 this function recurses.
890 ??? It's possible to make this cpu-independent.
891 One would have to deal with a few minor things.
892 At this point in time doing so would be more of a curiosity than useful
893 [for example this file isn't _that_ big], but keeping the possibility in
894 mind helps keep the design clean. */
897 mt_cgen_assemble_insn (CGEN_CPU_DESC cd
,
900 CGEN_INSN_BYTES_PTR buf
,
904 CGEN_INSN_LIST
*ilist
;
905 const char *parse_errmsg
= NULL
;
906 const char *insert_errmsg
= NULL
;
907 int recognized_mnemonic
= 0;
909 /* Skip leading white space. */
910 while (ISSPACE (* str
))
913 /* The instructions are stored in hashed lists.
914 Get the first in the list. */
915 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
917 /* Keep looking until we find a match. */
919 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
921 const CGEN_INSN
*insn
= ilist
->insn
;
922 recognized_mnemonic
= 1;
924 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
925 /* Not usually needed as unsupported opcodes
926 shouldn't be in the hash lists. */
927 /* Is this insn supported by the selected cpu? */
928 if (! mt_cgen_insn_supported (cd
, insn
))
931 /* If the RELAXED attribute is set, this is an insn that shouldn't be
932 chosen immediately. Instead, it is used during assembler/linker
933 relaxation if possible. */
934 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
939 /* Skip this insn if str doesn't look right lexically. */
940 if (CGEN_INSN_RX (insn
) != NULL
&&
941 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
944 /* Allow parse/insert handlers to obtain length of insn. */
945 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
947 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
948 if (parse_errmsg
!= NULL
)
951 /* ??? 0 is passed for `pc'. */
952 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
954 if (insert_errmsg
!= NULL
)
957 /* It is up to the caller to actually output the insn and any
963 static char errbuf
[150];
964 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
965 const char *tmp_errmsg
;
967 /* If requesting verbose error messages, use insert_errmsg.
968 Failing that, use parse_errmsg. */
969 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
970 parse_errmsg
? parse_errmsg
:
971 recognized_mnemonic
?
972 _("unrecognized form of instruction") :
973 _("unrecognized instruction"));
975 if (strlen (start
) > 50)
976 /* xgettext:c-format */
977 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
979 /* xgettext:c-format */
980 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
982 if (strlen (start
) > 50)
983 /* xgettext:c-format */
984 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
986 /* xgettext:c-format */
987 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);