mod.rs 2.18 KB
Newer Older
1
pub mod mm;
qinsoon's avatar
qinsoon committed
2
pub mod thread;
3
pub mod entrypoints;
qinsoon's avatar
qinsoon committed
4

5 6
pub use runtime::mm::common::Address;
pub use runtime::mm::common::ObjectReference;
7

8
use utils;
9 10 11 12
use ast::ir;
use ast::ptr::*;
use ast::types::MuType_;
use ast::types::MuType;
13 14 15 16
use ast::ir::*;
use compiler::backend::Word;
use compiler::backend::RegGroup;

17 18 19 20
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::ffi::CString;

21 22 23 24 25 26
lazy_static! {
    pub static ref ADDRESS_TYPE : P<MuType> = P(
        MuType::new(ir::new_internal_id(), MuType_::int(64))
    );
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#[link(name="dl")]
extern "C" {
    fn dlopen(filename: *const c_char, flags: isize) -> *const c_void;
    fn dlsym(handle: *const c_void, symbol: *const c_char) -> *const c_void;
}

pub fn resolve_symbol(symbol: String) -> Address {
    use std::ptr;
    
    let rtld_default = unsafe {dlopen(ptr::null(), 0)};
    let ret = unsafe {dlsym(rtld_default, CString::new(symbol.clone()).unwrap().as_ptr())};
    
    if ret == 0 as *const c_void {
        panic!("cannot find symbol {}", symbol);
    }
    
    Address::from_ptr(ret)
}

46
#[derive(Clone, Debug)]
47 48 49 50 51 52 53 54 55 56 57 58 59 60
pub enum ValueLocation {
    Register(RegGroup, MuID),
    Direct(RegGroup, Address),
    Indirect(RegGroup, Address),
    Constant(RegGroup, Word),
    
    Relocatable(RegGroup, MuName)
}

impl ValueLocation {
    pub fn load_value(&self) -> (RegGroup, Word) {
        match self {
            &ValueLocation::Register(_, _)
            | &ValueLocation::Direct(_, _)
61 62 63 64 65
            | &ValueLocation::Indirect(_, _) => unimplemented!(),
            
            &ValueLocation::Constant(group, word) => {
                (group, word)
            }
66 67 68
            &ValueLocation::Relocatable(_, _) => panic!("expect a runtime value")
        }
    }
qinsoon's avatar
qinsoon committed
69
    
70
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
71
    pub fn from_constant(c: Constant) -> ValueLocation {
72 73 74 75 76 77 78
        match c {
            Constant::Int(int_val) => ValueLocation::Constant(RegGroup::GPR, utils::mem::u64_to_raw(int_val)),
            Constant::Float(f32_val) => ValueLocation::Constant(RegGroup::FPR, utils::mem::f32_to_raw(f32_val)),
            Constant::Double(f64_val) => ValueLocation::Constant(RegGroup::FPR, utils::mem::f64_to_raw(f64_val)),
            
            _ => unimplemented!()
        }
qinsoon's avatar
qinsoon committed
79
    }
qinsoon's avatar
qinsoon committed
80
}