Module leo_passes::flattening

source ·
Expand description

The flattening pass traverses the AST after the SSA pass and converts into a sequential code. The pass flattens ConditionalStatements into a sequence of AssignStatements. The pass rewrites ReturnStatements into AssignStatements and consolidates the returned values as a single ReturnStatement at the end of the function. The pass rewrites ternary expressions over composite data types, into ternary expressions over the individual fields of the composite data type, followed by an expression constructing the composite data type. Note that this transformation is not applied to async functioins.

Consider the following Leo code, output by the SSA pass.

function main(flag: u8, value: u8) -> u8 {
    $var$0 = flag == 0u8;
    if ($var$0) {
        $var$1 = value + 1u8;
        value$1 = $var$1;
        return value$1;
    } else {
        $var$2 = value + 2u8;
        value$2 = $var$2;
    }
    value$3 = $var$0 ? value$1 : value$2;
    return value$3;
}

The flattening pass produces the following code.

function main(flag: u8, value: u8) -> u8 {
    $var$0 = flag == 0u8;
    $var$1 = value + 1u8;
    value$1 = $var$1;
    $var$2 = value + 2u8;
    value$2 = $var$2;
    value$3 = $var$0 ? value$1 : value$2;
    ret$4 = $var$0 ? value$1 : value$3;
    return ret$4;
}

Re-exports§

Modules§