Skip to content
Merged
81 changes: 81 additions & 0 deletions tests/unit/compiler/venom/test_lower_dload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from tests.venom_utils import assert_ctx_eq, parse_from_basic_block
from vyper.venom.analysis import IRAnalysesCache
from vyper.venom.passes import LowerDloadPass

"""
test dload/dloadbytes -> codecopy pass
"""


def _check_pre_post(pre, post):
ctx = parse_from_basic_block(pre)
for fn in ctx.functions.values():
ac = IRAnalysesCache(fn)
LowerDloadPass(ac, fn).run_pass()
assert_ctx_eq(ctx, parse_from_basic_block(post))


def test_lower_dload_basic():
pre = """
main:
%d1 = dload 100
sink %d1
"""

post = """
main:
%1 = add @code_end, 100
codecopy 0, %1, 32
%d1 = mload 0
sink %d1
"""

_check_pre_post(pre, post)


def test_lower_dload_var():
"""
test that dload lowering pass lowers dload properly when the argument is a param
"""
pre = """
main:
%par = param
%d1 = dload %par
sink %d1
"""

post = """
main:
%par = param
%1 = add @code_end, %par
codecopy 0, %1, 32
%d1 = mload 0
sink %d1
"""

_check_pre_post(pre, post)


def test_lower_dload_dloadbytes():
"""
test that dload lowering pass lowers dloadbytes instruction
"""
pre = """
main:
%par = param
dloadbytes 100, 200, 50
dloadbytes 300, %par, 50
stop
"""

post = """
main:
%par = param
%1 = add @code_end, 200
codecopy 100, %1, 50
%2 = add @code_end, %par
codecopy 300, %2, 50
stop
"""

_check_pre_post(pre, post)
4 changes: 4 additions & 0 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
NO_OUTPUT_INSTRUCTIONS - VOLATILE_INSTRUCTIONS
)

# These instructions should be eliminated/rewritten
# before going into assembly emission
PSEUDO_INSTRUCTION = frozenset(["dload", "dloadbytes"])

CFG_ALTERING_INSTRUCTIONS = frozenset(["jmp", "djmp", "jnz"])

COMMUTATIVE_INSTRUCTIONS = frozenset(["add", "mul", "smul", "or", "xor", "and", "eq"])
Expand Down
8 changes: 5 additions & 3 deletions vyper/venom/venom_to_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from vyper.utils import MemoryPositions, OrderedSet, wrap256
from vyper.venom.analysis import CFGAnalysis, DFGAnalysis, IRAnalysesCache, LivenessAnalysis
from vyper.venom.basicblock import (
PSEUDO_INSTRUCTION,
TEST_INSTRUCTIONS,
IRBasicBlock,
IRInstruction,
Expand Down Expand Up @@ -47,6 +48,7 @@
"number",
"extcodesize",
"extcodehash",
"codecopy",
"extcodecopy",
"returndatasize",
"returndatacopy",
Expand Down Expand Up @@ -476,8 +478,6 @@ def _generate_evm_for_instruction(
pass
elif opcode == "store":
pass
elif opcode in ["codecopy", "dloadbytes"]:
assembly.append("CODECOPY")
elif opcode == "dbname":
pass
elif opcode == "jnz":
Expand Down Expand Up @@ -562,7 +562,9 @@ def _generate_evm_for_instruction(
assembly.extend([f"LOG{log_topic_count}"])
elif opcode == "nop":
pass
elif opcode in TEST_INSTRUCTIONS:
elif opcode in PSEUDO_INSTRUCTION: # pragma: nocover
raise CompilerPanic(f"Bad instruction: {opcode}")
elif opcode in TEST_INSTRUCTIONS: # pragma: nocover
raise CompilerPanic(f"Bad instruction: {opcode}")
else:
raise Exception(f"Unknown opcode: {opcode}")
Expand Down