diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 18:04:13 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 18:07:41 +0100 |
commit | 36bcd90c81f7dc0d7ab268a30768c80ff53eb93d (patch) | |
tree | 62b68065c74ff06fd7391c5e54840e7426a93d47 /src/runtime.c | |
parent | 936971c1a3abe4bd5f197bad65dc8330fb4a22dc (diff) | |
download | ovm-36bcd90c81f7dc0d7ab268a30768c80ff53eb93d.tar.gz ovm-36bcd90c81f7dc0d7ab268a30768c80ff53eb93d.tar.bz2 ovm-36bcd90c81f7dc0d7ab268a30768c80ff53eb93d.zip |
When VEROBSE flag is set greater than 0, print traces in vm_execute_all
For each cycle, print the cycle and any changes. We track changes on
the stack by remembering the previous stack pointer. For registers, I
remember the previous array of registers and do a byte level compare
of the current registers and the remembered ones.
Produces pretty log messages and an easy way to track execution.
Diffstat (limited to 'src/runtime.c')
-rw-r--r-- | src/runtime.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/runtime.c b/src/runtime.c index 77af177..77ad109 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -114,8 +114,52 @@ void vm_execute(vm_t *vm) void vm_execute_all(vm_t *vm) { struct Program *program = &vm->program; +#if VERBOSE == 1 + struct Registers prev_registers = vm->registers; + size_t cycles = 0; + size_t prev_sptr = 0; +#endif while (program->instructions[program->ptr].opcode != OP_HALT) + { +#if VERBOSE >= 1 + fprintf(stdout, "[vm_execute_all]: Trace(Cycle %lu)\n", cycles); + fputs( + "----------------------------------------------------------------------" + "----------\n", + stdout); + vm_print_program(vm, stdout); + fputs( + "----------------------------------------------------------------------" + "----------\n", + stdout); + if (memcmp(prev_registers.reg, vm->registers.reg, + ARR_SIZE(vm->registers.reg)) != 0) + { + vm_print_registers(vm, stdout); + prev_registers = vm->registers; + fputs("------------------------------------------------------------------" + "----" + "----------\n", + stdout); + } + if (prev_sptr != vm->stack.ptr) + { + vm_print_stack(vm, stdout); + prev_sptr = vm->stack.ptr; + fputs("------------------------------------------------------------------" + "----" + "----------\n", + stdout); + } + ++cycles; +#endif vm_execute(vm); + } + +#if VERBOSE >= 1 + fprintf(stdout, "[vm_execute_all]: Final VM state(Cycle %lu)\n", cycles); + vm_print_all(vm, stdout); +#endif } void vm_load_stack(vm_t *vm, byte *bytes, size_t size) |