Simplify OP_JUMP_IF implementation
Same method as when simplifying OP_POP's implementation: use the lookup table along with OPCODE_DATA_TYPE abuse. In this case I made a lookup table called OP_POP for this method to work, but it wasn't difficult.
This commit is contained in:
14
vm/runtime.c
14
vm/runtime.c
@@ -139,13 +139,13 @@ err_t vm_execute(vm_t *vm)
|
|||||||
else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
||||||
{
|
{
|
||||||
data_t datum = {0};
|
data_t datum = {0};
|
||||||
err_t err = ERR_OK;
|
|
||||||
if (instruction.opcode == OP_JUMP_IF_BYTE)
|
// Here we add OP_POP_BYTE and the data_type_t of the opcode to
|
||||||
err = vm_pop_byte(vm, &datum);
|
// get the right typed OP_POP opcode.
|
||||||
else if (instruction.opcode == OP_JUMP_IF_HWORD)
|
opcode_t pop_opcode =
|
||||||
err = vm_pop_hword(vm, &datum);
|
OPCODE_DATA_TYPE(instruction.opcode, OP_JUMP_IF) + OP_POP_BYTE;
|
||||||
else if (instruction.opcode == OP_JUMP_IF_WORD)
|
|
||||||
err = vm_pop_word(vm, &datum);
|
err_t err = POP_ROUTINES[pop_opcode](vm, &datum);
|
||||||
|
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|||||||
@@ -89,6 +89,13 @@ err_t vm_pop_byte(vm_t *, data_t *);
|
|||||||
err_t vm_pop_hword(vm_t *, data_t *);
|
err_t vm_pop_hword(vm_t *, data_t *);
|
||||||
err_t vm_pop_word(vm_t *, data_t *);
|
err_t vm_pop_word(vm_t *, data_t *);
|
||||||
|
|
||||||
|
typedef err_t (*pop_f)(vm_t *, data_t *);
|
||||||
|
static const pop_f POP_ROUTINES[] = {
|
||||||
|
[OP_POP_BYTE] = vm_pop_byte,
|
||||||
|
[OP_POP_HWORD] = vm_pop_hword,
|
||||||
|
[OP_POP_WORD] = vm_pop_word,
|
||||||
|
};
|
||||||
|
|
||||||
err_t vm_push_byte(vm_t *, data_t);
|
err_t vm_push_byte(vm_t *, data_t);
|
||||||
err_t vm_push_hword(vm_t *, data_t);
|
err_t vm_push_hword(vm_t *, data_t);
|
||||||
err_t vm_push_word(vm_t *, data_t);
|
err_t vm_push_word(vm_t *, data_t);
|
||||||
|
|||||||
Reference in New Issue
Block a user