ai: stage1 unify IR values and globals

- add spl_ir_value_t (INST/CONST/GLOBAL) and global table
- remove mem.global_alloc and global.ref instructions
- dedup byte constants in a program-level pool
- zero-init aggregate literals in ast2ir; honor undefined
- C backend: fix zero-arg main return, extern prototypes/varargs
- fix dangling inline-module method names (0018 flake)
This commit is contained in:
zzy committed 2026-12-02 22:41:20 +08:00
1 parent 663adf6cae
commit 480945a92f
9 files changed
+1647 -1100

No files matched your search

+230 -167
View File
@@ -8,16 +8,27 @@
聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。 聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。
类型/布局全部走 layout,生成期不做运行时分发。非 extern 函数名 f<fid>。 */ 类型/布局全部走 layout,生成期不做运行时分发。非 extern 函数名 f<fid>。 */
typedef struct {
spl_ir_value_kind_t kind;
spl_type_id_t tid;
uint64_t bits;
usize slot;
} cconst_t;
typedef VEC(cconst_t) cconst_vec_t;
typedef struct { typedef struct {
const spl_ir_t *ir; const spl_ir_t *ir;
const spl_type_t *type; const spl_type_t *type;
spl_layout_t *layout; spl_layout_t *layout;
const spl_attr_t *attr; const spl_attr_t *attr;
spl_ir2c_buf_t *out; spl_ir2c_buf_t *out;
usize gdata_n; /* 每个 mem.global_alloc 生成一块静态数据,从 1 开始编号 */ cconst_vec_t cconsts; /* 当前函数的 CONST 操作数槽 */
bool failed; bool failed;
} ir2c_t; } ir2c_t;
/* 常量操作数占用 v 的高位编号,避免与指令结果 v1..vN 冲突。 */
#define SPL_IR2C_CONST_BASE ((usize)1 << 30)
static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) { static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) {
if (n == 0) if (n == 0)
return; return;
@@ -34,12 +45,24 @@ static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) {
static void put(ir2c_t *c, const char *s) { buf_append(c->out, s, strlen(s)); } static void put(ir2c_t *c, const char *s) { buf_append(c->out, s, strlen(s)); }
static void put_n(ir2c_t *c, const char *s, usize n) { buf_append(c->out, s, n); } static void put_n(ir2c_t *c, const char *s, usize n) { buf_append(c->out, s, n); }
static void put_byte(ir2c_t *c, char b) { buf_append(c->out, &b, 1); } static void put_byte(ir2c_t *c, char b) { buf_append(c->out, &b, 1); }
static void put_u(ir2c_t *c, usize v) { static void put_u_raw(ir2c_t *c, usize v) {
char buf[32]; char buf[32];
int n = snprintf(buf, sizeof buf, "%llu", (unsigned long long)v); int n = snprintf(buf, sizeof buf, "%llu", (unsigned long long)v);
if (n > 0) if (n > 0)
buf_append(c->out, buf, (usize)n); buf_append(c->out, buf, (usize)n);
} }
static usize const_slot_lookup(ir2c_t *c, spl_ir_value_t v);
static void put_value_id(ir2c_t *c, spl_ir_value_t v) {
if (v.kind == SPL_IR_VALUE_INST) {
put_u_raw(c, v.inst);
return;
}
put_u_raw(c, SPL_IR2C_CONST_BASE + const_slot_lookup(c, v));
}
/* usize 走原路径;Value 常量走 v<base+slot>。 */
#define put_u(c_, x_) _Generic((x_), spl_ir_value_t: put_value_id, default: put_u_raw)((c_), (x_))
static void put_i(ir2c_t *c, long long v) { static void put_i(ir2c_t *c, long long v) {
char buf[32]; char buf[32];
int n = snprintf(buf, sizeof buf, "%lld", v); int n = snprintf(buf, sizeof buf, "%lld", v);
@@ -181,41 +204,31 @@ static void c_fn_name(ir2c_t *c, spl_ir_func_ref_t fid) {
put_u(c, fid); put_u(c, fid);
} }
/* 读取 type.const 的整数值;按 tid 决定落在哪个 union 成员(同 ir2vm 约定)。 */ /* 常量操作数按 (tid, 位模式) 共享一个 v<slot> 变量。 */
static usize const_node(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) { static uint64_t value_bits(spl_ir_value_t v) {
if (ref == 0 || ref >= vec_size(f->nodes)) uint64_t bits = 0;
return 0; memcpy(&bits, &v.int_bits, sizeof bits);
const spl_ir_node_t *n = &vec_at(f->nodes, ref); return bits;
if (n->kind != SPL_IR_TYPE_CONST)
return 0;
spl_type_node_t *te = spl_type_node(c->type, n->type_const.tid);
if (!te)
return 0;
switch (te->kind) {
case SPL_TYPE_BOOL:
return n->type_const.bool_lit ? 1 : 0;
case SPL_TYPE_INT:
if (te->int_type.bits == 8)
return (usize)(unsigned char)n->type_const.ch_lit;
return n->type_const.int_lit;
case SPL_TYPE_PTR:
return (usize)n->type_const.ptr_lit;
default:
return 0;
}
} }
/* 函数常量节点(Fn lit)持有的函数 ref;非函数常量返回 0。 */ static usize const_slot_lookup(ir2c_t *c, spl_ir_value_t v) {
static spl_ir_func_ref_t const_fn_ref(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) { uint64_t bits = value_bits(v);
if (ref == 0 || ref >= vec_size(f->nodes)) for (usize i = 0; i < vec_size(c->cconsts); ++i) {
return 0; cconst_t *e = &vec_at(c->cconsts, i);
const spl_ir_node_t *n = &vec_at(f->nodes, ref); if (e->kind == v.kind && e->tid == v.tid && e->bits == bits)
if (n->kind != SPL_IR_TYPE_CONST) return e->slot;
return 0; }
spl_type_node_t *te = spl_type_node(c->type, n->type_const.tid); cconst_t e = {.kind = v.kind, .tid = v.tid, .bits = bits, .slot = vec_size(c->cconsts)};
if (!te || te->kind != SPL_TYPE_FN) vec_push(c->cconsts, e);
return 0; return e.slot;
return n->type_const.fn; }
static usize value_int(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_CONST ? (usize)v.int_bits : 0;
}
static spl_ir_func_ref_t value_fn(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_CONST ? v.fn : 0;
} }
static spl_type_id_t field_tid(ir2c_t *c, spl_type_id_t tid, usize idx) { static spl_type_id_t field_tid(ir2c_t *c, spl_type_id_t tid, usize idx) {
@@ -291,16 +304,12 @@ static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) {
return n->cast.to_tid; return n->cast.to_tid;
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
return make_ptr(c, n->mem_alloc.tid); return make_ptr(c, n->mem_alloc.tid);
case SPL_IR_MEM_GLOBAL_ALLOC:
return n->mem_global_alloc.tid; /* 已是 *u8 */
case SPL_IR_MEM_LOAD: case SPL_IR_MEM_LOAD:
return n->mem_load.tid; return n->mem_load.tid;
case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_OFFSET:
return make_ptr(c, n->mem_offset.tid); return make_ptr(c, n->mem_offset.tid);
case SPL_IR_MEM_FIELD_PTR: case SPL_IR_MEM_FIELD_PTR:
return make_ptr(c, field_tid(c, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx)); return make_ptr(c, field_tid(c, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx));
case SPL_IR_TYPE_CONST:
return n->type_const.tid;
case SPL_IR_TYPE_BITSIZEOF: case SPL_IR_TYPE_BITSIZEOF:
case SPL_IR_TYPE_SIZEOF: case SPL_IR_TYPE_SIZEOF:
case SPL_IR_TYPE_ALIGNOF: case SPL_IR_TYPE_ALIGNOF:
@@ -324,6 +333,14 @@ static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) {
} }
} }
static spl_type_id_t value_type(ir2c_t *c, const spl_ir_func_t *f, spl_ir_value_t v) {
if (v.kind != SPL_IR_VALUE_INST)
return v.tid;
if (v.inst == 0 || v.inst >= vec_size(f->nodes))
return 0;
return result_tid(c, &vec_at(f->nodes, v.inst));
}
static void bind_agg(ir2c_t *c, usize ref) { static void bind_agg(ir2c_t *c, usize ref) {
put(c, "v"); put(c, "v");
put_u(c, ref); put_u(c, ref);
@@ -362,7 +379,9 @@ static void emit_agg_construct(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
bind_agg(c, ref); bind_agg(c, ref);
spl_type_id_t agg_t = n->agg_construct.tid; spl_type_id_t agg_t = n->agg_construct.tid;
for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) { for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) {
spl_ir_node_ref_t fref = vec_at(n->agg_construct.fields, i); spl_ir_value_t fref = vec_at(n->agg_construct.fields, i);
if (spl_ir_value_is_none(fref))
continue;
spl_type_id_t ftid = field_tid(c, agg_t, i); spl_type_id_t ftid = field_tid(c, agg_t, i);
if (!ftid) if (!ftid)
continue; continue;
@@ -623,68 +642,8 @@ static void emit_cast(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
put(c, ";\n"); put(c, ";\n");
} }
static void emit_const(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
spl_type_id_t tid = n->type_const.tid;
spl_type_node_t *te = spl_type_node(c->type, tid);
if (!te)
return;
if (te->kind == SPL_TYPE_UNIT)
return;
put(c, "v");
put_u(c, ref);
put(c, " = ");
switch (te->kind) {
case SPL_TYPE_BOOL:
put(c, n->type_const.bool_lit ? "1" : "0");
break;
case SPL_TYPE_INT: {
usize raw = (te->int_type.bits == 8) ? (usize)(unsigned char)n->type_const.ch_lit
: n->type_const.int_lit;
if (te->int_type.is_signed) {
long long sv = (long long)raw;
if (sv == (-9223372036854775807LL - 1))
put(c, "(-9223372036854775807 - 1)");
else
put_i(c, sv);
} else {
put_u(c, raw);
if (te->int_type.bits >= 64)
put(c, "ull");
}
break;
}
case SPL_TYPE_PTR:
put(c, "(void*)");
put_u(c, (usize)n->type_const.ptr_lit);
break;
/* 字节常量(字符串/文件名):只被 mem.global_alloc 通过字段引用,vN 是死值。
若输出宿主指针会导致生成物随编译进程地址而变,这里固定为 0。 */
case SPL_TYPE_ARRAY:
put(c, "(void*)0");
break;
case SPL_TYPE_FN:
put(c, "(void*)&");
c_fn_name(c, n->type_const.fn);
break;
case SPL_TYPE_FLOAT: {
double d = n->type_const.float_lit;
if (isfinite(d)) {
char buf[64];
snprintf(buf, sizeof buf, "%.17g", d);
put(c, buf);
} else {
put(c, "0.0");
}
break;
}
default:
put(c, "0");
break;
}
put(c, ";\n");
}
static void emit_mem(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { static void emit_mem(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) {
(void)f;
switch (n->kind) { switch (n->kind) {
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
@@ -766,62 +725,13 @@ static void emit_mem(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_
break; break;
} }
case SPL_IR_MEM_GLOBAL_ALLOC: {
spl_type_id_t ctid = 0;
const void *pv = NULL;
if (n->mem_global_alloc.const_node > 0 &&
n->mem_global_alloc.const_node < vec_size(f->nodes)) {
const spl_ir_node_t *cn = &vec_at(f->nodes, n->mem_global_alloc.const_node);
if (cn->kind == SPL_IR_TYPE_CONST) {
ctid = cn->type_const.tid;
pv = cn->type_const.ptr_lit;
}
}
if (!pv) {
put(c, "v");
put_u(c, ref);
put(c, " = (void*)0;\n");
break;
}
/* size 为 0(空数组)时只生成一个零词,避免越界读取宿主内存导致生成物不确定。 */
usize size = type_size(c, ctid);
usize gid = c->gdata_n++;
usize words = (size + 7) / 8;
if (words == 0)
words = 1;
put(c, "static const u64 g");
put_u(c, gid);
put(c, "[");
put_u(c, words);
put(c, "] = {");
for (usize j = 0; j < words; ++j) {
if (j > 0)
put(c, ", ");
uint64_t w = 0;
for (usize k = 0; k < 8; ++k) {
usize idx = j * 8 + k;
uint64_t b = 0;
if (idx < size)
b = (uint64_t)((const unsigned char *)pv)[idx];
w |= b << (k * 8);
}
put_u(c, (usize)w);
put(c, "ull");
}
put(c, "};\nv");
put_u(c, ref);
put(c, " = (void*)g");
put_u(c, gid);
put(c, ";\n");
break;
}
default: default:
break; break;
} }
} }
static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) {
(void)f;
usize v = 0; usize v = 0;
switch (n->kind) { switch (n->kind) {
case SPL_IR_TYPE_SIZEOF: case SPL_IR_TYPE_SIZEOF:
@@ -838,7 +748,7 @@ static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_
break; break;
case SPL_IR_TYPE_OFFSETOF: { case SPL_IR_TYPE_OFFSETOF: {
/* field_idx 是一个常量节点 ref */ /* field_idx 是一个常量节点 ref */
usize idx = const_node(c, f, n->ir_offsetof.field_idx); usize idx = value_int(n->ir_offsetof.field_idx);
v = field_off(c, n->ir_offsetof.tid, idx); v = field_off(c, n->ir_offsetof.tid, idx);
break; break;
} }
@@ -872,7 +782,7 @@ static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_
} }
static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) {
spl_ir_func_ref_t callee = const_fn_ref(c, f, n->control_call.func); spl_ir_func_ref_t callee = value_fn(n->control_call.func);
bool ret_agg = is_aggregate(c, n->control_call.tid); bool ret_agg = is_aggregate(c, n->control_call.tid);
if (callee != 0) { if (callee != 0) {
if (ret_agg) { if (ret_agg) {
@@ -899,9 +809,7 @@ static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir
} }
/* 间接调用:从 func 值的类型构造函数指针类型 */ /* 间接调用:从 func 值的类型构造函数指针类型 */
spl_type_id_t func_tid = 0; spl_type_id_t func_tid = value_type(c, f, n->control_call.func);
if (n->control_call.func < vec_size(f->nodes))
func_tid = result_tid(c, &vec_at(f->nodes, n->control_call.func));
spl_type_node_t *ftnode = spl_type_node(c->type, func_tid); spl_type_node_t *ftnode = spl_type_node(c->type, func_tid);
if (!ftnode || ftnode->kind != SPL_TYPE_FN) { if (!ftnode || ftnode->kind != SPL_TYPE_FN) {
put_unsupported(c, "indirect call type"); put_unsupported(c, "indirect call type");
@@ -959,7 +867,7 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl
spl_type_id_t ret_tid) { spl_type_id_t ret_tid) {
switch (n->kind) { switch (n->kind) {
case SPL_IR_CONTROL_PARAM: { case SPL_IR_CONTROL_PARAM: {
usize idx = const_node(c, f, n->control_param.idx); usize idx = value_int(n->control_param.idx);
if (is_aggregate(c, n->control_param.tid)) { if (is_aggregate(c, n->control_param.tid)) {
put(c, "v"); put(c, "v");
put_u(c, ref); put_u(c, ref);
@@ -980,7 +888,7 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl
case SPL_IR_CONTROL_RET: case SPL_IR_CONTROL_RET:
if (is_aggregate(c, ret_tid)) { if (is_aggregate(c, ret_tid)) {
if (n->control_ret.val) { if (!spl_ir_value_is_none(n->control_ret.val)) {
usize sz = type_size(c, ret_tid); usize sz = type_size(c, ret_tid);
put(c, "memcpy(ret__, v"); put(c, "memcpy(ret__, v");
put_u(c, n->control_ret.val); put_u(c, n->control_ret.val);
@@ -1026,10 +934,6 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl
static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n, static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n,
spl_type_id_t ret_tid) { spl_type_id_t ret_tid) {
switch (n->kind) { switch (n->kind) {
case SPL_IR_TYPE_CONST:
emit_const(c, ref, n);
break;
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_ADD:
case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_SUB:
case SPL_IR_ARITH_MUL: case SPL_IR_ARITH_MUL:
@@ -1074,7 +978,6 @@ static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir
case SPL_IR_MEM_STORE: case SPL_IR_MEM_STORE:
case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_OFFSET:
case SPL_IR_MEM_FIELD_PTR: case SPL_IR_MEM_FIELD_PTR:
case SPL_IR_MEM_GLOBAL_ALLOC:
emit_mem(c, f, ref, n); emit_mem(c, f, ref, n);
break; break;
@@ -1160,11 +1063,106 @@ static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir
} }
} }
static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) {
if (v.kind == SPL_IR_VALUE_GLOBAL) {
put(c, "(void*)gg");
put_u_raw(c, v.global_id);
return;
}
spl_type_node_t *te = spl_type_node(c->type, v.tid);
if (!te) {
put(c, "0");
return;
}
switch (te->kind) {
case SPL_TYPE_BOOL:
put(c, v.int_bits ? "1" : "0");
break;
case SPL_TYPE_INT: {
uint64_t raw = v.int_bits;
if (te->int_type.bits == 8)
raw = (unsigned char)raw;
if (te->int_type.is_signed) {
long long sv = (long long)raw;
if (sv == (-9223372036854775807LL - 1))
put(c, "(-9223372036854775807LL - 1)");
else {
put(c, "(");
put_i(c, sv);
put(c, ")");
}
} else {
put(c, "(");
put_u_raw(c, (usize)raw);
if (te->int_type.bits >= 64)
put(c, "ull");
put(c, ")");
}
break;
}
case SPL_TYPE_PTR:
if (v.global_id) {
put(c, "(void*)gg");
put_u_raw(c, v.global_id);
} else {
put(c, "(void*)0");
}
break;
case SPL_TYPE_FN:
if (v.fn == 0) {
put(c, "(void*)0");
} else {
put(c, "(void*)&");
c_fn_name(c, v.fn);
}
break;
case SPL_TYPE_FLOAT: {
char buf[64];
if (isfinite(v.float_bits))
snprintf(buf, sizeof buf, "%.17g", v.float_bits);
else
snprintf(buf, sizeof buf, "0.0");
put(c, buf);
break;
}
default:
put(c, "0");
break;
}
}
static void collect_const_cb(void *ud, spl_ir_value_t v) {
ir2c_t *c = ud;
if (v.kind != SPL_IR_VALUE_INST)
(void)const_slot_lookup(c, v);
}
static void declare_consts(ir2c_t *c) {
for (usize i = 0; i < vec_size(c->cconsts); ++i) {
cconst_t *e = &vec_at(c->cconsts, i);
if (!put_type(c, e->tid))
put(c, "void*");
put(c, " v");
put_u_raw(c, SPL_IR2C_CONST_BASE + e->slot);
put(c, " = ");
spl_ir_value_t v = {0};
v.kind = e->kind;
v.tid = e->tid;
memcpy(&v.int_bits, &e->bits, sizeof e->bits);
emit_const_literal(c, v);
put(c, ";\n");
}
}
static void declare_all(ir2c_t *c, const spl_ir_func_t *f) { static void declare_all(ir2c_t *c, const spl_ir_func_t *f) {
c->cconsts.size = 0;
for (usize i = 1; i < vec_size(f->nodes); ++i)
spl_ir_node_visit_values(&vec_at(f->nodes, i), collect_const_cb, c);
for (usize i = 1; i < vec_size(f->nodes); ++i) { for (usize i = 1; i < vec_size(f->nodes); ++i) {
const spl_ir_node_t *n = &vec_at(f->nodes, i); const spl_ir_node_t *n = &vec_at(f->nodes, i);
if (n->kind == SPL_IR_MEM_ALLOCA) { if (n->kind == SPL_IR_MEM_ALLOCA) {
usize count = const_node(c, f, n->mem_alloc.count); usize count = value_int(n->mem_alloc.count);
if (count == 0) if (count == 0)
count = 1; count = 1;
spl_type_id_t pointee = n->mem_alloc.tid; spl_type_id_t pointee = n->mem_alloc.tid;
@@ -1197,6 +1195,7 @@ static void declare_all(ir2c_t *c, const spl_ir_func_t *f) {
if (tid != 0 && !tid_is_unit(c, tid)) if (tid != 0 && !tid_is_unit(c, tid))
declare_ref(c, i, tid); declare_ref(c, i, tid);
} }
declare_consts(c);
} }
static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) { static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) {
@@ -1222,6 +1221,13 @@ static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) {
if (need) if (need)
put(c, ", "); put(c, ", ");
spl_type_id_t pt = vec_at(te->fn_type.params, k); spl_type_id_t pt = vec_at(te->fn_type.params, k);
spl_type_node_t *ptn = spl_type_node(c->type, pt);
if (ptn && ptn->kind == SPL_TYPE_UNIT) {
/* varargs 标记在 IR 里落成 unit 类型。 */
put(c, "...");
need = true;
continue;
}
if (is_aggregate(c, pt)) if (is_aggregate(c, pt))
put(c, "void*"); put(c, "void*");
else if (!put_type(c, pt)) else if (!put_type(c, pt))
@@ -1267,14 +1273,63 @@ static spl_ir_func_ref_t find_main(ir2c_t *c) {
return 0; return 0;
} }
static void emit_globals(ir2c_t *c) {
for (usize i = 1; i < vec_size(c->ir->globals); ++i) {
const spl_ir_global_t *g = &vec_at(c->ir->globals, i);
usize size = type_size(c, g->tid);
usize words = (size + 7) / 8;
if (words == 0)
words = 1;
put(c, "static ");
if (g->readonly)
put(c, "const ");
put(c, "u64 gg");
put_u_raw(c, i);
put(c, "[");
put_u_raw(c, words);
put(c, "] = {");
for (usize w = 0; w < words; ++w) {
if (w > 0)
put(c, ", ");
uint64_t word = 0;
if (g->init.kind == SPL_IR_VALUE_CONST) {
spl_type_node_t *it = spl_type_node(c->type, g->init.tid);
if (it && it->kind == SPL_TYPE_ARRAY) {
spl_ir_const_t *k =
spl_ir_const((spl_ir_t *)c->ir, g->init.const_id);
if (k) {
for (usize b = 0; b < 8; ++b) {
usize idx = w * 8 + b;
if (idx < k->bytes.len)
word |= (uint64_t)k->bytes.data[idx] << (b * 8);
}
}
} else if (w == 0) {
memcpy(&word, &g->init.int_bits, sizeof word);
}
}
put_u_raw(c, (usize)word);
put(c, "ull");
}
put(c, "};");
if (g->name) {
put(c, " /* ");
put(c, g->name);
put(c, " */");
}
put(c, "\n");
}
}
static void run(ir2c_t *c) { static void run(ir2c_t *c) {
put(c, "// AUTO-GENERATED\n"); put(c, "// AUTO-GENERATED\n");
put(c, "#include \"spl_rt.h\"\n"); put(c, "#include \"spl_rt.h\"\n");
emit_globals(c);
usize nf = vec_size(c->ir->funcs); usize nf = vec_size(c->ir->funcs);
for (spl_ir_func_ref_t i = 1; i < nf; ++i) { for (spl_ir_func_ref_t i = 1; i < nf; ++i) {
const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, i); const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, i);
if (f && !fn_is_extern(c, f)) if (f)
emit_proto(c, i); emit_proto(c, i);
} }
for (spl_ir_func_ref_t i = 1; i < nf; ++i) { for (spl_ir_func_ref_t i = 1; i < nf; ++i) {
@@ -1291,9 +1346,16 @@ static void run(ir2c_t *c) {
if (te && te->kind == SPL_TYPE_FN) if (te && te->kind == SPL_TYPE_FN)
nargs = vec_size(te->fn_type.params); nargs = vec_size(te->fn_type.params);
if (nargs == 0) { if (nargs == 0) {
put(c, "int main(void) { f"); put(c, "int main(void) { ");
if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) {
put(c, "return (int)f");
put_u(c, m);
put(c, "(); }\n");
} else {
put(c, "f");
put_u(c, m); put_u(c, m);
put(c, "(); return 0; }\n"); put(c, "(); return 0; }\n");
}
} else { } else {
put(c, "int main(int argc, char** argv) { "); put(c, "int main(int argc, char** argv) { ");
if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) { if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) {
@@ -1317,9 +1379,10 @@ bool spl_ir2c_run(const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layo
.layout = layout, .layout = layout,
.attr = attr, .attr = attr,
.out = out, .out = out,
.gdata_n = 1,
.failed = false, .failed = false,
}; };
vec_init(c.cconsts);
run(&c); run(&c);
vec_free(c.cconsts);
return !c.failed; return !c.failed;
} }
+140 -106
View File
@@ -125,11 +125,11 @@ static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_type_id_t tid) {
} }
} }
/* 计算 IR 节点的"值类型",两处用途: /* 计算 IR 节点的值类型,两处用途:
* 1) frame_build 判断该节点是否占用结果槽(只看是否非 0); * 1) frame_build 判断该节点是否占用结果槽(只看是否非 0);
* 2) CALL 推导实参的 LOAD/STORE kind。 * 2) CALL 推导实参的 LOAD/STORE kind。
* mem.offset/mem.field 的 .tid 存的是 pointee/aggregate,须转成指针类型。 * mem.offset/mem.field 的 .tid 存的是 pointee/aggregate,须转成指针类型。
* cmp/type.* 这里刻意返回操作数/被测量类型的 tid 而非 bool/usize: * cmp/type.* 这里刻意返回操作数被测量类型的 tid 而非 bool/usize;
* CALL 实参 kind 依赖此点,改动会改变生成字节码,diff 测试据此守门。 */ * CALL 实参 kind 依赖此点,改动会改变生成字节码,diff 测试据此守门。 */
static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) { static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
switch (n->kind) { switch (n->kind) {
@@ -168,8 +168,6 @@ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
return n->cast.to_tid; return n->cast.to_tid;
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
return spl_type_builder_ptr((spl_type_t *)ctx->type, n->mem_alloc.tid); return spl_type_builder_ptr((spl_type_t *)ctx->type, n->mem_alloc.tid);
case SPL_IR_MEM_GLOBAL_ALLOC:
return n->mem_global_alloc.tid;
case SPL_IR_MEM_LOAD: case SPL_IR_MEM_LOAD:
return n->mem_load.tid; return n->mem_load.tid;
case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_OFFSET:
@@ -179,8 +177,6 @@ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
ir_agg_field_tid(ctx, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx); ir_agg_field_tid(ctx, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx);
return spl_type_builder_ptr((spl_type_t *)ctx->type, field); return spl_type_builder_ptr((spl_type_t *)ctx->type, field);
} }
case SPL_IR_TYPE_CONST:
return n->type_const.tid;
case SPL_IR_TYPE_BITSIZEOF: case SPL_IR_TYPE_BITSIZEOF:
return n->bitsizeof.tid; return n->bitsizeof.tid;
case SPL_IR_TYPE_SIZEOF: case SPL_IR_TYPE_SIZEOF:
@@ -226,16 +222,28 @@ typedef struct {
/* panic 数据区:fmt slice + msg slice,各 2 words(ptr + len)。 */ /* panic 数据区:fmt slice + msg slice,各 2 words(ptr + len)。 */
enum { IR2VM_PANIC_WORDS = 4 }; enum { IR2VM_PANIC_WORDS = 4 };
static spl_type_id_t value_tid(spl_ir2vm_t *ctx, const spl_ir_func_t *f, spl_ir_value_t v) {
if (v.kind != SPL_IR_VALUE_INST)
return v.tid;
if (v.inst == 0 || v.inst >= vec_size(f->nodes))
return 0;
return ir_result_tid(ctx, &vec_at(f->nodes, v.inst));
}
static usize value_int(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_CONST ? (usize)v.int_bits : 0;
}
static spl_ir_func_ref_t value_fn(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_CONST ? v.fn : 0;
}
/* 结果槽存标量;聚合/大对象额外分配一块数据区(data_off)。 */ /* 结果槽存标量;聚合/大对象额外分配一块数据区(data_off)。 */
static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_ir_node_t *node) { static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_ir_node_t *node) {
(void)f;
switch (node->kind) { switch (node->kind) {
case SPL_IR_MEM_ALLOCA: { case SPL_IR_MEM_ALLOCA: {
usize count = 1; usize count = value_int(node->mem_alloc.count);
if (node->mem_alloc.count < vec_size(f->nodes)) {
const spl_ir_node_t *cn = &vec_at(f->nodes, node->mem_alloc.count);
if (cn->kind == SPL_IR_TYPE_CONST)
count = (usize)cn->type_const.int_lit;
}
if (count == 0) if (count == 0)
count = 1; count = 1;
usize sz = ir_type_size(ctx, node->mem_alloc.tid); usize sz = ir_type_size(ctx, node->mem_alloc.tid);
@@ -280,7 +288,7 @@ static void frame_build(frame_layout_t *fl, spl_ir2vm_t *ctx, spl_ir_func_ref_t
for (usize i = 0; i < n; ++i) for (usize i = 0; i < n; ++i)
vec_push(fl->slots, (slot_t){0}); vec_push(fl->slots, (slot_t){0});
/* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽 */ /* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽。 */
usize nargs = fn_nargs(ctx, f->fn_tid); usize nargs = fn_nargs(ctx, f->fn_tid);
usize hidden_sret = ir_is_aggregate(ctx, fn_ret_type(ctx, f->fn_tid)) ? 1 : 0; usize hidden_sret = ir_is_aggregate(ctx, fn_ret_type(ctx, f->fn_tid)) ? 1 : 0;
fl->args_bytes = (nargs + hidden_sret) * sizeof(spl_vm_val_t); fl->args_bytes = (nargs + hidden_sret) * sizeof(spl_vm_val_t);
@@ -343,8 +351,14 @@ static void CG_OP(cgen_t *cg, uint8_t op, uint8_t k) { emit(cg, op, k, 0); }
static void CG_SWAP(cgen_t *cg) { emit(cg, SPL_SWAP, SPL_VOID, 0); } static void CG_SWAP(cgen_t *cg) { emit(cg, SPL_SWAP, SPL_VOID, 0); }
static void CG_GADDR(cgen_t *cg, spl_vm_val_t idx) { emit(cg, SPL_GADDR, SPL_VOID, idx); } static void CG_GADDR(cgen_t *cg, spl_vm_val_t idx) { emit(cg, SPL_GADDR, SPL_VOID, idx); }
static void cg_load_slot(cgen_t *cg, usize ref, uint8_t k) { static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k);
CG_LADDR(cg, vec_at(cg->frame.slots, ref).result_off);
static void cg_load_slot(cgen_t *cg, spl_ir_value_t v, uint8_t k) {
if (v.kind != SPL_IR_VALUE_INST) {
cg_push_const(cg, v, k);
return;
}
CG_LADDR(cg, vec_at(cg->frame.slots, v.inst).result_off);
CG_LOAD(cg, k); CG_LOAD(cg, k);
} }
@@ -364,6 +378,7 @@ typedef struct {
bool indirect; bool indirect;
usize off; usize off;
usize base; usize base;
int gdata; /* >0:全局数据区索引,直接 GADDR */
} data_loc_t; } data_loc_t;
static data_loc_t direct_data(usize off) { static data_loc_t direct_data(usize off) {
@@ -372,16 +387,19 @@ static data_loc_t direct_data(usize off) {
static data_loc_t direct_data_base(usize off, usize base) { static data_loc_t direct_data_base(usize off, usize base) {
return (data_loc_t){.indirect = false, .off = off, .base = base}; return (data_loc_t){.indirect = false, .off = off, .base = base};
} }
static data_loc_t ptr_slot(usize slot_off) {
return (data_loc_t){.indirect = true, .off = slot_off, .base = 0};
}
static data_loc_t ptr_slot_base(usize slot_off, usize base) { static data_loc_t ptr_slot_base(usize slot_off, usize base) {
return (data_loc_t){.indirect = true, .off = slot_off, .base = base}; return (data_loc_t){.indirect = true, .off = slot_off, .base = base};
} }
static void cg_push_addr(cgen_t *cg, data_loc_t loc, usize extra) { static void cg_push_addr(cgen_t *cg, data_loc_t loc, usize extra) {
usize delta = loc.base + extra; usize delta = loc.base + extra;
if (loc.indirect) { if (loc.gdata > 0) {
emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)loc.gdata);
if (delta) {
CG_PUSH(cg, SPL_U64, delta);
CG_OP(cg, SPL_ADD, SPL_PTR);
}
} else if (loc.indirect) {
CG_LADDR(cg, loc.off); CG_LADDR(cg, loc.off);
CG_LOAD(cg, SPL_PTR); CG_LOAD(cg, SPL_PTR);
if (delta) { if (delta) {
@@ -393,6 +411,18 @@ static void cg_push_addr(cgen_t *cg, data_loc_t loc, usize extra) {
} }
} }
/* 聚合值的存储位置:指令槽指针,或全局数据区地址。 */
static data_loc_t value_ptr_loc(cgen_t *cg, spl_ir_value_t v, usize base) {
if (v.kind == SPL_IR_VALUE_GLOBAL) {
usize gid = v.global_id;
int idx = 0;
if (gid > 0 && gid < vec_size(cg->ctx->global_data_idx))
idx = (int)vec_at(cg->ctx->global_data_idx, gid);
return (data_loc_t){.gdata = idx, .base = base};
}
return ptr_slot_base(vec_at(cg->frame.slots, v.inst).result_off, base);
}
static void cg_copy_agg(cgen_t *cg, data_loc_t src, data_loc_t dst, spl_type_id_t tid) { static void cg_copy_agg(cgen_t *cg, data_loc_t src, data_loc_t dst, spl_type_id_t tid) {
usize sz = ir_type_size(cg->ctx, tid); usize sz = ir_type_size(cg->ctx, tid);
usize done = 0; usize done = 0;
@@ -442,54 +472,57 @@ static void cg_cmp(cgen_t *cg, usize ref, const spl_ir_node_t *n, uint8_t op) {
cg_store_slot(cg, ref, SPL_BOOL); cg_store_slot(cg, ref, SPL_BOOL);
} }
static void cg_const(cgen_t *cg, usize ref, const spl_ir_node_t *n) { static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k) {
spl_ir2vm_t *ctx = cg->ctx; spl_ir2vm_t *ctx = cg->ctx;
spl_type_id_t tid = n->type_const.tid;
spl_type_node_t *tn = spl_type_node(ctx->type, tid); if (v.kind == SPL_IR_VALUE_GLOBAL) {
int gi = (v.global_id > 0 && v.global_id < vec_size(ctx->global_data_idx))
? (int)vec_at(ctx->global_data_idx, v.global_id)
: 0;
if (gi > 0)
emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)gi);
else
CG_PUSH(cg, SPL_PTR, 0);
return;
}
spl_type_node_t *tn = spl_type_node(ctx->type, v.tid);
if (!tn) if (!tn)
return; return;
if (tn->kind == SPL_TYPE_FN) { if (tn->kind == SPL_TYPE_FN) {
isize nat_idx = -1; isize nat_idx = -1;
if (n->type_const.fn > 0) if (v.fn > 0)
map_get(ctx->extern_native, n->type_const.fn, &nat_idx); map_get(ctx->extern_native, v.fn, &nat_idx);
if (nat_idx >= 0) { if (nat_idx >= 0) {
CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx); CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx);
} else { } else {
usize idx = ip_of(cg); usize idx = ip_of(cg);
CG_PUSH(cg, SPL_PTR, 0); CG_PUSH(cg, SPL_PTR, 0);
cg_add_fix(cg, idx, FIX_FNADDR, 0, n->type_const.fn); cg_add_fix(cg, idx, FIX_FNADDR, 0, v.fn);
} }
cg_store_slot(cg, ref, SPL_PTR);
return; return;
} }
uint8_t k = vm_kind_of(ctx, tid);
spl_vm_val_t imm = 0; spl_vm_val_t imm = 0;
switch (tn->kind) { switch (tn->kind) {
case SPL_TYPE_BOOL: case SPL_TYPE_BOOL:
imm = n->type_const.bool_lit ? 1 : 0; imm = v.int_bits ? 1 : 0;
break; break;
case SPL_TYPE_INT: case SPL_TYPE_INT:
if (tn->int_type.bits == 8) imm = (spl_vm_val_t)v.int_bits;
imm = (spl_vm_val_t)(unsigned char)n->type_const.ch_lit;
else
imm = (spl_vm_val_t)n->type_const.int_lit;
break; break;
case SPL_TYPE_FLOAT: case SPL_TYPE_FLOAT:
memcpy(&imm, &n->type_const.float_lit, sizeof(double)); memcpy(&imm, &v.float_bits, sizeof(double));
break; break;
case SPL_TYPE_PTR: case SPL_TYPE_PTR:
imm = (spl_vm_val_t)(usize)n->type_const.ptr_lit; imm = 0;
break; break;
case SPL_TYPE_UNIT:
return;
default: default:
break; break;
} }
CG_PUSH(cg, k, imm); CG_PUSH(cg, k, imm);
cg_store_slot(cg, ref, k);
} }
static void cg_cast(cgen_t *cg, usize ref, const spl_ir_node_t *n) { static void cg_cast(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
@@ -528,26 +561,11 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break; break;
} }
case SPL_IR_MEM_GLOBAL_ALLOC: {
const spl_ir_node_t *cn = &vec_at(cg->irf->nodes, n->mem_global_alloc.const_node);
int idx = 0;
if (cn->kind == SPL_IR_TYPE_CONST && cn->type_const.ptr_lit) {
usize size = ir_type_size(ctx, cn->type_const.tid);
idx = spl_prog_add_data(&ctx->prog, cn->type_const.ptr_lit, size);
}
if (idx > 0)
emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)idx);
else
CG_PUSH(cg, SPL_PTR, 0);
cg_store_slot(cg, ref, SPL_PTR);
break;
}
case SPL_IR_MEM_LOAD: { case SPL_IR_MEM_LOAD: {
spl_type_id_t tid = n->mem_load.tid; spl_type_id_t tid = n->mem_load.tid;
if (ir_is_aggregate(ctx, tid)) { if (ir_is_aggregate(ctx, tid)) {
usize data = vec_at(cg->frame.slots, ref).data_off; usize data = vec_at(cg->frame.slots, ref).data_off;
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->mem_load.ptr).result_off), cg_copy_agg(cg, value_ptr_loc(cg, n->mem_load.ptr, 0),
direct_data(data), tid); direct_data(data), tid);
CG_LADDR(cg, data); CG_LADDR(cg, data);
cg_store_slot(cg, ref, SPL_PTR); cg_store_slot(cg, ref, SPL_PTR);
@@ -563,8 +581,8 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
case SPL_IR_MEM_STORE: { case SPL_IR_MEM_STORE: {
spl_type_id_t tid = n->mem_store.tid; spl_type_id_t tid = n->mem_store.tid;
if (ir_is_aggregate(ctx, tid)) { if (ir_is_aggregate(ctx, tid)) {
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->mem_store.val).result_off), cg_copy_agg(cg, value_ptr_loc(cg, n->mem_store.val, 0),
ptr_slot(vec_at(cg->frame.slots, n->mem_store.ptr).result_off), tid); value_ptr_loc(cg, n->mem_store.ptr, 0), tid);
} else { } else {
uint8_t k = vm_kind_of(ctx, tid); uint8_t k = vm_kind_of(ctx, tid);
cg_load_slot(cg, n->mem_store.ptr, SPL_PTR); cg_load_slot(cg, n->mem_store.ptr, SPL_PTR);
@@ -613,14 +631,16 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
spl_type_id_t agg_t = n->agg_construct.tid; spl_type_id_t agg_t = n->agg_construct.tid;
for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) { for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) {
spl_ir_node_ref_t fref = vec_at(n->agg_construct.fields, i); spl_ir_value_t fref = vec_at(n->agg_construct.fields, i);
if (spl_ir_value_is_none(fref))
continue;
spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, i); spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, i);
if (!ftid) if (!ftid)
continue; continue;
usize foff = ir_agg_field_off(ctx, agg_t, i); usize foff = ir_agg_field_off(ctx, agg_t, i);
if (ir_is_aggregate(ctx, ftid)) { if (ir_is_aggregate(ctx, ftid)) {
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, fref).result_off), cg_copy_agg(cg, value_ptr_loc(cg, fref, 0),
direct_data_base(base, foff), ftid); direct_data_base(base, foff), ftid);
} else { } else {
uint8_t fk = vm_kind_of(ctx, ftid); uint8_t fk = vm_kind_of(ctx, ftid);
@@ -641,7 +661,7 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
if (ir_is_aggregate(ctx, ftid)) { if (ir_is_aggregate(ctx, ftid)) {
usize data = vec_at(cg->frame.slots, ref).data_off; usize data = vec_at(cg->frame.slots, ref).data_off;
cg_copy_agg(cg, cg_copy_agg(cg,
ptr_slot_base(vec_at(cg->frame.slots, n->agg_extract.val).result_off, foff), value_ptr_loc(cg, n->agg_extract.val, foff),
direct_data(data), ftid); direct_data(data), ftid);
CG_LADDR(cg, data); CG_LADDR(cg, data);
cg_store_slot(cg, ref, SPL_PTR); cg_store_slot(cg, ref, SPL_PTR);
@@ -662,14 +682,14 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
usize base = vec_at(cg->frame.slots, ref).data_off; usize base = vec_at(cg->frame.slots, ref).data_off;
spl_type_id_t agg_t = n->agg_insert.tid; spl_type_id_t agg_t = n->agg_insert.tid;
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->agg_insert.agg).result_off), cg_copy_agg(cg, value_ptr_loc(cg, n->agg_insert.agg, 0),
direct_data(base), agg_t); direct_data(base), agg_t);
usize foff = ir_agg_field_off(ctx, agg_t, n->agg_insert.field_idx); usize foff = ir_agg_field_off(ctx, agg_t, n->agg_insert.field_idx);
spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, n->agg_insert.field_idx); spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, n->agg_insert.field_idx);
if (ftid) { if (ftid) {
if (ir_is_aggregate(ctx, ftid)) { if (ir_is_aggregate(ctx, ftid)) {
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->agg_insert.field).result_off), cg_copy_agg(cg, value_ptr_loc(cg, n->agg_insert.field, 0),
direct_data_base(base, foff), ftid); direct_data_base(base, foff), ftid);
} else { } else {
uint8_t fk = vm_kind_of(ctx, ftid); uint8_t fk = vm_kind_of(ctx, ftid);
@@ -704,12 +724,7 @@ static void cg_typeop(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
v = ir_type_align(ctx, n->ir_alignof.tid); v = ir_type_align(ctx, n->ir_alignof.tid);
break; break;
case SPL_IR_TYPE_OFFSETOF: { case SPL_IR_TYPE_OFFSETOF: {
usize idx = 0; usize idx = value_int(n->ir_offsetof.field_idx);
if (n->ir_offsetof.field_idx < vec_size(cg->irf->nodes)) {
const spl_ir_node_t *idx_node = &vec_at(cg->irf->nodes, n->ir_offsetof.field_idx);
if (idx_node->kind == SPL_IR_TYPE_CONST)
idx = (usize)idx_node->type_const.int_lit;
}
v = ir_agg_field_off(ctx, n->ir_offsetof.tid, idx); v = ir_agg_field_off(ctx, n->ir_offsetof.tid, idx);
break; break;
} }
@@ -733,13 +748,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
case SPL_IR_CONTROL_PARAM: { case SPL_IR_CONTROL_PARAM: {
uint8_t k = vm_kind_of(ctx, n->control_param.tid); uint8_t k = vm_kind_of(ctx, n->control_param.tid);
usize idx = 0; usize idx = value_int(n->control_param.idx);
if (n->control_param.idx < vec_size(cg->irf->nodes)) { /* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起。 */
const spl_ir_node_t *idx_node = &vec_at(cg->irf->nodes, n->control_param.idx);
if (idx_node->kind == SPL_IR_TYPE_CONST)
idx = (usize)idx_node->type_const.int_lit;
}
/* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起 */
usize shift = ir_is_aggregate(ctx, fn_ret_type(ctx, cg->irf->fn_tid)) ? 1 : 0; usize shift = ir_is_aggregate(ctx, fn_ret_type(ctx, cg->irf->fn_tid)) ? 1 : 0;
CG_LADDR(cg, (idx + shift) * sizeof(spl_vm_val_t)); CG_LADDR(cg, (idx + shift) * sizeof(spl_vm_val_t));
CG_LOAD(cg, k); CG_LOAD(cg, k);
@@ -750,8 +760,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
case SPL_IR_CONTROL_RET: { case SPL_IR_CONTROL_RET: {
spl_type_id_t ret_tid = n->control_ret.tid; spl_type_id_t ret_tid = n->control_ret.tid;
if (ir_is_aggregate(ctx, ret_tid)) { if (ir_is_aggregate(ctx, ret_tid)) {
/* sret:把返回值写入 *(fp+0),然后以 void 返回 */ /* sret:把返回值写入 *(fp+0),然后以 void 返回。 */
if (n->control_ret.val) { if (!spl_ir_value_is_none(n->control_ret.val)) {
usize sz = ir_type_size(ctx, ret_tid); usize sz = ir_type_size(ctx, ret_tid);
CG_LADDR(cg, 0); CG_LADDR(cg, 0);
CG_LOAD(cg, SPL_PTR); CG_LOAD(cg, SPL_PTR);
@@ -763,7 +773,7 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break; break;
} }
uint8_t k = vm_kind_of(ctx, ret_tid); uint8_t k = vm_kind_of(ctx, ret_tid);
if (k != SPL_VOID && n->control_ret.val) if (k != SPL_VOID && !spl_ir_value_is_none(n->control_ret.val))
cg_load_slot(cg, n->control_ret.val, k); cg_load_slot(cg, n->control_ret.val, k);
emit(cg, SPL_RET, k, 0); emit(cg, SPL_RET, k, 0);
break; break;
@@ -788,13 +798,7 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
} }
case SPL_IR_CONTROL_CALL: { case SPL_IR_CONTROL_CALL: {
spl_ir_func_ref_t callee_ref = 0; spl_ir_func_ref_t callee_ref = value_fn(n->control_call.func);
spl_ir_node_ref_t fref = n->control_call.func;
if (fref < vec_size(cg->irf->nodes)) {
const spl_ir_node_t *fn_node = &vec_at(cg->irf->nodes, fref);
if (fn_node->kind == SPL_IR_TYPE_CONST)
callee_ref = fn_node->type_const.fn;
}
isize nat_idx = -1; isize nat_idx = -1;
if (callee_ref > 0) if (callee_ref > 0)
@@ -804,9 +808,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
if (nat_idx >= 0) { if (nat_idx >= 0) {
for (usize i = 0; i < nargs; ++i) { for (usize i = 0; i < nargs; ++i) {
spl_ir_node_ref_t pref = vec_at(n->control_call.params, i); spl_ir_value_t pref = vec_at(n->control_call.params, i);
const spl_ir_node_t *pn = &vec_at(cg->irf->nodes, pref); uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref));
uint8_t pk = vm_kind_of(ctx, ir_result_tid(ctx, pn));
cg_load_slot(cg, pref, pk); cg_load_slot(cg, pref, pk);
} }
CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx); CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx);
@@ -831,17 +834,16 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
CG_LADDR(cg, sret_off); CG_LADDR(cg, sret_off);
} }
for (usize i = 0; i < nargs; ++i) { for (usize i = 0; i < nargs; ++i) {
spl_ir_node_ref_t pref = vec_at(n->control_call.params, i); spl_ir_value_t pref = vec_at(n->control_call.params, i);
const spl_ir_node_t *pn = &vec_at(cg->irf->nodes, pref); uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref));
uint8_t pk = vm_kind_of(ctx, ir_result_tid(ctx, pn));
cg_load_slot(cg, pref, pk); cg_load_slot(cg, pref, pk);
} }
cg_load_slot(cg, fref, SPL_PTR); cg_load_slot(cg, n->control_call.func, SPL_PTR);
spl_vm_val_t actual_nargs = nargs + (ret_is_sret ? 1 : 0); spl_vm_val_t actual_nargs = nargs + (ret_is_sret ? 1 : 0);
emit(cg, SPL_CALL, SPL_VOID, actual_nargs); emit(cg, SPL_CALL, SPL_VOID, actual_nargs);
if (ret_is_sret) { if (ret_is_sret) {
/* result slot 保存 sret 指针 */ /* result slot 保存 sret 指针。 */
CG_LADDR(cg, sret_off); CG_LADDR(cg, sret_off);
cg_store_slot(cg, ref, SPL_PTR); cg_store_slot(cg, ref, SPL_PTR);
} else { } else {
@@ -865,10 +867,6 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) { static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
switch (n->kind) { switch (n->kind) {
case SPL_IR_TYPE_CONST:
cg_const(cg, ref, n);
break;
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_ADD:
cg_binop(cg, ref, n, SPL_ADD); cg_binop(cg, ref, n, SPL_ADD);
break; break;
@@ -970,7 +968,6 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break; break;
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
case SPL_IR_MEM_GLOBAL_ALLOC:
case SPL_IR_MEM_LOAD: case SPL_IR_MEM_LOAD:
case SPL_IR_MEM_STORE: case SPL_IR_MEM_STORE:
case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_OFFSET:
@@ -1011,19 +1008,19 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
if (msg_idx <= 0 || fmt_idx <= 0) if (msg_idx <= 0 || fmt_idx <= 0)
break; break;
/* fmt slice = {ptr, len=2} 写到 data+0 */ /* fmt slice = {ptr, len=2} 写到 data+0。 */
CG_GADDR(cg, (spl_vm_val_t)fmt_idx); CG_GADDR(cg, (spl_vm_val_t)fmt_idx);
cg_store_at(cg, data + 0, SPL_PTR); cg_store_at(cg, data + 0, SPL_PTR);
CG_PUSH(cg, SPL_U64, 2); CG_PUSH(cg, SPL_U64, 2);
cg_store_at(cg, data + 8, SPL_U64); cg_store_at(cg, data + 8, SPL_U64);
/* msg slice = {ptr, len} 写到 data+16 */ /* msg slice = {ptr, len} 写到 data+16。 */
CG_GADDR(cg, (spl_vm_val_t)msg_idx); CG_GADDR(cg, (spl_vm_val_t)msg_idx);
cg_store_at(cg, data + 16, SPL_PTR); cg_store_at(cg, data + 16, SPL_PTR);
CG_PUSH(cg, SPL_U64, n->dbg_panic.len); CG_PUSH(cg, SPL_U64, n->dbg_panic.len);
cg_store_at(cg, data + 24, SPL_U64); cg_store_at(cg, data + 24, SPL_U64);
/* push fmt slice 指针,push msg slice 指针 */ /* push fmt slice 指针,push msg slice 指针。 */
CG_LADDR(cg, data + 0); CG_LADDR(cg, data + 0);
CG_LADDR(cg, data + 16); CG_LADDR(cg, data + 16);
@@ -1040,7 +1037,7 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break; break;
default: default:
/* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */ /* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */
Panic("ir2vm: unsupported IR node kind %d", (int)n->kind); Panic("ir2vm: unsupported IR node kind %d", (int)n->kind);
break; break;
} }
@@ -1121,6 +1118,40 @@ static void register_externs(spl_ir2vm_t *ctx) {
static usize spl_map_hash(usize k) { return MAP_HASH_INT(k); } static usize spl_map_hash(usize k) { return MAP_HASH_INT(k); }
static int spl_map_cmp(usize a, usize b) { return MAP_CMP_INT(a, b); } static int spl_map_cmp(usize a, usize b) { return MAP_CMP_INT(a, b); }
static void prepare_globals(spl_ir2vm_t *ctx) {
vec_push(ctx->global_data_idx, 0);
for (usize i = 1; i < vec_size(ctx->ir->globals); ++i) {
const spl_ir_global_t *g = &vec_at(ctx->ir->globals, i);
usize size = ir_type_size(ctx, g->tid);
if (size == 0)
size = 1;
unsigned char *buf = calloc(1, size);
if (!buf) {
vec_push(ctx->global_data_idx, 0);
continue;
}
if (g->init.kind == SPL_IR_VALUE_CONST) {
spl_type_node_t *it = spl_type_node(ctx->type, g->init.tid);
if (it && it->kind == SPL_TYPE_ARRAY) {
spl_ir_const_t *k = spl_ir_const((spl_ir_t *)ctx->ir, g->init.const_id);
if (k) {
usize n = size < k->bytes.len ? size : k->bytes.len;
memcpy(buf, k->bytes.data, n);
}
} else {
uint64_t bits = 0;
memcpy(&bits, &g->init.int_bits, sizeof bits);
usize n = size < sizeof(bits) ? size : sizeof(bits);
for (usize j = 0; j < n; ++j)
buf[j] = (unsigned char)(bits >> (j * 8));
}
}
int idx = spl_prog_add_data(&ctx->prog, buf, size);
vec_push(ctx->global_data_idx, idx);
free(buf);
}
}
void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type, void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type,
spl_layout_t *layout, const spl_attr_t *attr) { spl_layout_t *layout, const spl_attr_t *attr) {
Assert(ir != NULL && type != NULL && attr != NULL); Assert(ir != NULL && type != NULL && attr != NULL);
@@ -1129,6 +1160,7 @@ void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type
ctx->attr = attr; ctx->attr = attr;
ctx->layout = layout; ctx->layout = layout;
map_init(ctx->extern_native, spl_map_hash, spl_map_cmp); map_init(ctx->extern_native, spl_map_hash, spl_map_cmp);
vec_init(ctx->global_data_idx);
spl_prog_init(&ctx->prog); spl_prog_init(&ctx->prog);
ctx->diag = NULL; ctx->diag = NULL;
@@ -1136,6 +1168,7 @@ void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type
void spl_ir2vm_drop(spl_ir2vm_t *ctx) { void spl_ir2vm_drop(spl_ir2vm_t *ctx) {
spl_prog_drop(&ctx->prog); spl_prog_drop(&ctx->prog);
vec_free(ctx->global_data_idx);
map_free(ctx->extern_native); map_free(ctx->extern_native);
} }
@@ -1145,12 +1178,13 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) {
return 0; return 0;
register_externs(ctx); register_externs(ctx);
prepare_globals(ctx);
/* 保留 insn 0 作为哨兵:确保真实函数入口地址非零, /* 保留 insn 0 作为哨兵:确认真实函数入口地址非零,
从而 0 可唯一表示 null 函数指针(函数指针与 null 比较)。 */ 从而使 0 唯一表示 null 函数指针(函数指针与 null 比较)。 */
spl_prog_add_instr(&ctx->prog, SPL_HALT, SPL_VOID, 0); spl_prog_add_instr(&ctx->prog, SPL_HALT, SPL_VOID, 0);
/* Pass 2:翻译非 extern 的函数。 */
/* Pass 2:翻译非 extern 的函数 */ /* Pass 2锛氱炕璇戦潪 extern 鐨勫嚱鏁?*/
usize *fid_ip = calloc(n, sizeof(usize)); usize *fid_ip = calloc(n, sizeof(usize));
fix_vec_t all_fixups; fix_vec_t all_fixups;
vec_init(all_fixups); vec_init(all_fixups);
@@ -1161,8 +1195,8 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) {
continue; continue;
fid_ip[i] = translate_function(ctx, i, &all_fixups); fid_ip[i] = translate_function(ctx, i, &all_fixups);
} }
/* FNADDR 回填。 */
/* FNADDR 回填 */ /* FNADDR 鍥炲~ */
for (usize i = 0; i < vec_size(all_fixups); ++i) { for (usize i = 0; i < vec_size(all_fixups); ++i) {
fix_t *fx = &vec_at(all_fixups, i); fix_t *fx = &vec_at(all_fixups, i);
if (fx->kind != FIX_FNADDR) if (fx->kind != FIX_FNADDR)
+10 -2
View File
@@ -5,26 +5,34 @@
#include "spl_ir.h" #include "spl_ir.h"
#include "spl_sema.h" #include "spl_sema.h"
typedef MAP(spl_def_id_t, spl_ir_node_ref_t) spl_def2ir_map_t; typedef MAP(spl_def_id_t, spl_ir_value_t) spl_def2ir_map_t;
typedef struct { typedef struct {
usize cont_blk; usize cont_blk;
usize defer_base; usize defer_base;
const char *label; const char *label;
spl_ir_node_ref_vec_t breaks; spl_ir_node_ref_vec_t breaks;
spl_ir_node_ref_t result_slot; spl_ir_value_t result_slot;
spl_type_id_t result_tid; spl_type_id_t result_tid;
} spl_loop_ctx_t; } spl_loop_ctx_t;
typedef VEC(spl_loop_ctx_t) spl_loop_ctx_vec_t; typedef VEC(spl_loop_ctx_t) spl_loop_ctx_vec_t;
typedef VEC(spl_ast_node_ref_t) spl_defer_stack_t; typedef VEC(spl_ast_node_ref_t) spl_defer_stack_t;
typedef struct {
spl_ast_t *ast;
spl_ast_node_ref_t decl;
} spl_module_init_t;
typedef VEC(spl_module_init_t) spl_module_init_vec_t;
typedef struct { typedef struct {
const spl_sema_t *sema; const spl_sema_t *sema;
spl_ir_builder_t *ir; spl_ir_builder_t *ir;
spl_ast_t *ast; spl_ast_t *ast;
spl_layout_t *layout; /* 借用 splc0 的 layout */ spl_layout_t *layout; /* 借用 splc0 的 layout */
spl_def2ir_map_t def2ir_map; spl_def2ir_map_t def2ir_map;
MAP(spl_def_id_t, usize) global_map;
spl_module_init_vec_t module_inits;
spl_loop_ctx_vec_t loop_stack; spl_loop_ctx_vec_t loop_stack;
spl_defer_stack_t defer_stack; spl_defer_stack_t defer_stack;
spl_lang_type_id_t cur_ret_lang; spl_lang_type_id_t cur_ret_lang;
+216 -106
View File
@@ -39,14 +39,12 @@
X(case.int2float, V0, SPL_IR_CASE_INT2FLOAT) \ X(case.int2float, V0, SPL_IR_CASE_INT2FLOAT) \
X(case.float2int, V0, SPL_IR_CASE_FLOAT2INT) \ X(case.float2int, V0, SPL_IR_CASE_FLOAT2INT) \
X(mem.alloca, V0, SPL_IR_MEM_ALLOCA) \ X(mem.alloca, V0, SPL_IR_MEM_ALLOCA) \
X(mem.global_alloc, V0, SPL_IR_MEM_GLOBAL_ALLOC) \
X(mem.load, V0, SPL_IR_MEM_LOAD) \ X(mem.load, V0, SPL_IR_MEM_LOAD) \
X(mem.store, V0, SPL_IR_MEM_STORE) \ X(mem.store, V0, SPL_IR_MEM_STORE) \
X(mem.offset, V0, SPL_IR_MEM_OFFSET) \ X(mem.offset, V0, SPL_IR_MEM_OFFSET) \
X(mem.field, V0, SPL_IR_MEM_FIELD_PTR) \ X(mem.field, V0, SPL_IR_MEM_FIELD_PTR) \
X(mem.copy, V0, SPL_IR_MEM_COPY) \ X(mem.copy, V0, SPL_IR_MEM_COPY) \
X(mem.set, V0, SPL_IR_MEM_SET) \ X(mem.set, V0, SPL_IR_MEM_SET) \
X(type.const, V0, SPL_IR_TYPE_CONST) \
X(type.bitsizeof, V0, SPL_IR_TYPE_BITSIZEOF) \ X(type.bitsizeof, V0, SPL_IR_TYPE_BITSIZEOF) \
X(type.sizeof, V0, SPL_IR_TYPE_SIZEOF) \ X(type.sizeof, V0, SPL_IR_TYPE_SIZEOF) \
X(type.alignof, V0, SPL_IR_TYPE_ALIGNOF) \ X(type.alignof, V0, SPL_IR_TYPE_ALIGNOF) \
@@ -65,6 +63,7 @@
X(control.trap, V0, SPL_IR_CONTROL_TRAP) \ X(control.trap, V0, SPL_IR_CONTROL_TRAP) \
X(dbg.breakpoint, V0, SPL_IR_DBG_BREAKPOINT) \ X(dbg.breakpoint, V0, SPL_IR_DBG_BREAKPOINT) \
X(dbg.panic, V0, SPL_IR_DBG_PANIC) \ X(dbg.panic, V0, SPL_IR_DBG_PANIC) \
/* clang-format on */
typedef enum { typedef enum {
#ifdef X #ifdef X
@@ -74,13 +73,129 @@ typedef enum {
SPL_IR_FN_TABLE SPL_IR_FN_TABLE
#undef X #undef X
} spl_ir_kind_t; } spl_ir_kind_t;
/* clang-format on */
typedef usize spl_ir_node_ref_t; /* 0 is error */ typedef usize spl_ir_node_ref_t; /* 0 is error */
typedef VEC(spl_ir_node_ref_t) spl_ir_node_ref_vec_t; typedef VEC(spl_ir_node_ref_t) spl_ir_node_ref_vec_t;
typedef usize spl_ir_func_ref_t; /* 0 is error */ typedef usize spl_ir_func_ref_t; /* 0 is error */
/* 抽象机器的值:指令结果、常量或全局实体句柄。常量按 tid 的 SPL_TYPE_* 解释:
* BOOL/INT(<=64) -> int_bits,FLOAT -> float_bits,FN -> fn,
* PTR -> 仅表示 null,其余需要去重的量 -> const_id。
* GLOBAL 表示模块级全局实体的地址,载荷是 global_id。 */
typedef enum {
SPL_IR_VALUE_INST,
SPL_IR_VALUE_CONST,
SPL_IR_VALUE_GLOBAL,
} spl_ir_value_kind_t;
typedef struct {
spl_ir_value_kind_t kind;
spl_type_id_t tid; /* CONST 时有效;INST 必须为 0 */
union {
spl_ir_node_ref_t inst;
uint64_t int_bits;
double float_bits;
spl_ir_func_ref_t fn;
usize global_id;
usize const_id;
};
} spl_ir_value_t;
typedef VEC(spl_ir_value_t) spl_ir_value_vec_t;
static inline spl_ir_value_t spl_ir_value_none(void) {
spl_ir_value_t v = {0};
return v;
}
static inline bool spl_ir_value_is_none(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_INST && v.inst == 0;
}
static inline spl_ir_value_t spl_ir_value_inst(spl_ir_node_ref_t ref) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_INST;
v.inst = ref;
return v;
}
static inline spl_ir_value_t spl_ir_value_const_int(spl_type_id_t tid, uint64_t bits) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_CONST;
v.tid = tid;
v.int_bits = bits;
return v;
}
static inline spl_ir_value_t spl_ir_value_const_float(spl_type_id_t tid, double bits) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_CONST;
v.tid = tid;
v.float_bits = bits;
return v;
}
static inline spl_ir_value_t spl_ir_value_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_CONST;
v.tid = tid;
v.fn = fn;
return v;
}
static inline spl_ir_value_t spl_ir_value_const_null(spl_type_id_t tid) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_CONST;
v.tid = tid;
v.global_id = 0;
return v;
}
static inline spl_ir_value_t spl_ir_value_global(spl_type_id_t ptr_tid, usize gid) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_GLOBAL;
v.tid = ptr_tid;
v.global_id = gid;
return v;
}
static inline spl_ir_value_t spl_ir_value_const_pool(spl_type_id_t tid, usize const_id) {
spl_ir_value_t v = {0};
v.kind = SPL_IR_VALUE_CONST;
v.tid = tid;
v.const_id = const_id;
return v;
}
static inline spl_ir_node_ref_t spl_ir_value_inst_ref(spl_ir_value_t v) {
return v.kind == SPL_IR_VALUE_INST ? v.inst : 0;
}
/* 常量池项:只放需要去重的量,标量直接内联在 Value 里。 */
typedef struct {
spl_type_id_t tid;
union {
struct {
unsigned char *data;
usize len;
} bytes;
struct {
uint64_t *limbs;
usize count;
} big_int;
};
} spl_ir_const_t;
typedef VEC(spl_ir_const_t) spl_ir_const_vec_t;
/* 全局存储声明。引用是 PTR 常量(global_id),不是指令。 */
typedef struct {
const char *name;
spl_type_id_t tid; /* 对象类型 */
spl_ir_value_t init;
bool readonly;
} spl_ir_global_t;
typedef VEC(spl_ir_global_t) spl_ir_global_vec_t;
#define SPL_ENUM_TAG_FIELD_IDX 0 #define SPL_ENUM_TAG_FIELD_IDX 0
typedef struct { typedef struct {
@@ -91,70 +206,54 @@ typedef struct {
spl_type_id_t tid; spl_type_id_t tid;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t left; spl_ir_value_t left;
spl_ir_node_ref_t right; spl_ir_value_t right;
} arith; } arith;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t a; spl_ir_value_t a;
spl_ir_node_ref_t b; spl_ir_value_t b;
} cmp; } cmp;
struct { struct {
spl_type_id_t from_tid; spl_type_id_t from_tid;
spl_type_id_t to_tid; spl_type_id_t to_tid;
spl_ir_node_ref_t val; spl_ir_value_t val;
} cast; } cast;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t count; spl_ir_value_t count;
} mem_alloc; } mem_alloc;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t const_node; spl_ir_value_t ptr;
} mem_global_alloc;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t ptr;
} mem_load; } mem_load;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t ptr; spl_ir_value_t ptr;
spl_ir_node_ref_t val; spl_ir_value_t val;
} mem_store; } mem_store;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t ptr; spl_ir_value_t ptr;
spl_ir_node_ref_t offset; spl_ir_value_t offset;
} mem_offset; } mem_offset;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t agg; spl_ir_value_t agg;
usize field_idx; usize field_idx;
} mem_field_ptr; } mem_field_ptr;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t dst; spl_ir_value_t dst;
spl_ir_node_ref_t src; spl_ir_value_t src;
spl_ir_node_ref_t size; spl_ir_value_t size;
} mem_copy; } mem_copy;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t dst; spl_ir_value_t dst;
spl_ir_node_ref_t val; spl_ir_value_t val;
spl_ir_node_ref_t size; spl_ir_value_t size;
} mem_set; } mem_set;
struct {
spl_type_id_t tid;
union {
bool bool_lit;
void *ptr_lit;
usize int_lit;
double float_lit;
// const char *cstr_lit;
char ch_lit;
spl_ir_func_ref_t fn;
};
} type_const;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
} bitsizeof; } bitsizeof;
@@ -166,7 +265,7 @@ typedef struct {
} ir_alignof; } ir_alignof;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t field_idx; spl_ir_value_t field_idx;
} ir_offsetof; } ir_offsetof;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
@@ -174,29 +273,29 @@ typedef struct {
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_vec_t fields; spl_ir_value_vec_t fields;
} agg_construct; } agg_construct;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_type_id_t field_tid; spl_type_id_t field_tid;
usize field_idx; usize field_idx;
spl_ir_node_ref_t val; spl_ir_value_t val;
} agg_extract; } agg_extract;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
usize field_idx; usize field_idx;
spl_ir_node_ref_t agg; spl_ir_value_t agg;
spl_ir_node_ref_t field; spl_ir_value_t field;
} agg_insert; } agg_insert;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t cond; spl_ir_value_t cond;
spl_ir_node_ref_t true_val; spl_ir_value_t true_val;
spl_ir_node_ref_t false_val; spl_ir_value_t false_val;
} control_select; } control_select;
struct { struct {
spl_ir_node_ref_t cond; spl_ir_value_t cond;
spl_ir_node_ref_t true_label; spl_ir_node_ref_t true_label;
spl_ir_node_ref_t false_label; spl_ir_node_ref_t false_label;
} control_br; } control_br;
@@ -205,16 +304,16 @@ typedef struct {
} control_jmp; } control_jmp;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t func; spl_ir_value_t func;
spl_ir_node_ref_vec_t params; spl_ir_value_vec_t params;
} control_call; } control_call;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t idx; spl_ir_value_t idx;
} control_param; } control_param;
struct { struct {
spl_type_id_t tid; spl_type_id_t tid;
spl_ir_node_ref_t val; spl_ir_value_t val;
} control_ret; } control_ret;
struct { struct {
@@ -243,8 +342,17 @@ typedef struct {
} spl_ir_fix_t; } spl_ir_fix_t;
typedef VEC(spl_ir_fix_t) spl_ir_fix_vec_t; typedef VEC(spl_ir_fix_t) spl_ir_fix_vec_t;
typedef struct {
spl_type_id_t tid;
const unsigned char *data;
usize len;
} spl_ir_const_key_t;
typedef struct { typedef struct {
spl_ir_func_vec_t funcs; spl_ir_func_vec_t funcs;
spl_ir_const_vec_t consts;
spl_ir_global_vec_t globals;
MAP(spl_ir_const_key_t, usize) const_index;
} spl_ir_t; } spl_ir_t;
void spl_ir_init(spl_ir_t *ir); void spl_ir_init(spl_ir_t *ir);
@@ -255,6 +363,12 @@ spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir);
spl_ir_node_t *spl_ir_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_node_ref_t node_id); spl_ir_node_t *spl_ir_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_node_ref_t node_id);
spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id); spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id);
spl_ir_const_t *spl_ir_const(spl_ir_t *ir, usize const_id);
/* INST 返回对应节点;CONST 返回 NULL。 */
spl_ir_node_t *spl_ir_value_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_value_t v);
/* 遍历节点里所有值型操作数(块标签除外)。 */
typedef void (*spl_ir_value_cb_t)(void *ud, spl_ir_value_t v);
void spl_ir_node_visit_values(spl_ir_node_t *n, spl_ir_value_cb_t cb, void *ud);
/* f 是否声明为 extern(属性表内名为 "extern" 的属性);命中返回该属性节点,否则 NULL。 /* f 是否声明为 extern(属性表内名为 "extern" 的属性);命中返回该属性节点,否则 NULL。
供各后端复用,避免 "extern" 字符串判断散落多处。 */ 供各后端复用,避免 "extern" 字符串判断散落多处。 */
@@ -323,102 +437,98 @@ usize spl_ir_builder_block_begin(spl_ir_builder_t *b);
void spl_ir_builder_block_end(spl_ir_builder_t *b); void spl_ir_builder_block_end(spl_ir_builder_t *b);
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \
spl_ir_node_ref_t left, spl_ir_node_ref_t right); spl_ir_value_t left, spl_ir_value_t right);
SPL_IR_BIN_ARITH_TABLE SPL_IR_BIN_ARITH_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \
spl_ir_node_ref_t val); spl_ir_value_t val);
SPL_IR_UN_ARITH_TABLE SPL_IR_UN_ARITH_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t tid, \
spl_ir_node_ref_t a, spl_ir_node_ref_t b); spl_ir_value_t a, spl_ir_value_t b);
SPL_IR_CMP_TABLE SPL_IR_CMP_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t from_tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t from_tid, \
spl_type_id_t to_tid, spl_ir_node_ref_t val); spl_type_id_t to_tid, spl_ir_value_t val);
SPL_IR_CAST_TABLE SPL_IR_CAST_TABLE
#undef X #undef X
/* memory */ /* memory */
spl_ir_node_ref_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t count); spl_ir_value_t count);
spl_ir_node_ref_t spl_ir_builder_mem_global_alloc(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr);
spl_ir_node_ref_t const_node);
spl_ir_node_ref_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr);
spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t val); spl_ir_value_t ptr, spl_ir_value_t val);
spl_ir_node_ref_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t offset); spl_ir_value_t ptr, spl_ir_value_t offset);
spl_ir_node_ref_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t agg,
spl_ir_node_ref_t agg, usize field_idx); usize field_idx);
spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
spl_ir_node_ref_t dst, spl_ir_node_ref_t src, spl_ir_value_t src, spl_ir_value_t size);
spl_ir_node_ref_t size); spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t val, spl_ir_value_t size);
spl_ir_node_ref_t dst, spl_ir_node_ref_t val,
spl_ir_node_ref_t size);
/* type const */ /* 常量:标量内联;需要去重的量进常量池并返回 const_id。 */
spl_ir_node_ref_t spl_ir_builder_type_const_ptr(spl_ir_builder_t *b, spl_type_id_t tid, void *val); spl_ir_value_t spl_ir_builder_const_int(spl_type_id_t tid, uint64_t bits);
spl_ir_node_ref_t spl_ir_builder_type_const_bool(spl_ir_builder_t *b, spl_type_id_t tid, bool val); spl_ir_value_t spl_ir_builder_const_bool(spl_type_id_t tid, bool val);
spl_ir_node_ref_t spl_ir_builder_type_const_int(spl_ir_builder_t *b, spl_type_id_t tid, usize val); spl_ir_value_t spl_ir_builder_const_float(spl_type_id_t tid, double val);
spl_ir_node_ref_t spl_ir_builder_type_const_float(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_const_null(spl_type_id_t tid);
double val); spl_ir_value_t spl_ir_builder_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn);
spl_ir_node_ref_t spl_ir_builder_type_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_global_addr(spl_type_id_t ptr_tid, usize global_id);
const void *data, usize len); spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, const void *data,
spl_ir_node_ref_t spl_ir_builder_type_const_char(spl_ir_builder_t *b, spl_type_id_t tid, char val); usize len);
spl_ir_node_ref_t spl_ir_builder_type_const_fn(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_func_ref_t val); usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_type_id_t tid,
spl_ir_value_t init, bool readonly);
/* type */ /* type */
spl_ir_node_ref_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid); spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid); spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid); spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t field_idx); spl_ir_value_t field_idx);
spl_ir_node_ref_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid); spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid);
/* agg */ /* agg */
spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid); spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid);
void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node, void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node,
spl_ir_node_ref_t field); spl_ir_value_t field);
spl_ir_node_ref_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid,
spl_type_id_t field_tid, usize field_idx, spl_type_id_t field_tid, usize field_idx,
spl_ir_node_ref_t val); spl_ir_value_t val);
spl_ir_node_ref_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx,
spl_ir_node_ref_t agg, spl_ir_node_ref_t field); spl_ir_value_t agg, spl_ir_value_t field);
/* control */ /* control */
spl_ir_node_ref_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t cond, spl_ir_node_ref_t true_val, spl_ir_value_t cond, spl_ir_value_t true_val,
spl_ir_node_ref_t false_val); spl_ir_value_t false_val);
spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_node_ref_t cond); spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_value_t cond);
void spl_ir_builder_control_br_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, usize true_block, void spl_ir_builder_control_br_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, usize true_block,
usize false_block); usize false_block);
spl_ir_node_ref_t spl_ir_builder_control_jmp(spl_ir_builder_t *b); spl_ir_node_ref_t spl_ir_builder_control_jmp(spl_ir_builder_t *b);
void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref,
usize target_block); usize target_block);
spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t func); spl_ir_value_t func);
void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node, void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node,
spl_ir_node_ref_t arg); spl_ir_value_t arg);
spl_ir_node_ref_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t idx); spl_ir_value_t idx);
spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t val); spl_ir_value_t val);
spl_ir_node_ref_t spl_ir_builder_control_unreachable(spl_ir_builder_t *b); spl_ir_node_ref_t spl_ir_builder_control_unreachable(spl_ir_builder_t *b);
spl_ir_node_ref_t spl_ir_builder_control_trap(spl_ir_builder_t *b); spl_ir_node_ref_t spl_ir_builder_control_trap(spl_ir_builder_t *b);
+1
View File
@@ -15,6 +15,7 @@ typedef struct {
const spl_type_t *type; const spl_type_t *type;
const spl_attr_t *attr; const spl_attr_t *attr;
spl_extern_idx_map_t extern_native; spl_extern_idx_map_t extern_native;
VEC(isize) global_data_idx;
spl_prog_t prog; spl_prog_t prog;
spl_layout_t *layout; /* 借用 splc0 的 layout(size/align/offset 唯一来源) */ spl_layout_t *layout; /* 借用 splc0 的 layout(size/align/offset 唯一来源) */
+137 -127
View File
@@ -12,62 +12,86 @@ typedef struct {
spl_ir_t *ir; spl_ir_t *ir;
spl_ir_func_ref_t caller; spl_ir_func_ref_t caller;
spl_ir_func_ref_t callee; spl_ir_func_ref_t callee;
spl_ir_node_ref_t shift; /* new = old + shift */ /* callee Ref -> caller Value;Param 直接映射到实参,其余映射到克隆节点。 */
usize nparams; spl_ir_value_t *val_map;
spl_ir_node_ref_t *param_ref; usize val_map_size;
spl_ir_node_ref_t *param_arg;
} inline_ctx_t; } inline_ctx_t;
static spl_ir_node_t *nd(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_ref_t ref) { static spl_ir_node_t *nd(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_ref_t ref) {
return spl_ir_node(ir, fid, ref); return spl_ir_node(ir, fid, ref);
} }
/* ---- 引用重写(克隆体旧 ref -> 新 ref) ---- */ /* ---- 引用重写(克隆体旧 ref -> 新 value) ---- */
static spl_ir_node_ref_t rw(inline_ctx_t *c, spl_ir_node_ref_t r) { static spl_ir_value_t rw_value(inline_ctx_t *c, spl_ir_value_t v) {
if (r == 0) if (v.kind != SPL_IR_VALUE_INST || v.inst == 0)
return 0; return v;
for (usize i = 0; i < c->nparams; i++) if (v.inst < c->val_map_size) {
if (c->param_ref[i] == r) spl_ir_value_t mapped = c->val_map[v.inst];
return c->param_arg[i]; if (mapped.kind != SPL_IR_VALUE_INST || mapped.inst != 0)
return r + c->shift; return mapped;
}
Assert(false && "unmapped callee value ref");
return v;
} }
typedef spl_ir_node_ref_t (*ref_xform_t)(void *ud, spl_ir_node_ref_t ref); static spl_ir_node_ref_t rw_label(inline_ctx_t *c, spl_ir_node_ref_t ref) {
if (ref == 0)
return 0;
return spl_ir_value_inst_ref(rw_value(c, spl_ir_value_inst(ref)));
}
static void xform_vec(spl_ir_node_ref_vec_t *v, ref_xform_t fx, void *ud) { typedef spl_ir_value_t (*value_xform_t)(void *ud, spl_ir_value_t v);
static void xform_value_vec(spl_ir_value_vec_t *v, value_xform_t fx, void *ud) {
for (usize i = 0; i < vec_size(*v); i++) for (usize i = 0; i < vec_size(*v); i++)
vec_at(*v, i) = fx(ud, vec_at(*v, i)); vec_at(*v, i) = fx(ud, vec_at(*v, i));
} }
/* 对节点的所有「值引用」字段应用 fx;块标签字段不在此列(由调用方单独处理)。 */ /* 对节点的所有「值」字段应用 fx;块标签字段由 rw_node 单独处理。 */
static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) { static void xform_node_values(spl_ir_node_t *n, value_xform_t fx, void *ud) {
switch (n->kind) { switch (n->kind) {
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL: case SPL_IR_ARITH_ADD:
case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: case SPL_IR_ARITH_NEG: case SPL_IR_ARITH_SUB:
case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_AND: case SPL_IR_ARITH_OR: case SPL_IR_ARITH_MUL:
case SPL_IR_ARITH_XOR: case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR: case SPL_IR_ARITH_DIV:
case SPL_IR_ARITH_REM:
case SPL_IR_ARITH_NEG:
case SPL_IR_ARITH_ABS:
case SPL_IR_ARITH_AND:
case SPL_IR_ARITH_OR:
case SPL_IR_ARITH_XOR:
case SPL_IR_ARITH_SHL:
case SPL_IR_ARITH_SHR:
case SPL_IR_ARITH_NOT: case SPL_IR_ARITH_NOT:
n->arith.left = fx(ud, n->arith.left); n->arith.left = fx(ud, n->arith.left);
n->arith.right = fx(ud, n->arith.right); n->arith.right = fx(ud, n->arith.right);
break; break;
case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: case SPL_IR_CMP_LT: case SPL_IR_CMP_EQ:
case SPL_IR_CMP_LE: case SPL_IR_CMP_GT: case SPL_IR_CMP_GE: case SPL_IR_CMP_NE:
case SPL_IR_CMP_LT:
case SPL_IR_CMP_LE:
case SPL_IR_CMP_GT:
case SPL_IR_CMP_GE:
n->cmp.a = fx(ud, n->cmp.a); n->cmp.a = fx(ud, n->cmp.a);
n->cmp.b = fx(ud, n->cmp.b); n->cmp.b = fx(ud, n->cmp.b);
break; break;
case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_SEXT: case SPL_IR_CAST_TRUNC:
case SPL_IR_CAST_FEXT: case SPL_IR_CAST_FTRUNC: case SPL_IR_CAST_BITCAST: case SPL_IR_CAST_ZEXT:
case SPL_IR_CAST_PTR2INT: case SPL_IR_CAST_INT2PTR: case SPL_IR_CAST_BOOL2INT: case SPL_IR_CAST_SEXT:
case SPL_IR_CASE_INT2FLOAT: case SPL_IR_CASE_FLOAT2INT: case SPL_IR_CAST_FEXT:
case SPL_IR_CAST_FTRUNC:
case SPL_IR_CAST_BITCAST:
case SPL_IR_CAST_PTR2INT:
case SPL_IR_CAST_INT2PTR:
case SPL_IR_CAST_BOOL2INT:
case SPL_IR_CASE_INT2FLOAT:
case SPL_IR_CASE_FLOAT2INT:
n->cast.val = fx(ud, n->cast.val); n->cast.val = fx(ud, n->cast.val);
break; break;
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
n->mem_alloc.count = fx(ud, n->mem_alloc.count); n->mem_alloc.count = fx(ud, n->mem_alloc.count);
break; break;
case SPL_IR_MEM_GLOBAL_ALLOC:
n->mem_global_alloc.const_node = fx(ud, n->mem_global_alloc.const_node);
break;
case SPL_IR_MEM_LOAD: case SPL_IR_MEM_LOAD:
n->mem_load.ptr = fx(ud, n->mem_load.ptr); n->mem_load.ptr = fx(ud, n->mem_load.ptr);
break; break;
@@ -96,7 +120,7 @@ static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) {
n->ir_offsetof.field_idx = fx(ud, n->ir_offsetof.field_idx); n->ir_offsetof.field_idx = fx(ud, n->ir_offsetof.field_idx);
break; break;
case SPL_IR_AGG_CONSTRUCT: case SPL_IR_AGG_CONSTRUCT:
xform_vec(&n->agg_construct.fields, fx, ud); xform_value_vec(&n->agg_construct.fields, fx, ud);
break; break;
case SPL_IR_AGG_EXTRACT: case SPL_IR_AGG_EXTRACT:
n->agg_extract.val = fx(ud, n->agg_extract.val); n->agg_extract.val = fx(ud, n->agg_extract.val);
@@ -121,27 +145,24 @@ static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) {
break; break;
case SPL_IR_CONTROL_CALL: case SPL_IR_CONTROL_CALL:
n->control_call.func = fx(ud, n->control_call.func); n->control_call.func = fx(ud, n->control_call.func);
xform_vec(&n->control_call.params, fx, ud); xform_value_vec(&n->control_call.params, fx, ud);
break; break;
default: default:
break; break;
} }
} }
static spl_ir_node_ref_t rw_thunk(void *ud, spl_ir_node_ref_t r) { static spl_ir_value_t rw_thunk(void *ud, spl_ir_value_t v) { return rw_value((inline_ctx_t *)ud, v); }
return rw((inline_ctx_t *)ud, r);
}
/* 克隆体旧 ref -> 新 ref;块标签也参与平移。 */
static void rw_node(inline_ctx_t *c, spl_ir_node_t *n) { static void rw_node(inline_ctx_t *c, spl_ir_node_t *n) {
xform_node_value_refs(n, rw_thunk, c); xform_node_values(n, rw_thunk, c);
switch (n->kind) { switch (n->kind) {
case SPL_IR_CONTROL_BR: case SPL_IR_CONTROL_BR:
n->control_br.true_label = rw(c, n->control_br.true_label); n->control_br.true_label = rw_label(c, n->control_br.true_label);
n->control_br.false_label = rw(c, n->control_br.false_label); n->control_br.false_label = rw_label(c, n->control_br.false_label);
break; break;
case SPL_IR_CONTROL_JMP: case SPL_IR_CONTROL_JMP:
n->control_jmp.label = rw(c, n->control_jmp.label); n->control_jmp.label = rw_label(c, n->control_jmp.label);
break; break;
default: default:
break; break;
@@ -166,6 +187,22 @@ static void clone_ref_vec(spl_ir_node_ref_vec_t *dst, const spl_ir_node_ref_vec_
dst->cap = n; dst->cap = n;
} }
static void clone_value_vec(spl_ir_value_vec_t *dst, const spl_ir_value_vec_t *src) {
dst->size = 0;
dst->cap = 0;
dst->data = NULL;
usize n = vec_size(*src);
if (n == 0)
return;
spl_ir_value_t *p = (spl_ir_value_t *)malloc(sizeof(spl_ir_value_t) * n);
if (!p)
return;
memcpy(p, src->data, sizeof(spl_ir_value_t) * n);
dst->data = p;
dst->size = n;
dst->cap = n;
}
static void clone_node(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t dst_ref, static void clone_node(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t dst_ref,
const spl_ir_node_t *src) { const spl_ir_node_t *src) {
spl_ir_node_t *dst = nd(ir, caller, dst_ref); spl_ir_node_t *dst = nd(ir, caller, dst_ref);
@@ -177,18 +214,20 @@ static void clone_node(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
clone_ref_vec((spl_ir_node_ref_vec_t *)&dst->attrs, clone_ref_vec((spl_ir_node_ref_vec_t *)&dst->attrs,
(const spl_ir_node_ref_vec_t *)&src->attrs); (const spl_ir_node_ref_vec_t *)&src->attrs);
if (src->kind == SPL_IR_AGG_CONSTRUCT) if (src->kind == SPL_IR_AGG_CONSTRUCT)
clone_ref_vec(&dst->agg_construct.fields, &src->agg_construct.fields); clone_value_vec(&dst->agg_construct.fields, &src->agg_construct.fields);
else if (src->kind == SPL_IR_CONTROL_CALL) else if (src->kind == SPL_IR_CONTROL_CALL)
clone_ref_vec(&dst->control_call.params, &src->control_call.params); clone_value_vec(&dst->control_call.params, &src->control_call.params);
} }
/* ---- 调用图可达性(防环) ---- */ /* ---- 调用图可达性(防环) ---- */
static spl_ir_func_ref_t call_callee(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_t *call) { static spl_ir_func_ref_t call_callee(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_t *call) {
spl_ir_node_t *fn = nd(ir, fid, call->control_call.func); (void)ir;
if (!fn || fn->kind != SPL_IR_TYPE_CONST) (void)fid;
spl_ir_value_t fn = call->control_call.func;
if (fn.kind != SPL_IR_VALUE_CONST)
return 0; return 0;
return fn->type_const.fn; return fn.fn;
} }
static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target, static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
@@ -234,38 +273,27 @@ static bool reaches_succ(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t
typedef struct { typedef struct {
spl_ir_node_ref_t clone_ref; spl_ir_node_ref_t clone_ref;
spl_ir_node_ref_t val; spl_ir_value_t val;
} ret_rec_t; } ret_rec_t;
static bool param_index(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_t *p, usize *out) { static bool param_index(spl_ir_node_t *p, usize *out) {
spl_ir_node_t *idx = nd(ir, fid, p->control_param.idx); spl_ir_value_t idx = p->control_param.idx;
if (!idx || idx->kind != SPL_IR_TYPE_CONST) if (idx.kind != SPL_IR_VALUE_CONST)
return false; return false;
*out = idx->type_const.int_lit; *out = (usize)idx.int_bits;
return true; return true;
} }
static spl_ir_node_ref_t find_const_src(spl_ir_t *ir, spl_ir_func_ref_t fid) {
spl_ir_func_t *f = spl_ir_func(ir, fid);
if (!f)
return 0;
for (usize i = 1; i < vec_size(f->nodes); i++) {
spl_ir_node_t *x = nd(ir, fid, i);
if (x && x->kind == SPL_IR_TYPE_CONST)
return i;
}
return 0;
}
/* 值引用定点替换:等于 ci 的改为 ret_new。 */
typedef struct { typedef struct {
spl_ir_node_ref_t ci; spl_ir_node_ref_t ci;
spl_ir_node_ref_t ret_new; spl_ir_value_t ret_new;
} replace_one_t; } replace_one_t;
static spl_ir_node_ref_t replace_one(void *ud, spl_ir_node_ref_t r) { static spl_ir_value_t replace_one(void *ud, spl_ir_value_t v) {
replace_one_t *p = ud; replace_one_t *p = ud;
return r == p->ci ? p->ret_new : r; if (v.kind == SPL_IR_VALUE_INST && v.inst == p->ci)
return p->ret_new;
return v;
} }
static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t ci, static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t ci,
@@ -285,7 +313,6 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
usize nargs = vec_size(call->control_call.params); usize nargs = vec_size(call->control_call.params);
/* 统计参数与返回,分配动态映射。 */
usize cnodes = vec_size(calf->nodes); usize cnodes = vec_size(calf->nodes);
usize in_params = 0; usize in_params = 0;
usize in_rets = 0; usize in_rets = 0;
@@ -306,100 +333,85 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
c.ir = ir; c.ir = ir;
c.caller = caller; c.caller = caller;
c.callee = callee; c.callee = callee;
c.nparams = in_params; c.val_map_size = cnodes;
c.param_ref = (spl_ir_node_ref_t *)malloc(sizeof(spl_ir_node_ref_t) * (in_params ? in_params : 1)); c.val_map = (spl_ir_value_t *)calloc(cnodes ? cnodes : 1, sizeof(spl_ir_value_t));
c.param_arg = (spl_ir_node_ref_t *)malloc(sizeof(spl_ir_node_ref_t) * (in_params ? in_params : 1));
ret_rec_t *recs = (ret_rec_t *)malloc(sizeof(ret_rec_t) * (in_rets ? in_rets : 1)); ret_rec_t *recs = (ret_rec_t *)malloc(sizeof(ret_rec_t) * (in_rets ? in_rets : 1));
if (!c.param_ref || !c.param_arg || !recs) { if (!c.val_map || !recs) {
free(c.param_ref); free(c.val_map);
free(c.param_arg);
free(recs); free(recs);
return false; return false;
} }
spl_type_id_t ret_tid = call->control_call.tid; bool ok = true;
/* 参数映射(旧 ref -> 实参 ref)。 */ /* 参数:直接映射到实参 Value。 */
usize pi = 0; for (usize i = 1; i < cnodes && ok; i++) {
for (usize i = 1; i < cnodes; i++) {
spl_ir_node_t *x = nd(ir, callee, i); spl_ir_node_t *x = nd(ir, callee, i);
if (x && x->kind == SPL_IR_CONTROL_PARAM) { if (x && x->kind == SPL_IR_CONTROL_PARAM) {
usize idx = 0; usize idx = 0;
if (!param_index(ir, callee, x, &idx) || idx >= nargs) { if (!param_index(x, &idx) || idx >= nargs) {
free(c.param_ref); ok = false;
free(c.param_arg); break;
}
c.val_map[i] = vec_at(call->control_call.params, idx);
}
}
if (!ok) {
free(c.val_map);
free(recs); free(recs);
return false; return false;
} }
c.param_ref[pi] = i;
c.param_arg[pi] = vec_at(call->control_call.params, idx);
pi++;
}
}
bool has_val = false; bool has_val = false;
for (usize i = 1; i < cnodes; i++) { for (usize i = 1; i < cnodes; i++) {
spl_ir_node_t *x = nd(ir, callee, i); spl_ir_node_t *x = nd(ir, callee, i);
if (x && x->kind == SPL_IR_CONTROL_RET && x->control_ret.val != 0) if (x && x->kind == SPL_IR_CONTROL_RET && !spl_ir_value_is_none(x->control_ret.val))
has_val = true; has_val = true;
} }
spl_ir_node_ref_t const_src = find_const_src(ir, callee);
if (has_val && const_src == 0) {
free(c.param_ref);
free(c.param_arg);
free(recs);
return false;
}
/* 释放调用点原 params(随后 ci 改为 jmp) */ spl_type_id_t ret_tid = call->control_call.tid;
/* 释放调用点原 params(实参已映射进 val_map)。 */
vec_free(call->control_call.params); vec_free(call->control_call.params);
/* 克隆体入口前置:常量 1 + ret 槽 */
spl_ir_node_ref_t body_entry = 0; spl_ir_node_ref_t body_entry = 0;
spl_ir_node_ref_t slot_ref = 0; spl_ir_node_ref_t slot_ref = 0;
if (has_val) { if (has_val) {
spl_ir_node_ref_t c_ref = spl_ir_alloc_node(ir, caller);
clone_node(ir, caller, c_ref, nd(ir, callee, const_src));
spl_ir_node_t *cn = nd(ir, caller, c_ref);
cn->kind = SPL_IR_TYPE_CONST;
cn->type_const.int_lit = 1;
slot_ref = spl_ir_alloc_node(ir, caller); slot_ref = spl_ir_alloc_node(ir, caller);
spl_ir_node_t *sn = nd(ir, caller, slot_ref); spl_ir_node_t *sn = nd(ir, caller, slot_ref);
memset(sn, 0, sizeof(*sn)); memset(sn, 0, sizeof(*sn));
sn->kind = SPL_IR_MEM_ALLOCA; sn->kind = SPL_IR_MEM_ALLOCA;
sn->mem_alloc.tid = ret_tid; sn->mem_alloc.tid = ret_tid;
sn->mem_alloc.count = c_ref; sn->mem_alloc.count = spl_ir_value_const_int(0, 1);
body_entry = slot_ref;
body_entry = c_ref;
} }
c.shift = vec_size(cf->nodes) - 1; /* 第一遍:为非 Param 节点分配目标下标,保证前向块标签可解析。 */
if (!has_val)
body_entry = vec_size(cf->nodes);
/* 克隆体 */
usize nrec = 0;
for (usize i = 1; i < cnodes; i++) { for (usize i = 1; i < cnodes; i++) {
spl_ir_node_t *src = nd(ir, callee, i);
if (!src || src->kind == SPL_IR_CONTROL_PARAM)
continue;
spl_ir_node_ref_t dst_ref = spl_ir_alloc_node(ir, caller); spl_ir_node_ref_t dst_ref = spl_ir_alloc_node(ir, caller);
if (dst_ref == 0) { if (dst_ref == 0) {
free(c.param_ref); free(c.val_map);
free(c.param_arg);
free(recs); free(recs);
return false; return false;
} }
c.val_map[i] = spl_ir_value_inst(dst_ref);
if (body_entry == 0)
body_entry = dst_ref;
}
/* 第二遍:复制内容、重写引用,并把 ret 改成 jmp 占位。 */
usize nrec = 0;
for (usize i = 1; i < cnodes; i++) {
spl_ir_node_t *src = nd(ir, callee, i); spl_ir_node_t *src = nd(ir, callee, i);
if (!src || src->kind == SPL_IR_CONTROL_PARAM)
continue;
spl_ir_node_ref_t dst_ref = spl_ir_value_inst_ref(c.val_map[i]);
clone_node(ir, caller, dst_ref, src); clone_node(ir, caller, dst_ref, src);
rw_node(&c, nd(ir, caller, dst_ref)); rw_node(&c, nd(ir, caller, dst_ref));
spl_ir_node_t *x = nd(ir, caller, dst_ref); spl_ir_node_t *x = nd(ir, caller, dst_ref);
/* 克隆进来的 Param 使用处已替换为实参,节点本身不能留在调用者里
* 改变其参数个数;退化为无害常量。 */
if (src->kind == SPL_IR_CONTROL_PARAM) {
x->kind = SPL_IR_TYPE_CONST;
x->type_const.tid = 0;
x->type_const.int_lit = 0;
continue;
}
if (x->kind == SPL_IR_CONTROL_RET) { if (x->kind == SPL_IR_CONTROL_RET) {
recs[nrec].clone_ref = dst_ref; recs[nrec].clone_ref = dst_ref;
recs[nrec].val = x->control_ret.val; recs[nrec].val = x->control_ret.val;
@@ -409,16 +421,15 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
} }
} }
spl_ir_node_ref_t ret_new = 0; spl_ir_value_t ret_new = spl_ir_value_none();
if (has_val) { if (has_val) {
spl_ir_node_ref_t join = spl_ir_alloc_node(ir, caller); spl_ir_node_ref_t join = spl_ir_alloc_node(ir, caller);
spl_ir_node_t *jn = nd(ir, caller, join); spl_ir_node_t *jn = nd(ir, caller, join);
memset(jn, 0, sizeof(*jn)); memset(jn, 0, sizeof(*jn));
jn->kind = SPL_IR_MEM_LOAD; jn->kind = SPL_IR_MEM_LOAD;
jn->mem_load.tid = ret_tid; jn->mem_load.tid = ret_tid;
jn->mem_load.ptr = slot_ref; jn->mem_load.ptr = spl_ir_value_inst(slot_ref);
/* join 之后跳回调用点的后继(原代码此处靠 fallthrough)。 */
spl_ir_node_ref_t jc = spl_ir_alloc_node(ir, caller); spl_ir_node_ref_t jc = spl_ir_alloc_node(ir, caller);
spl_ir_node_t *jcn = nd(ir, caller, jc); spl_ir_node_t *jcn = nd(ir, caller, jc);
memset(jcn, 0, sizeof(*jcn)); memset(jcn, 0, sizeof(*jcn));
@@ -431,7 +442,7 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
memset(stn, 0, sizeof(*stn)); memset(stn, 0, sizeof(*stn));
stn->kind = SPL_IR_MEM_STORE; stn->kind = SPL_IR_MEM_STORE;
stn->mem_store.tid = ret_tid; stn->mem_store.tid = ret_tid;
stn->mem_store.ptr = slot_ref; stn->mem_store.ptr = spl_ir_value_inst(slot_ref);
stn->mem_store.val = recs[k].val; stn->mem_store.val = recs[k].val;
spl_ir_node_ref_t jr = spl_ir_alloc_node(ir, caller); spl_ir_node_ref_t jr = spl_ir_alloc_node(ir, caller);
@@ -443,7 +454,7 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref); spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref);
rn->control_jmp.label = st; rn->control_jmp.label = st;
} }
ret_new = join; ret_new = spl_ir_value_inst(join);
} else { } else {
for (usize k = 0; k < nrec; k++) { for (usize k = 0; k < nrec; k++) {
spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref); spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref);
@@ -461,11 +472,10 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
for (usize j = 1; j < orig_len; j++) { for (usize j = 1; j < orig_len; j++) {
spl_ir_node_t *x = nd(ir, caller, j); spl_ir_node_t *x = nd(ir, caller, j);
if (x) if (x)
xform_node_value_refs(x, replace_one, &rep); xform_node_values(x, replace_one, &rep);
} }
free(c.param_ref); free(c.val_map);
free(c.param_arg);
free(recs); free(recs);
return true; return true;
} }
+352 -183
View File
@@ -1,4 +1,6 @@
#include "spl_ir.h" #include "spl_ir.h"
#include <stdlib.h>
#include <string.h>
const char *spl_ir_kind_name(spl_ir_kind_t kind) { const char *spl_ir_kind_name(spl_ir_kind_t kind) {
static const char *const names[] = { static const char *const names[] = {
@@ -11,9 +13,29 @@ const char *spl_ir_kind_name(spl_ir_kind_t kind) {
return "?"; return "?";
} }
static usize const_key_hash(spl_ir_const_key_t k) {
usize h = MAP_HASH_INT((usize)k.tid);
for (usize i = 0; i < k.len; ++i)
h = ((h << 5) + h) ^ (usize)k.data[i];
return h;
}
static int const_key_cmp(spl_ir_const_key_t a, spl_ir_const_key_t b) {
if (a.tid != b.tid || a.len != b.len)
return 1;
if (a.len == 0)
return 0;
return memcmp(a.data, b.data, a.len);
}
void spl_ir_init(spl_ir_t *ir) { void spl_ir_init(spl_ir_t *ir) {
vec_init(ir->funcs); vec_init(ir->funcs);
vec_push(ir->funcs, (spl_ir_func_t){0}); vec_push(ir->funcs, (spl_ir_func_t){0});
vec_init(ir->consts);
vec_push(ir->consts, (spl_ir_const_t){0});
vec_init(ir->globals);
vec_push(ir->globals, (spl_ir_global_t){0});
map_init(ir->const_index, const_key_hash, const_key_cmp);
} }
static void node_drop_vecs(spl_ir_node_t *n) { static void node_drop_vecs(spl_ir_node_t *n) {
@@ -43,8 +65,19 @@ void spl_ir_drop(spl_ir_t *ir) {
} }
vec_free(f->attrs); vec_free(f->attrs);
vec_free(f->nodes); vec_free(f->nodes);
free((char *)f->name);
} }
vec_free(ir->funcs); vec_free(ir->funcs);
for (usize i = 1; i < vec_size(ir->consts); ++i) {
spl_ir_const_t *c = &vec_at(ir->consts, i);
/* 当前池里只有字节块;大整数分支尚未产生,避免 union 双重释放。 */
free(c->bytes.data);
}
vec_free(ir->consts);
for (usize i = 1; i < vec_size(ir->globals); ++i)
free((char *)vec_at(ir->globals, i).name);
vec_free(ir->globals);
map_free(ir->const_index);
} }
spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir) { spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir) {
@@ -86,6 +119,125 @@ spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id) {
return &vec_at(ir->funcs, fn_id); return &vec_at(ir->funcs, fn_id);
} }
spl_ir_const_t *spl_ir_const(spl_ir_t *ir, usize const_id) {
if (!ir || const_id == 0 || const_id >= vec_size(ir->consts))
return NULL;
return &vec_at(ir->consts, const_id);
}
spl_ir_node_t *spl_ir_value_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_value_t v) {
if (v.kind != SPL_IR_VALUE_INST || v.inst == 0)
return NULL;
return spl_ir_node(ir, fn_id, v.inst);
}
void spl_ir_node_visit_values(spl_ir_node_t *n, spl_ir_value_cb_t cb, void *ud) {
if (!n || !cb)
return;
switch (n->kind) {
case SPL_IR_ARITH_ADD:
case SPL_IR_ARITH_SUB:
case SPL_IR_ARITH_MUL:
case SPL_IR_ARITH_DIV:
case SPL_IR_ARITH_REM:
case SPL_IR_ARITH_NEG:
case SPL_IR_ARITH_ABS:
case SPL_IR_ARITH_AND:
case SPL_IR_ARITH_OR:
case SPL_IR_ARITH_XOR:
case SPL_IR_ARITH_SHL:
case SPL_IR_ARITH_SHR:
case SPL_IR_ARITH_NOT:
cb(ud, n->arith.left);
cb(ud, n->arith.right);
break;
case SPL_IR_CMP_EQ:
case SPL_IR_CMP_NE:
case SPL_IR_CMP_LT:
case SPL_IR_CMP_LE:
case SPL_IR_CMP_GT:
case SPL_IR_CMP_GE:
cb(ud, n->cmp.a);
cb(ud, n->cmp.b);
break;
case SPL_IR_CAST_TRUNC:
case SPL_IR_CAST_ZEXT:
case SPL_IR_CAST_SEXT:
case SPL_IR_CAST_FEXT:
case SPL_IR_CAST_FTRUNC:
case SPL_IR_CAST_BITCAST:
case SPL_IR_CAST_PTR2INT:
case SPL_IR_CAST_INT2PTR:
case SPL_IR_CAST_BOOL2INT:
case SPL_IR_CASE_INT2FLOAT:
case SPL_IR_CASE_FLOAT2INT:
cb(ud, n->cast.val);
break;
case SPL_IR_MEM_ALLOCA:
cb(ud, n->mem_alloc.count);
break;
case SPL_IR_MEM_LOAD:
cb(ud, n->mem_load.ptr);
break;
case SPL_IR_MEM_STORE:
cb(ud, n->mem_store.ptr);
cb(ud, n->mem_store.val);
break;
case SPL_IR_MEM_OFFSET:
cb(ud, n->mem_offset.ptr);
cb(ud, n->mem_offset.offset);
break;
case SPL_IR_MEM_FIELD_PTR:
cb(ud, n->mem_field_ptr.agg);
break;
case SPL_IR_MEM_COPY:
cb(ud, n->mem_copy.dst);
cb(ud, n->mem_copy.src);
cb(ud, n->mem_copy.size);
break;
case SPL_IR_MEM_SET:
cb(ud, n->mem_set.dst);
cb(ud, n->mem_set.val);
cb(ud, n->mem_set.size);
break;
case SPL_IR_TYPE_OFFSETOF:
cb(ud, n->ir_offsetof.field_idx);
break;
case SPL_IR_AGG_CONSTRUCT:
for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i)
cb(ud, vec_at(n->agg_construct.fields, i));
break;
case SPL_IR_AGG_EXTRACT:
cb(ud, n->agg_extract.val);
break;
case SPL_IR_AGG_INSERT:
cb(ud, n->agg_insert.agg);
cb(ud, n->agg_insert.field);
break;
case SPL_IR_CONTROL_SELECT:
cb(ud, n->control_select.cond);
cb(ud, n->control_select.true_val);
cb(ud, n->control_select.false_val);
break;
case SPL_IR_CONTROL_BR:
cb(ud, n->control_br.cond);
break;
case SPL_IR_CONTROL_PARAM:
cb(ud, n->control_param.idx);
break;
case SPL_IR_CONTROL_RET:
cb(ud, n->control_ret.val);
break;
case SPL_IR_CONTROL_CALL:
cb(ud, n->control_call.func);
for (usize i = 0; i < vec_size(n->control_call.params); ++i)
cb(ud, vec_at(n->control_call.params, i));
break;
default:
break;
}
}
spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func_t *f) { spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func_t *f) {
if (!attr || !f) if (!attr || !f)
return NULL; return NULL;
@@ -111,6 +263,30 @@ static inline void ir_dump_ref(spl_ir_node_ref_t ref) {
} }
} }
static inline void ir_dump_value(const spl_type_t *ty, spl_ir_value_t v) {
if (v.kind == SPL_IR_VALUE_INST) {
ir_dump_ref(v.inst);
return;
}
if (v.kind == SPL_IR_VALUE_GLOBAL) {
printf(" g#%zu", v.global_id);
return;
}
spl_type_node_t *t = spl_type_node((spl_type_t *)ty, v.tid);
if (t && t->kind == SPL_TYPE_FLOAT)
printf(" float=%f", v.float_bits);
else if (t && t->kind == SPL_TYPE_FN)
printf(" fn#%zu", v.fn);
else if (t && t->kind == SPL_TYPE_PTR)
printf(" null");
else if (t && t->kind == SPL_TYPE_INT && t->int_type.bits == 8)
printf(" char='%c'", (char)v.int_bits);
else if (t && (t->kind == SPL_TYPE_ARRAY))
printf(" c#%zu", v.const_id);
else
printf(" int=%llu", (unsigned long long)v.int_bits);
}
static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
switch (n->kind) { switch (n->kind) {
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_ADD:
@@ -124,14 +300,14 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHL:
case SPL_IR_ARITH_SHR: case SPL_IR_ARITH_SHR:
ir_dump_type(ty, n->arith.tid); ir_dump_type(ty, n->arith.tid);
ir_dump_ref(n->arith.left); ir_dump_value(ty, n->arith.left);
ir_dump_ref(n->arith.right); ir_dump_value(ty, n->arith.right);
break; break;
case SPL_IR_ARITH_NEG: case SPL_IR_ARITH_NEG:
case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_ABS:
case SPL_IR_ARITH_NOT: case SPL_IR_ARITH_NOT:
ir_dump_type(ty, n->arith.tid); ir_dump_type(ty, n->arith.tid);
ir_dump_ref(n->arith.left); ir_dump_value(ty, n->arith.left);
break; break;
case SPL_IR_CMP_EQ: case SPL_IR_CMP_EQ:
case SPL_IR_CMP_NE: case SPL_IR_CMP_NE:
@@ -140,8 +316,8 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
case SPL_IR_CMP_GT: case SPL_IR_CMP_GT:
case SPL_IR_CMP_GE: case SPL_IR_CMP_GE:
ir_dump_type(ty, n->cmp.tid); ir_dump_type(ty, n->cmp.tid);
ir_dump_ref(n->cmp.a); ir_dump_value(ty, n->cmp.a);
ir_dump_ref(n->cmp.b); ir_dump_value(ty, n->cmp.b);
break; break;
case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_TRUNC:
case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_ZEXT:
@@ -158,64 +334,43 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
spl_type_pure_dump((spl_type_t *)ty, n->cast.from_tid); spl_type_pure_dump((spl_type_t *)ty, n->cast.from_tid);
printf(" -> "); printf(" -> ");
spl_type_pure_dump((spl_type_t *)ty, n->cast.to_tid); spl_type_pure_dump((spl_type_t *)ty, n->cast.to_tid);
ir_dump_ref(n->cast.val); ir_dump_value(ty, n->cast.val);
break; break;
case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_ALLOCA:
ir_dump_type(ty, n->mem_alloc.tid); ir_dump_type(ty, n->mem_alloc.tid);
ir_dump_ref(n->mem_alloc.count); ir_dump_value(ty, n->mem_alloc.count);
break;
case SPL_IR_MEM_GLOBAL_ALLOC:
ir_dump_type(ty, n->mem_global_alloc.tid);
printf(" g#%zu", n->mem_global_alloc.const_node);
break; break;
case SPL_IR_MEM_LOAD: case SPL_IR_MEM_LOAD:
ir_dump_type(ty, n->mem_load.tid); ir_dump_type(ty, n->mem_load.tid);
ir_dump_ref(n->mem_load.ptr); ir_dump_value(ty, n->mem_load.ptr);
break; break;
case SPL_IR_MEM_STORE: case SPL_IR_MEM_STORE:
ir_dump_type(ty, n->mem_store.tid); ir_dump_type(ty, n->mem_store.tid);
ir_dump_ref(n->mem_store.ptr); ir_dump_value(ty, n->mem_store.ptr);
ir_dump_ref(n->mem_store.val); ir_dump_value(ty, n->mem_store.val);
break; break;
case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_OFFSET:
ir_dump_type(ty, n->mem_offset.tid); ir_dump_type(ty, n->mem_offset.tid);
ir_dump_ref(n->mem_offset.ptr); ir_dump_value(ty, n->mem_offset.ptr);
ir_dump_ref(n->mem_offset.offset); ir_dump_value(ty, n->mem_offset.offset);
break; break;
case SPL_IR_MEM_FIELD_PTR: case SPL_IR_MEM_FIELD_PTR:
ir_dump_type(ty, n->mem_field_ptr.tid); ir_dump_type(ty, n->mem_field_ptr.tid);
ir_dump_ref(n->mem_field_ptr.agg); ir_dump_value(ty, n->mem_field_ptr.agg);
printf(" #%zu", n->mem_field_ptr.field_idx); printf(" #%zu", n->mem_field_ptr.field_idx);
break; break;
case SPL_IR_MEM_COPY: case SPL_IR_MEM_COPY:
ir_dump_type(ty, n->mem_copy.tid); ir_dump_type(ty, n->mem_copy.tid);
ir_dump_ref(n->mem_copy.dst); ir_dump_value(ty, n->mem_copy.dst);
ir_dump_ref(n->mem_copy.src); ir_dump_value(ty, n->mem_copy.src);
ir_dump_ref(n->mem_copy.size); ir_dump_value(ty, n->mem_copy.size);
break; break;
case SPL_IR_MEM_SET: case SPL_IR_MEM_SET:
ir_dump_type(ty, n->mem_set.tid); ir_dump_type(ty, n->mem_set.tid);
ir_dump_ref(n->mem_set.dst); ir_dump_value(ty, n->mem_set.dst);
ir_dump_ref(n->mem_set.val); ir_dump_value(ty, n->mem_set.val);
ir_dump_ref(n->mem_set.size); ir_dump_value(ty, n->mem_set.size);
break; break;
case SPL_IR_TYPE_CONST: {
ir_dump_type(ty, n->type_const.tid);
spl_type_node_t *t = spl_type_node((spl_type_t *)ty, n->type_const.tid);
if (t && t->kind == SPL_TYPE_FLOAT) {
printf(" float=%f", n->type_const.float_lit);
} else if (t && t->kind == SPL_TYPE_FN) {
printf(" fn#%zu", n->type_const.fn);
} else if (t && t->kind == SPL_TYPE_ARRAY) {
/* 字节常量:不打印宿主指针(不确定),只标记 bytes。 */
printf(" bytes");
} else if (t && t->kind == SPL_TYPE_INT && t->int_type.bits == 8) {
printf(" char='%c'", n->type_const.ch_lit);
} else {
printf(" int=%zu", n->type_const.int_lit);
}
break;
}
case SPL_IR_TYPE_BITSIZEOF: case SPL_IR_TYPE_BITSIZEOF:
ir_dump_type(ty, n->bitsizeof.tid); ir_dump_type(ty, n->bitsizeof.tid);
break; break;
@@ -227,34 +382,34 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
break; break;
case SPL_IR_TYPE_OFFSETOF: case SPL_IR_TYPE_OFFSETOF:
ir_dump_type(ty, n->ir_offsetof.tid); ir_dump_type(ty, n->ir_offsetof.tid);
ir_dump_ref(n->ir_offsetof.field_idx); ir_dump_value(ty, n->ir_offsetof.field_idx);
break; break;
case SPL_IR_TYPE_FIELD_COUNT: case SPL_IR_TYPE_FIELD_COUNT:
ir_dump_type(ty, n->field_count.tid); ir_dump_type(ty, n->field_count.tid);
break; break;
case SPL_IR_AGG_CONSTRUCT: case SPL_IR_AGG_CONSTRUCT:
ir_dump_type(ty, n->agg_construct.tid); ir_dump_type(ty, n->agg_construct.tid);
vec_for(n->agg_construct.fields, i) ir_dump_ref(vec_at(n->agg_construct.fields, i)); vec_for(n->agg_construct.fields, i) ir_dump_value(ty, vec_at(n->agg_construct.fields, i));
break; break;
case SPL_IR_AGG_EXTRACT: case SPL_IR_AGG_EXTRACT:
ir_dump_type(ty, n->agg_extract.tid); ir_dump_type(ty, n->agg_extract.tid);
printf(" #%zu", n->agg_extract.field_idx); printf(" #%zu", n->agg_extract.field_idx);
ir_dump_ref(n->agg_extract.val); ir_dump_value(ty, n->agg_extract.val);
break; break;
case SPL_IR_AGG_INSERT: case SPL_IR_AGG_INSERT:
ir_dump_type(ty, n->agg_insert.tid); ir_dump_type(ty, n->agg_insert.tid);
printf(" #%zu", n->agg_insert.field_idx); printf(" #%zu", n->agg_insert.field_idx);
ir_dump_ref(n->agg_insert.agg); ir_dump_value(ty, n->agg_insert.agg);
ir_dump_ref(n->agg_insert.field); ir_dump_value(ty, n->agg_insert.field);
break; break;
case SPL_IR_CONTROL_SELECT: case SPL_IR_CONTROL_SELECT:
ir_dump_type(ty, n->control_select.tid); ir_dump_type(ty, n->control_select.tid);
ir_dump_ref(n->control_select.cond); ir_dump_value(ty, n->control_select.cond);
ir_dump_ref(n->control_select.true_val); ir_dump_value(ty, n->control_select.true_val);
ir_dump_ref(n->control_select.false_val); ir_dump_value(ty, n->control_select.false_val);
break; break;
case SPL_IR_CONTROL_BR: case SPL_IR_CONTROL_BR:
ir_dump_ref(n->control_br.cond); ir_dump_value(ty, n->control_br.cond);
ir_dump_ref(n->control_br.true_label); ir_dump_ref(n->control_br.true_label);
ir_dump_ref(n->control_br.false_label); ir_dump_ref(n->control_br.false_label);
break; break;
@@ -263,16 +418,16 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
break; break;
case SPL_IR_CONTROL_CALL: case SPL_IR_CONTROL_CALL:
ir_dump_type(ty, n->control_call.tid); ir_dump_type(ty, n->control_call.tid);
ir_dump_ref(n->control_call.func); ir_dump_value(ty, n->control_call.func);
vec_for(n->control_call.params, i) ir_dump_ref(vec_at(n->control_call.params, i)); vec_for(n->control_call.params, i) ir_dump_value(ty, vec_at(n->control_call.params, i));
break; break;
case SPL_IR_CONTROL_PARAM: case SPL_IR_CONTROL_PARAM:
ir_dump_type(ty, n->control_param.tid); ir_dump_type(ty, n->control_param.tid);
ir_dump_ref(n->control_param.idx); ir_dump_value(ty, n->control_param.idx);
break; break;
case SPL_IR_CONTROL_RET: case SPL_IR_CONTROL_RET:
ir_dump_type(ty, n->control_ret.tid); ir_dump_type(ty, n->control_ret.tid);
ir_dump_ref(n->control_ret.val); ir_dump_value(ty, n->control_ret.val);
break; break;
case SPL_IR_CONTROL_UNREACHABLE: case SPL_IR_CONTROL_UNREACHABLE:
case SPL_IR_CONTROL_TRAP: case SPL_IR_CONTROL_TRAP:
@@ -297,6 +452,27 @@ static void ir_dump_attr_list(const spl_attr_t *attr, spl_attr_id_vec_t *ids) {
} }
void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr) { void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr) {
for (usize i = 1; i < vec_size(ir->consts); ++i) {
spl_ir_const_t *c = &vec_at(ir->consts, i);
printf("const c#%zu ", i);
spl_type_pure_dump((spl_type_t *)ty, c->tid);
spl_type_node_t *tn = spl_type_node((spl_type_t *)ty, c->tid);
if (tn && tn->kind == SPL_TYPE_ARRAY)
printf(" bytes=%zu", c->bytes.len);
printf("\n");
}
for (usize i = 1; i < vec_size(ir->globals); ++i) {
spl_ir_global_t *g = &vec_at(ir->globals, i);
printf("global g#%zu %s ", i, g->readonly ? "const" : "var");
spl_type_pure_dump((spl_type_t *)ty, g->tid);
if (g->name)
printf(" %s", g->name);
if (!spl_ir_value_is_none(g->init)) {
printf(" =");
ir_dump_value(ty, g->init);
}
printf("\n");
}
vec_for(ir->funcs, i) { vec_for(ir->funcs, i) {
if (i == 0) if (i == 0)
continue; continue;
@@ -367,7 +543,7 @@ spl_ir_func_ref_t spl_ir_builder_fn_begin(spl_ir_builder_t *b, const char *name,
} }
spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir); spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir);
spl_ir_func_t *f = spl_ir_func(&b->ir, fid); spl_ir_func_t *f = spl_ir_func(&b->ir, fid);
f->name = name; f->name = name ? strdup(name) : NULL;
vec_init(f->attrs); vec_init(f->attrs);
f->fn_tid = fn_tid; f->fn_tid = fn_tid;
@@ -383,7 +559,7 @@ spl_ir_func_ref_t spl_ir_builder_fn_reserve(spl_ir_builder_t *b, const char *nam
spl_type_id_t fn_tid) { spl_type_id_t fn_tid) {
spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir); spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir);
spl_ir_func_t *f = spl_ir_func(&b->ir, fid); spl_ir_func_t *f = spl_ir_func(&b->ir, fid);
f->name = name; f->name = name ? strdup(name) : NULL;
vec_init(f->attrs); vec_init(f->attrs);
f->fn_tid = fn_tid; f->fn_tid = fn_tid;
return fid; return fid;
@@ -458,86 +634,141 @@ void spl_ir_builder_block_end(spl_ir_builder_t *b) {
} }
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
spl_ir_node_ref_t x_left, spl_ir_node_ref_t x_right) { \ spl_ir_value_t x_left, spl_ir_value_t x_right) { \
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
n->arith.tid = x_tid; \ n->arith.tid = x_tid; \
n->arith.left = x_left; \ n->arith.left = x_left; \
n->arith.right = x_right; \ n->arith.right = x_right; \
return ref; \ return spl_ir_value_inst(ref); \
} }
SPL_IR_BIN_ARITH_TABLE SPL_IR_BIN_ARITH_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
spl_ir_node_ref_t x_val) { \ spl_ir_value_t x_val) { \
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
n->arith.tid = x_tid; \ n->arith.tid = x_tid; \
n->arith.left = x_val; \ n->arith.left = x_val; \
n->arith.right = 0; \ n->arith.right = spl_ir_value_none(); \
return ref; \ return spl_ir_value_inst(ref); \
} }
SPL_IR_UN_ARITH_TABLE SPL_IR_UN_ARITH_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
spl_ir_node_ref_t x_a, spl_ir_node_ref_t x_b) { \ spl_ir_value_t x_a, spl_ir_value_t x_b) { \
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
n->cmp.tid = x_tid; \ n->cmp.tid = x_tid; \
n->cmp.a = x_a; \ n->cmp.a = x_a; \
n->cmp.b = x_b; \ n->cmp.b = x_b; \
return ref; \ return spl_ir_value_inst(ref); \
} }
SPL_IR_CMP_TABLE SPL_IR_CMP_TABLE
#undef X #undef X
#define X(name, kind) \ #define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_from, \ spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_from, \
spl_type_id_t x_to, spl_ir_node_ref_t x_val) { \ spl_type_id_t x_to, spl_ir_value_t x_val) { \
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
n->cast.from_tid = x_from; \ n->cast.from_tid = x_from; \
n->cast.to_tid = x_to; \ n->cast.to_tid = x_to; \
n->cast.val = x_val; \ n->cast.val = x_val; \
return ref; \ return spl_ir_value_inst(ref); \
} }
SPL_IR_CAST_TABLE SPL_IR_CAST_TABLE
#undef X #undef X
spl_ir_node_ref_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t count) { spl_ir_value_t count) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_ALLOCA); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_ALLOCA);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_alloc.tid = tid; n->mem_alloc.tid = tid;
n->mem_alloc.count = count; n->mem_alloc.count = count;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_mem_global_alloc(spl_ir_builder_t *b, spl_type_id_t tid, static usize const_pool_add(spl_ir_t *ir, spl_type_id_t tid, const void *data, usize len) {
spl_ir_node_ref_t const_node) { spl_ir_const_key_t key = {.tid = tid, .data = data, .len = len};
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_GLOBAL_ALLOC); usize id = 0;
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); if (map_get(ir->const_index, key, &id))
n->mem_global_alloc.tid = tid; return id;
n->mem_global_alloc.const_node = const_node;
return ref; spl_ir_const_t entry = {0};
entry.tid = tid;
entry.bytes.len = len;
entry.bytes.data = malloc(len ? len : 1);
if (!entry.bytes.data)
return 0;
if (len)
memcpy(entry.bytes.data, data, len);
vec_push(ir->consts, entry);
id = vec_size(ir->consts) - 1;
/* 去重键必须指向池内稳定副本,调用方的指针可能马上失效。 */
key.data = entry.bytes.data;
map_put(ir->const_index, key, id);
return id;
} }
spl_ir_node_ref_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, const void *data,
spl_ir_node_ref_t ptr) { usize len) {
usize id = const_pool_add(&b->ir, tid, data, len);
return spl_ir_value_const_pool(tid, id);
}
spl_ir_value_t spl_ir_builder_const_int(spl_type_id_t tid, uint64_t bits) {
return spl_ir_value_const_int(tid, bits);
}
spl_ir_value_t spl_ir_builder_const_bool(spl_type_id_t tid, bool val) {
return spl_ir_value_const_int(tid, val ? 1 : 0);
}
spl_ir_value_t spl_ir_builder_const_float(spl_type_id_t tid, double val) {
return spl_ir_value_const_float(tid, val);
}
spl_ir_value_t spl_ir_builder_const_null(spl_type_id_t tid) {
return spl_ir_value_const_null(tid);
}
spl_ir_value_t spl_ir_builder_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn) {
return spl_ir_value_const_fn(tid, fn);
}
spl_ir_value_t spl_ir_builder_global_addr(spl_type_id_t ptr_tid, usize global_id) {
return spl_ir_value_global(ptr_tid, global_id);
}
usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_type_id_t tid,
spl_ir_value_t init, bool readonly) {
spl_ir_global_t g = {
.name = name ? strdup(name) : NULL,
.tid = tid,
.init = init,
.readonly = readonly,
};
vec_push(b->ir.globals, g);
return vec_size(b->ir.globals) - 1;
}
spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_LOAD); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_LOAD);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_load.tid = tid; n->mem_load.tid = tid;
n->mem_load.ptr = ptr; n->mem_load.ptr = ptr;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t val) { spl_ir_value_t ptr, spl_ir_value_t val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_STORE); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_STORE);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_store.tid = tid; n->mem_store.tid = tid;
@@ -546,29 +777,28 @@ spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t ti
return ref; return ref;
} }
spl_ir_node_ref_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t offset) { spl_ir_value_t offset) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_OFFSET); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_OFFSET);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_offset.tid = tid; n->mem_offset.tid = tid;
n->mem_offset.ptr = ptr; n->mem_offset.ptr = ptr;
n->mem_offset.offset = offset; n->mem_offset.offset = offset;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t agg,
spl_ir_node_ref_t agg, usize field_idx) { usize field_idx) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_FIELD_PTR); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_FIELD_PTR);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_field_ptr.tid = tid; n->mem_field_ptr.tid = tid;
n->mem_field_ptr.agg = agg; n->mem_field_ptr.agg = agg;
n->mem_field_ptr.field_idx = field_idx; n->mem_field_ptr.field_idx = field_idx;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
spl_ir_node_ref_t dst, spl_ir_node_ref_t src, spl_ir_value_t src, spl_ir_value_t size) {
spl_ir_node_ref_t size) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_COPY); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_COPY);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_copy.tid = tid; n->mem_copy.tid = tid;
@@ -578,9 +808,8 @@ spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid
return ref; return ref;
} }
spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
spl_ir_node_ref_t dst, spl_ir_node_ref_t val, spl_ir_value_t val, spl_ir_value_t size) {
spl_ir_node_ref_t size) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_SET); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_SET);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->mem_set.tid = tid; n->mem_set.tid = tid;
@@ -590,101 +819,41 @@ spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid,
return ref; return ref;
} }
spl_ir_node_ref_t spl_ir_builder_type_const_bool(spl_ir_builder_t *b, spl_type_id_t tid, bool val) { spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.bool_lit = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_ptr(spl_ir_builder_t *b, spl_type_id_t tid, void *val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.ptr_lit = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_int(spl_ir_builder_t *b, spl_type_id_t tid, usize val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.int_lit = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_float(spl_ir_builder_t *b, spl_type_id_t tid,
double val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.float_lit = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid,
const void *data, usize len) {
(void)len;
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.ptr_lit = (void *)data;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_char(spl_ir_builder_t *b, spl_type_id_t tid, char val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.ch_lit = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_const_fn(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_func_ref_t val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->type_const.tid = tid;
n->type_const.fn = val;
return ref;
}
spl_ir_node_ref_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_BITSIZEOF); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_BITSIZEOF);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->bitsizeof.tid = tid; n->bitsizeof.tid = tid;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_SIZEOF); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_SIZEOF);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->ir_sizeof.tid = tid; n->ir_sizeof.tid = tid;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_ALIGNOF); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_ALIGNOF);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->ir_alignof.tid = tid; n->ir_alignof.tid = tid;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t field_idx) { spl_ir_value_t field_idx) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_OFFSETOF); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_OFFSETOF);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->ir_offsetof.tid = tid; n->ir_offsetof.tid = tid;
n->ir_offsetof.field_idx = field_idx; n->ir_offsetof.field_idx = field_idx;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_FIELD_COUNT); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_FIELD_COUNT);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->field_count.tid = tid; n->field_count.tid = tid;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid) {
@@ -696,48 +865,48 @@ spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_
} }
void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node, void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node,
spl_ir_node_ref_t field) { spl_ir_value_t field) {
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, node); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, node);
Assert(n != NULL); Assert(n != NULL);
vec_push(n->agg_construct.fields, field); vec_push(n->agg_construct.fields, field);
} }
spl_ir_node_ref_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid,
spl_type_id_t field_tid, usize field_idx, spl_type_id_t field_tid, usize field_idx,
spl_ir_node_ref_t val) { spl_ir_value_t val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_EXTRACT); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_EXTRACT);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->agg_extract.tid = tid; n->agg_extract.tid = tid;
n->agg_extract.field_tid = field_tid; n->agg_extract.field_tid = field_tid;
n->agg_extract.field_idx = field_idx; n->agg_extract.field_idx = field_idx;
n->agg_extract.val = val; n->agg_extract.val = val;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx,
spl_ir_node_ref_t agg, spl_ir_node_ref_t field) { spl_ir_value_t agg, spl_ir_value_t field) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_INSERT); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_INSERT);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->agg_insert.tid = tid; n->agg_insert.tid = tid;
n->agg_insert.field_idx = field_idx; n->agg_insert.field_idx = field_idx;
n->agg_insert.agg = agg; n->agg_insert.agg = agg;
n->agg_insert.field = field; n->agg_insert.field = field;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t cond, spl_ir_node_ref_t true_val, spl_ir_value_t cond, spl_ir_value_t true_val,
spl_ir_node_ref_t false_val) { spl_ir_value_t false_val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_SELECT); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_SELECT);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->control_select.tid = tid; n->control_select.tid = tid;
n->control_select.cond = cond; n->control_select.cond = cond;
n->control_select.true_val = true_val; n->control_select.true_val = true_val;
n->control_select.false_val = false_val; n->control_select.false_val = false_val;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_node_ref_t cond) { spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_value_t cond) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_BR); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_BR);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->control_br.cond = cond; n->control_br.cond = cond;
@@ -778,7 +947,7 @@ void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref
} }
spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t func) { spl_ir_value_t func) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_CALL); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_CALL);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->control_call.tid = tid; n->control_call.tid = tid;
@@ -788,23 +957,23 @@ spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t
} }
void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node, void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node,
spl_ir_node_ref_t arg) { spl_ir_value_t arg) {
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, call_node); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, call_node);
Assert(n != NULL); Assert(n != NULL);
vec_push(n->control_call.params, arg); vec_push(n->control_call.params, arg);
} }
spl_ir_node_ref_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t idx) { spl_ir_value_t idx) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_PARAM); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_PARAM);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->control_param.tid = tid; n->control_param.tid = tid;
n->control_param.idx = idx; n->control_param.idx = idx;
return ref; return spl_ir_value_inst(ref);
} }
spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t val) { spl_ir_value_t val) {
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_RET); spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_RET);
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
n->control_ret.tid = tid; n->control_ret.tid = tid;
+556 -408
View File
File diff suppressed because it is too large. Load diff
+5 -1
View File
@@ -969,7 +969,11 @@ static spl_def_id_t sema_build_inline_prefixes(spl_sema_t *sema, const char *pat
sema->ast, 0); sema->ast, 0);
} }
spl_def_id_t prefix_def = node->owner_id; spl_def_id_t prefix_def = node->owner_id;
sema_ns_aggregate_add_method(sema, prefix_def, segs.data[i], child_def); /* segs 指向临时 buf,方法名必须引用生命周期更长的命名空间名。 */
spl_ns_id_t child_ns = spl_ns_child(sema->ns, prefix_ns, segs.data[i]);
const char *method_name =
child_ns ? vec_at(sema->ns->nodes, child_ns).name : segs.data[i];
sema_ns_aggregate_add_method(sema, prefix_def, method_name, child_def);
child_def = prefix_def; child_def = prefix_def;
} }