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
+275
-207
File diff suppressed because it is too large.
Load diff
@@ -83,14 +83,14 @@ spl_intrin_t spl_intrin_lookup_field(spl_lang_type_kind_t recv_kind, const char
|
||||
return SPL_INTRIN_NONE;
|
||||
}
|
||||
|
||||
spl_intrin_t spl_intrin_lookup_cast(spl_type_t *type, spl_type_id_t ft, spl_type_id_t tt) {
|
||||
spl_intrin_t spl_intrin_lookup_cast(spl_ir_type_table_t *type, spl_ir_type_id_t ft, spl_ir_type_id_t tt) {
|
||||
/* 这是 `as` 的展开规则表。要调整 as 的语义,改这里。 */
|
||||
spl_type_node_t *f = spl_type_node(type, ft);
|
||||
spl_type_node_t *t = spl_type_node(type, tt);
|
||||
spl_ir_type_node_t *f = spl_ir_type_node(type, ft);
|
||||
spl_ir_type_node_t *t = spl_ir_type_node(type, tt);
|
||||
if (!f || !t)
|
||||
return SPL_INTRIN_NONE;
|
||||
|
||||
if (f->kind == SPL_TYPE_INT && t->kind == SPL_TYPE_INT) {
|
||||
if (f->kind == SPL_IR_TYPE_INT && t->kind == SPL_IR_TYPE_INT) {
|
||||
usize fb = f->int_type.bits;
|
||||
usize tb = t->int_type.bits;
|
||||
if (fb == tb)
|
||||
@@ -99,15 +99,15 @@ spl_intrin_t spl_intrin_lookup_cast(spl_type_t *type, spl_type_id_t ft, spl_type
|
||||
return SPL_INTRIN_TRUNCATE;
|
||||
return SPL_INTRIN_INT_CAST;
|
||||
}
|
||||
if (f->kind == SPL_TYPE_FLOAT && t->kind == SPL_TYPE_FLOAT)
|
||||
if (f->kind == SPL_IR_TYPE_FLOAT && t->kind == SPL_IR_TYPE_FLOAT)
|
||||
return SPL_INTRIN_FLOAT_CAST;
|
||||
if (f->kind == SPL_TYPE_BOOL && t->kind == SPL_TYPE_INT)
|
||||
if (f->kind == SPL_IR_TYPE_BOOL && t->kind == SPL_IR_TYPE_INT)
|
||||
return SPL_INTRIN_BOOL2INT;
|
||||
if (f->kind == SPL_TYPE_PTR && t->kind == SPL_TYPE_INT)
|
||||
if (f->kind == SPL_IR_TYPE_PTR && t->kind == SPL_IR_TYPE_INT)
|
||||
return SPL_INTRIN_PTR2INT;
|
||||
if (f->kind == SPL_TYPE_INT && t->kind == SPL_TYPE_PTR)
|
||||
if (f->kind == SPL_IR_TYPE_INT && t->kind == SPL_IR_TYPE_PTR)
|
||||
return SPL_INTRIN_INT2PTR;
|
||||
if (f->kind == SPL_TYPE_PTR && t->kind == SPL_TYPE_PTR)
|
||||
if (f->kind == SPL_IR_TYPE_PTR && t->kind == SPL_IR_TYPE_PTR)
|
||||
return SPL_INTRIN_PTR_CAST;
|
||||
|
||||
/* int float 刻意不在这里:只能通过显式 builtin 调用。
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#include "spl_ir_link.h"
|
||||
|
||||
#include "spl_ast2ir.h"
|
||||
#include "spl_lang_type.h"
|
||||
#include "spl_sema.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
usize def_id;
|
||||
spl_ir_func_ref_t fn;
|
||||
} spl_ir_link_fn_entry_t;
|
||||
typedef VEC(spl_ir_link_fn_entry_t) spl_ir_link_fn_vec_t;
|
||||
|
||||
struct spl_ir_link {
|
||||
spl_ir_t *ir;
|
||||
spl_sema_t *sema;
|
||||
spl_ir_link_fn_vec_t fns;
|
||||
};
|
||||
|
||||
spl_ir_link_t *spl_ir_link_create(spl_ir_t *ir, spl_sema_t *sema) {
|
||||
if (!ir || !sema)
|
||||
return NULL;
|
||||
spl_ir_link_t *link = calloc(1, sizeof(*link));
|
||||
if (!link)
|
||||
return NULL;
|
||||
link->ir = ir;
|
||||
link->sema = sema;
|
||||
vec_init(link->fns);
|
||||
return link;
|
||||
}
|
||||
|
||||
void spl_ir_link_destroy(spl_ir_link_t *link) {
|
||||
if (!link)
|
||||
return;
|
||||
vec_free(link->fns);
|
||||
free(link);
|
||||
}
|
||||
|
||||
static spl_ir_link_fn_entry_t *link_find_def(spl_ir_link_t *link, usize def_id) {
|
||||
for (usize i = 0; i < vec_size(link->fns); ++i) {
|
||||
spl_ir_link_fn_entry_t *entry = &vec_at(link->fns, i);
|
||||
if (entry->def_id == def_id)
|
||||
return entry;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static spl_ir_link_fn_entry_t *link_find_fn(spl_ir_link_t *link, spl_ir_func_ref_t fn) {
|
||||
for (usize i = 0; i < vec_size(link->fns); ++i) {
|
||||
spl_ir_link_fn_entry_t *entry = &vec_at(link->fns, i);
|
||||
if (entry->fn == fn)
|
||||
return entry;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spl_ir_func_ref_t spl_ir_link_reserve_fn(spl_ir_link_t *link, usize def_id) {
|
||||
if (!link || !def_id)
|
||||
return 0;
|
||||
spl_ir_link_fn_entry_t *existing = link_find_def(link, def_id);
|
||||
if (existing)
|
||||
return existing->fn;
|
||||
|
||||
spl_def_node_t *def = spl_def_node(link->sema->type, def_id);
|
||||
if (!def || def->kind != SPL_DEF_FN || !def->owner_ast)
|
||||
return 0;
|
||||
|
||||
spl_lang_type_id_t fn_lang = def->lang_id;
|
||||
if (!fn_lang)
|
||||
return 0;
|
||||
spl_lang_type_node_t *fn_ln = spl_lang_type_node(link->sema->type, fn_lang);
|
||||
if (!fn_ln || fn_ln->kind != SPL_LANG_TYPE_FN || !fn_ln->ir_type_id)
|
||||
return 0;
|
||||
|
||||
char name_buf[1024];
|
||||
const char *name = def->name;
|
||||
spl_ast_node_t *fn_node = spl_ast_node(def->owner_ast, def->ast_node_ref);
|
||||
if (fn_node && fn_node->kind == SPL_AST_FN_DECL &&
|
||||
spl_ast2ir_fn_name(link->sema, link->sema->attr, def->owner_ast, fn_node, name_buf,
|
||||
sizeof(name_buf)))
|
||||
name = name_buf;
|
||||
|
||||
spl_ir_func_ref_t fn = spl_ir_alloc_fn(link->ir);
|
||||
spl_ir_func_t *irf = spl_ir_func(link->ir, fn);
|
||||
if (!irf)
|
||||
return 0;
|
||||
irf->name = name ? strdup(name) : NULL;
|
||||
vec_init(irf->attrs);
|
||||
irf->fn_tid = fn_ln->ir_type_id;
|
||||
if (fn_node && fn_node->kind == SPL_AST_FN_DECL && link->sema->attr) {
|
||||
for (usize i = 0; i < vec_size(fn_node->attrs); ++i) {
|
||||
spl_attr_node_t *an = spl_attr_node(link->sema->attr, vec_at(fn_node->attrs, i));
|
||||
if (an && an->info && an->info->name && strcmp(an->info->name, "extern") == 0) {
|
||||
irf->extern_fn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spl_ir_link_fn_entry_t entry = {.def_id = def_id, .fn = fn};
|
||||
vec_push(link->fns, entry);
|
||||
return fn;
|
||||
}
|
||||
|
||||
spl_ir_value_t spl_ir_link_fn_value(spl_ir_link_t *link, usize def_id) {
|
||||
spl_ir_func_ref_t fn = spl_ir_link_reserve_fn(link, def_id);
|
||||
if (!fn)
|
||||
return spl_ir_value_none();
|
||||
spl_ir_link_fn_entry_t *entry = link_find_fn(link, fn);
|
||||
if (!entry)
|
||||
return spl_ir_value_none();
|
||||
spl_def_node_t *def = spl_def_node(link->sema->type, entry->def_id);
|
||||
if (!def || !def->lang_id)
|
||||
return spl_ir_value_none();
|
||||
spl_lang_type_node_t *fn_ln = spl_lang_type_node(link->sema->type, def->lang_id);
|
||||
if (!fn_ln || !fn_ln->ir_type_id)
|
||||
return spl_ir_value_none();
|
||||
return spl_ir_value_const_fn(fn_ln->ir_type_id, fn);
|
||||
}
|
||||
|
||||
bool spl_ir_link_def_of_fn(const spl_ir_link_t *link, spl_ir_func_ref_t fn, usize *out_def) {
|
||||
if (!link || !fn)
|
||||
return false;
|
||||
for (usize i = 0; i < vec_size(link->fns); ++i) {
|
||||
const spl_ir_link_fn_entry_t *entry = &vec_at(link->fns, i);
|
||||
if (entry->fn == fn) {
|
||||
if (out_def)
|
||||
*out_def = entry->def_id;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user