extracted macros from oph_helpers.nim => oph_gen_handlers.nim
why: imports mostly need to import only one of either
This commit is contained in:
parent
45558282f7
commit
73900270db
|
@ -16,7 +16,7 @@ import
|
||||||
../../stack,
|
../../stack,
|
||||||
../op_codes,
|
../op_codes,
|
||||||
./oph_defs,
|
./oph_defs,
|
||||||
./oph_helpers,
|
./oph_gen_handlers,
|
||||||
sequtils,
|
sequtils,
|
||||||
stint,
|
stint,
|
||||||
strformat
|
strformat
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
# Nimbus
|
||||||
|
# Copyright (c) 2018 Status Research & Development GmbH
|
||||||
|
# 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
|
||||||
|
./oph_defs,
|
||||||
|
eth/common/eth_types,
|
||||||
|
macros,
|
||||||
|
stint,
|
||||||
|
strutils
|
||||||
|
|
||||||
|
type
|
||||||
|
OphNumToTextFn* = proc(n: int): string
|
||||||
|
OpHanldlerImplFn* = proc(k: var Vm2Ctx; n: int)
|
||||||
|
|
||||||
|
const
|
||||||
|
recForkSet = "Vm2OpAllForks"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# 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]];
|
||||||
|
body: static[OpHanldlerImplFn]): untyped =
|
||||||
|
## Generate the equivalent of
|
||||||
|
## ::
|
||||||
|
## const <runHandler>: Vm2OpFn = proc (k: var Vm2Ctx) =
|
||||||
|
## ## <itemInfo(n)>,
|
||||||
|
## <body(k,n)>
|
||||||
|
##
|
||||||
|
## for all `n` in `inxList`
|
||||||
|
##
|
||||||
|
result = newStmtList()
|
||||||
|
|
||||||
|
for n in inxList:
|
||||||
|
let
|
||||||
|
fnName = ident(n.runHandler)
|
||||||
|
comment = newCommentStmtNode(n.itemInfo)
|
||||||
|
|
||||||
|
# => push##Op: Vm2OpFn = proc (k: var Vm2Ctx) = ...
|
||||||
|
result.add quote do:
|
||||||
|
const `fnName`: Vm2OpFn = proc(k: var Vm2Ctx) =
|
||||||
|
`comment`
|
||||||
|
`body`(k,`n`)
|
||||||
|
# echo ">>>", result.repr
|
||||||
|
|
||||||
|
|
||||||
|
macro genOphList*(runHandler: static[OphNumToTextFn];
|
||||||
|
handlerInfo: static[OphNumToTextFn];
|
||||||
|
inxList: static[openArray[int]];
|
||||||
|
varName: static[string];
|
||||||
|
opCode: static[OphNumToTextFn]): untyped =
|
||||||
|
## Generate
|
||||||
|
## ::
|
||||||
|
## const <varName>*: seq[Vm2OpExec] = @[ <records> ]
|
||||||
|
##
|
||||||
|
## where <records> is a sequence of <record(n)> items like
|
||||||
|
## ::
|
||||||
|
## (opCode: <opCode(n)>,
|
||||||
|
## forks: Vm2OpAllForks,
|
||||||
|
## info: <handlerInfo(n)>,
|
||||||
|
## exec: (prep: vm2OpIgnore,
|
||||||
|
## run: <runHandler(n)>,
|
||||||
|
## post: vm2OpIgnore))
|
||||||
|
##
|
||||||
|
## 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"),
|
||||||
|
nnkPar.newTree(
|
||||||
|
"prep".asIdent("vm2OpIgnore"),
|
||||||
|
"run".asIdent(n.runHandler),
|
||||||
|
"post".asIdent("vm2OpIgnore"))))
|
||||||
|
|
||||||
|
# => const <varName>*: seq[Vm2OpExec] = @[ <records> ]
|
||||||
|
result = nnkStmtList.newTree(
|
||||||
|
nnkConstSection.newTree(
|
||||||
|
nnkConstDef.newTree(
|
||||||
|
nnkPostfix.newTree(
|
||||||
|
newIdentNode("*"),
|
||||||
|
newIdentNode(varName)),
|
||||||
|
nnkBracketExpr.newTree(
|
||||||
|
newIdentNode("seq"),
|
||||||
|
newIdentNode("Vm2OpExec")),
|
||||||
|
nnkPrefix.newTree(
|
||||||
|
newIdentNode("@"), records))))
|
||||||
|
# echo ">>> ", result.repr
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# End
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
# at your option. This file may not be copied, modified, or distributed except
|
# at your option. This file may not be copied, modified, or distributed except
|
||||||
# according to those terms.
|
# according to those terms.
|
||||||
|
|
||||||
## EVM Opcode Handlers: Helper Functions & Macros
|
## EVM Opcode Handlers: Common Helper Functions
|
||||||
## ==============================================
|
## ============================================
|
||||||
##
|
##
|
||||||
|
|
||||||
# Including types.nim and others unless included (not imported) into
|
# Including types.nim and others unless included (not imported) into
|
||||||
|
@ -22,36 +22,13 @@ when not declared(Computation):
|
||||||
../../types,
|
../../types,
|
||||||
../gas_costs,
|
../gas_costs,
|
||||||
../gas_meter,
|
../gas_meter,
|
||||||
./oph_defs,
|
|
||||||
eth/common
|
eth/common
|
||||||
|
|
||||||
import
|
import
|
||||||
../../../errors,
|
../../../errors,
|
||||||
eth/common/eth_types,
|
eth/common/eth_types,
|
||||||
macros,
|
macros,
|
||||||
stint,
|
stint
|
||||||
strutils
|
|
||||||
|
|
||||||
type
|
|
||||||
OphNumToTextFn* = proc(n: int): string
|
|
||||||
OpHanldlerImplFn* = proc(k: var Vm2Ctx; n: int)
|
|
||||||
|
|
||||||
const
|
|
||||||
recForkSet = "Vm2OpAllForks"
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
# 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
|
# Public
|
||||||
|
@ -80,83 +57,6 @@ template checkInStaticContext*(c: Computation) =
|
||||||
StaticContextError,
|
StaticContextError,
|
||||||
"Cannot modify state while inside of STATICCALL context")
|
"Cannot modify state while inside of STATICCALL context")
|
||||||
|
|
||||||
|
|
||||||
macro genOphHandlers*(runHandler: static[OphNumToTextFn];
|
|
||||||
itemInfo: static[OphNumToTextFn];
|
|
||||||
inxList: static[openArray[int]];
|
|
||||||
body: static[OpHanldlerImplFn]): untyped =
|
|
||||||
## Generate the equivalent of
|
|
||||||
## ::
|
|
||||||
## const <runHandler>: Vm2OpFn = proc (k: var Vm2Ctx) =
|
|
||||||
## ## <itemInfo(n)>,
|
|
||||||
## <body(k,n)>
|
|
||||||
##
|
|
||||||
## for all `n` in `inxList`
|
|
||||||
##
|
|
||||||
result = newStmtList()
|
|
||||||
|
|
||||||
for n in inxList:
|
|
||||||
let
|
|
||||||
fnName = ident(n.runHandler)
|
|
||||||
comment = newCommentStmtNode(n.itemInfo)
|
|
||||||
|
|
||||||
# => push##Op: Vm2OpFn = proc (k: var Vm2Ctx) = ...
|
|
||||||
result.add quote do:
|
|
||||||
const `fnName`: Vm2OpFn = proc(k: var Vm2Ctx) =
|
|
||||||
`comment`
|
|
||||||
`body`(k,`n`)
|
|
||||||
# echo ">>>", result.repr
|
|
||||||
|
|
||||||
|
|
||||||
macro genOphList*(runHandler: static[OphNumToTextFn];
|
|
||||||
handlerInfo: static[OphNumToTextFn];
|
|
||||||
inxList: static[openArray[int]];
|
|
||||||
varName: static[string];
|
|
||||||
opCode: static[OphNumToTextFn]): untyped =
|
|
||||||
## Generate
|
|
||||||
## ::
|
|
||||||
## const <varName>*: seq[Vm2OpExec] = @[ <records> ]
|
|
||||||
##
|
|
||||||
## where <records> is a sequence of <record(n)> items like
|
|
||||||
## ::
|
|
||||||
## (opCode: <opCode(n)>,
|
|
||||||
## forks: Vm2OpAllForks,
|
|
||||||
## info: <handlerInfo(n)>,
|
|
||||||
## exec: (prep: vm2OpIgnore,
|
|
||||||
## run: <runHandler(n)>,
|
|
||||||
## post: vm2OpIgnore))
|
|
||||||
##
|
|
||||||
## 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"),
|
|
||||||
nnkPar.newTree(
|
|
||||||
"prep".asIdent("vm2OpIgnore"),
|
|
||||||
"run".asIdent(n.runHandler),
|
|
||||||
"post".asIdent("vm2OpIgnore"))))
|
|
||||||
|
|
||||||
# => const <varName>*: seq[Vm2OpExec] = @[ <records> ]
|
|
||||||
result = nnkStmtList.newTree(
|
|
||||||
nnkConstSection.newTree(
|
|
||||||
nnkConstDef.newTree(
|
|
||||||
nnkPostfix.newTree(
|
|
||||||
newIdentNode("*"),
|
|
||||||
newIdentNode(varName)),
|
|
||||||
nnkBracketExpr.newTree(
|
|
||||||
newIdentNode("seq"),
|
|
||||||
newIdentNode("Vm2OpExec")),
|
|
||||||
nnkPrefix.newTree(
|
|
||||||
newIdentNode("@"), records))))
|
|
||||||
# echo ">>> ", result.repr
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# End
|
# End
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
|
@ -23,8 +23,7 @@ import
|
||||||
eth/common/eth_types,
|
eth/common/eth_types,
|
||||||
macros,
|
macros,
|
||||||
sets,
|
sets,
|
||||||
stint,
|
stint
|
||||||
strutils
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Kludge BEGIN
|
# Kludge BEGIN
|
||||||
|
|
|
@ -24,6 +24,7 @@ import
|
||||||
../op_codes,
|
../op_codes,
|
||||||
../utils/utils_numeric,
|
../utils/utils_numeric,
|
||||||
./oph_defs,
|
./oph_defs,
|
||||||
|
./oph_gen_handlers,
|
||||||
./oph_helpers,
|
./oph_helpers,
|
||||||
eth/common,
|
eth/common,
|
||||||
eth/common,
|
eth/common,
|
||||||
|
|
|
@ -17,7 +17,7 @@ import
|
||||||
../../stack,
|
../../stack,
|
||||||
../op_codes,
|
../op_codes,
|
||||||
./oph_defs,
|
./oph_defs,
|
||||||
./oph_helpers,
|
./oph_gen_handlers,
|
||||||
sequtils,
|
sequtils,
|
||||||
stint,
|
stint,
|
||||||
strformat
|
strformat
|
||||||
|
|
|
@ -16,7 +16,7 @@ import
|
||||||
../../stack,
|
../../stack,
|
||||||
../op_codes,
|
../op_codes,
|
||||||
./oph_defs,
|
./oph_defs,
|
||||||
./oph_helpers,
|
./oph_gen_handlers,
|
||||||
sequtils,
|
sequtils,
|
||||||
strformat
|
strformat
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue