improve debug vm

improved coloring
removed d:danger so debug output is present
fixed frame length debug output
This commit is contained in:
Productive2 2021-02-28 17:00:12 +01:00
parent 17913f1d7c
commit 5d98814d7d
5 changed files with 72 additions and 47 deletions

View File

@ -2,8 +2,7 @@
// from nim itself (the nim compiler options are identical to those of // from nim itself (the nim compiler options are identical to those of
// production.json) // production.json)
{"flags": { {"flags": {
"gc": "none", "gc": "none"
"d": "danger"
}, },
"verbose": true, "verbose": true,
"override_config": true, "override_config": true,

View File

@ -37,7 +37,7 @@ proc clear*(self: CallFrame): int =
inc result inc result
proc getView*(self: CallFrame): ptr ArrayList[ptr Obj] = 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 = proc len*(self: CallFrame): int =

View File

@ -84,7 +84,7 @@ proc `[]`*[T](self: ptr ArrayList[T], slice: Hslice[int, int]): ptr ArrayList[T]
## of the slice ## of the slice
if self.length == 0: if self.length == 0:
raise newException(IndexDefect, "ArrayList index out of bounds") 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") raise newException(IndexDefect, "ArrayList index out of bounds")
result = newArrayList[T]() result = newArrayList[T]()
for i in countup(slice.a, slice.b - 1): 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 = proc getIter*[T](self: ptr ArrayList[T]): Iterator =
## Returns the iterator object of the ## Returns the iterator object of the
## arraylist ## arraylist
result = allocate(Iterator, ) result = allocate(Iterator, )

View File

@ -20,17 +20,29 @@ import ../types/baseObject
import ../types/methods import ../types/methods
import ../types/arraylist import ../types/arraylist
import strformat 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 = proc simpleInstruction(name: string, index: int): int =
echo &"DEBUG - VM:\tInstruction -> {name}" write stdout, &"DEBUG - VM:\tInstruction -> "
printName(name)
nl()
return index + 1 return index + 1
proc byteInstruction(name: string, chunk: Chunk, offset: int): int = proc byteInstruction(name: string, chunk: Chunk, offset: int): int =
var slot = chunk.code[offset + 1] 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 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 constantArray: array[3, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]]
var constant: int var constant: int
copyMem(constant.addr, constantArray.addr, sizeof(constantArray)) 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] let obj = chunk.consts[constant]
echo &"DEBUG - VM:\tOperand -> {stringify(obj)}\nDEBUG - VM:\tValue kind -> {obj.kind}" echo &"DEBUG - VM:\tOperand -> {stringify(obj)}\nDEBUG - VM:\tValue kind -> {obj.kind}"
return offset + 4 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 jumpArray: array[2, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2]]
var jump: int var jump: int
copyMem(jump.addr, jumpArray.addr, sizeof(uint16)) 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 return offset + 3

View File

@ -44,6 +44,9 @@ import types/arraylist
# in production builds # in production builds
import util/debug import util/debug
when DEBUG_TRACE_VM:
import terminal
type type
KeyboardInterrupt* = object of CatchableError KeyboardInterrupt* = object of CatchableError
@ -292,44 +295,49 @@ proc readConstant(self: CallFrame): ptr Obj =
proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) = when DEBUG_TRACE_VM:
## Shows debug information about the current proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) =
## state of the virtual machine ## Shows debug information about the current
let view = frame.getView() ## state of the virtual machine
stdout.write("DEBUG - VM: General information\n")
stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\nDEBUG - VM:\tStack -> [") let view = frame.getView()
for i, v in self.stack: setForegroundColor(fgYellow)
stdout.write(stringify(v)) stdout.write("DEBUG - VM: General information\n")
if i < self.stack.high(): stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\n")
stdout.write(", ") setForegroundColor(fgDefault)
stdout.write("]\nDEBUG - VM: \tGlobals -> {") stdout.write("DEBUG - VM:\tStack -> [")
for i, (k, v) in enumerate(self.globals.pairs()): for i, v in self.stack:
stdout.write(&"'{k}': {stringify(v)}") stdout.write(stringify(v))
if i < self.globals.len() - 1: if i < self.stack.high():
stdout.write(", ") stdout.write(", ")
stdout.write("}\nDEBUG - VM: Frame information\n") stdout.write("]\nDEBUG - VM: \tGlobals -> {")
stdout.write("DEBUG - VM:\tType -> ") for i, (k, v) in enumerate(self.globals.pairs()):
if frame.function.name == nil: stdout.write(&"'{k}': {stringify(v)}")
stdout.write("main\n") if i < self.globals.len() - 1:
else: stdout.write(", ")
stdout.write(&"function, '{frame.function.name.stringify()}'\n") stdout.write("}\nDEBUG - VM: Frame information\n")
echo &"DEBUG - VM:\tCount -> {self.frames.len()}" stdout.write("DEBUG - VM:\tType -> ")
echo &"DEBUG - VM:\tLength -> {view.len}" if frame.function.name == nil:
stdout.write("DEBUG - VM:\tTable -> ") stdout.write("main\n")
stdout.write("[") else:
for i, e in frame.function.chunk.consts: stdout.write(&"function, '{frame.function.name.stringify()}'\n")
stdout.write(stringify(e)) echo &"DEBUG - VM:\tCount -> {self.frames.len()}"
if i < len(frame.function.chunk.consts) - 1: echo &"DEBUG - VM:\tLength -> {view.len}"
stdout.write(", ") stdout.write("DEBUG - VM:\tTable -> ")
stdout.write("]\nDEBUG - VM:\tStack view -> ") stdout.write("[")
stdout.write("[") for i, e in frame.function.chunk.consts:
for i, e in view: stdout.write(stringify(e))
stdout.write(stringify(e)) if i < len(frame.function.chunk.consts) - 1:
if i < len(view) - 1: stdout.write(", ")
stdout.write(", ") stdout.write("]\nDEBUG - VM:\tStack view -> ")
stdout.write("]\n") stdout.write("[")
echo "DEBUG - VM: Current instruction" for i, e in view:
discard disassembleInstruction(frame.function.chunk, frame.ip - 1) 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 = proc run(self: VM): InterpretResult =