stage1 重构代码 重构nob构建方式
This commit is contained in:
1 parent
3714c2d142
commit
4c0317fcb5
14 files changed
+4004
-2160
No files matched your search
@@ -8,6 +8,11 @@
|
|||||||
#include "nob/nob.h"
|
#include "nob/nob.h"
|
||||||
#undef NOB_IMPLEMENTATION
|
#undef NOB_IMPLEMENTATION
|
||||||
#include "nob/nob_popen.h"
|
#include "nob/nob_popen.h"
|
||||||
|
|
||||||
|
#define SCC_ARGPARSE_IMPLEMENTATION
|
||||||
|
#define SCC_OPTPARSER_IMPLEMENTATION
|
||||||
|
#include "nob/argparse.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <consoleapi2.h>
|
#include <consoleapi2.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -29,6 +34,7 @@ static const char *obj_path(const char *src) {
|
|||||||
memcpy(w, BUILD_FOLDER, prefix_len);
|
memcpy(w, BUILD_FOLDER, prefix_len);
|
||||||
w += prefix_len;
|
w += prefix_len;
|
||||||
for (const char *p = src; *p && *p != '.'; ++p) {
|
for (const char *p = src; *p && *p != '.'; ++p) {
|
||||||
|
assert(w != NULL);
|
||||||
*w++ = (*p == '/' || *p == '\\') ? '_' : *p;
|
*w++ = (*p == '/' || *p == '\\') ? '_' : *p;
|
||||||
}
|
}
|
||||||
memcpy(w, ".o", 3);
|
memcpy(w, ".o", 3);
|
||||||
@@ -227,10 +233,14 @@ bool run_target_dry(const char *name, Nob_Cmd *args, Nob_Cmd *result) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const char *exe = build_target(runs[i].name);
|
const char *exe = build_target(runs[i].name);
|
||||||
|
if (!exe) {
|
||||||
|
nob_log(NOB_ERROR, "failed to build executable `%s`", name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const char *interpreter = runs[i].interpreter;
|
const char *interpreter = runs[i].interpreter;
|
||||||
if (interpreter) {
|
if (interpreter) {
|
||||||
const char *inter = build_target(interpreter);
|
const char *inter = build_target(interpreter);
|
||||||
if (!exe) {
|
if (!inter) {
|
||||||
nob_log(NOB_ERROR, "failed to build interpreter `%s` for `%s`", interpreter, name);
|
nob_log(NOB_ERROR, "failed to build interpreter `%s` for `%s`", interpreter, name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -255,214 +265,220 @@ bool run_target(const char *name, Nob_Cmd *args) {
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_expect(const char *name, Nob_Cmd_Result *expect) {
|
// ---- 测试 ----
|
||||||
Nob_String_Builder sb = {0};
|
// Expect 协议:扫整个文件的注释行找 `// Expect retcode = N`,取第一条命中(注解可以在任意位置)。
|
||||||
nob_read_entire_file(name, &sb);
|
// `// Expect stdout = ...` 暂不比较,遇到跳过。
|
||||||
Nob_String_View sv = nob_sb_to_sv(sb);
|
// 返回 false 只表示文件读不出来或 retcode 写坏了;没写注解则 has_retcode 为假,由调用方按 0 处理。
|
||||||
while (sv.count > 0) {
|
typedef struct {
|
||||||
Nob_String_View line = nob_sv_chop_by_delim(&sv, '\n');
|
bool has_retcode;
|
||||||
line = nob_sv_trim_left(line); // 去掉前导空格
|
int retcode;
|
||||||
|
} test_expect_t;
|
||||||
|
|
||||||
if (!nob_sv_starts_with(line, nob_sv_from_cstr("//"))) {
|
static bool test_parse_int(Nob_String_View value, int *out) {
|
||||||
break;
|
if (value.count == 0)
|
||||||
}
|
|
||||||
|
|
||||||
Nob_String_View rest = nob_sv_from_parts(line.data + 2, line.count - 2);
|
|
||||||
rest = nob_sv_trim_left(rest);
|
|
||||||
|
|
||||||
if (rest.count == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!nob_sv_starts_with(rest, nob_sv_from_cstr("Expect"))) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
rest = nob_sv_from_parts(rest.data + 6, rest.count - 6);
|
|
||||||
rest = nob_sv_trim_left(rest);
|
|
||||||
|
|
||||||
// 按 '=' 分割
|
|
||||||
Nob_String_View key = nob_sv_chop_by_delim(&rest, '=');
|
|
||||||
key = nob_sv_trim(key);
|
|
||||||
Nob_String_View value = nob_sv_trim(rest);
|
|
||||||
|
|
||||||
if (nob_sv_eq(key, nob_sv_from_cstr("retcode"))) {
|
|
||||||
int val = 0;
|
|
||||||
for (size_t i = 0; i < value.count && isdigit(value.data[i]); ++i) {
|
|
||||||
val = val * 10 + (value.data[i] - '0');
|
|
||||||
}
|
|
||||||
// printf("expected code %d\n", val);
|
|
||||||
expect->exit_code = val;
|
|
||||||
} else if (nob_sv_eq(key, nob_sv_from_cstr("stdout"))) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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";
|
|
||||||
|
|
||||||
Nob_Cmd cmd = {0};
|
|
||||||
if (!build_target(compiler_path)) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
cmd.count = 0;
|
|
||||||
|
|
||||||
nob_cmd_append(&cmd, exe_path(compiler_path));
|
|
||||||
nob_cmd_append(&cmd, fname, target_sir);
|
|
||||||
if (!nob_cmd_run(&cmd)) {
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
bool negative = false;
|
||||||
|
if (value.data[0] == '-' || value.data[0] == '+') {
|
||||||
|
negative = value.data[0] == '-';
|
||||||
|
i = 1;
|
||||||
}
|
}
|
||||||
|
if (i >= value.count)
|
||||||
if (!build_target(cli_path)) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
cmd.count = 0;
|
|
||||||
nob_cmd_append(&cmd, exe_path(cli_path));
|
|
||||||
nob_cmd_append(&cmd, target_sir);
|
|
||||||
// nob_cmd_run(&cmd);
|
|
||||||
|
|
||||||
Nob_Cmd_Result result = {0};
|
|
||||||
Nob_String_Builder cmd_str = {0};
|
|
||||||
nob_cmd_render(cmd, &cmd_str);
|
|
||||||
|
|
||||||
Nob_String_View sv = nob_sb_to_sv(cmd_str);
|
|
||||||
nob_cmd_capture_popen(nob_temp_sv_to_cstr(sv), &result);
|
|
||||||
// Expect retcode = number
|
|
||||||
|
|
||||||
Nob_Cmd_Result expect = {0};
|
|
||||||
parse_expect(fname, &expect);
|
|
||||||
|
|
||||||
if (expect.exit_code != result.exit_code) {
|
|
||||||
nob_log(NOB_ERROR, "Expect code `%d`, Result code `%d`", expect.exit_code,
|
|
||||||
result.exit_code);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
nob_cmd_result_free(result);
|
int v = 0;
|
||||||
|
for (; i < value.count; i += 1) {
|
||||||
|
if (!isdigit((unsigned char)value.data[i]))
|
||||||
|
return false;
|
||||||
|
v = v * 10 + (value.data[i] - '0');
|
||||||
|
}
|
||||||
|
*out = negative ? -v : v;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool test_once_with_dump(const char *fname, void *user_data) {
|
static bool test_parse_expect(const char *fname, test_expect_t *expect) {
|
||||||
const char *target_name = "splc0";
|
Nob_String_Builder sb = {0};
|
||||||
if (!prepare_build())
|
if (!nob_read_entire_file(fname, &sb)) {
|
||||||
return 1;
|
nob_sb_free(sb);
|
||||||
if (!build_target(target_name))
|
return false;
|
||||||
return 1;
|
}
|
||||||
Nob_Cmd cmd = {0};
|
|
||||||
nob_cmd_append(&cmd, exe_path(target_name));
|
Nob_String_View sv = nob_sb_to_sv(sb);
|
||||||
nob_cmd_append(&cmd, "--dump", (char *)user_data, fname, BUILD_FOLDER "t.sir");
|
bool malformed = false;
|
||||||
return nob_cmd_run(&cmd);
|
while (sv.count > 0) {
|
||||||
|
Nob_String_View line = nob_sv_trim(nob_sv_chop_by_delim(&sv, '\n'));
|
||||||
|
if (!nob_sv_starts_with(line, nob_sv_from_cstr("//")))
|
||||||
|
continue;
|
||||||
|
line = nob_sv_trim_left(nob_sv_from_parts(line.data + 2, line.count - 2));
|
||||||
|
if (!nob_sv_starts_with(line, nob_sv_from_cstr("Expect")))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
line = nob_sv_trim_left(nob_sv_from_parts(line.data + 6, line.count - 6));
|
||||||
|
Nob_String_View key = nob_sv_trim(nob_sv_chop_by_delim(&line, '='));
|
||||||
|
Nob_String_View value = nob_sv_trim(line);
|
||||||
|
if (!nob_sv_eq(key, nob_sv_from_cstr("retcode")) || expect->has_retcode)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int parsed = 0;
|
||||||
|
if (!test_parse_int(value, &parsed)) {
|
||||||
|
malformed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
expect->retcode = parsed;
|
||||||
|
expect->has_retcode = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
nob_sb_free(sb);
|
||||||
|
return !malformed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 由源路径派生构建产物路径:build/<basename>.sir
|
// suite 只管"跑哪个目录的文件",用哪个编译器完全由命令行 target 决定。
|
||||||
static const char *artifact_sir(const char *src) {
|
typedef struct {
|
||||||
const char *base = src;
|
const char *name;
|
||||||
for (const char *p = src; *p; ++p)
|
const char *dir;
|
||||||
if (*p == '/' || *p == '\\')
|
} test_suite_t;
|
||||||
base = p + 1;
|
|
||||||
const char *dot = strrchr(base, '.');
|
static const test_suite_t TEST_SUITES[] = {
|
||||||
size_t len = dot ? (size_t)(dot - base) : strlen(base);
|
{"stage1", "./tests/stage1/"},
|
||||||
return nob_temp_sprintf(BUILD_FOLDER "%.*s.sir", (int)len, base);
|
{"stage2", "./tests/stage2/"},
|
||||||
|
{"stage3", "./tests/stage3/"},
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char *TEST_MODE_NAMES[] = {"lex", "ast", "sema", "ir", "vm"};
|
||||||
|
|
||||||
|
// 复用 runs[] 与 run_target_dry:构建一次,并拿到"怎么把这个 target 跑起来"的 argv 前缀。
|
||||||
|
// test/diff 每条用例复制这份前缀再补自己的参数,运行语义因此和 `nob run` 完全一致。
|
||||||
|
static bool build_run_prefix(const char *name, Nob_Cmd *prefix) {
|
||||||
|
Nob_Cmd no_args = {0};
|
||||||
|
return run_target_dry(name, &no_args, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
// `--dump <mode> <file>` 之后统一补一个 <out> 占位:splc0 强制要它,splc1 标成可选,splc2 忽略多余参数。
|
||||||
|
// 参差的是那几个程序自己的 CLI,不是 exe/.sir 的区别。
|
||||||
|
static void append_dump_args(Nob_Cmd *cmd, const char *mode, const char *fname) {
|
||||||
|
nob_cmd_append(cmd, "--dump", mode, fname, BUILD_FOLDER "dump.sir");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool run_capture(Nob_Cmd *cmd, Nob_Cmd_Result *result) {
|
static bool run_capture(Nob_Cmd *cmd, Nob_Cmd_Result *result) {
|
||||||
Nob_String_Builder rendered = {0};
|
Nob_String_Builder rendered = {0};
|
||||||
nob_cmd_render(*cmd, &rendered);
|
nob_cmd_render(*cmd, &rendered);
|
||||||
Nob_String_View sv = nob_sb_to_sv(rendered);
|
bool ok = nob_cmd_capture_popen(nob_temp_sv_to_cstr(nob_sb_to_sv(rendered)), result);
|
||||||
bool ok = nob_cmd_capture_popen(nob_temp_sv_to_cstr(sv), result);
|
|
||||||
nob_sb_free(rendered);
|
nob_sb_free(rendered);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// diff 测试:对同一文件跑 `splc0 --dump <mode>` 与 `spl_cli splc1.sir --dump <mode>`,
|
|
||||||
// 逐字节比较 stdout。
|
|
||||||
static bool test_once_diff(const char *fname, void *user_data) {
|
|
||||||
const char *mode = (const char *)user_data;
|
|
||||||
if (!mode) {
|
|
||||||
nob_log(NOB_ERROR, "diff test requires a mode: lex|ast|sema|ir|vm");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!build_target("splc0"))
|
|
||||||
return false;
|
|
||||||
if (!build_target("spl_cli"))
|
|
||||||
return false;
|
|
||||||
if (!build_target("splc1"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Nob_Cmd c_cmd = {0};
|
|
||||||
nob_cmd_append(&c_cmd, exe_path("splc0"), "--dump", (char *)mode, (char *)fname,
|
|
||||||
BUILD_FOLDER "diff_c.sir");
|
|
||||||
Nob_Cmd_Result c_res = {0};
|
|
||||||
if (!run_capture(&c_cmd, &c_res)) {
|
|
||||||
nob_cmd_result_free(c_res);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Nob_Cmd s_cmd = {0};
|
|
||||||
nob_cmd_append(&s_cmd, exe_path("spl_cli"), BUILD_FOLDER "splc1.sir", "--dump", (char *)mode,
|
|
||||||
(char *)fname);
|
|
||||||
Nob_Cmd_Result s_res = {0};
|
|
||||||
if (!run_capture(&s_cmd, &s_res)) {
|
|
||||||
nob_cmd_result_free(c_res);
|
|
||||||
nob_cmd_result_free(s_res);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Nob_String_View c_out = nob_sb_to_sv(c_res.out);
|
|
||||||
Nob_String_View s_out = nob_sb_to_sv(s_res.out);
|
|
||||||
bool same = nob_sv_eq(c_out, s_out);
|
|
||||||
if (!same) {
|
|
||||||
nob_log(NOB_ERROR, "diff mismatch: %s (c=%zu bytes, spl=%zu bytes)", fname, c_out.count,
|
|
||||||
s_out.count);
|
|
||||||
// 打印首个不相同行(按行比较)。
|
|
||||||
Nob_String_View ca = c_out;
|
|
||||||
Nob_String_View sa = s_out;
|
|
||||||
size_t lineno = 1;
|
|
||||||
while (ca.count > 0 || sa.count > 0) {
|
|
||||||
Nob_String_View cl = nob_sv_chop_by_delim(&ca, '\n');
|
|
||||||
Nob_String_View sl = nob_sv_chop_by_delim(&sa, '\n');
|
|
||||||
if (!nob_sv_eq(cl, sl)) {
|
|
||||||
nob_log(NOB_ERROR, " line %zu:", lineno);
|
|
||||||
nob_log(NOB_ERROR, " c : %.*s", (int)cl.count, cl.data);
|
|
||||||
nob_log(NOB_ERROR, " spl: %.*s", (int)sl.count, sl.data);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
lineno++;
|
|
||||||
}
|
|
||||||
nob_log(NOB_ERROR, "--- c stderr/stdout merged above; exit=%d vs spl exit=%d ---",
|
|
||||||
c_res.exit_code, s_res.exit_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
nob_cmd_result_free(c_res);
|
|
||||||
nob_cmd_result_free(s_res);
|
|
||||||
return same;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_stage_name(const char *s) {
|
|
||||||
return strcmp(s, "stage1") == 0 || strcmp(s, "stage2") == 0 || strcmp(s, "diff") == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_dump_mode(const char *s) {
|
|
||||||
return strcmp(s, "lex") == 0 || strcmp(s, "ast") == 0 || strcmp(s, "sema") == 0 ||
|
|
||||||
strcmp(s, "ir") == 0 || strcmp(s, "vm") == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *stage_dir_of(const char *stage) {
|
|
||||||
if (strcmp(stage, "stage2") == 0)
|
|
||||||
return "./stage2/";
|
|
||||||
return "./tests/stage1/";
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef bool (*test_fn_t)(const char *fname, void *user_data);
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Nob_Files *files;
|
Nob_Cmd compiler; // 选定编译器的调用前缀(已构建)
|
||||||
test_fn_t test_once;
|
Nob_Cmd vm; // 跑编译产物的 spl_cli 前缀(--mode 时不用)
|
||||||
void *user_data;
|
const char *out_sir;
|
||||||
} test_walk_ctx_t;
|
const char *mode; // 非 NULL:只做 `--dump <mode>` 冒烟
|
||||||
|
const scc_argparse_list_t *filters; // 按文件名子串过滤,空则全跑
|
||||||
|
int passed;
|
||||||
|
int failed;
|
||||||
|
int no_expect; // 没写 Expect 注解、按 0 期望的文件数
|
||||||
|
} test_ctx_t;
|
||||||
|
|
||||||
static bool test_walk_entry(Nob_Walk_Entry entry) {
|
static void test_compile_and_run(const char *fname, test_ctx_t *ctx) {
|
||||||
|
Nob_Cmd compile = {0};
|
||||||
|
nob_cmd_extend(&compile, &ctx->compiler);
|
||||||
|
nob_cmd_append(&compile, fname, ctx->out_sir);
|
||||||
|
bool compiled = nob_cmd_run(&compile);
|
||||||
|
nob_cmd_free(compile);
|
||||||
|
if (!compiled) {
|
||||||
|
nob_log(NOB_ERROR, "%s: compile failed", fname);
|
||||||
|
ctx->failed += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Nob_Cmd run = {0};
|
||||||
|
nob_cmd_extend(&run, &ctx->vm);
|
||||||
|
nob_cmd_append(&run, ctx->out_sir);
|
||||||
|
Nob_Cmd_Result result = {0};
|
||||||
|
if (!run_capture(&run, &result)) {
|
||||||
|
nob_log(NOB_ERROR, "%s: failed to run", fname);
|
||||||
|
ctx->failed += 1;
|
||||||
|
nob_cmd_free(run);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
nob_cmd_free(run);
|
||||||
|
|
||||||
|
test_expect_t expect = {0};
|
||||||
|
if (!test_parse_expect(fname, &expect)) {
|
||||||
|
nob_log(NOB_ERROR, "%s: broken `// Expect retcode = N`", fname);
|
||||||
|
ctx->failed += 1;
|
||||||
|
} else {
|
||||||
|
if (!expect.has_retcode) {
|
||||||
|
expect.retcode = 0; // 没写注解的库测试按"跑完退出 0"处理
|
||||||
|
ctx->no_expect += 1;
|
||||||
|
}
|
||||||
|
if (expect.retcode != result.exit_code) {
|
||||||
|
nob_log(NOB_ERROR, "%s: expect retcode %d, got %d", fname, expect.retcode,
|
||||||
|
result.exit_code);
|
||||||
|
ctx->failed += 1;
|
||||||
|
} else {
|
||||||
|
ctx->passed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nob_cmd_result_free(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --mode:只看选定的 target 能不能把这个文件 dump 出来,不比内容
|
||||||
|
static void test_dump(const char *fname, test_ctx_t *ctx) {
|
||||||
|
Nob_Cmd cmd = {0};
|
||||||
|
nob_cmd_extend(&cmd, &ctx->compiler);
|
||||||
|
append_dump_args(&cmd, ctx->mode, fname);
|
||||||
|
if (nob_cmd_run(&cmd)) {
|
||||||
|
ctx->passed += 1;
|
||||||
|
} else {
|
||||||
|
nob_log(NOB_ERROR, "%s: `--dump %s` failed", fname, ctx->mode);
|
||||||
|
ctx->failed += 1;
|
||||||
|
}
|
||||||
|
nob_cmd_free(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只负责跑:编译器由 cli_test 在建 suite 时构建一次。
|
||||||
|
static void test_run_file(const char *fname, void *user_data) {
|
||||||
|
test_ctx_t *ctx = (test_ctx_t *)user_data;
|
||||||
|
nob_log(NOB_INFO, "--- test %s ---", fname);
|
||||||
|
if (ctx->mode)
|
||||||
|
test_dump(fname, ctx);
|
||||||
|
else
|
||||||
|
test_compile_and_run(fname, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 目录遍历(test / diff 共用)----
|
||||||
|
typedef void (*walk_file_fn)(const char *fname, void *user_data);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const scc_argparse_list_t *filters;
|
||||||
|
walk_file_fn fn;
|
||||||
|
void *user_data;
|
||||||
|
} walk_ctx_t;
|
||||||
|
|
||||||
|
static const char *path_basename(const char *path) {
|
||||||
|
const char *base = path;
|
||||||
|
for (const char *p = path; *p; p += 1) {
|
||||||
|
if (*p == '/' || *p == '\\')
|
||||||
|
base = p + 1;
|
||||||
|
}
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool walk_match_filter(const char *path, const scc_argparse_list_t *filters) {
|
||||||
|
if (filters->count == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
const char *base = path_basename(path); // 只比文件名,不碰目录名
|
||||||
|
for (usize i = 0; i < filters->count; i += 1) {
|
||||||
|
if (strstr(base, filters->items[i]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单个用例失败只计数,遍历继续。
|
||||||
|
static bool walk_entry(Nob_Walk_Entry entry) {
|
||||||
if (entry.type != NOB_FILE_REGULAR)
|
if (entry.type != NOB_FILE_REGULAR)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -470,39 +486,95 @@ static bool test_walk_entry(Nob_Walk_Entry entry) {
|
|||||||
if (!dot || strcmp(dot, ".spl") != 0)
|
if (!dot || strcmp(dot, ".spl") != 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
test_walk_ctx_t *ctx = (test_walk_ctx_t *)entry.data;
|
walk_ctx_t *ctx = (walk_ctx_t *)entry.data;
|
||||||
bool run_all = (ctx->files->count == 0);
|
if (!walk_match_filter(entry.path, ctx->filters))
|
||||||
bool matched = run_all;
|
|
||||||
for (int i = 0; !matched && i < ctx->files->count; i += 1) {
|
|
||||||
if (strstr(entry.path, ctx->files->items[i]))
|
|
||||||
matched = true;
|
|
||||||
}
|
|
||||||
if (!matched)
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
printf("--- test %s ---\n", entry.path);
|
ctx->fn(entry.path, ctx->user_data);
|
||||||
if (ctx->test_once(entry.path, ctx->user_data) == false)
|
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool run_test(const char *stage, Nob_Files *files, test_fn_t test_once, void *user_data) {
|
static bool walk_spl_files(const char *dir, const scc_argparse_list_t *filters, walk_file_fn fn,
|
||||||
const char *test_folder = stage_dir_of(stage);
|
void *user_data) {
|
||||||
|
walk_ctx_t ctx = {
|
||||||
if (!prepare_build())
|
.filters = filters,
|
||||||
return false;
|
.fn = fn,
|
||||||
|
|
||||||
if (!nob_file_exists(test_folder)) {
|
|
||||||
nob_log(NOB_INFO, "%s does not exist, skip", test_folder);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
test_walk_ctx_t ctx = {
|
|
||||||
.files = files,
|
|
||||||
.test_once = test_once,
|
|
||||||
.user_data = user_data,
|
.user_data = user_data,
|
||||||
};
|
};
|
||||||
return nob_walk_dir(test_folder, test_walk_entry, .data = &ctx);
|
return nob_walk_dir(dir, walk_entry, .data = &ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- diff:两条流水线对撞 ----
|
||||||
|
// 对每份匹配的文件跑同一个 `--dump <mode>`,逐字节比 stdout。
|
||||||
|
typedef struct {
|
||||||
|
const char *expect_name; // 仅用于报错文案
|
||||||
|
const char *got_name;
|
||||||
|
Nob_Cmd expect; // 两侧的调用前缀(已构建)
|
||||||
|
Nob_Cmd got;
|
||||||
|
const char *mode;
|
||||||
|
const scc_argparse_list_t *filters;
|
||||||
|
int passed;
|
||||||
|
int failed;
|
||||||
|
} diff_ctx_t;
|
||||||
|
|
||||||
|
static void report_first_diff(const char *name, Nob_String_View expect, Nob_String_View got) {
|
||||||
|
nob_log(NOB_ERROR, "diff mismatch: %s (expect %zu bytes, got %zu bytes)", name, expect.count,
|
||||||
|
got.count);
|
||||||
|
|
||||||
|
Nob_String_View el = expect;
|
||||||
|
Nob_String_View gl = got;
|
||||||
|
size_t lineno = 1;
|
||||||
|
while (el.count > 0 || gl.count > 0) {
|
||||||
|
Nob_String_View e = nob_sv_chop_by_delim(&el, '\n');
|
||||||
|
Nob_String_View g = nob_sv_chop_by_delim(&gl, '\n');
|
||||||
|
if (!nob_sv_eq(e, g)) {
|
||||||
|
nob_log(NOB_ERROR, " line %zu:", lineno);
|
||||||
|
nob_log(NOB_ERROR, " expect: %.*s", (int)e.count, e.data);
|
||||||
|
nob_log(NOB_ERROR, " got : %.*s", (int)g.count, g.data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lineno += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void diff_run_file(const char *fname, void *user_data) {
|
||||||
|
diff_ctx_t *ctx = (diff_ctx_t *)user_data;
|
||||||
|
|
||||||
|
Nob_Cmd expect_cmd = {0};
|
||||||
|
nob_cmd_extend(&expect_cmd, &ctx->expect);
|
||||||
|
append_dump_args(&expect_cmd, ctx->mode, fname);
|
||||||
|
Nob_Cmd_Result expect = {0};
|
||||||
|
if (!run_capture(&expect_cmd, &expect)) {
|
||||||
|
nob_log(NOB_ERROR, "%s: failed to run `%s`", fname, ctx->expect_name);
|
||||||
|
ctx->failed += 1;
|
||||||
|
nob_cmd_free(expect_cmd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
nob_cmd_free(expect_cmd);
|
||||||
|
|
||||||
|
Nob_Cmd got_cmd = {0};
|
||||||
|
nob_cmd_extend(&got_cmd, &ctx->got);
|
||||||
|
append_dump_args(&got_cmd, ctx->mode, fname);
|
||||||
|
Nob_Cmd_Result got = {0};
|
||||||
|
if (!run_capture(&got_cmd, &got)) {
|
||||||
|
nob_log(NOB_ERROR, "%s: failed to run `%s`", fname, ctx->got_name);
|
||||||
|
ctx->failed += 1;
|
||||||
|
nob_cmd_free(got_cmd);
|
||||||
|
nob_cmd_result_free(expect);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
nob_cmd_free(got_cmd);
|
||||||
|
|
||||||
|
if (nob_sv_eq(nob_sb_to_sv(expect.out), nob_sb_to_sv(got.out))) {
|
||||||
|
ctx->passed += 1;
|
||||||
|
} else {
|
||||||
|
report_first_diff(fname, nob_sb_to_sv(expect.out), nob_sb_to_sv(got.out));
|
||||||
|
nob_log(NOB_ERROR, " `%s` exit=%d, `%s` exit=%d", ctx->expect_name, expect.exit_code,
|
||||||
|
ctx->got_name, got.exit_code);
|
||||||
|
ctx->failed += 1;
|
||||||
|
}
|
||||||
|
nob_cmd_result_free(expect);
|
||||||
|
nob_cmd_result_free(got);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool delete_walk_entry(Nob_Walk_Entry entry) { return nob_delete_file(entry.path); }
|
static bool delete_walk_entry(Nob_Walk_Entry entry) { return nob_delete_file(entry.path); }
|
||||||
@@ -511,18 +583,144 @@ static bool delete_directory_recursively(const char *dir_path) {
|
|||||||
return nob_walk_dir(dir_path, delete_walk_entry, .post_order = true);
|
return nob_walk_dir(dir_path, delete_walk_entry, .post_order = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_help(const char *program_name) {
|
typedef struct {
|
||||||
nob_log(NOB_INFO, "SPL C-side bootstrap build system (nob)");
|
cbool build, run, test, diff, clean, list; // 命中的子命令
|
||||||
nob_log(NOB_INFO, "%s", " ");
|
scc_argparse_list_t build_targets;
|
||||||
nob_log(NOB_INFO, "Usage: %s <command> [args...]", program_name);
|
const char *run_target;
|
||||||
nob_log(NOB_INFO, "%s", " ");
|
scc_argparse_list_t run_args;
|
||||||
nob_log(NOB_INFO, "Commands:");
|
int test_suite; // TEST_SUITES 下标
|
||||||
nob_log(NOB_INFO, " build [targets...] build all targets or the given ones");
|
const char *test_target; // 用哪个编译器编,NULL 用 suite 默认
|
||||||
nob_log(NOB_INFO, " run <target> [args] build and run a target");
|
int test_mode; // TEST_MODE_NAMES 下标,-1 表示不做 dump
|
||||||
nob_log(NOB_INFO, " test <kind> build and run tests");
|
scc_argparse_list_t test_filters;
|
||||||
nob_log(NOB_INFO, " clean remove build artifacts");
|
const char *diff_expect;
|
||||||
nob_log(NOB_INFO, " list list all targets");
|
const char *diff_got;
|
||||||
nob_log(NOB_INFO, " help/-h/--help print this help message");
|
int diff_suite; // TEST_SUITES 下标
|
||||||
|
int diff_mode; // TEST_MODE_NAMES 下标,-1 表示没给
|
||||||
|
scc_argparse_list_t diff_filters;
|
||||||
|
} nob_cli_t;
|
||||||
|
|
||||||
|
static int cli_build(nob_cli_t *cli) {
|
||||||
|
if (!prepare_build())
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
int rc = 0;
|
||||||
|
if (cli->build_targets.count == 0) {
|
||||||
|
rc = build_target(NULL) ? 0 : 1;
|
||||||
|
} else {
|
||||||
|
for (usize i = 0; i < cli->build_targets.count && rc == 0; i += 1) {
|
||||||
|
if (!build_target(cli->build_targets.items[i]))
|
||||||
|
rc = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cli_run(nob_cli_t *cli) {
|
||||||
|
Nob_Cmd runcmd = {0};
|
||||||
|
for (usize i = 0; i < cli->run_args.count; i += 1)
|
||||||
|
nob_cmd_append(&runcmd, cli->run_args.items[i]);
|
||||||
|
return run_target(cli->run_target, &runcmd) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cli_test(nob_cli_t *cli) {
|
||||||
|
const test_suite_t *suite = &TEST_SUITES[cli->test_suite];
|
||||||
|
const char *target = cli->test_target;
|
||||||
|
const char *mode = (cli->test_mode >= 0) ? TEST_MODE_NAMES[cli->test_mode] : NULL;
|
||||||
|
|
||||||
|
if (!prepare_build())
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
test_ctx_t ctx = {
|
||||||
|
.out_sir = BUILD_FOLDER "t.sir",
|
||||||
|
.mode = mode,
|
||||||
|
.filters = &cli->test_filters,
|
||||||
|
};
|
||||||
|
// build_run_prefix 会连带构建解释器;每个 target 只在这里构建一次
|
||||||
|
// (BUILD_SIR 没有新鲜度检查,放进逐用例循环会真重编)。
|
||||||
|
if (!build_run_prefix(target, &ctx.compiler))
|
||||||
|
return 1;
|
||||||
|
if (mode == NULL && !build_run_prefix("spl_cli", &ctx.vm))
|
||||||
|
return 1; // 编译产物由 spl_cli 跑
|
||||||
|
|
||||||
|
if (!nob_file_exists(suite->dir)) {
|
||||||
|
nob_log(NOB_INFO, "%s does not exist, skip", suite->dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool walked = walk_spl_files(suite->dir, &cli->test_filters, test_run_file, &ctx);
|
||||||
|
|
||||||
|
if (ctx.no_expect > 0)
|
||||||
|
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed (%d without `// Expect`)", suite->name,
|
||||||
|
target, ctx.passed, ctx.failed, ctx.no_expect);
|
||||||
|
else
|
||||||
|
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed", suite->name, target, ctx.passed,
|
||||||
|
ctx.failed);
|
||||||
|
|
||||||
|
nob_cmd_free(ctx.compiler);
|
||||||
|
nob_cmd_free(ctx.vm);
|
||||||
|
return (walked && ctx.failed == 0) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// diff:expect 目标(参考实现)与本目标各自 `--dump <mode>` 同一批文件,逐字节比 stdout。
|
||||||
|
static int cli_diff(nob_cli_t *cli) {
|
||||||
|
const test_suite_t *suite = &TEST_SUITES[cli->diff_suite];
|
||||||
|
|
||||||
|
if (!prepare_build())
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
diff_ctx_t ctx = {
|
||||||
|
.expect_name = cli->diff_expect,
|
||||||
|
.got_name = cli->diff_got,
|
||||||
|
.mode = TEST_MODE_NAMES[cli->diff_mode],
|
||||||
|
.filters = &cli->diff_filters,
|
||||||
|
};
|
||||||
|
// 同样两个 target 各只构建一次
|
||||||
|
if (!build_run_prefix(cli->diff_expect, &ctx.expect))
|
||||||
|
return 1;
|
||||||
|
if (!build_run_prefix(cli->diff_got, &ctx.got))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!nob_file_exists(suite->dir)) {
|
||||||
|
nob_log(NOB_INFO, "%s does not exist, skip", suite->dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
nob_log(NOB_INFO, "--- diff `%s` vs `%s` (--dump %s) ---", ctx.expect_name, ctx.got_name,
|
||||||
|
ctx.mode);
|
||||||
|
bool walked = walk_spl_files(suite->dir, &cli->diff_filters, diff_run_file, &ctx);
|
||||||
|
nob_log(NOB_INFO, "diff: %d matched, %d mismatched", ctx.passed, ctx.failed);
|
||||||
|
|
||||||
|
nob_cmd_free(ctx.expect);
|
||||||
|
nob_cmd_free(ctx.got);
|
||||||
|
return (walked && ctx.failed == 0) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cli_clean(void) {
|
||||||
|
if (nob_file_exists(BUILD_FOLDER) && !delete_directory_recursively(BUILD_FOLDER))
|
||||||
|
return 1;
|
||||||
|
nob_log(NOB_INFO, "cleaned %s", BUILD_FOLDER);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cli_list(void) {
|
||||||
|
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
|
||||||
|
nob_log(NOB_INFO, "%s:", targets[i].name);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cli_dispatch(nob_cli_t *cli) {
|
||||||
|
if (cli->clean)
|
||||||
|
return cli_clean();
|
||||||
|
if (cli->list)
|
||||||
|
return cli_list();
|
||||||
|
if (cli->run)
|
||||||
|
return cli_run(cli);
|
||||||
|
if (cli->test)
|
||||||
|
return cli_test(cli);
|
||||||
|
if (cli->diff)
|
||||||
|
return cli_diff(cli);
|
||||||
|
return cli_build(cli); // build,或没给命令:默认构建全部
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
@@ -533,106 +731,116 @@ int main(int argc, char **argv) {
|
|||||||
NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob/nob.h", "nob/nob_popen.h", "nob/nob_data.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);
|
set_log_handler(nob_cancer_log_handler);
|
||||||
|
|
||||||
const char *program_name = nob_shift(argv, argc);
|
nob_cli_t cli = {0};
|
||||||
const char *command_name = "build";
|
cli.test_mode = -1;
|
||||||
if (argc > 0) {
|
cli.diff_mode = -1;
|
||||||
command_name = nob_shift(argv, argc);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(command_name, "build") == 0) {
|
scc_argparse_t parser = {0};
|
||||||
if (!prepare_build())
|
scc_argparse_init(&parser, argv[0], "SPL C-side bootstrap build system (nob)");
|
||||||
return 1;
|
scc_argparse_cmd_t *root = scc_argparse_get_root(&parser);
|
||||||
if (argc > 0) {
|
|
||||||
while (argc > 0) {
|
|
||||||
const char *target_name = nob_shift(argv, argc);
|
|
||||||
if (!build_target(target_name))
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!build_target(NULL))
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(command_name, "run") == 0) {
|
scc_argparse_cmd_t build_cmd;
|
||||||
if (argc <= 0) {
|
scc_argparse_cmd_init(&build_cmd, "build", "build all targets or the given ones");
|
||||||
nob_log(NOB_ERROR, "usage: %s run <target> [args...]", program_name);
|
scc_argparse_cmd_set_triggered(&build_cmd, &cli.build);
|
||||||
return 1;
|
scc_argparse_arg_t targets_arg;
|
||||||
}
|
scc_argparse_arg_init(&targets_arg, "targets", "target names, default all");
|
||||||
const char *target_name = nob_shift(argv, argc);
|
scc_argparse_spec_setup_list(&targets_arg.spec, &cli.build_targets);
|
||||||
Nob_Cmd runcmd = {0};
|
scc_argparse_cmd_add_arg(&build_cmd, &targets_arg);
|
||||||
while (argc > 0) {
|
scc_argparse_cmd_add_subcmd(root, &build_cmd);
|
||||||
nob_cmd_append(&runcmd, nob_shift(argv, argc));
|
|
||||||
}
|
|
||||||
return !run_target(target_name, &runcmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(command_name, "test") == 0) {
|
scc_argparse_cmd_t run_cmd;
|
||||||
// 1. stage(可选,默认 stage1)
|
scc_argparse_cmd_init(&run_cmd, "run", "build and run a target");
|
||||||
const char *stage = "stage1";
|
scc_argparse_cmd_set_triggered(&run_cmd, &cli.run);
|
||||||
if (argc > 0 && is_stage_name(argv[0])) {
|
scc_argparse_arg_t run_target_arg;
|
||||||
stage = nob_shift(argv, argc);
|
scc_argparse_arg_init(&run_target_arg, "target", "target name");
|
||||||
}
|
scc_argparse_spec_setup_string(&run_target_arg.spec, &cli.run_target);
|
||||||
|
scc_argparse_spec_set_required(&run_target_arg.spec, true);
|
||||||
|
scc_argparse_arg_t run_args_arg;
|
||||||
|
scc_argparse_arg_init(&run_args_arg, "args", "arguments for the target");
|
||||||
|
scc_argparse_spec_setup_list(&run_args_arg.spec, &cli.run_args);
|
||||||
|
scc_argparse_cmd_add_arg(&run_cmd, &run_target_arg);
|
||||||
|
scc_argparse_cmd_add_arg(&run_cmd, &run_args_arg);
|
||||||
|
scc_argparse_cmd_add_subcmd(root, &run_cmd);
|
||||||
|
|
||||||
// 2. mode(可选)
|
scc_argparse_cmd_t test_cmd;
|
||||||
test_fn_t tfn = test_once;
|
scc_argparse_cmd_init(&test_cmd, "test", "build and run tests");
|
||||||
const char *mode_name = NULL;
|
scc_argparse_cmd_set_triggered(&test_cmd, &cli.test);
|
||||||
if (argc > 0) {
|
const char *suite_names[NOB_ARRAY_LEN(TEST_SUITES)];
|
||||||
if (is_dump_mode(argv[0])) {
|
for (size_t i = 0; i < NOB_ARRAY_LEN(TEST_SUITES); i += 1)
|
||||||
tfn = test_once_with_dump;
|
suite_names[i] = TEST_SUITES[i].name;
|
||||||
mode_name = nob_shift(argv, argc);
|
scc_argparse_arg_t test_target_arg;
|
||||||
} else if (strcmp(argv[0], "run") == 0) {
|
scc_argparse_arg_init(&test_target_arg, "target", "compiler target, e.g. splc0|splc1|splc2");
|
||||||
nob_shift(argv, argc);
|
scc_argparse_spec_setup_string(&test_target_arg.spec, &cli.test_target);
|
||||||
// HACK:只对 stage1 有意义,跑上次生成的 build/test.sir
|
scc_argparse_spec_set_required(&test_target_arg.spec, true);
|
||||||
Nob_Cmd cmd = {0};
|
scc_argparse_cmd_add_arg(&test_cmd, &test_target_arg);
|
||||||
nob_cmd_append(&cmd, BUILD_FOLDER "spl_cli", "-d");
|
scc_argparse_opt_t suite_opt;
|
||||||
nob_cmd_append(&cmd, BUILD_FOLDER "test.sir");
|
scc_argparse_opt_init(&suite_opt, 's', "suite", "test suite, default stage1");
|
||||||
return nob_cmd_run(&cmd) ? 0 : 1;
|
scc_argparse_spec_setup_choices(&suite_opt.spec, suite_names, (int)NOB_ARRAY_LEN(TEST_SUITES),
|
||||||
}
|
&cli.test_suite);
|
||||||
}
|
scc_argparse_cmd_add_opt(&test_cmd, &suite_opt);
|
||||||
|
scc_argparse_opt_t mode_opt;
|
||||||
|
scc_argparse_opt_init(&mode_opt, 'm', "mode", "dump smoke test through the target");
|
||||||
|
scc_argparse_spec_setup_choices(&mode_opt.spec, TEST_MODE_NAMES,
|
||||||
|
(int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.test_mode);
|
||||||
|
scc_argparse_cmd_add_opt(&test_cmd, &mode_opt);
|
||||||
|
scc_argparse_opt_t filters_opt;
|
||||||
|
scc_argparse_opt_init(&filters_opt, 'f', "filter", "file name filter, repeatable");
|
||||||
|
scc_argparse_spec_setup_list(&filters_opt.spec, &cli.test_filters);
|
||||||
|
scc_argparse_cmd_add_opt(&test_cmd, &filters_opt);
|
||||||
|
scc_argparse_cmd_add_subcmd(root, &test_cmd);
|
||||||
|
|
||||||
// diff 需要显式 mode,且使用专门的双编译器比较函数。
|
scc_argparse_cmd_t diff_cmd;
|
||||||
if (strcmp(stage, "diff") == 0) {
|
scc_argparse_cmd_init(&diff_cmd, "diff", "compare two targets' `--dump` output");
|
||||||
if (!mode_name) {
|
scc_argparse_cmd_set_triggered(&diff_cmd, &cli.diff);
|
||||||
nob_log(NOB_ERROR, "diff requires a mode: lex|ast|sema|ir|vm");
|
scc_argparse_arg_t expect_arg;
|
||||||
return 1;
|
scc_argparse_arg_init(&expect_arg, "expect", "reference build target");
|
||||||
}
|
scc_argparse_spec_setup_string(&expect_arg.spec, &cli.diff_expect);
|
||||||
tfn = test_once_diff;
|
scc_argparse_spec_set_required(&expect_arg.spec, true);
|
||||||
}
|
scc_argparse_arg_t got_arg;
|
||||||
|
scc_argparse_arg_init(&got_arg, "got", "build target under test");
|
||||||
|
scc_argparse_spec_setup_string(&got_arg.spec, &cli.diff_got);
|
||||||
|
scc_argparse_spec_set_required(&got_arg.spec, true);
|
||||||
|
scc_argparse_cmd_add_arg(&diff_cmd, &expect_arg);
|
||||||
|
scc_argparse_cmd_add_arg(&diff_cmd, &got_arg);
|
||||||
|
scc_argparse_opt_t diff_mode_opt;
|
||||||
|
scc_argparse_opt_init(&diff_mode_opt, 'm', "mode", "dump mode");
|
||||||
|
scc_argparse_spec_setup_choices(&diff_mode_opt.spec, TEST_MODE_NAMES,
|
||||||
|
(int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.diff_mode);
|
||||||
|
scc_argparse_spec_set_required(&diff_mode_opt.spec, true);
|
||||||
|
scc_argparse_cmd_add_opt(&diff_cmd, &diff_mode_opt);
|
||||||
|
scc_argparse_opt_t diff_suite_opt;
|
||||||
|
scc_argparse_opt_init(&diff_suite_opt, 's', "suite", "test suite, default stage1");
|
||||||
|
scc_argparse_spec_setup_choices(&diff_suite_opt.spec, suite_names,
|
||||||
|
(int)NOB_ARRAY_LEN(TEST_SUITES), &cli.diff_suite);
|
||||||
|
scc_argparse_cmd_add_opt(&diff_cmd, &diff_suite_opt);
|
||||||
|
scc_argparse_opt_t diff_filter_opt;
|
||||||
|
scc_argparse_opt_init(&diff_filter_opt, 'f', "filter", "file name filter, repeatable");
|
||||||
|
scc_argparse_spec_setup_list(&diff_filter_opt.spec, &cli.diff_filters);
|
||||||
|
scc_argparse_cmd_add_opt(&diff_cmd, &diff_filter_opt);
|
||||||
|
scc_argparse_cmd_add_subcmd(root, &diff_cmd);
|
||||||
|
|
||||||
// 3. 剩下的都当 pattern
|
scc_argparse_cmd_t clean_cmd;
|
||||||
Nob_Files files = {0};
|
scc_argparse_cmd_init(&clean_cmd, "clean", "remove build artifacts");
|
||||||
while (argc > 0) {
|
scc_argparse_cmd_set_triggered(&clean_cmd, &cli.clean);
|
||||||
da_append(&files, nob_shift(argv, argc));
|
scc_argparse_cmd_add_subcmd(root, &clean_cmd);
|
||||||
}
|
|
||||||
|
|
||||||
return run_test(stage, &files, tfn, (void *)mode_name) ? 0 : 1;
|
scc_argparse_cmd_t list_cmd;
|
||||||
}
|
scc_argparse_cmd_init(&list_cmd, "list", "list all targets");
|
||||||
|
scc_argparse_cmd_set_triggered(&list_cmd, &cli.list);
|
||||||
|
scc_argparse_cmd_add_subcmd(root, &list_cmd);
|
||||||
|
|
||||||
if (strcmp(command_name, "clean") == 0) {
|
int rc = scc_argparse_parse(&parser, argc, (const char **)argv);
|
||||||
if (nob_file_exists(BUILD_FOLDER)) {
|
if (parser.out.count > 0)
|
||||||
if (!delete_directory_recursively(BUILD_FOLDER))
|
fwrite(parser.out.items, 1, parser.out.count, stdout);
|
||||||
return 1;
|
if (rc != SCC_ARGPARSE_ERR_NONE)
|
||||||
}
|
return rc == SCC_ARGPARSE_ERR_PNT_DEFAULT ? 0 : 1;
|
||||||
nob_log(NOB_INFO, "cleaned %s", BUILD_FOLDER);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(command_name, "list") == 0) {
|
rc = cli_dispatch(&cli);
|
||||||
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
|
|
||||||
nob_log(NOB_INFO, "%s:", targets[i].name);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(command_name, "help") == 0 || strcmp(command_name, "-h") == 0 ||
|
scc_vec_free(cli.build_targets);
|
||||||
strcmp(command_name, "--help") == 0) {
|
scc_vec_free(cli.run_args);
|
||||||
print_help(program_name);
|
scc_vec_free(cli.test_filters);
|
||||||
return 0;
|
scc_vec_free(cli.diff_filters);
|
||||||
}
|
scc_argparse_drop(&parser);
|
||||||
|
return rc;
|
||||||
nob_log(NOB_ERROR, "unknown command `%s`", command_name);
|
|
||||||
print_help(program_name);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
+1291
File diff suppressed because it is too large.
Load diff
+17
-2
@@ -10,6 +10,21 @@ static const char *stage0_headers[] = {
|
|||||||
"stage0/spl_vm_embed.h",
|
"stage0/spl_vm_embed.h",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* stage1 头也参与依赖:改结构体布局/枚举值时所有 splc0 对象必须重编,
|
||||||
|
否则残留旧布局的 .o 与新政 ABI 不一致(生成物随机错乱)。 */
|
||||||
|
static const char *splc0_headers[] = {
|
||||||
|
"stage0/spl_asp.h", "stage0/spl_mcode.h", "stage0/spl_syscall.h",
|
||||||
|
"stage0/spl_vm.h", "stage0/spl_rt_core.h", "stage0/spl_rt.h",
|
||||||
|
"stage0/spl_vm_embed.h",
|
||||||
|
"stage1/ir_opt.h", "stage1/vm_opt.h", "stage1/spl_ast.h",
|
||||||
|
"stage1/spl_ast2ir.h", "stage1/spl_attr.h", "stage1/spl_dbg.h",
|
||||||
|
"stage1/spl_diag.h", "stage1/spl_fold.h", "stage1/spl_intrin.h",
|
||||||
|
"stage1/spl_ir.h", "stage1/spl_ir2c.h", "stage1/spl_ir2vm.h",
|
||||||
|
"stage1/spl_lang_type.h", "stage1/spl_layout.h", "stage1/spl_lexer.h",
|
||||||
|
"stage1/spl_mpa.h", "stage1/spl_parser.h", "stage1/spl_scope.h",
|
||||||
|
"stage1/spl_sema.h", "stage1/spl_tok.h", "stage1/spl_type.h",
|
||||||
|
};
|
||||||
|
|
||||||
static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS};
|
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 *vm_test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS};
|
||||||
static const char *splc0_srcs[] = {
|
static const char *splc0_srcs[] = {
|
||||||
@@ -243,8 +258,8 @@ static build_t targets[] = {
|
|||||||
BUILD_EXE,
|
BUILD_EXE,
|
||||||
.exe = {.srcs = splc0_srcs,
|
.exe = {.srcs = splc0_srcs,
|
||||||
.srcs_count = NOB_ARRAY_LEN(splc0_srcs),
|
.srcs_count = NOB_ARRAY_LEN(splc0_srcs),
|
||||||
.headers = stage0_headers,
|
.headers = splc0_headers,
|
||||||
.headers_count = NOB_ARRAY_LEN(stage0_headers)},
|
.headers_count = NOB_ARRAY_LEN(splc0_headers)},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"splc1",
|
"splc1",
|
||||||
|
|||||||
+317
@@ -0,0 +1,317 @@
|
|||||||
|
#ifndef __SCC_OPTPARSER_H__
|
||||||
|
#define __SCC_OPTPARSER_H__
|
||||||
|
|
||||||
|
#ifndef nullptr
|
||||||
|
#define nullptr ((void *)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct scc_optparse_opt {
|
||||||
|
char prefix;
|
||||||
|
char short_name;
|
||||||
|
const char *long_name;
|
||||||
|
void (*handle)(void *user_data);
|
||||||
|
void *user_data;
|
||||||
|
int min_args;
|
||||||
|
int max_args;
|
||||||
|
} scc_optparse_opt_t;
|
||||||
|
|
||||||
|
#define SCC_OPTPARSE_OPT(prefix, short_name, long_name, min_args, max_args) \
|
||||||
|
{prefix, short_name, long_name, 0, 0, min_args, max_args}
|
||||||
|
#define SCC_OPTPARSE_OPT_END() {0}
|
||||||
|
|
||||||
|
typedef enum scc_optparse_error {
|
||||||
|
SCC_OPT_ERROR_NONE,
|
||||||
|
SCC_OPT_ERROR_SEPARATOR, // 命中了分隔符本身:不是失败,交给调用方决定吞或留
|
||||||
|
SCC_OPT_ERROR_NOT_FOUND_LONG_ARG,
|
||||||
|
SCC_OPT_ERROR_NOT_FOUND_SHORT_ARG,
|
||||||
|
SCC_OPT_ERROR_NOT_ENOUGH_ARGS,
|
||||||
|
SCC_OPT_ERROR_TOO_MANY_ARGS,
|
||||||
|
} scc_optparse_error_t;
|
||||||
|
|
||||||
|
typedef struct scc_optparse_state {
|
||||||
|
const scc_optparse_opt_t *opt;
|
||||||
|
int count; // check for min_args <= count <= max_args
|
||||||
|
int arg_pos; // for argv pos
|
||||||
|
int opt_pos; // for short pos
|
||||||
|
} scc_optparse_state_t;
|
||||||
|
|
||||||
|
typedef struct scc_optparse_result {
|
||||||
|
const scc_optparse_opt_t *opt;
|
||||||
|
const char *value;
|
||||||
|
int error;
|
||||||
|
const char *raw_arg;
|
||||||
|
} scc_optparse_result_t;
|
||||||
|
|
||||||
|
typedef struct scc_optparse {
|
||||||
|
int argc;
|
||||||
|
const char **argv;
|
||||||
|
const scc_optparse_opt_t *opts;
|
||||||
|
int handle_positional;
|
||||||
|
int greedy_mode;
|
||||||
|
char short_prefix; // 短选项前缀,默认 '-'(匹配仍用每项的 opt->prefix)
|
||||||
|
const char *long_prefix; // 长选项前缀,长度任意,默认 "--";nullptr = 不支持
|
||||||
|
const char *separator; // 分隔符,整 token 精确匹配,默认 "--";nullptr = 不识别
|
||||||
|
scc_optparse_state_t current;
|
||||||
|
} scc_optparse_t;
|
||||||
|
|
||||||
|
void scc_optparse_init(scc_optparse_t *parser, int argc, const char **argv);
|
||||||
|
void scc_optparse_drop(scc_optparse_t *parser);
|
||||||
|
void scc_optparse_swap_opts(scc_optparse_t *parser, const scc_optparse_opt_t *opts);
|
||||||
|
void scc_optparse_set(scc_optparse_t *parser, const scc_optparse_opt_t *opts);
|
||||||
|
void scc_optparse_reset(scc_optparse_t *parser);
|
||||||
|
int scc_optparse_parse(scc_optparse_t *parser, scc_optparse_result_t *res);
|
||||||
|
|
||||||
|
#ifdef SCC_OPTPARSER_IMPLEMENTATION
|
||||||
|
void scc_optparse_init(scc_optparse_t *parser, int argc, const char **argv) {
|
||||||
|
parser->argc = argc;
|
||||||
|
parser->argv = argv;
|
||||||
|
parser->opts = 0;
|
||||||
|
parser->short_prefix = '-';
|
||||||
|
parser->long_prefix = "--";
|
||||||
|
parser->separator = "--";
|
||||||
|
scc_optparse_reset(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
void scc_optparse_drop(scc_optparse_t *parser) { (void)parser; }
|
||||||
|
|
||||||
|
void scc_optparse_swap_opts(scc_optparse_t *parser, const scc_optparse_opt_t *opts) {
|
||||||
|
parser->opts = opts;
|
||||||
|
parser->current.opt = 0;
|
||||||
|
parser->current.count = 0;
|
||||||
|
parser->current.opt_pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void scc_optparse_set(scc_optparse_t *parser, const scc_optparse_opt_t *opts) {
|
||||||
|
parser->opts = opts;
|
||||||
|
scc_optparse_reset(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
void scc_optparse_reset(scc_optparse_t *parser) {
|
||||||
|
parser->handle_positional = 0;
|
||||||
|
parser->current.opt = 0;
|
||||||
|
parser->current.count = 0;
|
||||||
|
parser->current.arg_pos = 1;
|
||||||
|
parser->current.opt_pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const char *str_chr(const char *s, char c) {
|
||||||
|
while (*s && *s != c)
|
||||||
|
s++;
|
||||||
|
return *s ? s : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// optparse 一直不依赖 libc,这几个字符串比较自己写(长度用 int,和本文件其余部分一致)。
|
||||||
|
static inline int scc_optparse_str_len(const char *s) {
|
||||||
|
int n = 0;
|
||||||
|
while (s[n])
|
||||||
|
n++;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int scc_optparse_str_eq(const char *a, const char *b) {
|
||||||
|
while (*a && *a == *b) {
|
||||||
|
a++;
|
||||||
|
b++;
|
||||||
|
}
|
||||||
|
return *a == *b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int scc_optparse_str_has_prefix(const char *s, const char *prefix, int len) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (s[i] != prefix[i])
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scc_optparse_opt_t *scc_optparse_get_long_name(const scc_optparse_opt_t *opts,
|
||||||
|
const char *name, const char end) {
|
||||||
|
if (*name == '\0')
|
||||||
|
return 0;
|
||||||
|
for (const scc_optparse_opt_t *opt = opts; opt->prefix; ++opt) {
|
||||||
|
if (!opt->long_name) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int i;
|
||||||
|
for (i = 0; opt->long_name[i]; ++i) {
|
||||||
|
if (name[i] != opt->long_name[i]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (opt->long_name[i] == '\0' && (name[i] == '\0' || name[i] == end))
|
||||||
|
return opt;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scc_optparse_opt_t *scc_optparse_get_short_name(const scc_optparse_opt_t *opts,
|
||||||
|
const char short_name) {
|
||||||
|
for (const scc_optparse_opt_t *opt = opts; opt->prefix; ++opt) {
|
||||||
|
if (opt->short_name == short_name) {
|
||||||
|
return opt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void scc_optparse_parse_long_name(const scc_optparse_opt_t *opts,
|
||||||
|
scc_optparse_result_t *res, const char *arg,
|
||||||
|
int name_offset) {
|
||||||
|
// caller MUST trim the configured long prefix already
|
||||||
|
const scc_optparse_opt_t *ret = 0;
|
||||||
|
const char *chr = str_chr(arg, '=');
|
||||||
|
if (chr) {
|
||||||
|
// --file=a.txt
|
||||||
|
ret = scc_optparse_get_long_name(opts, arg + name_offset, '=');
|
||||||
|
if (ret == 0) {
|
||||||
|
res->error = SCC_OPT_ERROR_NOT_FOUND_LONG_ARG;
|
||||||
|
} else {
|
||||||
|
res->opt = ret;
|
||||||
|
if (chr[1] == '\0') {
|
||||||
|
// TODO maybe can empty like -file=
|
||||||
|
res->error = SCC_OPT_ERROR_NOT_ENOUGH_ARGS;
|
||||||
|
}
|
||||||
|
res->value = chr + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// --file a.txt
|
||||||
|
ret = scc_optparse_get_long_name(opts, arg + name_offset, '\0');
|
||||||
|
if (ret == 0) {
|
||||||
|
res->error = SCC_OPT_ERROR_NOT_FOUND_LONG_ARG;
|
||||||
|
} else {
|
||||||
|
res->opt = ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void scc_optparse_parse_short_name(const scc_optparse_opt_t *opts,
|
||||||
|
scc_optparse_result_t *res, const char *arg,
|
||||||
|
int arg_start) {
|
||||||
|
// const char *chr =
|
||||||
|
// str_chr(arg + arg_start, '='); // TODO maybe short can have -I=/usr
|
||||||
|
const scc_optparse_opt_t *ret = scc_optparse_get_short_name(opts, arg[arg_start]);
|
||||||
|
if (ret == 0) {
|
||||||
|
res->error = SCC_OPT_ERROR_NOT_FOUND_SHORT_ARG;
|
||||||
|
} else {
|
||||||
|
res->opt = ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void scc_optparse_parse_position_arg(scc_optparse_t *parser, const char *arg,
|
||||||
|
scc_optparse_result_t *res) {
|
||||||
|
parser->current.arg_pos++;
|
||||||
|
res->value = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int scc_optparse_check(scc_optparse_t *parser, scc_optparse_result_t *res) {
|
||||||
|
if (!res->opt || res->error)
|
||||||
|
return 0;
|
||||||
|
if (res->value)
|
||||||
|
parser->current.count += 1;
|
||||||
|
if (parser->current.count < res->opt->max_args && res->opt->max_args > 0)
|
||||||
|
parser->current.opt = res->opt;
|
||||||
|
if (parser->current.count < res->opt->min_args && !res->value)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int scc_optparse_parse(scc_optparse_t *parser, scc_optparse_result_t *res) {
|
||||||
|
const scc_optparse_opt_t *opts = parser->opts;
|
||||||
|
const char *arg = 0;
|
||||||
|
res->opt = 0;
|
||||||
|
res->value = 0;
|
||||||
|
res->error = SCC_OPT_ERROR_NONE;
|
||||||
|
|
||||||
|
if (parser->current.opt_pos != 0) {
|
||||||
|
arg = parser->argv[parser->current.arg_pos];
|
||||||
|
goto CONTINUE_SHORT_OPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (parser->current.arg_pos < parser->argc) {
|
||||||
|
arg = parser->argv[parser->current.arg_pos];
|
||||||
|
|
||||||
|
if (arg[0] == '\0' || parser->handle_positional) {
|
||||||
|
scc_optparse_parse_position_arg(parser, arg, res);
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 分隔符:整 token 精确匹配,与前后缀解耦。以特殊 error 上报,不替调用方决定是吞是留。
|
||||||
|
if (parser->separator && parser->separator[0] &&
|
||||||
|
scc_optparse_str_eq(arg, parser->separator)) {
|
||||||
|
parser->handle_positional = 1; // “其后全是位置参数”属于分词
|
||||||
|
parser->current.arg_pos++;
|
||||||
|
res->error = SCC_OPT_ERROR_SEPARATOR;
|
||||||
|
res->value = arg;
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 长选项:按配置的前缀串匹配(长度任意);"分隔符"本身已在上一步处理掉。
|
||||||
|
int long_len = parser->long_prefix ? scc_optparse_str_len(parser->long_prefix) : 0;
|
||||||
|
if (long_len > 0 && scc_optparse_str_has_prefix(arg, parser->long_prefix, long_len) &&
|
||||||
|
arg[long_len] != '\0') {
|
||||||
|
parser->current.opt = 0;
|
||||||
|
parser->current.count = 0;
|
||||||
|
scc_optparse_parse_long_name(opts, res, arg, long_len);
|
||||||
|
parser->current.arg_pos += 1;
|
||||||
|
if (scc_optparse_check(parser, res)) {
|
||||||
|
goto NEXT;
|
||||||
|
}
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const scc_optparse_opt_t *opt = opts; opt && opt->prefix; ++opt) {
|
||||||
|
if (arg[0] != opt->prefix) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser->current.opt = 0;
|
||||||
|
parser->current.count = 0;
|
||||||
|
// short option like -
|
||||||
|
if (arg[1] == '\0') {
|
||||||
|
// the `-`
|
||||||
|
res->value = arg;
|
||||||
|
parser->current.arg_pos++;
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
parser->current.opt_pos = 1;
|
||||||
|
CONTINUE_SHORT_OPTION:
|
||||||
|
scc_optparse_parse_short_name(opts, res, arg, parser->current.opt_pos++);
|
||||||
|
int have_next = arg[parser->current.opt_pos];
|
||||||
|
|
||||||
|
parser->current.opt_pos = have_next ? parser->current.opt_pos : 0;
|
||||||
|
parser->current.arg_pos += have_next ? 0 : 1;
|
||||||
|
if (scc_optparse_check(parser, res)) {
|
||||||
|
if (have_next) {
|
||||||
|
res->value = arg + parser->current.opt_pos;
|
||||||
|
parser->current.opt_pos = 0;
|
||||||
|
parser->current.arg_pos += 1;
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
goto NEXT;
|
||||||
|
}
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
// position arg
|
||||||
|
if (parser->current.opt && parser->current.count < parser->current.opt->max_args) {
|
||||||
|
res->opt = parser->current.opt;
|
||||||
|
res->value = arg;
|
||||||
|
parser->current.count++;
|
||||||
|
parser->current.arg_pos++;
|
||||||
|
goto RETURN;
|
||||||
|
} else {
|
||||||
|
scc_optparse_parse_position_arg(parser, arg, res);
|
||||||
|
goto RETURN;
|
||||||
|
}
|
||||||
|
NEXT:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
RETURN:
|
||||||
|
if (res->opt && res->opt->max_args > 0 && parser->current.count > res->opt->max_args) {
|
||||||
|
res->error = SCC_OPT_ERROR_TOO_MANY_ARGS;
|
||||||
|
}
|
||||||
|
res->raw_arg = arg;
|
||||||
|
return res->opt != 0 || res->value != 0 || res->error != SCC_OPT_ERROR_NONE;
|
||||||
|
}
|
||||||
|
#endif // SCC_OPTPARSER_IMPLEMENTATION
|
||||||
|
|
||||||
|
#endif /* __SCC_OPTPARSER_H__ */
|
||||||
+93
-154
@@ -33,98 +33,116 @@ static spl_ir_node_ref_t rw(inline_ctx_t *c, spl_ir_node_ref_t r) {
|
|||||||
return r + c->shift;
|
return r + c->shift;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rw_vec(inline_ctx_t *c, spl_ir_node_ref_vec_t *v) {
|
typedef spl_ir_node_ref_t (*ref_xform_t)(void *ud, spl_ir_node_ref_t ref);
|
||||||
|
|
||||||
|
static void xform_vec(spl_ir_node_ref_vec_t *v, ref_xform_t fx, void *ud) {
|
||||||
for (usize i = 0; i < vec_size(*v); i++)
|
for (usize i = 0; i < vec_size(*v); i++)
|
||||||
vec_at(*v, i) = rw(c, vec_at(*v, i));
|
vec_at(*v, i) = fx(ud, vec_at(*v, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rw_node(inline_ctx_t *c, spl_ir_node_t *n) {
|
/* 对节点的所有「值引用」字段应用 fx;块标签字段不在此列(由调用方单独处理)。 */
|
||||||
|
static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) {
|
||||||
switch (n->kind) {
|
switch (n->kind) {
|
||||||
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL:
|
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL:
|
||||||
case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: case SPL_IR_ARITH_NEG:
|
case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: case SPL_IR_ARITH_NEG:
|
||||||
case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_AND: case SPL_IR_ARITH_OR:
|
case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_AND: case SPL_IR_ARITH_OR:
|
||||||
case SPL_IR_ARITH_XOR: case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR:
|
case SPL_IR_ARITH_XOR: case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR:
|
||||||
case SPL_IR_ARITH_NOT:
|
case SPL_IR_ARITH_NOT:
|
||||||
n->arith.left = rw(c, n->arith.left);
|
n->arith.left = fx(ud, n->arith.left);
|
||||||
n->arith.right = rw(c, n->arith.right);
|
n->arith.right = fx(ud, n->arith.right);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: case SPL_IR_CMP_LT:
|
case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: case SPL_IR_CMP_LT:
|
||||||
case SPL_IR_CMP_LE: case SPL_IR_CMP_GT: case SPL_IR_CMP_GE:
|
case SPL_IR_CMP_LE: case SPL_IR_CMP_GT: case SPL_IR_CMP_GE:
|
||||||
n->cmp.a = rw(c, n->cmp.a);
|
n->cmp.a = fx(ud, n->cmp.a);
|
||||||
n->cmp.b = rw(c, n->cmp.b);
|
n->cmp.b = fx(ud, n->cmp.b);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_SEXT:
|
case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_SEXT:
|
||||||
case SPL_IR_CAST_FEXT: case SPL_IR_CAST_FTRUNC: case SPL_IR_CAST_BITCAST:
|
case SPL_IR_CAST_FEXT: case SPL_IR_CAST_FTRUNC: case SPL_IR_CAST_BITCAST:
|
||||||
case SPL_IR_CAST_PTR2INT: case SPL_IR_CAST_INT2PTR: case SPL_IR_CAST_BOOL2INT:
|
case SPL_IR_CAST_PTR2INT: case SPL_IR_CAST_INT2PTR: case SPL_IR_CAST_BOOL2INT:
|
||||||
case SPL_IR_CASE_INT2FLOAT: case SPL_IR_CASE_FLOAT2INT:
|
case SPL_IR_CASE_INT2FLOAT: case SPL_IR_CASE_FLOAT2INT:
|
||||||
n->cast.val = rw(c, n->cast.val);
|
n->cast.val = fx(ud, n->cast.val);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_ALLOCA:
|
case SPL_IR_MEM_ALLOCA:
|
||||||
n->mem_alloc.count = rw(c, n->mem_alloc.count);
|
n->mem_alloc.count = fx(ud, n->mem_alloc.count);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_GLOBAL_ALLOC:
|
case SPL_IR_MEM_GLOBAL_ALLOC:
|
||||||
n->mem_global_alloc.const_node = rw(c, n->mem_global_alloc.const_node);
|
n->mem_global_alloc.const_node = fx(ud, n->mem_global_alloc.const_node);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_LOAD:
|
case SPL_IR_MEM_LOAD:
|
||||||
n->mem_load.ptr = rw(c, n->mem_load.ptr);
|
n->mem_load.ptr = fx(ud, n->mem_load.ptr);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_STORE:
|
case SPL_IR_MEM_STORE:
|
||||||
n->mem_store.ptr = rw(c, n->mem_store.ptr);
|
n->mem_store.ptr = fx(ud, n->mem_store.ptr);
|
||||||
n->mem_store.val = rw(c, n->mem_store.val);
|
n->mem_store.val = fx(ud, n->mem_store.val);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_OFFSET:
|
case SPL_IR_MEM_OFFSET:
|
||||||
n->mem_offset.ptr = rw(c, n->mem_offset.ptr);
|
n->mem_offset.ptr = fx(ud, n->mem_offset.ptr);
|
||||||
n->mem_offset.offset = rw(c, n->mem_offset.offset);
|
n->mem_offset.offset = fx(ud, n->mem_offset.offset);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_FIELD_PTR:
|
case SPL_IR_MEM_FIELD_PTR:
|
||||||
n->mem_field_ptr.agg = rw(c, n->mem_field_ptr.agg);
|
n->mem_field_ptr.agg = fx(ud, n->mem_field_ptr.agg);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_COPY:
|
case SPL_IR_MEM_COPY:
|
||||||
n->mem_copy.dst = rw(c, n->mem_copy.dst);
|
n->mem_copy.dst = fx(ud, n->mem_copy.dst);
|
||||||
n->mem_copy.src = rw(c, n->mem_copy.src);
|
n->mem_copy.src = fx(ud, n->mem_copy.src);
|
||||||
n->mem_copy.size = rw(c, n->mem_copy.size);
|
n->mem_copy.size = fx(ud, n->mem_copy.size);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_MEM_SET:
|
case SPL_IR_MEM_SET:
|
||||||
n->mem_set.dst = rw(c, n->mem_set.dst);
|
n->mem_set.dst = fx(ud, n->mem_set.dst);
|
||||||
n->mem_set.val = rw(c, n->mem_set.val);
|
n->mem_set.val = fx(ud, n->mem_set.val);
|
||||||
n->mem_set.size = rw(c, n->mem_set.size);
|
n->mem_set.size = fx(ud, n->mem_set.size);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_TYPE_OFFSETOF:
|
case SPL_IR_TYPE_OFFSETOF:
|
||||||
n->ir_offsetof.field_idx = rw(c, n->ir_offsetof.field_idx);
|
n->ir_offsetof.field_idx = fx(ud, n->ir_offsetof.field_idx);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_AGG_CONSTRUCT:
|
case SPL_IR_AGG_CONSTRUCT:
|
||||||
rw_vec(c, &n->agg_construct.fields);
|
xform_vec(&n->agg_construct.fields, fx, ud);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_AGG_EXTRACT:
|
case SPL_IR_AGG_EXTRACT:
|
||||||
n->agg_extract.val = rw(c, n->agg_extract.val);
|
n->agg_extract.val = fx(ud, n->agg_extract.val);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_AGG_INSERT:
|
case SPL_IR_AGG_INSERT:
|
||||||
n->agg_insert.agg = rw(c, n->agg_insert.agg);
|
n->agg_insert.agg = fx(ud, n->agg_insert.agg);
|
||||||
n->agg_insert.field = rw(c, n->agg_insert.field);
|
n->agg_insert.field = fx(ud, n->agg_insert.field);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CONTROL_SELECT:
|
case SPL_IR_CONTROL_SELECT:
|
||||||
n->control_select.cond = rw(c, n->control_select.cond);
|
n->control_select.cond = fx(ud, n->control_select.cond);
|
||||||
n->control_select.true_val = rw(c, n->control_select.true_val);
|
n->control_select.true_val = fx(ud, n->control_select.true_val);
|
||||||
n->control_select.false_val = rw(c, n->control_select.false_val);
|
n->control_select.false_val = fx(ud, n->control_select.false_val);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CONTROL_BR:
|
case SPL_IR_CONTROL_BR:
|
||||||
n->control_br.cond = rw(c, n->control_br.cond);
|
n->control_br.cond = fx(ud, n->control_br.cond);
|
||||||
|
break;
|
||||||
|
case SPL_IR_CONTROL_PARAM:
|
||||||
|
n->control_param.idx = fx(ud, n->control_param.idx);
|
||||||
|
break;
|
||||||
|
case SPL_IR_CONTROL_RET:
|
||||||
|
n->control_ret.val = fx(ud, n->control_ret.val);
|
||||||
|
break;
|
||||||
|
case SPL_IR_CONTROL_CALL:
|
||||||
|
n->control_call.func = fx(ud, n->control_call.func);
|
||||||
|
xform_vec(&n->control_call.params, fx, ud);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static spl_ir_node_ref_t rw_thunk(void *ud, spl_ir_node_ref_t r) {
|
||||||
|
return rw((inline_ctx_t *)ud, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 克隆体旧 ref -> 新 ref;块标签也参与平移。 */
|
||||||
|
static void rw_node(inline_ctx_t *c, spl_ir_node_t *n) {
|
||||||
|
xform_node_value_refs(n, rw_thunk, c);
|
||||||
|
switch (n->kind) {
|
||||||
|
case SPL_IR_CONTROL_BR:
|
||||||
n->control_br.true_label = rw(c, n->control_br.true_label);
|
n->control_br.true_label = rw(c, n->control_br.true_label);
|
||||||
n->control_br.false_label = rw(c, n->control_br.false_label);
|
n->control_br.false_label = rw(c, n->control_br.false_label);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CONTROL_JMP:
|
case SPL_IR_CONTROL_JMP:
|
||||||
n->control_jmp.label = rw(c, n->control_jmp.label);
|
n->control_jmp.label = rw(c, n->control_jmp.label);
|
||||||
break;
|
break;
|
||||||
case SPL_IR_CONTROL_PARAM:
|
|
||||||
n->control_param.idx = rw(c, n->control_param.idx);
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_RET:
|
|
||||||
n->control_ret.val = rw(c, n->control_ret.val);
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_CALL:
|
|
||||||
n->control_call.func = rw(c, n->control_call.func);
|
|
||||||
rw_vec(c, &n->control_call.params);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -174,14 +192,11 @@ static spl_ir_func_ref_t call_callee(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
||||||
unsigned char *mark) {
|
unsigned char *mark);
|
||||||
if (from == 0)
|
|
||||||
return false;
|
/* from 的任一直接被调者能否到达 target。 */
|
||||||
if (from == target)
|
static bool callees_reach(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
||||||
return true;
|
unsigned char *mark) {
|
||||||
if (mark[from])
|
|
||||||
return false;
|
|
||||||
mark[from] = 1;
|
|
||||||
spl_ir_func_t *f = spl_ir_func(ir, from);
|
spl_ir_func_t *f = spl_ir_func(ir, from);
|
||||||
if (!f)
|
if (!f)
|
||||||
return false;
|
return false;
|
||||||
@@ -197,22 +212,22 @@ static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t targ
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 从 from 的直接被调者出发是否能到达 target(用于判断 from 自身是否递归/自可达)。 */
|
static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
||||||
|
unsigned char *mark) {
|
||||||
|
if (from == 0)
|
||||||
|
return false;
|
||||||
|
if (from == target)
|
||||||
|
return true;
|
||||||
|
if (mark[from])
|
||||||
|
return false;
|
||||||
|
mark[from] = 1;
|
||||||
|
return callees_reach(ir, from, target, mark);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 从 from 的直接被调者出发是否能到达 target(用于判断 from 自身是否自可达)。 */
|
||||||
static bool reaches_succ(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
static bool reaches_succ(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target,
|
||||||
unsigned char *mark) {
|
unsigned char *mark) {
|
||||||
spl_ir_func_t *f = spl_ir_func(ir, from);
|
return callees_reach(ir, from, target, mark);
|
||||||
if (!f)
|
|
||||||
return false;
|
|
||||||
usize n = vec_size(f->nodes);
|
|
||||||
for (usize i = 1; i < n; i++) {
|
|
||||||
spl_ir_node_t *x = nd(ir, from, i);
|
|
||||||
if (x && x->kind == SPL_IR_CONTROL_CALL) {
|
|
||||||
spl_ir_func_ref_t c = call_callee(ir, from, x);
|
|
||||||
if (c != 0 && reaches(ir, c, target, mark))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---- 内联一个调用点 ---- */
|
/* ---- 内联一个调用点 ---- */
|
||||||
@@ -242,6 +257,17 @@ static spl_ir_node_ref_t find_const_src(spl_ir_t *ir, spl_ir_func_ref_t fid) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 值引用定点替换:等于 ci 的改为 ret_new。 */
|
||||||
|
typedef struct {
|
||||||
|
spl_ir_node_ref_t ci;
|
||||||
|
spl_ir_node_ref_t ret_new;
|
||||||
|
} replace_one_t;
|
||||||
|
|
||||||
|
static spl_ir_node_ref_t replace_one(void *ud, spl_ir_node_ref_t r) {
|
||||||
|
replace_one_t *p = ud;
|
||||||
|
return r == p->ci ? p->ret_new : r;
|
||||||
|
}
|
||||||
|
|
||||||
static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t ci,
|
static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t ci,
|
||||||
spl_ir_func_ref_t callee) {
|
spl_ir_func_ref_t callee) {
|
||||||
spl_ir_func_t *cf = spl_ir_func(ir, caller);
|
spl_ir_func_t *cf = spl_ir_func(ir, caller);
|
||||||
@@ -430,99 +456,12 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t
|
|||||||
call->kind = SPL_IR_CONTROL_JMP;
|
call->kind = SPL_IR_CONTROL_JMP;
|
||||||
call->control_jmp.label = body_entry;
|
call->control_jmp.label = body_entry;
|
||||||
|
|
||||||
/* 使用者替换:只把等于 ci 的引用改为 ret_new(不动其它 ref) */
|
/* 使用者替换:只把等于 ci 的值引用改为 ret_new(块标签不动) */
|
||||||
|
replace_one_t rep = {.ci = ci, .ret_new = ret_new};
|
||||||
for (usize j = 1; j < orig_len; j++) {
|
for (usize j = 1; j < orig_len; j++) {
|
||||||
spl_ir_node_t *x = nd(ir, caller, j);
|
spl_ir_node_t *x = nd(ir, caller, j);
|
||||||
if (!x)
|
if (x)
|
||||||
continue;
|
xform_node_value_refs(x, replace_one, &rep);
|
||||||
switch (x->kind) {
|
|
||||||
case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL:
|
|
||||||
case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: case SPL_IR_ARITH_NEG:
|
|
||||||
case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_AND: case SPL_IR_ARITH_OR:
|
|
||||||
case SPL_IR_ARITH_XOR: case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR:
|
|
||||||
case SPL_IR_ARITH_NOT:
|
|
||||||
if (x->arith.left == ci) x->arith.left = ret_new;
|
|
||||||
if (x->arith.right == ci) x->arith.right = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: case SPL_IR_CMP_LT:
|
|
||||||
case SPL_IR_CMP_LE: case SPL_IR_CMP_GT: case SPL_IR_CMP_GE:
|
|
||||||
if (x->cmp.a == ci) x->cmp.a = ret_new;
|
|
||||||
if (x->cmp.b == ci) x->cmp.b = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_SEXT:
|
|
||||||
case SPL_IR_CAST_FEXT: case SPL_IR_CAST_FTRUNC: case SPL_IR_CAST_BITCAST:
|
|
||||||
case SPL_IR_CAST_PTR2INT: case SPL_IR_CAST_INT2PTR: case SPL_IR_CAST_BOOL2INT:
|
|
||||||
case SPL_IR_CASE_INT2FLOAT: case SPL_IR_CASE_FLOAT2INT:
|
|
||||||
if (x->cast.val == ci) x->cast.val = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_ALLOCA:
|
|
||||||
if (x->mem_alloc.count == ci) x->mem_alloc.count = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_GLOBAL_ALLOC:
|
|
||||||
if (x->mem_global_alloc.const_node == ci) x->mem_global_alloc.const_node = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_LOAD:
|
|
||||||
if (x->mem_load.ptr == ci) x->mem_load.ptr = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_STORE:
|
|
||||||
if (x->mem_store.ptr == ci) x->mem_store.ptr = ret_new;
|
|
||||||
if (x->mem_store.val == ci) x->mem_store.val = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_OFFSET:
|
|
||||||
if (x->mem_offset.ptr == ci) x->mem_offset.ptr = ret_new;
|
|
||||||
if (x->mem_offset.offset == ci) x->mem_offset.offset = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_FIELD_PTR:
|
|
||||||
if (x->mem_field_ptr.agg == ci) x->mem_field_ptr.agg = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_COPY:
|
|
||||||
if (x->mem_copy.dst == ci) x->mem_copy.dst = ret_new;
|
|
||||||
if (x->mem_copy.src == ci) x->mem_copy.src = ret_new;
|
|
||||||
if (x->mem_copy.size == ci) x->mem_copy.size = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_MEM_SET:
|
|
||||||
if (x->mem_set.dst == ci) x->mem_set.dst = ret_new;
|
|
||||||
if (x->mem_set.val == ci) x->mem_set.val = ret_new;
|
|
||||||
if (x->mem_set.size == ci) x->mem_set.size = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_TYPE_OFFSETOF:
|
|
||||||
if (x->ir_offsetof.field_idx == ci) x->ir_offsetof.field_idx = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_AGG_CONSTRUCT:
|
|
||||||
for (usize k = 0; k < vec_size(x->agg_construct.fields); k++)
|
|
||||||
if (vec_at(x->agg_construct.fields, k) == ci)
|
|
||||||
vec_at(x->agg_construct.fields, k) = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_AGG_EXTRACT:
|
|
||||||
if (x->agg_extract.val == ci) x->agg_extract.val = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_AGG_INSERT:
|
|
||||||
if (x->agg_insert.agg == ci) x->agg_insert.agg = ret_new;
|
|
||||||
if (x->agg_insert.field == ci) x->agg_insert.field = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_SELECT:
|
|
||||||
if (x->control_select.cond == ci) x->control_select.cond = ret_new;
|
|
||||||
if (x->control_select.true_val == ci) x->control_select.true_val = ret_new;
|
|
||||||
if (x->control_select.false_val == ci) x->control_select.false_val = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_BR:
|
|
||||||
if (x->control_br.cond == ci) x->control_br.cond = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_CALL:
|
|
||||||
if (x->control_call.func == ci) x->control_call.func = ret_new;
|
|
||||||
for (usize k = 0; k < vec_size(x->control_call.params); k++)
|
|
||||||
if (vec_at(x->control_call.params, k) == ci)
|
|
||||||
vec_at(x->control_call.params, k) = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_PARAM:
|
|
||||||
if (x->control_param.idx == ci) x->control_param.idx = ret_new;
|
|
||||||
break;
|
|
||||||
case SPL_IR_CONTROL_RET:
|
|
||||||
if (x->control_ret.val == ci) x->control_ret.val = ret_new;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(c.param_ref);
|
free(c.param_ref);
|
||||||
|
|||||||
+698
-713
File diff suppressed because it is too large.
Load diff
@@ -5,9 +5,6 @@ typedef struct {
|
|||||||
const char *fname;
|
const char *fname;
|
||||||
int line;
|
int line;
|
||||||
int col;
|
int col;
|
||||||
|
|
||||||
const char *dbg_name;
|
|
||||||
// TODO
|
|
||||||
} spl_dbg_node_t;
|
} spl_dbg_node_t;
|
||||||
|
|
||||||
#define SPL_FATAL(dbg, fmt, ...) \
|
#define SPL_FATAL(dbg, fmt, ...) \
|
||||||
|
|||||||
+4
-1
@@ -223,6 +223,9 @@ typedef struct {
|
|||||||
usize frame_bytes;
|
usize frame_bytes;
|
||||||
} frame_layout_t;
|
} frame_layout_t;
|
||||||
|
|
||||||
|
/* panic 数据区:fmt slice + msg slice,各 2 words(ptr + len)。 */
|
||||||
|
enum { IR2VM_PANIC_WORDS = 4 };
|
||||||
|
|
||||||
/* 结果槽存标量;聚合/大对象额外分配一块数据区(data_off)。 */
|
/* 结果槽存标量;聚合/大对象额外分配一块数据区(data_off)。 */
|
||||||
static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_ir_node_t *node) {
|
static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_ir_node_t *node) {
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
@@ -254,7 +257,7 @@ static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_
|
|||||||
return ir_type_size(ctx, node->mem_load.tid);
|
return ir_type_size(ctx, node->mem_load.tid);
|
||||||
return 0;
|
return 0;
|
||||||
case SPL_IR_DBG_PANIC:
|
case SPL_IR_DBG_PANIC:
|
||||||
return sizeof(spl_vm_val_t) * 4; /* TODO: fmt slice + msg slice */
|
return sizeof(spl_vm_val_t) * IR2VM_PANIC_WORDS;
|
||||||
case SPL_IR_CONTROL_CALL:
|
case SPL_IR_CONTROL_CALL:
|
||||||
if (ir_is_aggregate(ctx, node->control_call.tid))
|
if (ir_is_aggregate(ctx, node->control_call.tid))
|
||||||
return ir_type_size(ctx, node->control_call.tid);
|
return ir_type_size(ctx, node->control_call.tid);
|
||||||
|
|||||||
@@ -252,8 +252,6 @@ static void resolve_type(spl_layout_t *layout, spl_type_t *type, spl_type_id_t i
|
|||||||
switch (tn->kind) {
|
switch (tn->kind) {
|
||||||
case SPL_TYPE_ERROR:
|
case SPL_TYPE_ERROR:
|
||||||
case SPL_TYPE_INFER:
|
case SPL_TYPE_INFER:
|
||||||
case SPL_TYPE_FORALL:
|
|
||||||
case SPL_TYPE_EXISTS:
|
|
||||||
case SPL_TYPE_NEVER:
|
case SPL_TYPE_NEVER:
|
||||||
case SPL_TYPE_UNIT:
|
case SPL_TYPE_UNIT:
|
||||||
info->size_bits = 0;
|
info->size_bits = 0;
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ typedef struct {
|
|||||||
} spl_layout_info_t;
|
} spl_layout_info_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
enum { SPL_BIG_ENDIAN, SPL_LITTLE_ENDIAN } endian; // TODO 更多位存储方式
|
enum { SPL_BIG_ENDIAN, SPL_LITTLE_ENDIAN } endian;
|
||||||
usize ptr_size;
|
usize ptr_size;
|
||||||
usize ptr_align;
|
usize ptr_align;
|
||||||
} spl_layout_target_t;
|
} spl_layout_target_t;
|
||||||
|
|||||||
+2
-1
@@ -465,7 +465,8 @@ spl_tok_vec_t spl_lex(const char *source, const char *fname) {
|
|||||||
TRY_OP1('$', TOK_DOLLAR);
|
TRY_OP1('$', TOK_DOLLAR);
|
||||||
TRY_OP1('?', TOK_COND);
|
TRY_OP1('?', TOK_COND);
|
||||||
|
|
||||||
// TODO
|
LOG_ERROR("%s:%zu:%zu: unknown character '%c' (0x%02x)", fname ? fname : "<null>", line, col,
|
||||||
|
(c >= 32 && c < 127) ? c : '?', (unsigned char)c);
|
||||||
spl_tok_t tok;
|
spl_tok_t tok;
|
||||||
memset(&tok, 0, sizeof(tok));
|
memset(&tok, 0, sizeof(tok));
|
||||||
tok.type = TOK_UNKNOWN;
|
tok.type = TOK_UNKNOWN;
|
||||||
|
|||||||
+1055
-955
File diff suppressed because it is too large.
Load diff
@@ -9,8 +9,6 @@ static usize spl_type_hash(spl_type_node_t n) {
|
|||||||
case SPL_TYPE_ANY:
|
case SPL_TYPE_ANY:
|
||||||
case SPL_TYPE_NEVER:
|
case SPL_TYPE_NEVER:
|
||||||
case SPL_TYPE_INFER:
|
case SPL_TYPE_INFER:
|
||||||
case SPL_TYPE_FORALL:
|
|
||||||
case SPL_TYPE_EXISTS:
|
|
||||||
case SPL_TYPE_BOOL:
|
case SPL_TYPE_BOOL:
|
||||||
break;
|
break;
|
||||||
case SPL_TYPE_INT:
|
case SPL_TYPE_INT:
|
||||||
@@ -61,8 +59,6 @@ static int spl_type_eq(spl_type_node_t n1, spl_type_node_t n2) {
|
|||||||
case SPL_TYPE_ANY:
|
case SPL_TYPE_ANY:
|
||||||
case SPL_TYPE_NEVER:
|
case SPL_TYPE_NEVER:
|
||||||
case SPL_TYPE_INFER:
|
case SPL_TYPE_INFER:
|
||||||
case SPL_TYPE_FORALL:
|
|
||||||
case SPL_TYPE_EXISTS:
|
|
||||||
case SPL_TYPE_BOOL:
|
case SPL_TYPE_BOOL:
|
||||||
return 0;
|
return 0;
|
||||||
case SPL_TYPE_INT:
|
case SPL_TYPE_INT:
|
||||||
@@ -253,10 +249,6 @@ static void spl_type_pure_dump_rec(spl_type_t *type, spl_type_id_t id, spl_type_
|
|||||||
case SPL_TYPE_INFER:
|
case SPL_TYPE_INFER:
|
||||||
printf("infer");
|
printf("infer");
|
||||||
break;
|
break;
|
||||||
case SPL_TYPE_FORALL:
|
|
||||||
case SPL_TYPE_EXISTS:
|
|
||||||
printf("quant");
|
|
||||||
break;
|
|
||||||
case SPL_TYPE_BOOL:
|
case SPL_TYPE_BOOL:
|
||||||
printf("bool");
|
printf("bool");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -42,8 +42,6 @@ typedef enum {
|
|||||||
SPL_TYPE_UNION,
|
SPL_TYPE_UNION,
|
||||||
SPL_TYPE_ENUM,
|
SPL_TYPE_ENUM,
|
||||||
SPL_TYPE_FN,
|
SPL_TYPE_FN,
|
||||||
SPL_TYPE_FORALL, // TODO delete
|
|
||||||
SPL_TYPE_EXISTS, // TODO delete
|
|
||||||
SPL_TYPE_SHAPE,
|
SPL_TYPE_SHAPE,
|
||||||
} spl_type_node_kind_t;
|
} spl_type_node_kind_t;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user