2019-02-07 00:26:45 +00:00
|
|
|
use crate::relocation::{ExternalRelocation, TrapSink};
|
|
|
|
|
|
|
|
use hashbrown::HashMap;
|
2019-02-21 00:41:41 +00:00
|
|
|
use std::sync::Arc;
|
2019-02-07 00:26:45 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-02-21 00:41:41 +00:00
|
|
|
backend::{sys::Memory, CacheGen},
|
2019-02-22 01:06:49 +00:00
|
|
|
cache::{Artifact, Error},
|
2019-04-21 01:37:39 +00:00
|
|
|
module::ModuleInfo,
|
2019-02-07 00:26:45 +00:00
|
|
|
structures::Map,
|
|
|
|
types::{LocalFuncIndex, SigIndex},
|
|
|
|
};
|
|
|
|
|
|
|
|
use serde_bench::{deserialize, serialize};
|
|
|
|
|
2019-02-19 17:58:01 +00:00
|
|
|
pub struct CacheGenerator {
|
|
|
|
backend_cache: BackendCache,
|
|
|
|
memory: Arc<Memory>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheGenerator {
|
|
|
|
pub fn new(backend_cache: BackendCache, memory: Arc<Memory>) -> Self {
|
2019-02-21 00:41:41 +00:00
|
|
|
Self {
|
|
|
|
backend_cache,
|
|
|
|
memory,
|
|
|
|
}
|
2019-02-19 17:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheGen for CacheGenerator {
|
2019-04-19 20:54:48 +00:00
|
|
|
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), Error> {
|
2019-02-21 00:41:41 +00:00
|
|
|
// Clone the memory to a new location. This could take a long time,
|
|
|
|
// depending on the throughput of your memcpy implementation.
|
|
|
|
let compiled_code = (*self.memory).clone();
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
self.backend_cache.into_backend_data()?.into_boxed_slice(),
|
|
|
|
compiled_code,
|
|
|
|
))
|
2019-02-19 17:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct TrampolineCache {
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
pub code: Vec<u8>,
|
|
|
|
pub offsets: HashMap<SigIndex, usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BackendCache {
|
|
|
|
pub external_relocs: Map<LocalFuncIndex, Box<[ExternalRelocation]>>,
|
|
|
|
pub offsets: Map<LocalFuncIndex, usize>,
|
2019-02-19 17:58:01 +00:00
|
|
|
pub trap_sink: Arc<TrapSink>,
|
2019-02-07 00:26:45 +00:00
|
|
|
pub trampolines: TrampolineCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackendCache {
|
2019-02-22 01:06:49 +00:00
|
|
|
pub fn from_cache(cache: Artifact) -> Result<(ModuleInfo, Memory, Self), Error> {
|
2019-02-21 00:41:41 +00:00
|
|
|
let (info, backend_data, compiled_code) = cache.consume();
|
2019-02-19 23:36:22 +00:00
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
let backend_cache =
|
|
|
|
deserialize(&backend_data).map_err(|e| Error::DeserializeError(e.to_string()))?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
Ok((info, compiled_code, backend_cache))
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:36:22 +00:00
|
|
|
pub fn into_backend_data(&self) -> Result<Vec<u8>, Error> {
|
2019-02-07 00:26:45 +00:00
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
2019-02-19 23:36:22 +00:00
|
|
|
serialize(&mut buffer, self).map_err(|e| Error::SerializeError(e.to_string()))?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
}
|