2021-04-23 10:47:58 +00:00
|
|
|
# Nimbus
|
2024-06-07 08:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-23 10:47:58 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## EVM Opcode Handlers: Macros For Generating OP Handlers
|
|
|
|
## ======================================================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2023-01-31 12:38:08 +00:00
|
|
|
std/[strutils, macros],
|
2024-06-07 08:24:32 +00:00
|
|
|
./oph_defs,
|
|
|
|
../../evm_errors
|
2021-04-23 10:47:58 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
OphNumToTextFn* = proc(n: int): string
|
|
|
|
|
|
|
|
const
|
2024-06-15 16:18:53 +00:00
|
|
|
recForkSet = "VmOpAllForks"
|
2021-04-23 10:47:58 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc asIdent(id, name: string): NimNode {.compileTime.} =
|
|
|
|
result = nnkExprColonExpr.newTree(
|
|
|
|
newIdentNode(id),
|
|
|
|
newIdentNode(name))
|
|
|
|
|
|
|
|
proc asText(id, name: string): NimNode {.compileTime.} =
|
|
|
|
result = nnkExprColonExpr.newTree(
|
|
|
|
newIdentNode(id),
|
|
|
|
newLit(name))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
macro genOphHandlers*(runHandler: static[OphNumToTextFn];
|
|
|
|
itemInfo: static[OphNumToTextFn];
|
|
|
|
inxList: static[openArray[int]];
|
2024-06-19 01:57:29 +00:00
|
|
|
body: untyped): untyped =
|
2021-04-23 10:47:58 +00:00
|
|
|
## Generate the equivalent of
|
|
|
|
## ::
|
2024-07-19 01:44:01 +00:00
|
|
|
## const <runHandler>: VmOpFn = proc(cpt: VmCpt) =
|
2021-04-23 10:47:58 +00:00
|
|
|
## ## <itemInfo(n)>,
|
2024-07-19 01:44:01 +00:00
|
|
|
## <body(cpt,n)>
|
2021-04-23 10:47:58 +00:00
|
|
|
##
|
|
|
|
## for all `n` in `inxList`
|
|
|
|
##
|
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
for n in inxList:
|
|
|
|
let
|
|
|
|
fnName = ident(n.runHandler)
|
|
|
|
comment = newCommentStmtNode(n.itemInfo)
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
# => push##Op: VmOpFn = proc(cpt: VmCpt) = ...
|
2021-04-23 10:47:58 +00:00
|
|
|
result.add quote do:
|
2024-07-19 01:44:01 +00:00
|
|
|
proc `fnName`(cpt: VmCpt): EvmResultVoid =
|
2021-04-23 10:47:58 +00:00
|
|
|
`comment`
|
2024-07-19 01:44:01 +00:00
|
|
|
`body`(cpt,`n`)
|
2021-04-23 10:47:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
macro genOphList*(runHandler: static[OphNumToTextFn];
|
|
|
|
handlerInfo: static[OphNumToTextFn];
|
|
|
|
inxList: static[openArray[int]];
|
|
|
|
varName: static[string];
|
|
|
|
opCode: static[OphNumToTextFn]): untyped =
|
|
|
|
## Generate
|
|
|
|
## ::
|
2024-06-15 16:18:53 +00:00
|
|
|
## const <varName>*: seq[VmOpExec] = @[ <records> ]
|
2021-04-23 10:47:58 +00:00
|
|
|
##
|
|
|
|
## where <records> is a sequence of <record(n)> items like
|
|
|
|
## ::
|
|
|
|
## (opCode: <opCode(n)>,
|
2024-06-15 16:18:53 +00:00
|
|
|
## forks: VmOpAllForks,
|
2021-04-23 10:47:58 +00:00
|
|
|
## info: <handlerInfo(n)>,
|
2024-07-19 01:44:01 +00:00
|
|
|
## exec: <runHandler(n)>)
|
2021-04-23 10:47:58 +00:00
|
|
|
##
|
|
|
|
## for all `n` in `inxList`
|
|
|
|
##
|
|
|
|
var records = nnkBracket.newTree()
|
|
|
|
for n in inxList:
|
|
|
|
var handlerName = n.runHandler.multiReplace(("Op",""),("OP",""))
|
|
|
|
records.add nnkPar.newTree(
|
|
|
|
"opCode".asIdent(n.opCode),
|
|
|
|
"forks".asIdent(recForkSet),
|
|
|
|
"name".asText(handlerName),
|
|
|
|
"info".asText(n.handlerInfo),
|
|
|
|
nnkExprColonExpr.newTree(
|
|
|
|
newIdentNode("exec"),
|
2024-06-24 05:58:15 +00:00
|
|
|
newIdentNode(n.runHandler)))
|
2021-04-23 10:47:58 +00:00
|
|
|
|
2024-06-15 16:18:53 +00:00
|
|
|
# => const <varName>*: seq[VmOpExec] = @[ <records> ]
|
2021-04-23 10:47:58 +00:00
|
|
|
result = nnkStmtList.newTree(
|
|
|
|
nnkConstSection.newTree(
|
|
|
|
nnkConstDef.newTree(
|
|
|
|
nnkPostfix.newTree(
|
|
|
|
newIdentNode("*"),
|
|
|
|
newIdentNode(varName)),
|
|
|
|
nnkBracketExpr.newTree(
|
|
|
|
newIdentNode("seq"),
|
2024-06-15 16:18:53 +00:00
|
|
|
newIdentNode("VmOpExec")),
|
2021-04-23 10:47:58 +00:00
|
|
|
nnkPrefix.newTree(
|
|
|
|
newIdentNode("@"), records))))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|