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

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

No files matched your search

+260
View File
@@ -0,0 +1,260 @@
#include "spl_sap.h"
#include <stdlib.h>
#include <string.h>
typedef struct {
spl_ast_t *ast;
spl_ast_node_ref_t ref;
} sap_handle_entry_t;
typedef VEC(sap_handle_entry_t) sap_handle_vec_t;
typedef struct {
spl_sap_t *sap;
const spl_sap_op_t *op;
} sap_binding_t;
typedef VEC(sap_binding_t) sap_binding_vec_t;
struct spl_sap {
spl_lang_type_t *type;
spl_lang_type_id_t meta_view_tid;
sap_handle_vec_t handles;
sap_binding_vec_t bindings;
};
static bool sap_host_kind_count(spl_sap_t *sap, spl_exec_t *exec, const spl_exec_value_t *args,
usize nargs, spl_exec_value_t *ret) {
(void)sap;
(void)exec;
(void)args;
if (nargs != 0)
return false;
ret->bits = spl_sap_kind_count();
return true;
}
static bool sap_host_kind(spl_sap_t *sap, spl_exec_t *exec, const spl_exec_value_t *args,
usize nargs, spl_exec_value_t *ret) {
(void)exec;
if (nargs != 1)
return false;
ret->bits = 0;
spl_sap_handle_t handle = (spl_sap_handle_t)args[0].bits;
if (!handle)
return true;
spl_ast_t *ast = NULL;
spl_ast_node_ref_t ref = 0;
if (!spl_sap_handle_node(sap, handle, &ast, &ref))
return false;
spl_ast_node_t *node = spl_ast_node(ast, ref);
if (!node)
return false;
ret->bits = (usize)node->kind;
return true;
}
static bool sap_write_usize(spl_exec_value_t *ret, usize bit_offset, usize value) {
usize off = bit_offset / 8;
if (!ret || !ret->mem.ptr || off + sizeof(value) > ret->size)
return false;
memcpy((unsigned char *)ret->mem.ptr + off, &value, sizeof(value));
return true;
}
static bool sap_host_view(spl_sap_t *sap, spl_exec_t *exec, const spl_exec_value_t *args,
usize nargs, spl_exec_value_t *ret) {
if (nargs != 1 || !ret || !ret->mem.ptr || !ret->size)
return false;
spl_layout_t *layout = spl_exec_layout(exec);
const spl_layout_info_t *info = layout ? spl_layout_info_of(layout, ret->type) : NULL;
if (!info || vec_size(info->field_bit_offsets) < 3)
return false;
memset(ret->mem.ptr, 0, ret->size);
spl_sap_handle_t handle = (spl_sap_handle_t)args[0].bits;
if (!handle)
return true;
spl_ast_t *ast = NULL;
spl_ast_node_ref_t ref = 0;
if (!spl_sap_handle_node(sap, handle, &ast, &ref))
return false;
spl_ast_node_t *node = spl_ast_node(ast, ref);
if (!node)
return false;
usize values[3] = {(usize)node->kind, (usize)node->dbg.line, (usize)node->dbg.col};
for (usize i = 0; i < 3; ++i) {
if (!sap_write_usize(ret, vec_at(info->field_bit_offsets, i), values[i]))
return false;
}
return true;
}
static const spl_sap_op_t g_sap_ops[] = {
{"sap.kind_count", 0, {0}, SPL_SAP_ARG_USIZE, sap_host_kind_count},
{"sap.kind", 1, {SPL_SAP_ARG_HANDLE}, SPL_SAP_ARG_USIZE, sap_host_kind},
{"sap.view", 1, {SPL_SAP_ARG_HANDLE}, SPL_SAP_ARG_VIEW, sap_host_view},
};
static const usize g_sap_op_count = sizeof(g_sap_ops) / sizeof(g_sap_ops[0]);
const spl_sap_op_t *spl_sap_lookup(const char *name) {
if (!name)
return NULL;
for (usize i = 0; i < g_sap_op_count; ++i) {
if (strcmp(g_sap_ops[i].name, name) == 0)
return &g_sap_ops[i];
}
return NULL;
}
const spl_sap_op_t *spl_sap_ops(usize *count) {
if (count)
*count = g_sap_op_count;
return g_sap_ops;
}
spl_lang_type_id_t spl_sap_meta_view_type(spl_sap_t *sap) {
if (!sap || !sap->type)
return 0;
if (sap->meta_view_tid)
return sap->meta_view_tid;
static const char *const names[] = {"kind", "line", "col"};
spl_lang_type_id_t usize_tid = spl_lang_make_int(sap->type, sizeof(void *) * 8, 0);
spl_lang_type_id_t types[3] = {usize_tid, usize_tid, usize_tid};
sap->meta_view_tid = spl_lang_make_struct_named(sap->type, "sap.MetaView", names, types, 3);
return sap->meta_view_tid;
}
const char *const *spl_sap_meta_view_fields(usize *count) {
static const char *const fields[] = {"kind", "line", "col"};
if (count)
*count = 3;
return fields;
}
spl_lang_type_id_t spl_sap_arg_type(spl_sap_t *sap, spl_sap_arg_kind_t kind) {
if (!sap || !sap->type)
return 0;
if (kind == SPL_SAP_ARG_VIEW)
return spl_sap_meta_view_type(sap);
/* Handle 与 usize 在编译期值表示上相同;ViewPoint 再区分。 */
return spl_lang_make_int(sap->type, sizeof(void *) * 8, 0);
}
spl_lang_type_id_t spl_sap_op_fn_type(spl_sap_t *sap, const spl_sap_op_t *op) {
if (!sap || !sap->type || !op || op->nparams > 4)
return 0;
spl_lang_type_id_t params[4];
for (usize i = 0; i < op->nparams; ++i)
params[i] = spl_sap_arg_type(sap, op->params[i]);
spl_lang_type_id_t ret = spl_sap_arg_type(sap, op->ret);
return spl_lang_make_fn(sap->type, ret, params, op->nparams);
}
usize spl_sap_kind_count(void) { return (usize)SPL_AST_COUNT; }
const char *spl_sap_kind_name(usize kind) {
if (kind >= spl_sap_kind_count())
return "?";
return spl_ast_kind_name((spl_ast_node_kind_t)kind);
}
spl_sap_t *spl_sap_create(spl_lang_type_t *type) {
if (!type)
return NULL;
spl_sap_t *sap = calloc(1, sizeof(*sap));
if (!sap)
return NULL;
sap->type = type;
vec_init(sap->handles);
vec_init(sap->bindings);
/* 0 号句柄保留为空。 */
sap_handle_entry_t none = {0};
vec_push(sap->handles, none);
return sap;
}
void spl_sap_destroy(spl_sap_t *sap) {
if (!sap)
return;
vec_free(sap->handles);
vec_free(sap->bindings);
free(sap);
}
spl_sap_handle_t spl_sap_handle_register(spl_sap_t *sap, spl_ast_t *ast,
spl_ast_node_ref_t ref) {
if (!sap || !ast || !ref)
return 0;
for (usize i = 1; i < vec_size(sap->handles); ++i) {
sap_handle_entry_t *entry = &vec_at(sap->handles, i);
if (entry->ast == ast && entry->ref == ref)
return i;
}
sap_handle_entry_t entry = {.ast = ast, .ref = ref};
vec_push(sap->handles, entry);
return vec_size(sap->handles) - 1;
}
bool spl_sap_handle_node(spl_sap_t *sap, spl_sap_handle_t handle, spl_ast_t **ast,
spl_ast_node_ref_t *ref) {
if (!sap || !handle || handle >= vec_size(sap->handles))
return false;
sap_handle_entry_t *entry = &vec_at(sap->handles, handle);
if (ast)
*ast = entry->ast;
if (ref)
*ref = entry->ref;
return true;
}
bool spl_sap_type_lookup(spl_sap_t *sap, const char *name, spl_lang_type_id_t *out) {
if (!sap || !name || !out)
return false;
if (strcmp(name, "Handle") == 0) {
*out = spl_sap_arg_type(sap, SPL_SAP_ARG_HANDLE);
return *out != 0;
}
if (strcmp(name, "MetaView") == 0) {
*out = spl_sap_meta_view_type(sap);
return *out != 0;
}
return false;
}
static bool sap_host_adapter(spl_exec_t *exec, void *user, const spl_exec_value_t *args,
usize nargs, spl_exec_value_t *ret) {
sap_binding_t *binding = (sap_binding_t *)user;
if (!binding || !binding->op || !binding->op->host)
return false;
return binding->op->host(binding->sap, exec, args, nargs, ret);
}
bool spl_sap_bind(spl_exec_t *exec, spl_sap_t *sap) {
if (!exec || !sap)
return false;
sap->bindings.size = 0;
for (usize i = 0; i < g_sap_op_count; ++i) {
sap_binding_t binding = {.sap = sap, .op = &g_sap_ops[i]};
vec_push(sap->bindings, binding);
}
for (usize i = 0; i < vec_size(sap->bindings); ++i) {
sap_binding_t *binding = &vec_at(sap->bindings, i);
spl_exec_host_t host = {
.name = binding->op->name,
.capabilities = SPL_EXEC_CAP_READ,
.fn = sap_host_adapter,
.user = binding,
};
if (!spl_exec_register_host(exec, &host))
return false;
}
return true;
}