diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-11-01 18:31:11 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-11-01 18:31:11 +0000 |
commit | cb8b3f3b8c96a44d65cfd5056c8d973750ac7525 (patch) | |
tree | 649581845981621d70d0265e84edd70a066bdb8f /vm/runtime.c | |
parent | dab1729f613edcdc7a14e20e728c22c40cdd0966 (diff) | |
download | ovm-cb8b3f3b8c96a44d65cfd5056c8d973750ac7525.tar.gz ovm-cb8b3f3b8c96a44d65cfd5056c8d973750ac7525.tar.bz2 ovm-cb8b3f3b8c96a44d65cfd5056c8d973750ac7525.zip |
Fix off by one issues in register implementations
word register 0 refers to the first 8 bytes of the dynamic array.
Hence the used counter should be at least 8 bytes. This deals with
those issues. Also print more useful information in
vm_print_registers (how many byte|hword|word registers are currently
in use, how many are available).
Diffstat (limited to 'vm/runtime.c')
-rw-r--r-- | vm/runtime.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/vm/runtime.c b/vm/runtime.c index 0bdda1d..622228f 100644 --- a/vm/runtime.c +++ b/vm/runtime.c @@ -336,10 +336,15 @@ void vm_stop(vm_t *vm) void vm_print_registers(vm_t *vm, FILE *fp) { registers_t reg = vm->registers; - fprintf(fp, "Registers.used = %luB\nRegisters.available = %luB\n", - vm->registers.used, vm->registers.available); + fprintf( + fp, + "Registers.used = %luB/%luH/%luW\nRegisters.available = %luB/%luH/%luW\n", + vm->registers.used, vm->registers.used / HWORD_SIZE, + vm->registers.used / WORD_SIZE, vm->registers.available, + vm->registers.available / HWORD_SIZE, + vm->registers.available / WORD_SIZE); fprintf(fp, "Registers.reg = ["); - for (size_t i = 0; i <= (reg.used / WORD_SIZE); ++i) + for (size_t i = 0; i < (reg.used / WORD_SIZE); ++i) { fprintf(fp, "{%lu:%lX}", i, VM_NTH_REGISTER(reg, i)); if (i != reg.used - 1) @@ -518,7 +523,7 @@ err_t vm_mov_hword(vm_t *vm, word reg) const size_t diff = (hwords + 1) * HWORD_SIZE; darr_ensure_capacity(&vm->registers, diff); - vm->registers.used = MAX(vm->registers.used, (reg * HWORD_SIZE) + 1); + vm->registers.used = MAX(vm->registers.used, (reg + 1) * HWORD_SIZE); } data_t ret = {0}; err_t err = vm_pop_hword(vm, &ret); @@ -540,7 +545,7 @@ err_t vm_mov_word(vm_t *vm, word reg) const size_t diff = (words + 1) * WORD_SIZE; darr_ensure_capacity(&vm->registers, diff); - vm->registers.used = MAX(vm->registers.used, (reg * WORD_SIZE) + 1); + vm->registers.used = MAX(vm->registers.used, (reg + 1) * WORD_SIZE); } else if (vm->stack.ptr < sizeof(word)) return ERR_STACK_UNDERFLOW; @@ -548,7 +553,7 @@ err_t vm_mov_word(vm_t *vm, word reg) err_t err = vm_pop_word(vm, &ret); if (err) return err; - VM_NTH_REGISTER(vm->registers, reg) = ret.as_word; + ((word *)(vm->registers.data))[reg] = ret.as_word; return ERR_OK; } |