2018-01-15 18:42:40 +00:00
|
|
|
import
|
2018-01-16 17:05:20 +00:00
|
|
|
strformat, macros,
|
2018-01-15 18:42:40 +00:00
|
|
|
value, ../errors, ../validation, ../utils_numeric, ../constants, ../logging
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
Stack* = ref object of RootObj
|
|
|
|
## VM Stack
|
|
|
|
logger*: Logger
|
|
|
|
values*: seq[Value]
|
|
|
|
|
|
|
|
template ensureStackLimit: untyped =
|
|
|
|
if len(stack.values) > 1023:
|
|
|
|
raise newException(FullStack, "Stack limit reached")
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc len*(stack: Stack): int =
|
2018-01-15 18:42:40 +00:00
|
|
|
len(stack.values)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc push*(stack: var Stack; value: Value) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Push an item onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
|
|
|
stack.values.add(value)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc push*(stack: var Stack; value: int) =
|
|
|
|
## Push an integer onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
|
|
|
stack.values.add(Value(kind: VInt, i: value.Int256))
|
|
|
|
|
|
|
|
proc push*(stack: var Stack; value: Int256) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Push an integer onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
|
|
|
stack.values.add(Value(kind: VInt, i: value))
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc push*(stack: var Stack; value: cstring) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Push a binary onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
|
|
|
stack.values.add(Value(kind: VBinary, b: value))
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc internalPop(stack: var Stack; numItems: int): seq[Value] =
|
2018-01-15 18:42:40 +00:00
|
|
|
if len(stack) < numItems:
|
|
|
|
result = @[]
|
|
|
|
else:
|
|
|
|
result = stack.values[^numItems .. ^1]
|
|
|
|
stack.values = stack.values[0 ..< ^numItems]
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
template toType(i: Int256, _: typedesc[Int256]): Int256 =
|
2018-01-15 18:42:40 +00:00
|
|
|
i
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
template toType(i: Int256, _: typedesc[cstring]): cstring =
|
2018-01-15 18:42:40 +00:00
|
|
|
intToBigEndian(i)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
template toType(b: cstring, _: typedesc[Int256]): Int256 =
|
2018-01-15 18:42:40 +00:00
|
|
|
bigEndianToInt(b)
|
|
|
|
|
|
|
|
template toType(b: cstring, _: typedesc[cstring]): cstring =
|
|
|
|
b
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc internalPop(stack: var Stack; numItems: int, T: typedesc): seq[T] =
|
2018-01-15 18:42:40 +00:00
|
|
|
result = @[]
|
|
|
|
if len(stack) < numItems:
|
|
|
|
return
|
|
|
|
|
|
|
|
for z in 0 ..< numItems:
|
|
|
|
var value = stack.values.pop()
|
|
|
|
case value.kind:
|
|
|
|
of VInt:
|
|
|
|
result.add(toType(value.i, T))
|
|
|
|
of VBinary:
|
|
|
|
result.add(toType(value.b, T))
|
|
|
|
|
|
|
|
template ensurePop(elements: untyped, a: untyped): untyped =
|
|
|
|
if len(`elements`) < `a`:
|
|
|
|
raise newException(InsufficientStack, "No stack items")
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc pop*(stack: var Stack): Value =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Pop an item off the stack
|
|
|
|
var elements = stack.internalPop(1)
|
|
|
|
ensurePop(elements, 1)
|
|
|
|
result = elements[0]
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc pop*(stack: var Stack; numItems: int): seq[Value] =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Pop many items off the stack
|
|
|
|
result = stack.internalPop(numItems)
|
|
|
|
ensurePop(result, numItems)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
# proc popInt*(stack: var Stack): Int256 =
|
|
|
|
# var elements = stack.internalPop(1, Int256)
|
|
|
|
# ensurePop(elements, 1)
|
|
|
|
# result = elements[0]
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
macro internalPopTuple(numItems: static[int]): untyped =
|
|
|
|
var name = ident(%"internalPopTuple{numItems}")
|
|
|
|
var typ = nnkPar.newTree()
|
|
|
|
var t = ident("T")
|
|
|
|
for z in 0 ..< numItems:
|
|
|
|
typ.add(t)
|
|
|
|
result = quote:
|
|
|
|
proc `name`(stack: var Stack, `t`: typedesc): `typ` =
|
|
|
|
for z in 0 ..< `numItems`:
|
|
|
|
var value = stack.values.pop()
|
|
|
|
case value.kind:
|
|
|
|
of VInt:
|
|
|
|
result[z] = toType(value.i, `t`)
|
|
|
|
of VBinary:
|
|
|
|
result[z] = toType(value.b, `t`)
|
|
|
|
|
|
|
|
# define pop<T> for tuples
|
|
|
|
internalPopTuple(2)
|
|
|
|
internalPopTuple(3)
|
|
|
|
internalPopTuple(4)
|
|
|
|
internalPopTuple(5)
|
|
|
|
internalPopTuple(6)
|
|
|
|
internalPopTuple(7)
|
|
|
|
|
|
|
|
macro popInt*(stack: typed; numItems: static[int]): untyped =
|
|
|
|
var resultNode = ident("result")
|
|
|
|
if numItems >= 8:
|
|
|
|
result = quote:
|
|
|
|
`resultNode` = `stack`.internalPop(`numItems`, Int256)
|
|
|
|
else:
|
|
|
|
var name = ident(%"internalPopTuple{numItems}")
|
|
|
|
result = quote:
|
|
|
|
`resultNode` = `name`(`stack`, Int256)
|
|
|
|
|
|
|
|
# proc popInt*(stack: var Stack, numItems: int): seq[Int256] =
|
|
|
|
# result = stack.internalPop(numItems, Int256)
|
|
|
|
# ensurePop(result, numItems)
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc popBinary*(stack: var Stack): cstring =
|
2018-01-15 18:42:40 +00:00
|
|
|
var elements = stack.internalPop(1, cstring)
|
|
|
|
ensurePop(elements, 1)
|
|
|
|
result = elements[0]
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc popBinary*(stack: var Stack; numItems: int): seq[cstring] =
|
2018-01-15 18:42:40 +00:00
|
|
|
result = stack.internalPop(numItems, cstring)
|
|
|
|
ensurePop(result, numItems)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc newStack*(): Stack =
|
|
|
|
new(result)
|
|
|
|
result.logger = logging.getLogger("evm.vm.stack.Stack")
|
2018-01-15 18:42:40 +00:00
|
|
|
result.values = @[]
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc swap*(stack: var Stack; position: int) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Perform a SWAP operation on the stack
|
|
|
|
var idx = position + 1
|
|
|
|
if idx < len(stack) + 1:
|
|
|
|
(stack.values[^1], stack.values[^idx]) = (stack.values[^idx], stack.values[^1])
|
|
|
|
else:
|
|
|
|
raise newException(InsufficientStack,
|
|
|
|
%"Insufficient stack items for SWAP{position}")
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc dup*(stack: var Stack; position: int) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Perform a DUP operation on the stack
|
|
|
|
if position < len(stack) + 1:
|
|
|
|
stack.push(stack.values[^position])
|
|
|
|
else:
|
|
|
|
raise newException(InsufficientStack,
|
|
|
|
%"Insufficient stack items for DUP{position}")
|
|
|
|
|