stage2 大量优化代码使其自举编译时间大幅缩短

This commit is contained in:
zzy committed 2026-09-20 09:33:20 +08:00
1 parent e02b0c633a
commit 846f441e3a
38 files changed
+2418 -835

No files matched your search

+30 -7
View File
@@ -10,6 +10,9 @@
#include <stdlib.h>
#include <string.h>
#include "ir_opt.h"
#include "vm_opt.h"
#include "spl_ast.h"
#include "spl_ast2ir.h"
#include "spl_diag.h"
@@ -217,7 +220,7 @@ static spl_ast_t *driver_load(void *user, const char *from_fname, const char *pa
}
static int compile_spl(const char *src, const char *fname, const char *outpath, int gen_debug,
dump_t dump) {
dump_t dump, int opt) {
(void)gen_debug;
spl_diag_t diag;
@@ -326,6 +329,10 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
rc = 1;
goto cleanup;
}
/* IR 层优化 pass(默认关闭,--opt 开启)。 */
if (opt)
spl_ir_opt(&ir_builder.ir, &attr);
if (dump == DUMP_IR) {
spl_ir_dump(&ir_builder.ir, &lang_type.ir, &attr);
goto cleanup;
@@ -334,7 +341,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
/* 类型已全部就绪:解析 layout,供 ir2vm 的 size/align/offset 使用。 */
spl_layout_resolve(&layout, &lang_type.ir);
spl_ir2vm_init(&ir2vm, &a2ir.ir->ir, &lang_type.ir, &layout, &attr);
spl_ir2vm_init(&ir2vm, &a2ir.ir->ir, &lang_type.ir, &layout, &attr, opt);
ir2vm_ok = 1;
spl_diag_set_stage(&diag, SPL_STAGE_IR2VM);
ir2vm.diag = &diag;
@@ -348,6 +355,10 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
goto cleanup;
}
/* ir2vm 之后的字节码优化 pass(默认关闭,--opt 开启)。 */
if (opt)
spl_prog_opt(&ir2vm.prog);
spl_prog_store_to_file(outpath, &ir2vm.prog);
rc = 0;
@@ -397,6 +408,11 @@ int main(int argc, char **argv) {
LOG_INFO("splc0 --dump <flags> <file> dump: lex,ast,sema,ir,vm,none");
return 0;
}
int opt = 0;
for (int ai = 1; ai < argc; ai++)
if (strcmp(argv[ai], "--opt") == 0)
opt = 1;
int argi = 1;
if (argi >= argc) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]");
@@ -435,19 +451,26 @@ int main(int argc, char **argv) {
return 1;
}
int gen_debug = 0;
if (strcmp(argv[argi], "-g") == 0) {
gen_debug = 1;
argi++;
while (argi < argc - 1) {
if (strcmp(argv[argi], "-g") == 0) {
gen_debug = 1;
argi++;
} else if (strcmp(argv[argi], "--opt") == 0) {
opt = 1;
argi++;
} else {
break;
}
}
if (argc < argi + 2) {
LOG_FATAL("Usage: splc0 <in> <out> [-g] or splc0 --dump <flags> <file>\n");
LOG_FATAL("Usage: splc0 <in> <out> [-g] [--opt] or splc0 --dump <flags> <file>\n");
return 1;
}
long len;
char *src = read_file(argv[argi], &len);
if (!src)
return 1;
int rc = compile_spl(src, argv[argi], argv[argi + 1], gen_debug, dump);
int rc = compile_spl(src, argv[argi], argv[argi + 1], gen_debug, dump, opt);
free(src);
return rc;
}