refactor(stage1 ai): 抽离 IR 类型系统并让 exec 基于 IR 运行

- IR 类型表并入 spl_ir_t,lang_type 改为借用
- 抽出 spl_ir_type / spl_ir_layout,移除前端专用 IR 类型
- lang 聚合改为 nominal 身份,字段与方法回收到 lang_type
- 新增 spl_ir_link,建立 def/lang_type <-> ir_func/ir_type 双向绑定
- exec 去除 CFG 依赖,plan 由 IR 调用图生成
- meta 不再构建 AST CFG,按 IR 函数按需编译
- host ABI 改为 typed value,支持聚合返回
- @sap.view 改为普通 host op,支持动态 handle
- 新增 nominal 聚合与动态 sap.view 回归测试
This commit is contained in:
zzy committed 2026-12-03 12:31:34 +08:00
1 parent 480945a92f
commit 6c764bb8a0
41 files changed
+4261 -1239

No files matched your search

+102 -87
View File
@@ -4,18 +4,18 @@
static usize align_up(usize x, usize a);
/* size/align/offset 统一取自 layout(spl_layout 是唯一准确来源)。 */
static const spl_layout_info_t *ir_layout(spl_ir2vm_t *ctx, spl_type_id_t tid) {
static const spl_layout_info_t *ir_layout(spl_ir2vm_t *ctx, spl_ir_type_id_t tid) {
return spl_layout_info_of(ctx->layout, tid);
}
static usize ir_type_size(spl_ir2vm_t *ctx, spl_type_id_t tid) {
static usize ir_type_size(spl_ir2vm_t *ctx, spl_ir_type_id_t tid) {
const spl_layout_info_t *info = ir_layout(ctx, tid);
if (!info)
return 0;
return (info->size_bits + 7) / 8;
}
static usize ir_type_align(spl_ir2vm_t *ctx, spl_type_id_t tid) {
static usize ir_type_align(spl_ir2vm_t *ctx, spl_ir_type_id_t tid) {
const spl_layout_info_t *info = ir_layout(ctx, tid);
if (!info)
return 1;
@@ -23,34 +23,34 @@ static usize ir_type_align(spl_ir2vm_t *ctx, spl_type_id_t tid) {
return a ? a : 1;
}
static bool ir_is_aggregate(spl_ir2vm_t *ctx, spl_type_id_t tid) {
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
static bool ir_is_aggregate(spl_ir2vm_t *ctx, spl_ir_type_id_t tid) {
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, tid);
if (!tn)
return false;
return tn->kind == SPL_TYPE_ARRAY || tn->kind == SPL_TYPE_STRUCT ||
tn->kind == SPL_TYPE_UNION || tn->kind == SPL_TYPE_ENUM || tn->kind == SPL_TYPE_SHAPE;
return tn->kind == SPL_IR_TYPE_ARRAY || tn->kind == SPL_IR_TYPE_STRUCT ||
tn->kind == SPL_IR_TYPE_UNION || tn->kind == SPL_IR_TYPE_ENUM;
}
/* 从 fn_tid 取返回类型;非 fn 类型返回 0。 */
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)
static spl_ir_type_id_t fn_ret_type(spl_ir2vm_t *ctx, spl_ir_type_id_t fn_tid) {
spl_ir_type_node_t *ftn = spl_ir_type_node(ctx->type, fn_tid);
if (!ftn || ftn->kind != SPL_IR_TYPE_FN)
return 0;
return ftn->fn_type.ret;
}
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)
static usize fn_nargs(spl_ir2vm_t *ctx, spl_ir_type_id_t fn_tid) {
spl_ir_type_node_t *ftn = spl_ir_type_node(ctx->type, fn_tid);
if (!ftn || ftn->kind != SPL_IR_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);
static spl_ir_type_id_t ir_agg_field_tid(spl_ir2vm_t *ctx, spl_ir_type_id_t tid, usize idx) {
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, tid);
if (!tn)
return 0;
if (tn->kind == SPL_TYPE_ENUM) {
if (tn->kind == SPL_IR_TYPE_ENUM) {
if (idx == SPL_ENUM_TAG_FIELD_IDX)
return tn->agg_type.tag_type;
usize vidx = idx - 1;
@@ -58,20 +58,20 @@ static spl_type_id_t ir_agg_field_tid(spl_ir2vm_t *ctx, spl_type_id_t tid, usize
return vec_at(tn->agg_type.variants, vidx).type_id;
return 0;
}
if (tn->kind == SPL_TYPE_STRUCT || tn->kind == SPL_TYPE_UNION) {
if (tn->kind == SPL_IR_TYPE_STRUCT || tn->kind == SPL_IR_TYPE_UNION) {
if (idx < vec_size(tn->agg_type.variants))
return vec_at(tn->agg_type.variants, idx).type_id;
} else if (tn->kind == SPL_TYPE_ARRAY) {
} else if (tn->kind == SPL_IR_TYPE_ARRAY) {
return tn->array_type.element;
}
return 0;
}
static usize ir_agg_field_off(spl_ir2vm_t *ctx, spl_type_id_t tid, usize idx) {
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
static usize ir_agg_field_off(spl_ir2vm_t *ctx, spl_ir_type_id_t tid, usize idx) {
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, tid);
if (!tn)
return 0;
if (tn->kind == SPL_TYPE_ARRAY)
if (tn->kind == SPL_IR_TYPE_ARRAY)
return idx * ir_type_size(ctx, tn->array_type.element);
const spl_layout_info_t *info = ir_layout(ctx, tid);
@@ -96,16 +96,16 @@ static int get_or_add_native(spl_ir2vm_t *ctx, const char *name) {
return (int)(vec_size(ctx->prog.natives) - 1);
}
static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_type_id_t tid) {
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_ir_type_id_t tid) {
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, tid);
if (!tn)
return SPL_VOID;
switch (tn->kind) {
case SPL_TYPE_UNIT:
case SPL_IR_TYPE_UNIT:
return SPL_VOID;
case SPL_TYPE_BOOL:
case SPL_IR_TYPE_BOOL:
return SPL_BOOL;
case SPL_TYPE_INT:
case SPL_IR_TYPE_INT:
if (tn->int_type.bits == 0)
return SPL_I32;
switch (tn->int_type.bits) {
@@ -118,7 +118,7 @@ static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_type_id_t tid) {
default:
return tn->int_type.is_signed ? SPL_I64 : SPL_U64;
}
case SPL_TYPE_FLOAT:
case SPL_IR_TYPE_FLOAT:
return tn->float_type.bits == 32 ? SPL_F32 : SPL_F64;
default:
return SPL_PTR;
@@ -131,7 +131,7 @@ static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_type_id_t tid) {
* mem.offset/mem.field 的 .tid 存的是 pointee/aggregate,须转成指针类型。
* 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) {
static spl_ir_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
switch (n->kind) {
case SPL_IR_ARITH_ADD:
case SPL_IR_ARITH_SUB:
@@ -167,15 +167,15 @@ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) {
case SPL_IR_CASE_FLOAT2INT:
return n->cast.to_tid;
case SPL_IR_MEM_ALLOCA:
return spl_type_builder_ptr((spl_type_t *)ctx->type, n->mem_alloc.tid);
return spl_ir_type_builder_ptr((spl_ir_type_table_t *)ctx->type, n->mem_alloc.tid);
case SPL_IR_MEM_LOAD:
return n->mem_load.tid;
case SPL_IR_MEM_OFFSET:
return spl_type_builder_ptr((spl_type_t *)ctx->type, n->mem_offset.tid);
return spl_ir_type_builder_ptr((spl_ir_type_table_t *)ctx->type, n->mem_offset.tid);
case SPL_IR_MEM_FIELD_PTR: {
spl_type_id_t field =
spl_ir_type_id_t field =
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_ir_type_builder_ptr((spl_ir_type_table_t *)ctx->type, field);
}
case SPL_IR_TYPE_BITSIZEOF:
return n->bitsizeof.tid;
@@ -222,7 +222,7 @@ 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) {
static spl_ir_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))
@@ -423,7 +423,7 @@ static data_loc_t value_ptr_loc(cgen_t *cg, spl_ir_value_t v, usize 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_ir_type_id_t tid) {
usize sz = ir_type_size(cg->ctx, tid);
usize done = 0;
while (sz - done >= 8) {
@@ -486,11 +486,11 @@ static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k) {
return;
}
spl_type_node_t *tn = spl_type_node(ctx->type, v.tid);
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, v.tid);
if (!tn)
return;
if (tn->kind == SPL_TYPE_FN) {
if (tn->kind == SPL_IR_TYPE_FN) {
isize nat_idx = -1;
if (v.fn > 0)
map_get(ctx->extern_native, v.fn, &nat_idx);
@@ -507,16 +507,16 @@ static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k) {
spl_vm_val_t imm = 0;
switch (tn->kind) {
case SPL_TYPE_BOOL:
case SPL_IR_TYPE_BOOL:
imm = v.int_bits ? 1 : 0;
break;
case SPL_TYPE_INT:
case SPL_IR_TYPE_INT:
imm = (spl_vm_val_t)v.int_bits;
break;
case SPL_TYPE_FLOAT:
case SPL_IR_TYPE_FLOAT:
memcpy(&imm, &v.float_bits, sizeof(double));
break;
case SPL_TYPE_PTR:
case SPL_IR_TYPE_PTR:
imm = 0;
break;
default:
@@ -527,8 +527,8 @@ static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k) {
static void cg_cast(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
spl_ir2vm_t *ctx = cg->ctx;
spl_type_node_t *ft = spl_type_node(ctx->type, n->cast.from_tid);
spl_type_node_t *tt = spl_type_node(ctx->type, n->cast.to_tid);
spl_ir_type_node_t *ft = spl_ir_type_node(ctx->type, n->cast.from_tid);
spl_ir_type_node_t *tt = spl_ir_type_node(ctx->type, n->cast.to_tid);
if (!ft || !tt)
return;
@@ -537,14 +537,14 @@ static void cg_cast(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
cg_load_slot(cg, n->cast.val, fk);
if (ft->kind == SPL_TYPE_INT && tt->kind == SPL_TYPE_INT) {
if (ft->kind == SPL_IR_TYPE_INT && tt->kind == SPL_IR_TYPE_INT) {
usize fb = ft->int_type.bits;
usize tb = tt->int_type.bits;
if (fb > tb)
emit(cg, SPL_TRUNC, SPL_VOID, tb);
else if (fb < tb)
emit(cg, ft->int_type.is_signed ? SPL_SEXT : SPL_ZEXT, SPL_VOID, fb);
} else if (ft->kind == SPL_TYPE_BOOL && tt->kind == SPL_TYPE_INT) {
} else if (ft->kind == SPL_IR_TYPE_BOOL && tt->kind == SPL_IR_TYPE_INT) {
emit(cg, SPL_ZEXT, SPL_VOID, 1);
}
@@ -562,7 +562,7 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
}
case SPL_IR_MEM_LOAD: {
spl_type_id_t tid = n->mem_load.tid;
spl_ir_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, value_ptr_loc(cg, n->mem_load.ptr, 0),
@@ -579,7 +579,7 @@ 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;
spl_ir_type_id_t tid = n->mem_store.tid;
if (ir_is_aggregate(ctx, tid)) {
cg_copy_agg(cg, value_ptr_loc(cg, n->mem_store.val, 0),
value_ptr_loc(cg, n->mem_store.ptr, 0), tid);
@@ -606,7 +606,7 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
}
case SPL_IR_MEM_FIELD_PTR: {
spl_type_id_t agg_tid = n->mem_field_ptr.tid;
spl_ir_type_id_t agg_tid = n->mem_field_ptr.tid;
usize off = ir_agg_field_off(ctx, agg_tid, n->mem_field_ptr.field_idx);
cg_load_slot(cg, n->mem_field_ptr.agg, SPL_PTR);
if (off) {
@@ -628,13 +628,13 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
case SPL_IR_AGG_CONSTRUCT: {
usize base = vec_at(cg->frame.slots, ref).data_off;
spl_type_id_t agg_t = n->agg_construct.tid;
spl_ir_type_id_t agg_t = n->agg_construct.tid;
for (usize i = 0; i < vec_size(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_ir_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);
@@ -654,8 +654,8 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
}
case SPL_IR_AGG_EXTRACT: {
spl_type_id_t agg_t = n->agg_extract.tid;
spl_type_id_t ftid = n->agg_extract.field_tid;
spl_ir_type_id_t agg_t = n->agg_extract.tid;
spl_ir_type_id_t ftid = n->agg_extract.field_tid;
usize foff = ir_agg_field_off(ctx, agg_t, n->agg_extract.field_idx);
if (ir_is_aggregate(ctx, ftid)) {
@@ -680,13 +680,13 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
case SPL_IR_AGG_INSERT: {
usize base = vec_at(cg->frame.slots, ref).data_off;
spl_type_id_t agg_t = n->agg_insert.tid;
spl_ir_type_id_t agg_t = n->agg_insert.tid;
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);
spl_ir_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, value_ptr_loc(cg, n->agg_insert.field, 0),
@@ -729,9 +729,9 @@ static void cg_typeop(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
break;
}
case SPL_IR_TYPE_FIELD_COUNT: {
spl_type_node_t *tn = spl_type_node(ctx->type, n->field_count.tid);
if (tn && (tn->kind == SPL_TYPE_STRUCT || tn->kind == SPL_TYPE_UNION ||
tn->kind == SPL_TYPE_ENUM || tn->kind == SPL_TYPE_SHAPE))
spl_ir_type_node_t *tn = spl_ir_type_node(ctx->type, n->field_count.tid);
if (tn && (tn->kind == SPL_IR_TYPE_STRUCT || tn->kind == SPL_IR_TYPE_UNION ||
tn->kind == SPL_IR_TYPE_ENUM))
v = vec_size(tn->agg_type.variants);
break;
}
@@ -758,7 +758,7 @@ 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;
spl_ir_type_id_t ret_tid = n->control_ret.tid;
if (ir_is_aggregate(ctx, ret_tid)) {
/* sret:把返回值写入 *(fp+0),然后以 void 返回。 */
if (!spl_ir_value_is_none(n->control_ret.val)) {
@@ -815,13 +815,27 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx);
emit(cg, SPL_NCALL, SPL_VOID, (spl_vm_val_t)nargs);
uint8_t rk = vm_kind_of(ctx, n->control_call.tid);
if (rk == SPL_VOID)
emit(cg, SPL_DROP, SPL_VOID, 0); /* NCALL always push result */
else
cg_store_slot(cg, ref, rk);
if (ir_is_aggregate(ctx, n->control_call.tid)) {
usize sz = ir_type_size(ctx, n->control_call.tid);
usize result_off = vec_at(cg->frame.slots, ref).result_off;
usize data_off = vec_at(cg->frame.slots, ref).data_off;
cg_store_slot(cg, ref, SPL_PTR);
CG_LADDR(cg, data_off);
CG_LADDR(cg, result_off);
CG_LOAD(cg, SPL_PTR);
CG_PUSH(cg, SPL_U64, (spl_vm_val_t)sz);
emit(cg, SPL_MEMCPY, SPL_VOID, 0);
CG_LADDR(cg, data_off);
cg_store_slot(cg, ref, SPL_PTR);
} else {
uint8_t rk = vm_kind_of(ctx, n->control_call.tid);
if (rk == SPL_VOID)
emit(cg, SPL_DROP, SPL_VOID, 0); /* NCALL always push result */
else
cg_store_slot(cg, ref, rk);
}
} else {
spl_type_id_t ret_t = n->control_call.tid;
spl_ir_type_id_t ret_t = n->control_call.tid;
bool ret_is_sret = ir_is_aggregate(ctx, ret_t);
usize sret_off = 0;
@@ -877,14 +891,14 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
cg_binop(cg, ref, n, SPL_MUL);
break;
case SPL_IR_ARITH_DIV: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_binop(cg, ref, n, sgn ? SPL_DIV_S : SPL_DIV_U);
break;
}
case SPL_IR_ARITH_REM: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_binop(cg, ref, n, sgn ? SPL_REM_S : SPL_REM_U);
break;
}
@@ -901,8 +915,8 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
cg_binop(cg, ref, n, SPL_SHL);
break;
case SPL_IR_ARITH_SHR: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->arith.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_binop(cg, ref, n, sgn ? SPL_SHR_S : SPL_SHR_U);
break;
}
@@ -910,8 +924,8 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
cg_unop(cg, ref, n, SPL_NEG);
break;
case SPL_IR_ARITH_NOT: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->arith.tid);
if (tn && tn->kind == SPL_TYPE_BOOL) {
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->arith.tid);
if (tn && tn->kind == SPL_IR_TYPE_BOOL) {
cg_load_slot(cg, n->arith.left, SPL_BOOL);
CG_PUSH(cg, SPL_BOOL, 0);
CG_OP(cg, SPL_EQ, SPL_BOOL);
@@ -929,26 +943,26 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) {
cg_cmp(cg, ref, n, SPL_NE);
break;
case SPL_IR_CMP_LT: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_cmp(cg, ref, n, sgn ? SPL_SLT : SPL_ULT);
break;
}
case SPL_IR_CMP_LE: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_cmp(cg, ref, n, sgn ? SPL_SLE : SPL_ULE);
break;
}
case SPL_IR_CMP_GT: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_cmp(cg, ref, n, sgn ? SPL_SGT : SPL_UGT);
break;
}
case SPL_IR_CMP_GE: {
spl_type_node_t *tn = spl_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_TYPE_INT && tn->int_type.is_signed;
spl_ir_type_node_t *tn = spl_ir_type_node(cg->ctx->type, n->cmp.tid);
bool sgn = tn && tn->kind == SPL_IR_TYPE_INT && tn->int_type.is_signed;
cg_cmp(cg, ref, n, sgn ? SPL_SGE : SPL_UGE);
break;
}
@@ -1107,8 +1121,9 @@ static void register_externs(spl_ir2vm_t *ctx) {
if (!irf)
continue;
spl_attr_node_t *ext = spl_ir_fn_extern_attr(ctx->attr, irf);
if (ext) {
ext->consumed_mask |= (1u << SPL_ATTR_STAGE_IR);
if (ext || irf->extern_fn) {
if (ext)
ext->consumed_mask |= (1u << SPL_ATTR_STAGE_IR);
int nat_idx = get_or_add_native(ctx, irf->name);
map_put(ctx->extern_native, i, (isize)nat_idx);
}
@@ -1131,8 +1146,8 @@ static void prepare_globals(spl_ir2vm_t *ctx) {
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_type_node_t *it = spl_ir_type_node(ctx->type, g->init.tid);
if (it && it->kind == SPL_IR_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;
@@ -1152,11 +1167,11 @@ static void prepare_globals(spl_ir2vm_t *ctx) {
}
}
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);
void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, spl_layout_t *layout,
const spl_attr_t *attr) {
Assert(ir != NULL && attr != NULL);
ctx->ir = ir;
ctx->type = type;
ctx->type = ir->types;
ctx->attr = attr;
ctx->layout = layout;
map_init(ctx->extern_native, spl_map_hash, spl_map_cmp);