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, 2008
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. */
54 #define CGEN_VALIDATE_INSN_SUPPORTED
56 const char * parse_csrn (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
57 const char * parse_tpreg (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
58 const char * parse_spreg (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
59 const char * parse_mep_align (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, long *);
60 const char * parse_mep_alignu (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, unsigned long *);
61 static const char * parse_signed16 (CGEN_CPU_DESC
, const char **, int, long *);
62 static const char * parse_unsigned16 (CGEN_CPU_DESC
, const char **, int, unsigned long *);
63 static const char * parse_lo16 (CGEN_CPU_DESC
, const char **, int, long *, long);
64 static const char * parse_unsigned7 (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, unsigned long *);
65 static const char * parse_zero (CGEN_CPU_DESC
, const char **, int, long *);
68 parse_csrn (CGEN_CPU_DESC cd
, const char **strp
,
69 CGEN_KEYWORD
*keyword_table
, long *field
)
74 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
78 err
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CSRN_IDX
, & value
);
85 /* begin-cop-ip-parse-handlers */
86 /* end-cop-ip-parse-handlers */
89 parse_tpreg (CGEN_CPU_DESC cd
, const char ** strp
,
90 CGEN_KEYWORD
*keyword_table
, long *field
)
94 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
98 return _("Only $tp or $13 allowed for this opcode");
103 parse_spreg (CGEN_CPU_DESC cd
, const char ** strp
,
104 CGEN_KEYWORD
*keyword_table
, long *field
)
108 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
112 return _("Only $sp or $15 allowed for this opcode");
117 parse_mep_align (CGEN_CPU_DESC cd
, const char ** strp
,
118 enum cgen_operand_type type
, long *field
)
125 case MEP_OPERAND_PCREL8A2
:
126 case MEP_OPERAND_PCREL12A2
:
127 case MEP_OPERAND_PCREL17A2
:
128 case MEP_OPERAND_PCREL24A2
:
129 case MEP_OPERAND_CDISP8A2
:
130 case MEP_OPERAND_CDISP8A4
:
131 case MEP_OPERAND_CDISP8A8
:
132 err
= cgen_parse_signed_integer (cd
, strp
, type
, field
);
134 case MEP_OPERAND_PCABS24A2
:
135 case MEP_OPERAND_UDISP7
:
136 case MEP_OPERAND_UDISP7A2
:
137 case MEP_OPERAND_UDISP7A4
:
138 case MEP_OPERAND_UIMM7A4
:
139 case MEP_OPERAND_ADDR24A4
:
140 err
= cgen_parse_unsigned_integer (cd
, strp
, type
, (unsigned long *) field
);
149 case MEP_OPERAND_UDISP7
:
152 case MEP_OPERAND_PCREL8A2
:
153 case MEP_OPERAND_PCREL12A2
:
154 case MEP_OPERAND_PCREL17A2
:
155 case MEP_OPERAND_PCREL24A2
:
156 case MEP_OPERAND_PCABS24A2
:
157 case MEP_OPERAND_UDISP7A2
:
158 case MEP_OPERAND_CDISP8A2
:
161 case MEP_OPERAND_UDISP7A4
:
162 case MEP_OPERAND_UIMM7A4
:
163 case MEP_OPERAND_ADDR24A4
:
164 case MEP_OPERAND_CDISP8A4
:
167 case MEP_OPERAND_CDISP8A8
:
171 /* Safe assumption? */
175 return "Value is not aligned enough";
180 parse_mep_alignu (CGEN_CPU_DESC cd
, const char ** strp
,
181 enum cgen_operand_type type
, unsigned long *field
)
183 return parse_mep_align (cd
, strp
, type
, (long *) field
);
187 /* Handle %lo(), %tpoff(), %sdaoff(), %hi(), and other signed
188 constants in a signed context. */
191 parse_signed16 (CGEN_CPU_DESC cd
,
196 return parse_lo16 (cd
, strp
, opindex
, valuep
, 1);
200 parse_lo16 (CGEN_CPU_DESC cd
,
207 enum cgen_parse_operand_result result_type
;
210 if (strncasecmp (*strp
, "%lo(", 4) == 0)
213 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
214 & result_type
, & value
);
216 return _("missing `)'");
219 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
222 *valuep
= (long)(short) value
;
228 if (strncasecmp (*strp
, "%hi(", 4) == 0)
231 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
232 & result_type
, & value
);
234 return _("missing `)'");
237 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
238 value
= (value
+ 0x8000) >> 16;
243 if (strncasecmp (*strp
, "%uhi(", 5) == 0)
246 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
247 & result_type
, & value
);
249 return _("missing `)'");
252 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
258 if (strncasecmp (*strp
, "%sdaoff(", 8) == 0)
261 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
264 return _("missing `)'");
270 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
273 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
276 return _("missing `)'");
283 return _("invalid %function() here");
285 return cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
289 parse_unsigned16 (CGEN_CPU_DESC cd
,
292 unsigned long *valuep
)
294 return parse_lo16 (cd
, strp
, opindex
, (long *) valuep
, 0);
297 /* A special case of parse_signed16 which accepts only the value zero. */
300 parse_zero (CGEN_CPU_DESC cd
, const char **strp
, int opindex
, long *valuep
)
303 enum cgen_parse_operand_result result_type
;
306 /*fprintf(stderr, "dj: signed parse opindex `%s'\n", *strp);*/
308 /* Prevent ($ry) from being attempted as an expression on 'sw $rx,($ry)'.
309 It will fail and cause ry to be listed as an undefined symbol in the
311 if (strncmp (*strp
, "($", 2) == 0)
312 return "not zero"; /* any string will do -- will never be seen. */
314 if (strncasecmp (*strp
, "%lo(", 4) == 0)
317 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
318 &result_type
, &value
);
320 return "missing `)'";
323 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
324 return "not zero"; /* any string will do -- will never be seen. */
329 if (strncasecmp (*strp
, "%hi(", 4) == 0)
332 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
333 &result_type
, &value
);
335 return "missing `)'";
338 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
339 return "not zero"; /* any string will do -- will never be seen. */
344 if (strncasecmp (*strp
, "%uhi(", 5) == 0)
347 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
348 &result_type
, &value
);
350 return "missing `)'";
353 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
354 return "not zero"; /* any string will do -- will never be seen. */
359 if (strncasecmp (*strp
, "%sdaoff(", 8) == 0)
362 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
363 &result_type
, &value
);
365 return "missing `)'";
368 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
369 return "not zero"; /* any string will do -- will never be seen. */
374 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
377 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
378 &result_type
, &value
);
380 return "missing `)'";
383 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
384 return "not zero"; /* any string will do -- will never be seen. */
390 return "invalid %function() here";
392 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_NONE
,
393 &result_type
, &value
);
395 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
396 return "not zero"; /* any string will do -- will never be seen. */
402 parse_unsigned7 (CGEN_CPU_DESC cd
, const char **strp
,
403 enum cgen_operand_type opindex
, unsigned long *valuep
)
408 /* fprintf(stderr, "dj: unsigned7 parse `%s'\n", *strp); */
410 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
416 case MEP_OPERAND_UDISP7
:
417 reloc
= BFD_RELOC_MEP_TPREL7
;
419 case MEP_OPERAND_UDISP7A2
:
420 reloc
= BFD_RELOC_MEP_TPREL7A2
;
422 case MEP_OPERAND_UDISP7A4
:
423 reloc
= BFD_RELOC_MEP_TPREL7A4
;
426 /* Safe assumption? */
429 errmsg
= cgen_parse_address (cd
, strp
, opindex
, reloc
,
432 return "missing `)'";
439 return _("invalid %function() here");
441 return parse_mep_alignu (cd
, strp
, opindex
, valuep
);
444 /* BEGIN LIGHTWEIGHT MACRO PROCESSOR. */
462 { "sizeof", "(`1.end + (- `1))"},
463 { "startof", "(`1 | 0)" },
464 { "align4", "(`1&(~3))"},
465 /*{ "hi", "(((`1+0x8000)>>16) & 0xffff)" }, */
466 /*{ "lo", "(`1 & 0xffff)" }, */
467 /*{ "sdaoff", "((`1-__sdabase) & 0x7f)"}, */
468 /*{ "tpoff", "((`1-__tpbase) & 0x7f)"}, */
472 static char * expand_string (const char *, int);
475 mep_cgen_expand_macros_and_parse_operand
476 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
479 str_append (char *dest
, const char *input
, int len
)
486 /* printf("str_append: <<%s>>, <<%s>>, %d\n", dest, input, len); */
487 oldlen
= (dest
? strlen(dest
) : 0);
488 new_dest
= realloc (dest
, oldlen
+ len
+ 1);
489 memset (new_dest
+ oldlen
, 0, len
+ 1);
490 return strncat (new_dest
, input
, len
);
494 lookup_macro (const char *name
)
498 for (m
= macros
; m
->name
; ++m
)
499 if (strncmp (m
->name
, name
, strlen(m
->name
)) == 0)
506 expand_macro (arg
*args
, int narg
, macro
*mac
)
508 char *result
= 0, *rescanned_result
= 0;
509 char *e
= mac
->expansion
;
513 /* printf("expanding macro %s with %d args\n", mac->name, narg + 1); */
518 ((*(e
+ 1) - '1') <= MAXARGS
) &&
519 ((*(e
+ 1) - '1') <= narg
))
521 result
= str_append (result
, mark
, e
- mark
);
522 arg
= (*(e
+ 1) - '1');
523 /* printf("replacing `%d with %s\n", arg+1, args[arg].start); */
524 result
= str_append (result
, args
[arg
].start
, args
[arg
].len
);
532 result
= str_append (result
, mark
, e
- mark
);
536 rescanned_result
= expand_string (result
, 0);
538 return rescanned_result
;
548 expand_string (const char *in
, int first_only
)
550 int num_expansions
= 0;
555 const char *mark
= in
;
566 if (*in
== '%' && *(in
+ 1) && (!first_only
|| num_expansions
== 0))
568 macro
= lookup_macro (in
+ 1);
571 /* printf("entering state %d at '%s'...\n", state, in); */
572 result
= str_append (result
, mark
, in
- mark
);
574 in
+= 1 + strlen (macro
->name
);
575 while (*in
== ' ') ++in
;
585 args
[narg
].start
= in
+ 1;
599 args
[narg
].start
= (in
+ 1);
604 /* printf("entering state %d at '%s'...\n", state, in); */
608 expansion
= expand_macro (args
, narg
, macro
);
612 result
= str_append (result
, expansion
, strlen (expansion
));
618 result
= str_append (result
, mark
, in
- mark
);
643 result
= str_append (result
, mark
, in
- mark
);
653 /* END LIGHTWEIGHT MACRO PROCESSOR. */
655 const char * mep_cgen_parse_operand
656 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
659 mep_cgen_expand_macros_and_parse_operand (CGEN_CPU_DESC cd
, int opindex
,
660 const char ** strp_in
, CGEN_FIELDS
* fields
)
662 const char * errmsg
= NULL
;
663 char *str
= 0, *hold
= 0;
664 const char **strp
= 0;
666 /* Set up a new pointer to macro-expanded string. */
667 str
= expand_string (*strp_in
, 1);
668 /* fprintf (stderr, " expanded <<%s>> to <<%s>>\n", *strp_in, str); */
671 strp
= (const char **)(&str
);
673 errmsg
= mep_cgen_parse_operand (cd
, opindex
, strp
, fields
);
675 /* Now work out the advance. */
676 if (strlen (str
) == 0)
677 *strp_in
+= strlen (*strp_in
);
681 if (strstr (*strp_in
, str
))
682 /* A macro-expansion was pulled off the front. */
683 *strp_in
= strstr (*strp_in
, str
);
685 /* A non-macro-expansion was pulled off the front. */
686 *strp_in
+= (str
- hold
);
695 #define CGEN_ASM_INIT_HOOK (cd->parse_operand = mep_cgen_expand_macros_and_parse_operand);
699 const char * mep_cgen_parse_operand
700 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
702 /* Main entry point for operand parsing.
704 This function is basically just a big switch statement. Earlier versions
705 used tables to look up the function to use, but
706 - if the table contains both assembler and disassembler functions then
707 the disassembler contains much of the assembler and vice-versa,
708 - there's a lot of inlining possibilities as things grow,
709 - using a switch statement avoids the function call overhead.
711 This function could be moved into `parse_insn_normal', but keeping it
712 separate makes clear the interface between `parse_insn_normal' and each of
716 mep_cgen_parse_operand (CGEN_CPU_DESC cd
,
719 CGEN_FIELDS
* fields
)
721 const char * errmsg
= NULL
;
722 /* Used by scalar operands that still need to be parsed. */
723 long junk ATTRIBUTE_UNUSED
;
727 case MEP_OPERAND_ADDR24A4
:
728 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_ADDR24A4
, (unsigned long *) (& fields
->f_24u8a4n
));
730 case MEP_OPERAND_CALLNUM
:
731 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CALLNUM
, (unsigned long *) (& fields
->f_callnum
));
733 case MEP_OPERAND_CCCC
:
734 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CCCC
, (unsigned long *) (& fields
->f_rm
));
736 case MEP_OPERAND_CCRN
:
737 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & fields
->f_ccrn
);
739 case MEP_OPERAND_CDISP8
:
740 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_CDISP8
, (long *) (& fields
->f_8s24
));
742 case MEP_OPERAND_CDISP8A2
:
743 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A2
, (long *) (& fields
->f_8s24a2
));
745 case MEP_OPERAND_CDISP8A4
:
746 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A4
, (long *) (& fields
->f_8s24a4
));
748 case MEP_OPERAND_CDISP8A8
:
749 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A8
, (long *) (& fields
->f_8s24a8
));
751 case MEP_OPERAND_CIMM4
:
752 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM4
, (unsigned long *) (& fields
->f_rn
));
754 case MEP_OPERAND_CIMM5
:
755 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM5
, (unsigned long *) (& fields
->f_5u24
));
757 case MEP_OPERAND_CODE16
:
758 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE16
, (unsigned long *) (& fields
->f_16u16
));
760 case MEP_OPERAND_CODE24
:
761 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE24
, (unsigned long *) (& fields
->f_24u4n
));
763 case MEP_OPERAND_CP_FLAG
:
764 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & junk
);
766 case MEP_OPERAND_CRN
:
767 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crn
);
769 case MEP_OPERAND_CRN64
:
770 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crn
);
772 case MEP_OPERAND_CRNX
:
773 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crnx
);
775 case MEP_OPERAND_CRNX64
:
776 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crnx
);
778 case MEP_OPERAND_CSRN
:
779 errmsg
= parse_csrn (cd
, strp
, & mep_cgen_opval_h_csr
, & fields
->f_csrn
);
781 case MEP_OPERAND_CSRN_IDX
:
782 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CSRN_IDX
, (unsigned long *) (& fields
->f_csrn
));
784 case MEP_OPERAND_DBG
:
785 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
787 case MEP_OPERAND_DEPC
:
788 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
790 case MEP_OPERAND_EPC
:
791 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
793 case MEP_OPERAND_EXC
:
794 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
796 case MEP_OPERAND_HI
:
797 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
799 case MEP_OPERAND_LO
:
800 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
802 case MEP_OPERAND_LP
:
803 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
805 case MEP_OPERAND_MB0
:
806 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
808 case MEP_OPERAND_MB1
:
809 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
811 case MEP_OPERAND_ME0
:
812 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
814 case MEP_OPERAND_ME1
:
815 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
817 case MEP_OPERAND_NPC
:
818 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
820 case MEP_OPERAND_OPT
:
821 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
823 case MEP_OPERAND_PCABS24A2
:
824 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_PCABS24A2
, (unsigned long *) (& fields
->f_24u5a2n
));
826 case MEP_OPERAND_PCREL12A2
:
827 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL12A2
, (long *) (& fields
->f_12s4a2
));
829 case MEP_OPERAND_PCREL17A2
:
830 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL17A2
, (long *) (& fields
->f_17s16a2
));
832 case MEP_OPERAND_PCREL24A2
:
833 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL24A2
, (long *) (& fields
->f_24s5a2n
));
835 case MEP_OPERAND_PCREL8A2
:
836 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL8A2
, (long *) (& fields
->f_8s8a2
));
838 case MEP_OPERAND_PSW
:
839 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
841 case MEP_OPERAND_R0
:
842 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
844 case MEP_OPERAND_R1
:
845 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
847 case MEP_OPERAND_RL
:
848 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rl
);
850 case MEP_OPERAND_RM
:
851 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
853 case MEP_OPERAND_RMA
:
854 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
856 case MEP_OPERAND_RN
:
857 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
859 case MEP_OPERAND_RN3
:
860 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
862 case MEP_OPERAND_RN3C
:
863 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
865 case MEP_OPERAND_RN3L
:
866 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
868 case MEP_OPERAND_RN3S
:
869 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
871 case MEP_OPERAND_RN3UC
:
872 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
874 case MEP_OPERAND_RN3UL
:
875 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
877 case MEP_OPERAND_RN3US
:
878 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
880 case MEP_OPERAND_RNC
:
881 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
883 case MEP_OPERAND_RNL
:
884 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
886 case MEP_OPERAND_RNS
:
887 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
889 case MEP_OPERAND_RNUC
:
890 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
892 case MEP_OPERAND_RNUL
:
893 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
895 case MEP_OPERAND_RNUS
:
896 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
898 case MEP_OPERAND_SAR
:
899 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
901 case MEP_OPERAND_SDISP16
:
902 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SDISP16
, (long *) (& fields
->f_16s16
));
904 case MEP_OPERAND_SIMM16
:
905 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SIMM16
, (long *) (& fields
->f_16s16
));
907 case MEP_OPERAND_SIMM6
:
908 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM6
, (long *) (& fields
->f_6s8
));
910 case MEP_OPERAND_SIMM8
:
911 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM8
, (long *) (& fields
->f_8s8
));
913 case MEP_OPERAND_SP
:
914 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
916 case MEP_OPERAND_SPR
:
917 errmsg
= parse_spreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
919 case MEP_OPERAND_TP
:
920 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
922 case MEP_OPERAND_TPR
:
923 errmsg
= parse_tpreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
925 case MEP_OPERAND_UDISP2
:
926 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_UDISP2
, (long *) (& fields
->f_2u6
));
928 case MEP_OPERAND_UDISP7
:
929 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7
, (unsigned long *) (& fields
->f_7u9
));
931 case MEP_OPERAND_UDISP7A2
:
932 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A2
, (unsigned long *) (& fields
->f_7u9a2
));
934 case MEP_OPERAND_UDISP7A4
:
935 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A4
, (unsigned long *) (& fields
->f_7u9a4
));
937 case MEP_OPERAND_UIMM16
:
938 errmsg
= parse_unsigned16 (cd
, strp
, MEP_OPERAND_UIMM16
, (unsigned long *) (& fields
->f_16u16
));
940 case MEP_OPERAND_UIMM2
:
941 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM2
, (unsigned long *) (& fields
->f_2u10
));
943 case MEP_OPERAND_UIMM24
:
944 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM24
, (unsigned long *) (& fields
->f_24u8n
));
946 case MEP_OPERAND_UIMM3
:
947 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM3
, (unsigned long *) (& fields
->f_3u5
));
949 case MEP_OPERAND_UIMM4
:
950 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM4
, (unsigned long *) (& fields
->f_4u8
));
952 case MEP_OPERAND_UIMM5
:
953 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM5
, (unsigned long *) (& fields
->f_5u8
));
955 case MEP_OPERAND_UIMM7A4
:
956 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_UIMM7A4
, (unsigned long *) (& fields
->f_7u9a4
));
958 case MEP_OPERAND_ZERO
:
959 errmsg
= parse_zero (cd
, strp
, MEP_OPERAND_ZERO
, (long *) (& junk
));
963 /* xgettext:c-format */
964 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
971 cgen_parse_fn
* const mep_cgen_parse_handlers
[] =
977 mep_cgen_init_asm (CGEN_CPU_DESC cd
)
979 mep_cgen_init_opcode_table (cd
);
980 mep_cgen_init_ibld_table (cd
);
981 cd
->parse_handlers
= & mep_cgen_parse_handlers
[0];
982 cd
->parse_operand
= mep_cgen_parse_operand
;
983 #ifdef CGEN_ASM_INIT_HOOK
990 /* Regex construction routine.
992 This translates an opcode syntax string into a regex string,
993 by replacing any non-character syntax element (such as an
994 opcode) with the pattern '.*'
996 It then compiles the regex and stores it in the opcode, for
997 later use by mep_cgen_assemble_insn
999 Returns NULL for success, an error message for failure. */
1002 mep_cgen_build_insn_regex (CGEN_INSN
*insn
)
1004 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
1005 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
1006 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
1008 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
1011 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
1013 /* Mnemonics come first in the syntax string. */
1014 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1015 return _("missing mnemonic in syntax string");
1018 /* Generate a case sensitive regular expression that emulates case
1019 insensitive matching in the "C" locale. We cannot generate a case
1020 insensitive regular expression because in Turkish locales, 'i' and 'I'
1021 are not equal modulo case conversion. */
1023 /* Copy the literal mnemonic out of the insn. */
1024 for (; *mnem
; mnem
++)
1031 *rx
++ = TOLOWER (c
);
1032 *rx
++ = TOUPPER (c
);
1039 /* Copy any remaining literals from the syntax string into the rx. */
1040 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
1042 if (CGEN_SYNTAX_CHAR_P (* syn
))
1044 char c
= CGEN_SYNTAX_CHAR (* syn
);
1048 /* Escape any regex metacharacters in the syntax. */
1049 case '.': case '[': case '\\':
1050 case '*': case '^': case '$':
1052 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
1053 case '?': case '{': case '}':
1054 case '(': case ')': case '*':
1055 case '|': case '+': case ']':
1065 *rx
++ = TOLOWER (c
);
1066 *rx
++ = TOUPPER (c
);
1076 /* Replace non-syntax fields with globs. */
1082 /* Trailing whitespace ok. */
1089 /* But anchor it after that. */
1093 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
1094 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
1100 static char msg
[80];
1102 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
1103 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
1104 free (CGEN_INSN_RX (insn
));
1105 (CGEN_INSN_RX (insn
)) = NULL
;
1111 /* Default insn parser.
1113 The syntax string is scanned and operands are parsed and stored in FIELDS.
1114 Relocs are queued as we go via other callbacks.
1116 ??? Note that this is currently an all-or-nothing parser. If we fail to
1117 parse the instruction, we return 0 and the caller will start over from
1118 the beginning. Backtracking will be necessary in parsing subexpressions,
1119 but that can be handled there. Not handling backtracking here may get
1120 expensive in the case of the m68k. Deal with later.
1122 Returns NULL for success, an error message for failure. */
1125 parse_insn_normal (CGEN_CPU_DESC cd
,
1126 const CGEN_INSN
*insn
,
1128 CGEN_FIELDS
*fields
)
1130 /* ??? Runtime added insns not handled yet. */
1131 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
1132 const char *str
= *strp
;
1135 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
1136 #ifdef CGEN_MNEMONIC_OPERANDS
1141 /* For now we assume the mnemonic is first (there are no leading operands).
1142 We can parse it without needing to set up operand parsing.
1143 GAS's input scrubber will ensure mnemonics are lowercase, but we may
1144 not be called from GAS. */
1145 p
= CGEN_INSN_MNEMONIC (insn
);
1146 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
1150 return _("unrecognized instruction");
1152 #ifndef CGEN_MNEMONIC_OPERANDS
1153 if (* str
&& ! ISSPACE (* str
))
1154 return _("unrecognized instruction");
1157 CGEN_INIT_PARSE (cd
);
1158 cgen_init_parse_operand (cd
);
1159 #ifdef CGEN_MNEMONIC_OPERANDS
1163 /* We don't check for (*str != '\0') here because we want to parse
1164 any trailing fake arguments in the syntax string. */
1165 syn
= CGEN_SYNTAX_STRING (syntax
);
1167 /* Mnemonics come first for now, ensure valid string. */
1168 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1175 /* Non operand chars must match exactly. */
1176 if (CGEN_SYNTAX_CHAR_P (* syn
))
1178 /* FIXME: While we allow for non-GAS callers above, we assume the
1179 first char after the mnemonic part is a space. */
1180 /* FIXME: We also take inappropriate advantage of the fact that
1181 GAS's input scrubber will remove extraneous blanks. */
1182 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
1184 #ifdef CGEN_MNEMONIC_OPERANDS
1185 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
1193 /* Syntax char didn't match. Can't be this insn. */
1194 static char msg
[80];
1196 /* xgettext:c-format */
1197 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
1198 CGEN_SYNTAX_CHAR(*syn
), *str
);
1203 /* Ran out of input. */
1204 static char msg
[80];
1206 /* xgettext:c-format */
1207 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
1208 CGEN_SYNTAX_CHAR(*syn
));
1214 /* We have an operand of some sort. */
1215 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
1220 /* Done with this operand, continue with next one. */
1224 /* If we're at the end of the syntax string, we're done. */
1227 /* FIXME: For the moment we assume a valid `str' can only contain
1228 blanks now. IE: We needn't try again with a longer version of
1229 the insn and it is assumed that longer versions of insns appear
1230 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
1231 while (ISSPACE (* str
))
1235 return _("junk at end of line"); /* FIXME: would like to include `str' */
1240 /* We couldn't parse it. */
1241 return _("unrecognized instruction");
1244 /* Main entry point.
1245 This routine is called for each instruction to be assembled.
1246 STR points to the insn to be assembled.
1247 We assume all necessary tables have been initialized.
1248 The assembled instruction, less any fixups, is stored in BUF.
1249 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
1250 still needs to be converted to target byte order, otherwise BUF is an array
1251 of bytes in target byte order.
1252 The result is a pointer to the insn's entry in the opcode table,
1253 or NULL if an error occured (an error message will have already been
1256 Note that when processing (non-alias) macro-insns,
1257 this function recurses.
1259 ??? It's possible to make this cpu-independent.
1260 One would have to deal with a few minor things.
1261 At this point in time doing so would be more of a curiosity than useful
1262 [for example this file isn't _that_ big], but keeping the possibility in
1263 mind helps keep the design clean. */
1266 mep_cgen_assemble_insn (CGEN_CPU_DESC cd
,
1268 CGEN_FIELDS
*fields
,
1269 CGEN_INSN_BYTES_PTR buf
,
1273 CGEN_INSN_LIST
*ilist
;
1274 const char *parse_errmsg
= NULL
;
1275 const char *insert_errmsg
= NULL
;
1276 int recognized_mnemonic
= 0;
1278 /* Skip leading white space. */
1279 while (ISSPACE (* str
))
1282 /* The instructions are stored in hashed lists.
1283 Get the first in the list. */
1284 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
1286 /* Keep looking until we find a match. */
1288 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
1290 const CGEN_INSN
*insn
= ilist
->insn
;
1291 recognized_mnemonic
= 1;
1293 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
1294 /* Not usually needed as unsupported opcodes
1295 shouldn't be in the hash lists. */
1296 /* Is this insn supported by the selected cpu? */
1297 if (! mep_cgen_insn_supported (cd
, insn
))
1300 /* If the RELAXED attribute is set, this is an insn that shouldn't be
1301 chosen immediately. Instead, it is used during assembler/linker
1302 relaxation if possible. */
1303 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
1308 /* Skip this insn if str doesn't look right lexically. */
1309 if (CGEN_INSN_RX (insn
) != NULL
&&
1310 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
1313 /* Allow parse/insert handlers to obtain length of insn. */
1314 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
1316 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
1317 if (parse_errmsg
!= NULL
)
1320 /* ??? 0 is passed for `pc'. */
1321 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
1323 if (insert_errmsg
!= NULL
)
1326 /* It is up to the caller to actually output the insn and any
1332 static char errbuf
[150];
1333 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
1334 const char *tmp_errmsg
;
1336 /* If requesting verbose error messages, use insert_errmsg.
1337 Failing that, use parse_errmsg. */
1338 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
1339 parse_errmsg
? parse_errmsg
:
1340 recognized_mnemonic
?
1341 _("unrecognized form of instruction") :
1342 _("unrecognized instruction"));
1344 if (strlen (start
) > 50)
1345 /* xgettext:c-format */
1346 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
1348 /* xgettext:c-format */
1349 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
1351 if (strlen (start
) > 50)
1352 /* xgettext:c-format */
1353 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
1355 /* xgettext:c-format */
1356 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);