frame.rs 3.44 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
use ast::ir::*;
use ast::ptr::*;
qinsoon's avatar
qinsoon committed
3
use ast::types::*;
qinsoon's avatar
qinsoon committed
4
use runtime::ValueLocation;
qinsoon's avatar
qinsoon committed
5

6
use std::fmt;
qinsoon's avatar
qinsoon committed
7
use std::collections::HashMap;
qinsoon's avatar
qinsoon committed
8
use utils::POINTER_SIZE;
qinsoon's avatar
qinsoon committed
9
use vm::VM;
qinsoon's avatar
qinsoon committed
10 11 12 13 14 15 16 17 18 19

// | previous frame ...
// |---------------
// | return address
// | old RBP        <- RBP
// | callee saved
// | spilled
// |---------------
// | alloca area

qinsoon's avatar
qinsoon committed
20 21 22
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

#[derive(RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
23
pub struct Frame {
qinsoon's avatar
qinsoon committed
24
    func_ver_id: MuID,
qinsoon's avatar
qinsoon committed
25 26
    cur_offset: isize, // offset to rbp
    
qinsoon's avatar
qinsoon committed
27
    pub allocated: HashMap<MuID, FrameSlot>,
qinsoon's avatar
qinsoon committed
28 29
    // (callsite, destination address)
    exception_callsites: Vec<(ValueLocation, ValueLocation)>
qinsoon's avatar
qinsoon committed
30 31
}

32 33 34 35 36 37 38 39
impl fmt::Display for Frame {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Frame for FuncVer {} {{", self.func_ver_id).unwrap();
        writeln!(f, "  allocated slots:").unwrap();
        for slot in self.allocated.values() {
            writeln!(f, "    {}", slot).unwrap();
        }
        writeln!(f, "  exception callsites:").unwrap();
qinsoon's avatar
qinsoon committed
40
        for &(ref callsite, ref dest) in self.exception_callsites.iter() {
41 42 43 44 45 46
            writeln!(f, "    callsite: {} -> {}", callsite, dest).unwrap()
        }
        writeln!(f, "}}")
    }
}

qinsoon's avatar
qinsoon committed
47
impl Frame {
qinsoon's avatar
qinsoon committed
48
    pub fn new(func_ver_id: MuID) -> Frame {
qinsoon's avatar
qinsoon committed
49
        Frame {
qinsoon's avatar
qinsoon committed
50
            func_ver_id: func_ver_id,
qinsoon's avatar
qinsoon committed
51
            cur_offset: - (POINTER_SIZE as isize * 1), // reserve for old RBP
qinsoon's avatar
qinsoon committed
52
            allocated: HashMap::new(),
qinsoon's avatar
qinsoon committed
53
            exception_callsites: vec![]
qinsoon's avatar
qinsoon committed
54 55 56 57
        }
    }
    
    pub fn alloc_slot_for_callee_saved_reg(&mut self, reg: P<Value>, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
58 59 60 61 62 63 64
        let slot = self.alloc_slot(&reg, vm);
        slot.make_memory_op(reg.ty.clone(), vm)
    }
    
    pub fn alloc_slot_for_spilling(&mut self, reg: P<Value>, vm: &VM) -> P<Value> {
        let slot = self.alloc_slot(&reg, vm);
        slot.make_memory_op(reg.ty.clone(), vm)
qinsoon's avatar
qinsoon committed
65 66
    }
    
qinsoon's avatar
qinsoon committed
67 68 69 70
    pub fn get_exception_callsites(&self) -> &Vec<(ValueLocation, ValueLocation)> {
        &self.exception_callsites
    }
    
qinsoon's avatar
qinsoon committed
71
    pub fn add_exception_callsite(&mut self, callsite: ValueLocation, dest: ValueLocation) {
72
        trace!("add exception callsite: {} to dest {}", callsite, dest);
qinsoon's avatar
qinsoon committed
73
        self.exception_callsites.push((callsite, dest));
qinsoon's avatar
qinsoon committed
74 75
    }
    
qinsoon's avatar
qinsoon committed
76
    fn alloc_slot(&mut self, val: &P<Value>, vm: &VM) -> &FrameSlot {
qinsoon's avatar
qinsoon committed
77
        let id = val.id();
qinsoon's avatar
qinsoon committed
78 79 80 81 82 83 84 85 86
        let ret = FrameSlot {
            offset: self.cur_offset,
            value: val.clone()
        };
        
        self.cur_offset -= vm.get_type_size(val.ty.id()) as isize;
        
        self.allocated.insert(id, ret);
        self.allocated.get(&id).unwrap()
qinsoon's avatar
qinsoon committed
87 88 89
    }
}

qinsoon's avatar
qinsoon committed
90
#[derive(RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
91 92 93
pub struct FrameSlot {
    pub offset: isize,
    pub value: P<Value>
qinsoon's avatar
qinsoon committed
94 95
}

96 97 98 99 100 101
impl fmt::Display for FrameSlot {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}(RBP): {}", self.offset, self.value)
    }
}

qinsoon's avatar
qinsoon committed
102 103
impl FrameSlot {
    #[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
104
    pub fn make_memory_op(&self, ty: P<MuType>, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
105
        use compiler::backend::x86_64;
qinsoon's avatar
qinsoon committed
106 107 108 109 110 111 112 113 114 115 116 117 118

        P(Value{
            hdr: MuEntityHeader::unnamed(vm.next_id()),
            ty: ty.clone(),
            v: Value_::Memory(
                MemoryLocation::Address{
                    base: x86_64::RBP.clone(),
                    offset: Some(Value::make_int_const(vm.next_id(), self.offset as u64)),
                    index: None,
                    scale: None
                }
            )
        })
qinsoon's avatar
qinsoon committed
119 120
    }
}