aboutsummaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-08 18:12:52 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-08 18:12:52 +0000
commit8fa40d8515daeaafe74e686265e7f3104ae76b65 (patch)
tree4289aa3aee4b693de384ebdf6b5c9872e85fe14f /vm
parent6e524569c34b0fb41c85280af5ea3f924999bbdf (diff)
downloadovm-8fa40d8515daeaafe74e686265e7f3104ae76b65.tar.gz
ovm-8fa40d8515daeaafe74e686265e7f3104ae76b65.tar.bz2
ovm-8fa40d8515daeaafe74e686265e7f3104ae76b65.zip
Fixed issue where, on error, runtime would show wrong instruction
When an error occurred, because prog->ptr was incremented beforehand the trace would show the next instruction as the culprit rather than the actual instruction. This commit fixes that by incrementing the program if and only if the command was run successfully.
Diffstat (limited to 'vm')
-rw-r--r--vm/runtime.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/vm/runtime.c b/vm/runtime.c
index 7b712f2..664fa25 100644
--- a/vm/runtime.c
+++ b/vm/runtime.c
@@ -74,29 +74,34 @@ err_t vm_execute(vm_t *vm)
OPCODE_IS_TYPE(instruction.opcode, OP_MSET) ||
OPCODE_IS_TYPE(instruction.opcode, OP_MGET))
{
+ err_t err =
+ WORD_ROUTINES[instruction.opcode](vm, instruction.operand.as_word);
+ if (err)
+ return err;
prog->ptr++;
- return WORD_ROUTINES[instruction.opcode](vm, instruction.operand.as_word);
}
else if (OPCODE_IS_TYPE(instruction.opcode, OP_POP))
{
// NOTE: We use the first register to hold the result of this pop
data_type_t type = OPCODE_DATA_TYPE(instruction.opcode, OP_POP);
- prog->ptr++;
+ err_t err = ERR_OK;
switch (type)
{
case DATA_TYPE_NIL:
break;
case DATA_TYPE_BYTE:
- return vm_mov_byte(vm, 0);
+ err = vm_mov_byte(vm, 0);
break;
case DATA_TYPE_HWORD:
- return vm_mov_hword(vm, 0);
+ err = vm_mov_hword(vm, 0);
break;
case DATA_TYPE_WORD:
- return vm_mov_word(vm, 0);
+ err = vm_mov_word(vm, 0);
break;
}
- return ERR_OK;
+ if (err)
+ return err;
+ prog->ptr++;
}
else if (OPCODE_IS_TYPE(instruction.opcode, OP_NOT) ||
OPCODE_IS_TYPE(instruction.opcode, OP_OR) ||
@@ -115,16 +120,18 @@ err_t vm_execute(vm_t *vm)
OPCODE_IS_TYPE(instruction.opcode, OP_MGET_STACK) ||
instruction.opcode == OP_MDELETE || instruction.opcode == OP_MSIZE)
{
+ err_t err = STACK_ROUTINES[instruction.opcode](vm);
prog->ptr++;
- return STACK_ROUTINES[instruction.opcode](vm);
+ if (err)
+ return err;
}
else if (instruction.opcode == OP_JUMP_ABS)
return vm_jump(vm, instruction.operand.as_word);
else if (instruction.opcode == OP_JUMP_STACK)
{
- // Set prog->ptr to the word on top of the stack
data_t ret = {0};
- err_t err = vm_pop_word(vm, &ret);
+ // Set prog->ptr to the word on top of the stack
+ err_t err = vm_pop_word(vm, &ret);
if (err)
return err;
return vm_jump(vm, ret.as_word);
@@ -620,8 +627,10 @@ err_t vm_push_hword(vm_t *vm, data_t f)
convert_hword_to_bytes(f.as_hword, bytes);
for (size_t i = 0; i < HWORD_SIZE; ++i)
{
- byte b = bytes[HWORD_SIZE - i - 1];
- vm_push_byte(vm, DBYTE(b));
+ byte b = bytes[HWORD_SIZE - i - 1];
+ err_t err = vm_push_byte(vm, DBYTE(b));
+ if (err)
+ return err;
}
return ERR_OK;
}
@@ -634,8 +643,10 @@ err_t vm_push_word(vm_t *vm, data_t w)
convert_word_to_bytes(w.as_word, bytes);
for (size_t i = 0; i < WORD_SIZE; ++i)
{
- byte b = bytes[WORD_SIZE - i - 1];
- vm_push_byte(vm, DBYTE(b));
+ byte b = bytes[WORD_SIZE - i - 1];
+ err_t err = vm_push_byte(vm, DBYTE(b));
+ if (err)
+ return err;
}
return ERR_OK;
}