waffle/
util.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::*;
pub fn add_start(m: &mut Module, tf: Func) {
    let s = SignatureData {
        params: vec![],
        returns: vec![],
    };
    let s = new_sig(m, s);
    let mut f = FunctionBody::new(&m, s);
    let vz = f.arg_pool.from_iter(std::iter::empty());
    let t = m.funcs[tf].sig();
    let t = m.signatures[t].clone().returns;
    let tz = f.type_pool.from_iter(t.into_iter());
    let v = f.add_value(ValueDef::Operator(
        Operator::Call { function_index: tf },
        vz,
        tz,
    ));
    f.append_to_block(f.entry, v);
    f.set_terminator(
        f.entry,
        match m.start_func {
            Some(a) => Terminator::ReturnCall {
                func: a,
                args: vec![],
            },
            None => Terminator::Return { values: vec![] },
        },
    );
    let f = m.funcs.push(FuncDecl::Body(s, format!("start"), f));
    m.start_func = Some(f);
}
pub fn new_sig(m: &mut Module, s: SignatureData) -> Signature {
    for (a, b) in m.signatures.entries() {
        if *b == s {
            return a;
        }
    }
    return m.signatures.push(s);
}