From cb8b3f3b8c96a44d65cfd5056c8d973750ac7525 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 1 Nov 2023 18:31:11 +0000 Subject: 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). --- vm/runtime.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'vm') 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; } -- cgit v1.2.3-13-gbd6f