From 7a1129d80f6903db6e6a919bdeae403f924d69f6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 1 Nov 2023 17:49:33 +0000 Subject: VM registers are now a dynamic array Stack based machines generally need "variable space". This may be quite via a symbol-to-word association a list, a hashmap, or some other system. Here I decide to go for the simplest: extending the register system to a dynamic/infinite number of them. This means, in practice, that we may use a theoretically infinite number of indexed words, hwords and bytes to act as variable space. This means that the onus is on those who are targeting this virtual machine to create their own association system to create syntactic variables: all the machinery is technically installed within the VM, without the veneer that causes extra cruft. --- vm/runtime.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'vm/runtime.h') diff --git a/vm/runtime.h b/vm/runtime.h index be907b3..1226ea9 100644 --- a/vm/runtime.h +++ b/vm/runtime.h @@ -33,13 +33,13 @@ typedef enum const char *err_as_cstr(err_t); -#define VM_REGISTERS 8 +typedef darr_t registers_t; +#define VM_NTH_REGISTER(REGISTERS, N) (((word *)((REGISTERS).data))[N]) +#define VM_REGISTERS_AVAILABLE(REGISTERS) (((REGISTERS).available) / WORD_SIZE) + typedef struct { - struct Registers - { - word reg[VM_REGISTERS]; - } registers; + registers_t registers; struct Stack { byte *data; @@ -57,6 +57,7 @@ err_t vm_execute_all(vm_t *); void vm_load_stack(vm_t *, byte *, size_t); void vm_load_program(vm_t *, inst_t *, size_t); +void vm_load_registers(vm_t *, registers_t); // Print routines #define VM_PRINT_PROGRAM_EXCERPT 5 @@ -83,15 +84,15 @@ static const push_f PUSH_ROUTINES[] = { [OP_PUSH_WORD] = vm_push_word, }; -err_t vm_push_byte_register(vm_t *, byte); -err_t vm_push_hword_register(vm_t *, byte); -err_t vm_push_word_register(vm_t *, byte); +err_t vm_push_byte_register(vm_t *, word); +err_t vm_push_hword_register(vm_t *, word); +err_t vm_push_word_register(vm_t *, word); -err_t vm_mov_byte(vm_t *, byte); -err_t vm_mov_hword(vm_t *, byte); -err_t vm_mov_word(vm_t *, byte); +err_t vm_mov_byte(vm_t *, word); +err_t vm_mov_hword(vm_t *, word); +err_t vm_mov_word(vm_t *, word); -typedef err_t (*reg_f)(vm_t *, byte); +typedef err_t (*reg_f)(vm_t *, word); static const reg_f REG_ROUTINES[] = { [OP_PUSH_REGISTER_BYTE] = vm_push_byte_register, [OP_PUSH_REGISTER_HWORD] = vm_push_hword_register, -- cgit v1.2.3-13-gbd6f