forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor[venom]: make venom repr parseable (vyperlang#4402)
make the `__repr__()` implementations for venom data structures (`IRContext`, `IRFunction, `IRBasicBlock`) emit strings which will round-trip through the parser. for labels generated in the frontend which are not necessarily valid identifiers (e.g. `"internal 5 foo()"`), these are represented as escaped strings. the expedient way to implement this was to simply use `json.loads` / `json.dumps`; there did not seem to be any convenient stdlib or lark function to do this. since this adds grammar complexity, the other method that was considered was to map all labels to valid identifiers in `ir_node_to_venom.py`. but this approach seems easier than cleaning up all the non-identifier labels generated by the frontend; plus, being able to have arbitrary strings in labels seems like it will come in handy during debugging some time. a couple other grammar updates/fixes: - update instruction order in the text format for `phi` and `invoke` - ensure instructions are terminated with newline (otherwise they can continue slurping tokens from the next line). - allow signed ints inside `CONST` nodes as `IRLiteral` accepts negative numbers misc/refactor: - remove a dead function (`str_short()`). - remove a dead branch in `ir_node_to_venom.py` - when optimization level is set to `CODESIZE`, the roundtrip test is set to xfail, as the data section contains bytestrings (which do not parse yet).
- Loading branch information
1 parent
f6030fb
commit eee31e7
Showing
8 changed files
with
111 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import glob | ||
|
||
import pytest | ||
|
||
from tests.venom_utils import assert_ctx_eq, parse_venom | ||
from vyper.compiler import compile_code | ||
from vyper.compiler.settings import OptimizationLevel | ||
from vyper.venom.context import IRContext | ||
|
||
""" | ||
Check that venom text format round-trips through parser | ||
""" | ||
|
||
|
||
def get_example_vy_filenames(): | ||
return glob.glob("**/*.vy", root_dir="examples/", recursive=True) | ||
|
||
|
||
@pytest.mark.parametrize("vy_filename", get_example_vy_filenames()) | ||
def test_round_trip(vy_filename, optimize, request): | ||
if optimize == OptimizationLevel.CODESIZE: | ||
# codesize optimization issues things like `db b"\x12\x34"` which we | ||
# don't handle. | ||
request.node.add_marker(pytest.mark.xfail(strict=False, reason="unimplemented in parser")) | ||
|
||
path = f"examples/{vy_filename}" | ||
with open(path) as f: | ||
vyper_source = f.read() | ||
|
||
out = compile_code(vyper_source, output_formats=["bb_runtime"]) | ||
bb_runtime = out["bb_runtime"] | ||
venom_code = IRContext.__repr__(bb_runtime) | ||
|
||
ctx = parse_venom(venom_code) | ||
|
||
assert_ctx_eq(bb_runtime, ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters