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):

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,12 +295,17 @@ proc readConstant(self: CallFrame): ptr Obj =
proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) = when DEBUG_TRACE_VM:
proc showRuntime*(self: VM, frame: CallFrame, iteration: uint64) =
## Shows debug information about the current ## Shows debug information about the current
## state of the virtual machine ## state of the virtual machine
let view = frame.getView() let view = frame.getView()
setForegroundColor(fgYellow)
stdout.write("DEBUG - VM: General information\n") stdout.write("DEBUG - VM: General information\n")
stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\nDEBUG - VM:\tStack -> [") stdout.write(&"DEBUG - VM:\tIteration -> {iteration}\n")
setForegroundColor(fgDefault)
stdout.write("DEBUG - VM:\tStack -> [")
for i, v in self.stack: for i, v in self.stack:
stdout.write(stringify(v)) stdout.write(stringify(v))
if i < self.stack.high(): if i < self.stack.high():