Skip to content

Commit f75d039

Browse files
committed
Added compile-time evaluation for convert, slice and extract32
1 parent 11522b8 commit f75d039

17 files changed

+747
-62
lines changed

tests/functional/builtins/codegen/test_create_functions.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,30 @@ def deploy_from_memory() -> address:
785785
assert env.get_code(res) == runtime
786786

787787

788+
# `initcode` and `value` arguments overlap
789+
def test_raw_create_memory_overlap(get_contract, env):
790+
to_deploy_code = """
791+
foo: public(uint256)
792+
"""
793+
794+
out = compile_code(to_deploy_code, output_formats=["bytecode", "bytecode_runtime"])
795+
initcode = bytes.fromhex(out["bytecode"].removeprefix("0x"))
796+
runtime = bytes.fromhex(out["bytecode_runtime"].removeprefix("0x"))
797+
798+
deployer_code = """
799+
@external
800+
def deploy_from_calldata(s: Bytes[49152]) -> address:
801+
v: DynArray[Bytes[49152], 2] = [s]
802+
x: address = raw_create(v[0], value = 0 if v.pop() == b'' else 0, revert_on_failure=False)
803+
return x
804+
"""
805+
806+
deployer = get_contract(deployer_code)
807+
808+
res = deployer.deploy_from_calldata(initcode)
809+
assert env.get_code(res) == runtime
810+
811+
788812
def test_raw_create_double_eval(get_contract, env):
789813
to_deploy_code = """
790814
foo: public(uint256)
@@ -1095,6 +1119,8 @@ def __init__(arg: uint256):
10951119
initcode = bytes.fromhex(out["bytecode"].removeprefix("0x"))
10961120
runtime = bytes.fromhex(out["bytecode_runtime"].removeprefix("0x"))
10971121

1122+
# the implementation of `raw_create` firstly caches
1123+
# `value` and then `salt`, here the source order is `salt` then `value`
10981124
deployer_code = """
10991125
c: Bytes[1024]
11001126
salt: bytes32
@@ -1122,3 +1148,33 @@ def deploy_from_calldata(s: Bytes[1024], arg: uint256, salt: bytes32, value_: ui
11221148
assert HexBytes(res) == create2_address_of(deployer.address, salt, initcode)
11231149
assert env.get_code(res) == runtime
11241150
assert env.get_balance(res) == value
1151+
1152+
1153+
# test vararg and kwarg order of evaluation
1154+
# test fails because `value` gets evaluated
1155+
# before the 1st vararg which doesn't follow
1156+
# source code order
1157+
@pytest.mark.xfail(raises=AssertionError)
1158+
def test_raw_create_eval_order(get_contract):
1159+
code = """
1160+
a: public(uint256)
1161+
1162+
@deploy
1163+
def __init__():
1164+
initcode: Bytes[100] = b"a"
1165+
res: address = raw_create(
1166+
initcode, self.test1(), value=self.test2(), revert_on_failure=False
1167+
)
1168+
1169+
@internal
1170+
def test1() -> uint256:
1171+
self.a = 1
1172+
return 1
1173+
1174+
@internal
1175+
def test2() -> uint256:
1176+
self.a = 2
1177+
return 2
1178+
"""
1179+
c = get_contract(code)
1180+
assert c.a() == 2

tests/hevm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from tests.venom_utils import parse_from_basic_block
77
from vyper.ir.compile_ir import assembly_to_evm
8-
from vyper.venom import LowerDloadPass, SimplifyCFGPass, StoreExpansionPass, VenomCompiler
8+
from vyper.venom import LowerDloadPass, SimplifyCFGPass, SingleUseExpansion, VenomCompiler
99
from vyper.venom.analysis import IRAnalysesCache
1010
from vyper.venom.basicblock import IRInstruction, IRLiteral
1111

@@ -64,7 +64,7 @@ def _prep_hevm_venom_ctx(ctx, verbose=False):
6464

6565
# requirements for venom_to_assembly
6666
LowerDloadPass(ac, fn).run_pass()
67-
StoreExpansionPass(ac, fn).run_pass()
67+
SingleUseExpansion(ac, fn).run_pass()
6868

6969
compiler = VenomCompiler([ctx])
7070
asm = compiler.generate_evm(no_optimize=False)

tests/unit/compiler/venom/test_algebraic_binopt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import pytest
22

33
from tests.venom_utils import PrePostChecker
4-
from vyper.venom.passes import AlgebraicOptimizationPass, StoreElimination
4+
from vyper.venom.passes import AlgebraicOptimizationPass, AssignElimination
55

66
"""
77
Test abstract binop+unop optimizations in algebraic optimizations pass
88
"""
99

1010
pytestmark = pytest.mark.hevm
1111

12-
_check_pre_post = PrePostChecker([StoreElimination, AlgebraicOptimizationPass, StoreElimination])
12+
_check_pre_post = PrePostChecker([AssignElimination, AlgebraicOptimizationPass, AssignElimination])
1313

1414

1515
def test_sccp_algebraic_opt_sub_xor():

tests/unit/compiler/venom/test_duplicate_operands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from vyper.venom import generate_assembly_experimental
33
from vyper.venom.analysis import IRAnalysesCache
44
from vyper.venom.context import IRContext
5-
from vyper.venom.passes import StoreExpansionPass
5+
from vyper.venom.passes import SingleUseExpansion
66

77

88
def test_duplicate_operands():
@@ -26,7 +26,7 @@ def test_duplicate_operands():
2626
bb.append_instruction("stop")
2727

2828
ac = IRAnalysesCache(fn)
29-
StoreExpansionPass(ac, fn).run_pass()
29+
SingleUseExpansion(ac, fn).run_pass()
3030

3131
optimize = OptimizationLevel.GAS
3232
asm = generate_assembly_experimental(ctx, optimize=optimize)

tests/unit/compiler/venom/test_load_elimination.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from tests.venom_utils import PrePostChecker
44
from vyper.evm.address_space import CALLDATA, DATA, MEMORY, STORAGE, TRANSIENT
5-
from vyper.venom.passes import LoadElimination, StoreElimination
5+
from vyper.venom.passes import AssignElimination, LoadElimination
66

77
pytestmark = pytest.mark.hevm
88

@@ -11,7 +11,7 @@
1111
# and the second/in post is needed to create
1212
# easier equivalence in the test for pre and post
1313
_check_pre_post = PrePostChecker(
14-
passes=[StoreElimination, LoadElimination, StoreElimination], post=[StoreElimination]
14+
passes=[AssignElimination, LoadElimination, AssignElimination], post=[AssignElimination]
1515
)
1616

1717

0 commit comments

Comments
 (0)