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 */
87 parse_fmax_cr (CGEN_CPU_DESC cd
,
89 CGEN_KEYWORD
*keyword_table ATTRIBUTE_UNUSED
,
92 return cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr_fmax
, field
);
95 parse_fmax_ccr (CGEN_CPU_DESC cd
,
97 CGEN_KEYWORD
*keyword_table ATTRIBUTE_UNUSED
,
100 return cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr_fmax
, field
);
102 /* end-cop-ip-parse-handlers */
105 parse_tpreg (CGEN_CPU_DESC cd
, const char ** strp
,
106 CGEN_KEYWORD
*keyword_table
, long *field
)
110 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
114 return _("Only $tp or $13 allowed for this opcode");
119 parse_spreg (CGEN_CPU_DESC cd
, const char ** strp
,
120 CGEN_KEYWORD
*keyword_table
, long *field
)
124 err
= cgen_parse_keyword (cd
, strp
, keyword_table
, field
);
128 return _("Only $sp or $15 allowed for this opcode");
133 parse_mep_align (CGEN_CPU_DESC cd
, const char ** strp
,
134 enum cgen_operand_type type
, long *field
)
141 case MEP_OPERAND_PCREL8A2
:
142 case MEP_OPERAND_PCREL12A2
:
143 case MEP_OPERAND_PCREL17A2
:
144 case MEP_OPERAND_PCREL24A2
:
145 case MEP_OPERAND_CDISP8A2
:
146 case MEP_OPERAND_CDISP8A4
:
147 case MEP_OPERAND_CDISP8A8
:
148 err
= cgen_parse_signed_integer (cd
, strp
, type
, field
);
150 case MEP_OPERAND_PCABS24A2
:
151 case MEP_OPERAND_UDISP7
:
152 case MEP_OPERAND_UDISP7A2
:
153 case MEP_OPERAND_UDISP7A4
:
154 case MEP_OPERAND_UIMM7A4
:
155 case MEP_OPERAND_ADDR24A4
:
156 err
= cgen_parse_unsigned_integer (cd
, strp
, type
, (unsigned long *) field
);
165 case MEP_OPERAND_UDISP7
:
168 case MEP_OPERAND_PCREL8A2
:
169 case MEP_OPERAND_PCREL12A2
:
170 case MEP_OPERAND_PCREL17A2
:
171 case MEP_OPERAND_PCREL24A2
:
172 case MEP_OPERAND_PCABS24A2
:
173 case MEP_OPERAND_UDISP7A2
:
174 case MEP_OPERAND_CDISP8A2
:
177 case MEP_OPERAND_UDISP7A4
:
178 case MEP_OPERAND_UIMM7A4
:
179 case MEP_OPERAND_ADDR24A4
:
180 case MEP_OPERAND_CDISP8A4
:
183 case MEP_OPERAND_CDISP8A8
:
187 /* Safe assumption? */
191 return "Value is not aligned enough";
196 parse_mep_alignu (CGEN_CPU_DESC cd
, const char ** strp
,
197 enum cgen_operand_type type
, unsigned long *field
)
199 return parse_mep_align (cd
, strp
, type
, (long *) field
);
203 /* Handle %lo(), %tpoff(), %sdaoff(), %hi(), and other signed
204 constants in a signed context. */
207 parse_signed16 (CGEN_CPU_DESC cd
,
212 return parse_lo16 (cd
, strp
, opindex
, valuep
, 1);
216 parse_lo16 (CGEN_CPU_DESC cd
,
223 enum cgen_parse_operand_result result_type
;
226 if (strncasecmp (*strp
, "%lo(", 4) == 0)
229 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
230 & result_type
, & value
);
232 return _("missing `)'");
235 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
238 *valuep
= (long)(short) value
;
244 if (strncasecmp (*strp
, "%hi(", 4) == 0)
247 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
248 & result_type
, & value
);
250 return _("missing `)'");
253 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
254 value
= (value
+ 0x8000) >> 16;
259 if (strncasecmp (*strp
, "%uhi(", 5) == 0)
262 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
263 & result_type
, & value
);
265 return _("missing `)'");
268 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
274 if (strncasecmp (*strp
, "%sdaoff(", 8) == 0)
277 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
280 return _("missing `)'");
286 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
289 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
292 return _("missing `)'");
299 return _("invalid %function() here");
301 return cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
305 parse_unsigned16 (CGEN_CPU_DESC cd
,
308 unsigned long *valuep
)
310 return parse_lo16 (cd
, strp
, opindex
, (long *) valuep
, 0);
313 /* A special case of parse_signed16 which accepts only the value zero. */
316 parse_zero (CGEN_CPU_DESC cd
, const char **strp
, int opindex
, long *valuep
)
319 enum cgen_parse_operand_result result_type
;
322 /*fprintf(stderr, "dj: signed parse opindex `%s'\n", *strp);*/
324 /* Prevent ($ry) from being attempted as an expression on 'sw $rx,($ry)'.
325 It will fail and cause ry to be listed as an undefined symbol in the
327 if (strncmp (*strp
, "($", 2) == 0)
328 return "not zero"; /* any string will do -- will never be seen. */
330 if (strncasecmp (*strp
, "%lo(", 4) == 0)
333 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_LOW16
,
334 &result_type
, &value
);
336 return "missing `)'";
339 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
340 return "not zero"; /* any string will do -- will never be seen. */
345 if (strncasecmp (*strp
, "%hi(", 4) == 0)
348 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16S
,
349 &result_type
, &value
);
351 return "missing `)'";
354 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
355 return "not zero"; /* any string will do -- will never be seen. */
360 if (strncasecmp (*strp
, "%uhi(", 5) == 0)
363 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_HI16U
,
364 &result_type
, &value
);
366 return "missing `)'";
369 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
370 return "not zero"; /* any string will do -- will never be seen. */
375 if (strncasecmp (*strp
, "%sdaoff(", 8) == 0)
378 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_GPREL
,
379 &result_type
, &value
);
381 return "missing `)'";
384 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
385 return "not zero"; /* any string will do -- will never be seen. */
390 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
393 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_MEP_TPREL
,
394 &result_type
, &value
);
396 return "missing `)'";
399 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
400 return "not zero"; /* any string will do -- will never be seen. */
406 return "invalid %function() here";
408 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_NONE
,
409 &result_type
, &value
);
411 && (result_type
!= CGEN_PARSE_OPERAND_RESULT_NUMBER
|| value
!= 0))
412 return "not zero"; /* any string will do -- will never be seen. */
418 parse_unsigned7 (CGEN_CPU_DESC cd
, const char **strp
,
419 enum cgen_operand_type opindex
, unsigned long *valuep
)
424 /* fprintf(stderr, "dj: unsigned7 parse `%s'\n", *strp); */
426 if (strncasecmp (*strp
, "%tpoff(", 7) == 0)
432 case MEP_OPERAND_UDISP7
:
433 reloc
= BFD_RELOC_MEP_TPREL7
;
435 case MEP_OPERAND_UDISP7A2
:
436 reloc
= BFD_RELOC_MEP_TPREL7A2
;
438 case MEP_OPERAND_UDISP7A4
:
439 reloc
= BFD_RELOC_MEP_TPREL7A4
;
442 /* Safe assumption? */
445 errmsg
= cgen_parse_address (cd
, strp
, opindex
, reloc
,
448 return "missing `)'";
455 return _("invalid %function() here");
457 return parse_mep_alignu (cd
, strp
, opindex
, valuep
);
460 /* BEGIN LIGHTWEIGHT MACRO PROCESSOR. */
478 { "sizeof", "(`1.end + (- `1))"},
479 { "startof", "(`1 | 0)" },
480 { "align4", "(`1&(~3))"},
481 /*{ "hi", "(((`1+0x8000)>>16) & 0xffff)" }, */
482 /*{ "lo", "(`1 & 0xffff)" }, */
483 /*{ "sdaoff", "((`1-__sdabase) & 0x7f)"}, */
484 /*{ "tpoff", "((`1-__tpbase) & 0x7f)"}, */
488 static char * expand_string (const char *, int);
491 mep_cgen_expand_macros_and_parse_operand
492 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
495 str_append (char *dest
, const char *input
, int len
)
502 /* printf("str_append: <<%s>>, <<%s>>, %d\n", dest, input, len); */
503 oldlen
= (dest
? strlen(dest
) : 0);
504 new_dest
= realloc (dest
, oldlen
+ len
+ 1);
505 memset (new_dest
+ oldlen
, 0, len
+ 1);
506 return strncat (new_dest
, input
, len
);
510 lookup_macro (const char *name
)
514 for (m
= macros
; m
->name
; ++m
)
515 if (strncmp (m
->name
, name
, strlen(m
->name
)) == 0)
522 expand_macro (arg
*args
, int narg
, macro
*mac
)
524 char *result
= 0, *rescanned_result
= 0;
525 char *e
= mac
->expansion
;
529 /* printf("expanding macro %s with %d args\n", mac->name, narg + 1); */
534 ((*(e
+ 1) - '1') <= MAXARGS
) &&
535 ((*(e
+ 1) - '1') <= narg
))
537 result
= str_append (result
, mark
, e
- mark
);
538 arg
= (*(e
+ 1) - '1');
539 /* printf("replacing `%d with %s\n", arg+1, args[arg].start); */
540 result
= str_append (result
, args
[arg
].start
, args
[arg
].len
);
548 result
= str_append (result
, mark
, e
- mark
);
552 rescanned_result
= expand_string (result
, 0);
554 return rescanned_result
;
564 expand_string (const char *in
, int first_only
)
566 int num_expansions
= 0;
571 const char *mark
= in
;
582 if (*in
== '%' && *(in
+ 1) && (!first_only
|| num_expansions
== 0))
584 macro
= lookup_macro (in
+ 1);
587 /* printf("entering state %d at '%s'...\n", state, in); */
588 result
= str_append (result
, mark
, in
- mark
);
590 in
+= 1 + strlen (macro
->name
);
591 while (*in
== ' ') ++in
;
601 args
[narg
].start
= in
+ 1;
615 args
[narg
].start
= (in
+ 1);
620 /* printf("entering state %d at '%s'...\n", state, in); */
624 expansion
= expand_macro (args
, narg
, macro
);
628 result
= str_append (result
, expansion
, strlen (expansion
));
634 result
= str_append (result
, mark
, in
- mark
);
659 result
= str_append (result
, mark
, in
- mark
);
669 /* END LIGHTWEIGHT MACRO PROCESSOR. */
671 const char * mep_cgen_parse_operand
672 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
675 mep_cgen_expand_macros_and_parse_operand (CGEN_CPU_DESC cd
, int opindex
,
676 const char ** strp_in
, CGEN_FIELDS
* fields
)
678 const char * errmsg
= NULL
;
679 char *str
= 0, *hold
= 0;
680 const char **strp
= 0;
682 /* Set up a new pointer to macro-expanded string. */
683 str
= expand_string (*strp_in
, 1);
684 /* fprintf (stderr, " expanded <<%s>> to <<%s>>\n", *strp_in, str); */
687 strp
= (const char **)(&str
);
689 errmsg
= mep_cgen_parse_operand (cd
, opindex
, strp
, fields
);
691 /* Now work out the advance. */
692 if (strlen (str
) == 0)
693 *strp_in
+= strlen (*strp_in
);
697 if (strstr (*strp_in
, str
))
698 /* A macro-expansion was pulled off the front. */
699 *strp_in
= strstr (*strp_in
, str
);
701 /* A non-macro-expansion was pulled off the front. */
702 *strp_in
+= (str
- hold
);
711 #define CGEN_ASM_INIT_HOOK (cd->parse_operand = mep_cgen_expand_macros_and_parse_operand);
715 const char * mep_cgen_parse_operand
716 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
718 /* Main entry point for operand parsing.
720 This function is basically just a big switch statement. Earlier versions
721 used tables to look up the function to use, but
722 - if the table contains both assembler and disassembler functions then
723 the disassembler contains much of the assembler and vice-versa,
724 - there's a lot of inlining possibilities as things grow,
725 - using a switch statement avoids the function call overhead.
727 This function could be moved into `parse_insn_normal', but keeping it
728 separate makes clear the interface between `parse_insn_normal' and each of
732 mep_cgen_parse_operand (CGEN_CPU_DESC cd
,
735 CGEN_FIELDS
* fields
)
737 const char * errmsg
= NULL
;
738 /* Used by scalar operands that still need to be parsed. */
739 long junk ATTRIBUTE_UNUSED
;
743 case MEP_OPERAND_ADDR24A4
:
744 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_ADDR24A4
, (unsigned long *) (& fields
->f_24u8a4n
));
746 case MEP_OPERAND_CALLNUM
:
747 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CALLNUM
, (unsigned long *) (& fields
->f_callnum
));
749 case MEP_OPERAND_CCCC
:
750 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CCCC
, (unsigned long *) (& fields
->f_rm
));
752 case MEP_OPERAND_CCRN
:
753 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & fields
->f_ccrn
);
755 case MEP_OPERAND_CDISP8
:
756 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_CDISP8
, (long *) (& fields
->f_8s24
));
758 case MEP_OPERAND_CDISP8A2
:
759 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A2
, (long *) (& fields
->f_8s24a2
));
761 case MEP_OPERAND_CDISP8A4
:
762 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A4
, (long *) (& fields
->f_8s24a4
));
764 case MEP_OPERAND_CDISP8A8
:
765 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_CDISP8A8
, (long *) (& fields
->f_8s24a8
));
767 case MEP_OPERAND_CIMM4
:
768 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM4
, (unsigned long *) (& fields
->f_rn
));
770 case MEP_OPERAND_CIMM5
:
771 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CIMM5
, (unsigned long *) (& fields
->f_5u24
));
773 case MEP_OPERAND_CODE16
:
774 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE16
, (unsigned long *) (& fields
->f_16u16
));
776 case MEP_OPERAND_CODE24
:
777 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CODE24
, (unsigned long *) (& fields
->f_24u4n
));
779 case MEP_OPERAND_CP_FLAG
:
780 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_ccr
, & junk
);
782 case MEP_OPERAND_CRN
:
783 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crn
);
785 case MEP_OPERAND_CRN64
:
786 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crn
);
788 case MEP_OPERAND_CRNX
:
789 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_crnx
);
791 case MEP_OPERAND_CRNX64
:
792 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_cr64
, & fields
->f_crnx
);
794 case MEP_OPERAND_CSRN
:
795 errmsg
= parse_csrn (cd
, strp
, & mep_cgen_opval_h_csr
, & fields
->f_csrn
);
797 case MEP_OPERAND_CSRN_IDX
:
798 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_CSRN_IDX
, (unsigned long *) (& fields
->f_csrn
));
800 case MEP_OPERAND_DBG
:
801 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
803 case MEP_OPERAND_DEPC
:
804 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
806 case MEP_OPERAND_EPC
:
807 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
809 case MEP_OPERAND_EXC
:
810 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
812 case MEP_OPERAND_FMAX_CCRN
:
813 errmsg
= parse_fmax_ccr (cd
, strp
, & mep_cgen_opval_h_ccr
, & fields
->f_fmax_4_4
);
815 case MEP_OPERAND_FMAX_FRD
:
816 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frd
);
818 case MEP_OPERAND_FMAX_FRD_INT
:
819 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frd
);
821 case MEP_OPERAND_FMAX_FRM
:
822 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frm
);
824 case MEP_OPERAND_FMAX_FRN
:
825 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frn
);
827 case MEP_OPERAND_FMAX_FRN_INT
:
828 errmsg
= parse_fmax_cr (cd
, strp
, & mep_cgen_opval_h_cr
, & fields
->f_fmax_frn
);
830 case MEP_OPERAND_FMAX_RM
:
831 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_fmax_rm
);
833 case MEP_OPERAND_HI
:
834 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
836 case MEP_OPERAND_LO
:
837 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
839 case MEP_OPERAND_LP
:
840 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
842 case MEP_OPERAND_MB0
:
843 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
845 case MEP_OPERAND_MB1
:
846 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
848 case MEP_OPERAND_ME0
:
849 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
851 case MEP_OPERAND_ME1
:
852 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
854 case MEP_OPERAND_NPC
:
855 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
857 case MEP_OPERAND_OPT
:
858 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
860 case MEP_OPERAND_PCABS24A2
:
861 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_PCABS24A2
, (unsigned long *) (& fields
->f_24u5a2n
));
863 case MEP_OPERAND_PCREL12A2
:
864 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL12A2
, (long *) (& fields
->f_12s4a2
));
866 case MEP_OPERAND_PCREL17A2
:
867 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL17A2
, (long *) (& fields
->f_17s16a2
));
869 case MEP_OPERAND_PCREL24A2
:
870 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL24A2
, (long *) (& fields
->f_24s5a2n
));
872 case MEP_OPERAND_PCREL8A2
:
873 errmsg
= parse_mep_align (cd
, strp
, MEP_OPERAND_PCREL8A2
, (long *) (& fields
->f_8s8a2
));
875 case MEP_OPERAND_PSW
:
876 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
878 case MEP_OPERAND_R0
:
879 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
881 case MEP_OPERAND_R1
:
882 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
884 case MEP_OPERAND_RL
:
885 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rl
);
887 case MEP_OPERAND_RM
:
888 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
890 case MEP_OPERAND_RMA
:
891 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rm
);
893 case MEP_OPERAND_RN
:
894 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
896 case MEP_OPERAND_RN3
:
897 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
899 case MEP_OPERAND_RN3C
:
900 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
902 case MEP_OPERAND_RN3L
:
903 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
905 case MEP_OPERAND_RN3S
:
906 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
908 case MEP_OPERAND_RN3UC
:
909 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
911 case MEP_OPERAND_RN3UL
:
912 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
914 case MEP_OPERAND_RN3US
:
915 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn3
);
917 case MEP_OPERAND_RNC
:
918 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
920 case MEP_OPERAND_RNL
:
921 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
923 case MEP_OPERAND_RNS
:
924 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
926 case MEP_OPERAND_RNUC
:
927 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
929 case MEP_OPERAND_RNUL
:
930 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
932 case MEP_OPERAND_RNUS
:
933 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & fields
->f_rn
);
935 case MEP_OPERAND_SAR
:
936 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_csr
, & junk
);
938 case MEP_OPERAND_SDISP16
:
939 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SDISP16
, (long *) (& fields
->f_16s16
));
941 case MEP_OPERAND_SIMM16
:
942 errmsg
= parse_signed16 (cd
, strp
, MEP_OPERAND_SIMM16
, (long *) (& fields
->f_16s16
));
944 case MEP_OPERAND_SIMM6
:
945 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM6
, (long *) (& fields
->f_6s8
));
947 case MEP_OPERAND_SIMM8
:
948 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_SIMM8
, (long *) (& fields
->f_8s8
));
950 case MEP_OPERAND_SP
:
951 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
953 case MEP_OPERAND_SPR
:
954 errmsg
= parse_spreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
956 case MEP_OPERAND_TP
:
957 errmsg
= cgen_parse_keyword (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
959 case MEP_OPERAND_TPR
:
960 errmsg
= parse_tpreg (cd
, strp
, & mep_cgen_opval_h_gpr
, & junk
);
962 case MEP_OPERAND_UDISP2
:
963 errmsg
= cgen_parse_signed_integer (cd
, strp
, MEP_OPERAND_UDISP2
, (long *) (& fields
->f_2u6
));
965 case MEP_OPERAND_UDISP7
:
966 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7
, (unsigned long *) (& fields
->f_7u9
));
968 case MEP_OPERAND_UDISP7A2
:
969 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A2
, (unsigned long *) (& fields
->f_7u9a2
));
971 case MEP_OPERAND_UDISP7A4
:
972 errmsg
= parse_unsigned7 (cd
, strp
, MEP_OPERAND_UDISP7A4
, (unsigned long *) (& fields
->f_7u9a4
));
974 case MEP_OPERAND_UIMM16
:
975 errmsg
= parse_unsigned16 (cd
, strp
, MEP_OPERAND_UIMM16
, (unsigned long *) (& fields
->f_16u16
));
977 case MEP_OPERAND_UIMM2
:
978 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM2
, (unsigned long *) (& fields
->f_2u10
));
980 case MEP_OPERAND_UIMM24
:
981 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM24
, (unsigned long *) (& fields
->f_24u8n
));
983 case MEP_OPERAND_UIMM3
:
984 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM3
, (unsigned long *) (& fields
->f_3u5
));
986 case MEP_OPERAND_UIMM4
:
987 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM4
, (unsigned long *) (& fields
->f_4u8
));
989 case MEP_OPERAND_UIMM5
:
990 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, MEP_OPERAND_UIMM5
, (unsigned long *) (& fields
->f_5u8
));
992 case MEP_OPERAND_UIMM7A4
:
993 errmsg
= parse_mep_alignu (cd
, strp
, MEP_OPERAND_UIMM7A4
, (unsigned long *) (& fields
->f_7u9a4
));
995 case MEP_OPERAND_ZERO
:
996 errmsg
= parse_zero (cd
, strp
, MEP_OPERAND_ZERO
, (long *) (& junk
));
1000 /* xgettext:c-format */
1001 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
1008 cgen_parse_fn
* const mep_cgen_parse_handlers
[] =
1014 mep_cgen_init_asm (CGEN_CPU_DESC cd
)
1016 mep_cgen_init_opcode_table (cd
);
1017 mep_cgen_init_ibld_table (cd
);
1018 cd
->parse_handlers
= & mep_cgen_parse_handlers
[0];
1019 cd
->parse_operand
= mep_cgen_parse_operand
;
1020 #ifdef CGEN_ASM_INIT_HOOK
1027 /* Regex construction routine.
1029 This translates an opcode syntax string into a regex string,
1030 by replacing any non-character syntax element (such as an
1031 opcode) with the pattern '.*'
1033 It then compiles the regex and stores it in the opcode, for
1034 later use by mep_cgen_assemble_insn
1036 Returns NULL for success, an error message for failure. */
1039 mep_cgen_build_insn_regex (CGEN_INSN
*insn
)
1041 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
1042 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
1043 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
1045 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
1048 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
1050 /* Mnemonics come first in the syntax string. */
1051 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1052 return _("missing mnemonic in syntax string");
1055 /* Generate a case sensitive regular expression that emulates case
1056 insensitive matching in the "C" locale. We cannot generate a case
1057 insensitive regular expression because in Turkish locales, 'i' and 'I'
1058 are not equal modulo case conversion. */
1060 /* Copy the literal mnemonic out of the insn. */
1061 for (; *mnem
; mnem
++)
1068 *rx
++ = TOLOWER (c
);
1069 *rx
++ = TOUPPER (c
);
1076 /* Copy any remaining literals from the syntax string into the rx. */
1077 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
1079 if (CGEN_SYNTAX_CHAR_P (* syn
))
1081 char c
= CGEN_SYNTAX_CHAR (* syn
);
1085 /* Escape any regex metacharacters in the syntax. */
1086 case '.': case '[': case '\\':
1087 case '*': case '^': case '$':
1089 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
1090 case '?': case '{': case '}':
1091 case '(': case ')': case '*':
1092 case '|': case '+': case ']':
1102 *rx
++ = TOLOWER (c
);
1103 *rx
++ = TOUPPER (c
);
1113 /* Replace non-syntax fields with globs. */
1119 /* Trailing whitespace ok. */
1126 /* But anchor it after that. */
1130 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
1131 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
1137 static char msg
[80];
1139 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
1140 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
1141 free (CGEN_INSN_RX (insn
));
1142 (CGEN_INSN_RX (insn
)) = NULL
;
1148 /* Default insn parser.
1150 The syntax string is scanned and operands are parsed and stored in FIELDS.
1151 Relocs are queued as we go via other callbacks.
1153 ??? Note that this is currently an all-or-nothing parser. If we fail to
1154 parse the instruction, we return 0 and the caller will start over from
1155 the beginning. Backtracking will be necessary in parsing subexpressions,
1156 but that can be handled there. Not handling backtracking here may get
1157 expensive in the case of the m68k. Deal with later.
1159 Returns NULL for success, an error message for failure. */
1162 parse_insn_normal (CGEN_CPU_DESC cd
,
1163 const CGEN_INSN
*insn
,
1165 CGEN_FIELDS
*fields
)
1167 /* ??? Runtime added insns not handled yet. */
1168 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
1169 const char *str
= *strp
;
1172 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
1173 #ifdef CGEN_MNEMONIC_OPERANDS
1178 /* For now we assume the mnemonic is first (there are no leading operands).
1179 We can parse it without needing to set up operand parsing.
1180 GAS's input scrubber will ensure mnemonics are lowercase, but we may
1181 not be called from GAS. */
1182 p
= CGEN_INSN_MNEMONIC (insn
);
1183 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
1187 return _("unrecognized instruction");
1189 #ifndef CGEN_MNEMONIC_OPERANDS
1190 if (* str
&& ! ISSPACE (* str
))
1191 return _("unrecognized instruction");
1194 CGEN_INIT_PARSE (cd
);
1195 cgen_init_parse_operand (cd
);
1196 #ifdef CGEN_MNEMONIC_OPERANDS
1200 /* We don't check for (*str != '\0') here because we want to parse
1201 any trailing fake arguments in the syntax string. */
1202 syn
= CGEN_SYNTAX_STRING (syntax
);
1204 /* Mnemonics come first for now, ensure valid string. */
1205 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
1212 /* Non operand chars must match exactly. */
1213 if (CGEN_SYNTAX_CHAR_P (* syn
))
1215 /* FIXME: While we allow for non-GAS callers above, we assume the
1216 first char after the mnemonic part is a space. */
1217 /* FIXME: We also take inappropriate advantage of the fact that
1218 GAS's input scrubber will remove extraneous blanks. */
1219 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
1221 #ifdef CGEN_MNEMONIC_OPERANDS
1222 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
1230 /* Syntax char didn't match. Can't be this insn. */
1231 static char msg
[80];
1233 /* xgettext:c-format */
1234 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
1235 CGEN_SYNTAX_CHAR(*syn
), *str
);
1240 /* Ran out of input. */
1241 static char msg
[80];
1243 /* xgettext:c-format */
1244 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
1245 CGEN_SYNTAX_CHAR(*syn
));
1251 /* We have an operand of some sort. */
1252 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
1257 /* Done with this operand, continue with next one. */
1261 /* If we're at the end of the syntax string, we're done. */
1264 /* FIXME: For the moment we assume a valid `str' can only contain
1265 blanks now. IE: We needn't try again with a longer version of
1266 the insn and it is assumed that longer versions of insns appear
1267 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
1268 while (ISSPACE (* str
))
1272 return _("junk at end of line"); /* FIXME: would like to include `str' */
1277 /* We couldn't parse it. */
1278 return _("unrecognized instruction");
1281 /* Main entry point.
1282 This routine is called for each instruction to be assembled.
1283 STR points to the insn to be assembled.
1284 We assume all necessary tables have been initialized.
1285 The assembled instruction, less any fixups, is stored in BUF.
1286 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
1287 still needs to be converted to target byte order, otherwise BUF is an array
1288 of bytes in target byte order.
1289 The result is a pointer to the insn's entry in the opcode table,
1290 or NULL if an error occured (an error message will have already been
1293 Note that when processing (non-alias) macro-insns,
1294 this function recurses.
1296 ??? It's possible to make this cpu-independent.
1297 One would have to deal with a few minor things.
1298 At this point in time doing so would be more of a curiosity than useful
1299 [for example this file isn't _that_ big], but keeping the possibility in
1300 mind helps keep the design clean. */
1303 mep_cgen_assemble_insn (CGEN_CPU_DESC cd
,
1305 CGEN_FIELDS
*fields
,
1306 CGEN_INSN_BYTES_PTR buf
,
1310 CGEN_INSN_LIST
*ilist
;
1311 const char *parse_errmsg
= NULL
;
1312 const char *insert_errmsg
= NULL
;
1313 int recognized_mnemonic
= 0;
1315 /* Skip leading white space. */
1316 while (ISSPACE (* str
))
1319 /* The instructions are stored in hashed lists.
1320 Get the first in the list. */
1321 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
1323 /* Keep looking until we find a match. */
1325 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
1327 const CGEN_INSN
*insn
= ilist
->insn
;
1328 recognized_mnemonic
= 1;
1330 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
1331 /* Not usually needed as unsupported opcodes
1332 shouldn't be in the hash lists. */
1333 /* Is this insn supported by the selected cpu? */
1334 if (! mep_cgen_insn_supported (cd
, insn
))
1337 /* If the RELAXED attribute is set, this is an insn that shouldn't be
1338 chosen immediately. Instead, it is used during assembler/linker
1339 relaxation if possible. */
1340 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
1345 /* Skip this insn if str doesn't look right lexically. */
1346 if (CGEN_INSN_RX (insn
) != NULL
&&
1347 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
1350 /* Allow parse/insert handlers to obtain length of insn. */
1351 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
1353 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
1354 if (parse_errmsg
!= NULL
)
1357 /* ??? 0 is passed for `pc'. */
1358 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
1360 if (insert_errmsg
!= NULL
)
1363 /* It is up to the caller to actually output the insn and any
1369 static char errbuf
[150];
1370 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
1371 const char *tmp_errmsg
;
1373 /* If requesting verbose error messages, use insert_errmsg.
1374 Failing that, use parse_errmsg. */
1375 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
1376 parse_errmsg
? parse_errmsg
:
1377 recognized_mnemonic
?
1378 _("unrecognized form of instruction") :
1379 _("unrecognized instruction"));
1381 if (strlen (start
) > 50)
1382 /* xgettext:c-format */
1383 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
1385 /* xgettext:c-format */
1386 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
1388 if (strlen (start
) > 50)
1389 /* xgettext:c-format */
1390 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
1392 /* xgettext:c-format */
1393 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);