-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathir.g
455 lines (411 loc) · 10.7 KB
/
ir.g
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* IR - Lightweight JIT Compilation Framework
* (IR loader)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*
* To generate ir_load.c use llk <https://github.com/dstogov/llk>:
* php llk.php ir.g
*/
%start ir
%case-sensetive true
%global-vars false
%output "ir_load.c"
%language "c"
%indent "\t"
%{
/*
* IR - Lightweight JIT Compilation Framework
* (IR loader)
* Copyright (C) 2005-2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*
* This file is generated from "ir.g". Do not edit!
*
* To generate ir_load.c use llk <https://github.com/dstogov/llk>:
* php llk.php ir.g
*/
#include "ir.h"
#include "ir_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef _WIN32
# include <unistd.h>
#endif
const unsigned char *yy_buf;
const unsigned char *yy_end;
const unsigned char *yy_pos;
const unsigned char *yy_text;
uint32_t yy_line;
typedef struct _ir_parser_ctx {
ir_ctx *ctx;
uint32_t undef_count;
ir_strtab var_tab;
} ir_parser_ctx;
static ir_strtab type_tab;
static ir_strtab op_tab;
#define IR_IS_UNRESOLVED(ref) \
((ref) < (ir_ref)0xc0000000)
#define IR_ENCODE_UNRESOLVED_REF(ref, op) \
((ir_ref)0xc0000000 - ((ref) * sizeof(ir_ref) + (op)))
#define IR_DECODE_UNRESOLVED_REF(ref) \
((ir_ref)0xc0000000 - (ref))
static ir_ref ir_use_var(ir_parser_ctx *p, uint32_t n, const char *str, size_t len) {
ir_ref ref;
uint32_t len32;
IR_ASSERT(len <= 0xffffffff);
len32 = (uint32_t)len;
ref = ir_strtab_find(&p->var_tab, str, len32);
if (!ref) {
p->undef_count++;
/* create a linked list of unresolved references with header in "var_tab" */
ref = IR_UNUSED; /* list terminator */
ir_strtab_lookup(&p->var_tab, str, len32, IR_ENCODE_UNRESOLVED_REF(p->ctx->insns_count, n));
} else if (IR_IS_UNRESOLVED(ref)) {
/* keep the linked list of unresolved references with header in "var_tab" */
/* "ref" keeps the tail of the list */
ir_strtab_update(&p->var_tab, str, len32, IR_ENCODE_UNRESOLVED_REF(p->ctx->insns_count, n));
}
return ref;
}
static void ir_define_var(ir_parser_ctx *p, const char *str, size_t len, ir_ref ref) {
ir_ref old_ref;
uint32_t len32;
IR_ASSERT(len <= 0xffffffff);
len32 = (uint32_t)len;
old_ref = ir_strtab_lookup(&p->var_tab, str, len32, ref);
if (ref != old_ref) {
if (IR_IS_UNRESOLVED(old_ref)) {
p->undef_count--;
/* update the linked list of unresolved references */
do {
ir_ref *ptr = ((ir_ref*)(p->ctx->ir_base)) + IR_DECODE_UNRESOLVED_REF(old_ref);
old_ref = *ptr;
*ptr = ref;
} while (old_ref != IR_UNUSED);
ir_strtab_update(&p->var_tab, str, len32, ref);
} else {
fprintf(stderr, "ERROR: Redefined variable `%.*s` on line %d\n", (int)len32, str, yy_line);
exit(2);
}
}
}
static void report_undefined_var(const char *str, uint32_t len, ir_ref val)
{
if (IR_IS_UNRESOLVED(val)) {
fprintf(stderr, "ERROR: Undefined variable `%.*s`\n", (int)len, str);
}
}
static void ir_check_indefined_vars(ir_parser_ctx *p)
{
ir_strtab_apply(&p->var_tab, report_undefined_var);
exit(2);
}
/* forward declarations */
static void yy_error(const char *msg);
static void yy_error_sym(const char *msg, int sym);
%}
ir(ir_loader *loader):
{ir_parser_ctx p;}
{ir_ctx ctx;}
{char name[256];}
{p.ctx = &ctx;}
(
(
ir_func_prototype(&p, name)
{ir_init(&ctx, loader->default_func_flags, 256, 1024);}
{if (loader->init_func && !loader->init_func(loader, &ctx, name)) yy_error("init_func error");}
ir_func(&p)
{if (loader->process_func && !loader->process_func(loader, &ctx, name)) yy_error("process_func error");}
{ir_free(&ctx);}
)+
|
{ir_init(&ctx, loader->default_func_flags, 256, 1024);}
{if (loader->init_func && !loader->init_func(loader, &ctx, NULL)) yy_error("ini_func error");}
ir_func(&p)
{if (loader->process_func && !loader->process_func(loader, &ctx, NULL)) yy_error("process_func error");}
{ir_free(&ctx);}
)
;
ir_func(ir_parser_ctx *p):
{p->undef_count = 0;}
{ir_strtab_init(&p->var_tab, 256, 4096);}
"{" (ir_insn(p) ";")* "}"
{if (p->undef_count) ir_check_indefined_vars(p);}
{ir_strtab_free(&p->var_tab);}
;
ir_func_prototype(ir_parser_ctx *p, char *buf):
{const char *name;}
{size_t len;}
{uint8_t t = 0;}
"func" ID(&name, &len)
{if (len > 255) yy_error("name too long");}
{memcpy(buf, name, len);}
{buf[len] = 0;}
"("
(
"void"
|
(
type(&t)
(
","
type(&t)
)*
)
)?
")"
":"
(
type(&t)
|
"void"
)
;
ir_insn(ir_parser_ctx *p):
{const char *str, *str2 = NULL, *func;}
{size_t len, len2 = 0, func_len;}
{uint8_t op;}
{uint8_t t = 0;}
{ir_ref op1 = IR_UNUSED;}
{ir_ref op2 = IR_UNUSED;}
{ir_ref op3 = IR_UNUSED;}
{ir_ref ref;}
{ir_val val;}
{ir_val count;}
{ir_val flags;}
{int32_t n;}
(
type(&t)
ID(&str, &len)
(
","
ID(&str2, &len2)
)?
|
ID(&str, &len)
)
"="
( {val.u64 = 0;}
const(t, &val)
{ref = ir_const(p->ctx, val, t);}
| "func" "(" ID(&func, &func_len)
{flags.u64 = 0;}
( ","
DECNUMBER(IR_U16, &flags)
)?
")"
{ref = ir_const_func(p->ctx, ir_strl(p->ctx, func, func_len), flags.u16);}
| "sym" "(" ID(&func, &func_len) ")"
{ref = ir_const_sym(p->ctx, ir_strl(p->ctx, func, func_len));}
| "func_addr" "("
( DECNUMBER(IR_ADDR, &val)
| HEXNUMBER(IR_ADDR, &val)
)
{flags.u64 = 0;}
( ","
DECNUMBER(IR_U16, &flags)
)?
")"
{ref = ir_const_func_addr(p->ctx, val.addr, flags.u16);}
| STRING(&func, &func_len)
{ref = ir_const_str(p->ctx, ir_strl(p->ctx, func, func_len));}
| func(&op)
(
"/"
DECNUMBER(IR_I32, &count)
{if (op == IR_PHI || op == IR_SNAPSHOT) count.i32++;}
{if (op == IR_CALL || op == IR_TAILCALL) count.i32+=2;}
{if (count.i32 < 0 || count.i32 > 255) yy_error("bad number of operands");}
{ref = ir_emit_N(p->ctx, IR_OPT(op, t), count.i32);}
( "("
( val(p, op, 1, &op1)
{n = 1;}
{if (n > count.i32) yy_error("too many operands");}
{ir_set_op(p->ctx, ref, n, op1);}
( ","
val(p, op, n, &op1)
{n++;}
{if (n > count.i32) yy_error("too many operands");}
{ir_set_op(p->ctx, ref, n, op1);}
)*
)?
")"
)?
|
{n = 0;}
( "("
( val(p, op, 1, &op1)
{n = 1;}
( ","
val(p, op, 2, &op2)
{n = 2;}
( ","
val(p, op, 3, &op3)
{n = 3;}
)?
)?
)?
")"
)?
{
if (IR_IS_FOLDABLE_OP(op)
&& !IR_IS_UNRESOLVED(op1)
&& !IR_IS_UNRESOLVED(op2)
&& !IR_IS_UNRESOLVED(op3)) {
ref = ir_fold(p->ctx, IR_OPT(op, t), op1, op2, op3);
} else {
uint32_t opt;
if (!IR_OP_HAS_VAR_INPUTS(ir_op_flags[op])) {
opt = IR_OPT(op, t);
} else {
opt = IR_OPTX(op, t, n);
}
ref = ir_emit(p->ctx, opt, op1, op2, op3);
}
}
)
)
{ir_define_var(p, str, len, ref);}
{if (str2) ir_define_var(p, str2, len2, ref);}
;
type(uint8_t *t):
{const char *str;}
{size_t len;}
{ir_ref ref;}
ID(&str, &len)
{IR_ASSERT(len <= 0xffffffff);}
{ref = ir_strtab_find(&type_tab, str, (uint32_t)len);}
{if (!ref) yy_error("invalid type");}
{*t = ref;}
;
func(uint8_t *op):
{const char *str;}
{size_t len;}
{ir_ref ref;}
ID(&str, &len)
{IR_ASSERT(len <= 0xffffffff);}
{ref = ir_strtab_find(&op_tab, str, (uint32_t)len);}
{if (!ref) yy_error("invalid op");}
{*op = ref - 1;}
;
val(ir_parser_ctx *p, uint8_t op, uint32_t n, ir_ref *ref):
{const char *str;}
{size_t len;}
{ir_val val;}
{uint32_t kind = IR_OPND_KIND(ir_op_flags[op], n);}
( ID(&str, &len)
{if (!IR_IS_REF_OPND_KIND(kind)) yy_error("unexpected reference");}
{*ref = ir_use_var(p, n, str, len);}
| STRING(&str, &len)
{if (kind != IR_OPND_STR) yy_error("unexpected string");}
{*ref = ir_strl(p->ctx, str, len);}
| DECNUMBER(IR_I32, &val)
{if (kind != IR_OPND_NUM && kind != IR_OPND_PROB) yy_error("unexpected number");}
{if (val.i64 < 0 || val.i64 > 0x7fffffff) yy_error("number out of range");}
{*ref = val.i32;}
| "null"
{*ref = IR_UNUSED;}
)
;
const(uint8_t t, ir_val *val):
DECNUMBER(t, val)
| HEXNUMBER(t, val)
| FLOATNUMBER(t, val)
| CHARACTER(val)
| "inf"
{if (t == IR_DOUBLE) val->d = INFINITY; else val->f = INFINITY;}
| "nan"
{if (t == IR_DOUBLE) val->d = NAN; else val->f = NAN;}
| "-" "inf"
{if (t == IR_DOUBLE) val->d = -INFINITY; else val->f = -INFINITY;}
;
/* scanner rules */
ID(const char **str, size_t *len):
/[A-Za-z_][A-Za-z_0-9]*/
{*str = (const char*)yy_text; *len = yy_pos - yy_text;}
;
DECNUMBER(uint32_t t, ir_val *val):
/[\-]?[0-9]+/
{if (t == IR_DOUBLE) val->d = atof((const char*)yy_text);
else if (t == IR_FLOAT) val->f = strtof((const char*)yy_text, NULL);
else if (IR_IS_TYPE_SIGNED(t)) val->i64 = atoll((const char*)yy_text);
else val->u64 = strtoull((const char*)yy_text, NULL, 10);}
;
HEXNUMBER(uint32_t t, ir_val *val):
/0x[0-9A-Fa-f]+/
{val->u64 = strtoull((const char*)yy_text + 2, NULL, 16);}
;
FLOATNUMBER(uint32_t t, ir_val *val):
/[\-]?([0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?|[0-9]+\.([Ee][\+\-]?[0-9]+)?|[0-9]+[Ee][\+\-]?[0-9]+)/
{if (t == IR_DOUBLE) val->d = atof((const char*)yy_text); else val->f = strtof((const char*)yy_text, NULL);}
;
CHARACTER(ir_val *val):
/'([^'\\]|\\.)*'/
{
if ((char)yy_text[1] != '\\') {
val->i64 = (char)yy_text[1];
} else if ((char)yy_text[2] == '\\') {
val->i64 = '\\';
} else if ((char)yy_text[2] == 'r') {
val->i64 = '\r';
} else if ((char)yy_text[2] == 'n') {
val->i64 = '\n';
} else if ((char)yy_text[2] == 't') {
val->i64 = '\t';
} else if ((char)yy_text[2] == '0') {
val->i64 = '\0';
} else {
IR_ASSERT(0);
}
}
;
STRING(const char **str, size_t *len):
/"([^"\\]|\\.)*"/
{*str = (const char*)yy_text + 1; *len = yy_pos - yy_text - 2;}
;
EOL: /\r\n|\r|\n/;
WS: /[ \t\f\v]+/;
ONE_LINE_COMMENT: /(\/\/|#)[^\r\n]*(\r\n|\r|\n)/;
COMMENT: /\/\*([^\*]|\*+[^\*\/])*\*+\//;
SKIP: ( EOL | WS | ONE_LINE_COMMENT | COMMENT )*;
%%
static void yy_error(const char *msg) {
fprintf(stderr, "ERROR: %s at line %d\n", msg, yy_line);
exit(2);
}
static void yy_error_sym(const char *msg, int sym) {
fprintf(stderr, "ERROR: %s '%s' at line %d\n", msg, sym_name[sym], yy_line);
exit(2);
}
int ir_load(ir_loader *loader, FILE *f) {
long pos, end;
pos = ftell(f);
fseek(f, 0, SEEK_END);
end = ftell(f);
fseek(f, pos, SEEK_SET);
yy_buf = alloca(end - pos + 1);
yy_end = yy_buf + (end - pos);
fread((void*)yy_buf, (end - pos), 1, f);
*(unsigned char*)yy_end = 0;
parse(loader);
return 1;
}
void ir_loader_init(void)
{
ir_ref i;
ir_strtab_init(&type_tab, IR_LAST_OP, 0);
for (i = 1; i < IR_LAST_TYPE; i++) {
ir_strtab_lookup(&type_tab, ir_type_cname[i], (uint32_t)strlen(ir_type_cname[i]), i);
}
ir_strtab_init(&op_tab, IR_LAST_OP, 0);
for (i = 0; i < IR_LAST_OP; i++) {
ir_strtab_lookup(&op_tab, ir_op_name[i], (uint32_t)strlen(ir_op_name[i]), i + 1);
}
}
void ir_loader_free(void)
{
ir_strtab_free(&type_tab);
ir_strtab_free(&op_tab);
}