2019-02-07 00:26:45 +00:00
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
mod cache;
|
2019-01-17 01:59:12 +00:00
|
|
|
mod func_env;
|
2019-01-08 21:04:03 +00:00
|
|
|
mod libcalls;
|
2019-01-17 01:59:12 +00:00
|
|
|
mod module;
|
|
|
|
mod module_env;
|
2019-01-08 21:04:03 +00:00
|
|
|
mod relocation;
|
|
|
|
mod resolver;
|
2019-02-08 01:08:42 +00:00
|
|
|
mod signal;
|
2019-01-22 23:00:27 +00:00
|
|
|
mod trampoline;
|
2019-01-08 21:04:03 +00:00
|
|
|
|
2019-01-09 02:57:28 +00:00
|
|
|
use cranelift_codegen::{
|
|
|
|
isa,
|
|
|
|
settings::{self, Configurable},
|
|
|
|
};
|
2019-01-08 21:04:03 +00:00
|
|
|
use target_lexicon::Triple;
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
use wasmer_runtime_core::{
|
|
|
|
backend::sys::Memory,
|
|
|
|
cache::{Cache, Error as CacheError},
|
|
|
|
module::ModuleInfo,
|
|
|
|
};
|
2019-01-22 19:02:06 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-01-18 20:13:01 +00:00
|
|
|
backend::{Compiler, Token},
|
2019-01-18 17:17:44 +00:00
|
|
|
error::{CompileError, CompileResult},
|
|
|
|
module::ModuleInner,
|
|
|
|
};
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
extern crate serde;
|
|
|
|
|
2019-01-08 21:04:03 +00:00
|
|
|
use wasmparser::{self, WasmDecoder};
|
|
|
|
|
|
|
|
pub struct CraneliftCompiler {}
|
|
|
|
|
|
|
|
impl CraneliftCompiler {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler for CraneliftCompiler {
|
2019-02-07 00:26:45 +00:00
|
|
|
/// Compiles wasm binary to a wasmer module.
|
2019-01-18 20:13:01 +00:00
|
|
|
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
|
2019-01-08 21:04:03 +00:00
|
|
|
validate(wasm)?;
|
|
|
|
|
|
|
|
let isa = get_isa();
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
let mut module = module::Module::empty();
|
|
|
|
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
let func_bodies = module_env.translate(wasm)?;
|
2019-01-08 21:04:03 +00:00
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
module.compile(&*isa, func_bodies)
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
/// Create a wasmer Module from an already-compiled cache.
|
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
unsafe fn from_cache(&self, cache: Cache, _: Token) -> Result<ModuleInner, CacheError> {
|
|
|
|
module::Module::from_cache(cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "cache")]
|
|
|
|
fn compile_to_backend_cache_data(
|
|
|
|
&self,
|
|
|
|
wasm: &[u8],
|
|
|
|
_: Token,
|
|
|
|
) -> CompileResult<(Box<ModuleInfo>, Vec<u8>, Memory)> {
|
|
|
|
validate(wasm)?;
|
|
|
|
|
|
|
|
let isa = get_isa();
|
|
|
|
|
|
|
|
let mut module = module::Module::empty();
|
|
|
|
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);
|
|
|
|
|
|
|
|
let func_bodies = module_env.translate(wasm)?;
|
|
|
|
|
|
|
|
let (info, backend_cache, compiled_code) = module
|
|
|
|
.compile_to_backend_cache(&*isa, func_bodies)
|
|
|
|
.map_err(|e| CompileError::InternalError {
|
|
|
|
msg: format!("{:?}", e),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let buffer =
|
|
|
|
backend_cache
|
|
|
|
.into_backend_data()
|
|
|
|
.map_err(|e| CompileError::InternalError {
|
|
|
|
msg: format!("{:?}", e),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok((Box::new(info), buffer, compiled_code))
|
|
|
|
}
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_isa() -> Box<isa::TargetIsa> {
|
|
|
|
let flags = {
|
|
|
|
let mut builder = settings::builder();
|
|
|
|
builder.set("opt_level", "best").unwrap();
|
|
|
|
|
|
|
|
if cfg!(not(test)) {
|
|
|
|
builder.set("enable_verifier", "false").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let flags = settings::Flags::new(builder);
|
|
|
|
debug_assert_eq!(flags.opt_level(), settings::OptLevel::Best);
|
|
|
|
flags
|
|
|
|
};
|
|
|
|
isa::lookup(Triple::host()).unwrap().finish(flags)
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:17:44 +00:00
|
|
|
fn validate(bytes: &[u8]) -> CompileResult<()> {
|
2019-01-08 21:04:03 +00:00
|
|
|
let mut parser = wasmparser::ValidatingParser::new(bytes, None);
|
|
|
|
loop {
|
|
|
|
let state = parser.read();
|
|
|
|
match *state {
|
2019-01-17 01:59:12 +00:00
|
|
|
wasmparser::ParserState::EndWasm => break Ok(()),
|
2019-01-18 17:17:44 +00:00
|
|
|
wasmparser::ParserState::Error(err) => Err(CompileError::ValidationError {
|
|
|
|
msg: err.message.to_string(),
|
|
|
|
})?,
|
2019-01-17 01:59:12 +00:00
|
|
|
_ => {}
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-09 02:57:28 +00:00
|
|
|
}
|
2019-01-22 23:26:56 +00:00
|
|
|
|
|
|
|
/// The current version of this crate
|
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|