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:
1 parent
480945a92f
commit
6c764bb8a0
41 files changed
+4261
-1239
No files matched your search
+93
-100
@@ -10,7 +10,7 @@
|
||||
|
||||
typedef struct {
|
||||
spl_ir_value_kind_t kind;
|
||||
spl_type_id_t tid;
|
||||
spl_ir_type_id_t tid;
|
||||
uint64_t bits;
|
||||
usize slot;
|
||||
} cconst_t;
|
||||
@@ -18,7 +18,7 @@ typedef VEC(cconst_t) cconst_vec_t;
|
||||
|
||||
typedef struct {
|
||||
const spl_ir_t *ir;
|
||||
const spl_type_t *type;
|
||||
const spl_ir_type_table_t *type;
|
||||
spl_layout_t *layout;
|
||||
const spl_attr_t *attr;
|
||||
spl_ir2c_buf_t *out;
|
||||
@@ -77,18 +77,18 @@ static void put_unsupported(ir2c_t *c, const char *what) {
|
||||
put(c, " */\n");
|
||||
}
|
||||
|
||||
static const spl_layout_info_t *info_of(ir2c_t *c, spl_type_id_t tid) {
|
||||
static const spl_layout_info_t *info_of(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
return spl_layout_info_of(c->layout, tid);
|
||||
}
|
||||
|
||||
static usize type_size(ir2c_t *c, spl_type_id_t tid) {
|
||||
static usize type_size(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
const spl_layout_info_t *info = info_of(c, tid);
|
||||
if (!info)
|
||||
return 0;
|
||||
return (info->size_bits + 7) / 8;
|
||||
}
|
||||
|
||||
static usize type_align(ir2c_t *c, spl_type_id_t tid) {
|
||||
static usize type_align(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
const spl_layout_info_t *info = info_of(c, tid);
|
||||
if (!info)
|
||||
return 1;
|
||||
@@ -96,68 +96,64 @@ static usize type_align(ir2c_t *c, spl_type_id_t tid) {
|
||||
return a ? a : 1;
|
||||
}
|
||||
|
||||
static bool is_aggregate(ir2c_t *c, spl_type_id_t tid) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
static bool is_aggregate(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
if (!te)
|
||||
return false;
|
||||
switch (te->kind) {
|
||||
case SPL_TYPE_ARRAY:
|
||||
case SPL_TYPE_STRUCT:
|
||||
case SPL_TYPE_UNION:
|
||||
case SPL_TYPE_ENUM:
|
||||
case SPL_TYPE_SHAPE:
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool tid_is_bool(ir2c_t *c, spl_type_id_t tid) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
return te && te->kind == SPL_TYPE_BOOL;
|
||||
static bool tid_is_bool(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
return te && te->kind == SPL_IR_TYPE_BOOL;
|
||||
}
|
||||
|
||||
static bool tid_is_unit(ir2c_t *c, spl_type_id_t tid) {
|
||||
static bool tid_is_unit(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
if (tid == 0)
|
||||
return true;
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
if (!te)
|
||||
return true;
|
||||
return te->kind == SPL_TYPE_UNIT;
|
||||
return te->kind == SPL_IR_TYPE_UNIT;
|
||||
}
|
||||
|
||||
static spl_type_id_t fn_ret_type(ir2c_t *c, spl_type_id_t fn_tid) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, fn_tid);
|
||||
if (!te || te->kind != SPL_TYPE_FN)
|
||||
static spl_ir_type_id_t fn_ret_type(ir2c_t *c, spl_ir_type_id_t fn_tid) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, fn_tid);
|
||||
if (!te || te->kind != SPL_IR_TYPE_FN)
|
||||
return 0;
|
||||
return te->fn_type.ret;
|
||||
}
|
||||
|
||||
static spl_type_id_t make_ptr(ir2c_t *c, spl_type_id_t pointee) {
|
||||
return spl_type_builder_ptr((spl_type_t *)c->type, pointee);
|
||||
static spl_ir_type_id_t make_ptr(ir2c_t *c, spl_ir_type_id_t pointee) {
|
||||
return spl_ir_type_builder_ptr((spl_ir_type_table_t *)c->type, pointee);
|
||||
}
|
||||
static spl_type_id_t make_bool(ir2c_t *c) {
|
||||
return spl_type_builder_simple((spl_type_t *)c->type, SPL_TYPE_BOOL);
|
||||
static spl_ir_type_id_t make_bool(ir2c_t *c) {
|
||||
return spl_ir_type_builder_simple((spl_ir_type_table_t *)c->type, SPL_IR_TYPE_BOOL);
|
||||
}
|
||||
static spl_type_id_t make_usize(ir2c_t *c) {
|
||||
return spl_type_builder_int((spl_type_t *)c->type, 64, 0);
|
||||
static spl_ir_type_id_t make_usize(ir2c_t *c) {
|
||||
return spl_ir_type_builder_int((spl_ir_type_table_t *)c->type, 64, 0);
|
||||
}
|
||||
|
||||
static bool put_type(ir2c_t *c, spl_type_id_t tid) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
static bool put_type(ir2c_t *c, spl_ir_type_id_t tid) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
if (!te)
|
||||
return false;
|
||||
switch (te->kind) {
|
||||
case SPL_TYPE_UNIT:
|
||||
case SPL_IR_TYPE_UNIT:
|
||||
put(c, "void");
|
||||
return true;
|
||||
case SPL_TYPE_NEVER:
|
||||
put(c, "void");
|
||||
return true;
|
||||
case SPL_TYPE_BOOL:
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
put(c, "u8");
|
||||
return true;
|
||||
case SPL_TYPE_INT:
|
||||
case SPL_IR_TYPE_INT:
|
||||
if (te->int_type.bits == 8)
|
||||
put(c, te->int_type.is_signed ? "i8" : "u8");
|
||||
else if (te->int_type.bits == 16)
|
||||
@@ -167,21 +163,20 @@ static bool put_type(ir2c_t *c, spl_type_id_t tid) {
|
||||
else
|
||||
put(c, te->int_type.is_signed ? "i64" : "u64");
|
||||
return true;
|
||||
case SPL_TYPE_FLOAT:
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
put(c, te->float_type.bits == 32 ? "float" : "double");
|
||||
return true;
|
||||
case SPL_TYPE_PTR:
|
||||
case SPL_IR_TYPE_PTR:
|
||||
put(c, "void*");
|
||||
return true;
|
||||
case SPL_TYPE_FN:
|
||||
case SPL_IR_TYPE_FN:
|
||||
put(c, "void*");
|
||||
return true;
|
||||
/* 聚合值统一以「指向存储的指针」表示 */
|
||||
case SPL_TYPE_ARRAY:
|
||||
case SPL_TYPE_STRUCT:
|
||||
case SPL_TYPE_UNION:
|
||||
case SPL_TYPE_ENUM:
|
||||
case SPL_TYPE_SHAPE:
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
put(c, "void*");
|
||||
return true;
|
||||
default:
|
||||
@@ -190,7 +185,7 @@ static bool put_type(ir2c_t *c, spl_type_id_t tid) {
|
||||
}
|
||||
|
||||
static bool fn_is_extern(ir2c_t *c, const spl_ir_func_t *f) {
|
||||
return spl_ir_fn_extern_attr(c->attr, f) != NULL;
|
||||
return f->extern_fn || spl_ir_fn_extern_attr(c->attr, f) != NULL;
|
||||
}
|
||||
|
||||
static void c_fn_name(ir2c_t *c, spl_ir_func_ref_t fid) {
|
||||
@@ -231,34 +226,33 @@ 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) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
static spl_ir_type_id_t field_tid(ir2c_t *c, spl_ir_type_id_t tid, usize idx) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
if (!te)
|
||||
return 0;
|
||||
switch (te->kind) {
|
||||
case SPL_TYPE_ENUM:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
if (idx == SPL_ENUM_TAG_FIELD_IDX)
|
||||
return te->agg_type.tag_type;
|
||||
idx -= 1;
|
||||
/* fallthrough */
|
||||
case SPL_TYPE_STRUCT:
|
||||
case SPL_TYPE_UNION:
|
||||
case SPL_TYPE_SHAPE:
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
if (idx < vec_size(te->agg_type.variants))
|
||||
return vec_at(te->agg_type.variants, idx).type_id;
|
||||
return 0;
|
||||
case SPL_TYPE_ARRAY:
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
return te->array_type.element;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static usize field_off(ir2c_t *c, spl_type_id_t tid, usize idx) {
|
||||
spl_type_node_t *te = spl_type_node(c->type, tid);
|
||||
static usize field_off(ir2c_t *c, spl_ir_type_id_t tid, usize idx) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, tid);
|
||||
if (!te)
|
||||
return 0;
|
||||
if (te->kind == SPL_TYPE_ARRAY)
|
||||
if (te->kind == SPL_IR_TYPE_ARRAY)
|
||||
return idx * type_size(c, te->array_type.element);
|
||||
const spl_layout_info_t *info = info_of(c, tid);
|
||||
if (!info || idx >= vec_size(info->field_bit_offsets))
|
||||
@@ -267,7 +261,7 @@ static usize field_off(ir2c_t *c, spl_type_id_t tid, usize idx) {
|
||||
}
|
||||
|
||||
/* 节点的「值类型」;0 表示无结果。 */
|
||||
static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) {
|
||||
static spl_ir_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) {
|
||||
switch (n->kind) {
|
||||
case SPL_IR_ARITH_ADD:
|
||||
case SPL_IR_ARITH_SUB:
|
||||
@@ -333,7 +327,7 @@ 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) {
|
||||
static spl_ir_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))
|
||||
@@ -349,7 +343,7 @@ static void bind_agg(ir2c_t *c, usize ref) {
|
||||
put(c, ";\n");
|
||||
}
|
||||
|
||||
static void declare_ref(ir2c_t *c, usize ref, spl_type_id_t tid) {
|
||||
static void declare_ref(ir2c_t *c, usize ref, spl_ir_type_id_t tid) {
|
||||
if (is_aggregate(c, tid)) {
|
||||
usize size = type_size(c, tid);
|
||||
usize words = (size + 7) / 8;
|
||||
@@ -377,12 +371,12 @@ static void emit_agg_construct(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
|
||||
return;
|
||||
}
|
||||
bind_agg(c, ref);
|
||||
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 = field_tid(c, agg_t, i);
|
||||
spl_ir_type_id_t ftid = field_tid(c, agg_t, i);
|
||||
if (!ftid)
|
||||
continue;
|
||||
usize foff = field_off(c, agg_t, i);
|
||||
@@ -440,7 +434,7 @@ static void emit_agg_extract(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
|
||||
|
||||
static void emit_agg_insert(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
|
||||
bind_agg(c, ref);
|
||||
spl_type_id_t agg_t = n->agg_insert.tid;
|
||||
spl_ir_type_id_t agg_t = n->agg_insert.tid;
|
||||
usize sz = type_size(c, agg_t);
|
||||
put(c, "memcpy(v");
|
||||
put_u(c, ref);
|
||||
@@ -449,7 +443,7 @@ static void emit_agg_insert(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
|
||||
put(c, ", ");
|
||||
put_u(c, sz);
|
||||
put(c, ");\n");
|
||||
spl_type_id_t ftid = field_tid(c, agg_t, n->agg_insert.field_idx);
|
||||
spl_ir_type_id_t ftid = field_tid(c, agg_t, n->agg_insert.field_idx);
|
||||
usize foff = field_off(c, agg_t, n->agg_insert.field_idx);
|
||||
if (!ftid)
|
||||
return;
|
||||
@@ -617,14 +611,14 @@ static void emit_cast(ir2c_t *c, usize ref, const spl_ir_node_t *n) {
|
||||
bool from_is_ptr = false;
|
||||
bool from_is_bool = false;
|
||||
bool to_is_ptr = false;
|
||||
spl_type_node_t *te = spl_type_node(c->type, n->cast.from_tid);
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, n->cast.from_tid);
|
||||
if (te) {
|
||||
from_is_ptr = te->kind == SPL_TYPE_PTR;
|
||||
from_is_bool = te->kind == SPL_TYPE_BOOL;
|
||||
from_is_ptr = te->kind == SPL_IR_TYPE_PTR;
|
||||
from_is_bool = te->kind == SPL_IR_TYPE_BOOL;
|
||||
}
|
||||
spl_type_node_t *tte = spl_type_node(c->type, n->cast.to_tid);
|
||||
spl_ir_type_node_t *tte = spl_ir_type_node(c->type, n->cast.to_tid);
|
||||
if (tte)
|
||||
to_is_ptr = tte->kind == SPL_TYPE_PTR;
|
||||
to_is_ptr = tte->kind == SPL_IR_TYPE_PTR;
|
||||
if (to_is_ptr) {
|
||||
put(c, "(void*)(usize)v");
|
||||
put_u(c, n->cast.val);
|
||||
@@ -753,16 +747,15 @@ static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_
|
||||
break;
|
||||
}
|
||||
case SPL_IR_TYPE_FIELD_COUNT: {
|
||||
spl_type_node_t *te = spl_type_node(c->type, n->field_count.tid);
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, n->field_count.tid);
|
||||
if (te) {
|
||||
switch (te->kind) {
|
||||
case SPL_TYPE_STRUCT:
|
||||
case SPL_TYPE_UNION:
|
||||
case SPL_TYPE_ENUM:
|
||||
case SPL_TYPE_SHAPE:
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
v = vec_size(te->agg_type.variants);
|
||||
break;
|
||||
case SPL_TYPE_ARRAY:
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
v = te->array_type.len;
|
||||
break;
|
||||
default:
|
||||
@@ -809,9 +802,9 @@ 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 = 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) {
|
||||
spl_ir_type_id_t func_tid = value_type(c, f, n->control_call.func);
|
||||
spl_ir_type_node_t *ftnode = spl_ir_type_node(c->type, func_tid);
|
||||
if (!ftnode || ftnode->kind != SPL_IR_TYPE_FN) {
|
||||
put_unsupported(c, "indirect call type");
|
||||
return;
|
||||
}
|
||||
@@ -838,7 +831,7 @@ static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir
|
||||
for (usize kp = 0; kp < np; ++kp) {
|
||||
if (need_comma)
|
||||
put(c, ", ");
|
||||
spl_type_id_t pt = vec_at(ftnode->fn_type.params, kp);
|
||||
spl_ir_type_id_t pt = vec_at(ftnode->fn_type.params, kp);
|
||||
if (is_aggregate(c, pt))
|
||||
put(c, "void*");
|
||||
else if (!put_type(c, pt))
|
||||
@@ -864,7 +857,7 @@ static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir
|
||||
}
|
||||
|
||||
static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n,
|
||||
spl_type_id_t ret_tid) {
|
||||
spl_ir_type_id_t ret_tid) {
|
||||
switch (n->kind) {
|
||||
case SPL_IR_CONTROL_PARAM: {
|
||||
usize idx = value_int(n->control_param.idx);
|
||||
@@ -932,7 +925,7 @@ 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) {
|
||||
spl_ir_type_id_t ret_tid) {
|
||||
switch (n->kind) {
|
||||
case SPL_IR_ARITH_ADD:
|
||||
case SPL_IR_ARITH_SUB:
|
||||
@@ -1069,16 +1062,16 @@ static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) {
|
||||
put_u_raw(c, v.global_id);
|
||||
return;
|
||||
}
|
||||
spl_type_node_t *te = spl_type_node(c->type, v.tid);
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, v.tid);
|
||||
if (!te) {
|
||||
put(c, "0");
|
||||
return;
|
||||
}
|
||||
switch (te->kind) {
|
||||
case SPL_TYPE_BOOL:
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
put(c, v.int_bits ? "1" : "0");
|
||||
break;
|
||||
case SPL_TYPE_INT: {
|
||||
case SPL_IR_TYPE_INT: {
|
||||
uint64_t raw = v.int_bits;
|
||||
if (te->int_type.bits == 8)
|
||||
raw = (unsigned char)raw;
|
||||
@@ -1100,7 +1093,7 @@ static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SPL_TYPE_PTR:
|
||||
case SPL_IR_TYPE_PTR:
|
||||
if (v.global_id) {
|
||||
put(c, "(void*)gg");
|
||||
put_u_raw(c, v.global_id);
|
||||
@@ -1108,7 +1101,7 @@ static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) {
|
||||
put(c, "(void*)0");
|
||||
}
|
||||
break;
|
||||
case SPL_TYPE_FN:
|
||||
case SPL_IR_TYPE_FN:
|
||||
if (v.fn == 0) {
|
||||
put(c, "(void*)0");
|
||||
} else {
|
||||
@@ -1116,7 +1109,7 @@ static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) {
|
||||
c_fn_name(c, v.fn);
|
||||
}
|
||||
break;
|
||||
case SPL_TYPE_FLOAT: {
|
||||
case SPL_IR_TYPE_FLOAT: {
|
||||
char buf[64];
|
||||
if (isfinite(v.float_bits))
|
||||
snprintf(buf, sizeof buf, "%.17g", v.float_bits);
|
||||
@@ -1165,7 +1158,7 @@ static void declare_all(ir2c_t *c, const spl_ir_func_t *f) {
|
||||
usize count = value_int(n->mem_alloc.count);
|
||||
if (count == 0)
|
||||
count = 1;
|
||||
spl_type_id_t pointee = n->mem_alloc.tid;
|
||||
spl_ir_type_id_t pointee = n->mem_alloc.tid;
|
||||
if (is_aggregate(c, pointee)) {
|
||||
usize esz = type_size(c, pointee);
|
||||
usize words = (esz * count + 7) / 8;
|
||||
@@ -1191,7 +1184,7 @@ static void declare_all(ir2c_t *c, const spl_ir_func_t *f) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
spl_type_id_t tid = result_tid(c, n);
|
||||
spl_ir_type_id_t tid = result_tid(c, n);
|
||||
if (tid != 0 && !tid_is_unit(c, tid))
|
||||
declare_ref(c, i, tid);
|
||||
}
|
||||
@@ -1200,7 +1193,7 @@ static void declare_all(ir2c_t *c, const spl_ir_func_t *f) {
|
||||
|
||||
static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) {
|
||||
const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, fid);
|
||||
spl_type_id_t ret_tid = fn_ret_type(c, f->fn_tid);
|
||||
spl_ir_type_id_t ret_tid = fn_ret_type(c, f->fn_tid);
|
||||
bool ret_agg = is_aggregate(c, ret_tid);
|
||||
if (ret_agg)
|
||||
put(c, "void");
|
||||
@@ -1214,15 +1207,15 @@ static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) {
|
||||
put(c, "void* ret__");
|
||||
need = true;
|
||||
}
|
||||
spl_type_node_t *te = spl_type_node(c->type, f->fn_tid);
|
||||
if (te && te->kind == SPL_TYPE_FN) {
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, f->fn_tid);
|
||||
if (te && te->kind == SPL_IR_TYPE_FN) {
|
||||
usize nargs = vec_size(te->fn_type.params);
|
||||
for (usize k = 0; k < nargs; ++k) {
|
||||
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) {
|
||||
spl_ir_type_id_t pt = vec_at(te->fn_type.params, k);
|
||||
spl_ir_type_node_t *ptn = spl_ir_type_node(c->type, pt);
|
||||
if (ptn && ptn->kind == SPL_IR_TYPE_UNIT) {
|
||||
/* varargs 标记在 IR 里落成 unit 类型。 */
|
||||
put(c, "...");
|
||||
need = true;
|
||||
@@ -1251,7 +1244,7 @@ static void emit_proto(ir2c_t *c, spl_ir_func_ref_t fid) {
|
||||
|
||||
static void emit_def(ir2c_t *c, spl_ir_func_ref_t fid) {
|
||||
const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, fid);
|
||||
spl_type_id_t ret_tid = fn_ret_type(c, f->fn_tid);
|
||||
spl_ir_type_id_t ret_tid = fn_ret_type(c, f->fn_tid);
|
||||
emit_fn_sig(c, fid, true);
|
||||
put(c, " {\n");
|
||||
declare_all(c, f);
|
||||
@@ -1293,8 +1286,8 @@ static void emit_globals(ir2c_t *c) {
|
||||
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_type_node_t *it = spl_ir_type_node(c->type, g->init.tid);
|
||||
if (it && it->kind == SPL_IR_TYPE_ARRAY) {
|
||||
spl_ir_const_t *k =
|
||||
spl_ir_const((spl_ir_t *)c->ir, g->init.const_id);
|
||||
if (k) {
|
||||
@@ -1342,8 +1335,8 @@ static void run(ir2c_t *c) {
|
||||
if (m != 0) {
|
||||
const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, m);
|
||||
usize nargs = 0;
|
||||
spl_type_node_t *te = spl_type_node(c->type, f->fn_tid);
|
||||
if (te && te->kind == SPL_TYPE_FN)
|
||||
spl_ir_type_node_t *te = spl_ir_type_node(c->type, f->fn_tid);
|
||||
if (te && te->kind == SPL_IR_TYPE_FN)
|
||||
nargs = vec_size(te->fn_type.params);
|
||||
if (nargs == 0) {
|
||||
put(c, "int main(void) { ");
|
||||
@@ -1371,11 +1364,11 @@ static void run(ir2c_t *c) {
|
||||
}
|
||||
}
|
||||
|
||||
bool spl_ir2c_run(const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layout,
|
||||
const spl_attr_t *attr, spl_ir2c_buf_t *out) {
|
||||
bool spl_ir2c_run(const spl_ir_t *ir, spl_layout_t *layout, const spl_attr_t *attr,
|
||||
spl_ir2c_buf_t *out) {
|
||||
ir2c_t c = {
|
||||
.ir = ir,
|
||||
.type = type,
|
||||
.type = ir->types,
|
||||
.layout = layout,
|
||||
.attr = attr,
|
||||
.out = out,
|
||||
|
||||
+102
-87
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user