Blame view

src/Rust/canonize.rs 5.15 KB
Ulrich Schoepp committed
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
use ident::Ident;
use tree::*;

pub fn canonize_prg(p: Prg) -> Prg {
    let mut cm = Vec::new();
    for m in p.functions {
        cm.push(canonize_fun(m))
    }
    Prg {
        functions: cm,
    }
}

fn canonize_fun(m: Function) -> Function {
    let mut cbody = Vec::new();
    for s in m.body {
        cbody.append(&mut canonize_stm(s))
    }
    Function {
        name: m.name,
        nparams: m.nparams,
        body: cbody,
        ret: m.ret, // is temp, need not be canonized
    }
}

fn canonize_stm(s: Stm) -> Vec<Stm> {
    use tree::Exp::*;
    use tree::Stm::*;
    match s {
        Move(e1, e2) => {
            match e1 {
                Mem(a) => {
                    let b1 = canonize_exp_no_top_call(*a);
                    let b2 = canonize_exp(e2);
                    Block::combine_stm(b1, b2, &|a, e2| Move(Mem(Box::new(a)), e2))
                }
                Temp(t) => {
                    let mut b = canonize_exp(e2);
                    b.stms.push(Move(Temp(t), b.exp));
                    b.stms
                }
                Param(i) => {
                    let mut b = canonize_exp(e2);
                    b.stms.push(Move(Param(i), b.exp));
                    b.stms
                }
                ESeq(mut s, e) => {
                    s.push(Move(*e, e2));
                    canonize_stm(Seq(s))
                }
                _ => unreachable!(),
            }
        }
        Jump(e, ids) => {
            let mut b = canonize_exp_no_top_call(e);
            b.stms.push(Jump(b.exp, ids));
            b.stms
        }
        CJump(o, e1, e2, t1, t2) => {
            let b1 = canonize_exp_no_top_call(e1);
            let b2 = canonize_exp_no_top_call(e2);
            Block::combine_stm(b1, b2, &|e1, e2| CJump(o, e1, e2, t1, t2))
        }
        Seq(ss) => {
            let mut cs = Vec::new();
            for s in ss {
                cs.append(&mut canonize_stm(s))
            }
            cs
        }
        Label(i) => vec![Label(i)],
    }
}

fn canonize_exp_no_top_call(e: Exp) -> Block {
    use tree::Exp::*;
    let mut b = canonize_exp(e);
    match b.exp {
        Call(f, args) => {
            let i = Ident::new();
            b.stms.push(Stm::Move(Temp(i), Call(f, args)));
            b.exp = Temp(i);
            b
        }
        _ => b,
    }
}

fn canonize_exp(e: Exp) -> Block {
    use tree::Exp::*;
    match e {
        Const(i) => Block::new_exp(Const(i)),
        Name(n) => Block::new_exp(Name(n)),
        Temp(x) => Block::new_exp(Temp(x)),
        Param(i) => Block::new_exp(Param(i)),
        Mem(e) => {
            let mut b = canonize_exp_no_top_call(*e);
            b.exp = Mem(Box::new(b.exp));
            b
        }
        BinOp(o, e1, e2) => {
            let b1 = canonize_exp_no_top_call(*e1);
            let b2 = canonize_exp_no_top_call(*e2);
            Block::combine(b1, b2, &|e1, e2| BinOp(o, Box::new(e1), Box::new(e2)))
        }
        Call(e, mut args) => {
            let b = canonize_exp_no_top_call(*e);
            let mut bs = Vec::new();
            args.reverse();
            for a in args {
                bs.push(canonize_exp_no_top_call(a));
            }
            Block::combine_n(b, bs, &|e, es| Call(Box::new(e), es))
        }
        ESeq(ss, e) => {
            let mut cs = Vec::new();
            for s in ss {
                cs.append(&mut canonize_stm(s))
            }
            Block::new(cs, *e)
        }
    }
}

struct Block {
    stms: Vec<Stm>,
    exp: Exp,
}

impl Block {
    fn new(s: Vec<Stm>, e: Exp) -> Block {
        Block { stms: s, exp: e }
    }

    fn new_exp(e: Exp) -> Block {
        Block {
            stms: Vec::new(),
            exp: e,
        }
    }

    fn commutes(_: &Stm, e: &Exp) -> bool {
        match *e {
            Exp::Name(_) => true,
            Exp::Const(_) => true,
            _ => false,
        }
    }

    fn commute(ss: &Vec<Stm>, e: &Exp) -> bool {
        for s in ss {
            if !Block::commutes(s, e) {
                return false;
            }
        }
        true
    }

    fn compose(mut b: Block, ss: &mut Vec<Stm>) -> Block {
        if Block::commute(&ss, &b.exp) {
            b.stms.append(ss);
            b
        } else {
            let i = Ident::new();
            let m = Stm::Move(Exp::Temp(i), b.exp);
            b.stms.push(m);
            b.stms.append(ss);
            b.exp = Exp::Temp(i);
            b
        }
    }

    fn combine(b1: Block, mut b2: Block, f: &Fn(Exp, Exp) -> Exp) -> Block {
        let mut b = Block::compose(b1, &mut b2.stms);
        b.exp = f(b.exp, b2.exp);
        b
    }

    fn combine_n(b: Block, mut bs: Vec<Block>, f: &Fn(Exp, Vec<Exp>) -> Exp) -> Block {
        bs.reverse();
        let mut ss = vec![];
        let mut es = vec![];
        for b in bs {
            let b1 = Block::compose(b, &mut ss);
            ss = b1.stms;
            es.push(b1.exp)
        }
        let mut b1 = Block::compose(b, &mut ss);
        b1.exp = f(b1.exp, es);
        b1
    }

    fn combine_stm(b1: Block, mut b2: Block, f: &Fn(Exp, Exp) -> Stm) -> Vec<Stm> {
        let mut b = Block::compose(b1, &mut b2.stms);
        b.stms.push(f(b.exp, b2.exp));
        b.stms
    }
}