stage1 实现更多ast2ir 修复bug 提供内置操作

This commit is contained in:
zzy committed 2026-09-11 19:26:48 +08:00
1 parent ab6e99c958
commit 49ccdec3dd
10 files changed
+396 -76

No files matched your search

+19 -28
View File
@@ -18,7 +18,7 @@
static const char *CFLAGS_STAMP = BUILD_FOLDER ".cflags";
/* clang-format off */
#define VM_SRCS "stage0/spl_mcode.c", "stage0/spl_syscall.c", "stage0/spl_vm.c"
#define VM_SRCS "stage0/spl_mcode.c", "stage0/spl_syscall.c", "stage0/spl_vm.c",
static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS};
static const char *test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS};
static const char *splc0_srcs[] = {
@@ -28,12 +28,12 @@ static const char *splc0_srcs[] = {
"stage1/spl_dumptree.c",
"stage1/spl_type.c",
"stage1/spl_sema.c",
// "stage1/spl_ir.c",
// "stage1/spl_ast2ir.c",
"stage1/spl_intrin.c",
"stage1/spl_ir.c",
"stage1/spl_ast2ir.c",
// "stage1/spl_ir2vm.c",
// "stage0/spl_mcode.c",
};
/* clang-format on */
typedef struct {
@@ -204,7 +204,9 @@ static void parse_expect(const char *name, Nob_Cmd_Result *expect) {
}
}
static bool test_once(const char *fname) {
static bool test_once(const char *fname, void *user_data) {
(void)user_data;
const char *compiler_path = "splc0";
const char *cli_path = "spl_cli";
const char *target_sir = BUILD_FOLDER "test.sir";
@@ -250,7 +252,7 @@ static bool test_once(const char *fname) {
return true;
}
static bool test_once_ast(const char *fname) {
static bool test_once_with_dump(const char *fname, void *user_data) {
const char *target_name = "splc0";
if (!prepare_build())
return 1;
@@ -258,24 +260,12 @@ static bool test_once_ast(const char *fname) {
return 1;
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
nob_cmd_append(&cmd, "--dump", "ast", fname, BUILD_FOLDER "t.sir");
nob_cmd_append(&cmd, "--dump", (char *)user_data, fname, BUILD_FOLDER "t.sir");
return nob_cmd_run(&cmd);
}
static bool test_once_sema(const char *fname) {
const char *target_name = "splc0";
if (!prepare_build())
return 1;
if (!build_target(target_name))
return 1;
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
nob_cmd_append(&cmd, "--dump", "sema", fname, BUILD_FOLDER "t.sir");
return nob_cmd_run(&cmd);
}
typedef bool (*test_fn_t)(const char *fname);
static bool run_test(Nob_Files *files, test_fn_t test_once) {
typedef bool (*test_fn_t)(const char *fname, void *user_data);
static bool run_test(Nob_Files *files, test_fn_t test_once, void *user_data) {
const char *test_folder = "./tests/stage1/";
Nob_File_Paths paths = {0};
@@ -289,7 +279,7 @@ static bool run_test(Nob_Files *files, test_fn_t test_once) {
}
const char *tname = nob_temp_sprintf("%s%s", test_folder, paths.items[j]);
printf("--- test %s ---\n", tname);
if (test_once(tname) == false)
if (test_once(tname, user_data) == false)
return false;
}
}
@@ -376,12 +366,13 @@ int main(int argc, char **argv) {
if (strcmp(command_name, "test") == 0) {
Nob_Files files = {0};
test_fn_t tfn = test_once;
const char *mode_name = NULL;
if (argc > 0) {
const char *mode_name = nob_shift(argv, argc);
if (strcmp(mode_name, "ast") == 0) {
tfn = test_once_ast;
} else if (strcmp(mode_name, "sema") == 0) {
tfn = test_once_sema;
mode_name = nob_shift(argv, argc);
if (strcmp(mode_name, "lex") == 0 || strcmp(mode_name, "ast") == 0 ||
strcmp(mode_name, "sema") == 0 || strcmp(mode_name, "ir") == 0 ||
strcmp(mode_name, "vm") == 0) {
tfn = test_once_with_dump;
} else {
da_append(&files, mode_name);
}
@@ -391,7 +382,7 @@ int main(int argc, char **argv) {
da_append(&files, nob_shift(argv, argc));
}
return run_test(&files, tfn) ? 0 : 1;
return run_test(&files, tfn, (void *)mode_name) ? 0 : 1;
}
if (strcmp(command_name, "clean") == 0) {