xref: /qemu/target/hexagon/gen_dectree_import.c (revision cbb9d715)
1ece6cd1eSTaylor Simpson /*
2ece6cd1eSTaylor Simpson  *  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3ece6cd1eSTaylor Simpson  *
4ece6cd1eSTaylor Simpson  *  This program is free software; you can redistribute it and/or modify
5ece6cd1eSTaylor Simpson  *  it under the terms of the GNU General Public License as published by
6ece6cd1eSTaylor Simpson  *  the Free Software Foundation; either version 2 of the License, or
7ece6cd1eSTaylor Simpson  *  (at your option) any later version.
8ece6cd1eSTaylor Simpson  *
9ece6cd1eSTaylor Simpson  *  This program is distributed in the hope that it will be useful,
10ece6cd1eSTaylor Simpson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11ece6cd1eSTaylor Simpson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12ece6cd1eSTaylor Simpson  *  GNU General Public License for more details.
13ece6cd1eSTaylor Simpson  *
14ece6cd1eSTaylor Simpson  *  You should have received a copy of the GNU General Public License
15ece6cd1eSTaylor Simpson  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16ece6cd1eSTaylor Simpson  */
17ece6cd1eSTaylor Simpson 
18ece6cd1eSTaylor Simpson /*
19ece6cd1eSTaylor Simpson  * This program generates the encodings file that is processed by
20ece6cd1eSTaylor Simpson  * the dectree.py script to produce the decoding tree.  We use the C
21ece6cd1eSTaylor Simpson  * preprocessor to manipulate the files imported from the Hexagon
22ece6cd1eSTaylor Simpson  * architecture library.
23ece6cd1eSTaylor Simpson  */
24ece6cd1eSTaylor Simpson #include <stdio.h>
25ece6cd1eSTaylor Simpson #include <string.h>
26ece6cd1eSTaylor Simpson #include "opcodes.h"
27ece6cd1eSTaylor Simpson 
28ece6cd1eSTaylor Simpson #define STRINGIZE(X)    #X
29ece6cd1eSTaylor Simpson 
30ece6cd1eSTaylor Simpson const char * const opcode_names[] = {
31ece6cd1eSTaylor Simpson #define OPCODE(IID) STRINGIZE(IID)
32ece6cd1eSTaylor Simpson #include "opcodes_def_generated.h.inc"
33ece6cd1eSTaylor Simpson     NULL
34ece6cd1eSTaylor Simpson #undef OPCODE
35ece6cd1eSTaylor Simpson };
36ece6cd1eSTaylor Simpson 
37ece6cd1eSTaylor Simpson /*
38ece6cd1eSTaylor Simpson  * Process the instruction definitions
39ece6cd1eSTaylor Simpson  *     Scalar core instructions have the following form
40ece6cd1eSTaylor Simpson  *         Q6INSN(A2_add,"Rd32=add(Rs32,Rt32)",ATTRIBS(),
41ece6cd1eSTaylor Simpson  *         "Add 32-bit registers",
42ece6cd1eSTaylor Simpson  *         { RdV=RsV+RtV;})
43*9f1f2fe5STaylor Simpson  *     HVX instructions have the following form
44*9f1f2fe5STaylor Simpson  *         EXTINSN(V6_vinsertwr, "Vx32.w=vinsert(Rt32)",
45*9f1f2fe5STaylor Simpson  *         ATTRIBS(A_EXTENSION,A_CVI,A_CVI_VX,A_CVI_LATE),
46*9f1f2fe5STaylor Simpson  *         "Insert Word Scalar into Vector",
47*9f1f2fe5STaylor Simpson  *         VxV.uw[0] = RtV;)
48ece6cd1eSTaylor Simpson  */
49ece6cd1eSTaylor Simpson const char * const opcode_syntax[XX_LAST_OPCODE] = {
50ece6cd1eSTaylor Simpson #define Q6INSN(TAG, BEH, ATTRIBS, DESCR, SEM) \
51ece6cd1eSTaylor Simpson    [TAG] = BEH,
52ece6cd1eSTaylor Simpson #define EXTINSN(TAG, BEH, ATTRIBS, DESCR, SEM) \
53ece6cd1eSTaylor Simpson    [TAG] = BEH,
54ece6cd1eSTaylor Simpson #include "imported/allidefs.def"
55ece6cd1eSTaylor Simpson #undef Q6INSN
56ece6cd1eSTaylor Simpson #undef EXTINSN
57ece6cd1eSTaylor Simpson };
58ece6cd1eSTaylor Simpson 
59ece6cd1eSTaylor Simpson const OpcodeEncoding opcode_encodings[] = {
60ece6cd1eSTaylor Simpson #define DEF_ENC32(TAG, ENCSTR) \
61ece6cd1eSTaylor Simpson     [TAG] = { .encoding = ENCSTR },
62ece6cd1eSTaylor Simpson #define DEF_ENC_SUBINSN(TAG, CLASS, ENCSTR) \
63ece6cd1eSTaylor Simpson     [TAG] = { .encoding = ENCSTR, .enc_class = CLASS },
64ece6cd1eSTaylor Simpson #define DEF_EXT_ENC(TAG, CLASS, ENCSTR) \
65ece6cd1eSTaylor Simpson     [TAG] = { .encoding = ENCSTR, .enc_class = CLASS },
66ece6cd1eSTaylor Simpson #include "imported/encode.def"
67ece6cd1eSTaylor Simpson #undef DEF_ENC32
68ece6cd1eSTaylor Simpson #undef DEF_ENC_SUBINSN
69ece6cd1eSTaylor Simpson #undef DEF_EXT_ENC
70ece6cd1eSTaylor Simpson };
71ece6cd1eSTaylor Simpson 
72ece6cd1eSTaylor Simpson static const char * const opcode_enc_class_names[XX_LAST_ENC_CLASS] = {
73ece6cd1eSTaylor Simpson     "NORMAL",
74ece6cd1eSTaylor Simpson     "16BIT",
75ece6cd1eSTaylor Simpson     "SUBINSN_A",
76ece6cd1eSTaylor Simpson     "SUBINSN_L1",
77ece6cd1eSTaylor Simpson     "SUBINSN_L2",
78ece6cd1eSTaylor Simpson     "SUBINSN_S1",
79ece6cd1eSTaylor Simpson     "SUBINSN_S2",
80ece6cd1eSTaylor Simpson     "EXT_noext",
81ece6cd1eSTaylor Simpson     "EXT_mmvec",
82ece6cd1eSTaylor Simpson };
83ece6cd1eSTaylor Simpson 
get_opcode_enc(int opcode)84ece6cd1eSTaylor Simpson static const char *get_opcode_enc(int opcode)
85ece6cd1eSTaylor Simpson {
86ece6cd1eSTaylor Simpson     const char *tmp = opcode_encodings[opcode].encoding;
87ece6cd1eSTaylor Simpson     if (tmp == NULL) {
88ece6cd1eSTaylor Simpson         tmp = "MISSING ENCODING";
89ece6cd1eSTaylor Simpson     }
90ece6cd1eSTaylor Simpson     return tmp;
91ece6cd1eSTaylor Simpson }
92ece6cd1eSTaylor Simpson 
get_opcode_enc_class(int opcode)93ece6cd1eSTaylor Simpson static const char *get_opcode_enc_class(int opcode)
94ece6cd1eSTaylor Simpson {
95*9f1f2fe5STaylor Simpson     const char *tmp = opcode_encodings[opcode].encoding;
96*9f1f2fe5STaylor Simpson     if (tmp == NULL) {
97*9f1f2fe5STaylor Simpson         const char *test = "V6_";        /* HVX */
98*9f1f2fe5STaylor Simpson         const char *name = opcode_names[opcode];
99*9f1f2fe5STaylor Simpson         if (strncmp(name, test, strlen(test)) == 0) {
100*9f1f2fe5STaylor Simpson             return "EXT_mmvec";
101*9f1f2fe5STaylor Simpson         }
102*9f1f2fe5STaylor Simpson     }
103ece6cd1eSTaylor Simpson     return opcode_enc_class_names[opcode_encodings[opcode].enc_class];
104ece6cd1eSTaylor Simpson }
105ece6cd1eSTaylor Simpson 
gen_iset_table(FILE * out)106ece6cd1eSTaylor Simpson static void gen_iset_table(FILE *out)
107ece6cd1eSTaylor Simpson {
108ece6cd1eSTaylor Simpson     int i;
109ece6cd1eSTaylor Simpson 
110ece6cd1eSTaylor Simpson     fprintf(out, "iset = {\n");
111ece6cd1eSTaylor Simpson     for (i = 0; i < XX_LAST_OPCODE; i++) {
112ece6cd1eSTaylor Simpson         fprintf(out, "\t\'%s\' : {\n", opcode_names[i]);
113ece6cd1eSTaylor Simpson         fprintf(out, "\t\t\'tag\' : \'%s\',\n", opcode_names[i]);
114ece6cd1eSTaylor Simpson         fprintf(out, "\t\t\'syntax\' : \'%s\',\n", opcode_syntax[i]);
115ece6cd1eSTaylor Simpson         fprintf(out, "\t\t\'enc\' : \'%s\',\n", get_opcode_enc(i));
116ece6cd1eSTaylor Simpson         fprintf(out, "\t\t\'enc_class\' : \'%s\',\n", get_opcode_enc_class(i));
117ece6cd1eSTaylor Simpson         fprintf(out, "\t},\n");
118ece6cd1eSTaylor Simpson     }
119ece6cd1eSTaylor Simpson     fprintf(out, "};\n\n");
120ece6cd1eSTaylor Simpson }
121ece6cd1eSTaylor Simpson 
gen_tags_list(FILE * out)122ece6cd1eSTaylor Simpson static void gen_tags_list(FILE *out)
123ece6cd1eSTaylor Simpson {
124ece6cd1eSTaylor Simpson     int i;
125ece6cd1eSTaylor Simpson 
126ece6cd1eSTaylor Simpson     fprintf(out, "tags = [\n");
127ece6cd1eSTaylor Simpson     for (i = 0; i < XX_LAST_OPCODE; i++) {
128ece6cd1eSTaylor Simpson         fprintf(out, "\t\'%s\',\n", opcode_names[i]);
129ece6cd1eSTaylor Simpson     }
130ece6cd1eSTaylor Simpson     fprintf(out, "];\n\n");
131ece6cd1eSTaylor Simpson }
132ece6cd1eSTaylor Simpson 
main(int argc,char * argv[])133ece6cd1eSTaylor Simpson int main(int argc, char *argv[])
134ece6cd1eSTaylor Simpson {
135ece6cd1eSTaylor Simpson     FILE *outfile;
136ece6cd1eSTaylor Simpson 
137ece6cd1eSTaylor Simpson     if (argc != 2) {
138ece6cd1eSTaylor Simpson         fprintf(stderr, "Usage: gen_dectree_import ouptputfile\n");
139ece6cd1eSTaylor Simpson         return 1;
140ece6cd1eSTaylor Simpson     }
141ece6cd1eSTaylor Simpson     outfile = fopen(argv[1], "w");
142ece6cd1eSTaylor Simpson     if (outfile == NULL) {
143ece6cd1eSTaylor Simpson         fprintf(stderr, "Cannot open %s for writing\n", argv[1]);
144ece6cd1eSTaylor Simpson         return 1;
145ece6cd1eSTaylor Simpson     }
146ece6cd1eSTaylor Simpson 
147ece6cd1eSTaylor Simpson     gen_iset_table(outfile);
148ece6cd1eSTaylor Simpson     gen_tags_list(outfile);
149ece6cd1eSTaylor Simpson 
150ece6cd1eSTaylor Simpson     fclose(outfile);
151ece6cd1eSTaylor Simpson     return 0;
152ece6cd1eSTaylor Simpson }
153