evm: generate more specialized functions (#2390)

Nicer name in profiler and avoids a few range checks
This commit is contained in:
Jacek Sieka 2024-06-19 03:57:29 +02:00 committed by GitHub
parent 86877004e6
commit 1a96b4a97c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 9 additions and 9 deletions

View File

@ -60,7 +60,7 @@ template read*(c: CodeStream, size: int): openArray[byte] =
c.pc = c.bytes.len c.pc = c.bytes.len
c.bytes.toOpenArray(0, -1) 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 ## Reads `n` bytes from the code stream and pads
## the remaining bytes with zeros. ## the remaining bytes with zeros.
let result_bytes = cast[ptr array[32, byte]](addr result) let result_bytes = cast[ptr array[32, byte]](addr result)

View File

@ -19,7 +19,7 @@ import
type type
OphNumToTextFn* = proc(n: int): string OphNumToTextFn* = proc(n: int): string
OpHanldlerImplFn* = proc(k: var VmCtx; n: int): EvmResultVoid # OpHanldlerImplFn* = proc(k: var VmCtx; n: static int): EvmResultVoid
const const
recForkSet = "VmOpAllForks" recForkSet = "VmOpAllForks"
@ -45,7 +45,7 @@ proc asText(id, name: string): NimNode {.compileTime.} =
macro genOphHandlers*(runHandler: static[OphNumToTextFn]; macro genOphHandlers*(runHandler: static[OphNumToTextFn];
itemInfo: static[OphNumToTextFn]; itemInfo: static[OphNumToTextFn];
inxList: static[openArray[int]]; inxList: static[openArray[int]];
body: static[OpHanldlerImplFn]): untyped = body: untyped): untyped =
## Generate the equivalent of ## Generate the equivalent of
## :: ## ::
## const <runHandler>: VmOpFn = proc (k: var VmCtx) = ## const <runHandler>: VmOpFn = proc (k: var VmCtx) =
@ -63,7 +63,7 @@ macro genOphHandlers*(runHandler: static[OphNumToTextFn];
# => push##Op: VmOpFn = proc (k: var VmCtx) = ... # => push##Op: VmOpFn = proc (k: var VmCtx) = ...
result.add quote do: result.add quote do:
const `fnName`: VmOpFn = proc(k: var VmCtx): EvmResultVoid = proc `fnName`(k: var VmCtx): EvmResultVoid =
`comment` `comment`
`body`(k,`n`) `body`(k,`n`)
# echo ">>>", result.repr # echo ">>>", result.repr

View File

@ -49,8 +49,8 @@ proc fnInfo(n: int): string {.compileTime.} =
"Append log record with " & $n & " " & blurb "Append log record with " & $n & " " & blurb
proc logImpl(c: Computation, opcode: Op, topicCount: int): EvmResultVoid = proc logImpl(c: Computation, opcode: Op, topicCount: static int): EvmResultVoid =
doAssert(topicCount in 0 .. 4) static: doAssert(topicCount in 0 .. 4)
? checkInStaticContext(c) ? checkInStaticContext(c)
let (memStartPosition, size) = ? c.stack.popInt(2) let (memStartPosition, size) = ? c.stack.popInt(2)
let (memPos, len) = (memStartPosition.cleanMemRef, size.cleanMemRef) let (memPos, len) = (memStartPosition.cleanMemRef, size.cleanMemRef)
@ -96,7 +96,7 @@ const
# Private, op handlers implementation # 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) logImpl(k.cpt, logOpArg[n], n)
genOphHandlers fnName, fnInfo, inxRange, wrapperFn genOphHandlers fnName, fnInfo, inxRange, wrapperFn

View File

@ -40,7 +40,7 @@ proc fnInfo(n: int): string {.compileTime.} =
"Push " & blurb & " on the stack" "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) k.cpt.stack.push k.cpt.code.readVmWord(n)
const const

View File

@ -41,7 +41,7 @@ proc fnInfo(n: int): string {.compileTime.} =
"Exchange first and " & blurb & " stack items" "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) k.cpt.stack.swap(n)
const const