frame.rs 3.33 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
    // key: callsite, val: destination address
    pub exception_callsites: HashMap<ValueLocation, ValueLocation>
qinsoon's avatar
qinsoon committed
30 31
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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();
        for (callsite, dest) in self.exception_callsites.iter() {
            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 53
            allocated: HashMap::new(),
            exception_callsites: HashMap::new()
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
    pub fn add_exception_callsite(&mut self, callsite: ValueLocation, dest: ValueLocation) {
68
        trace!("add exception callsite: {} to dest {}", callsite, dest);
qinsoon's avatar
qinsoon committed
69 70 71
        self.exception_callsites.insert(callsite, dest);
    }
    
qinsoon's avatar
qinsoon committed
72
    fn alloc_slot(&mut self, val: &P<Value>, vm: &VM) -> &FrameSlot {
qinsoon's avatar
qinsoon committed
73
        let id = val.id();
qinsoon's avatar
qinsoon committed
74 75 76 77 78 79 80 81 82
        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
83 84 85
    }
}

qinsoon's avatar
qinsoon committed
86
#[derive(RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
87 88 89
pub struct FrameSlot {
    pub offset: isize,
    pub value: P<Value>
qinsoon's avatar
qinsoon committed
90 91
}

92 93 94 95 96 97
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
98 99
impl FrameSlot {
    #[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
100
    pub fn make_memory_op(&self, ty: P<MuType>, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
101
        use compiler::backend::x86_64;
qinsoon's avatar
qinsoon committed
102 103 104 105 106 107 108 109 110 111 112 113 114

        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
115 116
    }
}