Skip to content

Commit

Permalink
[compiler-v2] Ast generator from stackless bytecode
Browse files Browse the repository at this point in the history
Converter from stackless bytecode into Model AST. We already have one from binary format to stackless.

This will be useful in many places in the stack, not at least for debugging, but also as a potential different approach to decompilation. For the later, we still need to create an AST -> Move translator, today we have only debug dump.

Most of the logic is contained in the single new file `ast_generator.rs`. There is some longer module description, and I refer to there for whats provided.

This is still a prototype, but I'd like to land for iteration. There are some tests in a new test crate `ast-generator-tests`. These call the v2 compiler to compile up to file format, then decompile from there into stackless bytecode, and into AST. However, the best way to test this functionality is to actually integrate it into an `compile-execute == compile-decompile-compile-execute` test on many of our existing tests, and even the framework.
  • Loading branch information
wrwg committed Sep 26, 2024
1 parent 5ad601c commit 04a48c5
Show file tree
Hide file tree
Showing 25 changed files with 3,884 additions and 35 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ members = [
"third_party/move/move-model",
"third_party/move/move-model/bytecode",
"third_party/move/move-model/bytecode-test-utils",
"third_party/move/move-model/bytecode/ast-generator-tests",
"third_party/move/move-prover",
"third_party/move/move-prover/boogie-backend",
"third_party/move/move-prover/bytecode-pipeline",
Expand Down Expand Up @@ -786,6 +787,7 @@ tonic = { version = "0.11.0", features = [
"zstd",
] }
tonic-reflection = "0.11.0"
topological-sort = "0.2.2"
triomphe = "0.1.9"
tui = "0.19.0"
typed-arena = "2.0.2"
Expand Down
5 changes: 4 additions & 1 deletion third_party/move/move-compiler-v2/src/bytecode_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl<'env> Generator<'env> {
self.emit_with(*id, |attr| Bytecode::Jump(attr, continue_label));
self.emit_with(*id, |attr| Bytecode::Label(attr, break_label));
},
ExpData::LoopCont(id, do_continue) => {
ExpData::LoopCont(id, 0, do_continue) => {
if let Some(LoopContext {
continue_label,
break_label,
Expand All @@ -463,6 +463,9 @@ impl<'env> Generator<'env> {
self.error(*id, "missing enclosing loop statement")
}
},
ExpData::LoopCont(_, _, _) => {
unimplemented!("continue/break with nesting")

Check warning on line 467 in third_party/move/move-compiler-v2/src/bytecode_generator.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-compiler-v2/src/bytecode_generator.rs#L467

Added line #L467 was not covered by tests
},
ExpData::SpecBlock(id, spec) => {
// Map locals in spec to assigned temporaries.
let mut replacer = |id, target| {
Expand Down
4 changes: 2 additions & 2 deletions third_party/move/move-compiler-v2/src/env_pipeline/inliner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl<'env, 'rewriter> InlinedRewriter<'env, 'rewriter> {
(lambda expressions)",
)
},
ExpData::LoopCont(node_id, is_continue) if !post && in_loop == 0 => {
ExpData::LoopCont(node_id, _, is_continue) if !post && in_loop == 0 => {
let node_loc = env.get_node_loc(*node_id);
env.error(
&node_loc,
Expand Down Expand Up @@ -1046,7 +1046,7 @@ impl<'env, 'rewriter> ExpRewriterFunctions for InlinedRewriter<'env, 'rewriter>
self.in_loop += 1;
true
},
ExpData::LoopCont(node_id, is_continue) if self.in_loop == 0 => {
ExpData::LoopCont(node_id, _, is_continue) if self.in_loop == 0 => {
let node_loc = self.env.get_node_loc(*node_id);
self.env.error(
&node_loc,
Expand Down
1 change: 1 addition & 0 deletions third_party/move/move-model/bytecode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log = { workspace = true }
num = { workspace = true }
paste = { workspace = true }
petgraph = { workspace = true }
topological-sort = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "ast-generator-tests"
version = "0.1.0"
edition = "2021"
license = { workspace = true }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
anyhow = { workspace = true }
codespan-reporting = { workspace = true, features = ["serde", "serialization"] }
datatest-stable = { workspace = true }
move-compiler-v2 = { workspace = true }
move-model = { workspace = true }
move-prover-test-utils = { workspace = true }

move-stackless-bytecode = { path = ".." }

[[test]]
name = "testsuite"
harness = false

[lib]
doctest = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © Aptos Foundation
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Intentionally empty
Loading

0 comments on commit 04a48c5

Please sign in to comment.