ir/ir.g

430 lines
9.5 KiB
Plaintext
Raw Normal View History

2022-04-05 23:19:23 +02:00
/*
2022-11-08 09:32:46 +01:00
* IR - Lightweight JIT Compilation Framework
* (IR loader)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <dmitry@php.net>
*
* To generate ir_load.c use llk <https://github.com/dstogov/llk>:
* php llk.php ir.g
*/
2022-04-05 23:19:23 +02:00
%start ir
%case-sensetive true
%global-vars false
%output "ir_load.c"
%language "c"
%indent "\t"
%{
2022-11-08 09:32:46 +01:00
/*
* IR - Lightweight JIT Compilation Framework
* (IR loader)
* Copyright (C) 2005-2022 Zend by Perforce.
* Authors: Dmitry Stogov <dmitry@php.net>
*
* 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
*/
2022-09-15 08:07:41 +02:00
2022-04-05 23:19:23 +02:00
#include "ir.h"
2022-05-27 12:18:04 +02:00
#include "ir_private.h"
2022-04-05 23:19:23 +02:00
#include <stdio.h>
#include <stdlib.h>
2023-02-28 00:11:09 +01:00
#ifndef _WIN32
# include <unistd.h>
#endif
2022-04-05 23:19:23 +02:00
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) {
2023-02-28 00:11:09 +01:00
ir_ref ref;
uint32_t len32;
IR_ASSERT(len <= 0xffffffff);
len32 = (uint32_t)len;
ref = ir_strtab_find(&p->var_tab, str, len32);
2022-04-05 23:19:23 +02:00
if (!ref) {
p->undef_count++;
/* create a linked list of unresolved references with header in "var_tab" */
ref = IR_UNUSED; /* list terminator */
2023-02-28 00:11:09 +01:00
ir_strtab_lookup(&p->var_tab, str, len32, IR_ENCODE_UNRESOLVED_REF(p->ctx->insns_count, n));
2022-04-05 23:19:23 +02:00
} 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 */
2023-02-28 00:11:09 +01:00
ir_strtab_update(&p->var_tab, str, len32, IR_ENCODE_UNRESOLVED_REF(p->ctx->insns_count, n));
2022-04-05 23:19:23 +02:00
}
return ref;
}
static void ir_define_var(ir_parser_ctx *p, const char *str, size_t len, ir_ref ref) {
2023-02-28 00:11:09 +01:00
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);
2022-04-05 23:19:23 +02:00
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);
2023-02-28 00:11:09 +01:00
ir_strtab_update(&p->var_tab, str, len32, ref);
2022-04-05 23:19:23 +02:00
} else {
2023-02-28 00:11:09 +01:00
fprintf(stderr, "ERROR: Redefined variable `%*s` on line %d\n", (int)len32, str, yy_line);
2022-04-05 23:19:23 +02:00
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)
2022-04-05 23:19:23 +02:00
{
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_parser_ctx p;}
(
ir_func_prototype(&p)
ir_func(&p)
)+
;
ir_func(ir_parser_ctx *p):
"{" (ir_insn(p) ";")* "}"
;
ir_func_prototype(ir_parser_ctx *p):
{const char *name;}
{size_t len;}
{uint8_t t = 0;}
"func" ID(&name, &len)
"("
(
type(&t)
(
","
type(&t)
)*
)?
")"
":"
type(&t)
;
ir_insn(ir_parser_ctx *p):
{const char *str, *str2 = NULL, *func;}
2022-09-05 21:43:27 +02:00
{size_t len, len2 = 0, func_len;}
2022-04-05 23:19:23 +02:00
{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;}
2022-04-05 23:19:23 +02:00
(
type(&t)
ID(&str, &len)
(
","
ID(&str2, &len2)
)?
|
ID(&str, &len)
)
"="
2022-09-05 21:43:27 +02:00
( {val.u64 = 0;}
const(t, &val)
2022-04-05 23:19:23 +02:00
{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);}
| "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);}
2022-04-05 23:19:23 +02:00
| 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++;}
2022-04-05 23:19:23 +02:00
{if (op == IR_CALL || op == IR_TAILCALL) count.i32+=2;}
{if (count.i32 < 0 || count.i32 > 255) yy_error("bad bumber of operands");}
{ref = ir_emit_N(p->ctx, IR_OPT(op, t), count.i32);}
( "("
2022-06-03 10:23:05 +02:00
( val(p, op, 1, &op1)
2022-04-05 23:19:23 +02:00
{n = 1;}
{if (n > count.i32) yy_error("too many operands");}
{ir_set_op(p->ctx, ref, n, op1);}
( ","
2022-06-03 10:23:05 +02:00
val(p, op, n, &op1)
2022-04-05 23:19:23 +02:00
{n++;}
{if (n > count.i32) yy_error("too many operands");}
{ir_set_op(p->ctx, ref, n, op1);}
)*
)?
")"
)?
|
{n = 0;}
2022-04-05 23:19:23 +02:00
( "("
2022-06-03 10:23:05 +02:00
( val(p, op, 1, &op1)
{n = 1;}
2022-04-05 23:19:23 +02:00
( ","
2022-06-03 10:23:05 +02:00
val(p, op, 2, &op2)
{n = 2;}
2022-04-05 23:19:23 +02:00
( ","
2022-06-03 10:23:05 +02:00
val(p, op, 3, &op3)
{n = 3;}
2022-04-05 23:19:23 +02:00
)?
)?
)?
")"
)?
{
2022-05-27 12:18:04 +02:00
if (IR_IS_FOLDABLE_OP(op)
2022-04-05 23:19:23 +02:00
&& !IR_IS_UNRESOLVED(op1)
&& !IR_IS_UNRESOLVED(op2)
&& !IR_IS_UNRESOLVED(op3)) {
2022-05-27 12:18:04 +02:00
ref = ir_fold(p->ctx, IR_OPT(op, t), op1, op2, op3);
2022-04-05 23:19:23 +02:00
} 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);
2022-04-05 23:19:23 +02:00
}
}
)
)
{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)
2023-02-28 00:11:09 +01:00
{IR_ASSERT(len <= 0xffffffff);}
{ref = ir_strtab_find(&type_tab, str, (uint32_t)len);}
2022-04-05 23:19:23 +02:00
{if (!ref) yy_error("invalid type");}
{*t = ref;}
;
func(uint8_t *op):
{const char *str;}
{size_t len;}
{ir_ref ref;}
ID(&str, &len)
2023-02-28 00:11:09 +01:00
{IR_ASSERT(len <= 0xffffffff);}
{ref = ir_strtab_find(&op_tab, str, (uint32_t)len);}
2022-04-05 23:19:23 +02:00
{if (!ref) yy_error("invalid op");}
{*op = ref - 1;}
;
2022-06-03 10:23:05 +02:00
val(ir_parser_ctx *p, uint8_t op, uint32_t n, ir_ref *ref):
2022-04-05 23:19:23 +02:00
{const char *str;}
{size_t len;}
{ir_val val;}
2022-06-03 10:23:05 +02:00
{uint32_t kind = IR_OPND_KIND(ir_op_flags[op], n);}
2022-04-05 23:19:23 +02:00
( ID(&str, &len)
2022-10-18 14:52:25 +02:00
{if (!IR_IS_REF_OPND_KIND(kind)) yy_error("unexpected reference");}
2022-04-05 23:19:23 +02:00
{*ref = ir_use_var(p, n, str, len);}
| STRING(&str, &len)
2022-06-03 10:23:05 +02:00
{if (kind != IR_OPND_STR) yy_error("unexpected string");}
2022-04-05 23:19:23 +02:00
{*ref = ir_strl(p->ctx, str, len);}
| DECNUMBER(IR_I32, &val)
2022-06-03 10:23:05 +02:00
{if (kind != IR_OPND_NUM && kind != IR_OPND_PROB) yy_error("unexpected number");}
2023-02-12 14:11:31 +01:00
{if (val.i64 < 0 || val.i64 > 0x7fffffff) yy_error("number out of range");}
2023-02-28 00:11:09 +01:00
{*ref = val.i32;}
2022-10-18 14:52:25 +02:00
| "null"
{*ref = IR_UNUSED;}
2022-04-05 23:19:23 +02:00
)
;
const(uint8_t t, ir_val *val):
DECNUMBER(t, val)
2022-04-19 12:11:12 +02:00
| HEXNUMBER(t, val)
2022-04-05 23:19:23 +02:00
| FLOATNUMBER(t, val)
| CHARACTER(val)
;
/* 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 val->i64 = atoll((const char*)yy_text);}
;
2022-04-19 12:11:12 +02:00
HEXNUMBER(uint32_t t, ir_val *val):
/0x[0-9A-Fa-f]+/
2022-04-19 13:11:07 +02:00
{val->u64 = strtoull((const char*)yy_text + 2, NULL, 16);}
2022-04-19 12:11:12 +02:00
;
2022-04-05 23:19:23 +02:00
FLOATNUMBER(uint32_t t, ir_val *val):
/[\-]?([0-9]*\.[0-9]+([Ee][\+\-]?[0-9]+)?|[0-9]+\.([Ee][\+\-]?[0-9]+)?|[0-9]+[Ee][\+\-]?[0-9]+)/
{val->d = atof((const char*)yy_text);}
;
CHARACTER(ir_val *val):
/'([^'\\]|\\.)*'/
2022-04-20 11:00:36 +02:00
{
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);
}
}
2022-04-05 23:19:23 +02:00
;
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_ctx *ctx, FILE *f) {
ir_parser_ctx p;
int sym;
long pos, end;
p.ctx = ctx;
p.undef_count = 0;
ir_strtab_init(&p.var_tab, 256, 4096);
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;
yy_pos = yy_text = yy_buf;
yy_line = 1;
sym = parse_ir_func(get_sym(), &p);
if (sym != YY_EOF) {
yy_error_sym("<EOF> expected, got", sym);
}
if (p.undef_count) {
ir_check_indefined_vars(&p);
}
ir_strtab_free(&p.var_tab);
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++) {
2023-02-28 00:11:09 +01:00
ir_strtab_lookup(&type_tab, ir_type_cname[i], (uint32_t)strlen(ir_type_cname[i]), i);
2022-04-05 23:19:23 +02:00
}
ir_strtab_init(&op_tab, IR_LAST_OP, 0);
for (i = 0; i < IR_LAST_OP; i++) {
2023-02-28 00:11:09 +01:00
ir_strtab_lookup(&op_tab, ir_op_name[i], (uint32_t)strlen(ir_op_name[i]), i + 1);
2022-04-05 23:19:23 +02:00
}
}
void ir_loader_free(void)
{
ir_strtab_free(&type_tab);
ir_strtab_free(&op_tab);
}