Skip to content

Commit

Permalink
Trace hook support for the nim vm
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Jan 31, 2022
1 parent 545b62d commit 71da996
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 17 additions & 1 deletion compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
move(regs, tos.slots)
var regs: seq[TFullReg] # alias to tos.slots for performance
updateRegsAlias

when callVMExecHooks:
defer:
if not c.exitHook.isNil:
c.exitHook(c, pc, tos)

#echo "NEW RUN ------------------------"
while true:
#{.computedGoto.}
Expand All @@ -556,6 +562,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
# other useful variables: c.loopIterations
echo "$# [$#] $#" % [c.config$info, $instr.opcode, c.config.sourceLine(info)]
c.profiler.enter(c, tos)

when callVMExecHooks:
if not c.enterHook.isNil:
c.enterHook(c, pc, tos, instr)

case instr.opcode
of opcEof: return regs[ra]
of opcRet:
Expand Down Expand Up @@ -2109,14 +2120,19 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
storeAny(regs[ra].node.strVal, typ, regs[rb].regToNode, c.config)

c.profiler.leave(c)

when callVMExecHooks:
if not c.leaveHook.isNil:
c.leaveHook(c, pc, tos, instr)
inc pc

proc execute(c: PCtx, start: int): PNode =
var tos = PStackFrame(prc: nil, comesFrom: 0, next: nil)
newSeq(tos.slots, c.prc.regInfo.len)
result = rawExecute(c, start, tos).regToNode

proc execFromCtx*(c: PCtx, pc: int, tos: PStackFrame): PNode =
result = rawExecute(c, pc, tos).regToNode

proc execProc*(c: PCtx; sym: PSym; args: openArray[PNode]): PNode =
c.loopIterations = c.config.maxLoopIterationsVM
if sym.kind in routineKinds:
Expand Down
7 changes: 7 additions & 0 deletions compiler/vmdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const
regBxMin* = -wordExcess+1
regBxMax* = wordExcess-1

callVMExecHooks* = defined(vmExecHooks)

type
TRegister* = range[0..regAMask.int]
TDest* = range[-1..regAMask.int]
Expand Down Expand Up @@ -269,6 +271,11 @@ type
vmstateDiff*: seq[(PSym, PNode)] # we remember the "diff" to global state here (feature for IC)
procToCodePos*: Table[int, int]

when callVMExecHooks:
exitHook*: proc (c: PCtx, pc: int, tos: PStackFrame)
enterHook*: proc (c: PCtx, pc: int, tos: PStackFrame, instr: TInstr)
leaveHook*: proc (c: PCtx, pc: int, tos: PStackFrame, instr: TInstr)

PStackFrame* = ref TStackFrame
TStackFrame* {.acyclic.} = object
prc*: PSym # current prc; proc that is evaluated
Expand Down

0 comments on commit 71da996

Please sign in to comment.