leo_passes/static_single_assignment/
mod.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! The Static Single Assignment pass traverses the AST and converts it into SSA form.
18//! See https://en.wikipedia.org/wiki/Static_single-assignment_form for more information.
19//! The pass also replaces `DefinitionStatement`s with `AssignmentStatement`s.
20//! The pass also simplifies complex expressions into a sequence of `AssignStatement`s. For example, `(a + b) * c` is rewritten into `$var$1 = a + b; $var$2 = $var$1 * c`.
21//!
22//! Consider the following Leo code.
23//! ```leo
24//! function main(flag: u8, value: u8) -> u8 {
25//!     if (flag == 0u8) {
26//!         value += 1u8;
27//!         return value;
28//!     } else {
29//!         value += 2u8;
30//!     }
31//!     return value;
32//! }
33//! ```
34//!
35//! The SSA pass produces the following code.
36//! ```leo
37//! function main(flag: u8, value: u8) -> u8 {
38//!     $var$0 = flag == 0u8;
39//!     if ($var$0) {
40//!         $var$1 = value + 1u8;
41//!         value$1 = $var$1;
42//!         return value$1;
43//!     } else {
44//!         $var$2 = value + 2u8;
45//!         value$2 = $var$2;
46//!     }
47//!     value$3 = $var$0 ? value$1 : value$2;
48//!     return value$3;
49//! }
50//! ```
51//! Note that the redundant assignments have no effect on the bytecode generated by the compiler.
52
53use crate::{Pass, RenameTable};
54
55use leo_ast::ProgramConsumer;
56use leo_errors::Result;
57use leo_span::Symbol;
58
59mod expression;
60
61mod program;
62
63mod statement;
64
65mod visitor;
66use visitor::*;
67
68pub struct SsaFormingInput {
69    pub rename_defs: bool,
70}
71
72pub struct SsaForming;
73
74impl Pass for SsaForming {
75    type Input = SsaFormingInput;
76    type Output = ();
77
78    const NAME: &str = "SsaForming";
79
80    fn do_pass(input: Self::Input, state: &mut crate::CompilerState) -> Result<Self::Output> {
81        let mut ast = std::mem::take(&mut state.ast);
82        let mut visitor = SsaFormingVisitor {
83            state,
84            rename_table: RenameTable::new(None),
85            program: Symbol::intern(""),
86            rename_defs: input.rename_defs,
87        };
88        ast.ast = visitor.consume_program(ast.ast);
89        visitor.state.handler.last_err().map_err(|e| *e)?;
90        visitor.state.ast = ast;
91        Ok(())
92    }
93}