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
+24
-13
@@ -20,7 +20,9 @@
|
||||
#include "spl_diag.h"
|
||||
#include "spl_ir2c.h"
|
||||
#include "spl_ir2vm.h"
|
||||
#include "spl_ir_link.h"
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_meta.h"
|
||||
#include "spl_module.h"
|
||||
#include "spl_parser.h"
|
||||
#include "spl_sema.h"
|
||||
@@ -267,6 +269,8 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
spl_layout_t layout;
|
||||
spl_namespace_t ns;
|
||||
spl_sema_t sema;
|
||||
spl_meta_t *meta = NULL;
|
||||
spl_ir_link_t *ir_link = NULL;
|
||||
spl_ast2ir_t a2ir;
|
||||
spl_ir_builder_t ir_builder;
|
||||
spl_ir2vm_t ir2vm;
|
||||
@@ -299,7 +303,9 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
|
||||
spl_attr_init(&attr);
|
||||
attr_ok = 1;
|
||||
spl_lang_type_init(&lang_type);
|
||||
spl_ir_builder_init(&ir_builder);
|
||||
irb_ok = 1;
|
||||
spl_lang_type_init(&lang_type, ir_builder.ir.types);
|
||||
lang_type_ok = 1;
|
||||
|
||||
/* 目标描述符:剥离目标机器细节,layout 是 size/align/offset 的唯一来源。 */
|
||||
@@ -315,6 +321,11 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
|
||||
spl_sema_init(&sema, &drv.main_ast, &lang_type, &layout, &ns, &attr);
|
||||
sema.modules = &drv.module_registry;
|
||||
sema.ir = &ir_builder.ir;
|
||||
ir_link = spl_ir_link_create(&ir_builder.ir, &sema);
|
||||
sema.ir_link = ir_link;
|
||||
meta = spl_meta_create(&sema);
|
||||
sema.meta = meta;
|
||||
sema_ok = 1;
|
||||
spl_module_loader_t loader = {.load = driver_load, .user = &drv};
|
||||
sema.loader = &loader;
|
||||
@@ -339,8 +350,6 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
for (usize i = 0; i < vec_size(drv.modules); ++i)
|
||||
vec_push(all_asts, vec_at(drv.modules, i).ast);
|
||||
|
||||
spl_ir_builder_init(&ir_builder);
|
||||
irb_ok = 1;
|
||||
spl_ast2ir_init(&a2ir, &ir_builder, &layout, &sema);
|
||||
a2ir_ok = 1;
|
||||
spl_diag_set_stage(&diag, SPL_STAGE_AST2IR);
|
||||
@@ -356,18 +365,18 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
spl_ir_opt(&ir_builder.ir, &attr);
|
||||
|
||||
if (dump == DUMP_IR) {
|
||||
spl_ir_dump(&ir_builder.ir, &lang_type.ir, &attr);
|
||||
spl_ir_dump(&ir_builder.ir, &attr);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* 类型已全部就绪:解析 layout,供 ir2vm/ir2c 的 size/align/offset 使用。 */
|
||||
spl_layout_resolve(&layout, &lang_type.ir);
|
||||
spl_layout_resolve(&layout, ir_builder.ir.types);
|
||||
|
||||
/* C 后端:IR -> C99 写入缓冲,再落盘,不经 VM。 */
|
||||
if (dump == DUMP_C) {
|
||||
spl_ir2c_buf_t cbuf;
|
||||
vec_init(cbuf);
|
||||
bool cok = spl_ir2c_run(&a2ir.ir->ir, &lang_type.ir, &layout, &attr, &cbuf);
|
||||
bool cok = spl_ir2c_run(&a2ir.ir->ir, &layout, &attr, &cbuf);
|
||||
FILE *cf = fopen(outpath, "wb");
|
||||
if (!cf) {
|
||||
LOG_FATAL("cannot open C output file `%s`", outpath);
|
||||
@@ -384,7 +393,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
spl_ir2vm_init(&ir2vm, &a2ir.ir->ir, &lang_type.ir, &layout, &attr);
|
||||
spl_ir2vm_init(&ir2vm, &a2ir.ir->ir, &layout, &attr);
|
||||
ir2vm_ok = 1;
|
||||
spl_diag_set_stage(&diag, SPL_STAGE_IR2VM);
|
||||
ir2vm.diag = &diag;
|
||||
@@ -411,16 +420,18 @@ cleanup:
|
||||
spl_ir2vm_drop(&ir2vm);
|
||||
if (a2ir_ok)
|
||||
spl_ast2ir_drop(&a2ir);
|
||||
if (irb_ok)
|
||||
spl_ir_builder_drop(&ir_builder);
|
||||
spl_meta_destroy(meta);
|
||||
if (sema_ok)
|
||||
spl_sema_drop(&sema);
|
||||
if (scope_ok)
|
||||
spl_ns_drop(&ns);
|
||||
if (layout_ok)
|
||||
spl_layout_drop(&layout);
|
||||
spl_ir_link_destroy(ir_link);
|
||||
if (lang_type_ok)
|
||||
spl_lang_type_drop(&lang_type);
|
||||
if (layout_ok)
|
||||
spl_layout_drop(&layout);
|
||||
if (irb_ok)
|
||||
spl_ir_builder_drop(&ir_builder);
|
||||
if (scope_ok)
|
||||
spl_ns_drop(&ns);
|
||||
if (attr_ok)
|
||||
spl_attr_drop(&attr);
|
||||
|
||||
|
||||
Reference in new issue
Block a user