forked from dstogov/ir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_ir_fold_hash.c
307 lines (273 loc) · 6.07 KB
/
gen_ir_fold_hash.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* IR - Lightweight JIT Compilation Framework
* (Folding engine generator)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*
* Based on Mike Pall's implementation for LuaJIT.
*/
#include "ir.h"
#include <string.h>
#include "ir_strtab.c"
#define MAX_RULES 2048
#define MAX_SLOTS (MAX_RULES * 4)
#define USE_SEMI_PERFECT_HASH 1
#define USE_SHL_HASH 1
#define USE_ROL_HASH 0
static ir_strtab strtab;
void print_hash(uint32_t *mask, uint32_t count)
{
uint32_t i;
printf("static const uint32_t _ir_fold_hash[%d] = {\n", count + 1);
for (i = 0; i < count; i++) {
printf("\t0x%08x,\n", mask[i]);
}
printf("\t0x%08x\n", 0);
printf("};\n\n");
}
#if USE_SHL_HASH
static uint32_t hash_shl2(uint32_t mask, uint32_t r1, uint32_t r2)
{
return ((mask << r1) - mask) << r2;
}
#endif
#if USE_ROL_HASH
#define ir_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))
#define ir_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))
static uint32_t hash_rol2(uint32_t mask, uint32_t r1, uint32_t r2)
{
return ir_rol((ir_rol(mask, r1) - mask), r2);
}
#endif
/* Find a perfect hash function */
int find_hash(uint32_t *mask, uint32_t count)
{
uint32_t hash[MAX_SLOTS];
uint32_t n, r1, r2, i, h;
for (n = (count | 1); n < MAX_SLOTS; n += 2) {
#if USE_SEMI_PERFECT_HASH
int semi_perfect = 0;
#endif
for (r1 = 0; r1 < 31; r1++) {
for (r2 = 0; r2 < 32; r2++) {
#if USE_SHL_HASH
memset(hash, 0, n * sizeof(uint32_t));
for (i = 0; i < count; i++) {
h = hash_shl2(mask[i] & 0x1fffff, r1, r2) % n;
if (hash[h]) {
#if USE_SEMI_PERFECT_HASH
h++;
if (!hash[h]) {
hash[h] = mask[i];
semi_perfect = 1;
continue;
}
#endif
break; /* collision */
}
hash[h] = mask[i];
}
if (i == count) {
print_hash(hash, n);
#if USE_SEMI_PERFECT_HASH
if (semi_perfect) {
printf("#define IR_FOLD_SEMI_PERFECT_HASH\n\n");
}
#endif
printf("static uint32_t _ir_fold_hashkey(uint32_t h)\n{\n\treturn (((h << %d) - h) << %d) %% %d;\n}\n", r1, r2, n);
return 1;
}
#endif
#if USE_ROL_HASH
memset(hash, 0, n * sizeof(uint32_t));
for (i = 0; i < count; i++) {
h = hash_rol2(mask[i] & 0x1fffff, r1, r2) % n;
if (hash[h]) {
#if USE_SEMI_PERFECT_HASH
h++;
if (!hash[h]) {
hash[h] = mask[i];
semi_perfect = 1;
continue;
}
#endif
break; /* collision */
}
hash[h] = mask[i];
}
if (i == count) {
print_hash(hash, n);
#if USE_SEMI_PERFECT_HASH
if (semi_perfect) {
printf("#define IR_FOLD_SEMI_PERFECT_HASH\n\n");
}
#endif
printf("static uint32_t _ir_fold_hashkey(uint32_t h)\n{\nreturn ir_rol32((ir_rol32(h, %d) - h), %d) %% %d;\n}\n", r1, r2, n);
return 1;
}
#endif
}
}
}
hash[0] = 0;
print_hash(hash, 1);
printf("static uint32_t _ir_fold_hashkey(uint32_t h)\n{\n\treturn 0;\n}\n");
return 0;
}
static int find_op(const char *s, size_t len)
{
return ir_strtab_find(&strtab, s, (uint8_t)len) - 1;
}
static int parse_rule(const char *buf)
{
const char *p = buf + sizeof("IR_FOLD(") - 1;
const char *q;
int op, mask;
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p < 'A' || *p > 'Z') {
return 0;
}
q = p + 1;
while ((*q >= 'A' && *q <= 'Z')
|| (*q >= '0' && *q <= '9')
|| *q == '_') {
q++;
}
op = find_op(p, q - p);
if (op < 0) {
return 0;
}
mask = op;
while (*q == ' ' || *q == '\t') {
q++;
}
if (*q == ')') {
return mask; /* unused operands */
} else if (*q != '(') {
return 0;
}
p = q + 1;
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '_') {
q = p + 1;
} else if (*p >= 'A' && *p <= 'Z') {
q = p + 1;
while ((*q >= 'A' && *q <= 'Z')
|| (*q >= '0' && *q <= '9')
|| *q == '_') {
q++;
}
op = find_op(p, q - p);
if (op < 0) {
return 0;
}
mask |= op << 7;
} else {
return 0;
}
while (*q == ' ' || *q == '\t') {
q++;
}
if (*q == ')') {
return mask; /* unused op2 */
} else if (*q != ',') {
return 0;
}
p = q + 1;
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '_') {
q = p + 1;
} else if (*p >= 'A' && *p <= 'Z') {
q = p + 1;
while ((*q >= 'A' && *q <= 'Z')
|| (*q >= '0' && *q <= '9')
|| *q == '_') {
q++;
}
op = find_op(p, q - p);
if (op < 0) {
return 0;
}
mask |= op << 14;
} else {
return 0;
}
while (*q == ' ' || *q == '\t') {
q++;
}
if (*q != ')') {
return 0;
}
q++;
while (*q == ' ' || *q == '\t') {
q++;
}
if (*q != ')') {
return 0;
}
return mask;
}
int main(int argc, char **argv)
{
char buf[4096];
FILE *f = stdin;
int line = 0;
int rules = 0;
int i;
uint32_t mask[MAX_RULES];
uint32_t rule[MAX_RULES];
ir_strtab_init(&strtab, IR_LAST_OP, 0);
#define IR_OP_ADD(name, flags, op1, op2, op3) \
ir_strtab_lookup(&strtab, #name, sizeof(#name) - 1, IR_ ## name + 1);
IR_OPS(IR_OP_ADD)
ir_strtab_lookup(&strtab, "C_INTPTR", sizeof("C_INTPTR") - 1, IR_C_INTPTR + 1);
ir_strtab_lookup(&strtab, "C_UINTPTR", sizeof("C_IINTPTR") - 1, IR_C_UINTPTR + 1);
while (fgets(buf, sizeof(buf) - 1, f)) {
size_t len = strlen(buf);
if (len > 0 && (buf[len - 1] == '\r' || buf[len - 1] == '\n')) {
buf[len - 1] = 0;
len--;
line++;
}
if (len >= sizeof("IR_FOLD(")-1
&& memcmp(buf, "IR_FOLD(", sizeof("IR_FOLD(")-1) == 0) {
if (rules >= MAX_RULES) {
fprintf(stderr, "ERROR: Too many rules\n");
return 1;
}
i = parse_rule(buf);
if (!i) {
fprintf(stderr, "ERROR: Incorrect '%s' rule on line %d\n", buf, line);
return 1;
}
// TODO: few masks may share the same rule ???
rule[rules] = line;
mask[rules] = i | (rules << 21);
rules++;
}
}
ir_strtab_free(&strtab);
#if 0
for (i = 0; i < rules; i++) {
printf("0x%08x\n", mask[i]);
}
#endif
printf("/* This file is generated from \"ir_fold.h\". Do not edit! */\n\n");
printf("typedef enum _ir_fold_rule_id {\n");
for (i = 0; i < rules; i++) {
printf("\tIR_RULE_%d,\n", rule[i]);
}
printf("\t_IR_RULE_LAST\n");
printf("} ir_fold_rule_id;\n\n");
if (!find_hash(mask, rules)) {
fprintf(stderr, "ERROR: Cannot find a good hash function\n");
return 1;
}
return 0;
}