mod.rs 8.65 KB
Newer Older
1
use utils;
2
use ast::ir::*;
3
use vm::VM;
4 5
use compiler::backend::Word;
use compiler::backend::RegGroup;
qinsoon's avatar
qinsoon committed
6
use utils::Address;
7

8
use std::fmt;
qinsoon's avatar
qinsoon committed
9
use std::os::raw::c_int;
10 11 12
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::ffi::CString;
13 14
use std::ffi::CStr;
use std::sync::Arc;
15

16
pub mod mm;
qinsoon's avatar
qinsoon committed
17
pub mod thread;
qinsoon's avatar
qinsoon committed
18
pub mod math;
qinsoon's avatar
qinsoon committed
19
pub mod entrypoints;
qinsoon's avatar
qinsoon committed
20 21 22

#[cfg(target_arch = "x86_64")]
#[path = "exception_x64.rs"]
23
pub mod exception;
qinsoon's avatar
qinsoon committed
24

25 26 27
// consider using libloading crate instead of the raw c functions for dynalic libraries
// however i am not sure if libloading can load symbols from current process (not from an actual dylib)
// so here i use dlopen/dlsym from C
28 29 30 31
#[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;
32
    fn dlerror() -> *const c_char;
33 34 35 36
}

pub fn resolve_symbol(symbol: String) -> Address {
    use std::ptr;
37 38

    let symbol = MuEntityHeader::name_check(symbol);
39 40 41
    
    let rtld_default = unsafe {dlopen(ptr::null(), 0)};
    let ret = unsafe {dlsym(rtld_default, CString::new(symbol.clone()).unwrap().as_ptr())};
42 43 44 45

    let error = unsafe {dlerror()};
    if !error.is_null() {
        let cstr = unsafe {CStr::from_ptr(error)};
46 47
        error!("cannot find symbol: {}", symbol);
        error!("{}", cstr.to_str().unwrap());
48 49

        panic!("failed to resolve symbol");
50 51 52 53 54
    }
    
    Address::from_ptr(ret)
}

qinsoon's avatar
qinsoon committed
55 56
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

qinsoon's avatar
qinsoon committed
57
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
58
pub enum ValueLocation {
qinsoon's avatar
qinsoon committed
59 60 61
    Register(RegGroup, MuID),     // 0
    Constant(RegGroup, Word),     // 1    
    Relocatable(RegGroup, MuName),// 2
62
    
qinsoon's avatar
qinsoon committed
63 64 65 66
    Direct(RegGroup, Address),    // 3
    Indirect(RegGroup, Address),  // 4
}

67 68 69 70 71 72 73 74 75 76 77 78
impl fmt::Display for ValueLocation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &ValueLocation::Register(_, id) => write!(f, "VL_Reg: {}", id),
            &ValueLocation::Constant(_, val) => write!(f, "VL_Const: {}", val),
            &ValueLocation::Relocatable(_, ref name) => write!(f, "VL_Reloc: {}", name),
            &ValueLocation::Direct(_, addr) => write!(f, "VL_Direct: 0x{:x}", addr),
            &ValueLocation::Indirect(_, addr) => write!(f, "VL_Indirect: 0x{:x}", addr)
        }
    }
}

qinsoon's avatar
qinsoon committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
impl Encodable for ValueLocation {
    fn encode<S: Encoder> (&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_enum("ValueLocation", |s| {
            match self {
                &ValueLocation::Register(grp, id) => {
                    s.emit_enum_variant("Register", 0, 2, |s| {
                        try!(s.emit_enum_variant_arg(0, |s| grp.encode(s)));
                        try!(s.emit_enum_variant_arg(1, |s| id.encode(s)));
                        Ok(())
                    })
                }
                &ValueLocation::Constant(grp, val) => {
                    s.emit_enum_variant("Constant", 1, 2, |s| {
                        try!(s.emit_enum_variant_arg(0, |s| grp.encode(s)));
                        try!(s.emit_enum_variant_arg(1, |s| val.encode(s)));
                        Ok(())
                    })    
                }                
                &ValueLocation::Relocatable(grp, ref name) => {
                    s.emit_enum_variant("Relocatable", 2, 2, |s| {
                        try!(s.emit_enum_variant_arg(0, |s| grp.encode(s)));
                        try!(s.emit_enum_variant_arg(1, |s| name.encode(s)));
                        Ok(())
                    })
                }
                &ValueLocation::Direct(_, _)
                | &ValueLocation::Indirect(_, _) => {
                    panic!("trying to encode an address location (not persistent)")
                }
            }
        })
    }
}

impl Decodable for ValueLocation {
    fn decode<D: Decoder>(d: &mut D) -> Result<ValueLocation, D::Error> {
        d.read_enum("ValueLocation", |d| {
            d.read_enum_variant(
                &vec!["Register", "Constant", "Relocatable"],
                |d, idx| {
                    match idx {
                        0 => {
                            // Register variant
                            let grp = try!(d.read_enum_variant_arg(0, |d| Decodable::decode(d)));
                            let id = try!(d.read_enum_variant_arg(1, |d| Decodable::decode(d)));
                            
                            Ok(ValueLocation::Register(grp, id))
                        }
                        1 => {
                            // Constant
                            let grp = try!(d.read_enum_variant_arg(0, |d| Decodable::decode(d)));
                            let val = try!(d.read_enum_variant_arg(1, |d| Decodable::decode(d)));
                            Ok(ValueLocation::Constant(grp, val))
                        }
                        2 => {
                            // Relocatable
                            let grp = try!(d.read_enum_variant_arg(0, |d| Decodable::decode(d)));
                            let name = try!(d.read_enum_variant_arg(1, |d| Decodable::decode(d)));
                            Ok(ValueLocation::Relocatable(grp, name))
                        }
                        _ => panic!("unexpected enum variant for ValueLocation: {}", idx)
                    }
                }
             ) 
        })
    }
145 146 147 148 149 150 151
}

impl ValueLocation {
    pub fn load_value(&self) -> (RegGroup, Word) {
        match self {
            &ValueLocation::Register(_, _)
            | &ValueLocation::Direct(_, _)
152 153 154 155 156
            | &ValueLocation::Indirect(_, _) => unimplemented!(),
            
            &ValueLocation::Constant(group, word) => {
                (group, word)
            }
157 158 159
            &ValueLocation::Relocatable(_, _) => panic!("expect a runtime value")
        }
    }
qinsoon's avatar
qinsoon committed
160
    
161
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
162
    pub fn from_constant(c: Constant) -> ValueLocation {
163 164 165 166 167 168 169
        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
170
    }
qinsoon's avatar
qinsoon committed
171 172 173 174 175 176 177 178 179 180
    
    pub fn to_address(&self) -> Address {
        match self {
            &ValueLocation::Register(_, _)
            | &ValueLocation::Constant(_, _) => panic!("a register/constant cannot be turned into address"),
            &ValueLocation::Direct(_, addr) => addr, 
            &ValueLocation::Indirect(_, addr) => unsafe {addr.load::<Address>()},
            &ValueLocation::Relocatable(_, ref symbol) => resolve_symbol(symbol.clone())
        }
    }
181 182 183 184 185 186 187

    pub fn to_relocatable(&self) -> MuName {
        match self {
            &ValueLocation::Relocatable(_, ref name) => name.clone(),
            _ => panic!("expecting Relocatable location, found {}", self)
        }
    }
188 189
}

190 191
pub const PRIMORDIAL_ENTRY : &'static str = "src/runtime/main.c";

192 193
#[no_mangle]
pub extern fn mu_trace_level_log() {
qinsoon's avatar
qinsoon committed
194
    VM::start_logging_trace();
195 196 197
}

#[no_mangle]
qinsoon's avatar
qinsoon committed
198
pub extern fn mu_main(serialized_vm : *const c_char, argc: c_int, argv: *const *const c_char) {
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    debug!("mu_main() started...");
    
    let str_vm = unsafe{CStr::from_ptr(serialized_vm)}.to_str().unwrap();
    
    let vm : Arc<VM> = Arc::new(VM::resume_vm(str_vm));
    
    let primordial = vm.primordial.read().unwrap();
    if primordial.is_none() {
        panic!("no primordial thread/stack/function. Client should provide an entry point");
    } else {
        let primordial = primordial.as_ref().unwrap();
        
        // create mu stack
        let stack = vm.new_stack(primordial.func_id);
qinsoon's avatar
qinsoon committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

        // if the primordial named some const arguments, we use the const args
        // otherwise we push 'argc' and 'argv' to new stack
        let args : Vec<ValueLocation> = if primordial.has_const_args {
            primordial.args.iter().map(|arg| ValueLocation::from_constant(arg.clone())).collect()
        } else {
            let mut args = vec![];

            // 1st arg: argc
            args.push(ValueLocation::from_constant(Constant::Int(argc as u64)));

            // 2nd arg: argv
            args.push(ValueLocation::from_constant(Constant::Int(argv as u64)));

            args
        };
229 230 231
        
        // FIXME: currently assumes no user defined thread local
        // will need to fix this after we can serialize heap object
232
        let thread = thread::MuThread::new_thread_normal(stack, unsafe{Address::zero()}, args, vm.clone());
233 234 235
        
        thread.join().unwrap();
    }
236
}
237 238 239 240 241 242

#[no_mangle]
#[allow(unreachable_code)]
pub extern fn muentry_print_hex(x: u64) {
    println!("0x{:x}", x);
}