stage0 更改名称spl_ir -> spl_mcode spl_cli提供debug模式 提供临时语言规范

This commit is contained in:
zzy
2026-07-29 12:05:18 +08:00
parent 89d4bae8db
commit a77eade06f
10 changed files with 1224 additions and 476 deletions

View File

@@ -5,7 +5,7 @@
*/
#include "spl_vm.h"
#include "spl_ir.h"
#include "spl_mcode.h"
#include <stdio.h>
#include <stdlib.h>
@@ -45,6 +45,7 @@ static int spl_type_size(spl_type_t t) {
switch (t) {
case SPL_VOID:
return 0;
case SPL_BOOL:
case SPL_I8:
case SPL_U8:
return 1;
@@ -80,6 +81,16 @@ static int spl_type_size(spl_type_t t) {
return -1; \
} while (0)
#define CHECK_ADDR(addr, label) do { \
if (vm->debug_addr && (uintptr_t)(addr) < 0x1000) { \
fprintf(stderr, "vm: %s at ip=%zd: LOW ADDR=%p sp=%zd fp=%zd\n", \
label, vm->ip - 1, (void*)(uintptr_t)(addr), vm->sp, vm->fp); \
spl_vm_stackdump(vm, vm->sp); \
spl_vm_backtrace(vm, vm->fp); \
vm->exit_code = 1; return -1; \
} \
} while(0)
/* ================================================================
* Stack push/pop (stacks.data is pre-allocated in init)
* ================================================================ */
@@ -547,6 +558,7 @@ void spl_vm_init_ex(spl_vm_t *vm, int stack_size, int call_depth) {
vm->prog = NULL;
vm->trace = 0;
vm->debug = 1;
vm->debug_addr = 0;
vm->exit_code = 0;
}
@@ -584,6 +596,7 @@ void spl_vm_set_debug(spl_vm_t *vm, int enabled) {
if (!vm)
return;
vm->debug = enabled ? 1 : 0;
vm->debug_addr = enabled ? 1 : 0;
}
#define STACK_CANARY(vm) (vm)->stacks.data[(vm)->fp - 1]
@@ -922,6 +935,7 @@ int spl_vm_run_once(spl_vm_t *vm) {
/* ========== Indirect Memory (load/store with types) ========== */
case SPL_LOAD: {
void *_addr = (void *)POP();
CHECK_ADDR(_addr, "LOAD");
spl_val_t _v = 0;
usize _sz = spl_type_size(ins->type);
memcpy(&_v, _addr, _sz);
@@ -936,6 +950,7 @@ int spl_vm_run_once(spl_vm_t *vm) {
case SPL_STORE: {
spl_val_t _v = POP();
void *_addr = (void *)POP();
CHECK_ADDR(_addr, "STORE");
memcpy(_addr, &_v, spl_type_size(ins->type));
break;
}