summaryrefslogtreecommitdiff
path: root/src/semantic/translate.rs
blob: f802c3f62c8faf7960e9f272ab20652ab2e09f81 (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
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<'a, F>
    where F: 'a + frame::Frame {
    Top,
    Level {
        parent: &'a Level<'a, F>,
        frame: F,
        unique_id: usize,
    },
}

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<'a>(
        &mut self,
        parent: &'a Level<F>,
        formals: Vec<frame::Escape>) -> Level<'a, F> {
        let id = self.level_counter;
        self.level_counter += 1;
        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<'a, 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))
            }
        }
    }
}