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