improve vmgen.codeListing formatting (#10306)

* improve vmgen.codeListing formatting
* address comments
This commit is contained in:
Timothee Cour 2019-01-15 10:02:01 -08:00 • committed by Andreas Rumpf
commit beed27b75d
3 changed files with 26 additions and 14 deletions

View file

@ -86,7 +86,7 @@ proc codeListing(c: ControlFlowGraph, result: var string, start=0; last = -1) =
result.add("\n") result.add("\n")
inc i inc i
if i in jumpTargets: result.add("L" & $i & ": End\n") if i in jumpTargets: result.add("L" & $i & ": End\n")
# consider calling `asciitables.alignTable`
proc echoCfg*(c: ControlFlowGraph; start=0; last = -1) {.deprecated.} = proc echoCfg*(c: ControlFlowGraph; start=0; last = -1) {.deprecated.} =
## echos the ControlFlowGraph for debugging purposes. ## echos the ControlFlowGraph for debugging purposes.

View file

@ -10,10 +10,6 @@
## This file implements the new evaluation engine for Nim code. ## This file implements the new evaluation engine for Nim code.
## An instruction is 1-3 int32s in memory, it is a register based VM. ## An instruction is 1-3 int32s in memory, it is a register based VM.
const
debugEchoCode = false
traceCode = debugEchoCode
import ast except getstr import ast except getstr
import import
@ -26,6 +22,9 @@ from evaltempl import evalTemplate
from modulegraphs import ModuleGraph, PPassContext from modulegraphs import ModuleGraph, PPassContext
const
traceCode = debugEchoCode
when hasFFI: when hasFFI:
import evalffi import evalffi

View file

@ -33,6 +33,11 @@ import
import platform import platform
from os import splitFile from os import splitFile
const
debugEchoCode* = defined(nimVMDebug)
when debugEchoCode:
import asciitables
when hasFFI: when hasFFI:
import evalffi import evalffi
@ -43,9 +48,10 @@ type
TGenFlags = set[TGenFlag] TGenFlags = set[TGenFlag]
proc debugInfo(c: PCtx; info: TLineInfo): string = proc debugInfo(c: PCtx; info: TLineInfo): string =
result = toFilename(c.config, info).splitFile.name & ":" & $info.line result = toFileLineCol(c.config, info)
proc codeListing(c: PCtx, result: var string, start=0; last = -1) = proc codeListing(c: PCtx, result: var string, start=0; last = -1) =
## for debugging purposes
# first iteration: compute all necessary labels: # first iteration: compute all necessary labels:
var jumpTargets = initIntSet() var jumpTargets = initIntSet()
let last = if last < 0: c.code.len-1 else: min(last, c.code.len-1) let last = if last < 0: c.code.len-1 else: min(last, c.code.len-1)
@ -54,7 +60,9 @@ proc codeListing(c: PCtx, result: var string, start=0; last = -1) =
if x.opcode in relativeJumps: if x.opcode in relativeJumps:
jumpTargets.incl(i+x.regBx-wordExcess) jumpTargets.incl(i+x.regBx-wordExcess)
# for debugging purposes template toStr(opc: TOpcode): string = ($opc).substr(3)
result.add "code listing:\n"
var i = start var i = start
while i <= last: while i <= last:
if i in jumpTargets: result.addf("L$1:\n", i) if i in jumpTargets: result.addf("L$1:\n", i)
@ -62,34 +70,39 @@ proc codeListing(c: PCtx, result: var string, start=0; last = -1) =
result.add($i) result.add($i)
let opc = opcode(x) let opc = opcode(x)
if opc in {opcConv, opcCast}: if opc in {opcIndCall, opcIndCallAsgn}:
result.addf("\t$#\tr$#, r$#, nargs:$#", opc.toStr, x.regA,
x.regB, x.regC)
elif opc in {opcConv, opcCast}:
let y = c.code[i+1] let y = c.code[i+1]
let z = c.code[i+2] let z = c.code[i+2]
result.addf("\t$#\tr$#, r$#, $#, $#", ($opc).substr(3), x.regA, x.regB, result.addf("\t$#\tr$#, r$#, $#, $#", opc.toStr, x.regA, x.regB,
c.types[y.regBx-wordExcess].typeToString, c.types[y.regBx-wordExcess].typeToString,
c.types[z.regBx-wordExcess].typeToString) c.types[z.regBx-wordExcess].typeToString)
inc i, 2 inc i, 2
elif opc < firstABxInstr: elif opc < firstABxInstr:
result.addf("\t$#\tr$#, r$#, r$#", ($opc).substr(3), x.regA, result.addf("\t$#\tr$#, r$#, r$#", opc.toStr, x.regA,
x.regB, x.regC) x.regB, x.regC)
elif opc in relativeJumps: elif opc in relativeJumps:
result.addf("\t$#\tr$#, L$#", ($opc).substr(3), x.regA, result.addf("\t$#\tr$#, L$#", opc.toStr, x.regA,
i+x.regBx-wordExcess) i+x.regBx-wordExcess)
elif opc in {opcLdConst, opcAsgnConst}: elif opc in {opcLdConst, opcAsgnConst}:
let idx = x.regBx-wordExcess let idx = x.regBx-wordExcess
result.addf("\t$#\tr$#, $# ($#)", ($opc).substr(3), x.regA, result.addf("\t$#\tr$#, $# ($#)", opc.toStr, x.regA,
c.constants[idx].renderTree, $idx) c.constants[idx].renderTree, $idx)
elif opc in {opcMarshalLoad, opcMarshalStore}: elif opc in {opcMarshalLoad, opcMarshalStore}:
let y = c.code[i+1] let y = c.code[i+1]
result.addf("\t$#\tr$#, r$#, $#", ($opc).substr(3), x.regA, x.regB, result.addf("\t$#\tr$#, r$#, $#", opc.toStr, x.regA, x.regB,
c.types[y.regBx-wordExcess].typeToString) c.types[y.regBx-wordExcess].typeToString)
inc i inc i
else: else:
result.addf("\t$#\tr$#, $#", ($opc).substr(3), x.regA, x.regBx-wordExcess) result.addf("\t$#\tr$#, $#", opc.toStr, x.regA, x.regBx-wordExcess)
result.add("\t#") result.add("\t#")
result.add(debugInfo(c, c.debug[i])) result.add(debugInfo(c, c.debug[i]))
result.add("\n") result.add("\n")
inc i inc i
when debugEchoCode:
result = result.alignTable
proc echoCode*(c: PCtx; start=0; last = -1) {.deprecated.} = proc echoCode*(c: PCtx; start=0; last = -1) {.deprecated.} =
var buf = "" var buf = ""