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

+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)