stage1 人工重构ast.c
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
#include <stdlib.h>
|
||||
#define log_vsnprintf vsnprintf
|
||||
#define log_puts puts
|
||||
#define log_abort abort
|
||||
#define log_abort() exit(1)
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__ // GCC, Clang
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_cli.c — SIR VM launcher + gdb-style interactive debugger
|
||||
/* spl_cli.c SIR VM launcher + gdb-style interactive debugger
|
||||
*
|
||||
* Usage: spl_cli [options] <file.sir> [args...]
|
||||
* -d, --debug stack-canary checks
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_disasm.c — SIR bytecode disassembler
|
||||
/* spl_disasm.c - SIR bytecode disassembler
|
||||
*
|
||||
* Usage: spl_disasm <file.sir>
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_mcode.c — SPL VM machine code binary serialization, deserialization, and utilities
|
||||
/* spl_mcode.c - SPL VM machine code binary serialization, deserialization, and utilities
|
||||
*
|
||||
* Binary format (all metadata fields are spl_val_t = uint64_t LE):
|
||||
* [HEADER] magic(8) nfuncs(8) ninsns(8) nnatives(8) nstrs(8) ndata(8)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_syscall.c — built-in syscall implementations + registration
|
||||
/* spl_syscall.c - built-in syscall implementations + registration
|
||||
*
|
||||
* All syscalls validate nargs, cast spl_val_t args to the expected C types,
|
||||
* execute, and return the result as spl_val_t.
|
||||
@@ -23,7 +23,7 @@
|
||||
* Each SYSCALL_N macro:
|
||||
* 1. Validates nargs (prints error and returns -1 on mismatch)
|
||||
* 2. Casts args[n] from spl_val_t to the specified C type via
|
||||
* (type)(uintptr_t) — this handles both integer and pointer types
|
||||
* (type)(uintptr_t) - this handles both integer and pointer types
|
||||
* 3. Evaluates the expression and returns the result as spl_val_t
|
||||
* ================================================================= */
|
||||
|
||||
@@ -80,34 +80,34 @@
|
||||
* OS syscalls
|
||||
* ================================================================= */
|
||||
|
||||
/* vm_exit — terminate process with the given exit code */
|
||||
/* vm_exit - terminate process with the given exit code */
|
||||
SYSCALL_1(vm_exit, int, (exit(a1), (spl_val_t)0))
|
||||
|
||||
/* vm_putchar — write a single character to stdout */
|
||||
/* vm_putchar - write a single character to stdout */
|
||||
SYSCALL_1(vm_putchar, int, putchar(a1))
|
||||
|
||||
/* vm_getchar — read a single character from stdin */
|
||||
/* vm_getchar - read a single character from stdin */
|
||||
SYSCALL_0(vm_getchar, getchar())
|
||||
|
||||
/* vm_putint — print an integer to stdout */
|
||||
/* vm_putint - print an integer to stdout */
|
||||
SYSCALL_1(vm_putint, int, (fprintf(stdout, "%d", a1), (spl_val_t)0))
|
||||
|
||||
/* vm_putstr — print a string to stdout (no trailing newline) */
|
||||
/* vm_putstr - print a string to stdout (no trailing newline) */
|
||||
SYSCALL_1(vm_putstr, const char *, (fputs(a1, stdout), (spl_val_t)0))
|
||||
|
||||
/* vm_fopen — open a file, returns FILE* as spl_val_t */
|
||||
/* vm_fopen - open a file, returns FILE* as spl_val_t */
|
||||
SYSCALL_2(vm_fopen, const char *, const char *, (uintptr_t)fopen(a1, a2))
|
||||
|
||||
/* vm_fclose — close a file, returns 0 on success */
|
||||
/* vm_fclose - close a file, returns 0 on success */
|
||||
SYSCALL_1(vm_fclose, FILE *, fclose(a1))
|
||||
|
||||
/* vm_fread — read from file, returns number of items read */
|
||||
/* vm_fread - read from file, returns number of items read */
|
||||
SYSCALL_4(vm_fread, void *, size_t, size_t, FILE *, fread(a1, a2, a3, a4))
|
||||
|
||||
/* vm_fwrite — write to file, returns number of items written */
|
||||
/* vm_fwrite - write to file, returns number of items written */
|
||||
SYSCALL_4(vm_fwrite, const void *, size_t, size_t, FILE *, fwrite(a1, a2, a3, a4))
|
||||
|
||||
/* vm_fsize — get file size in bytes */
|
||||
/* vm_fsize - get file size in bytes */
|
||||
static spl_val_t vm_fsize(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_fsize", 1);
|
||||
FILE *f = (FILE *)(uintptr_t)args[0];
|
||||
@@ -122,7 +122,7 @@ SYSCALL_0(vm_stdin, stdin)
|
||||
SYSCALL_0(vm_stdout, stdout)
|
||||
SYSCALL_0(vm_stderr, stderr)
|
||||
|
||||
/* vm_read_file — read entire file into a malloc'd, null-terminated buffer */
|
||||
/* vm_read_file - read entire file into a malloc'd, null-terminated buffer */
|
||||
static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_read_file", 1);
|
||||
const char *path = (const char *)(uintptr_t)args[0];
|
||||
@@ -149,22 +149,22 @@ static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
|
||||
return (spl_val_t)(uintptr_t)buf;
|
||||
}
|
||||
|
||||
/* vm_alloc — allocate memory (malloc) */
|
||||
/* vm_alloc - allocate memory (malloc) */
|
||||
SYSCALL_1(vm_alloc, size_t, (uintptr_t)malloc(a1))
|
||||
|
||||
/* vm_free — free memory */
|
||||
/* vm_free - free memory */
|
||||
SYSCALL_1(vm_free, void *, (free(a1), (spl_val_t)0))
|
||||
|
||||
/* vm_realloc — reallocate memory (realloc) */
|
||||
/* vm_realloc - reallocate memory (realloc) */
|
||||
SYSCALL_2(vm_realloc, void *, size_t, (uintptr_t)realloc(a1, a2))
|
||||
|
||||
/* vm_strlen — get string length */
|
||||
/* vm_strlen - get string length */
|
||||
SYSCALL_1(vm_strlen, const char *, strlen(a1))
|
||||
|
||||
/* vm_strcmp — compare two strings */
|
||||
/* vm_strcmp - compare two strings */
|
||||
SYSCALL_2(vm_strcmp, const char *, const char *, strcmp(a1, a2))
|
||||
|
||||
/* vm_memcpy — copy memory, returns dst */
|
||||
/* vm_memcpy - copy memory, returns dst */
|
||||
SYSCALL_3(vm_memcpy, void *, const void *, size_t, (memcpy(a1, a2, a3), (uintptr_t)a1))
|
||||
|
||||
static spl_val_t vm_printf(int nargs, spl_val_t *args) {
|
||||
@@ -228,13 +228,13 @@ static spl_val_t vm_printf(int nargs, spl_val_t *args) {
|
||||
}
|
||||
|
||||
/* =================================================================
|
||||
* VM syscalls (for comptime — compile-time code execution)
|
||||
* VM syscalls (for comptime - compile-time code execution)
|
||||
*
|
||||
* These let SPL code create isolated sub-VMs, load compiled .sir
|
||||
* programs into them, push arguments, call functions, and run.
|
||||
* ================================================================= */
|
||||
|
||||
/* vm_new — create a new sub-VM instance, returns spl_vm_t* */
|
||||
/* vm_new - create a new sub-VM instance, returns spl_vm_t* */
|
||||
static spl_val_t vm_new(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_new", 0);
|
||||
(void)args;
|
||||
@@ -245,7 +245,7 @@ static spl_val_t vm_new(int nargs, spl_val_t *args) {
|
||||
return (spl_val_t)(uintptr_t)vm;
|
||||
}
|
||||
|
||||
/* vm_drop — destroy a sub-VM and its loaded program */
|
||||
/* vm_drop - destroy a sub-VM and its loaded program */
|
||||
static spl_val_t vm_drop(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_drop", 1);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
@@ -261,7 +261,7 @@ static spl_val_t vm_drop(int nargs, spl_val_t *args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vm_load — load a .sir file, register syscalls, return spl_prog_t* */
|
||||
/* vm_load - load a .sir file, register syscalls, return spl_prog_t* */
|
||||
static spl_val_t vm_load(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_load", 2);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
@@ -285,7 +285,7 @@ static spl_val_t vm_load(int nargs, spl_val_t *args) {
|
||||
return (spl_val_t)(uintptr_t)prog;
|
||||
}
|
||||
|
||||
/* vm_push — push a value onto the sub-VM's stack (for passing arguments) */
|
||||
/* vm_push - push a value onto the sub-VM's stack (for passing arguments) */
|
||||
static spl_val_t vm_push(int nargs, spl_val_t *args) {
|
||||
CHECK_NARGS("vm_push", 2);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
@@ -300,7 +300,7 @@ static spl_val_t vm_push(int nargs, spl_val_t *args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vm_call — call a function by name with pre-pushed arguments
|
||||
/* vm_call - call a function by name with pre-pushed arguments
|
||||
*
|
||||
* Args (3): vm, function_name, nargs
|
||||
* The arguments should already be on the sub-VM's stack via vm_push.
|
||||
@@ -347,7 +347,7 @@ static spl_val_t vm_call(int nargs, spl_val_t *args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vm_run — run a sub-VM to completion, returns exit_code */
|
||||
/* vm_run - run a sub-VM to completion, returns exit_code */
|
||||
SYSCALL_1(vm_run, spl_vm_t *, spl_vm_run_until(a1, 0))
|
||||
|
||||
/* =================================================================
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_syscall.h — built-in syscall layer for SIR VM
|
||||
/* spl_syscall.h - built-in syscall layer for SIR VM
|
||||
*
|
||||
* Provides I/O native functions (putchar, getchar, file ops, etc.)
|
||||
* that compiled SPL programs can call via NCALL.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_vm.c — SIR step-by-step interpreter
|
||||
/* spl_vm.c - SIR step-by-step interpreter
|
||||
*
|
||||
* Implements the spl_vm.h API with macro-based type dispatch to
|
||||
* eliminate repetitive per-type switch cases.
|
||||
@@ -109,13 +109,13 @@ static int spl_type_size(spl_type_t t) {
|
||||
/* ================================================================
|
||||
* Type-dispatch macros for arithmetic / comparison
|
||||
*
|
||||
* ARITH_BINOP — ADD / SUB / MUL (two's complement: op same for
|
||||
* ARITH_BINOP - ADD / SUB / MUL (two's complement: op same for
|
||||
* signed and unsigned at the same bit-width)
|
||||
* DIV_REM_S — signed division / remainder
|
||||
* DIV_REM_U — unsigned division / remainder
|
||||
* CMP_ALL — EQ / NE (bitwise compare, also handles floats)
|
||||
* CMP_S — signed ordering (<, <=, >, >=)
|
||||
* CMP_U — unsigned ordering
|
||||
* DIV_REM_S - signed division / remainder
|
||||
* DIV_REM_U - unsigned division / remainder
|
||||
* CMP_ALL - EQ / NE (bitwise compare, also handles floats)
|
||||
* CMP_S - signed ordering (<, <=, >, >=)
|
||||
* CMP_U - unsigned ordering
|
||||
* ================================================================ */
|
||||
|
||||
#define ARITH_BINOP(OP) \
|
||||
@@ -177,7 +177,7 @@ static int spl_type_size(spl_type_t t) {
|
||||
PUSH(_r); \
|
||||
} while (0)
|
||||
|
||||
/* signed division / remainder — all types cast to signed */
|
||||
/* signed division / remainder - all types cast to signed */
|
||||
#define DIV_REM_S(OP) \
|
||||
do { \
|
||||
spl_val_t _b = POP(), _a = POP(); \
|
||||
@@ -280,7 +280,7 @@ static int spl_type_size(spl_type_t t) {
|
||||
PUSH(_r); \
|
||||
} while (0)
|
||||
|
||||
/* CMP_ALL — equality comparisons (all types, floats via memcpy) */
|
||||
/* CMP_ALL - equality comparisons (all types, floats via memcpy) */
|
||||
#define CMP_ALL(OP) \
|
||||
do { \
|
||||
spl_val_t _b = POP(), _a = POP(); \
|
||||
@@ -334,7 +334,7 @@ static int spl_type_size(spl_type_t t) {
|
||||
PUSH(_r); \
|
||||
} while (0)
|
||||
|
||||
/* CMP_S — signed ordering (all ints cast to signed, floats OK) */
|
||||
/* CMP_S - signed ordering (all ints cast to signed, floats OK) */
|
||||
#define CMP_S(OP) \
|
||||
do { \
|
||||
spl_val_t _b = POP(), _a = POP(); \
|
||||
@@ -388,7 +388,7 @@ static int spl_type_size(spl_type_t t) {
|
||||
PUSH(_r); \
|
||||
} while (0)
|
||||
|
||||
/* CMP_U — unsigned ordering (all ints cast to unsigned, no float) */
|
||||
/* CMP_U - unsigned ordering (all ints cast to unsigned, no float) */
|
||||
#define CMP_U(OP) \
|
||||
do { \
|
||||
spl_val_t _b = POP(), _a = POP(); \
|
||||
@@ -726,7 +726,7 @@ int spl_vm_prepare(spl_vm_t *vm, const char *entry, int argc, const char **argv,
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* spl_vm_run_once — execute one instruction
|
||||
* spl_vm_run_once - execute one instruction
|
||||
*
|
||||
* Returns: 0 = still running, 1 = halted, 2 = breakpoint (SPL_DBG), -1 = error
|
||||
* ================================================================ */
|
||||
@@ -745,7 +745,7 @@ int spl_vm_run_once(spl_vm_t *vm) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 断点:即将执行的指令命中 ip 断点 → 暂停(返回 2)。
|
||||
/* 断点:即将执行的指令命中 ip 断点 -> 暂停(返回 2)。
|
||||
* skip_bp 置位时执行当前指令(continue 越过当前断点)。 */
|
||||
if (ip_breakpoint_hit(vm)) {
|
||||
if (vm->skip_bp) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* spl_vm.h — SIR interpreter */
|
||||
/* spl_vm.h - SIR interpreter */
|
||||
|
||||
#ifndef __SPL_VM_H__
|
||||
#define __SPL_VM_H__
|
||||
|
||||
Reference in New Issue
Block a user