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,
|
||||
|
||||
Reference in new issue
Block a user