stage1 重写进行代码设计并重构 提供diag替换老旧错误处理
This commit is contained in:
1 parent
7cbe2d2b9a
commit
e4347cc3ea
14 files changed
+983
-526
No files matched your search
+23
-9
@@ -52,6 +52,23 @@ static bool ir_is_aggregate(spl_ir2vm_t *ctx, spl_type_id_t tid) {
|
||||
tn->kind == SPL_TYPE_UNION || tn->kind == SPL_TYPE_ENUM || tn->kind == SPL_TYPE_SHAPE;
|
||||
}
|
||||
|
||||
/* 从 fn_tid 取返回类型;非 fn 类型返回 0。
|
||||
集中处理 fn 类型查表,避免调用点各自 spl_type_node + kind 检查。 */
|
||||
static spl_type_id_t fn_ret_type(spl_ir2vm_t *ctx, spl_type_id_t fn_tid) {
|
||||
spl_type_node_t *ftn = spl_type_node(ctx->type, fn_tid);
|
||||
if (!ftn || ftn->kind != SPL_TYPE_FN)
|
||||
return 0;
|
||||
return ftn->fn_type.ret;
|
||||
}
|
||||
|
||||
/* 从 fn_tid 取参数个数;非 fn 类型返回 0 */
|
||||
static usize fn_nargs(spl_ir2vm_t *ctx, spl_type_id_t fn_tid) {
|
||||
spl_type_node_t *ftn = spl_type_node(ctx->type, fn_tid);
|
||||
if (!ftn || ftn->kind != SPL_TYPE_FN)
|
||||
return 0;
|
||||
return vec_size(ftn->fn_type.params);
|
||||
}
|
||||
|
||||
static spl_type_id_t ir_agg_field_tid(spl_ir2vm_t *ctx, spl_type_id_t tid, usize idx) {
|
||||
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
|
||||
if (!tn)
|
||||
@@ -304,11 +321,9 @@ 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});
|
||||
|
||||
spl_type_node_t *ftn = spl_type_node(ctx->type, f->fn_tid);
|
||||
usize nargs = (ftn && ftn->kind == SPL_TYPE_FN) ? vec_size(ftn->fn_type.params) : 0;
|
||||
spl_type_id_t ret_t = (ftn && ftn->kind == SPL_TYPE_FN) ? ftn->fn_type.ret : 0;
|
||||
/* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽 */
|
||||
usize hidden_sret = ir_is_aggregate(ctx, ret_t) ? 1 : 0;
|
||||
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);
|
||||
|
||||
usize off = fl->args_bytes;
|
||||
@@ -747,9 +762,7 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
|
||||
idx = (usize)idx_node->type_const.int_lit;
|
||||
}
|
||||
/* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起 */
|
||||
spl_type_node_t *ftn = spl_type_node(ctx->type, cg->irf->fn_tid);
|
||||
spl_type_id_t cur_ret = (ftn && ftn->kind == SPL_TYPE_FN) ? ftn->fn_type.ret : 0;
|
||||
usize shift = ir_is_aggregate(ctx, cur_ret) ? 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_LOAD(cg, k);
|
||||
cg_store_slot(cg, ref, k);
|
||||
@@ -1074,8 +1087,7 @@ static usize translate_function(spl_ir2vm_t *ctx, spl_ir_func_ref_t fid, fix_vec
|
||||
spl_vm_func_t entry = {0};
|
||||
entry.name = strdup(irf->name ? irf->name : "");
|
||||
entry.idx_of_strtab = 0;
|
||||
spl_type_node_t *ftn = spl_type_node(ctx->type, irf->fn_tid);
|
||||
entry.nargs = (ftn && ftn->kind == SPL_TYPE_FN) ? vec_size(ftn->fn_type.params) : 0;
|
||||
entry.nargs = fn_nargs(ctx, irf->fn_tid);
|
||||
entry.ninsns = 0;
|
||||
entry.address = func_ip;
|
||||
vec_push(ctx->prog.funcs, entry);
|
||||
@@ -1121,6 +1133,8 @@ void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type
|
||||
ctx->attr = attr;
|
||||
map_init(ctx->extern_native, spl_map_hash, spl_map_cmp);
|
||||
spl_prog_init(&ctx->prog);
|
||||
|
||||
ctx->diag = NULL;
|
||||
}
|
||||
|
||||
void spl_ir2vm_drop(spl_ir2vm_t *ctx) {
|
||||
|
||||
Reference in new issue
Block a user