From 1a96b4a97c208e1a72aa475321d1335dd45a214f Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Wed, 19 Jun 2024 03:57:29 +0200 Subject: [PATCH] evm: generate more specialized functions (#2390) Nicer name in profiler and avoids a few range checks --- nimbus/evm/code_stream.nim | 2 +- nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim | 6 +++--- nimbus/evm/interpreter/op_handlers/oph_log.nim | 6 +++--- nimbus/evm/interpreter/op_handlers/oph_push.nim | 2 +- nimbus/evm/interpreter/op_handlers/oph_swap.nim | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nimbus/evm/code_stream.nim b/nimbus/evm/code_stream.nim index b50ca55c5..ccc9d46b7 100644 --- a/nimbus/evm/code_stream.nim +++ b/nimbus/evm/code_stream.nim @@ -60,7 +60,7 @@ template read*(c: CodeStream, size: int): openArray[byte] = c.pc = c.bytes.len c.bytes.toOpenArray(0, -1) -proc readVmWord*(c: var CodeStream, n: int): UInt256 = +proc readVmWord*(c: var CodeStream, n: static int): UInt256 = ## Reads `n` bytes from the code stream and pads ## the remaining bytes with zeros. let result_bytes = cast[ptr array[32, byte]](addr result) diff --git a/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim b/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim index 3355148cc..bff13538b 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_gen_handlers.nim @@ -19,7 +19,7 @@ import type OphNumToTextFn* = proc(n: int): string - OpHanldlerImplFn* = proc(k: var VmCtx; n: int): EvmResultVoid + # OpHanldlerImplFn* = proc(k: var VmCtx; n: static int): EvmResultVoid const recForkSet = "VmOpAllForks" @@ -45,7 +45,7 @@ proc asText(id, name: string): NimNode {.compileTime.} = macro genOphHandlers*(runHandler: static[OphNumToTextFn]; itemInfo: static[OphNumToTextFn]; inxList: static[openArray[int]]; - body: static[OpHanldlerImplFn]): untyped = + body: untyped): untyped = ## Generate the equivalent of ## :: ## const : VmOpFn = proc (k: var VmCtx) = @@ -63,7 +63,7 @@ macro genOphHandlers*(runHandler: static[OphNumToTextFn]; # => push##Op: VmOpFn = proc (k: var VmCtx) = ... result.add quote do: - const `fnName`: VmOpFn = proc(k: var VmCtx): EvmResultVoid = + proc `fnName`(k: var VmCtx): EvmResultVoid = `comment` `body`(k,`n`) # echo ">>>", result.repr diff --git a/nimbus/evm/interpreter/op_handlers/oph_log.nim b/nimbus/evm/interpreter/op_handlers/oph_log.nim index 49f744a75..3929d770a 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_log.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_log.nim @@ -49,8 +49,8 @@ proc fnInfo(n: int): string {.compileTime.} = "Append log record with " & $n & " " & blurb -proc logImpl(c: Computation, opcode: Op, topicCount: int): EvmResultVoid = - doAssert(topicCount in 0 .. 4) +proc logImpl(c: Computation, opcode: Op, topicCount: static int): EvmResultVoid = + static: doAssert(topicCount in 0 .. 4) ? checkInStaticContext(c) let (memStartPosition, size) = ? c.stack.popInt(2) let (memPos, len) = (memStartPosition.cleanMemRef, size.cleanMemRef) @@ -96,7 +96,7 @@ const # Private, op handlers implementation # ------------------------------------------------------------------------------ -proc wrapperFn(k: var VmCtx; n: int): EvmResultVoid = +proc wrapperFn(k: var VmCtx; n: static int): EvmResultVoid = logImpl(k.cpt, logOpArg[n], n) genOphHandlers fnName, fnInfo, inxRange, wrapperFn diff --git a/nimbus/evm/interpreter/op_handlers/oph_push.nim b/nimbus/evm/interpreter/op_handlers/oph_push.nim index da025418a..1416191b5 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_push.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_push.nim @@ -40,7 +40,7 @@ proc fnInfo(n: int): string {.compileTime.} = "Push " & blurb & " on the stack" -proc pushImpl(k: var VmCtx; n: int): EvmResultVoid = +proc pushImpl(k: var VmCtx; n: static int): EvmResultVoid = k.cpt.stack.push k.cpt.code.readVmWord(n) const diff --git a/nimbus/evm/interpreter/op_handlers/oph_swap.nim b/nimbus/evm/interpreter/op_handlers/oph_swap.nim index 1d58a41c8..5c81f9d07 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_swap.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_swap.nim @@ -41,7 +41,7 @@ proc fnInfo(n: int): string {.compileTime.} = "Exchange first and " & blurb & " stack items" -func swapImpl(k: var VmCtx; n: int): EvmResultVoid = +func swapImpl(k: var VmCtx; n: static int): EvmResultVoid = k.cpt.stack.swap(n) const