Skip to content

Commit

Permalink
EVM cosmetic changes, one less indirect access of VmCpt
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jul 18, 2024
1 parent ee323d5 commit f604f96
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 326 deletions.
65 changes: 32 additions & 33 deletions nimbus/evm/interpreter/op_dispatcher.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,42 @@ export
# Helpers
# ------------------------------------------------------------------------------

template handleStopDirective(k: var VmCtx) =
template handleStopDirective(cpt: VmCpt) =
#trace "op: Stop"
if not k.cpt.code.atEnd() and k.cpt.tracingEnabled:
if not cpt.code.atEnd() and cpt.tracingEnabled:
# we only trace `REAL STOP` and ignore `FAKE STOP`
k.cpt.opIndex = k.cpt.traceOpCodeStarted(Stop)
k.cpt.traceOpCodeEnded(Stop, k.cpt.opIndex)
cpt.opIndex = cpt.traceOpCodeStarted(Stop)
cpt.traceOpCodeEnded(Stop, cpt.opIndex)


template handleFixedGasCostsDirective(fork: EVMFork; op: Op; k: var VmCtx) =
if k.cpt.tracingEnabled:
k.cpt.opIndex = k.cpt.traceOpCodeStarted(op)
template handleFixedGasCostsDirective(fork: EVMFork; op: Op; cpt: VmCpt) =
if cpt.tracingEnabled:
cpt.opIndex = cpt.traceOpCodeStarted(op)

? k.cpt.opcodeGasCost(op, k.cpt.gasCosts[op].cost, reason = $op)
? vmOpHandlers[fork][op].run(k)
? cpt.opcodeGasCost(op, cpt.gasCosts[op].cost, reason = $op)
? vmOpHandlers[fork][op].run(cpt)

# If continuation is not nil, traceOpCodeEnded will be called in executeOpcodes.
if k.cpt.tracingEnabled and k.cpt.continuation.isNil:
k.cpt.traceOpCodeEnded(op, k.cpt.opIndex)
if cpt.tracingEnabled and cpt.continuation.isNil:
cpt.traceOpCodeEnded(op, cpt.opIndex)


template handleOtherDirective(fork: EVMFork; op: Op; k: var VmCtx) =
if k.cpt.tracingEnabled:
k.cpt.opIndex = k.cpt.traceOpCodeStarted(op)
template handleOtherDirective(fork: EVMFork; op: Op; cpt: VmCpt) =
if cpt.tracingEnabled:
cpt.opIndex = cpt.traceOpCodeStarted(op)

? vmOpHandlers[fork][op].run(k)
? vmOpHandlers[fork][op].run(cpt)

# If continuation is not nil, traceOpCodeEnded will be called in executeOpcodes.
if k.cpt.tracingEnabled and k.cpt.continuation.isNil:
k.cpt.traceOpCodeEnded(op, k.cpt.opIndex)
if cpt.tracingEnabled and cpt.continuation.isNil:
cpt.traceOpCodeEnded(op, cpt.opIndex)

# ------------------------------------------------------------------------------
# Private, big nasty doubly nested case matrix generator
# ------------------------------------------------------------------------------

# reminiscent of Mamy's opTableToCaseStmt() from original VM
proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode =
proc toCaseStmt(forkArg, opArg, cpt: NimNode): NimNode =

# Outer case/switch => Op
let branchOnOp = quote do: `opArg`
Expand All @@ -89,13 +89,13 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode =
let branchStmt = block:
if op == Stop:
quote do:
handleStopDirective(`k`)
handleStopDirective(`cpt`)
elif gcTable[op] == GckFixed:
quote do:
handleFixedGasCostsDirective(`asFork`,`asOp`,`k`)
handleFixedGasCostsDirective(`asFork`,`asOp`,`cpt`)
else:
quote do:
handleOtherDirective(`asFork`,`asOp`,`k`)
handleOtherDirective(`asFork`,`asOp`,`cpt`)

forkCaseSubExpr.add nnkOfBranch.newTree(asFork, branchStmt)

Expand All @@ -112,7 +112,7 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode =
# break no matter what).
quote do:
`forkCaseSubExpr`
if not `k`.cpt.continuation.isNil:
if not `cpt`.continuation.isNil:
break

result.add nnkOfBranch.newTree(asOp, branchStmt)
Expand All @@ -124,26 +124,26 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode =
# Public macros/functions
# ------------------------------------------------------------------------------

macro genOptimisedDispatcher*(fork: EVMFork; op: Op; k: VmCtx): untyped =
result = fork.toCaseStmt(op, k)
macro genOptimisedDispatcher*(fork: EVMFork; op: Op; cpt: VmCpt): untyped =
result = fork.toCaseStmt(op, cpt)


template genLowMemDispatcher*(fork: EVMFork; op: Op; k: VmCtx) =
template genLowMemDispatcher*(fork: EVMFork; op: Op; cpt: VmCpt) =
if op == Stop:
handleStopDirective(k)
handleStopDirective(cpt)
break

if BaseGasCosts[op].kind == GckFixed:
handleFixedGasCostsDirective(fork, op, k)
handleFixedGasCostsDirective(fork, op, cpt)
else:
handleOtherDirective(fork, op, k)
handleOtherDirective(fork, op, cpt)

case c.instr
case cpt.instr
of Return, Revert, SelfDestruct:
break
else:
# FIXME-manyOpcodesNowRequireContinuations
if not k.cpt.continuation.isNil:
if not cpt.continuation.isNil:
break

# ------------------------------------------------------------------------------
Expand All @@ -154,10 +154,9 @@ when isMainModule and isChatty:

import ../types

proc optimised(c: Computation, fork: EVMFork): EvmResultVoid {.compileTime.} =
var desc: VmCtx
proc optimised(cpt: VmCpt, fork: EVMFork): EvmResultVoid {.compileTime.} =
while true:
genOptimisedDispatcher(fork, desc.cpt.instr, desc)
genOptimisedDispatcher(fork, cpt.instr, desc)

# ------------------------------------------------------------------------------
# End
Expand Down
4 changes: 2 additions & 2 deletions nimbus/evm/interpreter/op_handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ const

when isMainModule and isChatty:

proc opHandlersRun(fork: EVMFork; op: Op; d: var VmCtx) {.used.} =
proc opHandlersRun(fork: EVMFork; op: Op; cpt: VmCpt) {.used.} =
## Given a particular `fork` and an `op`-code, run the associated handler
vmOpHandlers[fork][op].run(d)
vmOpHandlers[fork][op].run(cpt)

proc opHandlersName(fork: EVMFork; op: Op): string {.used.} =
## Get name (or ID) of op handler
Expand Down
Loading

0 comments on commit f604f96

Please sign in to comment.