aboutsummaryrefslogtreecommitdiff
path: root/src/runtime.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:57:29 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:57:29 +0100
commit7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb (patch)
tree055cb12056a3be08dcadad1f8fff59a2292c701c /src/runtime.c
parentd8e45fce0435dc0961db2f1b8f0f4a5805347118 (diff)
downloadovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.tar.gz
ovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.tar.bz2
ovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.zip
Added and implemented OP_JUMP_(STACK|REGISTER)
Uses the stack and register, respectively, to jump to an absolute address. The stack based jump pops a word off the stack to perform a jump, while the register based one uses the operand to figure out which register to use.
Diffstat (limited to 'src/runtime.c')
-rw-r--r--src/runtime.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/runtime.c b/src/runtime.c
index 6b708ee..e68dd25 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -19,7 +19,7 @@
void vm_execute(vm_t *vm)
{
- static_assert(NUMBER_OF_OPCODES == 32, "vm_execute: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 34, "vm_execute: Out of date");
struct Program *prog = &vm->program;
if (prog->ptr >= prog->max)
// TODO: Error (Went past end of program)
@@ -96,6 +96,18 @@ void vm_execute(vm_t *vm)
// Set prog->ptr to the jump point requested
prog->ptr = instruction.operand.as_word;
}
+ else if (instruction.opcode == OP_JUMP_STACK)
+ {
+ // Set prog->ptr to the word on top of the stack
+ prog->ptr = vm_pop_word(vm).as_word;
+ }
+ else if (instruction.opcode == OP_JUMP_REGISTER)
+ {
+ if (instruction.operand.as_byte >= 8)
+ // TODO: Error register is not a valid word register
+ return;
+ prog->ptr = vm->registers.reg[instruction.operand.as_byte];
+ }
else if (instruction.opcode == OP_HALT)
{
// Do nothing here. Should be caught by callers of vm_execute