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
+2
-2
@@ -369,7 +369,7 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
|
||||
has_val = true;
|
||||
}
|
||||
|
||||
spl_type_id_t ret_tid = call->control_call.tid;
|
||||
spl_ir_type_id_t ret_tid = call->control_call.tid;
|
||||
|
||||
/* 释放调用点原 params(实参已映射进 val_map)。 */
|
||||
vec_free(call->control_call.params);
|
||||
@@ -500,7 +500,7 @@ static int opt_function(spl_ir_t *ir, spl_ir_func_ref_t fid, const spl_attr_t *a
|
||||
if (!func_has_attr(attr, calf, "inline") || func_has_attr(attr, calf, "noinline"))
|
||||
continue;
|
||||
/* extern 是占位体,绝不能内联。 */
|
||||
if (func_has_attr(attr, calf, "extern"))
|
||||
if (calf->extern_fn || func_has_attr(attr, calf, "extern"))
|
||||
continue;
|
||||
/* 防环:callee 传递可达 caller 时拒绝(覆盖直接/间接互递归)。 */
|
||||
memset(mark, 0, nfuncs);
|
||||
|
||||
+129
-54
@@ -29,6 +29,21 @@ static int const_key_cmp(spl_ir_const_key_t a, spl_ir_const_key_t b) {
|
||||
}
|
||||
|
||||
void spl_ir_init(spl_ir_t *ir) {
|
||||
ir->types = calloc(1, sizeof(*ir->types));
|
||||
spl_ir_type_table_init(ir->types);
|
||||
ir->owns_types = true;
|
||||
vec_init(ir->funcs);
|
||||
vec_push(ir->funcs, (spl_ir_func_t){0});
|
||||
vec_init(ir->consts);
|
||||
vec_push(ir->consts, (spl_ir_const_t){0});
|
||||
vec_init(ir->globals);
|
||||
vec_push(ir->globals, (spl_ir_global_t){0});
|
||||
map_init(ir->const_index, const_key_hash, const_key_cmp);
|
||||
}
|
||||
|
||||
void spl_ir_init_shared(spl_ir_t *ir, spl_ir_type_table_t *types) {
|
||||
ir->types = types;
|
||||
ir->owns_types = false;
|
||||
vec_init(ir->funcs);
|
||||
vec_push(ir->funcs, (spl_ir_func_t){0});
|
||||
vec_init(ir->consts);
|
||||
@@ -78,6 +93,11 @@ void spl_ir_drop(spl_ir_t *ir) {
|
||||
free((char *)vec_at(ir->globals, i).name);
|
||||
vec_free(ir->globals);
|
||||
map_free(ir->const_index);
|
||||
if (ir->owns_types && ir->types) {
|
||||
spl_ir_type_table_drop(ir->types);
|
||||
free(ir->types);
|
||||
}
|
||||
ir->types = NULL;
|
||||
}
|
||||
|
||||
spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir) {
|
||||
@@ -238,6 +258,49 @@ void spl_ir_node_visit_values(spl_ir_node_t *n, spl_ir_value_cb_t cb, void *ud)
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
spl_ir_t *ir;
|
||||
spl_ir_func_ref_vec_t *out;
|
||||
} spl_ir_callee_scan_t;
|
||||
|
||||
static bool callee_contains(const spl_ir_func_ref_vec_t *out, spl_ir_func_ref_t fn) {
|
||||
for (usize i = 0; i < vec_size(*out); ++i) {
|
||||
if (vec_at(*out, i) == fn)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void callee_scan_value(void *ud, spl_ir_value_t v) {
|
||||
spl_ir_callee_scan_t *scan = (spl_ir_callee_scan_t *)ud;
|
||||
if (v.kind != SPL_IR_VALUE_CONST || !v.tid || !v.fn)
|
||||
return;
|
||||
spl_ir_type_node_t *tn = spl_ir_type_node(scan->ir->types, v.tid);
|
||||
if (!tn || tn->kind != SPL_IR_TYPE_FN)
|
||||
return;
|
||||
spl_ir_func_t *callee = spl_ir_func(scan->ir, v.fn);
|
||||
if (!callee || callee->extern_fn)
|
||||
return;
|
||||
if (!callee_contains(scan->out, v.fn))
|
||||
vec_push(*scan->out, v.fn);
|
||||
}
|
||||
|
||||
bool spl_ir_func_is_extern(spl_ir_t *ir, spl_ir_func_ref_t fn) {
|
||||
spl_ir_func_t *f = spl_ir_func(ir, fn);
|
||||
return f && f->extern_fn;
|
||||
}
|
||||
|
||||
void spl_ir_func_callees(spl_ir_t *ir, spl_ir_func_ref_t fn, spl_ir_func_ref_vec_t *out) {
|
||||
if (!ir || !out)
|
||||
return;
|
||||
spl_ir_func_t *f = spl_ir_func(ir, fn);
|
||||
if (!f)
|
||||
return;
|
||||
spl_ir_callee_scan_t scan = {.ir = ir, .out = out};
|
||||
for (usize i = 1; i < vec_size(f->nodes); ++i)
|
||||
spl_ir_node_visit_values(&vec_at(f->nodes, i), callee_scan_value, &scan);
|
||||
}
|
||||
|
||||
spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func_t *f) {
|
||||
if (!attr || !f)
|
||||
return NULL;
|
||||
@@ -249,9 +312,9 @@ spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline void ir_dump_type(const spl_type_t *ty, spl_type_id_t tid) {
|
||||
static inline void ir_dump_type(const spl_ir_type_table_t *ty, spl_ir_type_id_t tid) {
|
||||
printf("(");
|
||||
spl_type_pure_dump((spl_type_t *)ty, tid);
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, tid);
|
||||
printf(")");
|
||||
}
|
||||
|
||||
@@ -263,7 +326,7 @@ static inline void ir_dump_ref(spl_ir_node_ref_t ref) {
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ir_dump_value(const spl_type_t *ty, spl_ir_value_t v) {
|
||||
static inline void ir_dump_value(const spl_ir_type_table_t *ty, spl_ir_value_t v) {
|
||||
if (v.kind == SPL_IR_VALUE_INST) {
|
||||
ir_dump_ref(v.inst);
|
||||
return;
|
||||
@@ -272,22 +335,22 @@ static inline void ir_dump_value(const spl_type_t *ty, spl_ir_value_t v) {
|
||||
printf(" g#%zu", v.global_id);
|
||||
return;
|
||||
}
|
||||
spl_type_node_t *t = spl_type_node((spl_type_t *)ty, v.tid);
|
||||
if (t && t->kind == SPL_TYPE_FLOAT)
|
||||
spl_ir_type_node_t *t = spl_ir_type_node((spl_ir_type_table_t *)ty, v.tid);
|
||||
if (t && t->kind == SPL_IR_TYPE_FLOAT)
|
||||
printf(" float=%f", v.float_bits);
|
||||
else if (t && t->kind == SPL_TYPE_FN)
|
||||
else if (t && t->kind == SPL_IR_TYPE_FN)
|
||||
printf(" fn#%zu", v.fn);
|
||||
else if (t && t->kind == SPL_TYPE_PTR)
|
||||
else if (t && t->kind == SPL_IR_TYPE_PTR)
|
||||
printf(" null");
|
||||
else if (t && t->kind == SPL_TYPE_INT && t->int_type.bits == 8)
|
||||
else if (t && t->kind == SPL_IR_TYPE_INT && t->int_type.bits == 8)
|
||||
printf(" char='%c'", (char)v.int_bits);
|
||||
else if (t && (t->kind == SPL_TYPE_ARRAY))
|
||||
else if (t && (t->kind == SPL_IR_TYPE_ARRAY))
|
||||
printf(" c#%zu", v.const_id);
|
||||
else
|
||||
printf(" int=%llu", (unsigned long long)v.int_bits);
|
||||
}
|
||||
|
||||
static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
|
||||
static void ir_dump_node(const spl_ir_type_table_t *ty, spl_ir_node_t *n) {
|
||||
switch (n->kind) {
|
||||
case SPL_IR_ARITH_ADD:
|
||||
case SPL_IR_ARITH_SUB:
|
||||
@@ -331,9 +394,9 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) {
|
||||
case SPL_IR_CASE_INT2FLOAT:
|
||||
case SPL_IR_CASE_FLOAT2INT:
|
||||
printf(" ");
|
||||
spl_type_pure_dump((spl_type_t *)ty, n->cast.from_tid);
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, n->cast.from_tid);
|
||||
printf(" -> ");
|
||||
spl_type_pure_dump((spl_type_t *)ty, n->cast.to_tid);
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, n->cast.to_tid);
|
||||
ir_dump_value(ty, n->cast.val);
|
||||
break;
|
||||
case SPL_IR_MEM_ALLOCA:
|
||||
@@ -451,20 +514,21 @@ static void ir_dump_attr_list(const spl_attr_t *attr, spl_attr_id_vec_t *ids) {
|
||||
printf("]");
|
||||
}
|
||||
|
||||
void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr) {
|
||||
void spl_ir_dump(spl_ir_t *ir, const spl_attr_t *attr) {
|
||||
const spl_ir_type_table_t *ty = ir->types;
|
||||
for (usize i = 1; i < vec_size(ir->consts); ++i) {
|
||||
spl_ir_const_t *c = &vec_at(ir->consts, i);
|
||||
printf("const c#%zu ", i);
|
||||
spl_type_pure_dump((spl_type_t *)ty, c->tid);
|
||||
spl_type_node_t *tn = spl_type_node((spl_type_t *)ty, c->tid);
|
||||
if (tn && tn->kind == SPL_TYPE_ARRAY)
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, c->tid);
|
||||
spl_ir_type_node_t *tn = spl_ir_type_node((spl_ir_type_table_t *)ty, c->tid);
|
||||
if (tn && tn->kind == SPL_IR_TYPE_ARRAY)
|
||||
printf(" bytes=%zu", c->bytes.len);
|
||||
printf("\n");
|
||||
}
|
||||
for (usize i = 1; i < vec_size(ir->globals); ++i) {
|
||||
spl_ir_global_t *g = &vec_at(ir->globals, i);
|
||||
printf("global g#%zu %s ", i, g->readonly ? "const" : "var");
|
||||
spl_type_pure_dump((spl_type_t *)ty, g->tid);
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, g->tid);
|
||||
if (g->name)
|
||||
printf(" %s", g->name);
|
||||
if (!spl_ir_value_is_none(g->init)) {
|
||||
@@ -479,7 +543,7 @@ void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr) {
|
||||
spl_ir_func_t *func = &vec_at(ir->funcs, i);
|
||||
printf("func %s %%%zu", func->name ? func->name : "?", i);
|
||||
printf(" tid = ");
|
||||
spl_type_pure_dump((spl_type_t *)ty, func->fn_tid);
|
||||
spl_ir_type_dump((spl_ir_type_table_t *)ty, func->fn_tid);
|
||||
ir_dump_attr_list(attr, &func->attrs);
|
||||
printf("\n");
|
||||
|
||||
@@ -520,8 +584,19 @@ static spl_ir_node_ref_t builder_alloc_node(spl_ir_builder_t *b, spl_ir_kind_t k
|
||||
return ref;
|
||||
}
|
||||
|
||||
static void spl_ir_builder_reset(spl_ir_builder_t *b);
|
||||
|
||||
void spl_ir_builder_init(spl_ir_builder_t *b) {
|
||||
spl_ir_init(&b->ir);
|
||||
spl_ir_builder_reset(b);
|
||||
}
|
||||
|
||||
void spl_ir_builder_init_shared(spl_ir_builder_t *b, spl_ir_type_table_t *types) {
|
||||
spl_ir_init_shared(&b->ir, types);
|
||||
spl_ir_builder_reset(b);
|
||||
}
|
||||
|
||||
static void spl_ir_builder_reset(spl_ir_builder_t *b) {
|
||||
b->current_fn = 0;
|
||||
b->dbg = (spl_dbg_node_t){0};
|
||||
vec_init(b->blocks);
|
||||
@@ -537,7 +612,7 @@ void spl_ir_builder_drop(spl_ir_builder_t *b) {
|
||||
}
|
||||
|
||||
spl_ir_func_ref_t spl_ir_builder_fn_begin(spl_ir_builder_t *b, const char *name,
|
||||
spl_type_id_t fn_tid) {
|
||||
spl_ir_type_id_t fn_tid) {
|
||||
if (b->current_fn) {
|
||||
spl_ir_builder_fn_end(b);
|
||||
}
|
||||
@@ -556,7 +631,7 @@ spl_ir_func_ref_t spl_ir_builder_fn_begin(spl_ir_builder_t *b, const char *name,
|
||||
}
|
||||
|
||||
spl_ir_func_ref_t spl_ir_builder_fn_reserve(spl_ir_builder_t *b, const char *name,
|
||||
spl_type_id_t fn_tid) {
|
||||
spl_ir_type_id_t fn_tid) {
|
||||
spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir);
|
||||
spl_ir_func_t *f = spl_ir_func(&b->ir, fid);
|
||||
f->name = name ? strdup(name) : NULL;
|
||||
@@ -634,7 +709,7 @@ void spl_ir_builder_block_end(spl_ir_builder_t *b) {
|
||||
}
|
||||
|
||||
#define X(name, kind) \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_ir_type_id_t x_tid, \
|
||||
spl_ir_value_t x_left, spl_ir_value_t x_right) { \
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
|
||||
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
|
||||
@@ -647,7 +722,7 @@ SPL_IR_BIN_ARITH_TABLE
|
||||
#undef X
|
||||
|
||||
#define X(name, kind) \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_ir_type_id_t x_tid, \
|
||||
spl_ir_value_t x_val) { \
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
|
||||
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
|
||||
@@ -660,7 +735,7 @@ SPL_IR_UN_ARITH_TABLE
|
||||
#undef X
|
||||
|
||||
#define X(name, kind) \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_ir_type_id_t x_tid, \
|
||||
spl_ir_value_t x_a, spl_ir_value_t x_b) { \
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
|
||||
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
|
||||
@@ -673,8 +748,8 @@ SPL_IR_CMP_TABLE
|
||||
#undef X
|
||||
|
||||
#define X(name, kind) \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_from, \
|
||||
spl_type_id_t x_to, spl_ir_value_t x_val) { \
|
||||
spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_ir_type_id_t x_from, \
|
||||
spl_ir_type_id_t x_to, spl_ir_value_t x_val) { \
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \
|
||||
spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \
|
||||
n->cast.from_tid = x_from; \
|
||||
@@ -685,7 +760,7 @@ SPL_IR_CMP_TABLE
|
||||
SPL_IR_CAST_TABLE
|
||||
#undef X
|
||||
|
||||
spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t count) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_ALLOCA);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -694,7 +769,7 @@ spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
static usize const_pool_add(spl_ir_t *ir, spl_type_id_t tid, const void *data, usize len) {
|
||||
static usize const_pool_add(spl_ir_t *ir, spl_ir_type_id_t tid, const void *data, usize len) {
|
||||
spl_ir_const_key_t key = {.tid = tid, .data = data, .len = len};
|
||||
usize id = 0;
|
||||
if (map_get(ir->const_index, key, &id))
|
||||
@@ -717,37 +792,37 @@ static usize const_pool_add(spl_ir_t *ir, spl_type_id_t tid, const void *data, u
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, const void *data,
|
||||
spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_ir_type_id_t tid, const void *data,
|
||||
usize len) {
|
||||
usize id = const_pool_add(&b->ir, tid, data, len);
|
||||
return spl_ir_value_const_pool(tid, id);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_int(spl_type_id_t tid, uint64_t bits) {
|
||||
spl_ir_value_t spl_ir_builder_const_int(spl_ir_type_id_t tid, uint64_t bits) {
|
||||
return spl_ir_value_const_int(tid, bits);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_bool(spl_type_id_t tid, bool val) {
|
||||
spl_ir_value_t spl_ir_builder_const_bool(spl_ir_type_id_t tid, bool val) {
|
||||
return spl_ir_value_const_int(tid, val ? 1 : 0);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_float(spl_type_id_t tid, double val) {
|
||||
spl_ir_value_t spl_ir_builder_const_float(spl_ir_type_id_t tid, double val) {
|
||||
return spl_ir_value_const_float(tid, val);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_null(spl_type_id_t tid) {
|
||||
spl_ir_value_t spl_ir_builder_const_null(spl_ir_type_id_t tid) {
|
||||
return spl_ir_value_const_null(tid);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn) {
|
||||
spl_ir_value_t spl_ir_builder_const_fn(spl_ir_type_id_t tid, spl_ir_func_ref_t fn) {
|
||||
return spl_ir_value_const_fn(tid, fn);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_global_addr(spl_type_id_t ptr_tid, usize global_id) {
|
||||
spl_ir_value_t spl_ir_builder_global_addr(spl_ir_type_id_t ptr_tid, usize global_id) {
|
||||
return spl_ir_value_global(ptr_tid, global_id);
|
||||
}
|
||||
|
||||
usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_type_id_t tid,
|
||||
usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t init, bool readonly) {
|
||||
spl_ir_global_t g = {
|
||||
.name = name ? strdup(name) : NULL,
|
||||
@@ -759,7 +834,7 @@ usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_t
|
||||
return vec_size(b->ir.globals) - 1;
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr) {
|
||||
spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_ir_type_id_t tid, spl_ir_value_t ptr) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_LOAD);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->mem_load.tid = tid;
|
||||
@@ -767,7 +842,7 @@ spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, s
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t ptr, spl_ir_value_t val) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_STORE);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -777,7 +852,7 @@ spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t ti
|
||||
return ref;
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr,
|
||||
spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_ir_type_id_t tid, spl_ir_value_t ptr,
|
||||
spl_ir_value_t offset) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_OFFSET);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -787,7 +862,7 @@ spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t agg,
|
||||
spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_ir_type_id_t tid, spl_ir_value_t agg,
|
||||
usize field_idx) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_FIELD_PTR);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -797,7 +872,7 @@ spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_ir_type_id_t tid, spl_ir_value_t dst,
|
||||
spl_ir_value_t src, spl_ir_value_t size) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_COPY);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -808,7 +883,7 @@ spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid
|
||||
return ref;
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst,
|
||||
spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_ir_type_id_t tid, spl_ir_value_t dst,
|
||||
spl_ir_value_t val, spl_ir_value_t size) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_SET);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -819,28 +894,28 @@ spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
return ref;
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid) {
|
||||
spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_ir_type_id_t tid) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_BITSIZEOF);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->bitsizeof.tid = tid;
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid) {
|
||||
spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_ir_type_id_t tid) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_SIZEOF);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->ir_sizeof.tid = tid;
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid) {
|
||||
spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_ir_type_id_t tid) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_ALIGNOF);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->ir_alignof.tid = tid;
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t field_idx) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_OFFSETOF);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -849,14 +924,14 @@ spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t t
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid) {
|
||||
spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_ir_type_id_t tid) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_FIELD_COUNT);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->field_count.tid = tid;
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid) {
|
||||
spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_ir_type_id_t tid) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_CONSTRUCT);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
n->agg_construct.tid = tid;
|
||||
@@ -871,8 +946,8 @@ void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t n
|
||||
vec_push(n->agg_construct.fields, field);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_type_id_t field_tid, usize field_idx,
|
||||
spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_type_id_t field_tid, usize field_idx,
|
||||
spl_ir_value_t val) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_EXTRACT);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -883,7 +958,7 @@ spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx,
|
||||
spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_ir_type_id_t tid, usize field_idx,
|
||||
spl_ir_value_t agg, spl_ir_value_t field) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_INSERT);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -894,7 +969,7 @@ spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t cond, spl_ir_value_t true_val,
|
||||
spl_ir_value_t false_val) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_SELECT);
|
||||
@@ -946,7 +1021,7 @@ void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref
|
||||
vec_push(b->fixups, fx);
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t func) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_CALL);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -963,7 +1038,7 @@ void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t ca
|
||||
vec_push(n->control_call.params, arg);
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t idx) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_PARAM);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
@@ -972,7 +1047,7 @@ spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t t
|
||||
return spl_ir_value_inst(ref);
|
||||
}
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_ir_type_id_t tid,
|
||||
spl_ir_value_t val) {
|
||||
spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_RET);
|
||||
spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref);
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
#include "spl_ir_type.h"
|
||||
|
||||
static usize spl_type_hash(spl_ir_type_node_t n) {
|
||||
usize hash = spl_hash_seed();
|
||||
hash = spl_hash_mix(hash, (usize)n.kind);
|
||||
switch (n.kind) {
|
||||
case SPL_IR_TYPE_ERROR:
|
||||
case SPL_IR_TYPE_UNIT:
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
break;
|
||||
case SPL_IR_TYPE_INT:
|
||||
hash = spl_hash_mix(hash, n.int_type.bits);
|
||||
hash = spl_hash_mix(hash, (usize)n.int_type.is_signed);
|
||||
break;
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
hash = spl_hash_mix(hash, n.float_type.bits);
|
||||
break;
|
||||
case SPL_IR_TYPE_PTR:
|
||||
hash = spl_hash_mix(hash, n.ptr_type.pointee);
|
||||
break;
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
hash = spl_hash_mix(hash, n.array_type.element);
|
||||
hash = spl_hash_mix(hash, n.array_type.len);
|
||||
break;
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
hash = spl_hash_mix(hash, (usize)n.layout.mode);
|
||||
hash = spl_hash_mix(hash, n.layout.fixed_align_bits);
|
||||
hash = spl_hash_mix(hash, n.agg_type.tag_type);
|
||||
hash = spl_hash_mix(hash, vec_size(n.agg_type.variants));
|
||||
for (usize i = 0; i < vec_size(n.agg_type.variants); i++) {
|
||||
spl_ir_type_member_t *m = &vec_at(n.agg_type.variants, i);
|
||||
hash = spl_hash_mix(hash, m->type_id);
|
||||
hash = spl_hash_mix_str(hash, m->name);
|
||||
}
|
||||
break;
|
||||
case SPL_IR_TYPE_FN:
|
||||
hash = spl_hash_mix(hash, n.fn_type.ret);
|
||||
hash = spl_hash_mix(hash, vec_size(n.fn_type.params));
|
||||
for (usize i = 0; i < vec_size(n.fn_type.params); i++) {
|
||||
hash = spl_hash_mix(hash, vec_at(n.fn_type.params, i));
|
||||
}
|
||||
break;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
static int spl_type_eq(spl_ir_type_node_t n1, spl_ir_type_node_t n2) {
|
||||
if (n1.kind != n2.kind)
|
||||
return 1;
|
||||
switch (n1.kind) {
|
||||
case SPL_IR_TYPE_ERROR:
|
||||
case SPL_IR_TYPE_UNIT:
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
return 0;
|
||||
case SPL_IR_TYPE_INT:
|
||||
return (n1.int_type.bits != n2.int_type.bits) ||
|
||||
(n1.int_type.is_signed != n2.int_type.is_signed);
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
return n1.float_type.bits != n2.float_type.bits;
|
||||
case SPL_IR_TYPE_PTR:
|
||||
return n1.ptr_type.pointee != n2.ptr_type.pointee;
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
return (n1.array_type.element != n2.array_type.element) ||
|
||||
(n1.array_type.len != n2.array_type.len);
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
if (n1.layout.mode != n2.layout.mode ||
|
||||
n1.layout.fixed_align_bits != n2.layout.fixed_align_bits)
|
||||
return 1;
|
||||
if (n1.agg_type.tag_type != n2.agg_type.tag_type)
|
||||
return 1;
|
||||
if (vec_size(n1.agg_type.variants) != vec_size(n2.agg_type.variants))
|
||||
return 1;
|
||||
for (usize i = 0; i < vec_size(n1.agg_type.variants); i++) {
|
||||
spl_ir_type_member_t *a = &n1.agg_type.variants.data[i];
|
||||
spl_ir_type_member_t *b = &n2.agg_type.variants.data[i];
|
||||
if (a->type_id != b->type_id)
|
||||
return 1;
|
||||
if ((a->name == NULL) != (b->name == NULL))
|
||||
return 1;
|
||||
if (a->name && strcmp(a->name, b->name) != 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
case SPL_IR_TYPE_FN:
|
||||
if (n1.fn_type.ret != n2.fn_type.ret)
|
||||
return 1;
|
||||
if (vec_size(n1.fn_type.params) != vec_size(n2.fn_type.params))
|
||||
return 1;
|
||||
for (usize i = 0; i < vec_size(n1.fn_type.params); i++) {
|
||||
if (vec_at(n1.fn_type.params, i) != vec_at(n2.fn_type.params, i))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void spl_ir_type_node_free_vecs(spl_ir_type_node_t *tn) {
|
||||
switch (tn->kind) {
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM:
|
||||
vec_free(tn->agg_type.variants);
|
||||
break;
|
||||
case SPL_IR_TYPE_FN:
|
||||
vec_free(tn->fn_type.params);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* spl_fold 回调:只在链内做结构比较(同桶已保证 hash 相同)。 */
|
||||
static int spl_type_fold_eq(void *ctx, spl_fold_id_t node, const void *probe) {
|
||||
spl_ir_type_table_t *type = (spl_ir_type_table_t *)ctx;
|
||||
return spl_type_eq(vec_at(type->type_table, node), *(const spl_ir_type_node_t *)probe) == 0;
|
||||
}
|
||||
|
||||
static spl_fold_id_t spl_type_fold_next(void *ctx, spl_fold_id_t node) {
|
||||
spl_ir_type_table_t *type = (spl_ir_type_table_t *)ctx;
|
||||
return vec_at(type->type_table, node).hash_next;
|
||||
}
|
||||
|
||||
static void spl_type_fold_set_next(void *ctx, spl_fold_id_t node, spl_fold_id_t next) {
|
||||
spl_ir_type_table_t *type = (spl_ir_type_table_t *)ctx;
|
||||
vec_at(type->type_table, node).hash_next = next;
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_push(spl_ir_type_table_t *type, spl_ir_type_node_t *tn) {
|
||||
usize hash = spl_type_hash(*tn);
|
||||
|
||||
spl_ir_type_id_t existing = spl_fold_find(&type->type_fold, hash, tn);
|
||||
if (existing) {
|
||||
spl_ir_type_node_free_vecs(tn);
|
||||
return existing;
|
||||
}
|
||||
|
||||
spl_ir_type_id_t id = vec_size(type->type_table);
|
||||
vec_push(type->type_table, *tn);
|
||||
spl_fold_insert(&type->type_fold, hash, id);
|
||||
tn->kind = SPL_IR_TYPE_ERROR;
|
||||
return id;
|
||||
}
|
||||
|
||||
void spl_ir_type_table_init(spl_ir_type_table_t *type) {
|
||||
vec_init(type->type_table);
|
||||
spl_ir_type_node_t type_node = {0};
|
||||
vec_push(type->type_table, type_node);
|
||||
spl_fold_init(&type->type_fold, type, spl_type_fold_eq, spl_type_fold_next,
|
||||
spl_type_fold_set_next);
|
||||
}
|
||||
|
||||
void spl_ir_type_table_drop(spl_ir_type_table_t *type) {
|
||||
for (usize i = 0; i < vec_size(type->type_table); i++) {
|
||||
spl_ir_type_node_t *n = &vec_at(type->type_table, i);
|
||||
spl_ir_type_node_free_vecs(n);
|
||||
}
|
||||
spl_fold_drop(&type->type_fold);
|
||||
vec_free(type->type_table);
|
||||
}
|
||||
|
||||
spl_ir_type_node_t *spl_ir_type_node(const spl_ir_type_table_t *type, spl_ir_type_id_t id) {
|
||||
if (!id || id >= vec_size(type->type_table))
|
||||
return NULL;
|
||||
return &vec_at(type->type_table, id);
|
||||
}
|
||||
|
||||
static void spl_ir_type_dump_rec(spl_ir_type_table_t *type, spl_ir_type_id_t id, spl_ir_type_id_vec_t *stack) {
|
||||
for (usize i = 0; i < vec_size(*stack); i++) {
|
||||
if (vec_at(*stack, i) == id) {
|
||||
printf("<rec#%zu>", id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (vec_size(*stack) > 32) {
|
||||
printf("...");
|
||||
return;
|
||||
}
|
||||
vec_push(*stack, id);
|
||||
|
||||
spl_ir_type_node_t *n = spl_ir_type_node(type, id);
|
||||
if (!n) {
|
||||
printf("none");
|
||||
(void)vec_pop(*stack);
|
||||
return;
|
||||
}
|
||||
switch (n->kind) {
|
||||
case SPL_IR_TYPE_ERROR:
|
||||
printf("error");
|
||||
break;
|
||||
case SPL_IR_TYPE_UNIT:
|
||||
printf("unit");
|
||||
break;
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
printf("bool");
|
||||
break;
|
||||
case SPL_IR_TYPE_INT:
|
||||
if (n->int_type.bits == 0)
|
||||
printf("comptime_int");
|
||||
else
|
||||
printf("%s%zu", n->int_type.is_signed ? "i" : "u", n->int_type.bits);
|
||||
break;
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
if (n->float_type.bits == 0)
|
||||
printf("comptime_float");
|
||||
else
|
||||
printf("f%zu", n->float_type.bits);
|
||||
break;
|
||||
case SPL_IR_TYPE_PTR:
|
||||
printf("*");
|
||||
spl_ir_type_dump_rec(type, n->ptr_type.pointee, stack);
|
||||
break;
|
||||
case SPL_IR_TYPE_ARRAY:
|
||||
printf("[%zu]", n->array_type.len);
|
||||
spl_ir_type_dump_rec(type, n->array_type.element, stack);
|
||||
break;
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM: {
|
||||
const char *kw = "struct";
|
||||
if (n->kind == SPL_IR_TYPE_UNION)
|
||||
kw = "union";
|
||||
else if (n->kind == SPL_IR_TYPE_ENUM)
|
||||
kw = "enum";
|
||||
printf("%s{", kw);
|
||||
for (usize i = 0; i < vec_size(n->agg_type.variants); i++) {
|
||||
if (i)
|
||||
printf(", ");
|
||||
spl_ir_type_member_t *m = &n->agg_type.variants.data[i];
|
||||
printf("%s:", m->name ? m->name : "?");
|
||||
spl_ir_type_dump_rec(type, m->type_id, stack);
|
||||
}
|
||||
printf("}");
|
||||
break;
|
||||
}
|
||||
case SPL_IR_TYPE_FN:
|
||||
printf("fn(");
|
||||
for (usize i = 0; i < vec_size(n->fn_type.params); i++) {
|
||||
if (i)
|
||||
printf(",");
|
||||
spl_ir_type_dump_rec(type, vec_at(n->fn_type.params, i), stack);
|
||||
}
|
||||
printf(")");
|
||||
spl_ir_type_dump_rec(type, n->fn_type.ret, stack);
|
||||
break;
|
||||
}
|
||||
|
||||
(void)vec_pop(*stack);
|
||||
}
|
||||
|
||||
void spl_ir_type_dump(spl_ir_type_table_t *type, spl_ir_type_id_t id) {
|
||||
spl_ir_type_id_vec_t stack;
|
||||
vec_init(stack);
|
||||
spl_ir_type_dump_rec(type, id, &stack);
|
||||
vec_free(stack);
|
||||
}
|
||||
|
||||
static const spl_ir_type_member_t *spl_ir_type_member_find(spl_ir_type_table_t *type, spl_ir_type_id_t tid,
|
||||
const char *name, usize *out_idx) {
|
||||
spl_ir_type_node_t *n = spl_ir_type_node(type, tid);
|
||||
if (!n || !name)
|
||||
return NULL;
|
||||
if (n->kind == SPL_IR_TYPE_STRUCT || n->kind == SPL_IR_TYPE_UNION ||
|
||||
n->kind == SPL_IR_TYPE_ENUM) {
|
||||
for (usize i = 0; i < vec_size(n->agg_type.variants); i++) {
|
||||
spl_ir_type_member_t *m = &n->agg_type.variants.data[i];
|
||||
if (m->name && strcmp(m->name, name) == 0) {
|
||||
if (out_idx)
|
||||
*out_idx = i;
|
||||
return m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const spl_ir_type_member_t *spl_ir_type_member(spl_ir_type_table_t *type, spl_ir_type_id_t tid,
|
||||
const char *name) {
|
||||
return spl_ir_type_member_find(type, tid, name, NULL);
|
||||
}
|
||||
|
||||
bool spl_ir_type_member_idx(spl_ir_type_table_t *type, spl_ir_type_id_t tid, const char *name,
|
||||
usize *out_idx) {
|
||||
return spl_ir_type_member_find(type, tid, name, out_idx) != NULL;
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_simple(spl_ir_type_table_t *type, spl_ir_type_kind_t kind) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = kind;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_int(spl_ir_type_table_t *type, usize bits, int is_signed) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = SPL_IR_TYPE_INT;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
tn.int_type.bits = bits;
|
||||
tn.int_type.is_signed = is_signed;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_float(spl_ir_type_table_t *type, usize bits) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = SPL_IR_TYPE_FLOAT;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
tn.float_type.bits = bits;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_ptr(spl_ir_type_table_t *type, spl_ir_type_id_t pointee) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = SPL_IR_TYPE_PTR;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
tn.ptr_type.pointee = pointee;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_array(spl_ir_type_table_t *type, spl_ir_type_id_t element, usize len) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = SPL_IR_TYPE_ARRAY;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
tn.array_type.element = element;
|
||||
tn.array_type.len = len;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_fn(spl_ir_type_table_t *type, spl_ir_type_id_t ret, const spl_ir_type_id_t *params,
|
||||
usize nparams) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = SPL_IR_TYPE_FN;
|
||||
tn.layout.mode = SPL_IR_TYPE_LAYOUT_AUTO;
|
||||
vec_init(tn.fn_type.params);
|
||||
for (usize i = 0; i < nparams; i++) {
|
||||
vec_push(tn.fn_type.params, params[i]);
|
||||
}
|
||||
tn.fn_type.ret = ret;
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_ir_type_id_t spl_ir_type_builder_agg(spl_ir_type_table_t *type, spl_ir_type_kind_t tk,
|
||||
spl_ir_type_member_vec_t *variants, spl_ir_type_id_t tag_type,
|
||||
spl_ir_type_layout_t layout) {
|
||||
spl_ir_type_node_t tn = {0};
|
||||
tn.kind = tk;
|
||||
tn.layout = layout;
|
||||
tn.agg_type.variants = *variants;
|
||||
vec_init(*variants);
|
||||
if (tk == SPL_IR_TYPE_ENUM) {
|
||||
tn.agg_type.tag_type = tag_type;
|
||||
} else {
|
||||
tn.agg_type.tag_type = 0;
|
||||
}
|
||||
return spl_ir_type_push(type, &tn);
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
#include "spl_layout.h"
|
||||
|
||||
static void infos_grow(spl_layout_t *layout, usize n) {
|
||||
while (vec_size(layout->infos) < n) {
|
||||
spl_layout_info_t info = {0};
|
||||
vec_init(info.field_bit_offsets);
|
||||
vec_init(info.field_bit_sizes);
|
||||
vec_push(layout->infos, info);
|
||||
}
|
||||
}
|
||||
|
||||
static spl_layout_info_t *infos_at(spl_layout_t *layout, spl_ir_type_id_t id) {
|
||||
if (id == 0 || id >= vec_size(layout->infos))
|
||||
return NULL;
|
||||
return &vec_at(layout->infos, id);
|
||||
}
|
||||
|
||||
void spl_layout_add_scalar(spl_layout_t *layout, usize size, usize abi_align) {
|
||||
spl_layout_scalar_t s = {0};
|
||||
s.size = size;
|
||||
s.abi_align = abi_align;
|
||||
s.pref_align = abi_align;
|
||||
vec_init(s.invalid_values);
|
||||
vec_push(layout->scalars, s);
|
||||
}
|
||||
|
||||
void spl_layout_add_ptr(spl_layout_t *layout, usize addr_space, usize size, usize align) {
|
||||
spl_layout_ptr_t p = {0};
|
||||
p.add_space = addr_space;
|
||||
p.ptr_size = size;
|
||||
p.ptr_align = align;
|
||||
vec_push(layout->pointers, p);
|
||||
}
|
||||
|
||||
void spl_layout_init(spl_layout_t *layout, spl_layout_target_t *target) {
|
||||
vec_init(layout->scalars);
|
||||
vec_init(layout->pointers);
|
||||
vec_init(layout->infos);
|
||||
layout->target = target;
|
||||
|
||||
/* 默认聚合策略:按最大字段对齐,不允许重排 */
|
||||
layout->aggregate.allow_field_reordering = false;
|
||||
layout->aggregate.align_rule = ALIGN_MAX;
|
||||
layout->aggregate.fixed_align = 0;
|
||||
|
||||
/* 常见 64 位平台的默认布局。
|
||||
若目标平台不适用,调用者 clear 后自行 add_*。 */
|
||||
spl_layout_add_scalar(layout, 1, 1); /* i8 / u8 / bool */
|
||||
spl_layout_add_scalar(layout, 2, 2); /* i16 / u16 */
|
||||
spl_layout_add_scalar(layout, 4, 4); /* i32 / u32 / f32 */
|
||||
spl_layout_add_scalar(layout, 8, 8); /* i64 / u64 / f64 */
|
||||
|
||||
usize ps = target && target->ptr_size ? target->ptr_size : sizeof(void *);
|
||||
usize pa = target && target->ptr_align ? target->ptr_align : sizeof(void *);
|
||||
spl_layout_add_ptr(layout, 0, ps, pa);
|
||||
}
|
||||
|
||||
void spl_layout_drop(spl_layout_t *layout) {
|
||||
for (usize i = 0; i < vec_size(layout->scalars); ++i)
|
||||
vec_free(vec_at(layout->scalars, i).invalid_values);
|
||||
vec_free(layout->scalars);
|
||||
|
||||
vec_free(layout->pointers);
|
||||
|
||||
for (usize i = 0; i < vec_size(layout->infos); ++i) {
|
||||
vec_free(vec_at(layout->infos, i).field_bit_offsets);
|
||||
vec_free(vec_at(layout->infos, i).field_bit_sizes);
|
||||
}
|
||||
vec_free(layout->infos);
|
||||
}
|
||||
|
||||
static const spl_layout_scalar_t *scalar_for_size(spl_layout_t *layout, usize size_bytes) {
|
||||
for (usize i = 0; i < vec_size(layout->scalars); ++i) {
|
||||
spl_layout_scalar_t *s = &vec_at(layout->scalars, i);
|
||||
if (s->size == size_bytes)
|
||||
return s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const spl_layout_ptr_t *default_ptr(spl_layout_t *layout) {
|
||||
if (vec_size(layout->pointers) == 0)
|
||||
return NULL;
|
||||
return &vec_at(layout->pointers, 0);
|
||||
}
|
||||
|
||||
static bool layout_scalar(spl_layout_t *layout, spl_ir_type_node_t *tn, spl_layout_info_t *out) {
|
||||
usize size_bits = 0;
|
||||
switch (tn->kind) {
|
||||
case SPL_IR_TYPE_INT:
|
||||
if (tn->int_type.bits == 0)
|
||||
return false; /* comptime_int 未落地 */
|
||||
size_bits = tn->int_type.bits;
|
||||
break;
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
if (tn->float_type.bits == 0)
|
||||
return false; /* comptime_float 未落地 */
|
||||
size_bits = tn->float_type.bits;
|
||||
break;
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
size_bits = 8;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
usize size_bytes = (size_bits + 7) / 8;
|
||||
const spl_layout_scalar_t *sl = scalar_for_size(layout, size_bytes);
|
||||
if (!sl)
|
||||
return false;
|
||||
|
||||
out->size_bits = sl->size * 8;
|
||||
out->align_bits = sl->abi_align * 8;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool layout_ptr(spl_layout_t *layout, spl_layout_info_t *out) {
|
||||
const spl_layout_ptr_t *pl = default_ptr(layout);
|
||||
if (!pl)
|
||||
return false;
|
||||
out->size_bits = pl->ptr_size * 8;
|
||||
out->align_bits = pl->ptr_align * 8;
|
||||
return true;
|
||||
}
|
||||
|
||||
static usize round_up_bits(usize x, usize align_bits) {
|
||||
if (align_bits <= 1)
|
||||
return x;
|
||||
usize a = align_bits;
|
||||
return (x + a - 1) / a * a;
|
||||
}
|
||||
|
||||
static bool layout_agg(spl_layout_t *layout, spl_ir_type_node_t *tn, spl_layout_info_t *out) {
|
||||
usize n = vec_size(tn->agg_type.variants);
|
||||
|
||||
if (tn->kind == SPL_IR_TYPE_ENUM) {
|
||||
spl_ir_type_id_t tag_tid = tn->agg_type.tag_type;
|
||||
if (!tag_tid)
|
||||
return false;
|
||||
spl_layout_info_t *tag_info = infos_at(layout, tag_tid);
|
||||
if (!tag_info || !tag_info->resolved)
|
||||
return false;
|
||||
|
||||
usize tag_align_bits = tag_info->align_bits ? tag_info->align_bits : 8;
|
||||
usize payload_align_bits = 8;
|
||||
usize payload_size_bits = 0;
|
||||
|
||||
for (usize i = 0; i < n; ++i) {
|
||||
spl_ir_type_member_t *m = &vec_at(tn->agg_type.variants, i);
|
||||
if (m->type_id == 0) /* 无载荷变体 */
|
||||
continue;
|
||||
spl_layout_info_t *fi = infos_at(layout, m->type_id);
|
||||
if (!fi || !fi->resolved)
|
||||
return false;
|
||||
usize fa = fi->align_bits ? fi->align_bits : 8;
|
||||
if (fa > payload_align_bits)
|
||||
payload_align_bits = fa;
|
||||
if (fi->size_bits > payload_size_bits)
|
||||
payload_size_bits = fi->size_bits;
|
||||
}
|
||||
|
||||
usize payload_off = round_up_bits(tag_info->size_bits, payload_align_bits);
|
||||
usize final_align_bits =
|
||||
tag_align_bits > payload_align_bits ? tag_align_bits : payload_align_bits;
|
||||
out->size_bits = round_up_bits(payload_off + payload_size_bits, final_align_bits);
|
||||
out->align_bits = final_align_bits;
|
||||
|
||||
/* 字段索引约定(与 ir2vm 一致):0 = tag,1..n = 各变体载荷(同一偏移)。 */
|
||||
vec_push(out->field_bit_offsets, 0);
|
||||
vec_push(out->field_bit_sizes, tag_info->size_bits);
|
||||
for (usize i = 0; i < n; ++i) {
|
||||
spl_ir_type_member_t *m = &vec_at(tn->agg_type.variants, i);
|
||||
usize fsize = 0;
|
||||
if (m->type_id) {
|
||||
spl_layout_info_t *fi = infos_at(layout, m->type_id);
|
||||
if (fi)
|
||||
fsize = fi->size_bits;
|
||||
}
|
||||
vec_push(out->field_bit_offsets, payload_off);
|
||||
vec_push(out->field_bit_sizes, fsize);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
out->size_bits = 0;
|
||||
out->align_bits = 8;
|
||||
return true;
|
||||
}
|
||||
|
||||
usize max_align_bits = 8;
|
||||
usize offset_bits = 0;
|
||||
bool is_union = (tn->kind == SPL_IR_TYPE_UNION);
|
||||
|
||||
for (usize i = 0; i < n; ++i) {
|
||||
spl_ir_type_member_t *m = &vec_at(tn->agg_type.variants, i);
|
||||
usize fsize_bits = 0;
|
||||
usize fsize_align = 8;
|
||||
if (m->type_id) {
|
||||
spl_layout_info_t *fi = infos_at(layout, m->type_id);
|
||||
if (!fi || !fi->resolved)
|
||||
return false;
|
||||
fsize_bits = fi->size_bits;
|
||||
fsize_align = fi->align_bits ? fi->align_bits : 8;
|
||||
}
|
||||
usize field_align_bits = fsize_align;
|
||||
|
||||
if (is_union) {
|
||||
vec_push(out->field_bit_offsets, 0);
|
||||
vec_push(out->field_bit_sizes, fsize_bits);
|
||||
if (fsize_bits > out->size_bits)
|
||||
out->size_bits = fsize_bits;
|
||||
} else {
|
||||
offset_bits = round_up_bits(offset_bits, field_align_bits);
|
||||
vec_push(out->field_bit_offsets, offset_bits);
|
||||
vec_push(out->field_bit_sizes, fsize_bits);
|
||||
offset_bits += fsize_bits;
|
||||
}
|
||||
if (field_align_bits > max_align_bits)
|
||||
max_align_bits = field_align_bits;
|
||||
}
|
||||
|
||||
usize final_align_bits = max_align_bits;
|
||||
if (layout->aggregate.align_rule == ALIGN_FIXED) {
|
||||
final_align_bits = layout->aggregate.fixed_align * 8;
|
||||
} else if (layout->aggregate.align_rule == ALIGN_MAX_WITH_MIN) {
|
||||
usize min_bits = layout->aggregate.fixed_align * 8;
|
||||
if (final_align_bits < min_bits)
|
||||
final_align_bits = min_bits;
|
||||
}
|
||||
|
||||
if (is_union)
|
||||
out->size_bits = round_up_bits(out->size_bits, final_align_bits);
|
||||
else
|
||||
out->size_bits = round_up_bits(offset_bits, final_align_bits);
|
||||
out->align_bits = final_align_bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void resolve_type(spl_layout_t *layout, const spl_ir_type_table_t *type, spl_ir_type_id_t id) {
|
||||
spl_layout_info_t *info = infos_at(layout, id);
|
||||
if (!info || info->resolved)
|
||||
return;
|
||||
|
||||
spl_ir_type_node_t *tn = spl_ir_type_node(type, id);
|
||||
if (!tn)
|
||||
return;
|
||||
|
||||
/* 先标记已解析:自引用类型(通过 ptr)不会无限递归,
|
||||
但保持防御性写法。 */
|
||||
info->resolved = true;
|
||||
|
||||
switch (tn->kind) {
|
||||
case SPL_IR_TYPE_ERROR:
|
||||
case SPL_IR_TYPE_UNIT:
|
||||
info->size_bits = 0;
|
||||
info->align_bits = 8;
|
||||
return;
|
||||
|
||||
case SPL_IR_TYPE_BOOL:
|
||||
case SPL_IR_TYPE_INT:
|
||||
case SPL_IR_TYPE_FLOAT:
|
||||
layout_scalar(layout, tn, info);
|
||||
return;
|
||||
|
||||
case SPL_IR_TYPE_PTR:
|
||||
layout_ptr(layout, info);
|
||||
return;
|
||||
|
||||
case SPL_IR_TYPE_ARRAY: {
|
||||
resolve_type(layout, type, tn->array_type.element);
|
||||
spl_layout_info_t *elem = infos_at(layout, tn->array_type.element);
|
||||
if (!elem || !elem->resolved)
|
||||
return;
|
||||
info->size_bits = elem->size_bits * tn->array_type.len;
|
||||
info->align_bits = elem->align_bits;
|
||||
return;
|
||||
}
|
||||
|
||||
case SPL_IR_TYPE_STRUCT:
|
||||
case SPL_IR_TYPE_UNION:
|
||||
case SPL_IR_TYPE_ENUM: {
|
||||
if (tn->kind == SPL_IR_TYPE_ENUM && tn->agg_type.tag_type)
|
||||
resolve_type(layout, type, tn->agg_type.tag_type);
|
||||
for (usize i = 0; i < vec_size(tn->agg_type.variants); ++i) {
|
||||
spl_ir_type_member_t *m = &vec_at(tn->agg_type.variants, i);
|
||||
if (m->type_id)
|
||||
resolve_type(layout, type, m->type_id);
|
||||
}
|
||||
layout_agg(layout, tn, info);
|
||||
return;
|
||||
}
|
||||
|
||||
case SPL_IR_TYPE_FN:
|
||||
/* 函数类型按函数指针布局 */
|
||||
for (usize i = 0; i < vec_size(tn->fn_type.params); ++i)
|
||||
resolve_type(layout, type, vec_at(tn->fn_type.params, i));
|
||||
resolve_type(layout, type, tn->fn_type.ret);
|
||||
layout_ptr(layout, info);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void spl_layout_resolve(spl_layout_t *layout, const spl_ir_type_table_t *type) {
|
||||
infos_grow(layout, vec_size(type->type_table));
|
||||
for (usize i = 0; i < vec_size(layout->infos); ++i) {
|
||||
spl_layout_info_t *info = &vec_at(layout->infos, i);
|
||||
vec_free(info->field_bit_offsets);
|
||||
vec_free(info->field_bit_sizes);
|
||||
vec_init(info->field_bit_offsets);
|
||||
vec_init(info->field_bit_sizes);
|
||||
info->size_bits = 0;
|
||||
info->align_bits = 0;
|
||||
info->resolved = false;
|
||||
}
|
||||
for (usize i = 1; i < vec_size(type->type_table); ++i)
|
||||
resolve_type(layout, type, i);
|
||||
}
|
||||
|
||||
const spl_layout_info_t *spl_layout_info_of(spl_layout_t *layout, spl_ir_type_id_t id) {
|
||||
spl_layout_info_t *info = infos_at(layout, id);
|
||||
if (!info || !info->resolved)
|
||||
return NULL;
|
||||
return info;
|
||||
}
|
||||
Reference in new issue
Block a user