Clean up runtime source code a little bit

This commit is contained in:
2024-06-20 02:28:15 +01:00
parent ecadcb5e36
commit 8984c97d33
2 changed files with 7 additions and 7 deletions

View File

@@ -13,12 +13,13 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#if VERBOSE >= 2 #if VERBOSE >= 2
#include <string.h> #include <string.h>
#endif #endif
#include "./runtime.h" #include <vm/runtime.h>
const char *err_as_cstr(err_t err) const char *err_as_cstr(err_t err)
{ {
@@ -67,6 +68,7 @@ err_t vm_execute(vm_t *vm)
return ERR_END_OF_PROGRAM; return ERR_END_OF_PROGRAM;
inst_t instruction = program_data.instructions[prog->ptr]; inst_t instruction = program_data.instructions[prog->ptr];
// Opcodes which defer to another function using lookup table
if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
{ {
err_t err = PUSH_ROUTINES[instruction.opcode](vm, instruction.operand); err_t err = PUSH_ROUTINES[instruction.opcode](vm, instruction.operand);
@@ -128,6 +130,7 @@ err_t vm_execute(vm_t *vm)
return err; return err;
prog->ptr++; prog->ptr++;
} }
// Opcodes defined in loop
else if (instruction.opcode == OP_JUMP_ABS) else if (instruction.opcode == OP_JUMP_ABS)
return vm_jump(vm, instruction.operand.as_word); return vm_jump(vm, instruction.operand.as_word);
else if (instruction.opcode == OP_JUMP_STACK) else if (instruction.opcode == OP_JUMP_STACK)
@@ -141,18 +144,17 @@ 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};
static_assert(DATA_TYPE_NIL == -1 && DATA_TYPE_WORD == 3, static_assert(DATA_TYPE_NIL == -1 && DATA_TYPE_WORD == 3,
"Code using OPCODE_DATA_TYPE for quick same type opcode " "Code using OPCODE_DATA_TYPE for quick same type opcode "
"conversion may be out of date."); "conversion may be out of date.");
data_t datum = {0};
// Here we add OP_POP_BYTE and the data_type_t of the opcode to // Here we add OP_POP_BYTE and the data_type_t of the opcode to
// get the right OP_POP opcode. // get the right OP_POP opcode.
opcode_t pop_opcode = opcode_t pop_opcode =
OPCODE_DATA_TYPE(instruction.opcode, OP_JUMP_IF) + OP_POP_BYTE; OPCODE_DATA_TYPE(instruction.opcode, OP_JUMP_IF) + OP_POP_BYTE;
err_t err = POP_ROUTINES[pop_opcode](vm, &datum); err_t err = POP_ROUTINES[pop_opcode](vm, &datum);
if (err) if (err)
return err; return err;

View File

@@ -13,9 +13,7 @@
#ifndef RUNTIME_H #ifndef RUNTIME_H
#define RUNTIME_H #define RUNTIME_H
#include <lib/inst.h> #include <vm/struct.h>
#include "./struct.h"
typedef enum typedef enum
{ {