stage1 初步实现

This commit is contained in:
zzy
2026-08-06 19:26:30 +08:00
parent a56b4cbb70
commit 314100afbc
35 changed files with 556 additions and 2236 deletions

View File

@@ -168,7 +168,50 @@ static int cmd_dump(const char *flags, const char *path) {
return 0;
}
static int compile_spl(const char *src, const char *fname, const char *outpath) {
static spl_ast_node_t *ast_node_at(spl_ast_t *ast, spl_ast_node_ref_t ref) {
if (ref == 0 || ref >= ast->buckets.size)
return NULL;
return &ast->buckets.data[ref];
}
static int ast_line(spl_ast_t *ast, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = ast_node_at(ast, ref);
return n ? n->loc.line : 0;
}
/* -g在 .sir 尾部追加 debug 段文本IR 行 + VAR 行spl_cli -g 读取 */
static void gen_debug_map(const char *outpath, spl_ast_t *ast, const spl_ir_t *ir,
const spl_ir2vm_t *ir2vm) {
FILE *f = fopen(outpath, "ab");
if (!f)
return;
fprintf(f, "SPLDBG\n");
for (usize i = 0; i < ir2vm->fdbg.size; i++) {
const spl_ir2vm_fdbg_t *fd = &ir2vm->fdbg.data[i];
if (fd->fid >= ir->funcs.size)
continue;
const spl_ir_func_t *fn = &ir->funcs.data[fd->fid];
const char *fname = fn->name ? fn->name : "?";
for (usize ref = 1; ref < fd->node_first_ip.size; ref++) {
usize ip = fd->node_first_ip.data[ref];
if (ip == (usize)-1)
continue;
const spl_ir_node_t *n = &fn->nodes.data[ref];
fprintf(f, "IR %zu %zu %d %s\n", ip, ref, ast_line(ast, n->src_ref),
spl_ir_kind_name(n->kind));
}
for (usize j = 0; j < fn->dbg_vars.size; j++) {
const spl_ir_dbg_var_t *dv = &fn->dbg_vars.data[j];
if (!dv->name)
continue;
fprintf(f, "VAR %s %s %zu %zu %d\n", fname, dv->name, dv->offset, dv->tid,
dv->is_param);
}
}
fclose(f);
}
static int compile_spl(const char *src, const char *fname, const char *outpath, int gen_debug) {
spl_tok_vec_t toks = spl_lex(src, fname);
spl_ast_t ast;
spl_ast_init(&ast, &toks);
@@ -203,6 +246,8 @@ static int compile_spl(const char *src, const char *fname, const char *outpath)
spl_ir2vm_t ir2vm;
spl_ir2vm_init(&ir2vm, &a2ir.ir, &sema.type);
int rc = spl_ir2vm_run(&ir2vm, outpath);
if (gen_debug && rc == 0)
gen_debug_map(outpath, &ast, &a2ir.ir, &ir2vm);
spl_ir2vm_drop(&ir2vm);
spl_ast2ir_drop(&a2ir);
spl_sema_drop(&sema);
@@ -212,36 +257,45 @@ static int compile_spl(const char *src, const char *fname, const char *outpath)
int main(int argc, char **argv) {
if (argc < 2) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]\n");
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]");
return 1;
}
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
LOG_INFO("splc0 <in> <out> compile (.spl -> .sir, 阶段 B)\n");
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,sema,ir,all\n");
LOG_INFO("splc0 <in> <out> compile (.spl -> .sir, stage B)");
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,sema,ir,all");
return 0;
}
int argi = 1;
if (argi >= argc) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]\n");
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]");
return 1;
}
if (strcmp(argv[argi], "--dump") == 0) {
if (argc < argi + 3) {
LOG_INFO("splc0: --dump need <flags> <file>\n");
LOG_INFO("splc0: --dump need <flags> <file>");
return 1;
}
return cmd_dump(argv[argi + 1], argv[argi + 2]);
}
/* splc0 <in> <out> */
/* splc0 [-g] <in> <out> */
if (argc < argi + 2) {
LOG_FATAL("Usage: splc0 <in> <out> or splc0 --dump <flags> <file>\n");
LOG_FATAL("Usage: splc0 <in> <out> [-g] or splc0 --dump <flags> <file>\n");
return 1;
}
int gen_debug = 0;
if (strcmp(argv[argi], "-g") == 0) {
gen_debug = 1;
argi++;
}
if (argc < argi + 2) {
LOG_FATAL("Usage: splc0 <in> <out> [-g] 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]);
int rc = compile_spl(src, argv[argi], argv[argi + 1], gen_debug);
free(src);
return rc;
}