Skip to content

Commit

Permalink
[move-decompiler] Improvements to the astifier
Browse files Browse the repository at this point in the history
This PR implements some major improvements to the astifier:

- Finally (I think) cracked the way how to do correct topological sorting and managing nested blocks. More complicated examples now produce more reasonable results.
- Rewrote AST transformations for if-then-else and loops.
- Fixed issues with the fixpoint analysis on the AST
- Did some minor fixes in the sourcifier

The generated source is still not correct in some cases, but much better then before.
  • Loading branch information
wrwg committed Nov 3, 2024
1 parent c8a1eab commit c6bb3ad
Show file tree
Hide file tree
Showing 28 changed files with 2,613 additions and 1,116 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ toml_edit = "0.14.3"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["json", "env-filter"] }
trybuild = "1.0.80"
try_match = "0.4.2"
tokio = { version = "1.35.1", features = ["full"] }
tokio-io-timeout = "1.2.0"
tokio-metrics = "0.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ mod tests {
// Check that the transaction is valid JSON
let transaction = serde_json::from_slice::<Transaction>(json_bytes).unwrap();

assert_eq!(transaction.version, 65);
assert_eq!(transaction.version, 61);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ module 0x2::M {
}
fun t4() {
let () = loop break;
let () = loop if (false) break;
let () = while (!false) ();
}
}
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 @@ -25,6 +25,7 @@ num = { workspace = true }
paste = { workspace = true }
petgraph = { workspace = true }
topological-sort = { workspace = true }
try_match = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@

=== Processing m::f =====================================================
--- Source
public fun f(length: u64): u64 {
assert!(length > 0, 1);
assert!(length < 100, 2);
let counter = 0;
while (counter < length) counter += 1;
counter
}

--- Stackless Bytecode
public fun m::f($t0|length: u64): u64 {
var $t1|$t3: u64
var $t2|$t11: u64 [unused]
var $t3: u64
var $t4: u64
var $t5: bool
var $t6: u64
var $t7: u64
var $t8: u64
var $t9: bool
var $t10: u64
var $t11: u64
var $t12: u64
var $t13: u64
var $t14: bool
var $t15: u64
var $t16: u64
var $t17: u64
var $t18: u64
0: $t3 := copy($t0)
1: $t4 := 0
2: $t5 := >($t3, $t4)
3: if ($t5) goto 4 else goto 6
4: label L1
5: goto 9
6: label L0
7: $t6 := 1
8: abort($t6)
9: label L2
10: $t7 := copy($t0)
11: $t8 := 100
12: $t9 := <($t7, $t8)
13: if ($t9) goto 14 else goto 16
14: label L4
15: goto 19
16: label L3
17: $t10 := 2
18: abort($t10)
19: label L5
20: $t11 := 0
21: $t1 := $t11
22: goto 23
23: label L10
24: $t12 := copy($t1)
25: $t13 := copy($t0)
26: $t14 := <($t12, $t13)
27: if ($t14) goto 28 else goto 34
28: label L7
29: $t15 := move($t1)
30: $t16 := 1
31: $t17 := +($t15, $t16)
32: $t1 := $t17
33: goto 36
34: label L6
35: goto 38
36: label L8
37: goto 23
38: label L9
39: $t18 := move($t1)
40: return $t18
}

--- Raw Generated AST
_t3: u64 = length;
_t4: u64 = 0;
_t5: bool = Gt(_t3, _t4);
loop {
if (_t5) break;
_t6: u64 = 1;
Abort(_t6)
};
_t7: u64 = length;
_t8: u64 = 100;
_t9: bool = Lt(_t7, _t8);
loop {
if (_t9) break;
_t10: u64 = 2;
Abort(_t10)
};
_t11: u64 = 0;
_t1: u64 = _t11;
loop {
_t12: u64 = _t1;
_t13: u64 = length;
_t14: bool = Lt(_t12, _t13);
if (Not(_t14)) break;
_t15: u64 = _t1;
_t16: u64 = 1;
_t17: u64 = Add(_t15, _t16);
_t1: u64 = _t17;
continue
};
_t18: u64 = _t1;
return _t18

--- Assign-Transformed Generated AST
loop {
if (Gt(length, 0)) break;
Abort(1)
};
loop {
if (Lt(length, 100)) break;
Abort(2)
};
_t1: u64 = 0;
loop {
if (Not(Lt(_t1, length))) break;
_t1: u64 = Add(_t1, 1);
continue
};
return _t1

--- If-Transformed Generated AST
if (Not(Gt(length, 0))) Abort(1);
if (Not(Lt(length, 100))) Abort(2);
_t1: u64 = 0;
loop {
if (Not(Lt(_t1, length))) break;
_t1: u64 = Add(_t1, 1);
continue
};
return _t1

--- Var-Bound Generated AST
{
let _t1: u64;
if (Not(Gt(length, 0))) Abort(1);
if (Not(Lt(length, 100))) Abort(2);
_t1: u64 = 0;
loop {
if (Not(Lt(_t1, length))) break;
_t1: u64 = Add(_t1, 1);
continue
};
return _t1
}

=== Sourcified Output ============================================
module 0x815::m {
public fun f(length: u64): u64 {
let _t1;
if (!(length > 0)) abort 1;
if (!(length < 100)) abort 2;
_t1 = 0;
while (_t1 < length) _t1 = _t1 + 1;
_t1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module 0x815::m {
public fun f(length: u64): u64 {
assert!(length > 0, 1);
assert!(length < 100, 2);
let counter = 0;
while (counter < length) counter += 1;
counter
}
}
Loading

0 comments on commit c6bb3ad

Please sign in to comment.