Debian Codegraph @

suite/synctools/disassemblertables2.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3
4 // this tool is to generate arch/X86/X86GenDisassemblerTables2.inc
5 // NOTE: this requires updated X86GenDisassemblerTables2 & X86GenDisassemblerTables2
6 // generatedy by ./disassemblertables.py & disassemblertables_reduce.py
7
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <string.h>
11
12 // X86DisassemblerDecoderCommon.h is copied from Capstone src
13 #include "../../arch/X86/X86DisassemblerDecoderCommon.h"
14
15 #define ARR_SIZE(a) (sizeof(a) / sizeof(a[0]))
16
17 /// Specifies whether a ModR/M byte is needed and (if so) which
18 /// instruction each possible value of the ModR/M byte corresponds to. Once
19 /// this information is known, we have narrowed down to a single instruction.
20 struct ModRMDecision {
21 uint8_t modrm_type;
22 uint16_t instructionIDs;
23 };
24
25 /// Specifies which set of ModR/M->instruction tables to look at
26 /// given a particular opcode.
27 struct OpcodeDecision {
28 struct ModRMDecision modRMDecisions[256];
29 };
30
31 /// Specifies which opcode->instruction tables to look at given
32 /// a particular context (set of attributes). Since there are many possible
33 /// contexts, the decoder first uses CONTEXTS_SYM to determine which context
34 /// applies given a specific set of attributes. Hence there are only IC_max
35 /// entries in this table, rather than 2^(ATTR_max).
36 struct ContextDecision {
37 struct OpcodeDecision opcodeDecisions[IC_max];
38 };
39
40 #ifdef CAPSTONE_X86_REDUCE
41 #include "X86GenDisassemblerTables_reduce2.inc"
42 #else
43 #include "X86GenDisassemblerTables2.inc"
44 #endif
45
46 static void index_OpcodeDecision(const struct OpcodeDecision *decisions, size_t size,
47 const struct OpcodeDecision *emptyDecision, unsigned int *index_table,
48 const char *opcodeTable,
49 const char *index_opcodeTable)
50 {
51 unsigned int i, count = 0;
52
53 for (i = 0; i < size; i++) {
54 if (memcmp((const void *)&decisions[i],
55 emptyDecision, sizeof(*emptyDecision)) != 0) {
56 // this is a non-zero entry
57 // index_table entry must be != 0
58 index_table[i] = count + 1;
59 count++;
60 } else
61 // empty entry
62 index_table[i] = 0;
63 }
64
65 printf("static const unsigned char %s[] = {\n", index_opcodeTable);
66
67 for (i = 0; i < size; i++) {
68 printf(" %u,\n", index_table[i]);
69 }
70
71 printf("};\n\n");
72
73 printf("static const struct OpcodeDecision %s[] = {\n", opcodeTable);
74 for (i = 0; i < size; i++) {
75 if (index_table[i]) {
76 unsigned int j;
77 const struct OpcodeDecision *decision;
78
79 // print out this non-zero entry
80 printf(" { {\n");
81 decision = &decisions[i];
82
83 for(j = 0; j < ARR_SIZE(emptyDecision->modRMDecisions); j++) {
84 const char *modrm;
85
86 switch(decision->modRMDecisions[j].modrm_type) {
87 default:
88 modrm = "MODRM_ONEENTRY";
89 break;
90 case 1:
91 modrm = "MODRM_SPLITRM";
92 break;
93 case 2:
94 modrm = "MODRM_SPLITMISC";
95 break;
96 case 3:
97 modrm = "MODRM_SPLITREG";
98 break;
99 case 4:
100 modrm = "MODRM_FULL";
101 break;
102 }
103 printf(" { %s, %u },\n",
104 modrm, decision->modRMDecisions[j].instructionIDs);
105 }
106 printf(" } },\n");
107 }
108 }
109
110 printf("};\n\n");
111 }
112
113
114 int main(int argc, char **argv)
115 {
116 unsigned int index_table[ARR_SIZE(x86DisassemblerOneByteOpcodes.opcodeDecisions)];
117 const struct OpcodeDecision emptyDecision;
118
119 memset((void *)&emptyDecision, 0, sizeof(emptyDecision));
120
121 printf("/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n");
122 printf("/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */\n");
123 printf("\n");
124
125 index_OpcodeDecision(x86DisassemblerOneByteOpcodes.opcodeDecisions,
126 ARR_SIZE(x86DisassemblerOneByteOpcodes.opcodeDecisions),
127 &emptyDecision, index_table,
128 "x86DisassemblerOneByteOpcodes",
129 "index_x86DisassemblerOneByteOpcodes");
130
131 index_OpcodeDecision(x86DisassemblerTwoByteOpcodes.opcodeDecisions,
132 ARR_SIZE(x86DisassemblerTwoByteOpcodes.opcodeDecisions),
133 &emptyDecision, index_table,
134 "x86DisassemblerTwoByteOpcodes",
135 "index_x86DisassemblerTwoByteOpcodes");
136
137 index_OpcodeDecision(x86DisassemblerThreeByte38Opcodes.opcodeDecisions,
138 ARR_SIZE(x86DisassemblerThreeByte38Opcodes.opcodeDecisions),
139 &emptyDecision, index_table,
140 "x86DisassemblerThreeByte38Opcodes",
141 "index_x86DisassemblerThreeByte38Opcodes");
142
143 index_OpcodeDecision(x86DisassemblerThreeByte3AOpcodes.opcodeDecisions,
144 ARR_SIZE(x86DisassemblerThreeByte3AOpcodes.opcodeDecisions),
145 &emptyDecision, index_table,
146 "x86DisassemblerThreeByte3AOpcodes",
147 "index_x86DisassemblerThreeByte3AOpcodes");
148
149 #ifndef CAPSTONE_X86_REDUCE
150 index_OpcodeDecision(x86DisassemblerXOP8Opcodes.opcodeDecisions,
151 ARR_SIZE(x86DisassemblerXOP8Opcodes.opcodeDecisions),
152 &emptyDecision, index_table,
153 "x86DisassemblerXOP8Opcodes",
154 "index_x86DisassemblerXOP8Opcodes");
155
156 index_OpcodeDecision(x86DisassemblerXOP9Opcodes.opcodeDecisions,
157 ARR_SIZE(x86DisassemblerXOP9Opcodes.opcodeDecisions),
158 &emptyDecision, index_table,
159 "x86DisassemblerXOP9Opcodes",
160 "index_x86DisassemblerXOP9Opcodes");
161
162 index_OpcodeDecision(x86DisassemblerXOPAOpcodes.opcodeDecisions,
163 ARR_SIZE(x86DisassemblerXOPAOpcodes.opcodeDecisions),
164 &emptyDecision, index_table,
165 "x86DisassemblerXOPAOpcodes",
166 "index_x86DisassemblerXOPAOpcodes");
167
168 index_OpcodeDecision(x86Disassembler3DNowOpcodes.opcodeDecisions,
169 ARR_SIZE(x86Disassembler3DNowOpcodes.opcodeDecisions),
170 &emptyDecision, index_table,
171 "x86Disassembler3DNowOpcodes",
172 "index_x86Disassembler3DNowOpcodes");
173 #endif
174
175 return 0;
176 }