-
Notifications
You must be signed in to change notification settings - Fork 5
/
genconst.php
241 lines (181 loc) · 5.5 KB
/
genconst.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
function collect_enums($filename) {
$file = fopen(__DIR__.'/capstone/include/capstone/'.$filename, 'r');
$state = 0;
$name = null;
$enums = [];
$results = [];
while(!feof($file)) {
$line = fgets($file);
switch($state) {
case 0:
if (preg_match('/^typedef enum (\w+)/', $line, $match)) {
$name = $match[1];
$enums = [];
$state = 1;
}
break;
case 1:
if (preg_match('/^} (\w+)/', $line, $match)) {
$state = 0;
if ($name != $match[1]) {
fprintf(STDERR, "WARNING: unmatch open-close enum %s != %s", $name, $match[1]);
}
$results[$name] = $enums;
} else {
foreach(explode(",", $line) as $word) {
if (preg_match('/^\s*(\w+)/', $word, $match)) {
$enums[] = $match[1];
}
if (strpos($word, "//") !== false) break;
if (strpos($word, "/*") !== false) break;
}
}
}
}
fclose($file);
return $results;
}
function collect_defines($filename)
{
$results = [];
$file = fopen(__DIR__.'/capstone/include/capstone/'.$filename, 'r');
while(!feof($file)) {
$line = fgets($file);
if (preg_match('/^#define (\w+) (.+)$/', $line, $match)) {
$results[$match[1]] = $match[2];
}
}
fclose($file);
return $results;
}
///////////////////////////////////////////////////////////////////////////////
//
$output = fopen(__DIR__.'/const.c', 'w');
fwrite($output, <<<SCRIPT
/* Generated by `genconst.php` */
#include "php_capstone.h"
#define REGISTER_CAPSTONE_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
void php_capstone_register_constants(int module_number)
{
SCRIPT
);
$header = fopen(__DIR__.'/const.inc', 'w');
fwrite($header, <<<SCRIPT
/* Generated by `genconst.php` */
void php_capstone_register_constants(int);
SCRIPT
);
foreach (collect_enums('capstone.h') as $name=>$enums) {
fprintf($output, sprintf("// {{{ %s\n", $name));
foreach($enums as $enum) {
fprintf($output, "REGISTER_CAPSTONE_CONSTANT(%s);\n", $enum);
}
fprintf($output, "// }}} %s\n\n", $name);
}
fwrite($output, <<<SCRIPT
}
SCRIPT
);
///////
foreach (collect_enums('x86.h') as $name=>$enums) {
// Skip: x86_insn
if ($name == "x86_insn") continue;
// Fix: x86_op_type
$str_len = strlen($name) + 1;
if ($name == "x86_op_type") $str_len -= 5;
fprintf($header, sprintf("const char *php_capstone_%s_name(%s);\n", $name, $name));
fprintf($output, sprintf("const char *php_capstone_%s_name(%s id) {\n", $name, $name));
fprintf($output, "switch (id) {\n");
foreach($enums as $enum) {
$str = substr($enum, $str_len);
// Skip: INVALID
if ($str == 'INVALID') continue;
// Skip: duplicate with X86_PREFIX_REP
if ($enum == 'X86_PREFIX_REPE') continue;
// Transform
$str = strtr(strtolower($str), ["_" => "/"]);
fprintf($output, "case %s: return \"%s\"; break;\n", $enum, $str);
}
fprintf($output, "default: break;\n} // switch\n");
fprintf($output, "return NULL;\n} // %s\n\n", $name);
}
///////
$eflags_states = ['modify', 'prior', 'reset', 'set', 'test', 'undefined'];
$max = count($eflags_states);
fprintf($header, "void php_capstone_x86_eflags(zval*, uint64_t);\n");
fprintf($output, <<<SCRIPT
void php_capstone_x86_eflags(zval *peflagsob, uint64_t eflags)
{
zval statesar[$max];
SCRIPT
);
foreach($eflags_states as $idx=>$state) {
fprintf($output, <<<SCRIPT
array_init(&statesar[$idx]); // $state
SCRIPT
);
}
foreach(collect_defines('x86.h') as $name=>$value) {
if (preg_match('/^X86_EFLAGS_([A-Z]+)_([A-Z]+)/', $name, $match)) {
$flag = strtolower($match[2]);
$idx = array_search(strtolower($match[1]), $eflags_states);
if ($idx === false) continue;
fprintf($output, <<<SCRIPT
if (eflags & $name) add_next_index_string(&statesar[$idx], "$flag");
SCRIPT
);
}
}
foreach($eflags_states as $idx=>$state) {
fprintf($output, <<<SCRIPT
add_property_zval(peflagsob, "$state", &statesar[$idx]);
zval_ptr_dtor(&statesar[$idx]);
SCRIPT
);
}
fprintf($output, <<<SCRIPT
}
SCRIPT
);
///////
$fpu_flags_states = ['modify', 'reset', 'set', 'undefined', 'test'];
$max = count($fpu_flags_states);
fprintf($header, "void php_capstone_x86_fpu_flags(zval*, uint64_t);\n");
fprintf($output, <<<SCRIPT
void php_capstone_x86_fpu_flags(zval *pfpu_flagsob, uint64_t fpu_flags)
{
zval statesar[$max];
SCRIPT
);
foreach($fpu_flags_states as $idx=>$state) {
fprintf($output, <<<SCRIPT
array_init(&statesar[$idx]); // $state
SCRIPT
);
}
foreach(collect_defines('x86.h') as $name=>$value) {
if (preg_match('/^X86_FPU_FLAGS_([A-Z]+)_([A-Z0-9]+)/', $name, $match)) {
$flag = strtolower($match[2]);
$idx = array_search(strtolower($match[1]), $fpu_flags_states);
if ($idx === false) continue;
fprintf($output, <<<SCRIPT
if (fpu_flags & $name) add_next_index_string(&statesar[$idx], "fp$flag");
SCRIPT
);
}
}
foreach($fpu_flags_states as $idx=>$state) {
fprintf($output, <<<SCRIPT
add_property_zval(pfpu_flagsob, "$state", &statesar[$idx]);
zval_ptr_dtor(&statesar[$idx]);
SCRIPT
);
}
fprintf($output, <<<SCRIPT
}
SCRIPT
);
///////
fclose($output);
fclose($header);