TritonVM / tasm-lang

Writing tasm with Rust syntax
15 stars 2 forks source link

Add type for subroutine #31

Closed Sword-Smith closed 1 year ago

Sword-Smith commented 1 year ago
struct FunctionState {
    vstack: VStack,
    var_addr: VarAddr,
    spill_required: HashSet<ValueIdentifier>,
    subroutines: Vec<Vec<LabelledInstruction>>,
}

We're testing some properties of subroutines in FunctionState like so:

assert!(
        state.function_state.subroutines.iter().all(|subroutine| {
            if let Label(subroutine_label) = subroutine.first().unwrap() {
                assert!(all_subroutines.insert(subroutine_label.to_owned()), "subroutine labels must be unique");
            } else {
                panic!("Each subroutine must begin with a label");
            }

            let ends_with_return_or_recurse = *subroutine.last().unwrap() == return_()
                || *subroutine.last().unwrap() == recurse();
            let contains_return = subroutine.iter().any(|x| *x == return_());
            ends_with_return_or_recurse && contains_return
        }),
        "Each subroutine must begin with a label, contain a return and end with a return or a recurse"
    );

Instead of doing this test inline, we should add a struct for Subroutine which can either be verified, or is verified in its constructor. So the field subroutines in FunctionsState should have its type changed to Vec<Subroutine>.