Debian Codegraph @

suite/synctools/strinforeduce/strinforeduce.cpp

raw symbols SCIP
1 // By Martin Tofall, Obsidium Software
2 #define GET_INSTRINFO_ENUM
3 #define GET_INSTRINFO_MC_DESC
4
5 #ifdef CAPSTONE_X86_REDUCE
6 #include "X86GenInstrInfo_reduce.inc"
7 #else
8 #include "X86GenInstrInfo.inc"
9 #endif
10
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <string>
15
16 static const char *x86DisassemblerGetInstrName(unsigned Opcode)
17 {
18 return &llvm::X86InstrNameData[llvm::X86InstrNameIndices[Opcode]];
19 }
20
21 static bool is16BitEquivalent(const char* orig, const char* equiv)
22 {
23 size_t i;
24
25 for (i = 0;; i++) {
26 if (orig[i] == '\0' && equiv[i] == '\0')
27 return true;
28
29 if (orig[i] == '\0' || equiv[i] == '\0')
30 return false;
31
32 if (orig[i] != equiv[i]) {
33 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
34 continue;
35
36 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
37 continue;
38
39 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
40 continue;
41
42 return false;
43 }
44 }
45 }
46
47 // static const char *header = "#ifdef GET_INSTRINFO_MC_DESC\n#undef GET_INSTRINFO_MC_DESC\n\n"
48 static const char *header =
49 "typedef struct x86_op_id_pair {\n"\
50 "\tuint16_t first;\n" \
51 "\tuint16_t second;\n" \
52 "} x86_op_id_pair;\n\n" \
53 "static const x86_op_id_pair x86_16_bit_eq_tbl[] = {\n";
54 static const char *footer = "};\n\n";
55
56 static const char *header_lookup = "static const uint16_t x86_16_bit_eq_lookup[] = {\n";
57 //static const char *footer_lookup = "};\n\n#endif\n";
58 static const char *footer_lookup = "};\n";
59
60 static bool is16BitEquivalent_old(unsigned id1, unsigned id2)
61 {
62 return (is16BitEquivalent(x86DisassemblerGetInstrName(id1), x86DisassemblerGetInstrName(id2))) != false;
63 }
64
65 //#include "reduced.h"
66
67 #if 0
68 static bool is16BitEquivalent_new(unsigned orig, unsigned equiv)
69 {
70 size_t i;
71 uint16_t idx;
72
73 if (orig == equiv)
74 return true; // emulate old behaviour
75
76 if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
77 for (i = idx - 1; x86_16_bit_eq_tbl[i].first == orig; ++i) {
78 if (x86_16_bit_eq_tbl[i].second == equiv)
79 return true;
80 }
81 }
82
83 return false;
84 }
85 #endif
86
87 int main()
88 {
89 size_t size_names = sizeof(llvm::X86InstrNameData);
90 size_t size_indices = sizeof(llvm::X86InstrNameIndices);
91 size_t size_total = size_names + size_indices;
92
93 #if 1
94 printf("%s", header);
95
96 size_t eq_count = 0;
97 std::string str_lookup;
98 bool got_i = false;
99
100 for (size_t i = 0; i < llvm::X86::INSTRUCTION_LIST_END; ++i) {
101 const char *name1 = x86DisassemblerGetInstrName(i);
102 for (size_t j = 0; j < llvm::X86::INSTRUCTION_LIST_END; ++j) {
103 const char *name2 = x86DisassemblerGetInstrName(j);
104 if (i != j && is16BitEquivalent(name1, name2) != false) {
105 //printf("Found equivalent %d and %d\n", i, j);
106 printf("\t{ %zu, %zu },\n", i, j);
107 if (!got_i) {
108 char buf[16];
109 sprintf(buf, "\t%zu,\n", eq_count + 1);
110 str_lookup += buf;
111
112 got_i = true;
113 }
114 ++eq_count;
115 }
116 }
117
118 if (!got_i) {
119 //char buf[32];
120 //sprintf(buf, "\t0, //%d\n", i);
121 //str_lookup += buf;
122 str_lookup += "\t0,\n";
123 }
124
125 // reset got_i
126 got_i = false;
127 }
128
129 printf("%s", footer);
130 printf("%s", header_lookup);
131 printf("%s", str_lookup.c_str());
132 printf("%s", footer_lookup);
133
134 // printf("%zu equivalents total\n", eq_count);
135 // size_t size_new = eq_count * 4 + llvm::X86::INSTRUCTION_LIST_END * 2;
136 // printf("before: %zu, after: %zu, %zu bytes saved\n", size_total, size_new, size_total - size_new);
137 #endif
138
139 #if 0
140 for (size_t i = 0; i < llvm::X86::INSTRUCTION_LIST_END; ++i) {
141 for (size_t j = 0; j < llvm::X86::INSTRUCTION_LIST_END; ++j) {
142 if (is16BitEquivalent_new(i, j) != is16BitEquivalent_old(i, j)) {
143 bool old_result = is16BitEquivalent_old(i, j);
144 bool new_result = is16BitEquivalent_new(i, j);
145 printf("ERROR!\n");
146 }
147 }
148 }
149 #endif
150
151 #if 0
152 static const size_t BENCH_LOOPS = 50;
153
154 size_t eq_count = 0;
155 DWORD time = GetTickCount();
156 for (size_t l = 0; l < BENCH_LOOPS; ++l) {
157 for (size_t i = 0; i < llvm::X86::INSTRUCTION_LIST_END; ++i) {
158 for (size_t j = 0; j < llvm::X86::INSTRUCTION_LIST_END; ++j)
159 if (is16BitEquivalent_new(i, j))
160 ++eq_count;
161 }
162 }
163
164 time = GetTickCount() - time;
165 printf("new: %f msecs\n", static_cast<float>(time) / static_cast<float>(BENCH_LOOPS));
166
167 eq_count = 0;
168 time = GetTickCount();
169 for (size_t l = 0; l < BENCH_LOOPS; ++l) {
170 for (size_t i = 0; i < llvm::X86::INSTRUCTION_LIST_END; ++i) {
171 for (size_t j = 0; j < llvm::X86::INSTRUCTION_LIST_END; ++j)
172 if (is16BitEquivalent_old(i, j))
173 ++eq_count;
174 }
175 }
176
177 time = GetTickCount() - time;
178 printf("old: %f msecs\n", static_cast<float>(time) / static_cast<float>(BENCH_LOOPS));
179 #endif
180
181 return 0;
182 }
183