Added vm_execute_all which executes an entire program

Handles OP_HALT
This commit is contained in:
2023-10-21 23:31:48 +01:00
parent 1c2de9a926
commit d2cdb211b2
3 changed files with 13 additions and 7 deletions

View File

@@ -28,13 +28,7 @@ int interpret_bytecode(const char *filepath)
vm_t vm = {0}; vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack)); vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number); vm_load_program(&vm, instructions, number);
for (size_t i = 0; i < number; ++i) vm_execute_all(&vm);
{
vm_execute(&vm);
printf("Cycle %lu\n", i);
vm_print_all(&vm, stdout);
printf("\n");
}
free(instructions); free(instructions);
return 0; return 0;
} }

View File

@@ -50,6 +50,10 @@ void vm_execute(vm_t *vm)
vm->registers.ret = d.as_word; // will do type punning for me vm->registers.ret = d.as_word; // will do type punning for me
prog->ptr++; prog->ptr++;
} }
else if (instruction.opcode == OP_HALT)
{
// Do nothing here. Should be caught by callers of vm_execute
}
else else
{ {
// TODO: Error (Unknown opcode) // TODO: Error (Unknown opcode)
@@ -57,6 +61,13 @@ void vm_execute(vm_t *vm)
} }
} }
void vm_execute_all(vm_t *vm)
{
struct Program *program = &vm->program;
while (program->instructions[program->ptr].opcode != OP_HALT)
vm_execute(vm);
}
void vm_load_stack(vm_t *vm, byte *bytes, size_t size) void vm_load_stack(vm_t *vm, byte *bytes, size_t size)
{ {
vm->stack.data = bytes; vm->stack.data = bytes;

View File

@@ -44,6 +44,7 @@ typedef struct
#define VM_REG_WORD(REG) ((REG)) #define VM_REG_WORD(REG) ((REG))
void vm_execute(vm_t *); void vm_execute(vm_t *);
void vm_execute_all(vm_t *);
void vm_load_stack(vm_t *, byte *, size_t); void vm_load_stack(vm_t *, byte *, size_t);
void vm_load_program(vm_t *, inst_t *, size_t); void vm_load_program(vm_t *, inst_t *, size_t);