summaryrefslogtreecommitdiff
path: root/src/semantic/translate.rs
blob: 4d51e6077a0671fa2f409b69a73b9f45138afafe (plain)
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
use std::cell::RefCell;
use std::rc::Rc;

use super::frame;
use super::ir;
use super::temp;

#[derive(Clone,Debug)]
pub enum Expression {
    Exp(ir::Expression),
    Void(ir::Statement),
}

pub struct Translate<F> {
    pub temp: temp::Temp,
    level_counter: usize,
    _frametype: ::std::marker::PhantomData<F>,
}

pub enum Level<F>
    where F: frame::Frame {
    Top,
    Level {
        parent: LevelRef<F>,
        frame: F,
        unique_id: usize,
    },
}

pub type LevelRef<F> = Rc<RefCell<Level<F>>>;

impl<F: frame::Frame> Translate<F> {
    pub fn new() -> Translate<F> {
        Translate {
            temp: temp::Temp::new(),
            level_counter: 0,
            _frametype: ::std::marker::PhantomData,
        }
    }

    pub fn make_level<I: Iterator<Item=frame::Escape>>(
        &mut self,
        parent: LevelRef<F>,
        formals: I) -> LevelRef<F> {
        let id = self.level_counter;
        self.level_counter += 1;
        Rc::new(RefCell::new(Level::Level {
            parent: parent,
            frame: F::new(self.temp.next_label(), formals),
            unique_id: id,
        }))
    }

    pub fn make_num(&mut self, num: u64) -> Expression {
        Expression::Exp(ir::Expression::Const(num))
    }

    pub fn make_binop(&mut self, op: ir::BinOp,
                      left: Expression, right: Expression) -> Expression {
        match (left, right) {
            (Expression::Exp(left), Expression::Exp(right)) =>
                Expression::Exp(ir::Expression::BinOp(op, Box::new(left), Box::new(right))),
            // TODO: return Result
            _ => panic!("Can't make binop from Void"),
        }
    }

    pub fn alloc_local<'a>(&mut self,
                           level: &mut Level<F>,
                           escapes: frame::Escape) -> Expression {
        match level {
            &mut Level::Top => {
                panic!("Can't allocate in toplevel frame")
            },
            &mut Level::Level { parent: _, ref mut frame, .. } => {
                let access = frame.alloc_local(&mut self.temp, escapes);
                Expression::Exp(frame.location_to_exp(access))
            }
        }
    }

    pub fn call<I: Iterator<Item=Expression>>(&mut self, target: Expression, parameters: I) -> Expression {
        Expression::Exp(ir::Expression::Call(
            Box::new(match target {
                Expression::Exp(e) => e,
                Expression::Void(_) => panic!("Can't call Void expression"),
            }), parameters.into_iter().map(|p| match p {
                Expression::Exp(e) => e,
                Expression::Void(_) => panic!("Can't evaluate Void expression"),
            }).collect()))
    }
}

impl<F: frame::Frame> Level<F> {
    pub fn to_exp(&self) -> Expression {
        match self {
            &Level::Top => {
                panic!("Function defined in top level frame")
            }
            &Level::Level { ref frame, .. } => {
                Expression::Exp(frame.to_exp())
            }
        }
    }
}