From 36bcd90c81f7dc0d7ab268a30768c80ff53eb93d Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 22 Oct 2023 18:04:13 +0100 Subject: 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. --- src/runtime.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) 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) -- cgit v1.2.3-13-gbd6f