aboutsummaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 19:09:13 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 19:09:13 +0000
commita7588ccc6166d1fa49a37db6d2d3aba552901e9e (patch)
tree560f4230f07d6ac7c0b5730394d9998c9534d155 /vm
parentfe7f26256b5da63e46d3e36a8ddfe119dc9095b4 (diff)
downloadovm-a7588ccc6166d1fa49a37db6d2d3aba552901e9e.tar.gz
ovm-a7588ccc6166d1fa49a37db6d2d3aba552901e9e.tar.bz2
ovm-a7588ccc6166d1fa49a37db6d2d3aba552901e9e.zip
Use program structure for runtime
Very barebones, essentially a simple refactor. I need to introduce a feature to append to a program as well, but as it's a flexible structure it will likely have to be functional.
Diffstat (limited to 'vm')
-rw-r--r--vm/runtime.c27
-rw-r--r--vm/runtime.h6
2 files changed, 16 insertions, 17 deletions
diff --git a/vm/runtime.c b/vm/runtime.c
index c5f82c5..16c75f9 100644
--- a/vm/runtime.c
+++ b/vm/runtime.c
@@ -58,9 +58,9 @@ err_t vm_execute(vm_t *vm)
{
static_assert(NUMBER_OF_OPCODES == 98, "vm_execute: Out of date");
struct Program *prog = &vm->program;
- if (prog->ptr >= prog->max)
+ if (prog->ptr >= prog->data->count)
return ERR_END_OF_PROGRAM;
- inst_t instruction = prog->instructions[prog->ptr];
+ inst_t instruction = prog->data->instructions[prog->ptr];
if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
{
@@ -281,8 +281,8 @@ err_t vm_execute_all(vm_t *vm)
size_t prev_pages = 0;
size_t prev_cptr = 0;
#endif
- while (program->instructions[program->ptr].opcode != OP_HALT &&
- program->ptr < program->max)
+ while (program->data->instructions[program->ptr].opcode != OP_HALT &&
+ program->ptr < program->data->count)
{
#if VERBOSE >= 2
fprintf(stdout, "[vm_execute_all]: Trace(Cycle %lu)\n", cycles);
@@ -355,11 +355,10 @@ void vm_load_stack(vm_t *vm, byte *bytes, size_t size)
vm->stack.ptr = 0;
}
-void vm_load_program(vm_t *vm, inst_t *instructions, size_t size)
+void vm_load_program(vm_t *vm, prog_t *program)
{
- vm->program.instructions = instructions;
- vm->program.max = size;
- vm->program.ptr = 0;
+ vm->program.ptr = 0;
+ vm->program.data = program;
}
void vm_load_registers(vm_t *vm, registers_t registers)
@@ -430,7 +429,7 @@ void vm_stop(vm_t *vm)
#endif
free(vm->registers.data);
- free(vm->program.instructions);
+ free(vm->program.data);
free(vm->stack.data);
heap_stop(&vm->heap);
free(vm->call_stack.address_pointers);
@@ -489,7 +488,7 @@ void vm_print_program(vm_t *vm, FILE *fp)
fprintf(fp,
"Program.max = %lu\nProgram.ptr = "
"%lu\nProgram.instructions = [\n",
- program.max, program.ptr);
+ program.data->count, program.ptr);
size_t beg = 0;
if (program.ptr >= VM_PRINT_PROGRAM_EXCERPT)
{
@@ -498,16 +497,16 @@ void vm_print_program(vm_t *vm, FILE *fp)
}
else
beg = 0;
- size_t end = MIN(program.ptr + VM_PRINT_PROGRAM_EXCERPT, program.max);
+ size_t end = MIN(program.ptr + VM_PRINT_PROGRAM_EXCERPT, program.data->count);
for (size_t i = beg; i < end; ++i)
{
fprintf(fp, "\t%lu: ", i);
- inst_print(program.instructions[i], fp);
+ inst_print(program.data->instructions[i], fp);
if (i == program.ptr)
fprintf(fp, " <---");
fprintf(fp, "\n");
}
- if (end != program.max)
+ if (end != program.data->count)
fprintf(fp, "\t...\n");
fprintf(fp, "]\n");
}
@@ -597,7 +596,7 @@ void vm_print_all(vm_t *vm, FILE *fp)
err_t vm_jump(vm_t *vm, word w)
{
- if (w >= vm->program.max)
+ if (w >= vm->program.data->count)
return ERR_INVALID_PROGRAM_ADDRESS;
vm->program.ptr = w;
return ERR_OK;
diff --git a/vm/runtime.h b/vm/runtime.h
index c98dfcf..7fd72b8 100644
--- a/vm/runtime.h
+++ b/vm/runtime.h
@@ -53,8 +53,8 @@ typedef struct
heap_t heap;
struct Program
{
- inst_t *instructions;
- size_t ptr, max;
+ prog_t *data;
+ word ptr;
} program;
struct CallStack
{
@@ -69,7 +69,7 @@ err_t vm_execute_all(vm_t *);
void vm_load_stack(vm_t *, byte *, size_t);
void vm_load_registers(vm_t *, registers_t);
void vm_load_heap(vm_t *, heap_t);
-void vm_load_program(vm_t *, inst_t *, size_t);
+void vm_load_program(vm_t *, prog_t *);
void vm_load_call_stack(vm_t *, word *, size_t);
void vm_stop(vm_t *);