stage1 修复bug 删除递归测试

This commit is contained in:
zzy
2026-07-08 11:45:45 +08:00
parent 463177d3be
commit 147f26e063
11 changed files with 601 additions and 96 deletions

View File

@@ -118,6 +118,10 @@ static spl_val_t vm_fsize(int nargs, spl_val_t *args) {
return (spl_val_t)(uintptr_t)sz;
}
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 */
static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_read_file", 1);
@@ -357,6 +361,9 @@ void spl_syscall_register(spl_prog_t *prog) {
{"vm_fread", 0, vm_fread},
{"vm_fwrite", 0, vm_fwrite},
{"vm_fsize", 0, vm_fsize},
{"vm_stdin", 0, vm_stdin},
{"vm_stdout", 0, vm_stdout},
{"vm_stderr", 0, vm_stderr},
{"vm_read_file", 0, vm_read_file},
{"vm_alloc", 0, vm_alloc},
{"vm_free", 0, vm_free},

View File

@@ -29,6 +29,18 @@
* ================================================================ */
static int spl_is_float(spl_type_t t) { return t == SPL_F32 || t == SPL_F64; }
static int spl_is_signed(spl_type_t t) {
switch (t) {
case SPL_I8:
case SPL_I16:
case SPL_I32:
case SPL_I64:
case SPL_ISIZE:
return 1;
default:
return 0;
}
}
static int spl_type_size(spl_type_t t) {
switch (t) {
case SPL_VOID:
@@ -904,7 +916,13 @@ int spl_vm_run_once(spl_vm_t *vm) {
case SPL_LOAD: {
void *_addr = (void *)POP();
spl_val_t _v = 0;
memcpy(&_v, _addr, spl_type_size(ins->type));
usize _sz = spl_type_size(ins->type);
memcpy(&_v, _addr, _sz);
/* Sign-extend signed integer types smaller than 64 bits */
if (_sz > 0 && _sz < sizeof(spl_val_t) && spl_is_signed(ins->type)) {
usize _shift = (sizeof(spl_val_t) - _sz) * 8;
_v = (spl_val_t)(((isize)(_v << _shift)) >> _shift);
}
PUSH(_v);
break;
}
@@ -1071,10 +1089,11 @@ void spl_vm_dump_instr(spl_vm_t *vm, spl_val_t ip) {
void spl_vm_stackdump(spl_vm_t *vm, spl_val_t sp) {
if (!vm)
return;
fprintf(stderr, " stack (sp=%zd, fp=%zd):\n", sp, vm->fp);
spl_val_t start = sp > 16 ? sp - 16 : 0;
for (spl_val_t i = start; i < sp; i++) {
fprintf(stderr, " [%3zd] = 0x%016zx (%zd)\n", i, vm->stacks.data[i], vm->stacks.data[i]);
fprintf(stderr, "stack dump (sp=%zd, fp=%zd):\n", sp, vm->fp);
spl_val_t start = sp > 32 ? sp - 32 : 0;
for (spl_val_t i = start; i <= sp; i++) {
fprintf(stderr, " [%3zd] = [addr 0x%p] 0x%016zx (%zd)\n", i, &vm->stacks.data[i],
vm->stacks.data[i], vm->stacks.data[i]);
}
}
@@ -1082,7 +1101,7 @@ int spl_vm_backtrace(spl_vm_t *vm, spl_val_t fp) {
if (!vm || !vm->prog)
return -1;
(void)fp;
fprintf(stderr, "=== backtrace ===\n");
fprintf(stderr, "backtrace: \n");
for (isize i = vm->cp - 1; i >= 0; i--) {
spl_val_t _saved_ip = vm->frames.data[i].saved_ip;
spl_val_t _saved_fp = vm->frames.data[i].saved_fp;