wasmer/lib/clif-backend/src/lib.rs

74 lines
1.8 KiB
Rust
Raw Normal View History

2019-01-17 01:59:12 +00:00
// pub mod codegen;
2019-01-18 22:30:15 +00:00
mod call;
2019-01-17 01:59:12 +00:00
mod func_env;
mod libcalls;
2019-01-17 01:59:12 +00:00
mod module;
mod module_env;
mod relocation;
mod resolver;
2019-01-09 02:57:28 +00:00
use cranelift_codegen::{
isa,
settings::{self, Configurable},
};
use target_lexicon::Triple;
use wasmer_runtime::{
2019-01-18 20:13:01 +00:00
backend::{Compiler, Token},
error::{CompileError, CompileResult},
module::ModuleInner,
};
use wasmparser::{self, WasmDecoder};
pub struct CraneliftCompiler {}
impl CraneliftCompiler {
pub fn new() -> Self {
Self {}
}
}
impl Compiler for CraneliftCompiler {
2019-01-13 03:02:19 +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> {
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);
let func_bodies = module_env.translate(wasm)?;
2019-01-17 01:59:12 +00:00
module.compile(&*isa, func_bodies)
}
}
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)
}
fn validate(bytes: &[u8]) -> CompileResult<()> {
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(()),
wasmparser::ParserState::Error(err) => Err(CompileError::ValidationError {
msg: err.message.to_string(),
})?,
2019-01-17 01:59:12 +00:00
_ => {}
}
}
2019-01-09 02:57:28 +00:00
}