stage0 重命名字段 修复windows函数没有进入宏包装
This commit is contained in:
@@ -36,44 +36,44 @@
|
||||
} while (0)
|
||||
|
||||
#define SYSCALL_0(name, ret_expr) \
|
||||
static spl_val_t name(int nargs, spl_val_t *args) { \
|
||||
static spl_vm_val_t name(int nargs, spl_vm_val_t *args) { \
|
||||
CHECK_NARGS(#name, 0); \
|
||||
(void)args; \
|
||||
return (spl_val_t)(uintptr_t)(ret_expr); \
|
||||
return (spl_vm_val_t)(uintptr_t)(ret_expr); \
|
||||
}
|
||||
|
||||
#define SYSCALL_1(name, t1, ret_expr) \
|
||||
static spl_val_t name(int nargs, spl_val_t *args) { \
|
||||
static spl_vm_val_t name(int nargs, spl_vm_val_t *args) { \
|
||||
CHECK_NARGS(#name, 1); \
|
||||
t1 a1 = (t1)(uintptr_t)args[0]; \
|
||||
return (spl_val_t)(uintptr_t)(ret_expr); \
|
||||
return (spl_vm_val_t)(uintptr_t)(ret_expr); \
|
||||
}
|
||||
|
||||
#define SYSCALL_2(name, t1, t2, ret_expr) \
|
||||
static spl_val_t name(int nargs, spl_val_t *args) { \
|
||||
static spl_vm_val_t name(int nargs, spl_vm_val_t *args) { \
|
||||
CHECK_NARGS(#name, 2); \
|
||||
t1 a1 = (t1)(uintptr_t)args[0]; \
|
||||
t2 a2 = (t2)(uintptr_t)args[1]; \
|
||||
return (spl_val_t)(uintptr_t)(ret_expr); \
|
||||
return (spl_vm_val_t)(uintptr_t)(ret_expr); \
|
||||
}
|
||||
|
||||
#define SYSCALL_3(name, t1, t2, t3, ret_expr) \
|
||||
static spl_val_t name(int nargs, spl_val_t *args) { \
|
||||
static spl_vm_val_t name(int nargs, spl_vm_val_t *args) { \
|
||||
CHECK_NARGS(#name, 3); \
|
||||
t1 a1 = (t1)(uintptr_t)args[0]; \
|
||||
t2 a2 = (t2)(uintptr_t)args[1]; \
|
||||
t3 a3 = (t3)(uintptr_t)args[2]; \
|
||||
return (spl_val_t)(uintptr_t)(ret_expr); \
|
||||
return (spl_vm_val_t)(uintptr_t)(ret_expr); \
|
||||
}
|
||||
|
||||
#define SYSCALL_4(name, t1, t2, t3, t4, ret_expr) \
|
||||
static spl_val_t name(int nargs, spl_val_t *args) { \
|
||||
static spl_vm_val_t name(int nargs, spl_vm_val_t *args) { \
|
||||
CHECK_NARGS(#name, 4); \
|
||||
t1 a1 = (t1)(uintptr_t)args[0]; \
|
||||
t2 a2 = (t2)(uintptr_t)args[1]; \
|
||||
t3 a3 = (t3)(uintptr_t)args[2]; \
|
||||
t4 a4 = (t4)(uintptr_t)args[3]; \
|
||||
return (spl_val_t)(uintptr_t)(ret_expr); \
|
||||
return (spl_vm_val_t)(uintptr_t)(ret_expr); \
|
||||
}
|
||||
|
||||
/* =================================================================
|
||||
@@ -81,7 +81,7 @@
|
||||
* ================================================================= */
|
||||
|
||||
/* vm_exit - terminate process with the given exit code */
|
||||
SYSCALL_1(vm_exit, int, (exit(a1), (spl_val_t)0))
|
||||
SYSCALL_1(vm_exit, int, (exit(a1), (spl_vm_val_t)0))
|
||||
|
||||
/* vm_putchar - write a single character to stdout */
|
||||
SYSCALL_1(vm_putchar, int, putchar(a1))
|
||||
@@ -90,10 +90,10 @@ SYSCALL_1(vm_putchar, int, putchar(a1))
|
||||
SYSCALL_0(vm_getchar, getchar())
|
||||
|
||||
/* vm_putint - print an integer to stdout */
|
||||
SYSCALL_1(vm_putint, int, (fprintf(stdout, "%d", a1), (spl_val_t)0))
|
||||
SYSCALL_1(vm_putint, int, (fprintf(stdout, "%d", a1), (spl_vm_val_t)0))
|
||||
|
||||
/* vm_putstr - print a string to stdout (no trailing newline) */
|
||||
SYSCALL_1(vm_putstr, const char *, (fputs(a1, stdout), (spl_val_t)0))
|
||||
SYSCALL_1(vm_putstr, const char *, (fputs(a1, stdout), (spl_vm_val_t)0))
|
||||
|
||||
/* vm_fopen - open a file, returns FILE* as spl_val_t */
|
||||
SYSCALL_2(vm_fopen, const char *, const char *, (uintptr_t)fopen(a1, a2))
|
||||
@@ -108,14 +108,14 @@ SYSCALL_4(vm_fread, void *, size_t, size_t, FILE *, fread(a1, a2, a3, a4))
|
||||
SYSCALL_4(vm_fwrite, const void *, size_t, size_t, FILE *, fwrite(a1, a2, a3, a4))
|
||||
|
||||
/* vm_fsize - get file size in bytes */
|
||||
static spl_val_t vm_fsize(int nargs, spl_val_t *args) {
|
||||
static spl_vm_val_t vm_fsize(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_fsize", 1);
|
||||
FILE *f = (FILE *)(uintptr_t)args[0];
|
||||
long cur = ftell(f);
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fseek(f, cur, SEEK_SET);
|
||||
return (spl_val_t)(uintptr_t)sz;
|
||||
return (spl_vm_val_t)(uintptr_t)sz;
|
||||
}
|
||||
|
||||
SYSCALL_0(vm_stdin, stdin)
|
||||
@@ -123,7 +123,7 @@ 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) {
|
||||
static spl_vm_val_t vm_read_file(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_read_file", 1);
|
||||
const char *path = (const char *)(uintptr_t)args[0];
|
||||
if (path == nullptr) {
|
||||
@@ -146,14 +146,14 @@ static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
|
||||
size_t nread = fread(buf, 1, (size_t)sz, f);
|
||||
fclose(f);
|
||||
buf[nread] = '\0';
|
||||
return (spl_val_t)(uintptr_t)buf;
|
||||
return (spl_vm_val_t)(uintptr_t)buf;
|
||||
}
|
||||
|
||||
/* vm_alloc - allocate memory (malloc) */
|
||||
SYSCALL_1(vm_alloc, size_t, (uintptr_t)malloc(a1))
|
||||
|
||||
/* vm_free - free memory */
|
||||
SYSCALL_1(vm_free, void *, (free(a1), (spl_val_t)0))
|
||||
SYSCALL_1(vm_free, void *, (free(a1), (spl_vm_val_t)0))
|
||||
|
||||
/* vm_realloc - reallocate memory (realloc) */
|
||||
SYSCALL_2(vm_realloc, void *, size_t, (uintptr_t)realloc(a1, a2))
|
||||
@@ -167,7 +167,7 @@ SYSCALL_2(vm_strcmp, const char *, const char *, strcmp(a1, a2))
|
||||
/* 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) {
|
||||
static spl_vm_val_t vm_printf(int nargs, spl_vm_val_t *args) {
|
||||
(void)args;
|
||||
if (nargs <= 0) {
|
||||
return 0;
|
||||
@@ -235,18 +235,18 @@ static spl_val_t vm_printf(int nargs, spl_val_t *args) {
|
||||
* ================================================================= */
|
||||
|
||||
/* vm_new - create a new sub-VM instance, returns spl_vm_t* */
|
||||
static spl_val_t vm_new(int nargs, spl_val_t *args) {
|
||||
static spl_vm_val_t vm_new(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_new", 0);
|
||||
(void)args;
|
||||
spl_vm_t *vm = (spl_vm_t *)malloc(sizeof(spl_vm_t));
|
||||
if (!vm)
|
||||
return 0;
|
||||
spl_vm_init(vm);
|
||||
return (spl_val_t)(uintptr_t)vm;
|
||||
return (spl_vm_val_t)(uintptr_t)vm;
|
||||
}
|
||||
|
||||
/* vm_drop - destroy a sub-VM and its loaded program */
|
||||
static spl_val_t vm_drop(int nargs, spl_val_t *args) {
|
||||
static spl_vm_val_t vm_drop(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_drop", 1);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
if (!vm)
|
||||
@@ -262,7 +262,7 @@ static spl_val_t vm_drop(int nargs, spl_val_t *args) {
|
||||
}
|
||||
|
||||
/* vm_load - load a .sir file, register syscalls, return spl_prog_t* */
|
||||
static spl_val_t vm_load(int nargs, spl_val_t *args) {
|
||||
static spl_vm_val_t vm_load(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_load", 2);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
const char *path = (const char *)(uintptr_t)args[1];
|
||||
@@ -282,14 +282,14 @@ static spl_val_t vm_load(int nargs, spl_val_t *args) {
|
||||
free(prog);
|
||||
return -1;
|
||||
}
|
||||
return (spl_val_t)(uintptr_t)prog;
|
||||
return (spl_vm_val_t)(uintptr_t)prog;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
static spl_vm_val_t vm_push(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_push", 2);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
spl_val_t val = args[1];
|
||||
spl_vm_val_t val = args[1];
|
||||
if (!vm)
|
||||
return -1;
|
||||
if (vm->sp >= vm->config.max_stack_depth) {
|
||||
@@ -311,15 +311,15 @@ static spl_val_t vm_push(int nargs, spl_val_t *args) {
|
||||
* - Sets fp = sp - nargs (so arg0 = data[fp], arg1 = data[fp+1], ...)
|
||||
* - Sets ip to the function address
|
||||
*/
|
||||
static spl_val_t vm_call(int nargs, spl_val_t *args) {
|
||||
static spl_vm_val_t vm_call(int nargs, spl_vm_val_t *args) {
|
||||
CHECK_NARGS("vm_call", 3);
|
||||
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
|
||||
const char *name = (const char *)(uintptr_t)args[1];
|
||||
spl_val_t call_nargs = args[2];
|
||||
spl_vm_val_t call_nargs = args[2];
|
||||
if (!vm || !name)
|
||||
return -1;
|
||||
|
||||
spl_func_t *func = spl_prog_get_func(vm->prog, name);
|
||||
spl_vm_func_t *func = spl_prog_get_func(vm->prog, name);
|
||||
if (!func) {
|
||||
fprintf(stderr, "vm_call: function '%s' not found\n", name);
|
||||
return -1;
|
||||
@@ -358,7 +358,7 @@ SYSCALL_1(vm_run, spl_vm_t *, spl_vm_run_until(a1, 0))
|
||||
* ================================================================= */
|
||||
|
||||
void spl_syscall_register(spl_prog_t *prog) {
|
||||
static spl_native_t table[] = {
|
||||
static spl_vm_native_t table[] = {
|
||||
/* OS operations */
|
||||
{"vm_exit", 0, vm_exit},
|
||||
{"vm_putchar", 0, vm_putchar},
|
||||
@@ -393,7 +393,7 @@ void spl_syscall_register(spl_prog_t *prog) {
|
||||
if (!prog)
|
||||
return;
|
||||
vec_for(prog->natives, i) {
|
||||
spl_native_t *nat = &vec_at(prog->natives, i);
|
||||
spl_vm_native_t *nat = &vec_at(prog->natives, i);
|
||||
if (!nat->name || nat->impl_fn)
|
||||
continue;
|
||||
for (int j = 0; j < n; j++) {
|
||||
|
||||
Reference in New Issue
Block a user