ai: 重新设计stage1的spl 让ai重构代码
This commit is contained in:
1 parent
032102f1d7
commit
e531aab2d3
44 files changed
+708
-422
No files matched your search
+116
-86
@@ -5,8 +5,12 @@
|
||||
* -g, --debug-cli interactive debugger (REPL)
|
||||
* --entry <name> entry function (default: main)
|
||||
* --trace print every instruction
|
||||
* --profile native sampling profiler
|
||||
* --profile-interval <N> profile sampling interval (default: 10000)
|
||||
* -h, --help show help
|
||||
*
|
||||
* 选项可以出现在 <file.sir> 之前或之间;`--` 之后全部作为 spl 程序参数。
|
||||
*
|
||||
* spl 程序 argv:argv[0]=file.sir,argv[1..]=args。
|
||||
* REPL 配合 splc0 -g(.sir 尾部内嵌 debug 段:IR 行 + VAR 行)可显示
|
||||
* 当前 IR 节点/源行/局部变量。
|
||||
@@ -21,103 +25,121 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SCC_ARGPARSE_IMPLEMENTATION
|
||||
#define SCC_OPTPARSER_IMPLEMENTATION
|
||||
#include "../nob/argparse.h"
|
||||
|
||||
/* ================================================================
|
||||
* 命令行解析
|
||||
* ================================================================ */
|
||||
|
||||
typedef struct {
|
||||
int debug;
|
||||
int debug_cli;
|
||||
int trace;
|
||||
int disasm;
|
||||
int profile;
|
||||
cbool debug;
|
||||
cbool debug_cli;
|
||||
cbool trace;
|
||||
cbool disasm;
|
||||
cbool profile;
|
||||
int profile_interval;
|
||||
const char *profile_out;
|
||||
int stats;
|
||||
cbool stats;
|
||||
const char *entry;
|
||||
const char *path; /* file.sir */
|
||||
const char **args; /* spl 程序参数(file.sir 之后) */
|
||||
int nargs;
|
||||
const char *path; /* file.sir */
|
||||
scc_argparse_list_t args; /* spl 程序参数(file.sir 之后) */
|
||||
} cli_opts_t;
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr, "Usage: spl_cli [options] <file.sir> [args...]\n"
|
||||
" -d, --debug stack-canary checks\n"
|
||||
" -g, --debug-cli interactive debugger (REPL)\n"
|
||||
" --disasm disasm dump sir\n"
|
||||
" --entry <name> entry function (default: main)\n"
|
||||
" --trace print every instruction\n"
|
||||
" --profile[=N] native sampling profiler (every N insns, default 10000)\n"
|
||||
" --profile-out <p> profile output prefix (default build/profile)\n"
|
||||
" --stats print executed instructions / call count at exit\n"
|
||||
" -h, --help show help\n");
|
||||
static void add_bool_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
|
||||
cbool *store) {
|
||||
scc_argparse_opt_t opt;
|
||||
scc_argparse_opt_init(&opt, sn, ln, desc);
|
||||
scc_argparse_spec_setup_bool(&opt.spec, store);
|
||||
scc_argparse_cmd_add_opt(cmd, &opt);
|
||||
}
|
||||
|
||||
/* options 可穿插;`--` 终止 option 解析(本身不作为 file/args)。
|
||||
* 第一个非 option 为 file.sir,其后为 spl args。 */
|
||||
static int parse_args(int argc, const char **argv, cli_opts_t *o) {
|
||||
memset(o, 0, sizeof *o);
|
||||
o->entry = "main";
|
||||
int i = 1;
|
||||
for (; i < argc; i++) {
|
||||
const char *a = argv[i];
|
||||
if (a[0] == '-' && a[1]) {
|
||||
if (strcmp(a, "--") == 0) {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
if (strcmp(a, "-d") == 0 || strcmp(a, "--debug") == 0) {
|
||||
o->debug = 1;
|
||||
} else if (strcmp(a, "-g") == 0 || strcmp(a, "--debug-cli") == 0) {
|
||||
o->debug_cli = 1;
|
||||
} else if (strcmp(a, "--trace") == 0) {
|
||||
o->trace = 1;
|
||||
} else if (strcmp(a, "--disasm") == 0) {
|
||||
o->disasm = 1;
|
||||
} else if (strcmp(a, "--stats") == 0) {
|
||||
o->stats = 1;
|
||||
} else if (strcmp(a, "--profile") == 0) {
|
||||
o->profile = 1;
|
||||
} else if (strncmp(a, "--profile=", 10) == 0) {
|
||||
o->profile = 1;
|
||||
o->profile_interval = (int)strtol(a + 10, NULL, 10);
|
||||
} else if (strcmp(a, "--profile-out") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "spl_cli: --profile-out needs a prefix\n");
|
||||
return -1;
|
||||
}
|
||||
o->profile_out = argv[++i];
|
||||
} else if (strcmp(a, "--entry") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "spl_cli: --entry needs a name\n");
|
||||
return -1;
|
||||
}
|
||||
o->entry = argv[++i];
|
||||
} else if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
|
||||
usage();
|
||||
return -1;
|
||||
} else {
|
||||
fprintf(stderr, "spl_cli: unknown option '%s'\n", a);
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
static void add_string_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
|
||||
const char **store) {
|
||||
scc_argparse_opt_t opt;
|
||||
scc_argparse_opt_init(&opt, sn, ln, desc);
|
||||
scc_argparse_spec_setup_string(&opt.spec, store);
|
||||
scc_argparse_cmd_add_opt(cmd, &opt);
|
||||
}
|
||||
|
||||
static void add_int_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
|
||||
int *store) {
|
||||
scc_argparse_opt_t opt;
|
||||
scc_argparse_opt_init(&opt, sn, ln, desc);
|
||||
scc_argparse_spec_setup_int(&opt.spec, store);
|
||||
scc_argparse_cmd_add_opt(cmd, &opt);
|
||||
}
|
||||
|
||||
static void add_arg_string(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
|
||||
const char **store, cbool required) {
|
||||
scc_argparse_arg_t arg;
|
||||
scc_argparse_arg_init(&arg, name, desc);
|
||||
scc_argparse_spec_setup_string(&arg.spec, store);
|
||||
arg.spec.flag_required = required;
|
||||
scc_argparse_cmd_add_arg(cmd, &arg);
|
||||
}
|
||||
|
||||
static void add_arg_list(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
|
||||
scc_argparse_list_t *store) {
|
||||
scc_argparse_arg_t arg;
|
||||
scc_argparse_arg_init(&arg, name, desc);
|
||||
scc_argparse_spec_setup_list(&arg.spec, store);
|
||||
scc_argparse_cmd_add_arg(cmd, &arg);
|
||||
}
|
||||
|
||||
/* argparse 的 BOOL 会忽略 `--profile=500` 的尾部值;显式拦掉,避免间隔被悄悄丢掉。 */
|
||||
static int reject_inline_profile(int argc, const char **argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--") == 0)
|
||||
break;
|
||||
if (strncmp(argv[i], "--profile=", 10) == 0) {
|
||||
fprintf(stderr,
|
||||
"spl_cli: `%s` is not supported, use `--profile --profile-interval N`\n",
|
||||
argv[i]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* 跳过前导 `--`(pipeline 调用会带 `--` 分隔 file 与 args) */
|
||||
while (i < argc && strcmp(argv[i], "--") == 0)
|
||||
i++;
|
||||
if (i >= argc) {
|
||||
fprintf(stderr, "spl_cli: missing <file.sir>\n");
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* `--` 之后 optparse 不再识别选项,剩余 token 全部落进 args。 */
|
||||
static int parse_cli(int argc, const char **argv, cli_opts_t *o) {
|
||||
memset(o, 0, sizeof *o);
|
||||
o->entry = "main";
|
||||
if (reject_inline_profile(argc, argv) != 0)
|
||||
return -1;
|
||||
|
||||
scc_argparse_t parser;
|
||||
scc_argparse_init(&parser, "spl_cli", "SIR VM launcher + gdb-style debugger");
|
||||
scc_argparse_cmd_t *root = scc_argparse_get_root(&parser);
|
||||
|
||||
add_bool_opt(root, 'd', "debug", "stack-canary checks", &o->debug);
|
||||
add_bool_opt(root, 'g', "debug-cli", "interactive debugger (REPL)", &o->debug_cli);
|
||||
add_bool_opt(root, 0, "disasm", "disasm dump sir", &o->disasm);
|
||||
add_string_opt(root, 0, "entry", "entry function (default: main)", &o->entry);
|
||||
add_bool_opt(root, 0, "trace", "print every instruction", &o->trace);
|
||||
add_bool_opt(root, 0, "profile", "native sampling profiler", &o->profile);
|
||||
add_int_opt(root, 0, "profile-interval", "profile sampling interval in insns (default: 10000)",
|
||||
&o->profile_interval);
|
||||
add_string_opt(root, 0, "profile-out", "profile output prefix (default: build/profile)",
|
||||
&o->profile_out);
|
||||
add_bool_opt(root, 0, "stats", "print executed instructions / call count at exit",
|
||||
&o->stats);
|
||||
|
||||
add_arg_string(root, "file", "SIR bytecode file", &o->path, true);
|
||||
add_arg_list(root, "args", "arguments passed to the spl program", &o->args);
|
||||
|
||||
int rc = scc_argparse_parse(&parser, argc, argv);
|
||||
if (parser.out.count > 0)
|
||||
fwrite(parser.out.items, 1, parser.out.count, stdout);
|
||||
scc_argparse_drop(&parser);
|
||||
|
||||
if (rc != SCC_ARGPARSE_ERR_NONE) {
|
||||
scc_vec_free(o->args);
|
||||
/* 1 表示 -h/--version 已处理,按成功退出。 */
|
||||
return rc == SCC_ARGPARSE_ERR_PNT_DEFAULT ? 1 : -1;
|
||||
}
|
||||
o->path = argv[i++];
|
||||
while (i < argc && strcmp(argv[i], "--") == 0)
|
||||
i++;
|
||||
o->args = argv + i;
|
||||
o->nargs = argc - i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -640,17 +662,20 @@ static void repl_loop(dbg_t *d) {
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
cli_opts_t o;
|
||||
if (parse_args(argc, argv, &o) != 0)
|
||||
return 1;
|
||||
int cli_rc = parse_cli(argc, argv, &o);
|
||||
if (cli_rc != 0)
|
||||
return cli_rc > 0 ? 0 : 1;
|
||||
|
||||
spl_prog_t prog;
|
||||
if (spl_prog_load_from_file(o.path, &prog) != 0) {
|
||||
fprintf(stderr, "spl_cli: cannot load '%s'\n", o.path);
|
||||
scc_vec_free(o.args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (o.disasm) {
|
||||
spl_prog_dump(&prog);
|
||||
scc_vec_free(o.args);
|
||||
return 0;
|
||||
}
|
||||
spl_syscall_register(&prog);
|
||||
@@ -671,6 +696,7 @@ int main(int argc, const char **argv) {
|
||||
fprintf(stderr, "spl_cli: failed to load prog\n");
|
||||
spl_vm_drop(&vm);
|
||||
spl_prog_drop(&prog);
|
||||
scc_vec_free(o.args);
|
||||
return 1;
|
||||
}
|
||||
if (o.profile) {
|
||||
@@ -680,21 +706,24 @@ int main(int argc, const char **argv) {
|
||||
}
|
||||
|
||||
/* spl 程序 argv:argv[0]=file.sir(程序名),argv[1..]=args */
|
||||
const char **spl_argv = (const char **)malloc(sizeof(char *) * (size_t)(o.nargs + 1));
|
||||
int nargs = (int)o.args.count;
|
||||
const char **spl_argv = (const char **)malloc(sizeof(char *) * (size_t)(nargs + 1));
|
||||
if (!spl_argv) {
|
||||
spl_vm_drop(&vm);
|
||||
spl_prog_drop(&prog);
|
||||
scc_vec_free(o.args);
|
||||
return 1;
|
||||
}
|
||||
spl_argv[0] = o.path;
|
||||
for (int i = 0; i < o.nargs; i++)
|
||||
spl_argv[i + 1] = o.args[i];
|
||||
for (int i = 0; i < nargs; i++)
|
||||
spl_argv[i + 1] = o.args.items[i];
|
||||
|
||||
if (spl_vm_prepare(&vm, o.entry, o.nargs + 1, spl_argv, NULL) != 0) {
|
||||
if (spl_vm_prepare(&vm, o.entry, nargs + 1, spl_argv, NULL) != 0) {
|
||||
fprintf(stderr, "spl_cli: entry point '%s' not found\n", o.entry);
|
||||
free(spl_argv);
|
||||
spl_vm_drop(&vm);
|
||||
spl_prog_drop(&prog);
|
||||
scc_vec_free(o.args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -735,6 +764,7 @@ int main(int argc, const char **argv) {
|
||||
free(spl_argv);
|
||||
spl_vm_drop(&vm);
|
||||
spl_prog_drop(&prog);
|
||||
scc_vec_free(o.args);
|
||||
|
||||
if (ret < 0)
|
||||
return 1;
|
||||
|
||||
Reference in new issue
Block a user