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
+1657 -1110

No files matched your search

+232 -169
View File
@@ -8,16 +8,27 @@
聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。
类型/布局全部走 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 {
const spl_ir_t *ir;
const spl_type_t *type;
spl_layout_t *layout;
const spl_attr_t *attr;
spl_ir2c_buf_t *out;
usize gdata_n; /* 每个 mem.global_alloc 生成一块静态数据,从 1 开始编号 */
cconst_vec_t cconsts; /* 当前函数的 CONST 操作数槽 */
bool failed;
} 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) {
if (n == 0)
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_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_u(ir2c_t *c, usize v) {
static void put_u_raw(ir2c_t *c, usize v) {
char buf[32];
int n = snprintf(buf, sizeof buf, "%llu", (unsigned long long)v);
if (n > 0)
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) {
char buf[32];
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);
}
/* 读取 type.const 的整数值;按 tid 决定落在哪个 union 成员(同 ir2vm 约定)。 */
static usize const_node(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) {
if (ref == 0 || ref >= vec_size(f->nodes))
return 0;
const spl_ir_node_t *n = &vec_at(f->nodes, ref);
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;
}
/* 常量操作数按 (tid, 位模式) 共享一个 v<slot> 变量。 */
static uint64_t value_bits(spl_ir_value_t v) {
uint64_t bits = 0;
memcpy(&bits, &v.int_bits, sizeof bits);
return bits;
}
/* 函数常量节点(Fn lit)持有的函数 ref;非函数常量返回 0。 */
static spl_ir_func_ref_t const_fn_ref(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) {
if (ref == 0 || ref >= vec_size(f->nodes))
return 0;
const spl_ir_node_t *n = &vec_at(f->nodes, ref);
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 || te->kind != SPL_TYPE_FN)
return 0;
return n->type_const.fn;
static usize const_slot_lookup(ir2c_t *c, spl_ir_value_t v) {
uint64_t bits = value_bits(v);
for (usize i = 0; i < vec_size(c->cconsts); ++i) {
cconst_t *e = &vec_at(c->cconsts, i);
if (e->kind == v.kind && e->tid == v.tid && e->bits == bits)
return e->slot;
}
cconst_t e = {.kind = v.kind, .tid = v.tid, .bits = bits, .slot = vec_size(c->cconsts)};
vec_push(c->cconsts, e);
return e.slot;
}
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) {
@@ -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;
case SPL_IR_MEM_ALLOCA:
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:
return n->mem_load.tid;
case SPL_IR_MEM_OFFSET:
return make_ptr(c, n->mem_offset.tid);
case SPL_IR_MEM_FIELD_PTR:
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_SIZEOF:
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) {
put(c, "v");
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);
spl_type_id_t agg_t = n->agg_construct.tid;
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);
if (!ftid)
continue;
@@ -623,68 +642,8 @@ static void emit_cast(ir2c_t *c, usize ref, const spl_ir_node_t *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) {
(void)f;
switch (n->kind) {
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;
}
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:
break;
}
}
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;
switch (n->kind) {
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;
case SPL_IR_TYPE_OFFSETOF: {
/* 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);
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) {
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);
if (callee != 0) {
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 值的类型构造函数指针类型 */
spl_type_id_t func_tid = 0;
if (n->control_call.func < vec_size(f->nodes))
func_tid = result_tid(c, &vec_at(f->nodes, n->control_call.func));
spl_type_id_t func_tid = value_type(c, f, n->control_call.func);
spl_type_node_t *ftnode = spl_type_node(c->type, func_tid);
if (!ftnode || ftnode->kind != SPL_TYPE_FN) {
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) {
switch (n->kind) {
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)) {
put(c, "v");
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:
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);
put(c, "memcpy(ret__, v");
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,
spl_type_id_t ret_tid) {
switch (n->kind) {
case SPL_IR_TYPE_CONST:
emit_const(c, ref, n);
break;
case SPL_IR_ARITH_ADD:
case SPL_IR_ARITH_SUB:
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_OFFSET:
case SPL_IR_MEM_FIELD_PTR:
case SPL_IR_MEM_GLOBAL_ALLOC:
emit_mem(c, f, ref, n);
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) {
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) {
const spl_ir_node_t *n = &vec_at(f->nodes, i);
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)
count = 1;
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))
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) {
@@ -1222,6 +1221,13 @@ static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) {
if (need)
put(c, ", ");
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))
put(c, "void*");
else if (!put_type(c, pt))
@@ -1267,14 +1273,63 @@ static spl_ir_func_ref_t find_main(ir2c_t *c) {
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) {
put(c, "// AUTO-GENERATED\n");
put(c, "#include \"spl_rt.h\"\n");
emit_globals(c);
usize nf = vec_size(c->ir->funcs);
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);
if (f && !fn_is_extern(c, f))
if (f)
emit_proto(c, 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)
nargs = vec_size(te->fn_type.params);
if (nargs == 0) {
put(c, "int main(void) { f");
put_u(c, m);
put(c, "(); return 0; }\n");
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(c, "(); return 0; }\n");
}
} else {
put(c, "int main(int argc, char** argv) { ");
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,
.attr = attr,
.out = out,
.gdata_n = 1,
.failed = false,
};
vec_init(c.cconsts);
run(&c);
vec_free(c.cconsts);
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);
* 2) CALL 推导实参的 LOAD/STORE kind。
* mem.offset/mem.field 的 .tid 存的是 pointee/aggregate,须转成指针类型。
* cmp/type.* 这里刻意返回操作数/被测量类型的 tid 而非 bool/usize:
* cmp/type.* 这里刻意返回操作数被测量类型的 tid 而非 bool/usize;
* CALL 实参 kind 依赖此点,改动会改变生成字节码,diff 测试据此守门。 */
static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
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;
case SPL_IR_MEM_ALLOCA:
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:
return n->mem_load.tid;
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);
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:
return n->bitsizeof.tid;
case SPL_IR_TYPE_SIZEOF:
@@ -226,16 +222,28 @@ typedef struct {
/* panic 数据区:fmt slice + msg slice,各 2 words(ptr + len)。 */
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)。 */
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) {
case SPL_IR_MEM_ALLOCA: {
usize count = 1;
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;
}
usize count = value_int(node->mem_alloc.count);
if (count == 0)
count = 1;
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)
vec_push(fl->slots, (slot_t){0});
/* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽 */
/* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽。 */
usize nargs = fn_nargs(ctx, f->fn_tid);
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);
@@ -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_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) {
CG_LADDR(cg, vec_at(cg->frame.slots, ref).result_off);
static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k);
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);
}
@@ -364,6 +378,7 @@ typedef struct {
bool indirect;
usize off;
usize base;
int gdata; /* >0:全局数据区索引,直接 GADDR */
} data_loc_t;
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) {
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) {
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) {
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_LOAD(cg, SPL_PTR);
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) {
usize sz = ir_type_size(cg->ctx, tid);
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);
}
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_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)
return;
if (tn->kind == SPL_TYPE_FN) {
isize nat_idx = -1;
if (n->type_const.fn > 0)
map_get(ctx->extern_native, n->type_const.fn, &nat_idx);
if (v.fn > 0)
map_get(ctx->extern_native, v.fn, &nat_idx);
if (nat_idx >= 0) {
CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx);
} else {
usize idx = ip_of(cg);
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;
}
uint8_t k = vm_kind_of(ctx, tid);
spl_vm_val_t imm = 0;
switch (tn->kind) {
case SPL_TYPE_BOOL:
imm = n->type_const.bool_lit ? 1 : 0;
imm = v.int_bits ? 1 : 0;
break;
case SPL_TYPE_INT:
if (tn->int_type.bits == 8)
imm = (spl_vm_val_t)(unsigned char)n->type_const.ch_lit;
else
imm = (spl_vm_val_t)n->type_const.int_lit;
imm = (spl_vm_val_t)v.int_bits;
break;
case SPL_TYPE_FLOAT:
memcpy(&imm, &n->type_const.float_lit, sizeof(double));
memcpy(&imm, &v.float_bits, sizeof(double));
break;
case SPL_TYPE_PTR:
imm = (spl_vm_val_t)(usize)n->type_const.ptr_lit;
imm = 0;
break;
case SPL_TYPE_UNIT:
return;
default:
break;
}
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) {
@@ -528,26 +561,11 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
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: {
spl_type_id_t tid = n->mem_load.tid;
if (ir_is_aggregate(ctx, tid)) {
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);
CG_LADDR(cg, data);
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: {
spl_type_id_t tid = n->mem_store.tid;
if (ir_is_aggregate(ctx, tid)) {
cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->mem_store.val).result_off),
ptr_slot(vec_at(cg->frame.slots, n->mem_store.ptr).result_off), tid);
cg_copy_agg(cg, value_ptr_loc(cg, n->mem_store.val, 0),
value_ptr_loc(cg, n->mem_store.ptr, 0), tid);
} else {
uint8_t k = vm_kind_of(ctx, tid);
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;
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);
if (!ftid)
continue;
usize foff = ir_agg_field_off(ctx, agg_t, i);
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);
} else {
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)) {
usize data = vec_at(cg->frame.slots, ref).data_off;
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);
CG_LADDR(cg, data);
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;
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);
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);
if (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);
} else {
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);
break;
case SPL_IR_TYPE_OFFSETOF: {
usize idx = 0;
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;
}
usize idx = value_int(n->ir_offsetof.field_idx);
v = ir_agg_field_off(ctx, n->ir_offsetof.tid, idx);
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: {
uint8_t k = vm_kind_of(ctx, n->control_param.tid);
usize idx = 0;
if (n->control_param.idx < vec_size(cg->irf->nodes)) {
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 idx = value_int(n->control_param.idx);
/* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起。 */
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_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: {
spl_type_id_t ret_tid = n->control_ret.tid;
if (ir_is_aggregate(ctx, ret_tid)) {
/* sret:把返回值写入 *(fp+0),然后以 void 返回 */
if (n->control_ret.val) {
/* sret:把返回值写入 *(fp+0),然后以 void 返回。 */
if (!spl_ir_value_is_none(n->control_ret.val)) {
usize sz = ir_type_size(ctx, ret_tid);
CG_LADDR(cg, 0);
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;
}
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);
emit(cg, SPL_RET, k, 0);
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: {
spl_ir_func_ref_t callee_ref = 0;
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;
}
spl_ir_func_ref_t callee_ref = value_fn(n->control_call.func);
isize nat_idx = -1;
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) {
for (usize i = 0; i < nargs; ++i) {
spl_ir_node_ref_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, ir_result_tid(ctx, pn));
spl_ir_value_t pref = vec_at(n->control_call.params, i);
uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref));
cg_load_slot(cg, pref, pk);
}
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);
}
for (usize i = 0; i < nargs; ++i) {
spl_ir_node_ref_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, ir_result_tid(ctx, pn));
spl_ir_value_t pref = vec_at(n->control_call.params, i);
uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref));
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);
emit(cg, SPL_CALL, SPL_VOID, actual_nargs);
if (ret_is_sret) {
/* result slot 保存 sret 指针 */
/* result slot 保存 sret 指针。 */
CG_LADDR(cg, sret_off);
cg_store_slot(cg, ref, SPL_PTR);
} 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) {
switch (n->kind) {
case SPL_IR_TYPE_CONST:
cg_const(cg, ref, n);
break;
case SPL_IR_ARITH_ADD:
cg_binop(cg, ref, n, SPL_ADD);
break;
@@ -970,7 +968,6 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break;
case SPL_IR_MEM_ALLOCA:
case SPL_IR_MEM_GLOBAL_ALLOC:
case SPL_IR_MEM_LOAD:
case SPL_IR_MEM_STORE:
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)
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_store_at(cg, data + 0, SPL_PTR);
CG_PUSH(cg, SPL_U64, 2);
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_store_at(cg, data + 16, SPL_PTR);
CG_PUSH(cg, SPL_U64, n->dbg_panic.len);
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 + 16);
@@ -1040,7 +1037,7 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break;
default:
/* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */
/* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */
Panic("ir2vm: unsupported IR node kind %d", (int)n->kind);
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 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,
spl_layout_t *layout, const spl_attr_t *attr) {
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->layout = layout;
map_init(ctx->extern_native, spl_map_hash, spl_map_cmp);
vec_init(ctx->global_data_idx);
spl_prog_init(&ctx->prog);
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) {
spl_prog_drop(&ctx->prog);
vec_free(ctx->global_data_idx);
map_free(ctx->extern_native);
}
@@ -1145,12 +1178,13 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) {
return 0;
register_externs(ctx);
prepare_globals(ctx);
/* 保留 insn 0 作为哨兵:确保真实函数入口地址非零,
从而 0 可唯一表示 null 函数指针(函数指针与 null 比较)。 */
/* 保留 insn 0 作为哨兵:确认真实函数入口地址非零,
从而使 0 唯一表示 null 函数指针(函数指针与 null 比较)。 */
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));
fix_vec_t all_fixups;
vec_init(all_fixups);
@@ -1161,8 +1195,8 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) {
continue;
fid_ip[i] = translate_function(ctx, i, &all_fixups);
}
/* FNADDR 回填 */
/* FNADDR 回填。 */
/* FNADDR 鍥炲~ */
for (usize i = 0; i < vec_size(all_fixups); ++i) {
fix_t *fx = &vec_at(all_fixups, i);
if (fx->kind != FIX_FNADDR)