Blame view

src/Rust/tree.rs 5 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
use std::fmt;
use ident::{Ident, Label};

#[derive(Copy, Clone, Debug)]
pub enum BinOp {
    Plus,
    Minus,
    Mul,
    Div,
    And,
    Or,
    LShift,
    RShift,
    ARShift,
    Xor,
}

#[derive(Copy, Clone, Debug)]
pub enum RelOp {
    EQ,
    NE,
    LT,
    GT,
    LE,
    GE,
    ULT,
    ULE,
    UGT,
    UGE,
}

#[derive(Debug)]
pub enum Exp {
    Const(i32),
    Name(Label),
    Temp(Ident),
    Param(u32),
    Mem(Box<Exp>),
    BinOp(BinOp, Box<Exp>, Box<Exp>),
    Call(Box<Exp>, Vec<Exp>),
    ESeq(Vec<Stm>, Box<Exp>),
}

#[derive(Debug)]
pub enum Stm {
    Move(Exp, Exp),
    Jump(Exp, Vec<Label>),
    CJump(RelOp, Exp, Exp, Label, Label),
    Seq(Vec<Stm>),
    Label(Label),
}

#[derive(Debug)]
pub struct Function {
    pub name: Label,
    pub nparams: usize,
    pub body: Vec<Stm>,
    pub ret: Ident,
}

#[derive(Debug)]
pub struct Prg {
    pub functions: Vec<Function>,
}

macro_rules! jump {
($e:expr) => { Stm::Jump(Exp::Name($e), vec![$e]) }
}

impl RelOp {
    pub fn neg(&self) -> RelOp {
Ulrich Schoepp committed
72
        use tree::RelOp::*;
Ulrich Schoepp committed
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        match *self {
            EQ => NE,
            NE => EQ,
            LT => GE,
            GT => LE,
            LE => GT,
            GE => LT,
            ULT => UGE,
            ULE => UGT,
            UGT => ULE,
            UGE => ULT,
        }
    }
}

impl fmt::Display for BinOp {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::BinOp::*;
        match *self {
            Plus => write!(fmt, "PLUS"),
            Minus => write!(fmt, "MINUS"),
            Mul => write!(fmt, "MUL"),
            Div => write!(fmt, "DIV"),
            And => write!(fmt, "AND"),
            Or => write!(fmt, "OR"),
            LShift => write!(fmt, "LSHIFT"),
            RShift => write!(fmt, "RSHIFT"),
            ARShift => write!(fmt, "ARSHIFT"),
            Xor => write!(fmt, "XOR"),
        }
    }
}

impl fmt::Display for RelOp {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::RelOp::*;
        match *self {
            EQ => write!(fmt, "EQ"),
            NE => write!(fmt, "NE"),
            LT => write!(fmt, "LT"),
            GT => write!(fmt, "GT"),
            LE => write!(fmt, "LE"),
            GE => write!(fmt, "GE"),
            ULT => write!(fmt, "ULT"),
            ULE => write!(fmt, "ULE"),
            UGT => write!(fmt, "UGT"),
            UGE => write!(fmt, "UGE"),
        }
    }
}


impl fmt::Display for Exp {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::Exp::*;
        match *self {
            Const(i) => write!(fmt, "CONST({})", i),
            Name(ref x) => {
                write!(fmt, "NAME({})", x)
            }
            Temp(ref x) => {
                write!(fmt, "TEMP({})", x)
            }
            Param(i) => write!(fmt, "PARAM({})", i),
            Mem(ref e) => {
                write!(fmt, "MEM({})", e)
            }
            BinOp(ref op, ref e1, ref e2) => {
                write!(fmt, "BINOP({}, {}, {})", op, e1, e2)
            }
            Call(ref ef, ref eargs) => {
                write!(fmt, "CALL({} ", ef)?;
                for arg in eargs {
                    write!(fmt, ", {}", arg)?;
                }
                write!(fmt, ")")
            }
            ESeq(ref ss, ref e) => {
                write!(fmt, "ESEQ(")?;
                for s in ss {
                    write!(fmt, "{}, ", s)?;
                }
                write!(fmt, "{})", e)
            }
        }
    }
}

impl fmt::Display for Stm {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::Stm::*;
        match *self {
            Move(ref e1, ref e2) => {
                write!(fmt, "MOVE({}, {})", e1, e2)
            }
            Jump(ref e, ref is) => {
                write!(fmt, "JUMP({}", e)?;
                for i in is {
                    write!(fmt, ", {}", i)?;
                }
                write!(fmt, ")")
            }
            CJump(ref op, ref e1, ref e2, ref l1, ref l2) => {
                write!(fmt, "CJUMP({}, {}, {}, {}, {})", op, e1, e2, l1, l2)
            }
            Seq(ref ss) => {
                write!(fmt, "SEQ(")?;
                let mut sep = "";
                for s in ss {
                    write!(fmt, "{}{}", sep, s)?;
                    sep = ", ";
                }
                write!(fmt, ")")
            }
            Label(ref l) => {
                write!(fmt, "LABEL({})", l)
            }
        }
    }
}

impl fmt::Display for Function {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        writeln!(fmt, "{}({}){{", self.name, self.nparams)?;
        for s in &self.body {
            writeln!(fmt, "  {}", s)?;
        }
        writeln!(fmt, "return {}", self.ret)?;
        writeln!(fmt, "}}")
    }
}


impl fmt::Display for Prg {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        for m in &self.functions {
            writeln!(fmt, "{}", m)?;
        }
        Ok(())
    }
}