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
// production.json)
{"flags": {
"gc": "none",
"d": "danger"
"gc": "none"
},
"verbose": true,
"override_config": true,

View File

@ -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 =

View File

@ -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, )
result = allocate(Iterator, )

View File

@ -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

View File

@ -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 =