From 9ecb92cb03827e56553c151d0b6272ebd1b3d9e4 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Sun, 20 Sep 2026 18:24:25 +0800 Subject: [PATCH] =?UTF-8?q?stage2=20=E8=BF=81=E7=A7=BBnob=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dnob=E7=9A=84=E9=97=AE=E9=A2=98=20=E7=BB=A7=E7=BB=AD?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=B1=BB=E5=9E=8B=E5=AF=BC=E5=85=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nob.c | 296 +++------------------------------ nob.h => nob/nob.h | 0 nob/nob_data.h | 228 +++++++++++++++++++++++++ nob_popen.h => nob/nob_popen.h | 0 stage0/include/core_vec.h | 5 +- 5 files changed, 251 insertions(+), 278 deletions(-) rename nob.h => nob/nob.h (100%) create mode 100644 nob/nob_data.h rename nob_popen.h => nob/nob_popen.h (100%) diff --git a/nob.c b/nob.c index f267fb1..e765a1e 100644 --- a/nob.c +++ b/nob.c @@ -5,9 +5,9 @@ #define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS) #undef UNICODE #define NOB_IMPLEMENTATION -#include "nob.h" +#include "nob/nob.h" #undef NOB_IMPLEMENTATION -#include "nob_popen.h" +#include "nob/nob_popen.h" #ifdef _WIN32 #include #endif @@ -19,93 +19,7 @@ // object files get rebuilt. 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", -static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS}; -static const char *vm_test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS}; -static const char *splc0_srcs[] = { - "stage1/splc0.c", - "stage1/spl_diag.c", - "stage1/spl_tok.c", - "stage1/spl_lexer.c", - "stage1/spl_ast.c", - "stage1/spl_parser.c", - "stage1/spl_dumptree.c", - "stage1/spl_type.c", - "stage1/spl_fold.c", - "stage1/spl_lang_type.c", - "stage1/spl_attr.c", - "stage1/spl_scope.c", - "stage1/spl_sema.c", - "stage1/spl_intrin.c", - "stage1/spl_ir.c", - "stage1/spl_ast2ir.c", - "stage1/spl_layout.c", - "stage1/spl_ir2vm.c", - "stage1/ir_opt.c", - "stage1/vm_opt.c", - "stage0/spl_mcode.c", -}; - -static const char* splc1_deps[] = { - "stage2/sys/exit.spl", - "stage2/sys/file.spl", - "stage2/sys/mem.spl", - "stage2/sys/spl_rt.h", - "stage2/sys/spl_vm_embed.h", - "stage2/sys/time.spl", - "stage2/sys/vm.spl", - "stage2/utils/algorithm.spl", - "stage2/utils/arena.spl", - "stage2/utils/ascii.spl", - "stage2/utils/byte_buffer.spl", - "stage2/utils/byte_view.spl", - "stage2/utils/error.spl", - "stage2/utils/fmt.spl", - "stage2/utils/hash.spl", - "stage2/utils/io.spl", - "stage2/utils/json.spl", - "stage2/utils/log.spl", - "stage2/utils/lsp_frame.spl", - "stage2/utils/map.spl", - "stage2/utils/memory.spl", - "stage2/utils/mp.spl", - "stage2/utils/path.spl", - "stage2/utils/profile.spl", - "stage2/utils/set.spl", - "stage2/utils/source.spl", - "stage2/utils/str.spl", - "stage2/utils/utf8.spl", - "stage2/utils/value.spl", - "stage2/utils/vec.spl", - "stage2/ast.spl", - "stage2/ast2ir.spl", - "stage2/attr.spl", - "stage2/def.spl", - "stage2/diag.spl", - "stage2/eval.spl", - "stage2/intrin.spl", - "stage2/ir.spl", - "stage2/ir2c.spl", - "stage2/ir2vm.spl", - "stage2/ir_builder.spl", - "stage2/lang_type.spl", - "stage2/layout.spl", - "stage2/lexer.spl", - "stage2/mcode.spl", - "stage2/parser.spl", - "stage2/scope.spl", - "stage2/sema.spl", - "stage2/splc1.spl", - "stage2/token.spl", - "stage2/type_table.spl", -}; - -static const char* splc2_deps[] = { - -}; - -/* clang-format on */ +#include "nob/nob_data.h" static const char *obj_path(const char *src) { // "stage0/spl_vm.c" -> "build/stage0_spl_vm.o" @@ -162,47 +76,12 @@ static bool prepare_build(void) { return true; } -typedef struct build { - const char *name; - enum { - BUILD_EXE, - BUILD_EXE_WITH_OPTIMIZATION, - BUILD_SIR, - BUILD_NATIVE, - } kind; - union { - struct { - const char **srcs; - size_t srcs_count; - const char **headers; - size_t headers_count; - } exe; - struct { - const char *src; - const char *compiler; - const char **deps; - size_t deps_count; - } sir; - struct { - const char *src; - const char *compiler; - const char **deps; - size_t deps_count; - } native; - }; -} build_t; - typedef struct { const char **items; size_t count; size_t capacity; } str_da_t; -typedef struct run { - const char *name; - const char *interpreter; -} run_t; - bool run_target(const char *name, Nob_Cmd *args); const char *build_target(const char *name); @@ -271,143 +150,6 @@ defer: return NULL; } -static const char *build_native(const char *name) { - const char *exe = exe_path(name); - const char *cli = build_target("spl_cli"); - const char *sir = build_target("splc1"); - if (!cli || !sir) { - nob_log(NOB_ERROR, "failed to build prerequisites for `%s`", name); - return NULL; - } - const char *cpath = BUILD_FOLDER "splc1.c"; - const char *inputs[] = {sir, - "stage2/sys/spl_rt.h", - "stage2/sys/spl_vm_embed.h", - "stage2/sys/spl_vm_embed.c", - "stage0/spl_vm.c", - "stage0/spl_mcode.c", - "stage0/spl_syscall.c", - "stage0/spl_vm.h", - "stage0/spl_mcode.h"}; - int needs = nob_needs_rebuild(exe, inputs, NOB_ARRAY_LEN(inputs)); - if (needs < 0) - return NULL; - if (needs == 0) - return exe; - - Nob_Cmd cmd = {0}; - if (nob_file_exists(exe)) { - // 已有原生编译器:用原生自举重新生成 C(0.1s 级) - nob_cmd_append(&cmd, exe, "--dump", "c", "stage2/splc1.spl", cpath); - } else { - // 冷启动:用 VM 版 splc1 生成 C - nob_cmd_append(&cmd, cli, sir, "--dump", "c", "stage2/splc1.spl", cpath); - } - if (!nob_cmd_run(&cmd)) { - nob_log(NOB_ERROR, "ir2c C generation failed"); - nob_cmd_free(cmd); - return NULL; - } - cmd.count = 0; - nob_cc(&cmd); - nob_cc_inputs(&cmd, cpath); - nob_cc_inputs(&cmd, "stage2/sys/spl_vm_embed.c"); - nob_cc_inputs(&cmd, "stage0/spl_vm.c"); - nob_cc_inputs(&cmd, "stage0/spl_mcode.c"); - nob_cc_inputs(&cmd, "stage0/spl_syscall.c"); - nob_cc_output(&cmd, exe); - if (!nob_cmd_run(&cmd)) { - nob_log(NOB_ERROR, "native link failed"); - nob_cmd_free(cmd); - return NULL; - } - nob_cmd_free(cmd); - return exe; -} - -static build_t targets[] = { - { - "spl_cli", - BUILD_EXE_WITH_OPTIMIZATION, - .exe = {.srcs = spl_cli_srcs, - .srcs_count = NOB_ARRAY_LEN(spl_cli_srcs), - .headers = NULL, - .headers_count = 0}, - }, - { - "spl_cli_g", - BUILD_EXE, - .exe = {.srcs = spl_cli_srcs, - .srcs_count = NOB_ARRAY_LEN(spl_cli_srcs), - .headers = NULL, - .headers_count = 0}, - }, - { - "vm_test", - BUILD_EXE, - .exe = {.srcs = vm_test_srcs, - .srcs_count = NOB_ARRAY_LEN(vm_test_srcs), - .headers = NULL, - .headers_count = 0}, - }, - { - "splc0", - BUILD_EXE, - .exe = {.srcs = splc0_srcs, - .srcs_count = NOB_ARRAY_LEN(splc0_srcs), - .headers = NULL, - .headers_count = 0}, - }, - { - "splc1", - BUILD_SIR, - .sir = {.src = "stage2/splc1.spl", - .compiler = "splc0", - .deps = splc1_deps, - .deps_count = NOB_ARRAY_LEN(splc1_deps)}, - }, - { - "splc1s", - BUILD_SIR, - .sir = {.src = "stage2/splc1.spl", - .compiler = "splc1", - .deps = splc1_deps, - .deps_count = NOB_ARRAY_LEN(splc1_deps)}, - }, - { - "splc1r", - BUILD_SIR, - .sir = {.src = "stage2/splc1.spl", - .compiler = "splc1s", - .deps = splc1_deps, - .deps_count = NOB_ARRAY_LEN(splc1_deps)}, - }, - { - "splc1n", - BUILD_NATIVE, - .native = {.src = "stage2/splc1.spl", - .compiler = "splc1", - .deps = splc1_deps, - .deps_count = NOB_ARRAY_LEN(splc1_deps)}, - }, - { - "splc2", - BUILD_SIR, - .sir = {.src = "stage3/splc2.spl", - .compiler = "splc1n", - .deps = splc2_deps, - .deps_count = NOB_ARRAY_LEN(splc2_deps)}, - }, - { - "splc2n", - BUILD_NATIVE, - .native = {.src = "stage3/splc2.spl", - .compiler = "splc1n", - .deps = splc2_deps, - .deps_count = NOB_ARRAY_LEN(splc2_deps)}, - }, -}; - const char *build_target(const char *name) { if (!prepare_build()) return NULL; @@ -439,20 +181,26 @@ const char *build_target(const char *name) { const char *target_c = c_path(name); // TODO need_rebuild - int needs = - nob_needs_rebuild(target_exe, target->native.deps, target->native.deps_count); + int needs = nob_needs_rebuild(target_c, target->native.deps, target->native.deps_count); + if (needs < 0) + return NULL; + if (needs != 0) { + nob_cmd_append(&cmd, "--dump", "c", target->native.src, target_c); + if (!run_target(target->native.compiler, &cmd)) { + nob_log(NOB_ERROR, "failed to build `%s`", name); + nob_cmd_free(cmd); + return NULL; + } + cmd.count = 0; + } + + needs = nob_needs_rebuild(target_exe, target->native.crt_deps, + target->native.crt_deps_count); if (needs < 0) return NULL; if (needs == 0) return target_exe; - nob_cmd_append(&cmd, "--dump", "c", target->native.src, target_c); - if (!run_target(target->native.compiler, &cmd)) { - nob_log(NOB_ERROR, "failed to build `%s`", name); - nob_cmd_free(cmd); - return NULL; - } - cmd.count = 0; nob_cc(&cmd); nob_cmd_append(&cmd, "-I./stage2/sys", "-O2", "-D_CRT_SECURE_NO_WARNINGS"); nob_cc_inputs(&cmd, target_c); @@ -473,12 +221,6 @@ const char *build_target(const char *name) { return NULL; } -static run_t runs[] = { - {"spl_cli", NULL}, {"vm_test", NULL}, {"splc0", NULL}, - {"splc1", "spl_cli"}, {"splc1r", "spl_cli"}, {"splc1s", "spl_cli"}, - {"splc1n", NULL}, {"splc2", "spl_cli"}, {"splc2n", NULL}, -}; - bool run_target_dry(const char *name, Nob_Cmd *args, Nob_Cmd *result) { for (size_t i = 0; i < NOB_ARRAY_LEN(runs); ++i) { if (strcmp(runs[i].name, name) != 0) { @@ -788,7 +530,7 @@ int main(int argc, char **argv) { SetConsoleOutputCP(CP_UTF8); SetConsoleCP(CP_UTF8); #endif - NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob.h", "nob_popen.h"); + NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob/nob.h", "nob/nob_popen.h", "nob/nob_data.h"); set_log_handler(nob_cancer_log_handler); const char *program_name = nob_shift(argv, argc); diff --git a/nob.h b/nob/nob.h similarity index 100% rename from nob.h rename to nob/nob.h diff --git a/nob/nob_data.h b/nob/nob_data.h new file mode 100644 index 0000000..229d2c9 --- /dev/null +++ b/nob/nob_data.h @@ -0,0 +1,228 @@ +#ifndef __NOB_DATA_H__ +#define __NOB_DATA_H__ + +/* clang-format off */ +#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 *vm_test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS}; +static const char *splc0_srcs[] = { + "stage1/splc0.c", + "stage1/spl_diag.c", + "stage1/spl_tok.c", + "stage1/spl_lexer.c", + "stage1/spl_ast.c", + "stage1/spl_parser.c", + "stage1/spl_dumptree.c", + "stage1/spl_type.c", + "stage1/spl_fold.c", + "stage1/spl_lang_type.c", + "stage1/spl_attr.c", + "stage1/spl_scope.c", + "stage1/spl_sema.c", + "stage1/spl_intrin.c", + "stage1/spl_ir.c", + "stage1/spl_ast2ir.c", + "stage1/spl_layout.c", + "stage1/spl_ir2vm.c", + "stage1/ir_opt.c", + "stage1/vm_opt.c", + "stage0/spl_mcode.c", +}; + +static const char *splc1_crt_deps[] ={ + "stage2/sys/spl_rt.h", + "stage2/sys/spl_vm_embed.h", + // TODO +}; + +static const char* splc1_deps[] = { + "stage2/sys/exit.spl", + "stage2/sys/file.spl", + "stage2/sys/mem.spl", + "stage2/sys/time.spl", + "stage2/sys/vm.spl", + "stage2/utils/algorithm.spl", + "stage2/utils/arena.spl", + "stage2/utils/ascii.spl", + "stage2/utils/byte_buffer.spl", + "stage2/utils/byte_view.spl", + "stage2/utils/error.spl", + "stage2/utils/fmt.spl", + "stage2/utils/hash.spl", + "stage2/utils/io.spl", + "stage2/utils/json.spl", + "stage2/utils/log.spl", + "stage2/utils/lsp_frame.spl", + "stage2/utils/map.spl", + "stage2/utils/memory.spl", + "stage2/utils/mp.spl", + "stage2/utils/path.spl", + "stage2/utils/profile.spl", + "stage2/utils/set.spl", + "stage2/utils/source.spl", + "stage2/utils/str.spl", + "stage2/utils/utf8.spl", + "stage2/utils/value.spl", + "stage2/utils/vec.spl", + "stage2/ast.spl", + "stage2/ast2ir.spl", + "stage2/attr.spl", + "stage2/def.spl", + "stage2/diag.spl", + "stage2/eval.spl", + "stage2/intrin.spl", + "stage2/ir.spl", + "stage2/ir2c.spl", + "stage2/ir2vm.spl", + "stage2/ir_builder.spl", + "stage2/lang_type.spl", + "stage2/layout.spl", + "stage2/lexer.spl", + "stage2/mcode.spl", + "stage2/parser.spl", + "stage2/scope.spl", + "stage2/sema.spl", + "stage2/splc1.spl", + "stage2/token.spl", + "stage2/type_table.spl", +}; + +static const char* splc2_deps[] = { + +}; + +static const char *splc2_crt_deps[] = {}; + +/* clang-format on */ + +typedef struct build { + const char *name; + enum { + BUILD_EXE, + BUILD_EXE_WITH_OPTIMIZATION, + BUILD_SIR, + BUILD_NATIVE, + } kind; + union { + struct { + const char **srcs; + size_t srcs_count; + const char **headers; + size_t headers_count; + } exe; + struct { + const char *src; + const char *compiler; + const char **deps; + size_t deps_count; + } sir; + struct { + const char *src; + const char *compiler; + const char **deps; + size_t deps_count; + const char **crt_deps; + size_t crt_deps_count; + } native; + }; +} build_t; + +typedef struct run { + const char *name; + const char *interpreter; +} run_t; + +static build_t targets[] = { + { + "spl_cli", + BUILD_EXE_WITH_OPTIMIZATION, + .exe = {.srcs = spl_cli_srcs, + .srcs_count = NOB_ARRAY_LEN(spl_cli_srcs), + .headers = NULL, + .headers_count = 0}, + }, + { + "spl_cli_g", + BUILD_EXE, + .exe = {.srcs = spl_cli_srcs, + .srcs_count = NOB_ARRAY_LEN(spl_cli_srcs), + .headers = NULL, + .headers_count = 0}, + }, + { + "vm_test", + BUILD_EXE, + .exe = {.srcs = vm_test_srcs, + .srcs_count = NOB_ARRAY_LEN(vm_test_srcs), + .headers = NULL, + .headers_count = 0}, + }, + { + "splc0", + BUILD_EXE, + .exe = {.srcs = splc0_srcs, + .srcs_count = NOB_ARRAY_LEN(splc0_srcs), + .headers = NULL, + .headers_count = 0}, + }, + { + "splc1", + BUILD_SIR, + .sir = {.src = "stage2/splc1.spl", + .compiler = "splc0", + .deps = splc1_deps, + .deps_count = NOB_ARRAY_LEN(splc1_deps)}, + }, + { + "splc1s", + BUILD_SIR, + .sir = {.src = "stage2/splc1.spl", + .compiler = "splc1", + .deps = splc1_deps, + .deps_count = NOB_ARRAY_LEN(splc1_deps)}, + }, + { + "splc1r", + BUILD_SIR, + .sir = {.src = "stage2/splc1.spl", + .compiler = "splc1s", + .deps = splc1_deps, + .deps_count = NOB_ARRAY_LEN(splc1_deps)}, + }, + { + "splc1n", + BUILD_NATIVE, + .native = {.src = "stage2/splc1.spl", + .compiler = "splc1", + .deps = splc1_deps, + .deps_count = NOB_ARRAY_LEN(splc1_deps), + .crt_deps = splc1_crt_deps, + .crt_deps_count = NOB_ARRAY_LEN(splc1_crt_deps)}, + }, + { + "splc2", + BUILD_SIR, + .sir = {.src = "stage3/splc2.spl", + .compiler = "splc1n", + .deps = splc2_deps, + .deps_count = NOB_ARRAY_LEN(splc2_deps)}, + }, + { + "splc2n", + BUILD_NATIVE, + .native = {.src = "stage3/splc2.spl", + .compiler = "splc1n", + .deps = splc2_deps, + .deps_count = NOB_ARRAY_LEN(splc2_deps), + .crt_deps = splc2_crt_deps, + .crt_deps_count = NOB_ARRAY_LEN(splc2_crt_deps)}, + }, +}; + +static run_t runs[] = { + {"spl_cli", NULL}, {"vm_test", NULL}, {"splc0", NULL}, + {"splc1", "spl_cli"}, {"splc1r", "spl_cli"}, {"splc1s", "spl_cli"}, + {"splc1n", NULL}, {"splc2", "spl_cli"}, {"splc2n", NULL}, +}; + +#endif /* __NOB_DATA_H__ */ diff --git a/nob_popen.h b/nob/nob_popen.h similarity index 100% rename from nob_popen.h rename to nob/nob_popen.h diff --git a/stage0/include/core_vec.h b/stage0/include/core_vec.h index 8c0c6e2..5f69992 100644 --- a/stage0/include/core_vec.h +++ b/stage0/include/core_vec.h @@ -26,7 +26,10 @@ #ifndef nullptr #define nullptr NULL #endif -typedef size_t usize; +#ifndef SPL_USIZE_TYPEDEF +#define SPL_USIZE_TYPEDEF +typedef uintptr_t usize; +#endif #define __vec_realloc realloc #define __vec_free free #define __vec_memcpy memcpy