wip frames, some fixes

This commit is contained in:
zongor 2026-02-01 10:08:18 -08:00
parent 1191dd7476
commit 7b87ab8435
3 changed files with 22 additions and 14 deletions

View File

@ -1,7 +1,6 @@
#include "../../../vm/vm.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
#define CODE_SIZE 8192
#define MEMORY_SIZE 65536
@ -10,7 +9,6 @@ u32 lcode[CODE_SIZE] = {0};
bool init_vm() {
mem = lmem;
memset(mem, 0, MEMORY_SIZE*sizeof(u8));
code = lcode;
mp = 0;
cp = 0;
@ -19,7 +17,7 @@ bool init_vm() {
return true;
}
u32 syscall(u32 id, u32 args, u32 mem_ptr) {
u32 syscall(u32 id, u32 mem_ptr) {
return 0; // success
}

View File

@ -95,12 +95,11 @@ bool step_vm() {
}
case OP_SYSCALL: {
u32 id = stack[--sp]; /* syscall id */
u32 size = stack[--sp]; /* size of heap at that pointer */
u32 rd = stack[--sp]; /* the pointer */
status = syscall(id, size, rd);
status = syscall(id, rd);
return true;
}
case OP_PUSH: {
case OP_PUSH_8: {
return false;
}
case OP_POP: {

27
vm/vm.h
View File

@ -15,9 +15,14 @@ typedef enum {
OP_STORE_16, /* &dest obj1 `store-16`- | memory[dest] = obj1 << 16 */
OP_STORE_32, /* &dest obj1 `store` - | memory[dest] = obj1 */
OP_MALLOC, /* size `malloc` ptr | allocate 'size + 4' of memory and push ptr to memory on stack */
OP_PUSH, /* &local `get` obj1 | get the value from the local slot */
OP_PUSH_8, /* const `push8` obj1 | push a 8 bit const onto the stack */
OP_PUSH_16, /* const `push16` obj1 | push a 16 bit value onto the stack */
OP_PUSH_32, /* const `push32` obj1 | push a 32 bit value onto the stack */
OP_POP, /* - `pop` - | removes top item from the stack */
OP_SET, /* obj1 &local `set` - | set the value of the local slot */
OP_SET_ARG, /* &parent_local `set_arg` - | sets a child's local */
OP_GET, /* &local `get` obj1 | get the value of the local slot */
OP_GET_ARG, /* &parent_local `set_arg` - | sets a child's local */
OP_DUP, /* obj1 `dup` obj1 obj1 | duplicates the top of the stack */
OP_EXCH, /* obj2 obj1 `exch` obj1 obj2 | swaps the top two values on the stack */
OP_OVER, /* obj2 obj1 `over` obj2 | copys the 2nd to the top element and pushes to the stack */
@ -75,12 +80,12 @@ typedef enum {
OP_JGT_REAL, /* obj2 obj1 pc `jump_gt_real` | jump to pc if obj1 as real > obj2 as real */
OP_JLT_REAL, /* obj2 obj1 pc `jump_lt_real` | jump to pc if obj1 as real < obj2 as real */
OP_JLE_REAL, /* obj2 obj1 pc `jump_le_real` | jump to pc if obj1 as real <= obj2 as real */
OP_INT_TO_STR, /* obj1 `int-to-string` str_ptr | convert obj1 to str */
OP_NAT_TO_STR, /* obj1 `nat-to-string` str_ptr | convert obj1 to str */
OP_REAL_TO_STR, /* obj1 `real-to-string` str_ptr | convert obj1 to str */
OP_STR_TO_INT, /* str_ptr `string-to-int` obj | convert obj1 to int */
OP_STR_TO_NAT, /* str_ptr `string-to-nat` obj | convert obj1 to nat */
OP_STR_TO_REAL, /* str_ptr `string-to-real` obj | convert obj1 to real */
OP_INT_TO_STR, /* obj1 `int_to_string` str_ptr | convert obj1 to str */
OP_NAT_TO_STR, /* obj1 `nat_to_string` str_ptr | convert obj1 to str */
OP_REAL_TO_STR, /* obj1 `real_to_string` str_ptr | convert obj1 to str */
OP_STR_TO_INT, /* str_ptr `string_to_int` obj | convert obj1 to int */
OP_STR_TO_NAT, /* str_ptr `string_to_nat` obj | convert obj1 to nat */
OP_STR_TO_REAL, /* str_ptr `string_to_real` obj | convert obj1 to real */
OP_MAX_OPCODE /* not an opcode count of instructions */
} Opcode;
@ -90,6 +95,12 @@ typedef enum {
SYSCALL_MAX
} Syscall;
typedef struct frame_s Frame;
struct frame_s {
u32 return_pc;
u32 start_mp;
};
extern u32 pc; /* program counter */
extern u32 cp; /* code pointer */
extern u32 mp; /* memory pointer */
@ -163,7 +174,7 @@ extern u8 *mem; /* memory */
} while (0)
extern bool init_vm();
extern u32 syscall(u32 id, u32 args, u32 mem_ptr);
extern u32 syscall(u32 id, u32 mem_ptr);
bool step_vm();
u32 str_alloc(char *str, u32 length);