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

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

type SlotID = usize;

// | 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 24 25 26
pub struct Frame {
    cur_slot_id: SlotID,
    cur_offset: isize, // offset to rbp
    
qinsoon's avatar
qinsoon committed
27
    allocated: HashMap<SlotID, FrameSlot>,
qinsoon's avatar
qinsoon committed
28 29 30 31 32 33
}

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

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

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

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