stage1 实现部分ast2ir内容并重构stage0 引入并重构ir2vm 构建系统修改部分
This commit is contained in:
1 parent
3d57260f3f
commit
34df303b56
17 files changed
+636
-1800
No files matched your search
@@ -29,6 +29,7 @@ typedef struct {
|
||||
int debug;
|
||||
int debug_cli;
|
||||
int trace;
|
||||
int disasm;
|
||||
const char *entry;
|
||||
const char *path; /* file.sir */
|
||||
const char **args; /* spl 程序参数(file.sir 之后) */
|
||||
@@ -39,6 +40,7 @@ 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"
|
||||
" -h, --help show help\n");
|
||||
@@ -63,6 +65,8 @@ static int parse_args(int argc, const char **argv, cli_opts_t *o) {
|
||||
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, "--entry") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "spl_cli: --entry needs a name\n");
|
||||
@@ -624,6 +628,11 @@ int main(int argc, const char **argv) {
|
||||
fprintf(stderr, "spl_cli: cannot load '%s'\n", o.path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (o.disasm) {
|
||||
spl_prog_dump(&prog);
|
||||
return 0;
|
||||
}
|
||||
spl_syscall_register(&prog);
|
||||
|
||||
spl_vm_t vm;
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
/* spl_disasm.c - SIR bytecode disassembler
|
||||
*
|
||||
* Usage: spl_disasm <file.sir>
|
||||
*/
|
||||
|
||||
#include "spl_mcode.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: spl_disasm <file.sir>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
spl_prog_t prog;
|
||||
if (spl_prog_load_from_file(argv[1], &prog) != 0) {
|
||||
fprintf(stderr, "spl_disasm: cannot load '%s'\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf(";; SPL bytecode dump: %s\n", argv[1]);
|
||||
printf(";; ninsns=%zu nfuncs=%zu nnatives=%zu nstrs=%zu\n\n", vec_size(prog.insns),
|
||||
vec_size(prog.funcs), vec_size(prog.natives), vec_size(prog.strtab));
|
||||
|
||||
if (vec_size(prog.funcs) > 0) {
|
||||
printf(";; --- functions ---\n");
|
||||
for (usize i = 0; i < vec_size(prog.funcs); i++) {
|
||||
spl_vm_func_t *f = &vec_at(prog.funcs, i);
|
||||
printf(" %s nargs=%zu ninsns=%zu addr=%zu\n", f->name ? f->name : "(anon)", f->nargs,
|
||||
f->ninsns, f->address);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (vec_size(prog.natives) > 0) {
|
||||
printf(";; --- natives ---\n");
|
||||
for (int i = 0; i < (int)vec_size(prog.natives); i++) {
|
||||
spl_vm_native_t *n = &vec_at(prog.natives, i);
|
||||
printf(" %s\n", n->name ? n->name : "(anon)");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (vec_size(prog.strtab) > 0) {
|
||||
printf(";; --- string table ---\n");
|
||||
for (int i = 0; i < (int)vec_size(prog.strtab); i++) {
|
||||
printf(" %d: \"%s\"\n", i, vec_at(prog.strtab, i));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf(";; --- instructions ---\n");
|
||||
for (usize i = 0; i < vec_size(prog.insns); i++) {
|
||||
spl_vm_ins_t *ins = &vec_at(prog.insns, i);
|
||||
printf("%4zd: %s", i, spl_vm_opcode_name((spl_vm_opcode_t)ins->opcode));
|
||||
if (ins->type != SPL_VOID)
|
||||
printf(" %s", spl_vm_type_kind_name((spl_vm_kind_t)ins->type));
|
||||
printf(" %zd", ins->imm);
|
||||
|
||||
/* annotate jumps / calls */
|
||||
if (ins->opcode == SPL_JMP || ins->opcode == SPL_BZ || ins->opcode == SPL_BNZ) {
|
||||
spl_vm_val_t target = i + 1 + ins->imm;
|
||||
printf(" ; -> %zu", target);
|
||||
} else if (ins->opcode == SPL_CALL) {
|
||||
printf(" ; nargs=%zu, from stack", ins->imm);
|
||||
} else if (ins->opcode == SPL_CALLI) {
|
||||
printf(" ; indirect call");
|
||||
} else if (ins->opcode == SPL_GADDR) {
|
||||
printf(" ; gdata[%zu]", ins->imm);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
spl_prog_drop(&prog);
|
||||
return 0;
|
||||
}
|
||||
@@ -43,6 +43,61 @@ void spl_prog_drop(spl_prog_t *prog) {
|
||||
prog->debug = NULL;
|
||||
}
|
||||
|
||||
void spl_prog_dump(spl_prog_t *prog) {
|
||||
printf(";; SPL bytecode dump: \n");
|
||||
printf(";; ninsns=%zu nfuncs=%zu nnatives=%zu nstrs=%zu\n\n", vec_size(prog->insns),
|
||||
vec_size(prog->funcs), vec_size(prog->natives), vec_size(prog->strtab));
|
||||
|
||||
if (vec_size(prog->funcs) > 0) {
|
||||
printf(";; --- functions ---\n");
|
||||
for (usize i = 0; i < vec_size(prog->funcs); i++) {
|
||||
spl_vm_func_t *f = &vec_at(prog->funcs, i);
|
||||
printf(" %s nargs=%zu ninsns=%zu addr=%zu\n", f->name ? f->name : "(anon)", f->nargs,
|
||||
f->ninsns, f->address);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (vec_size(prog->natives) > 0) {
|
||||
printf(";; --- natives ---\n");
|
||||
for (int i = 0; i < (int)vec_size(prog->natives); i++) {
|
||||
spl_vm_native_t *n = &vec_at(prog->natives, i);
|
||||
printf(" %s\n", n->name ? n->name : "(anon)");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (vec_size(prog->strtab) > 0) {
|
||||
printf(";; --- string table ---\n");
|
||||
for (int i = 0; i < (int)vec_size(prog->strtab); i++) {
|
||||
printf(" %d: \"%s\"\n", i, vec_at(prog->strtab, i));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf(";; --- instructions ---\n");
|
||||
for (usize i = 0; i < vec_size(prog->insns); i++) {
|
||||
spl_vm_ins_t *ins = &vec_at(prog->insns, i);
|
||||
printf("%4zd: %s", i, spl_vm_opcode_name((spl_vm_opcode_t)ins->opcode));
|
||||
if (ins->type != SPL_VOID)
|
||||
printf(" %s", spl_vm_type_kind_name((spl_vm_kind_t)ins->type));
|
||||
printf(" %zd", ins->imm);
|
||||
|
||||
/* annotate jumps / calls */
|
||||
if (ins->opcode == SPL_JMP || ins->opcode == SPL_BZ || ins->opcode == SPL_BNZ) {
|
||||
spl_vm_val_t target = i + 1 + ins->imm;
|
||||
printf(" ; -> %zu", target);
|
||||
} else if (ins->opcode == SPL_CALL) {
|
||||
printf(" ; nargs=%zu, from stack", ins->imm);
|
||||
} else if (ins->opcode == SPL_CALLI) {
|
||||
printf(" ; indirect call");
|
||||
} else if (ins->opcode == SPL_GADDR) {
|
||||
printf(" ; gdata[%zu]", ins->imm);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- LE read/write helpers ---- */
|
||||
|
||||
static inline spl_vm_val_t rd64(const unsigned char **p) {
|
||||
|
||||
@@ -199,6 +199,7 @@ typedef struct spl_prog {
|
||||
|
||||
void spl_prog_init(spl_prog_t *prog);
|
||||
void spl_prog_drop(spl_prog_t *prog);
|
||||
void spl_prog_dump(spl_prog_t *prog);
|
||||
|
||||
int spl_prog_load_from_file(const char *fname, spl_prog_t *prog);
|
||||
int spl_prog_store_to_file(const char *fname, spl_prog_t *prog);
|
||||
|
||||
Reference in new issue
Block a user