frame.rs 2.53 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

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

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

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

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

impl Frame {
qinsoon's avatar
qinsoon committed
32
    pub fn new(func_ver_id: MuID) -> Frame {
qinsoon's avatar
qinsoon committed
33
        Frame {
qinsoon's avatar
qinsoon committed
34
            func_ver_id: func_ver_id,
qinsoon's avatar
qinsoon committed
35
            cur_offset: - (POINTER_SIZE as isize * 1), // reserve for old RBP
qinsoon's avatar
qinsoon committed
36 37
            allocated: HashMap::new(),
            exception_callsites: HashMap::new()
qinsoon's avatar
qinsoon committed
38 39 40 41
        }
    }
    
    pub fn alloc_slot_for_callee_saved_reg(&mut self, reg: P<Value>, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
42 43 44 45 46 47 48
        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
49 50
    }
    
qinsoon's avatar
qinsoon committed
51 52 53 54
    pub fn add_exception_callsite(&mut self, callsite: ValueLocation, dest: ValueLocation) {
        self.exception_callsites.insert(callsite, dest);
    }
    
qinsoon's avatar
qinsoon committed
55
    fn alloc_slot(&mut self, val: &P<Value>, vm: &VM) -> &FrameSlot {
qinsoon's avatar
qinsoon committed
56
        let id = val.id();
qinsoon's avatar
qinsoon committed
57 58 59 60 61 62 63 64 65
        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
66 67 68
    }
}

qinsoon's avatar
qinsoon committed
69
#[derive(RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
70 71 72
pub struct FrameSlot {
    pub offset: isize,
    pub value: P<Value>
qinsoon's avatar
qinsoon committed
73 74 75 76
}

impl FrameSlot {
    #[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
77
    pub fn make_memory_op(&self, ty: P<MuType>, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
78
        use compiler::backend::x86_64;
qinsoon's avatar
qinsoon committed
79 80 81 82 83 84 85 86 87 88 89 90 91

        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
92 93
    }
}