From 5d98814d7d75d0b322ebb723221be59f94e94659 Mon Sep 17 00:00:00 2001 From: Productive2 <48047721+Productive2@users.noreply.github.com> Date: Sun, 28 Feb 2021 17:00:12 +0100 Subject: [PATCH] improve debug vm improved coloring removed d:danger so debug output is present fixed frame length debug output --- resources/profiles/debug_vm.json | 3 +- src/meta/frame.nim | 2 +- src/types/arraylist.nim | 4 +- src/util/debug.nim | 26 ++++++++-- src/vm.nim | 84 +++++++++++++++++--------------- 5 files changed, 72 insertions(+), 47 deletions(-) diff --git a/resources/profiles/debug_vm.json b/resources/profiles/debug_vm.json index 5d861ad..8737928 100644 --- a/resources/profiles/debug_vm.json +++ b/resources/profiles/debug_vm.json @@ -2,8 +2,7 @@ // from nim itself (the nim compiler options are identical to those of // production.json) {"flags": { - "gc": "none", - "d": "danger" + "gc": "none" }, "verbose": true, "override_config": true, diff --git a/src/meta/frame.nim b/src/meta/frame.nim index 99ad354..79c16b3 100644 --- a/src/meta/frame.nim +++ b/src/meta/frame.nim @@ -37,7 +37,7 @@ proc clear*(self: CallFrame): int = inc result proc getView*(self: CallFrame): ptr ArrayList[ptr Obj] = - result = self.stack[self.slot..self.stack.high()] + result = self.stack[self.slot..self.stack.len()] proc len*(self: CallFrame): int = diff --git a/src/types/arraylist.nim b/src/types/arraylist.nim index e7b5c22..668e779 100644 --- a/src/types/arraylist.nim +++ b/src/types/arraylist.nim @@ -84,7 +84,7 @@ proc `[]`*[T](self: ptr ArrayList[T], slice: Hslice[int, int]): ptr ArrayList[T] ## of the slice if self.length == 0: raise newException(IndexDefect, "ArrayList index out of bounds") - if slice.a notin 0..self.length - 1 or slice.b notin 0..self.length - 1: + if slice.a notin 0..self.length - 1 or slice.b notin 0..self.length: raise newException(IndexDefect, "ArrayList index out of bounds") result = newArrayList[T]() for i in countup(slice.a, slice.b - 1): @@ -189,4 +189,4 @@ proc `$`*[T](self: ptr ArrayList[T]): string = proc getIter*[T](self: ptr ArrayList[T]): Iterator = ## Returns the iterator object of the ## arraylist - result = allocate(Iterator, ) \ No newline at end of file + result = allocate(Iterator, ) diff --git a/src/util/debug.nim b/src/util/debug.nim index f3355f7..00d6e32 100644 --- a/src/util/debug.nim +++ b/src/util/debug.nim @@ -20,17 +20,29 @@ import ../types/baseObject import ../types/methods import ../types/arraylist import strformat +import terminal +proc printName(name: string) = + setForegroundColor(fgGreen) + write stdout, name + setForegroundColor(fgDefault) +proc nl = + write stdout, "\n" proc simpleInstruction(name: string, index: int): int = - echo &"DEBUG - VM:\tInstruction -> {name}" + write stdout, &"DEBUG - VM:\tInstruction -> " + printName(name) + nl() return index + 1 proc byteInstruction(name: string, chunk: Chunk, offset: int): int = var slot = chunk.code[offset + 1] - echo &"DEBUG - VM:\tInstruction -> {name}, points to slot {slot}" + write stdout, &"DEBUG - VM:\tInstruction -> " + printName(name) + write stdout, &", points to slot {slot}" + nl() return offset + 2 @@ -39,7 +51,10 @@ proc constantInstruction(name: string, chunk: Chunk, offset: int): int = var constantArray: array[3, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]] var constant: int copyMem(constant.addr, constantArray.addr, sizeof(constantArray)) - echo &"DEBUG - VM:\tInstruction -> {name}, points to slot {constant}" + write stdout, &"DEBUG - VM:\tInstruction -> " + printName(name) + write stdout, &", points to slot {constant}" + nl() let obj = chunk.consts[constant] echo &"DEBUG - VM:\tOperand -> {stringify(obj)}\nDEBUG - VM:\tValue kind -> {obj.kind}" return offset + 4 @@ -49,7 +64,10 @@ proc jumpInstruction(name: string, chunk: Chunk, offset: int): int = var jumpArray: array[2, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2]] var jump: int copyMem(jump.addr, jumpArray.addr, sizeof(uint16)) - echo &"DEBUG - VM:\tInstruction -> {name}\nDEBUG - VM:\tJump size -> {jump}" + write stdout, &"DEBUG - VM:\tInstruction -> " + printName(name) + write stdout, &"\nDEBUG - VM:\tJump size -> {jump}" + nl() return offset + 3 diff --git a/src/vm.nim b/src/vm.nim index 64c17a8..353082d 100644 --- a/src/vm.nim +++ b/src/vm.nim @@ -44,6 +44,9 @@ import types/arraylist # in production builds import util/debug +when DEBUG_TRACE_VM: + import terminal + type KeyboardInterrupt* = object of CatchableError @@ -292,44 +295,49 @@ proc readConstant(self: CallFrame): ptr Obj = -proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) = - ## Shows debug information about the current - ## state of the virtual machine - let view = frame.getView() - stdout.write("DEBUG - VM: General information\n") - stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\nDEBUG - VM:\tStack -> [") - for i, v in self.stack: - stdout.write(stringify(v)) - if i < self.stack.high(): - stdout.write(", ") - stdout.write("]\nDEBUG - VM: \tGlobals -> {") - for i, (k, v) in enumerate(self.globals.pairs()): - stdout.write(&"'{k}': {stringify(v)}") - if i < self.globals.len() - 1: - stdout.write(", ") - stdout.write("}\nDEBUG - VM: Frame information\n") - stdout.write("DEBUG - VM:\tType -> ") - if frame.function.name == nil: - stdout.write("main\n") - else: - stdout.write(&"function, '{frame.function.name.stringify()}'\n") - echo &"DEBUG - VM:\tCount -> {self.frames.len()}" - echo &"DEBUG - VM:\tLength -> {view.len}" - stdout.write("DEBUG - VM:\tTable -> ") - stdout.write("[") - for i, e in frame.function.chunk.consts: - stdout.write(stringify(e)) - if i < len(frame.function.chunk.consts) - 1: - stdout.write(", ") - stdout.write("]\nDEBUG - VM:\tStack view -> ") - stdout.write("[") - for i, e in view: - stdout.write(stringify(e)) - if i < len(view) - 1: - stdout.write(", ") - stdout.write("]\n") - echo "DEBUG - VM: Current instruction" - discard disassembleInstruction(frame.function.chunk, frame.ip - 1) +when DEBUG_TRACE_VM: + proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) = + ## Shows debug information about the current + ## state of the virtual machine + + let view = frame.getView() + setForegroundColor(fgYellow) + stdout.write("DEBUG - VM: General information\n") + stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\n") + setForegroundColor(fgDefault) + stdout.write("DEBUG - VM:\tStack -> [") + for i, v in self.stack: + stdout.write(stringify(v)) + if i < self.stack.high(): + stdout.write(", ") + stdout.write("]\nDEBUG - VM: \tGlobals -> {") + for i, (k, v) in enumerate(self.globals.pairs()): + stdout.write(&"'{k}': {stringify(v)}") + if i < self.globals.len() - 1: + stdout.write(", ") + stdout.write("}\nDEBUG - VM: Frame information\n") + stdout.write("DEBUG - VM:\tType -> ") + if frame.function.name == nil: + stdout.write("main\n") + else: + stdout.write(&"function, '{frame.function.name.stringify()}'\n") + echo &"DEBUG - VM:\tCount -> {self.frames.len()}" + echo &"DEBUG - VM:\tLength -> {view.len}" + stdout.write("DEBUG - VM:\tTable -> ") + stdout.write("[") + for i, e in frame.function.chunk.consts: + stdout.write(stringify(e)) + if i < len(frame.function.chunk.consts) - 1: + stdout.write(", ") + stdout.write("]\nDEBUG - VM:\tStack view -> ") + stdout.write("[") + for i, e in view: + stdout.write(stringify(e)) + if i < len(view) - 1: + stdout.write(", ") + stdout.write("]\n") + echo "DEBUG - VM: Current instruction" + discard disassembleInstruction(frame.function.chunk, frame.ip - 1) proc run(self: VM): InterpretResult =