Skip to content

Commit b0712a6

Browse files
committed
Trace hook support for the nim vm
1 parent 53ee1d2 commit b0712a6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

compiler/vm.nim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,12 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
535535
move(regs, tos.slots)
536536
var regs: seq[TFullReg] # alias to tos.slots for performance
537537
updateRegsAlias
538+
539+
when callVMExecHooks:
540+
defer:
541+
if not c.exitHook.isNil:
542+
c.exitHook(c, pc, tos)
543+
538544
#echo "NEW RUN ------------------------"
539545
while true:
540546
#{.computedGoto.}
@@ -556,6 +562,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
556562
# other useful variables: c.loopIterations
557563
echo "$# [$#] $#" % [c.config$info, $instr.opcode, c.config.sourceLine(info)]
558564
c.profiler.enter(c, tos)
565+
566+
when callVMExecHooks:
567+
if not c.enterHook.isNil:
568+
c.enterHook(c, pc, tos, instr)
569+
559570
case instr.opcode
560571
of opcEof: return regs[ra]
561572
of opcRet:
@@ -2109,14 +2120,19 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
21092120
storeAny(regs[ra].node.strVal, typ, regs[rb].regToNode, c.config)
21102121

21112122
c.profiler.leave(c)
2112-
2123+
when callVMExecHooks:
2124+
if not c.leaveHook.isNil:
2125+
c.leaveHook(c, pc, tos, instr)
21132126
inc pc
21142127

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

2133+
proc execFromCtx*(c: PCtx, pc: int, tos: PStackFrame): PNode =
2134+
result = rawExecute(c, pc, tos).regToNode
2135+
21202136
proc execProc*(c: PCtx; sym: PSym; args: openArray[PNode]): PNode =
21212137
c.loopIterations = c.config.maxLoopIterationsVM
21222138
if sym.kind in routineKinds:

compiler/vmdef.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const
4444
regBxMin* = -wordExcess+1
4545
regBxMax* = wordExcess-1
4646

47+
callVMExecHooks* = defined(vmExecHooks)
48+
4749
type
4850
TRegister* = range[0..regAMask.int]
4951
TDest* = range[-1..regAMask.int]
@@ -269,6 +271,11 @@ type
269271
vmstateDiff*: seq[(PSym, PNode)] # we remember the "diff" to global state here (feature for IC)
270272
procToCodePos*: Table[int, int]
271273

274+
when callVMExecHooks:
275+
exitHook*: proc (c: PCtx, pc: int, tos: PStackFrame)
276+
enterHook*: proc (c: PCtx, pc: int, tos: PStackFrame, instr: TInstr)
277+
leaveHook*: proc (c: PCtx, pc: int, tos: PStackFrame, instr: TInstr)
278+
272279
PStackFrame* = ref TStackFrame
273280
TStackFrame* {.acyclic.} = object
274281
prc*: PSym # current prc; proc that is evaluated

0 commit comments

Comments
 (0)