Commit e9483b22 authored by John Zhang's avatar John Zhang

adding `compile_to_sharedlib` API call for testing JIT

parent a9b0e905
......@@ -243,6 +243,16 @@ extern fn _forwarder__MuVM__set_trap_handler(mvm: *mut CMuVM, trap_handler: CMuT
};
}
extern fn _forwarder__MuVM__compile_to_sharedlib(mvm: *mut CMuVM, fncname: CMuCString) -> CMuCString {
let mut _arg_mvm = from_MuVM_ptr(mvm);
let mut _arg_fncname = from_MuCString(fncname);
let _rv = unsafe {
(*_arg_mvm).compile_to_sharedlib(_arg_fncname)
};
let _rv_prep = _rv;
_rv_prep
}
extern fn _forwarder__MuCtx__id_of(ctx: *mut CMuCtx, name: CMuName) -> CMuID {
let mut _arg_ctx = from_MuCtx_ptr(ctx);
let mut _arg_name = from_MuName(name);
......@@ -2037,6 +2047,7 @@ pub fn make_new_MuVM(header: *mut c_void) -> *mut CMuVM {
id_of: _forwarder__MuVM__id_of,
name_of: _forwarder__MuVM__name_of,
set_trap_handler: _forwarder__MuVM__set_trap_handler,
compile_to_sharedlib: _forwarder__MuVM__compile_to_sharedlib,
});
Box::into_raw(bx)
......
......@@ -109,6 +109,8 @@ pub struct CMuVM {
pub id_of: extern fn(*mut CMuVM, CMuName) -> CMuID,
pub name_of: extern fn(*mut CMuVM, CMuID) -> CMuName,
pub set_trap_handler: extern fn(*mut CMuVM, CMuTrapHandler, CMuCPtr),
// this function is only used in testing jit
pub compile_to_sharedlib: extern fn(*mut CMuVM, CMuCString) -> CMuCString,
}
#[repr(C)]
......
use super::common::*;
use std::sync::Arc;
pub struct MuVM {
// The actual VM
pub vm: VM,
pub vm: Arc<VM>,
// Cache C strings. The C client expects `char*` from `name_of`. We assume the client won't
// call `name_of` very often, so that we don't need to initialise this hashmap on startup.
......@@ -19,7 +20,7 @@ impl MuVM {
*/
pub fn new() -> MuVM {
MuVM {
vm: VM::new(),
vm: Arc::new(VM::new()),
// Cache C strings. The C client expects `char*` from `name_of`. We assume the client
// won't call `name_of` very often, so that we don't need to initialise this hashmap on
// startup.
......@@ -69,6 +70,27 @@ impl MuVM {
panic!("Not implemented")
}
pub fn compile_to_sharedlib(&self, fnc_name: String) -> CMuCString {
extern crate libloading as ll;
use compiler::*;
use testutil::aot;
let compiler = Compiler::new(CompilerPolicy::default(), self.vm.clone());
let fnc_id = self.vm.id_of(&fnc_name);
{
let funcs = self.vm.funcs().read().unwrap();
let func = funcs.get(&fnc_id).unwrap().read().unwrap();
let func_vers = self.vm.func_vers().read().unwrap();
let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
compiler.compile(&mut func_ver);
}
backend::emit_context(&self.vm);
let libname = &format!("lib{}.dylib", fnc_name);
let dylib = aot::link_dylib(vec![fnc_name], libname);
dylib.to_str().unwrap().as_ptr() as CMuCString
}
}
/**
......
......@@ -270,6 +270,8 @@ struct MuVM {
// Set handlers
void (*set_trap_handler)(MuVM *mvm, MuTrapHandler trap_handler, MuCPtr userdata);
MuCString (*compile_to_sharedlib)(MuVM *mvm, MuCString fncname);
};
// A local context. It can only be used by one thread at a time. It holds many
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment