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

136 lines
4.0 KiB
Rust
Raw Normal View History

2019-02-19 17:58:01 +00:00
use crate::cache::{BackendCache, CacheGenerator};
2019-05-27 03:32:11 +00:00
use crate::{resolver::FuncResolverBuilder, signal::Caller};
2019-05-27 03:32:11 +00:00
use cranelift_codegen::ir;
2019-01-17 01:59:12 +00:00
use cranelift_entity::EntityRef;
use cranelift_wasm;
2019-02-19 17:58:01 +00:00
use std::sync::Arc;
2019-02-22 22:07:03 +00:00
use wasmer_runtime_core::cache::{Artifact, Error as CacheError};
use wasmer_runtime_core::{
2019-05-27 03:32:11 +00:00
module::{ModuleInfo, ModuleInner},
structures::TypedIndex,
types::{FuncIndex, FuncSig, GlobalIndex, MemoryIndex, SigIndex, TableIndex, Type},
2019-01-16 18:26:10 +00:00
};
2019-01-17 01:59:12 +00:00
/// This contains all of the items in a `ModuleInner` except the `func_resolver`.
2019-01-16 18:26:10 +00:00
pub struct Module {
pub info: ModuleInfo,
2019-01-17 01:59:12 +00:00
}
impl Module {
pub fn from_cache(cache: Artifact) -> Result<ModuleInner, CacheError> {
let (info, compiled_code, backend_cache) = BackendCache::from_cache(cache)?;
let (func_resolver_builder, trampolines, handler_data) =
FuncResolverBuilder::new_from_backend_cache(backend_cache, compiled_code, &info)?;
2019-02-21 00:41:41 +00:00
let (func_resolver, backend_cache) = func_resolver_builder
.finalize(
&info.signatures,
Arc::clone(&trampolines),
handler_data.clone(),
)
.map_err(|e| CacheError::Unknown(format!("{:?}", e)))?;
2019-02-21 00:41:41 +00:00
let cache_gen = Box::new(CacheGenerator::new(
backend_cache,
Arc::clone(&func_resolver.memory),
));
2019-04-12 17:27:14 +00:00
let runnable_module = Caller::new(handler_data, trampolines, func_resolver);
Ok(ModuleInner {
runnable_module: Box::new(runnable_module),
2019-02-21 00:41:41 +00:00
cache_gen,
2019-02-19 17:58:01 +00:00
info,
})
}
2019-01-17 01:59:12 +00:00
}
pub struct Converter<T>(pub T);
macro_rules! convert_clif_to_runtime_index {
($clif_index:ident, $runtime_index:ident) => {
impl From<Converter<cranelift_wasm::$clif_index>> for $runtime_index {
fn from(clif_index: Converter<cranelift_wasm::$clif_index>) -> Self {
$runtime_index::new(clif_index.0.index())
}
}
impl From<Converter<$runtime_index>> for cranelift_wasm::$clif_index {
fn from(runtime_index: Converter<$runtime_index>) -> Self {
cranelift_wasm::$clif_index::new(runtime_index.0.index())
}
}
};
($(($clif_index:ident: $runtime_index:ident),)*) => {
$(
convert_clif_to_runtime_index!($clif_index, $runtime_index);
)*
};
}
convert_clif_to_runtime_index![
(FuncIndex: FuncIndex),
(MemoryIndex: MemoryIndex),
(TableIndex: TableIndex),
(GlobalIndex: GlobalIndex),
(SignatureIndex: SigIndex),
];
impl From<Converter<ir::Signature>> for FuncSig {
2019-04-03 05:13:40 +00:00
fn from(signature: Converter<ir::Signature>) -> Self {
2019-01-29 18:16:39 +00:00
FuncSig::new(
signature
2019-01-17 01:59:12 +00:00
.0
.params
.iter()
.map(|param| Converter(param.value_type).into())
2019-01-29 18:16:39 +00:00
.collect::<Vec<_>>(),
signature
2019-01-17 01:59:12 +00:00
.0
.returns
.iter()
.map(|ret| Converter(ret.value_type).into())
2019-01-29 18:16:39 +00:00
.collect::<Vec<_>>(),
)
2019-01-17 01:59:12 +00:00
}
}
2019-01-16 18:26:10 +00:00
2019-01-17 01:59:12 +00:00
impl From<Converter<ir::Type>> for Type {
fn from(ty: Converter<ir::Type>) -> Self {
match ty.0 {
ir::types::I32 => Type::I32,
ir::types::I64 => Type::I64,
ir::types::F32 => Type::F32,
ir::types::F64 => Type::F64,
_ => panic!("unsupported wasm type"),
}
}
}
impl From<Converter<Type>> for ir::Type {
fn from(ty: Converter<Type>) -> Self {
match ty.0 {
Type::I32 => ir::types::I32,
Type::I64 => ir::types::I64,
Type::F32 => ir::types::F32,
Type::F64 => ir::types::F64,
}
}
}
impl From<Converter<Type>> for ir::AbiParam {
fn from(ty: Converter<Type>) -> Self {
match ty.0 {
Type::I32 => ir::AbiParam::new(ir::types::I32),
Type::I64 => ir::AbiParam::new(ir::types::I64),
Type::F32 => ir::AbiParam::new(ir::types::F32),
Type::F64 => ir::AbiParam::new(ir::types::F64),
}
}
}