Skip to content

Commit 72a60c6

Browse files
committed
0.1.29 w/ IR changes
1 parent 11b7939 commit 72a60c6

File tree

8 files changed

+68
-28
lines changed

8 files changed

+68
-28
lines changed

dasy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from .parser import parse
77
from .parser.parse import parse_src, parse_node
88

9-
__version__ = "0.1.28"
9+
__version__ = "0.1.29"

dasy/builtin/functions.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
from vyper.ast import Call, Expr
33
from vyper.ir.s_expressions import parse_s_exp
44
from vyper.codegen.ir_node import IRnode
5-
from vyper.builtins.functions import STMT_DISPATCH_TABLE, BuiltinFunction
5+
from vyper.builtins.functions import (
6+
STMT_DISPATCH_TABLE,
7+
DISPATCH_TABLE,
8+
BuiltinFunction,
9+
)
610
from vyper.compiler import phases
711

812
from dasy import parser
13+
from dasy.parser.utils import get_ir_type
914

1015
from hy import repr
1116

1217

13-
def parse_venom(expr):
14-
ir = IRnode.from_list((parse_s_exp(repr(expr[1])[1:]))[0])
18+
def parse_ir(expr):
19+
# check for optional return type annotation as second element
20+
ret_type = None
21+
ir_expr = None
22+
if len(expr) == 3:
23+
ret_type = get_ir_type(expr[1].name)
24+
ir_expr = expr[2]
25+
elif len(expr) == 2:
26+
ir_expr = expr[1]
27+
ir = IRnode.from_list((parse_s_exp(repr(ir_expr)[1:]))[0], typ=ret_type)
1528

1629
# generate some vyper code to patch in.
1730
IDENTIFIER = f"__DASY_VENOM_BUILTIN_{parser.next_nodeid()}__"
@@ -21,13 +34,23 @@ def parse_venom(expr):
2134
class generated_builtin(BuiltinFunction):
2235
_id = IDENTIFIER
2336
_inputs = ()
24-
_return_type = None
37+
_return_type = ret_type
38+
39+
def fetch_call_return(self, node):
40+
return self._return_type
41+
42+
def infer_arg_types(self, node):
43+
return []
2544

2645
def build_IR(self, expr, context):
2746
return ir
2847

29-
STMT_DISPATCH_TABLE[IDENTIFIER] = generated_builtin()
48+
if ret_type is not None:
49+
DISPATCH_TABLE[IDENTIFIER] = generated_builtin()
50+
gend_ast = phases.generate_ast(insert_code, 0, "")
51+
return gend_ast[1].body[0].value
3052

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

3356

dasy/parser/utils.hy

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
(import vyper.ast.nodes *
22
hy.models [Symbol Sequence]
3-
hyrule.iterables [flatten])
3+
hyrule.iterables [flatten]
4+
vyper.semantics.types.primitives [SINT UINT BytesM_T])
5+
6+
(require
7+
hyrule.control [branch]
8+
hyrule.argmove [->])
9+
10+
(defn get-ir-type [name]
11+
;; check if starts with "uint" or "int"
12+
;; if so, return the corresponding type
13+
;; otherwise, return None
14+
(let [name-str (str name)
15+
[type-constructor size-index] (branch (name-str.startswith it)
16+
"uint" [UINT 4]
17+
"int" [SINT 3]
18+
"bytes" [BytesM_T 5]
19+
)]
20+
(-> name-str
21+
(cut size-index None)
22+
int
23+
type-constructor)))
424

525
(require
626
hyrule [assoc]

examples/venom.dasy

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
(defn retOne [] :uint256 :external
2-
(defvar x :uint256 0)
3-
(venom (mstore 64 1))
4-
x)
2+
(ir :uint256 (seq 1)))
53

6-
7-
;; this works as-is but unecessary variable costs gas
84
(defn addTwoNums [:uint256 x y] :uint256 :external
9-
(defvar z :uint256 0) ;; first variable is at offset 64
10-
(venom (mstore 64 (add (calldataload 4) (calldataload 36))))
11-
z)
12-
13-
;; ;; this might be ideal, removing implicit returns around venom blocks
14-
;; (defn addTwoNums2 [:uint256 x y] :uint256 :external
15-
;; (venom (seq
16-
;; (mstore 64 (add (calldataload 4) (calldataload 36))))))
5+
(ir :uint256
6+
(add (calldataload 4)
7+
(calldataload 36))))

examples/venom_comp.vy

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
@external
22
def retOne() -> uint256:
3-
x: uint256 = 0
4-
x = 1
5-
return x
3+
return 1
64

75

8-
# 71 gas
96
@external
107
def addTwoNums(a: uint256, b: uint256) -> uint256:
118
return unsafe_add(a, b)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dasy"
3-
version = "0.1.28"
3+
version = "0.1.29"
44
description = "an evm lisp"
55
authors = ["z80 <z80@ophy.xyz>"]
66

tests/parser/test_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
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
1+
from dasy.parser.utils import (
2+
process_body,
3+
add_src_map,
4+
set_parent_children,
5+
build_node,
6+
next_node_id_maker,
7+
pairwise,
8+
filename_to_contract_name,
9+
has_return,
10+
)
211
from vyper.ast.nodes import Expr
312
import dasy
413

tests/test_dasy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ def testInterface():
319319

320320

321321
def test_reentrancy():
322-
c = compile("examples/nonreentrant.dasy") # noqa: F841
322+
c = compile("examples/nonreentrant.dasy") # noqa: F841
323323

324324

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

329329

330330
def test_token():

0 commit comments

Comments
 (0)