Skip to content

Commit

Permalink
0.1.29 w/ IR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Dec 14, 2023
1 parent 11b7939 commit 72a60c6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dasy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .parser import parse
from .parser.parse import parse_src, parse_node

__version__ = "0.1.28"
__version__ = "0.1.29"
33 changes: 28 additions & 5 deletions dasy/builtin/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
from vyper.ast import Call, Expr
from vyper.ir.s_expressions import parse_s_exp
from vyper.codegen.ir_node import IRnode
from vyper.builtins.functions import STMT_DISPATCH_TABLE, BuiltinFunction
from vyper.builtins.functions import (
STMT_DISPATCH_TABLE,
DISPATCH_TABLE,
BuiltinFunction,
)
from vyper.compiler import phases

from dasy import parser
from dasy.parser.utils import get_ir_type

from hy import repr


def parse_venom(expr):
ir = IRnode.from_list((parse_s_exp(repr(expr[1])[1:]))[0])
def parse_ir(expr):
# check for optional return type annotation as second element
ret_type = None
ir_expr = None
if len(expr) == 3:
ret_type = get_ir_type(expr[1].name)
ir_expr = expr[2]
elif len(expr) == 2:
ir_expr = expr[1]
ir = IRnode.from_list((parse_s_exp(repr(ir_expr)[1:]))[0], typ=ret_type)

# generate some vyper code to patch in.
IDENTIFIER = f"__DASY_VENOM_BUILTIN_{parser.next_nodeid()}__"
Expand All @@ -21,13 +34,23 @@ def parse_venom(expr):
class generated_builtin(BuiltinFunction):
_id = IDENTIFIER
_inputs = ()
_return_type = None
_return_type = ret_type

def fetch_call_return(self, node):
return self._return_type

def infer_arg_types(self, node):
return []

def build_IR(self, expr, context):
return ir

STMT_DISPATCH_TABLE[IDENTIFIER] = generated_builtin()
if ret_type is not None:
DISPATCH_TABLE[IDENTIFIER] = generated_builtin()
gend_ast = phases.generate_ast(insert_code, 0, "")
return gend_ast[1].body[0].value

STMT_DISPATCH_TABLE[IDENTIFIER] = generated_builtin()
return phases.generate_ast(insert_code, 0, "")[1].body[0]


Expand Down
22 changes: 21 additions & 1 deletion dasy/parser/utils.hy
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
(import vyper.ast.nodes *
hy.models [Symbol Sequence]
hyrule.iterables [flatten])
hyrule.iterables [flatten]
vyper.semantics.types.primitives [SINT UINT BytesM_T])

(require
hyrule.control [branch]
hyrule.argmove [->])

(defn get-ir-type [name]
;; check if starts with "uint" or "int"
;; if so, return the corresponding type
;; otherwise, return None
(let [name-str (str name)
[type-constructor size-index] (branch (name-str.startswith it)
"uint" [UINT 4]
"int" [SINT 3]
"bytes" [BytesM_T 5]
)]
(-> name-str
(cut size-index None)
int
type-constructor)))

(require
hyrule [assoc]
Expand Down
17 changes: 4 additions & 13 deletions examples/venom.dasy
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
(defn retOne [] :uint256 :external
(defvar x :uint256 0)
(venom (mstore 64 1))
x)
(ir :uint256 (seq 1)))


;; this works as-is but unecessary variable costs gas
(defn addTwoNums [:uint256 x y] :uint256 :external
(defvar z :uint256 0) ;; first variable is at offset 64
(venom (mstore 64 (add (calldataload 4) (calldataload 36))))
z)

;; ;; this might be ideal, removing implicit returns around venom blocks
;; (defn addTwoNums2 [:uint256 x y] :uint256 :external
;; (venom (seq
;; (mstore 64 (add (calldataload 4) (calldataload 36))))))
(ir :uint256
(add (calldataload 4)
(calldataload 36))))
5 changes: 1 addition & 4 deletions examples/venom_comp.vy
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
@external
def retOne() -> uint256:
x: uint256 = 0
x = 1
return x
return 1


# 71 gas
@external
def addTwoNums(a: uint256, b: uint256) -> uint256:
return unsafe_add(a, b)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dasy"
version = "0.1.28"
version = "0.1.29"
description = "an evm lisp"
authors = ["z80 <[email protected]>"]

Expand Down
11 changes: 10 additions & 1 deletion tests/parser/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from dasy.parser.utils import process_body, add_src_map, set_parent_children, build_node, next_node_id_maker, pairwise, filename_to_contract_name, has_return
from dasy.parser.utils import (
process_body,
add_src_map,
set_parent_children,
build_node,
next_node_id_maker,
pairwise,
filename_to_contract_name,
has_return,
)
from vyper.ast.nodes import Expr
import dasy

Expand Down
4 changes: 2 additions & 2 deletions tests/test_dasy.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ def testInterface():


def test_reentrancy():
c = compile("examples/nonreentrant.dasy") # noqa: F841
c = compile("examples/nonreentrant.dasy") # noqa: F841


def test_auction():
a = boa.env.generate_address()
c = compile("examples/simple_auction.dasy", a, 100, 10000000) # noqa: F841
c = compile("examples/simple_auction.dasy", a, 100, 10000000) # noqa: F841


def test_token():
Expand Down

0 comments on commit 72a60c6

Please sign in to comment.