diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:30:05 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:30:05 +0100 |
commit | 9da31398baa93e8485aa70fc09cbca66daa0fa40 (patch) | |
tree | 7a59ac23e5cbfb3563b4ed80f36615d534fd98a0 /src/runtime.c | |
parent | 073a23152e509d42ce256f635fbb3da99e0d176a (diff) | |
download | ovm-9da31398baa93e8485aa70fc09cbca66daa0fa40.tar.gz ovm-9da31398baa93e8485aa70fc09cbca66daa0fa40.tar.bz2 ovm-9da31398baa93e8485aa70fc09cbca66daa0fa40.zip |
Implemented vm_* routines for OP_DUP and vm_execute code
Diffstat (limited to 'src/runtime.c')
-rw-r--r-- | src/runtime.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/runtime.c b/src/runtime.c index a5ffd09..241d664 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -67,6 +67,14 @@ void vm_execute(vm_t *vm) vm->registers.ret = d.as_word; prog->ptr++; } + else if (OPCODE_IS_TYPE(instruction.opcode, OP_DUP)) + { + DUP_ROUTINES[instruction.opcode](vm, instruction.operand.as_word); + data_type_t type = OPCODE_DATA_TYPE(instruction.opcode, OP_DUP); + data_t datum = vm_peek(vm, type); + vm->registers.ret = datum.as_word; + prog->ptr++; + } else if (OPCODE_IS_TYPE(instruction.opcode, OP_NOT)) { NOT_ROUTINES[instruction.opcode](vm); @@ -426,6 +434,46 @@ data_t vm_mov_word(vm_t *vm, byte reg) return ret; } +void vm_dup_byte(vm_t *vm, word w) +{ + if (vm->stack.ptr < w + 1) + // TODO: Error STACK_UNDERFLOW + return; + else if (vm->stack.ptr >= vm->stack.max) + // TODO: Error STACK_OVERFLOW + return; + vm_push_byte(vm, DBYTE(vm->stack.data[vm->stack.ptr - 1 - w])); +} + +void vm_dup_hword(vm_t *vm, word w) +{ + if (vm->stack.ptr < HWORD_SIZE * (w + 1)) + // TODO: Error STACK_UNDERFLOW + return; + else if (vm->stack.ptr + HWORD_SIZE >= vm->stack.max) + // TODO: Error STACK_OVERFLOW + return; + byte bytes[HWORD_SIZE] = {0}; + for (size_t i = 0; i < HWORD_SIZE; ++i) + bytes[HWORD_SIZE - i - 1] = + vm->stack.data[vm->stack.ptr - (HWORD_SIZE * (w + 1)) + i]; + vm_push_hword(vm, DHWORD(convert_bytes_to_hword(bytes))); +} + +void vm_dup_word(vm_t *vm, word w) +{ + if (vm->stack.ptr < WORD_SIZE * (w + 1)) + // TODO: Error STACK_UNDERFLOW + return; + else if (vm->stack.ptr + WORD_SIZE >= vm->stack.max) + // TODO: Error STACK_OVERFLOW + return; + byte bytes[WORD_SIZE] = {0}; + for (size_t i = 0; i < WORD_SIZE; ++i) + bytes[i] = vm->stack.data[vm->stack.ptr - 1 - (WORD_SIZE * (w + 1)) + i]; + vm_push_word(vm, DWORD(convert_bytes_to_word(bytes))); +} + data_t vm_pop_byte(vm_t *vm) { if (vm->stack.ptr == 0) |