aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 21:39:00 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 21:39:00 +0000
commit32c1bcb859426c73e3bc45fa537e932018346d07 (patch)
treebb65a479169ba91418c2422181d5a663a5900c80
parentbe312cfbbf6daf28c7a99464f2f59bd0c1dd355e (diff)
downloadovm-32c1bcb859426c73e3bc45fa537e932018346d07.tar.gz
ovm-32c1bcb859426c73e3bc45fa537e932018346d07.tar.bz2
ovm-32c1bcb859426c73e3bc45fa537e932018346d07.zip
Added a print_heap mechanism into vm
Pretty simple implementation, nothing massive. In VERBOSE mode, heap is tracked for any allocations/deletions, printing it if so.
-rw-r--r--vm/runtime.c48
-rw-r--r--vm/runtime.h1
2 files changed, 49 insertions, 0 deletions
diff --git a/vm/runtime.c b/vm/runtime.c
index 953470c..8a3ce2e 100644
--- a/vm/runtime.c
+++ b/vm/runtime.c
@@ -261,6 +261,7 @@ err_t vm_execute_all(vm_t *vm)
#if VERBOSE >= 2
registers_t prev_registers = vm->registers;
size_t prev_sptr = 0;
+ size_t prev_pages = 0;
#endif
while (program->instructions[program->ptr].opcode != OP_HALT &&
program->ptr < program->max)
@@ -276,6 +277,15 @@ err_t vm_execute_all(vm_t *vm)
"----------------------------------------------------------------------"
"----------\n",
stdout);
+ if (prev_pages != vm->heap.pages)
+ {
+ vm_print_heap(vm, stdout);
+ prev_pages = vm->heap.pages;
+ fputs("------------------------------------------------------------------"
+ "----"
+ "----------\n",
+ stdout);
+ }
if (memcmp(&prev_registers, &vm->registers, sizeof(darr_t)) != 0)
{
vm_print_registers(vm, stdout);
@@ -419,6 +429,40 @@ void vm_print_program(vm_t *vm, FILE *fp)
fprintf(fp, "]\n");
}
+void vm_print_heap(vm_t *vm, FILE *fp)
+{
+ heap_t heap = vm->heap;
+ fprintf(fp, "Heap.pages = %lu\nHeap.data = [", heap.pages);
+ if (heap.pages == 0)
+ {
+ fprintf(fp, "]\n");
+ return;
+ }
+ page_t *cur = heap.beg;
+ fprintf(fp, "\n");
+ for (size_t i = 0; i < heap.pages; ++i)
+ {
+ fprintf(fp, "\tPage[%lu]: ", i);
+ if (!cur)
+ fprintf(fp, "<NIL>\n");
+ else
+ {
+ fprintf(fp, "{");
+ for (size_t j = 0; j < cur->available; ++j)
+ {
+ fprintf(fp, "%x", cur->data[j]);
+ if (j != cur->available - 1)
+ fprintf(fp, ", ");
+ else if ((j % 8) == 7)
+ fprintf(fp, ",\n\t\t");
+ }
+ fprintf(fp, "}\n");
+ cur = cur->next;
+ }
+ }
+ fprintf(fp, "]\n");
+}
+
void vm_print_all(vm_t *vm, FILE *fp)
{
fputs("----------------------------------------------------------------------"
@@ -428,6 +472,10 @@ void vm_print_all(vm_t *vm, FILE *fp)
fputs("----------------------------------------------------------------------"
"----------\n",
fp);
+ vm_print_heap(vm, fp);
+ fputs("----------------------------------------------------------------------"
+ "----------\n",
+ fp);
vm_print_registers(vm, fp);
fputs("----------------------------------------------------------------------"
"----------\n",
diff --git a/vm/runtime.h b/vm/runtime.h
index 9ac1b47..c8f5d49 100644
--- a/vm/runtime.h
+++ b/vm/runtime.h
@@ -70,6 +70,7 @@ void vm_stop(vm_t *);
void vm_print_registers(vm_t *, FILE *);
void vm_print_stack(vm_t *, FILE *);
void vm_print_program(vm_t *, FILE *);
+void vm_print_heap(vm_t *, FILE *);
void vm_print_all(vm_t *, FILE *);
// Execution routines