From e2689792b0d641371fde616f8228222189a4aef6 Mon Sep 17 00:00:00 2001 From: Jamie Lokier Date: Tue, 1 Jun 2021 10:35:01 +0100 Subject: [PATCH] vm2: Remove `toSymbolName` unnecessary symbol-text-symbol conversion There's no need for macro `toSymbolName` to convert fork enum values to their presentation texts (logging etc) then re-parse them back to a fork enum value. `asFork` is already used in the same function and works without these steps, so use it consistently. Same applies to `op.toSymbolName` and `asOp`. This makes the code simpler, and removes a text pattern-matching requirement. The patch has been checked to confirm it doesn't change the compiled code. Motivation: The forks list will be removed from VM because it is used outside the VM as well. Doing so highlighted vm2's `toSymbolName`. It's not needed, and it's best if the VM doesn't constrain text strings used outside the VM Signed-off-by: Jamie Lokier --- nimbus/vm2/interpreter/forks_list.nim | 16 +--------------- nimbus/vm2/interpreter/op_codes.nim | 6 ------ nimbus/vm2/interpreter/op_dispatcher.nim | 14 ++++---------- 3 files changed, 5 insertions(+), 31 deletions(-) diff --git a/nimbus/vm2/interpreter/forks_list.nim b/nimbus/vm2/interpreter/forks_list.nim index 92c32daaf..d401d0f57 100644 --- a/nimbus/vm2/interpreter/forks_list.nim +++ b/nimbus/vm2/interpreter/forks_list.nim @@ -8,20 +8,11 @@ # at your option. This file may not be copied, modified, or distributed except # according to those terms. -## List of known Etheroum forks +## List of known Ethereum forks ## ============================ ## ## See `here <../../ex/vm/interpreter/forks_list.html>`_ for an ## overview. -## -## Assumptions on the naming of the fork list: -## * each symbol start with the prefix "Fk" -## * the first word of the prettified text representaion is the same -## text as the one following the "Fk" in the symbol name (irrespective -## of character case.) - -import - strutils type Fork* = enum @@ -35,9 +26,4 @@ type FkIstanbul = "istanbul" FkBerlin = "berlin" -proc toSymbolName*(fork: Fork): string = - ## Given a `fork` argument, print the symbol name so that it can be used - ## in a macro statement. - "Fk" & ($fork).split(' ')[0] - # End diff --git a/nimbus/vm2/interpreter/op_codes.nim b/nimbus/vm2/interpreter/op_codes.nim index 2f753267b..d73e0a572 100644 --- a/nimbus/vm2/interpreter/op_codes.nim +++ b/nimbus/vm2/interpreter/op_codes.nim @@ -208,12 +208,6 @@ type SelfDestruct = 0xff ## Halt execution and register account for later ## deletion. - -proc toSymbolName*(op: Op): string = - ## Given an `op` argument, print the symbol name so that it can be used - ## in a macro statement. - $op - # ------------------------------------------------------------------------------ # Verify that Op is contiguous and sym names follow some standards # ------------------------------------------------------------------------------ diff --git a/nimbus/vm2/interpreter/op_dispatcher.nim b/nimbus/vm2/interpreter/op_dispatcher.nim index 3d0585520..c3576215f 100644 --- a/nimbus/vm2/interpreter/op_dispatcher.nim +++ b/nimbus/vm2/interpreter/op_dispatcher.nim @@ -74,15 +74,13 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode = let branchOnOp = quote do: `opArg` result = nnkCaseStmt.newTree(branchOnOp) for op in Op: + let asOp = quote do: Op(`op`) # Inner case/switch => Fork let branchOnFork = quote do: `forkArg` var forkCaseSubExpr = nnkCaseStmt.newTree(branchOnFork) for fork in Fork: - - let - asFork = quote do: Fork(`fork`) - asOp = quote do: Op(`op`) + let asFork = quote do: Fork(`fork`) let branchStmt = block: if op == Stop: @@ -95,9 +93,7 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode = quote do: handleOtherDirective(`asFork`,`asOp`,`k`) - forkCaseSubExpr.add nnkOfBranch.newTree( - newIdentNode(fork.toSymbolName), - branchStmt) + forkCaseSubExpr.add nnkOfBranch.newTree(asFork, branchStmt) # Wrap innner case/switch into outer case/switch let branchStmt = block: @@ -115,9 +111,7 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode = quote do: `forkCaseSubExpr` - result.add nnkOfBranch.newTree( - newIdentNode(op.toSymbolName), - branchStmt) + result.add nnkOfBranch.newTree(asOp, branchStmt) when isChatty: echo ">>> ", result.repr