Skip to content

Commit

Permalink
test that parsed venom can go to bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Dec 24, 2024
1 parent bcbb5fe commit 1728bee
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions tests/functional/venom/test_venom_repr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import glob

import textwrap
import pytest

from tests.venom_utils import assert_ctx_eq, parse_venom
from vyper.compiler import compile_code
from vyper.compiler.phases import generate_bytecode
from vyper.venom import generate_assembly_experimental, run_passes_on
from vyper.venom.context import IRContext

"""
Expand All @@ -16,15 +19,47 @@ def get_example_vy_filenames():


@pytest.mark.parametrize("vy_filename", get_example_vy_filenames())
def test_round_trip(vy_filename, optimize, request):
def test_round_trip_example(vy_filename, optimize):
"""
Check all examples round trip
"""
path = f"examples/{vy_filename}"
with open(path) as f:
vyper_source = f.read()

out = compile_code(vyper_source, output_formats=["bb_runtime"])
_round_trip_helper(vyper_source, optimize)

vyper_sources = ["""
@external
def _loop() -> uint256:
res: uint256 = 9
for i: uint256 in range(res, bound=10):
res = res + i
return res
"""]

@pytest.mark.parametrize("vyper_source", vyper_sources)
def test_round_trip_source(vyper_source, optimize):
vyper_source = textwrap.dedent(vyper_source)
_round_trip_helper(vyper_source, optimize)


def _round_trip_helper(vyper_source, optimize):
out = compile_code(vyper_source, output_formats=["bb_runtime", "bytecode_runtime"])
bb_runtime = out["bb_runtime"]
venom_code = IRContext.__repr__(bb_runtime)

ctx = parse_venom(venom_code)

assert_ctx_eq(bb_runtime, ctx)

# check it's valid to run venom passes+analyses
run_passes_on(ctx, optimize)

asm = generate_assembly_experimental(ctx)
bytecode = generate_bytecode(asm, compiler_metadata=None)
bytecode = f"0x{bytecode.hex()}"

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable bytecode is not used.

# TODO investigate: bytecodes should be equal (even without
# `run_passes_on`) but not for some reason
# assert bytecode == out["bytecode_runtime"]

0 comments on commit 1728bee

Please sign in to comment.