2019-02-22 17:34:55 -08:00
|
|
|
use inkwell::{
|
|
|
|
execution_engine::JitFunction,
|
|
|
|
targets::{CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine},
|
|
|
|
OptimizationLevel,
|
|
|
|
};
|
2019-02-09 15:53:40 -08:00
|
|
|
use wasmer_runtime_core::{
|
|
|
|
backend::{Compiler, Token},
|
2019-02-22 17:34:55 -08:00
|
|
|
cache::{Artifact, Error as CacheError},
|
2019-02-09 15:53:40 -08:00
|
|
|
error::CompileError,
|
2019-02-11 19:34:04 -08:00
|
|
|
module::ModuleInner,
|
2019-02-09 15:53:40 -08:00
|
|
|
};
|
|
|
|
|
2019-02-25 18:07:22 -08:00
|
|
|
mod backend;
|
2019-02-09 15:53:40 -08:00
|
|
|
mod code;
|
|
|
|
mod intrinsics;
|
|
|
|
mod read_info;
|
|
|
|
mod state;
|
|
|
|
|
|
|
|
pub struct LLVMCompiler {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LLVMCompiler {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { _private: () }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler for LLVMCompiler {
|
|
|
|
fn compile(&self, wasm: &[u8], _: Token) -> Result<ModuleInner, CompileError> {
|
2019-02-25 18:07:22 -08:00
|
|
|
let (info, code_reader) = read_info::read_module(wasm).unwrap();
|
|
|
|
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();
|
2019-02-09 15:53:40 -08:00
|
|
|
|
2019-02-25 18:07:22 -08:00
|
|
|
let backend = backend::LLVMBackend::new(module, intrinsics);
|
|
|
|
|
|
|
|
// Create placeholder values here.
|
|
|
|
let (protected_caller, cache_gen) = {
|
|
|
|
use wasmer_runtime_core::backend::{
|
|
|
|
sys::Memory, CacheGen, ProtectedCaller, UserTrapper,
|
|
|
|
};
|
|
|
|
use wasmer_runtime_core::cache::Error as CacheError;
|
|
|
|
use wasmer_runtime_core::error::RuntimeResult;
|
|
|
|
use wasmer_runtime_core::module::ModuleInfo;
|
|
|
|
use wasmer_runtime_core::types::{FuncIndex, Value};
|
|
|
|
use wasmer_runtime_core::vm;
|
|
|
|
struct Placeholder;
|
|
|
|
impl ProtectedCaller for Placeholder {
|
|
|
|
fn call(
|
|
|
|
&self,
|
|
|
|
_module: &ModuleInner,
|
|
|
|
_func_index: FuncIndex,
|
|
|
|
_params: &[Value],
|
|
|
|
_import_backing: &vm::ImportBacking,
|
|
|
|
_vmctx: *mut vm::Ctx,
|
|
|
|
_: Token,
|
|
|
|
) -> RuntimeResult<Vec<Value>> {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl CacheGen for Placeholder {
|
|
|
|
fn generate_cache(
|
|
|
|
&self,
|
|
|
|
module: &ModuleInner,
|
|
|
|
) -> Result<(Box<ModuleInfo>, Box<[u8]>, Memory), CacheError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(Box::new(Placeholder), Box::new(Placeholder))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ModuleInner {
|
|
|
|
func_resolver: Box::new(backend),
|
|
|
|
protected_caller,
|
|
|
|
cache_gen,
|
|
|
|
|
|
|
|
info,
|
|
|
|
})
|
2019-02-09 15:53:40 -08:00
|
|
|
}
|
2019-02-22 17:34:55 -08:00
|
|
|
|
|
|
|
unsafe fn from_cache(&self, _artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
|
2019-02-25 18:07:22 -08:00
|
|
|
unimplemented!("the llvm backend doesn't support caching yet")
|
2019-02-22 17:34:55 -08:00
|
|
|
}
|
2019-02-09 15:53:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_module() {
|
|
|
|
use wabt::wat2wasm;
|
2019-02-22 17:34:55 -08:00
|
|
|
use wasmer_runtime_core::vmcalls;
|
2019-02-14 18:08:20 -08:00
|
|
|
// let wasm = include_bytes!("../../spectests/examples/simple/simple.wasm") as &[u8];
|
2019-02-12 18:02:00 -08:00
|
|
|
let wat = r#"
|
2019-02-09 15:53:40 -08:00
|
|
|
(module
|
|
|
|
(type $t0 (func (param i32) (result i32)))
|
2019-02-12 18:02:00 -08:00
|
|
|
(type $t1 (func (result i32)))
|
2019-02-14 15:13:58 -08:00
|
|
|
(memory 1)
|
2019-02-22 17:34:55 -08:00
|
|
|
(table 10 anyfunc)
|
|
|
|
(elem (i32.const 0) $foobar)
|
2019-02-14 18:08:20 -08:00
|
|
|
(global $g0 (mut i32) (i32.const 0))
|
2019-02-12 18:02:00 -08:00
|
|
|
(func $foo (type $t0) (param i32) (result i32)
|
|
|
|
get_local 0
|
2019-02-22 17:34:55 -08:00
|
|
|
i32.const 0
|
|
|
|
call_indirect (type $t0)
|
2019-02-15 16:02:20 -08:00
|
|
|
)
|
|
|
|
(func $foobar (type $t0)
|
|
|
|
get_local 0
|
2019-02-12 18:02:00 -08:00
|
|
|
))
|
2019-02-09 15:53:40 -08:00
|
|
|
"#;
|
2019-02-12 18:02:00 -08:00
|
|
|
let wasm = wat2wasm(wat).unwrap();
|
2019-02-09 15:53:40 -08:00
|
|
|
|
|
|
|
let (info, code_reader) = read_info::read_module(&wasm).unwrap();
|
|
|
|
|
2019-02-15 16:02:20 -08:00
|
|
|
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();
|
|
|
|
|
2019-02-25 18:07:22 -08:00
|
|
|
// let backend = backend::LLVMBackend::new(module, intrinsics);
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn test_cpp();
|
2019-02-15 16:02:20 -08:00
|
|
|
}
|
|
|
|
|
2019-02-25 18:07:22 -08:00
|
|
|
unsafe { test_cpp() };
|
|
|
|
|
|
|
|
// let exec_engine = module
|
|
|
|
// .create_jit_execution_engine(OptimizationLevel::Default)
|
|
|
|
// .unwrap();
|
|
|
|
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_grow_dynamic_local,
|
|
|
|
// vmcalls::local_dynamic_memory_grow as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_grow_static_local,
|
|
|
|
// vmcalls::local_static_memory_grow as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_grow_dynamic_import,
|
|
|
|
// vmcalls::imported_dynamic_memory_grow as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_grow_static_import,
|
|
|
|
// vmcalls::imported_static_memory_grow as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_size_dynamic_local,
|
|
|
|
// vmcalls::local_dynamic_memory_size as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_size_static_local,
|
|
|
|
// vmcalls::local_static_memory_size as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_size_dynamic_import,
|
|
|
|
// vmcalls::imported_dynamic_memory_size as usize,
|
|
|
|
// );
|
|
|
|
// exec_engine.add_global_mapping(
|
|
|
|
// &intrinsics.memory_size_static_import,
|
|
|
|
// vmcalls::imported_static_memory_size as usize,
|
|
|
|
// );
|
2019-02-15 16:02:20 -08:00
|
|
|
|
|
|
|
// unsafe {
|
|
|
|
// let func: JitFunction<unsafe extern fn(*mut u8, i32) -> i32> = exec_engine.get_function("fn0").unwrap();
|
|
|
|
// let result = func.call(0 as _, 0);
|
|
|
|
// println!("result: {}", result);
|
|
|
|
// }
|
2019-02-09 15:53:40 -08:00
|
|
|
}
|