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
8 Free Software Foundation, Inc.
10 This file is part of the GNU Binutils and GDB, the GNU debugger.
12 This program 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 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public 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. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
38 #include "libiberty.h"
39 #include "safe-ctype.h"
42 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #define max(a,b) ((a) > (b) ? (a) : (b))
46 static const char * parse_insn_normal
47 (CGEN_CPU_DESC
, const CGEN_INSN
*, const char **, CGEN_FIELDS
*);
49 /* -- assembler routines inserted here. */
53 #define CGEN_VALIDATE_INSN_SUPPORTED
55 const char * parse_csrn (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
56 const char * parse_tpreg (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
57 const char * parse_spreg (CGEN_CPU_DESC
, const char **, CGEN_KEYWORD
*, long *);
58 const char * parse_mep_align (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, long *);
59 const char * parse_mep_alignu (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, unsigned long *);
60 static const char * parse_signed16 (CGEN_CPU_DESC
, const char **, int, long *);
61 static const char * parse_unsigned16 (CGEN_CPU_DESC
, const char **, int, unsigned long *);
62 static const char * parse_lo16 (CGEN_CPU_DESC
, const char **, int, long *, long);
63 static const char * parse_unsigned7 (CGEN_CPU_DESC
, const char **, enum cgen_operand_type
, unsigned long *);
64 static const char * parse_zero (CGEN_CPU_DESC
, const char **, int, long *);
67 parse_csrn (CGEN_CPU_DESC cd
, const char **strp
,
68 CGEN_KEYWORD
*keyword_table
, long *field
)
73 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
77 err
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CSRN_IDX
, & value
);
84 /* begin-cop-ip-parse-handlers */
86 parse_fmax_cr (CGEN_CPU_DESC cd
,
88 CGEN_KEYWORD
*keyword_table ATTRIBUTE_UNUSED
,
91 return cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr_fmax
, field
);
94 parse_fmax_ccr (CGEN_CPU_DESC cd
,
96 CGEN_KEYWORD
*keyword_table ATTRIBUTE_UNUSED
,
99 return cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr_fmax
, field
);
101 /* end-cop-ip-parse-handlers */
104 parse_tpreg (CGEN_CPU_DESC cd
, const char ** strp
,
105 CGEN_KEYWORD
*keyword_table
, long *field
)
109 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
113 return _("Only $tp or $13 allowed for this opcode");
118 parse_spreg (CGEN_CPU_DESC cd
, const char ** strp
,
119 CGEN_KEYWORD
*keyword_table
, long *field
)
123 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
127 return _("Only $sp or $15 allowed for this opcode");
132 parse_mep_align (CGEN_CPU_DESC cd
, const char ** strp
,
133 enum cgen_operand_type type
, long *field
)
140 case MEP_OPERAND_PCREL8A2
:
141 case MEP_OPERAND_PCREL12A2
:
142 case MEP_OPERAND_PCREL17A2
:
143 case MEP_OPERAND_PCREL24A2
:
144 case MEP_OPERAND_CDISP8A2
:
145 case MEP_OPERAND_CDISP8A4
:
146 case MEP_OPERAND_CDISP8A8
:
147 err
= cgen_parse_signed_integer (cd
, strp
, type
, field
);
149 case MEP_OPERAND_PCABS24A2
:
150 case MEP_OPERAND_UDISP7
:
151 case MEP_OPERAND_UDISP7A2
:
152 case MEP_OPERAND_UDISP7A4
:
153 case MEP_OPERAND_UIMM7A4
:
154 case MEP_OPERAND_ADDR24A4
:
155 err
= cgen_parse_unsigned_integer (cd
, strp
, type
, (unsigned long *) field
);
164 case MEP_OPERAND_UDISP7
:
167 case MEP_OPERAND_PCREL8A2
:
168 case MEP_OPERAND_PCREL12A2
:
169 case MEP_OPERAND_PCREL17A2
:
170 case MEP_OPERAND_PCREL24A2
:
171 case MEP_OPERAND_PCABS24A2
:
172 case MEP_OPERAND_UDISP7A2
:
173 case MEP_OPERAND_CDISP8A2
:
176 case MEP_OPERAND_UDISP7A4
:
177 case MEP_OPERAND_UIMM7A4
:
178 case MEP_OPERAND_ADDR24A4
:
179 case MEP_OPERAND_CDISP8A4
:
182 case MEP_OPERAND_CDISP8A8
:
186 /* Safe assumption? */
190 return "Value is not aligned enough";
195 parse_mep_alignu (CGEN_CPU_DESC cd
, const char ** strp
,
196 enum cgen_operand_type type
, unsigned long *field
)
198 return parse_mep_align (cd
, strp
, type
, (long *) field
);
202 /* Handle %lo(), %tpoff(), %sdaoff(), %hi(), and other signed
203 constants in a signed context. */
206 parse_signed16 (CGEN_CPU_DESC cd
,
211 return parse_lo16 (cd
, strp
, opindex
, valuep
, 1);
215 parse_lo16 (CGEN_CPU_DESC cd
,
222 enum cgen_parse_operand_result result_type
;
225 if (strncasecmp (*strp
, "%lo(", 4) == 0)
228 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
229 & result_type
, & value
);
231 return _("missing `)'");
234 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
237 *valuep
= (long)(short) value
;
243 if (strncasecmp (*strp
, "%hi(", 4) == 0)
246 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
247 & result_type
, & value
);
249 return _("missing `)'");
252 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
253 value
= (value
+ 0x8000) >> 16;
258 if (strncasecmp (*strp
, "%uhi(", 5) == 0)
261 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
262 & result_type
, & value
);
264 return _("missing `)'");
267 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
273 if (strncasecmp (*strp
, "%sdaoff(", 8) == 0)
276 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
279 return _("missing `)'");
285 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
288 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
291 return _("missing `)'");
298 return _("invalid %function() here");
300 return cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
304 parse_unsigned16 (CGEN_CPU_DESC cd
,
307 unsigned long *valuep
)
309 return parse_lo16 (cd
, strp
, opindex
, (long *) valuep
, 0);
312 /* A special case of parse_signed16 which accepts only the value zero. */
315 parse_zero (CGEN_CPU_DESC cd
, const char **strp
, int opindex
, long *valuep
)
318 enum cgen_parse_operand_result result_type
;
321 /*fprintf(stderr, "dj: signed parse opindex `%s'\n", *strp);*/
323 /* Prevent ($ry) from being attempted as an expression on 'sw $rx,($ry)'.
324 It will fail and cause ry to be listed as an undefined symbol in the
326 if (strncmp (*strp
, "($", 2) == 0)
327 return "not zero"; /* any string will do -- will never be seen. */
329 if (strncasecmp (*strp
, "%lo(", 4) == 0)
332 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
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
, "%hi(", 4) == 0)
347 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
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
, "%uhi(", 5) == 0)
362 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
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
, "%sdaoff(", 8) == 0)
377 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
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. */
389 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
392 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
393 &result_type
, &value
);
395 return "missing `)'";
398 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
399 return "not zero"; /* any string will do -- will never be seen. */
405 return "invalid %function() here";
407 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_NONE
,
408 &result_type
, &value
);
410 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
411 return "not zero"; /* any string will do -- will never be seen. */
417 parse_unsigned7 (CGEN_CPU_DESC cd
, const char **strp
,
418 enum cgen_operand_type opindex
, unsigned long *valuep
)
423 /* fprintf(stderr, "dj: unsigned7 parse `%s'\n", *strp); */
425 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
431 case MEP_OPERAND_UDISP7
:
432 reloc
= BFD_RELOC_MEP_TPREL7
;
434 case MEP_OPERAND_UDISP7A2
:
435 reloc
= BFD_RELOC_MEP_TPREL7A2
;
437 case MEP_OPERAND_UDISP7A4
:
438 reloc
= BFD_RELOC_MEP_TPREL7A4
;
441 /* Safe assumption? */
444 errmsg
= cgen_parse_address (cd
, strp
, opindex
, reloc
,
447 return "missing `)'";
454 return _("invalid %function() here");
456 return parse_mep_alignu (cd
, strp
, opindex
, valuep
);
459 /* BEGIN LIGHTWEIGHT MACRO PROCESSOR. */
477 { "sizeof", "(`1.end + (- `1))"},
478 { "startof", "(`1 | 0)" },
479 { "align4", "(`1&(~3))"},
480 /*{ "hi", "(((`1+0x8000)>>16) & 0xffff)" }, */
481 /*{ "lo", "(`1 & 0xffff)" }, */
482 /*{ "sdaoff", "((`1-__sdabase) & 0x7f)"}, */
483 /*{ "tpoff", "((`1-__tpbase) & 0x7f)"}, */
487 static char * expand_string (const char *, int);
490 mep_cgen_expand_macros_and_parse_operand
491 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
494 str_append (char *dest
, const char *input
, int len
)
501 /* printf("str_append: <<%s>>, <<%s>>, %d\n", dest, input, len); */
502 oldlen
= (dest
? strlen(dest
) : 0);
503 new_dest
= realloc (dest
, oldlen
+ len
+ 1);
504 memset (new_dest
+ oldlen
, 0, len
+ 1);
505 return strncat (new_dest
, input
, len
);
509 lookup_macro (const char *name
)
513 for (m
= macros
; m
->name
; ++m
)
514 if (strncmp (m
->name
, name
, strlen(m
->name
)) == 0)
521 expand_macro (arg
*args
, int narg
, macro
*mac
)
523 char *result
= 0, *rescanned_result
= 0;
524 char *e
= mac
->expansion
;
528 /* printf("expanding macro %s with %d args\n", mac->name, narg + 1); */
533 ((*(e
+ 1) - '1') <= MAXARGS
) &&
534 ((*(e
+ 1) - '1') <= narg
))
536 result
= str_append (result
, mark
, e
- mark
);
537 arg
= (*(e
+ 1) - '1');
538 /* printf("replacing `%d with %s\n", arg+1, args[arg].start); */
539 result
= str_append (result
, args
[arg
].start
, args
[arg
].len
);
547 result
= str_append (result
, mark
, e
- mark
);
551 rescanned_result
= expand_string (result
, 0);
553 return rescanned_result
;
563 expand_string (const char *in
, int first_only
)
565 int num_expansions
= 0;
570 const char *mark
= in
;
581 if (*in
== '%' && *(in
+ 1) && (!first_only
|| num_expansions
== 0))
583 macro
= lookup_macro (in
+ 1);
586 /* printf("entering state %d at '%s'...\n", state, in); */
587 result
= str_append (result
, mark
, in
- mark
);
589 in
+= 1 + strlen (macro
->name
);
590 while (*in
== ' ') ++in
;
600 args
[narg
].start
= in
+ 1;
614 args
[narg
].start
= (in
+ 1);
619 /* printf("entering state %d at '%s'...\n", state, in); */
623 expansion
= expand_macro (args
, narg
, macro
);
627 result
= str_append (result
, expansion
, strlen (expansion
));
633 result
= str_append (result
, mark
, in
- mark
);
658 result
= str_append (result
, mark
, in
- mark
);
668 /* END LIGHTWEIGHT MACRO PROCESSOR. */
670 const char * mep_cgen_parse_operand
671 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
674 mep_cgen_expand_macros_and_parse_operand (CGEN_CPU_DESC cd
, int opindex
,
675 const char ** strp_in
, CGEN_FIELDS
* fields
)
677 const char * errmsg
= NULL
;
678 char *str
= 0, *hold
= 0;
679 const char **strp
= 0;
681 /* Set up a new pointer to macro-expanded string. */
682 str
= expand_string (*strp_in
, 1);
683 /* fprintf (stderr, " expanded <<%s>> to <<%s>>\n", *strp_in, str); */
686 strp
= (const char **)(&str
);
688 errmsg
= mep_cgen_parse_operand (cd
, opindex
, strp
, fields
);
690 /* Now work out the advance. */
691 if (strlen (str
) == 0)
692 *strp_in
+= strlen (*strp_in
);
696 if (strstr (*strp_in
, str
))
697 /* A macro-expansion was pulled off the front. */
698 *strp_in
= strstr (*strp_in
, str
);
700 /* A non-macro-expansion was pulled off the front. */
701 *strp_in
+= (str
- hold
);
710 #define CGEN_ASM_INIT_HOOK (cd->parse_operand = mep_cgen_expand_macros_and_parse_operand);
714 const char * mep_cgen_parse_operand
715 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
717 /* Main entry point for operand parsing.
719 This function is basically just a big switch statement. Earlier versions
720 used tables to look up the function to use, but
721 - if the table contains both assembler and disassembler functions then
722 the disassembler contains much of the assembler and vice-versa,
723 - there's a lot of inlining possibilities as things grow,
724 - using a switch statement avoids the function call overhead.
726 This function could be moved into `parse_insn_normal', but keeping it
727 separate makes clear the interface between `parse_insn_normal' and each of
731 mep_cgen_parse_operand (CGEN_CPU_DESC cd
,
734 CGEN_FIELDS
* fields
)
736 const char * errmsg
= NULL
;
737 /* Used by scalar operands that still need to be parsed. */
738 long junk ATTRIBUTE_UNUSED
;
742 case MEP_OPERAND_ADDR24A4
:
743 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_ADDR24A4
, (unsigned long *) (& fields
->f_24u8a4n
));
745 case MEP_OPERAND_CALLNUM
:
746 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CALLNUM
, (unsigned long *) (& fields
->f_callnum
));
748 case MEP_OPERAND_CCCC
:
749 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CCCC
, (unsigned long *) (& fields
->f_rm
));
751 case MEP_OPERAND_CCRN
:
752 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & fields
->f_ccrn
);
754 case MEP_OPERAND_CDISP8
:
755 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_CDISP8
, (long *) (& fields
->f_8s24
));
757 case MEP_OPERAND_CDISP8A2
:
758 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A2
, (long *) (& fields
->f_8s24a2
));
760 case MEP_OPERAND_CDISP8A4
:
761 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A4
, (long *) (& fields
->f_8s24a4
));
763 case MEP_OPERAND_CDISP8A8
:
764 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A8
, (long *) (& fields
->f_8s24a8
));
766 case MEP_OPERAND_CIMM4
:
767 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM4
, (unsigned long *) (& fields
->f_rn
));
769 case MEP_OPERAND_CIMM5
:
770 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM5
, (unsigned long *) (& fields
->f_5u24
));
772 case MEP_OPERAND_CODE16
:
773 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE16
, (unsigned long *) (& fields
->f_16u16
));
775 case MEP_OPERAND_CODE24
:
776 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE24
, (unsigned long *) (& fields
->f_24u4n
));
778 case MEP_OPERAND_CP_FLAG
:
779 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & junk
);
781 case MEP_OPERAND_CRN
:
782 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crn
);
784 case MEP_OPERAND_CRN64
:
785 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crn
);
787 case MEP_OPERAND_CRNX
:
788 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crnx
);
790 case MEP_OPERAND_CRNX64
:
791 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crnx
);
793 case MEP_OPERAND_CSRN
:
794 errmsg
= parse_csrn (cd
, strp
, & mep_cgen_opval_h_csr
, & fields
->f_csrn
);
796 case MEP_OPERAND_CSRN_IDX
:
797 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CSRN_IDX
, (unsigned long *) (& fields
->f_csrn
));
799 case MEP_OPERAND_DBG
:
800 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
802 case MEP_OPERAND_DEPC
:
803 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
805 case MEP_OPERAND_EPC
:
806 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
808 case MEP_OPERAND_EXC
:
809 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
811 case MEP_OPERAND_FMAX_CCRN
:
812 errmsg
= parse_fmax_ccr (cd
, strp
, & mep_cgen_opval_h_ccr
, & fields
->f_fmax_4_4
);
814 case MEP_OPERAND_FMAX_FRD
:
815 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frd
);
817 case MEP_OPERAND_FMAX_FRD_INT
:
818 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frd
);
820 case MEP_OPERAND_FMAX_FRM
:
821 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frm
);
823 case MEP_OPERAND_FMAX_FRN
:
824 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frn
);
826 case MEP_OPERAND_FMAX_FRN_INT
:
827 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frn
);
829 case MEP_OPERAND_FMAX_RM
:
830 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_fmax_rm
);
832 case MEP_OPERAND_HI
:
833 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
835 case MEP_OPERAND_LO
:
836 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
838 case MEP_OPERAND_LP
:
839 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
841 case MEP_OPERAND_MB0
:
842 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
844 case MEP_OPERAND_MB1
:
845 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
847 case MEP_OPERAND_ME0
:
848 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
850 case MEP_OPERAND_ME1
:
851 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
853 case MEP_OPERAND_NPC
:
854 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
856 case MEP_OPERAND_OPT
:
857 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
859 case MEP_OPERAND_PCABS24A2
:
860 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_PCABS24A2
, (unsigned long *) (& fields
->f_24u5a2n
));
862 case MEP_OPERAND_PCREL12A2
:
863 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL12A2
, (long *) (& fields
->f_12s4a2
));
865 case MEP_OPERAND_PCREL17A2
:
866 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL17A2
, (long *) (& fields
->f_17s16a2
));
868 case MEP_OPERAND_PCREL24A2
:
869 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL24A2
, (long *) (& fields
->f_24s5a2n
));
871 case MEP_OPERAND_PCREL8A2
:
872 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL8A2
, (long *) (& fields
->f_8s8a2
));
874 case MEP_OPERAND_PSW
:
875 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
877 case MEP_OPERAND_R0
:
878 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
880 case MEP_OPERAND_R1
:
881 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
883 case MEP_OPERAND_RL
:
884 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rl
);
886 case MEP_OPERAND_RM
:
887 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
889 case MEP_OPERAND_RMA
:
890 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
892 case MEP_OPERAND_RN
:
893 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
895 case MEP_OPERAND_RN3
:
896 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
898 case MEP_OPERAND_RN3C
:
899 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
901 case MEP_OPERAND_RN3L
:
902 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
904 case MEP_OPERAND_RN3S
:
905 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
907 case MEP_OPERAND_RN3UC
:
908 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
910 case MEP_OPERAND_RN3UL
:
911 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
913 case MEP_OPERAND_RN3US
:
914 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
916 case MEP_OPERAND_RNC
:
917 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
919 case MEP_OPERAND_RNL
:
920 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
922 case MEP_OPERAND_RNS
:
923 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
925 case MEP_OPERAND_RNUC
:
926 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
928 case MEP_OPERAND_RNUL
:
929 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
931 case MEP_OPERAND_RNUS
:
932 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
934 case MEP_OPERAND_SAR
:
935 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
937 case MEP_OPERAND_SDISP16
:
938 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SDISP16
, (long *) (& fields
->f_16s16
));
940 case MEP_OPERAND_SIMM16
:
941 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SIMM16
, (long *) (& fields
->f_16s16
));
943 case MEP_OPERAND_SIMM6
:
944 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM6
, (long *) (& fields
->f_6s8
));
946 case MEP_OPERAND_SIMM8
:
947 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM8
, (long *) (& fields
->f_8s8
));
949 case MEP_OPERAND_SP
:
950 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
952 case MEP_OPERAND_SPR
:
953 errmsg
= parse_spreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
955 case MEP_OPERAND_TP
:
956 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
958 case MEP_OPERAND_TPR
:
959 errmsg
= parse_tpreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
961 case MEP_OPERAND_UDISP2
:
962 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_UDISP2
, (long *) (& fields
->f_2u6
));
964 case MEP_OPERAND_UDISP7
:
965 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7
, (unsigned long *) (& fields
->f_7u9
));
967 case MEP_OPERAND_UDISP7A2
:
968 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A2
, (unsigned long *) (& fields
->f_7u9a2
));
970 case MEP_OPERAND_UDISP7A4
:
971 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A4
, (unsigned long *) (& fields
->f_7u9a4
));
973 case MEP_OPERAND_UIMM16
:
974 errmsg
= parse_unsigned16 (cd
, strp
, MEP_OPERAND_UIMM16
, (unsigned long *) (& fields
->f_16u16
));
976 case MEP_OPERAND_UIMM2
:
977 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM2
, (unsigned long *) (& fields
->f_2u10
));
979 case MEP_OPERAND_UIMM24
:
980 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM24
, (unsigned long *) (& fields
->f_24u8n
));
982 case MEP_OPERAND_UIMM3
:
983 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM3
, (unsigned long *) (& fields
->f_3u5
));
985 case MEP_OPERAND_UIMM4
:
986 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM4
, (unsigned long *) (& fields
->f_4u8
));
988 case MEP_OPERAND_UIMM5
:
989 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM5
, (unsigned long *) (& fields
->f_5u8
));
991 case MEP_OPERAND_UIMM7A4
:
992 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_UIMM7A4
, (unsigned long *) (& fields
->f_7u9a4
));
994 case MEP_OPERAND_ZERO
:
995 errmsg
= parse_zero (cd
, strp
, MEP_OPERAND_ZERO
, (long *) (& junk
));
999 /* xgettext:c-format */
1000 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
1007 cgen_parse_fn
* const mep_cgen_parse_handlers
[] =
1013 mep_cgen_init_asm (CGEN_CPU_DESC cd
)
1015 mep_cgen_init_opcode_table (cd
);
1016 mep_cgen_init_ibld_table (cd
);
1017 cd
->parse_handlers
= & mep_cgen_parse_handlers
[0];
1018 cd
->parse_operand
= mep_cgen_parse_operand
;
1019 #ifdef CGEN_ASM_INIT_HOOK
1026 /* Regex construction routine.
1028 This translates an opcode syntax string into a regex string,
1029 by replacing any non-character syntax element (such as an
1030 opcode) with the pattern '.*'
1032 It then compiles the regex and stores it in the opcode, for
1033 later use by mep_cgen_assemble_insn
1035 Returns NULL for success, an error message for failure. */
1038 mep_cgen_build_insn_regex (CGEN_INSN
*insn
)
1040 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
1041 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
1042 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
1044 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
1047 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
1049 /* Mnemonics come first in the syntax string. */
1050 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1051 return _("missing mnemonic in syntax string");
1054 /* Generate a case sensitive regular expression that emulates case
1055 insensitive matching in the "C" locale. We cannot generate a case
1056 insensitive regular expression because in Turkish locales, 'i' and 'I'
1057 are not equal modulo case conversion. */
1059 /* Copy the literal mnemonic out of the insn. */
1060 for (; *mnem
; mnem
++)
1067 *rx
++ = TOLOWER (c
);
1068 *rx
++ = TOUPPER (c
);
1075 /* Copy any remaining literals from the syntax string into the rx. */
1076 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
1078 if (CGEN_SYNTAX_CHAR_P (* syn
))
1080 char c
= CGEN_SYNTAX_CHAR (* syn
);
1084 /* Escape any regex metacharacters in the syntax. */
1085 case '.': case '[': case '\\':
1086 case '*': case '^': case '$':
1088 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
1089 case '?': case '{': case '}':
1090 case '(': case ')': case '*':
1091 case '|': case '+': case ']':
1101 *rx
++ = TOLOWER (c
);
1102 *rx
++ = TOUPPER (c
);
1112 /* Replace non-syntax fields with globs. */
1118 /* Trailing whitespace ok. */
1125 /* But anchor it after that. */
1129 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
1130 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
1136 static char msg
[80];
1138 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
1139 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
1140 free (CGEN_INSN_RX (insn
));
1141 (CGEN_INSN_RX (insn
)) = NULL
;
1147 /* Default insn parser.
1149 The syntax string is scanned and operands are parsed and stored in FIELDS.
1150 Relocs are queued as we go via other callbacks.
1152 ??? Note that this is currently an all-or-nothing parser. If we fail to
1153 parse the instruction, we return 0 and the caller will start over from
1154 the beginning. Backtracking will be necessary in parsing subexpressions,
1155 but that can be handled there. Not handling backtracking here may get
1156 expensive in the case of the m68k. Deal with later.
1158 Returns NULL for success, an error message for failure. */
1161 parse_insn_normal (CGEN_CPU_DESC cd
,
1162 const CGEN_INSN
*insn
,
1164 CGEN_FIELDS
*fields
)
1166 /* ??? Runtime added insns not handled yet. */
1167 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
1168 const char *str
= *strp
;
1171 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
1172 #ifdef CGEN_MNEMONIC_OPERANDS
1177 /* For now we assume the mnemonic is first (there are no leading operands).
1178 We can parse it without needing to set up operand parsing.
1179 GAS's input scrubber will ensure mnemonics are lowercase, but we may
1180 not be called from GAS. */
1181 p
= CGEN_INSN_MNEMONIC (insn
);
1182 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
1186 return _("unrecognized instruction");
1188 #ifndef CGEN_MNEMONIC_OPERANDS
1189 if (* str
&& ! ISSPACE (* str
))
1190 return _("unrecognized instruction");
1193 CGEN_INIT_PARSE (cd
);
1194 cgen_init_parse_operand (cd
);
1195 #ifdef CGEN_MNEMONIC_OPERANDS
1199 /* We don't check for (*str != '\0') here because we want to parse
1200 any trailing fake arguments in the syntax string. */
1201 syn
= CGEN_SYNTAX_STRING (syntax
);
1203 /* Mnemonics come first for now, ensure valid string. */
1204 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1211 /* Non operand chars must match exactly. */
1212 if (CGEN_SYNTAX_CHAR_P (* syn
))
1214 /* FIXME: While we allow for non-GAS callers above, we assume the
1215 first char after the mnemonic part is a space. */
1216 /* FIXME: We also take inappropriate advantage of the fact that
1217 GAS's input scrubber will remove extraneous blanks. */
1218 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
1220 #ifdef CGEN_MNEMONIC_OPERANDS
1221 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
1229 /* Syntax char didn't match. Can't be this insn. */
1230 static char msg
[80];
1232 /* xgettext:c-format */
1233 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
1234 CGEN_SYNTAX_CHAR(*syn
), *str
);
1239 /* Ran out of input. */
1240 static char msg
[80];
1242 /* xgettext:c-format */
1243 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
1244 CGEN_SYNTAX_CHAR(*syn
));
1250 /* We have an operand of some sort. */
1251 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
1256 /* Done with this operand, continue with next one. */
1260 /* If we're at the end of the syntax string, we're done. */
1263 /* FIXME: For the moment we assume a valid `str' can only contain
1264 blanks now. IE: We needn't try again with a longer version of
1265 the insn and it is assumed that longer versions of insns appear
1266 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
1267 while (ISSPACE (* str
))
1271 return _("junk at end of line"); /* FIXME: would like to include `str' */
1276 /* We couldn't parse it. */
1277 return _("unrecognized instruction");
1280 /* Main entry point.
1281 This routine is called for each instruction to be assembled.
1282 STR points to the insn to be assembled.
1283 We assume all necessary tables have been initialized.
1284 The assembled instruction, less any fixups, is stored in BUF.
1285 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
1286 still needs to be converted to target byte order, otherwise BUF is an array
1287 of bytes in target byte order.
1288 The result is a pointer to the insn's entry in the opcode table,
1289 or NULL if an error occured (an error message will have already been
1292 Note that when processing (non-alias) macro-insns,
1293 this function recurses.
1295 ??? It's possible to make this cpu-independent.
1296 One would have to deal with a few minor things.
1297 At this point in time doing so would be more of a curiosity than useful
1298 [for example this file isn't _that_ big], but keeping the possibility in
1299 mind helps keep the design clean. */
1302 mep_cgen_assemble_insn (CGEN_CPU_DESC cd
,
1304 CGEN_FIELDS
*fields
,
1305 CGEN_INSN_BYTES_PTR buf
,
1309 CGEN_INSN_LIST
*ilist
;
1310 const char *parse_errmsg
= NULL
;
1311 const char *insert_errmsg
= NULL
;
1312 int recognized_mnemonic
= 0;
1314 /* Skip leading white space. */
1315 while (ISSPACE (* str
))
1318 /* The instructions are stored in hashed lists.
1319 Get the first in the list. */
1320 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
1322 /* Keep looking until we find a match. */
1324 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
1326 const CGEN_INSN
*insn
= ilist
->insn
;
1327 recognized_mnemonic
= 1;
1329 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
1330 /* Not usually needed as unsupported opcodes
1331 shouldn't be in the hash lists. */
1332 /* Is this insn supported by the selected cpu? */
1333 if (! mep_cgen_insn_supported (cd
, insn
))
1336 /* If the RELAXED attribute is set, this is an insn that shouldn't be
1337 chosen immediately. Instead, it is used during assembler/linker
1338 relaxation if possible. */
1339 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
1344 /* Skip this insn if str doesn't look right lexically. */
1345 if (CGEN_INSN_RX (insn
) != NULL
&&
1346 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
1349 /* Allow parse/insert handlers to obtain length of insn. */
1350 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
1352 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
1353 if (parse_errmsg
!= NULL
)
1356 /* ??? 0 is passed for `pc'. */
1357 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
1359 if (insert_errmsg
!= NULL
)
1362 /* It is up to the caller to actually output the insn and any
1368 static char errbuf
[150];
1369 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
1370 const char *tmp_errmsg
;
1372 /* If requesting verbose error messages, use insert_errmsg.
1373 Failing that, use parse_errmsg. */
1374 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
1375 parse_errmsg
? parse_errmsg
:
1376 recognized_mnemonic
?
1377 _("unrecognized form of instruction") :
1378 _("unrecognized instruction"));
1380 if (strlen (start
) > 50)
1381 /* xgettext:c-format */
1382 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
1384 /* xgettext:c-format */
1385 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
1387 if (strlen (start
) > 50)
1388 /* xgettext:c-format */
1389 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
1391 /* xgettext:c-format */
1392 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);