diff --git a/nimbus/evm/interpreter/op_dispatcher.nim b/nimbus/evm/interpreter/op_dispatcher.nim index 4a6d12c19..ffec01c0f 100644 --- a/nimbus/evm/interpreter/op_dispatcher.nim +++ b/nimbus/evm/interpreter/op_dispatcher.nim @@ -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` @@ -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) @@ -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) @@ -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 # ------------------------------------------------------------------------------ @@ -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 diff --git a/nimbus/evm/interpreter/op_handlers.nim b/nimbus/evm/interpreter/op_handlers.nim index 66ff96fad..7940c171a 100644 --- a/nimbus/evm/interpreter/op_handlers.nim +++ b/nimbus/evm/interpreter/op_handlers.nim @@ -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 diff --git a/nimbus/evm/interpreter/op_handlers/oph_arithmetic.nim b/nimbus/evm/interpreter/op_handlers/oph_arithmetic.nim index e84d6920c..befdd3ddb 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_arithmetic.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_arithmetic.nim @@ -37,19 +37,19 @@ func slt(x, y: UInt256): bool = # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc addOp (k: var VmCtx): EvmResultVoid = +proc addOp(cpt: VmCpt): EvmResultVoid = ## 0x01, Addition - k.cpt.stack.binaryOp(`+`) + cpt.stack.binaryOp(`+`) -proc mulOp(k: var VmCtx): EvmResultVoid = +proc mulOp(cpt: VmCpt): EvmResultVoid = ## 0x02, Multiplication - k.cpt.stack.binaryOp(`*`) + cpt.stack.binaryOp(`*`) -proc subOp(k: var VmCtx): EvmResultVoid = +proc subOp(cpt: VmCpt): EvmResultVoid = ## 0x03, Substraction - k.cpt.stack.binaryOp(`-`) + cpt.stack.binaryOp(`-`) -proc divideOp(k: var VmCtx): EvmResultVoid = +proc divideOp(cpt: VmCpt): EvmResultVoid = ## 0x04, Division template div256(top, lhs, rhs) = if rhs.isZero: @@ -58,9 +58,9 @@ proc divideOp(k: var VmCtx): EvmResultVoid = else: top = lhs div rhs - k.cpt.stack.binaryWithTop(div256) + cpt.stack.binaryWithTop(div256) -proc sdivOp(k: var VmCtx): EvmResultVoid = +proc sdivOp(cpt: VmCpt): EvmResultVoid = ## 0x05, Signed division template sdiv256(top, lhs, rhs) = if rhs.isZero.not: @@ -70,9 +70,9 @@ proc sdivOp(k: var VmCtx): EvmResultVoid = top = lhs div rhs setSign(top, signA xor signB) - k.cpt.stack.binaryWithTop(sdiv256) + cpt.stack.binaryWithTop(sdiv256) -proc moduloOp(k: var VmCtx): EvmResultVoid = +proc moduloOp(cpt: VmCpt): EvmResultVoid = ## 0x06, Modulo template mod256(top, lhs, rhs) = if rhs.isZero: @@ -80,9 +80,9 @@ proc moduloOp(k: var VmCtx): EvmResultVoid = else: top = lhs mod rhs - k.cpt.stack.binaryWithTop(mod256) + cpt.stack.binaryWithTop(mod256) -proc smodOp(k: var VmCtx): EvmResultVoid = +proc smodOp(cpt: VmCpt): EvmResultVoid = ## 0x07, Signed modulo template smod256(top, lhs, rhs) = if rhs.isZero.not: @@ -92,47 +92,47 @@ proc smodOp(k: var VmCtx): EvmResultVoid = top = lhs mod rhs setSign(top, sign) - k.cpt.stack.binaryWithTop(smod256) + cpt.stack.binaryWithTop(smod256) -proc addmodOp(k: var VmCtx): EvmResultVoid = +proc addmodOp(cpt: VmCpt): EvmResultVoid = ## 0x08, Modulo addition ## Intermediate computations do not roll over at 2^256 - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - lhs = k.cpt.stack.lsPeekInt(^1) - rhs = k.cpt.stack.lsPeekInt(^2) - modulus = k.cpt.stack.lsPeekInt(^3) + lhs = cpt.stack.lsPeekInt(^1) + rhs = cpt.stack.lsPeekInt(^2) + modulus = cpt.stack.lsPeekInt(^3) value = if modulus.isZero: zero(UInt256) else: addmod(lhs, rhs, modulus) - k.cpt.stack.lsShrink(2) - k.cpt.stack.lsTop value + cpt.stack.lsShrink(2) + cpt.stack.lsTop value ok() -proc mulmodOp(k: var VmCtx): EvmResultVoid = +proc mulmodOp(cpt: VmCpt): EvmResultVoid = ## 0x09, Modulo multiplication ## Intermediate computations do not roll over at 2^256 - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - lhs = k.cpt.stack.lsPeekInt(^1) - rhs = k.cpt.stack.lsPeekInt(^2) - modulus = k.cpt.stack.lsPeekInt(^3) + lhs = cpt.stack.lsPeekInt(^1) + rhs = cpt.stack.lsPeekInt(^2) + modulus = cpt.stack.lsPeekInt(^3) value = if modulus.isZero: zero(UInt256) else: mulmod(lhs, rhs, modulus) - k.cpt.stack.lsShrink(2) - k.cpt.stack.lsTop value + cpt.stack.lsShrink(2) + cpt.stack.lsTop value ok() -proc expOp(k: var VmCtx): EvmResultVoid = +proc expOp(cpt: VmCpt): EvmResultVoid = ## 0x0A, Exponentiation template exp256(top, base, exponent) = - ? k.cpt.opcodeGasCost(Exp, - k.cpt.gasCosts[Exp].d_handler(exponent), + ? cpt.opcodeGasCost(Exp, + cpt.gasCosts[Exp].d_handler(exponent), reason = "EXP: exponent bytes") if not base.isZero: @@ -145,9 +145,9 @@ proc expOp(k: var VmCtx): EvmResultVoid = else: top = zero(UInt256) - k.cpt.stack.binaryWithTop(exp256) + cpt.stack.binaryWithTop(exp256) -proc signExtendOp(k: var VmCtx): EvmResultVoid = +proc signExtendOp(cpt: VmCpt): EvmResultVoid = ## 0x0B, Sign extend ## Extend length of two’s complement signed integer. template se256(top, bits, value) = @@ -164,62 +164,62 @@ proc signExtendOp(k: var VmCtx): EvmResultVoid = else: top = value - k.cpt.stack.binaryWithTop(se256) + cpt.stack.binaryWithTop(se256) -proc ltOp(k: var VmCtx): EvmResultVoid = +proc ltOp(cpt: VmCpt): EvmResultVoid = ## 0x10, Less-than comparison template lt256(lhs, rhs): auto = (lhs < rhs).uint.u256 - k.cpt.stack.binaryOp(lt256) + cpt.stack.binaryOp(lt256) -proc gtOp(k: var VmCtx): EvmResultVoid = +proc gtOp(cpt: VmCpt): EvmResultVoid = ## 0x11, Greater-than comparison template gt256(lhs, rhs): auto = (lhs > rhs).uint.u256 - k.cpt.stack.binaryOp(gt256) + cpt.stack.binaryOp(gt256) -proc sltOp(k: var VmCtx): EvmResultVoid = +proc sltOp(cpt: VmCpt): EvmResultVoid = ## 0x12, Signed less-than comparison template slt256(lhs, rhs): auto = slt(lhs, rhs).uint.u256 - k.cpt.stack.binaryOp(slt256) + cpt.stack.binaryOp(slt256) -proc sgtOp(k: var VmCtx): EvmResultVoid = +proc sgtOp(cpt: VmCpt): EvmResultVoid = ## 0x13, Signed greater-than comparison # Arguments are swapped and SLT is used. template sgt256(lhs, rhs): auto = slt(rhs, lhs).uint.u256 - k.cpt.stack.binaryOp(sgt256) + cpt.stack.binaryOp(sgt256) -proc eqOp(k: var VmCtx): EvmResultVoid = +proc eqOp(cpt: VmCpt): EvmResultVoid = ## 0x14, Equality comparison template eq256(lhs, rhs): auto = (lhs == rhs).uint.u256 - k.cpt.stack.binaryOp(eq256) + cpt.stack.binaryOp(eq256) -proc isZeroOp(k: var VmCtx): EvmResultVoid = +proc isZeroOp(cpt: VmCpt): EvmResultVoid = ## 0x15, Check if zero template zero256(value): auto = value.isZero.uint.u256 - k.cpt.stack.unaryOp(zero256) + cpt.stack.unaryOp(zero256) -proc andOp(k: var VmCtx): EvmResultVoid = +proc andOp(cpt: VmCpt): EvmResultVoid = ## 0x16, Bitwise AND - k.cpt.stack.binaryOp(`and`) + cpt.stack.binaryOp(`and`) -proc orOp(k: var VmCtx): EvmResultVoid = +proc orOp(cpt: VmCpt): EvmResultVoid = ## 0x17, Bitwise OR - k.cpt.stack.binaryOp(`or`) + cpt.stack.binaryOp(`or`) -proc xorOp(k: var VmCtx): EvmResultVoid = +proc xorOp(cpt: VmCpt): EvmResultVoid = ## 0x18, Bitwise XOR - k.cpt.stack.binaryOp(`xor`) + cpt.stack.binaryOp(`xor`) -proc notOp(k: var VmCtx): EvmResultVoid = +proc notOp(cpt: VmCpt): EvmResultVoid = ## 0x19, Check if zero - k.cpt.stack.unaryOp(`not`) + cpt.stack.unaryOp(`not`) -proc byteOp(k: var VmCtx): EvmResultVoid = +proc byteOp(cpt: VmCpt): EvmResultVoid = ## 0x20, Retrieve single byte from word. template byte256(top, position, value) = if position >= 32.u256: @@ -231,11 +231,11 @@ proc byteOp(k: var VmCtx): EvmResultVoid = else: top = cast[array[32, byte]](value)[31 - pos].u256 - k.cpt.stack.binaryWithTop(byte256) + cpt.stack.binaryWithTop(byte256) # Constantinople's new opcodes -proc shlOp(k: var VmCtx): EvmResultVoid = +proc shlOp(cpt: VmCpt): EvmResultVoid = ## 0x1b, Shift left template shl256(top, lhs, num) = let shiftLen = lhs.safeInt @@ -244,9 +244,9 @@ proc shlOp(k: var VmCtx): EvmResultVoid = else: top = num shl shiftLen - k.cpt.stack.binaryWithTop(shl256) + cpt.stack.binaryWithTop(shl256) -proc shrOp(k: var VmCtx): EvmResultVoid = +proc shrOp(cpt: VmCpt): EvmResultVoid = ## 0x1c, Shift right logical template shr256(top, lhs, num) = let shiftLen = lhs.safeInt @@ -256,9 +256,9 @@ proc shrOp(k: var VmCtx): EvmResultVoid = # uint version of `shr` top = num shr shiftLen - k.cpt.stack.binaryWithTop(shr256) + cpt.stack.binaryWithTop(shr256) -proc sarOp(k: var VmCtx): EvmResultVoid = +proc sarOp(cpt: VmCpt): EvmResultVoid = ## 0x1d, Shift right arithmetic template sar256(top, lhs, num256) = let @@ -275,7 +275,7 @@ proc sarOp(k: var VmCtx): EvmResultVoid = # into uint256 top = cast[UInt256](num shr shiftLen) - k.cpt.stack.binaryWithTop(sar256) + cpt.stack.binaryWithTop(sar256) # ------------------------------------------------------------------------------ # Public, op exec table entries diff --git a/nimbus/evm/interpreter/op_handlers/oph_blockdata.nim b/nimbus/evm/interpreter/op_handlers/oph_blockdata.nim index 7dd0d31b1..91853b8ae 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_blockdata.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_blockdata.nim @@ -30,66 +30,65 @@ when not defined(evmc_enabled): # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc blockhashOp (k: var VmCtx): EvmResultVoid = +proc blockhashOp(cpt: VmCpt): EvmResultVoid = ## 0x40, Get the hash of one of the 256 most recent complete blocks. template block256(top, number, conv) = if number > high(BlockNumber).u256: top = zero(UInt256) else: - conv(k.cpt.getBlockHash(number.truncate(BlockNumber)), top) + conv(cpt.getBlockHash(number.truncate(BlockNumber)), top) - k.cpt.stack.unaryWithTop(block256) + cpt.stack.unaryWithTop(block256) -proc coinBaseOp (k: var VmCtx): EvmResultVoid = +proc coinBaseOp(cpt: VmCpt): EvmResultVoid = ## 0x41, Get the block's beneficiary address. - k.cpt.stack.push k.cpt.getCoinbase + cpt.stack.push cpt.getCoinbase -proc timestampOp (k: var VmCtx): EvmResultVoid = +proc timestampOp(cpt: VmCpt): EvmResultVoid = ## 0x42, Get the block's timestamp. - k.cpt.stack.push k.cpt.getTimestamp + cpt.stack.push cpt.getTimestamp -proc blocknumberOp (k: var VmCtx): EvmResultVoid = +proc blocknumberOp(cpt: VmCpt): EvmResultVoid = ## 0x43, Get the block's number. - k.cpt.stack.push k.cpt.getBlockNumber + cpt.stack.push cpt.getBlockNumber -proc difficultyOp (k: var VmCtx): EvmResultVoid = +proc difficultyOp(cpt: VmCpt): EvmResultVoid = ## 0x44, Get the block's difficulty - k.cpt.stack.push k.cpt.getDifficulty + cpt.stack.push cpt.getDifficulty -proc gasLimitOp (k: var VmCtx): EvmResultVoid = +proc gasLimitOp(cpt: VmCpt): EvmResultVoid = ## 0x45, Get the block's gas limit - k.cpt.stack.push k.cpt.getGasLimit + cpt.stack.push cpt.getGasLimit -proc chainIdOp (k: var VmCtx): EvmResultVoid = +proc chainIdOp(cpt: VmCpt): EvmResultVoid = ## 0x46, Get current chain’s EIP-155 unique identifier. - k.cpt.stack.push k.cpt.getChainId + cpt.stack.push cpt.getChainId -proc selfBalanceOp (k: var VmCtx): EvmResultVoid = +proc selfBalanceOp(cpt: VmCpt): EvmResultVoid = ## 0x47, Get current contract's balance. - let cpt = k.cpt cpt.stack.push cpt.getBalance(cpt.msg.contractAddress) -proc baseFeeOp (k: var VmCtx): EvmResultVoid = +proc baseFeeOp(cpt: VmCpt): EvmResultVoid = ## 0x48, Get the block's base fee. - k.cpt.stack.push k.cpt.getBaseFee + cpt.stack.push cpt.getBaseFee -proc blobHashOp (k: var VmCtx): EvmResultVoid = +proc blobHashOp(cpt: VmCpt): EvmResultVoid = ## 0x49, Get current transaction's EIP-4844 versioned hash. template blob256(top, number, conv) = let index = number.safeInt - len = k.cpt.getVersionedHashesLen + len = cpt.getVersionedHashesLen if index < len: - conv(k.cpt.getVersionedHash(index), top) + conv(cpt.getVersionedHash(index), top) else: top = zero(UInt256) - k.cpt.stack.unaryWithTop(blob256) + cpt.stack.unaryWithTop(blob256) -proc blobBaseFeeOp (k: var VmCtx): EvmResultVoid = +proc blobBaseFeeOp(cpt: VmCpt): EvmResultVoid = ## 0x4a, Get the block's base fee. - k.cpt.stack.push k.cpt.getBlobBaseFee + cpt.stack.push cpt.getBlobBaseFee # ------------------------------------------------------------------------------ diff --git a/nimbus/evm/interpreter/op_handlers/oph_call.nim b/nimbus/evm/interpreter/op_handlers/oph_call.nim index a88066282..aaf28d204 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_call.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_call.nim @@ -213,14 +213,12 @@ else: # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc callOp(k: var VmCtx): EvmResultVoid = +proc callOp(cpt: VmCpt): EvmResultVoid = ## 0xf1, Message-Call into an account - let cpt = k.cpt - if EVMC_STATIC in cpt.msg.flags: - let val = ? cpt.stack[^3, UInt256] - if val > 0.u256: - return err(opErr(StaticContext)) + let val = ? cpt.stack[^3, UInt256] + if val > 0.u256: + return err(opErr(StaticContext)) let p = ? cpt.callParams @@ -292,10 +290,9 @@ proc callOp(k: var VmCtx): EvmResultVoid = # --------------------- -proc callCodeOp(k: var VmCtx): EvmResultVoid = +proc callCodeOp(cpt: VmCpt): EvmResultVoid = ## 0xf2, Message-call into this account with an alternative account's code. let - cpt = k.cpt p = ? cpt.callCodeParams (gasCost, childGasLimit) = ? cpt.gasCosts[CallCode].c_handler( p.value, @@ -365,11 +362,10 @@ proc callCodeOp(k: var VmCtx): EvmResultVoid = # --------------------- -proc delegateCallOp(k: var VmCtx): EvmResultVoid = +proc delegateCallOp(cpt: VmCpt): EvmResultVoid = ## 0xf4, Message-call into this account with an alternative account's ## code, but persisting the current values for sender and value. let - cpt = k.cpt p = ? cpt.delegateCallParams (gasCost, childGasLimit) = ? cpt.gasCosts[DelegateCall].c_handler( p.value, @@ -433,11 +429,10 @@ proc delegateCallOp(k: var VmCtx): EvmResultVoid = # --------------------- -proc staticCallOp(k: var VmCtx): EvmResultVoid = +proc staticCallOp(cpt: VmCpt): EvmResultVoid = ## 0xfa, Static message-call into an account. let - cpt = k.cpt p = ? cpt.staticCallParams (gasCost, childGasLimit) = ? cpt.gasCosts[StaticCall].c_handler( p.value, diff --git a/nimbus/evm/interpreter/op_handlers/oph_create.nim b/nimbus/evm/interpreter/op_handlers/oph_create.nim index 1b1b188b2..515fd3d72 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_create.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_create.nim @@ -87,13 +87,12 @@ else: # ------------------------------------------------------------------------------ -proc createOp(k: var VmCtx): EvmResultVoid = +proc createOp(cpt: VmCpt): EvmResultVoid = ## 0xf0, Create a new account with associated code - ? checkInStaticContext(k.cpt) - ? k.cpt.stack.lsCheck(3) + ? cpt.checkInStaticContext() + ? cpt.stack.lsCheck(3) let - cpt = k.cpt endowment = cpt.stack.lsPeekInt(^1) memPos = cpt.stack.lsPeekSafeInt(^2) memLen = cpt.stack.lsPeekSafeInt(^3) @@ -167,13 +166,12 @@ proc createOp(k: var VmCtx): EvmResultVoid = # --------------------- -proc create2Op(k: var VmCtx): EvmResultVoid = +proc create2Op(cpt: VmCpt): EvmResultVoid = ## 0xf5, Behaves identically to CREATE, except using keccak256 - ? checkInStaticContext(k.cpt) - ? k.cpt.stack.lsCheck(4) + ? cpt.checkInStaticContext() + ? cpt.stack.lsCheck(4) let - cpt = k.cpt endowment = cpt.stack.lsPeekInt(^1) memPos = cpt.stack.lsPeekSafeInt(^2) memLen = cpt.stack.lsPeekSafeInt(^3) diff --git a/nimbus/evm/interpreter/op_handlers/oph_defs.nim b/nimbus/evm/interpreter/op_handlers/oph_defs.nim index ee4bfb7a3..7dce2a1ac 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_defs.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_defs.nim @@ -21,12 +21,11 @@ import ../op_codes type - VmCtx* = tuple - cpt: Computation ## computation text + VmCpt* = Computation ## computation text VmOpFn* = ## general op handler, return codes are passed - ## back via argument descriptor ``k`` - proc(k: var VmCtx): EvmResultVoid {.nimcall, gcsafe, raises:[].} + ## back via argument descriptor ``cpt`` + proc(cpt: VmCpt): EvmResultVoid {.nimcall, gcsafe, raises:[].} VmOpExec* = tuple ## op code handler entry opCode: Op ## index back-reference @@ -41,7 +40,7 @@ type const VmOpIgnore*: VmOpFn = ## No operation, placeholder function - proc(k: var VmCtx): EvmResultVoid = ok() + proc(cpt: VmCpt): EvmResultVoid = ok() # similar to: toSeq(Fork).mapIt({it}).foldl(a+b) VmOpAllForks* = diff --git a/nimbus/evm/interpreter/op_handlers/oph_dup.nim b/nimbus/evm/interpreter/op_handlers/oph_dup.nim index db3ac63ee..693f3344f 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_dup.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_dup.nim @@ -41,8 +41,8 @@ proc fnInfo(n: int): string {.compileTime.} = "Duplicate " & blurb & " item in the stack" -template dupImpl(k: var VmCtx; n: int): EvmResultVoid = - k.cpt.stack.dup(n) +template dupImpl(cpt: VmCpt; n: int): EvmResultVoid = + cpt.stack.dup(n) const inxRange = toSeq(1 .. 16) diff --git a/nimbus/evm/interpreter/op_handlers/oph_envinfo.nim b/nimbus/evm/interpreter/op_handlers/oph_envinfo.nim index dd75e0871..40e40e62e 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_envinfo.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_envinfo.nim @@ -35,71 +35,70 @@ when not defined(evmc_enabled): # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc addressOp (k: var VmCtx): EvmResultVoid = +proc addressOp(cpt: VmCpt): EvmResultVoid = ## 0x30, Get address of currently executing account. - k.cpt.stack.push k.cpt.msg.contractAddress + cpt.stack.push cpt.msg.contractAddress # ------------------ -proc balanceOp (k: var VmCtx): EvmResultVoid = +proc balanceOp(cpt: VmCpt): EvmResultVoid = ## 0x31, Get balance of the given account. template balance256(address): auto = - k.cpt.getBalance(address) - k.cpt.stack.unaryAddress(balance256) + cpt.getBalance(address) + cpt.stack.unaryAddress(balance256) -proc balanceEIP2929Op (k: var VmCtx): EvmResultVoid = +proc balanceEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x31, EIP292: Get balance of the given account for Berlin and later template balanceEIP2929(address): auto = - let gasCost = k.cpt.gasEip2929AccountCheck(address) - ? k.cpt.opcodeGasCost(Balance, gasCost, reason = "Balance EIP2929") - k.cpt.getBalance(address) - k.cpt.stack.unaryAddress(balanceEIP2929) + let gasCost = cpt.gasEip2929AccountCheck(address) + ? cpt.opcodeGasCost(Balance, gasCost, reason = "Balance EIP2929") + cpt.getBalance(address) + cpt.stack.unaryAddress(balanceEIP2929) # ------------------ -proc originOp (k: var VmCtx): EvmResultVoid = +proc originOp(cpt: VmCpt): EvmResultVoid = ## 0x32, Get execution origination address. - k.cpt.stack.push k.cpt.getOrigin() + cpt.stack.push cpt.getOrigin() -proc callerOp (k: var VmCtx): EvmResultVoid = +proc callerOp(cpt: VmCpt): EvmResultVoid = ## 0x33, Get caller address. - k.cpt.stack.push k.cpt.msg.sender + cpt.stack.push cpt.msg.sender -proc callValueOp (k: var VmCtx): EvmResultVoid = +proc callValueOp(cpt: VmCpt): EvmResultVoid = ## 0x34, Get deposited value by the instruction/transaction ## responsible for this execution - k.cpt.stack.push k.cpt.msg.value + cpt.stack.push cpt.msg.value -proc callDataLoadOp (k: var VmCtx): EvmResultVoid = +proc callDataLoadOp(cpt: VmCpt): EvmResultVoid = ## 0x35, Get input data of current environment - ? k.cpt.stack.lsCheck(1) - let start = k.cpt.stack.lsPeekMemRef(^1) + ? cpt.stack.lsCheck(1) + let start = cpt.stack.lsPeekMemRef(^1) - if start >= k.cpt.msg.data.len: - k.cpt.stack.lsTop 0 + if start >= cpt.msg.data.len: + cpt.stack.lsTop 0 return ok() # If the data does not take 32 bytes, pad with zeros let - endRange = min(k.cpt.msg.data.len - 1, start + 31) + endRange = min(cpt.msg.data.len - 1, start + 31) presentBytes = endRange - start # We rely on value being initialized with 0 by default var value: array[32, byte] - assign(value.toOpenArray(0, presentBytes), k.cpt.msg.data.toOpenArray(start, endRange)) - k.cpt.stack.lsTop value + assign(value.toOpenArray(0, presentBytes), cpt.msg.data.toOpenArray(start, endRange)) + cpt.stack.lsTop value ok() -proc callDataSizeOp (k: var VmCtx): EvmResultVoid = +proc callDataSizeOp(cpt: VmCpt): EvmResultVoid = ## 0x36, Get size of input data in current environment. - k.cpt.stack.push k.cpt.msg.data.len.u256 + cpt.stack.push cpt.msg.data.len.u256 -proc callDataCopyOp (k: var VmCtx): EvmResultVoid = +proc callDataCopyOp(cpt: VmCpt): EvmResultVoid = ## 0x37, Copy input data in current environment to memory. - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - cpt = k.cpt memPos = cpt.stack.lsPeekMemRef(^1) copyPos = cpt.stack.lsPeekMemRef(^2) len = cpt.stack.lsPeekMemRef(^3) @@ -113,17 +112,15 @@ proc callDataCopyOp (k: var VmCtx): EvmResultVoid = ok() -proc codeSizeOp (k: var VmCtx): EvmResultVoid = +proc codeSizeOp(cpt: VmCpt): EvmResultVoid = ## 0x38, Get size of code running in current environment. - let cpt = k.cpt cpt.stack.push cpt.code.len -proc codeCopyOp (k: var VmCtx): EvmResultVoid = +proc codeCopyOp(cpt: VmCpt): EvmResultVoid = ## 0x39, Copy code running in current environment to memory. - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - cpt = k.cpt memPos = cpt.stack.lsPeekMemRef(^1) copyPos = cpt.stack.lsPeekMemRef(^2) len = cpt.stack.lsPeekMemRef(^3) @@ -136,34 +133,33 @@ proc codeCopyOp (k: var VmCtx): EvmResultVoid = cpt.memory.writePadded(cpt.code.bytes, memPos, copyPos, len) ok() -proc gasPriceOp (k: var VmCtx): EvmResultVoid = +proc gasPriceOp(cpt: VmCpt): EvmResultVoid = ## 0x3A, Get price of gas in current environment. - k.cpt.stack.push k.cpt.getGasPrice() + cpt.stack.push cpt.getGasPrice() # ----------- -proc extCodeSizeOp (k: var VmCtx): EvmResultVoid = +proc extCodeSizeOp(cpt: VmCpt): EvmResultVoid = ## 0x3b, Get size of an account's code template ecs256(address): auto = - k.cpt.getCodeSize(address) - k.cpt.stack.unaryAddress(ecs256) + cpt.getCodeSize(address) + cpt.stack.unaryAddress(ecs256) -proc extCodeSizeEIP2929Op (k: var VmCtx): EvmResultVoid = +proc extCodeSizeEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x3b, Get size of an account's code template ecsEIP2929(address): auto = - let gasCost = k.cpt.gasEip2929AccountCheck(address) - ? k.cpt.opcodeGasCost(ExtCodeSize, gasCost, reason = "ExtCodeSize EIP2929") - k.cpt.getCodeSize(address) + let gasCost = cpt.gasEip2929AccountCheck(address) + ? cpt.opcodeGasCost(ExtCodeSize, gasCost, reason = "ExtCodeSize EIP2929") + cpt.getCodeSize(address) - k.cpt.stack.unaryAddress(ecsEIP2929) + cpt.stack.unaryAddress(ecsEIP2929) # ----------- -proc extCodeCopyOp (k: var VmCtx): EvmResultVoid = +proc extCodeCopyOp(cpt: VmCpt): EvmResultVoid = ## 0x3c, Copy an account's code to memory. - ? k.cpt.stack.lsCheck(4) + ? cpt.stack.lsCheck(4) let - cpt = k.cpt address = cpt.stack.lsPeekAddress(^1) memPos = cpt.stack.lsPeekMemRef(^2) codePos = cpt.stack.lsPeekMemRef(^3) @@ -179,11 +175,10 @@ proc extCodeCopyOp (k: var VmCtx): EvmResultVoid = ok() -proc extCodeCopyEIP2929Op (k: var VmCtx): EvmResultVoid = +proc extCodeCopyEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x3c, Copy an account's code to memory. - ? k.cpt.stack.lsCheck(4) + ? cpt.stack.lsCheck(4) let - cpt = k.cpt address = cpt.stack.lsPeekAddress(^1) memPos = cpt.stack.lsPeekMemRef(^2) codePos = cpt.stack.lsPeekMemRef(^3) @@ -200,17 +195,16 @@ proc extCodeCopyEIP2929Op (k: var VmCtx): EvmResultVoid = # ----------- -proc returnDataSizeOp (k: var VmCtx): EvmResultVoid = +proc returnDataSizeOp(cpt: VmCpt): EvmResultVoid = ## 0x3d, Get size of output data from the previous call from the ## current environment. - k.cpt.stack.push k.cpt.returnData.len + cpt.stack.push cpt.returnData.len -proc returnDataCopyOp (k: var VmCtx): EvmResultVoid = +proc returnDataCopyOp(cpt: VmCpt): EvmResultVoid = ## 0x3e, Copy output data from the previous call to memory. - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - cpt = k.cpt memPos = cpt.stack.lsPeekMemRef(^1) copyPos = cpt.stack.lsPeekMemRef(^2) len = cpt.stack.lsPeekMemRef(^3) @@ -227,19 +221,19 @@ proc returnDataCopyOp (k: var VmCtx): EvmResultVoid = # --------------- -proc extCodeHashOp (k: var VmCtx): EvmResultVoid = +proc extCodeHashOp(cpt: VmCpt): EvmResultVoid = ## 0x3f, Returns the keccak256 hash of a contract’s code template ech256(address): auto = - k.cpt.getCodeHash(address) - k.cpt.stack.unaryAddress(ech256) + cpt.getCodeHash(address) + cpt.stack.unaryAddress(ech256) -proc extCodeHashEIP2929Op (k: var VmCtx): EvmResultVoid = +proc extCodeHashEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x3f, EIP2929: Returns the keccak256 hash of a contract’s code template echEIP2929(address): auto = - let gasCost = k.cpt.gasEip2929AccountCheck(address) - ? k.cpt.opcodeGasCost(ExtCodeHash, gasCost, reason = "ExtCodeHash EIP2929") - k.cpt.getCodeHash(address) - k.cpt.stack.unaryAddress(echEIP2929) + let gasCost = cpt.gasEip2929AccountCheck(address) + ? cpt.opcodeGasCost(ExtCodeHash, gasCost, reason = "ExtCodeHash EIP2929") + cpt.getCodeHash(address) + cpt.stack.unaryAddress(echEIP2929) # ------------------------------------------------------------------------------ # Public, op exec table entries diff --git a/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim b/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim index ee955091d..a9744f2b1 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim @@ -19,7 +19,6 @@ import type OphNumToTextFn* = proc(n: int): string - # OpHanldlerImplFn* = proc(k: var VmCtx; n: static int): EvmResultVoid const recForkSet = "VmOpAllForks" @@ -48,9 +47,9 @@ macro genOphHandlers*(runHandler: static[OphNumToTextFn]; body: untyped): untyped = ## Generate the equivalent of ## :: - ## const : VmOpFn = proc (k: var VmCtx) = + ## const : VmOpFn = proc(cpt: VmCpt) = ## ## , - ## + ## ## ## for all `n` in `inxList` ## @@ -61,12 +60,11 @@ macro genOphHandlers*(runHandler: static[OphNumToTextFn]; fnName = ident(n.runHandler) comment = newCommentStmtNode(n.itemInfo) - # => push##Op: VmOpFn = proc (k: var VmCtx) = ... + # => push##Op: VmOpFn = proc(cpt: VmCpt) = ... result.add quote do: - proc `fnName`(k: var VmCtx): EvmResultVoid = + proc `fnName`(cpt: VmCpt): EvmResultVoid = `comment` - `body`(k,`n`) - # echo ">>>", result.repr + `body`(cpt,`n`) macro genOphList*(runHandler: static[OphNumToTextFn]; @@ -83,9 +81,7 @@ macro genOphList*(runHandler: static[OphNumToTextFn]; ## (opCode: , ## forks: VmOpAllForks, ## info: , - ## exec: (prep: VmOpIgnore, - ## run: , - ## post: VmOpIgnore)) + ## exec: ) ## ## for all `n` in `inxList` ## @@ -113,7 +109,6 @@ macro genOphList*(runHandler: static[OphNumToTextFn]; newIdentNode("VmOpExec")), nnkPrefix.newTree( newIdentNode("@"), records)))) - # echo ">>> ", result.repr # ------------------------------------------------------------------------------ # End diff --git a/nimbus/evm/interpreter/op_handlers/oph_hash.nim b/nimbus/evm/interpreter/op_handlers/oph_hash.nim index 4fa461198..98a84e9be 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_hash.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_hash.nim @@ -29,11 +29,10 @@ import # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc sha3Op(k: var VmCtx): EvmResultVoid = +proc sha3Op(cpt: VmCpt): EvmResultVoid = ## 0x20, Compute Keccak-256 hash. - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt pos = cpt.stack.lsPeekSafeInt(^1) len = cpt.stack.lsPeekSafeInt(^2) cpt.stack.lsShrink(1) @@ -51,7 +50,7 @@ proc sha3Op(k: var VmCtx): EvmResultVoid = if endRange == -1 or pos >= cpt.memory.len: cpt.stack.lsTop(EMPTY_SHA3) else: - cpt.stack.lsTop keccakHash k.cpt.memory.bytes.toOpenArray(pos, endRange) + cpt.stack.lsTop keccakHash cpt.memory.bytes.toOpenArray(pos, endRange) ok() # ------------------------------------------------------------------------------ diff --git a/nimbus/evm/interpreter/op_handlers/oph_log.nim b/nimbus/evm/interpreter/op_handlers/oph_log.nim index aa7a84b36..87b4ad5f3 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_log.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_log.nim @@ -98,8 +98,8 @@ const # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc wrapperFn(k: var VmCtx; n: static int): EvmResultVoid = - logImpl(k.cpt, logOpArg[n], n) +proc wrapperFn(cpt: VmCpt; n: static int): EvmResultVoid = + cpt.logImpl(logOpArg[n], n) genOphHandlers fnName, fnInfo, inxRange, wrapperFn diff --git a/nimbus/evm/interpreter/op_handlers/oph_memory.nim b/nimbus/evm/interpreter/op_handlers/oph_memory.nim index 4d247c21b..e9e818040 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_memory.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_memory.nim @@ -120,19 +120,17 @@ func jumpImpl(c: Computation; jumpTarget: UInt256): EvmResultVoid = # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc popOp(k: var VmCtx): EvmResultVoid = +proc popOp(cpt: VmCpt): EvmResultVoid = ## 0x50, Remove item from stack. - k.cpt.stack.popInt.isOkOr: + cpt.stack.popInt.isOkOr: return err(error) ok() -proc mloadOp (k: var VmCtx): EvmResultVoid = +proc mloadOp(cpt: VmCpt): EvmResultVoid = ## 0x51, Load word from memory - ? k.cpt.stack.lsCheck(1) - let - cpt = k.cpt - memPos = cpt.stack.lsPeekMemRef(^1) + ? cpt.stack.lsCheck(1) + let memPos = cpt.stack.lsPeekMemRef(^1) ? cpt.opcodeGasCost(Mload, cpt.gasCosts[Mload].m_handler(cpt.memory.len, memPos, 32), @@ -143,11 +141,10 @@ proc mloadOp (k: var VmCtx): EvmResultVoid = ok() -proc mstoreOp (k: var VmCtx): EvmResultVoid = +proc mstoreOp(cpt: VmCpt): EvmResultVoid = ## 0x52, Save word to memory - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt memPos = cpt.stack.lsPeekMemRef(^1) value = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -160,11 +157,10 @@ proc mstoreOp (k: var VmCtx): EvmResultVoid = cpt.memory.write(memPos, value.toBytesBE) -proc mstore8Op (k: var VmCtx): EvmResultVoid = +proc mstore8Op(cpt: VmCpt): EvmResultVoid = ## 0x53, Save byte to memory - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt memPos = cpt.stack.lsPeekMemRef(^1) value = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -179,27 +175,26 @@ proc mstore8Op (k: var VmCtx): EvmResultVoid = # ------- -proc sloadOp (k: var VmCtx): EvmResultVoid = +proc sloadOp(cpt: VmCpt): EvmResultVoid = ## 0x54, Load word from storage. template sload256(top, slot, conv) = - top = k.cpt.getStorage(slot) - k.cpt.stack.unaryWithTop(sload256) + top = cpt.getStorage(slot) + cpt.stack.unaryWithTop(sload256) -proc sloadEIP2929Op (k: var VmCtx): EvmResultVoid = +proc sloadEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x54, EIP2929: Load word from storage for Berlin and later template sloadEIP2929(top, slot, conv) = - let gasCost = k.cpt.gasEip2929AccountCheck(k.cpt.msg.contractAddress, slot) - ? k.cpt.opcodeGasCost(Sload, gasCost, reason = "sloadEIP2929") - top = k.cpt.getStorage(slot) - k.cpt.stack.unaryWithTop(sloadEIP2929) + let gasCost = cpt.gasEip2929AccountCheck(cpt.msg.contractAddress, slot) + ? cpt.opcodeGasCost(Sload, gasCost, reason = "sloadEIP2929") + top = cpt.getStorage(slot) + cpt.stack.unaryWithTop(sloadEIP2929) # ------- -proc sstoreOp (k: var VmCtx): EvmResultVoid = +proc sstoreOp(cpt: VmCpt): EvmResultVoid = ## 0x55, Save word to storage. - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) newValue = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -208,11 +203,10 @@ proc sstoreOp (k: var VmCtx): EvmResultVoid = sstoreEvmcOrSstore(cpt, slot, newValue) -proc sstoreEIP1283Op (k: var VmCtx): EvmResultVoid = +proc sstoreEIP1283Op(cpt: VmCpt): EvmResultVoid = ## 0x55, EIP1283: sstore for Constantinople and later - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) newValue = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -221,11 +215,10 @@ proc sstoreEIP1283Op (k: var VmCtx): EvmResultVoid = sstoreEvmcOrNetGasMetering(cpt, slot, newValue) -proc sstoreEIP2200Op (k: var VmCtx): EvmResultVoid = +proc sstoreEIP2200Op(cpt: VmCpt): EvmResultVoid = ## 0x55, EIP2200: sstore for Istanbul and later - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) newValue = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -239,11 +232,10 @@ proc sstoreEIP2200Op (k: var VmCtx): EvmResultVoid = sstoreEvmcOrNetGasMetering(cpt, slot, newValue) -proc sstoreEIP2929Op (k: var VmCtx): EvmResultVoid = +proc sstoreEIP2929Op(cpt: VmCpt): EvmResultVoid = ## 0x55, EIP2929: sstore for Berlin and later - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) newValue = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -270,60 +262,58 @@ proc sstoreEIP2929Op (k: var VmCtx): EvmResultVoid = # ------- -proc jumpOp (k: var VmCtx): EvmResultVoid = +proc jumpOp(cpt: VmCpt): EvmResultVoid = ## 0x56, Alter the program counter - let jumpTarget = ? k.cpt.stack.popInt() - jumpImpl(k.cpt, jumpTarget) + let jumpTarget = ? cpt.stack.popInt() + cpt.jumpImpl(jumpTarget) -proc jumpIOp (k: var VmCtx): EvmResultVoid = +proc jumpIOp(cpt: VmCpt): EvmResultVoid = ## 0x57, Conditionally alter the program counter. - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - jumpTarget = k.cpt.stack.lsPeekInt(^1) - testedValue = k.cpt.stack.lsPeekInt(^2) - k.cpt.stack.lsShrink(2) + jumpTarget = cpt.stack.lsPeekInt(^1) + testedValue = cpt.stack.lsPeekInt(^2) + cpt.stack.lsShrink(2) if testedValue.isZero: return ok() - jumpImpl(k.cpt, jumpTarget) + cpt.jumpImpl(jumpTarget) -proc pcOp (k: var VmCtx): EvmResultVoid = +proc pcOp(cpt: VmCpt): EvmResultVoid = ## 0x58, Get the value of the program counter prior to the increment ## corresponding to this instruction. - k.cpt.stack.push max(k.cpt.code.pc - 1, 0) + cpt.stack.push max(cpt.code.pc - 1, 0) -proc msizeOp (k: var VmCtx): EvmResultVoid = +proc msizeOp(cpt: VmCpt): EvmResultVoid = ## 0x59, Get the size of active memory in bytes. - k.cpt.stack.push k.cpt.memory.len + cpt.stack.push cpt.memory.len -proc gasOp (k: var VmCtx): EvmResultVoid = +proc gasOp(cpt: VmCpt): EvmResultVoid = ## 0x5a, Get the amount of available gas, including the corresponding ## reduction for the cost of this instruction. - k.cpt.stack.push k.cpt.gasMeter.gasRemaining + cpt.stack.push cpt.gasMeter.gasRemaining -proc jumpDestOp (k: var VmCtx): EvmResultVoid = +proc jumpDestOp(cpt: VmCpt): EvmResultVoid = ## 0x5b, Mark a valid destination for jumps. This operation has no effect ## on machine state during execution. ok() -proc tloadOp (k: var VmCtx): EvmResultVoid = +proc tloadOp(cpt: VmCpt): EvmResultVoid = ## 0x5c, Load word from transient storage. - ? k.cpt.stack.lsCheck(1) + ? cpt.stack.lsCheck(1) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) val = cpt.getTransientStorage(slot) cpt.stack.lsTop val ok() -proc tstoreOp (k: var VmCtx): EvmResultVoid = +proc tstoreOp(cpt: VmCpt): EvmResultVoid = ## 0x5d, Save word to transient storage. - ? checkInStaticContext(k.cpt) + ? cpt.checkInStaticContext() - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt slot = cpt.stack.lsPeekInt(^1) val = cpt.stack.lsPeekInt(^2) cpt.stack.lsShrink(2) @@ -331,11 +321,10 @@ proc tstoreOp (k: var VmCtx): EvmResultVoid = cpt.setTransientStorage(slot, val) ok() -proc mCopyOp (k: var VmCtx): EvmResultVoid = +proc mCopyOp(cpt: VmCpt): EvmResultVoid = ## 0x5e, Copy memory - ? k.cpt.stack.lsCheck(3) + ? cpt.stack.lsCheck(3) let - cpt = k.cpt dstPos = cpt.stack.lsPeekMemRef(^1) srcPos = cpt.stack.lsPeekMemRef(^2) len = cpt.stack.lsPeekMemRef(^3) diff --git a/nimbus/evm/interpreter/op_handlers/oph_push.nim b/nimbus/evm/interpreter/op_handlers/oph_push.nim index e20e8185b..70f5259e5 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_push.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_push.nim @@ -40,8 +40,8 @@ proc fnInfo(n: int): string {.compileTime.} = "Push " & blurb & " on the stack" -template pushImpl(k: var VmCtx; n: static int): EvmResultVoid = - k.cpt.stack.push k.cpt.code.readVmWord(n) +template pushImpl(cpt: VmCpt; n: static int): EvmResultVoid = + cpt.stack.push cpt.code.readVmWord(n) const inxRange = toSeq(1 .. 32) @@ -65,9 +65,9 @@ genOphList fnName, fnInfo, inxRange, "VmOpExecPush", opName # about which opcodes are for which forks, but that seems uglier than # just adding Push0 here as a special case.) -proc push0Op(k: var VmCtx): EvmResultVoid = +proc push0Op(cpt: VmCpt): EvmResultVoid = ## 0x5f, push 0 onto the stack - k.cpt.stack.push(0) + cpt.stack.push(0) const VmOpExecPushZero*: seq[VmOpExec] = @[ diff --git a/nimbus/evm/interpreter/op_handlers/oph_swap.nim b/nimbus/evm/interpreter/op_handlers/oph_swap.nim index 349d048b5..57396399a 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_swap.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_swap.nim @@ -41,8 +41,8 @@ proc fnInfo(n: int): string {.compileTime.} = "Exchange first and " & blurb & " stack items" -template swapImpl(k: var VmCtx; n: static int): EvmResultVoid = - k.cpt.stack.swap(n) +template swapImpl(cpt: VmCpt; n: static int): EvmResultVoid = + cpt.stack.swap(n) const inxRange = toSeq(1 .. 16) diff --git a/nimbus/evm/interpreter/op_handlers/oph_sysops.nim b/nimbus/evm/interpreter/op_handlers/oph_sysops.nim index 6d8e8f958..b5b363e09 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_sysops.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_sysops.nim @@ -37,11 +37,10 @@ when not defined(evmc_enabled): # Private # ------------------------------------------------------------------------------ -proc returnOp(k: var VmCtx): EvmResultVoid = +proc returnOp(cpt: VmCpt): EvmResultVoid = ## 0xf3, Halt execution returning output data. - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt pos = cpt.stack.lsPeekMemRef(^1) len = cpt.stack.lsPeekMemRef(^2) cpt.stack.lsShrink(2) @@ -55,12 +54,11 @@ proc returnOp(k: var VmCtx): EvmResultVoid = ok() -proc revertOp(k: var VmCtx): EvmResultVoid = +proc revertOp(cpt: VmCpt): EvmResultVoid = ## 0xfd, Halt execution reverting state changes but returning data ## and remaining gas. - ? k.cpt.stack.lsCheck(2) + ? cpt.stack.lsCheck(2) let - cpt = k.cpt pos = cpt.stack.lsPeekMemRef(^1) len = cpt.stack.lsPeekMemRef(^2) cpt.stack.lsShrink(2) @@ -75,29 +73,24 @@ proc revertOp(k: var VmCtx): EvmResultVoid = cpt.setError(EVMC_REVERT, "REVERT opcode executed", false) ok() -proc invalidOp(k: var VmCtx): EvmResultVoid = +proc invalidOp(cpt: VmCpt): EvmResultVoid = err(opErr(InvalidInstruction)) # ----------- -proc selfDestructOp(k: var VmCtx): EvmResultVoid = +proc selfDestructOp(cpt: VmCpt): EvmResultVoid = ## 0xff, Halt execution and register account for later deletion. - let - cpt = k.cpt - beneficiary = ? cpt.stack.popAddress() + let beneficiary = ? cpt.stack.popAddress() when defined(evmc_enabled): - block: - cpt.selfDestruct(beneficiary) + cpt.selfDestruct(beneficiary) else: - block: - cpt.selfDestruct(beneficiary) + cpt.selfDestruct(beneficiary) ok() -proc selfDestructEIP150Op(k: var VmCtx): EvmResultVoid = +proc selfDestructEIP150Op(cpt: VmCpt): EvmResultVoid = ## selfDestructEip150 (auto generated comment) let - cpt = k.cpt beneficiary = ? cpt.stack.popAddress() condition = not cpt.accountExists(beneficiary) gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition) @@ -107,27 +100,25 @@ proc selfDestructEIP150Op(k: var VmCtx): EvmResultVoid = cpt.selfDestruct(beneficiary) ok() -proc selfDestructEIP161Op(k: var VmCtx): EvmResultVoid = +proc selfDestructEIP161Op(cpt: VmCpt): EvmResultVoid = ## selfDestructEip161 (auto generated comment) - let cpt = k.cpt - ? checkInStaticContext(cpt) + ? cpt.checkInStaticContext() let beneficiary = ? cpt.stack.popAddress() - isDead = not cpt.accountExists(beneficiary) - balance = cpt.getBalance(cpt.msg.contractAddress) - condition = isDead and not balance.isZero - gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition) + isDead = not cpt.accountExists(beneficiary) + balance = cpt.getBalance(cpt.msg.contractAddress) + condition = isDead and not balance.isZero + gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition) ? cpt.opcodeGasCost(SelfDestruct, gasCost, reason = "SELFDESTRUCT EIP161") cpt.selfDestruct(beneficiary) ok() -proc selfDestructEIP2929Op(k: var VmCtx): EvmResultVoid = +proc selfDestructEIP2929Op(cpt: VmCpt): EvmResultVoid = ## selfDestructEIP2929 (auto generated comment) - let cpt = k.cpt - ? checkInStaticContext(cpt) + ? cpt.checkInStaticContext() let beneficiary = ? cpt.stack.popAddress() diff --git a/nimbus/evm/interpreter_dispatch.nim b/nimbus/evm/interpreter_dispatch.nim index 91273afad..4d1b3a80c 100644 --- a/nimbus/evm/interpreter_dispatch.nim +++ b/nimbus/evm/interpreter_dispatch.nim @@ -38,10 +38,8 @@ when optimizationCondition: # this is a top level pragma since nim 1.6.16 {.optimization: speed.} -proc selectVM(c: Computation, fork: EVMFork, shouldPrepareTracer: bool): EvmResultVoid = +proc selectVM(c: VmCpt, fork: EVMFork, shouldPrepareTracer: bool): EvmResultVoid = ## Op code execution handler main loop. - var desc: VmCtx - desc.cpt = c # It's important not to re-prepare the tracer after # an async operation, only after a call/create. @@ -99,12 +97,12 @@ proc selectVM(c: Computation, fork: EVMFork, shouldPrepareTracer: bool): EvmResu else: {.warning: "*** Unsupported OS => no handler switch optimisation".} - genOptimisedDispatcher(fork, c.instr, desc) + genOptimisedDispatcher(fork, c.instr, c) else: {.warning: "*** low memory compiler mode => program will be slow".} - genLowMemDispatcher(fork, c.instr, desc) + genLowMemDispatcher(fork, c.instr, c) ok()