2019-04-27 04:31:04 +00:00
|
|
|
use crate::{
|
|
|
|
backend::RunnableModule,
|
2019-07-27 18:13:13 +00:00
|
|
|
backend::{Backend, CacheGen, Compiler, CompilerConfig, Features, Token},
|
2019-04-27 04:31:04 +00:00
|
|
|
cache::{Artifact, Error as CacheError},
|
|
|
|
error::{CompileError, CompileResult},
|
|
|
|
module::{ModuleInfo, ModuleInner},
|
2019-04-27 08:31:47 +00:00
|
|
|
structures::Map,
|
|
|
|
types::{FuncIndex, FuncSig, SigIndex},
|
2019-04-27 04:31:04 +00:00
|
|
|
};
|
|
|
|
use smallvec::SmallVec;
|
2019-06-05 03:51:33 +00:00
|
|
|
use std::any::Any;
|
2019-06-27 07:49:43 +00:00
|
|
|
use std::collections::HashMap;
|
2019-05-05 18:37:36 +00:00
|
|
|
use std::fmt;
|
2019-04-27 08:31:47 +00:00
|
|
|
use std::fmt::Debug;
|
2019-04-27 04:31:04 +00:00
|
|
|
use std::marker::PhantomData;
|
2019-05-26 00:30:44 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-05-25 23:06:41 +00:00
|
|
|
use wasmparser::{self, WasmDecoder};
|
2019-04-27 08:31:47 +00:00
|
|
|
use wasmparser::{Operator, Type as WpType};
|
2019-04-27 04:31:04 +00:00
|
|
|
|
2019-07-03 17:45:54 +00:00
|
|
|
pub type BreakpointHandler =
|
|
|
|
Box<Fn(BreakpointInfo) -> Result<(), Box<dyn Any>> + Send + Sync + 'static>;
|
2019-07-03 17:45:06 +00:00
|
|
|
pub type BreakpointMap = Arc<HashMap<usize, BreakpointHandler>>;
|
2019-06-27 07:49:43 +00:00
|
|
|
|
2019-05-03 05:14:25 +00:00
|
|
|
#[derive(Debug)]
|
2019-04-27 04:31:04 +00:00
|
|
|
pub enum Event<'a, 'b> {
|
|
|
|
Internal(InternalEvent),
|
|
|
|
Wasm(&'b Operator<'a>),
|
2019-05-16 17:10:21 +00:00
|
|
|
WasmOwned(Operator<'a>),
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum InternalEvent {
|
2019-04-27 08:31:47 +00:00
|
|
|
FunctionBegin(u32),
|
2019-04-27 04:31:04 +00:00
|
|
|
FunctionEnd,
|
2019-07-03 17:45:06 +00:00
|
|
|
Breakpoint(BreakpointHandler),
|
2019-04-27 08:31:47 +00:00
|
|
|
SetInternal(u32),
|
|
|
|
GetInternal(u32),
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 05:14:25 +00:00
|
|
|
impl fmt::Debug for InternalEvent {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
InternalEvent::FunctionBegin(_) => write!(f, "FunctionBegin"),
|
|
|
|
InternalEvent::FunctionEnd => write!(f, "FunctionEnd"),
|
|
|
|
InternalEvent::Breakpoint(_) => write!(f, "Breakpoint"),
|
|
|
|
InternalEvent::SetInternal(_) => write!(f, "SetInternal"),
|
|
|
|
InternalEvent::GetInternal(_) => write!(f, "GetInternal"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:45:06 +00:00
|
|
|
pub struct BreakpointInfo<'a> {
|
2019-06-27 07:49:43 +00:00
|
|
|
pub fault: Option<&'a dyn Any>,
|
2019-05-16 17:10:21 +00:00
|
|
|
}
|
2019-04-27 08:31:47 +00:00
|
|
|
|
2019-04-27 04:31:04 +00:00
|
|
|
pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule, E: Debug> {
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Creates a new module code generator.
|
2019-04-27 04:31:04 +00:00
|
|
|
fn new() -> Self;
|
2019-05-14 08:13:42 +00:00
|
|
|
|
|
|
|
/// Returns the backend id associated with this MCG.
|
2019-04-27 04:31:04 +00:00
|
|
|
fn backend_id() -> Backend;
|
|
|
|
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Feeds the compiler config.
|
|
|
|
fn feed_compiler_config(&mut self, _config: &CompilerConfig) -> Result<(), E> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
/// Adds an import function.
|
|
|
|
fn feed_import_function(&mut self) -> Result<(), E>;
|
2019-04-27 04:31:04 +00:00
|
|
|
|
|
|
|
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>;
|
|
|
|
/// Sets function signatures.
|
2019-04-27 08:31:47 +00:00
|
|
|
fn feed_function_signatures(&mut self, assoc: Map<FuncIndex, SigIndex>) -> Result<(), E>;
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Checks the precondition for a module.
|
|
|
|
fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
|
|
|
|
/// Creates a new function and returns the function-scope code generator for it.
|
2019-05-26 17:04:45 +00:00
|
|
|
fn next_function(&mut self, module_info: Arc<RwLock<ModuleInfo>>) -> Result<&mut FCG, E>;
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Finalizes this module.
|
|
|
|
fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>;
|
2019-04-27 04:31:04 +00:00
|
|
|
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Creates a module from cache.
|
2019-05-06 01:11:47 +00:00
|
|
|
unsafe fn from_cache(cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StreamingCompiler<
|
|
|
|
MCG: ModuleCodeGenerator<FCG, RM, E>,
|
|
|
|
FCG: FunctionCodeGenerator<E>,
|
|
|
|
RM: RunnableModule + 'static,
|
|
|
|
E: Debug,
|
|
|
|
CGEN: Fn() -> MiddlewareChain,
|
|
|
|
> {
|
|
|
|
middleware_chain_generator: CGEN,
|
|
|
|
_phantom_mcg: PhantomData<MCG>,
|
|
|
|
_phantom_fcg: PhantomData<FCG>,
|
|
|
|
_phantom_rm: PhantomData<RM>,
|
|
|
|
_phantom_e: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SimpleStreamingCompilerGen<
|
|
|
|
MCG: ModuleCodeGenerator<FCG, RM, E>,
|
|
|
|
FCG: FunctionCodeGenerator<E>,
|
|
|
|
RM: RunnableModule + 'static,
|
|
|
|
E: Debug,
|
|
|
|
> {
|
|
|
|
_phantom_mcg: PhantomData<MCG>,
|
|
|
|
_phantom_fcg: PhantomData<FCG>,
|
|
|
|
_phantom_rm: PhantomData<RM>,
|
|
|
|
_phantom_e: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2019-04-27 08:31:47 +00:00
|
|
|
MCG: ModuleCodeGenerator<FCG, RM, E>,
|
|
|
|
FCG: FunctionCodeGenerator<E>,
|
|
|
|
RM: RunnableModule + 'static,
|
|
|
|
E: Debug,
|
|
|
|
> SimpleStreamingCompilerGen<MCG, FCG, RM, E>
|
|
|
|
{
|
2019-04-27 04:31:04 +00:00
|
|
|
pub fn new() -> StreamingCompiler<MCG, FCG, RM, E, impl Fn() -> MiddlewareChain> {
|
|
|
|
StreamingCompiler::new(|| MiddlewareChain::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2019-04-27 08:31:47 +00:00
|
|
|
MCG: ModuleCodeGenerator<FCG, RM, E>,
|
|
|
|
FCG: FunctionCodeGenerator<E>,
|
|
|
|
RM: RunnableModule + 'static,
|
|
|
|
E: Debug,
|
|
|
|
CGEN: Fn() -> MiddlewareChain,
|
|
|
|
> StreamingCompiler<MCG, FCG, RM, E, CGEN>
|
|
|
|
{
|
2019-04-27 04:31:04 +00:00
|
|
|
pub fn new(chain_gen: CGEN) -> Self {
|
|
|
|
Self {
|
|
|
|
middleware_chain_generator: chain_gen,
|
|
|
|
_phantom_mcg: PhantomData,
|
|
|
|
_phantom_fcg: PhantomData,
|
|
|
|
_phantom_rm: PhantomData,
|
|
|
|
_phantom_e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 18:13:13 +00:00
|
|
|
pub fn validating_parser_config(features: &Features) -> wasmparser::ValidatingParserConfig {
|
2019-06-20 21:51:31 +00:00
|
|
|
wasmparser::ValidatingParserConfig {
|
|
|
|
operator_config: wasmparser::OperatorValidatorConfig {
|
|
|
|
enable_threads: false,
|
|
|
|
enable_reference_types: false,
|
2019-07-27 18:13:13 +00:00
|
|
|
enable_simd: features.simd,
|
2019-06-20 21:51:31 +00:00
|
|
|
enable_bulk_memory: false,
|
|
|
|
enable_multi_value: false,
|
|
|
|
},
|
2019-08-02 21:16:39 +00:00
|
|
|
mutable_global_imports: true,
|
2019-06-20 21:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:01:16 +00:00
|
|
|
fn validate_with_features(bytes: &[u8], features: &Features) -> CompileResult<()> {
|
2019-06-20 21:51:31 +00:00
|
|
|
let mut parser =
|
2019-07-27 18:13:13 +00:00
|
|
|
wasmparser::ValidatingParser::new(bytes, Some(validating_parser_config(features)));
|
2019-05-25 23:06:41 +00:00
|
|
|
loop {
|
|
|
|
let state = parser.read();
|
|
|
|
match *state {
|
|
|
|
wasmparser::ParserState::EndWasm => break Ok(()),
|
|
|
|
wasmparser::ParserState::Error(err) => Err(CompileError::ValidationError {
|
|
|
|
msg: err.message.to_string(),
|
|
|
|
})?,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 04:31:04 +00:00
|
|
|
impl<
|
2019-04-27 08:31:47 +00:00
|
|
|
MCG: ModuleCodeGenerator<FCG, RM, E>,
|
|
|
|
FCG: FunctionCodeGenerator<E>,
|
|
|
|
RM: RunnableModule + 'static,
|
|
|
|
E: Debug,
|
|
|
|
CGEN: Fn() -> MiddlewareChain,
|
|
|
|
> Compiler for StreamingCompiler<MCG, FCG, RM, E, CGEN>
|
|
|
|
{
|
2019-04-27 04:31:04 +00:00
|
|
|
fn compile(
|
|
|
|
&self,
|
|
|
|
wasm: &[u8],
|
|
|
|
compiler_config: CompilerConfig,
|
|
|
|
_: Token,
|
|
|
|
) -> CompileResult<ModuleInner> {
|
2019-05-27 03:44:37 +00:00
|
|
|
if requires_pre_validation(MCG::backend_id()) {
|
2019-07-29 19:01:16 +00:00
|
|
|
validate_with_features(wasm, &compiler_config.features)?;
|
2019-05-25 23:06:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 04:31:04 +00:00
|
|
|
let mut mcg = MCG::new();
|
|
|
|
let mut chain = (self.middleware_chain_generator)();
|
2019-04-27 08:31:47 +00:00
|
|
|
let info = crate::parse::read_module(
|
|
|
|
wasm,
|
|
|
|
MCG::backend_id(),
|
|
|
|
&mut mcg,
|
|
|
|
&mut chain,
|
|
|
|
&compiler_config,
|
|
|
|
)?;
|
2019-05-06 01:11:47 +00:00
|
|
|
let (exec_context, cache_gen) =
|
2019-05-26 00:30:44 +00:00
|
|
|
mcg.finalize(&info.read().unwrap())
|
2019-05-06 01:11:47 +00:00
|
|
|
.map_err(|x| CompileError::InternalError {
|
|
|
|
msg: format!("{:?}", x),
|
|
|
|
})?;
|
2019-04-27 04:31:04 +00:00
|
|
|
Ok(ModuleInner {
|
2019-05-06 01:11:47 +00:00
|
|
|
cache_gen,
|
2019-04-27 04:31:04 +00:00
|
|
|
runnable_module: Box::new(exec_context),
|
2019-05-26 00:30:44 +00:00
|
|
|
info: Arc::try_unwrap(info).unwrap().into_inner().unwrap(),
|
2019-04-27 04:31:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-06 01:11:47 +00:00
|
|
|
unsafe fn from_cache(
|
|
|
|
&self,
|
|
|
|
artifact: Artifact,
|
|
|
|
token: Token,
|
|
|
|
) -> Result<ModuleInner, CacheError> {
|
|
|
|
MCG::from_cache(artifact, token)
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 03:44:37 +00:00
|
|
|
fn requires_pre_validation(backend: Backend) -> bool {
|
|
|
|
match backend {
|
|
|
|
Backend::Cranelift => true,
|
|
|
|
Backend::LLVM => false,
|
|
|
|
Backend::Singlepass => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 04:31:04 +00:00
|
|
|
pub struct EventSink<'a, 'b> {
|
2019-04-27 08:31:47 +00:00
|
|
|
buffer: SmallVec<[Event<'a, 'b>; 2]>,
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> EventSink<'a, 'b> {
|
|
|
|
pub fn push(&mut self, ev: Event<'a, 'b>) {
|
|
|
|
self.buffer.push(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MiddlewareChain {
|
|
|
|
chain: Vec<Box<GenericFunctionMiddleware>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MiddlewareChain {
|
|
|
|
pub fn new() -> MiddlewareChain {
|
2019-04-27 08:31:47 +00:00
|
|
|
MiddlewareChain { chain: vec![] }
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push<M: FunctionMiddleware + 'static>(&mut self, m: M) {
|
|
|
|
self.chain.push(Box::new(m));
|
|
|
|
}
|
|
|
|
|
2019-04-27 08:31:47 +00:00
|
|
|
pub(crate) fn run<E: Debug, FCG: FunctionCodeGenerator<E>>(
|
|
|
|
&mut self,
|
|
|
|
fcg: Option<&mut FCG>,
|
|
|
|
ev: Event,
|
|
|
|
module_info: &ModuleInfo,
|
|
|
|
) -> Result<(), String> {
|
2019-04-27 04:31:04 +00:00
|
|
|
let mut sink = EventSink {
|
|
|
|
buffer: SmallVec::new(),
|
|
|
|
};
|
|
|
|
sink.push(ev);
|
|
|
|
for m in &mut self.chain {
|
|
|
|
let prev: SmallVec<[Event; 2]> = sink.buffer.drain().collect();
|
|
|
|
for ev in prev {
|
|
|
|
m.feed_event(ev, module_info, &mut sink)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(fcg) = fcg {
|
|
|
|
for ev in sink.buffer {
|
2019-04-27 08:31:47 +00:00
|
|
|
fcg.feed_event(ev, module_info)
|
|
|
|
.map_err(|x| format!("{:?}", x))?;
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FunctionMiddleware {
|
|
|
|
type Error: Debug;
|
2019-04-27 08:31:47 +00:00
|
|
|
fn feed_event<'a, 'b: 'a>(
|
2019-04-27 04:31:04 +00:00
|
|
|
&mut self,
|
2019-04-27 08:31:47 +00:00
|
|
|
op: Event<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
module_info: &ModuleInfo,
|
2019-04-27 08:31:47 +00:00
|
|
|
sink: &mut EventSink<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
) -> Result<(), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait GenericFunctionMiddleware {
|
2019-04-27 08:31:47 +00:00
|
|
|
fn feed_event<'a, 'b: 'a>(
|
2019-04-27 04:31:04 +00:00
|
|
|
&mut self,
|
2019-04-27 08:31:47 +00:00
|
|
|
op: Event<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
module_info: &ModuleInfo,
|
2019-04-27 08:31:47 +00:00
|
|
|
sink: &mut EventSink<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
) -> Result<(), String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Debug, T: FunctionMiddleware<Error = E>> GenericFunctionMiddleware for T {
|
2019-04-27 08:31:47 +00:00
|
|
|
fn feed_event<'a, 'b: 'a>(
|
2019-04-27 04:31:04 +00:00
|
|
|
&mut self,
|
2019-04-27 08:31:47 +00:00
|
|
|
op: Event<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
module_info: &ModuleInfo,
|
2019-04-27 08:31:47 +00:00
|
|
|
sink: &mut EventSink<'a, 'b>,
|
2019-04-27 04:31:04 +00:00
|
|
|
) -> Result<(), String> {
|
2019-04-27 08:31:47 +00:00
|
|
|
<Self as FunctionMiddleware>::feed_event(self, op, module_info, sink)
|
|
|
|
.map_err(|x| format!("{:?}", x))
|
2019-04-27 04:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The function-scope code generator trait.
|
|
|
|
pub trait FunctionCodeGenerator<E: Debug> {
|
|
|
|
/// Sets the return type.
|
|
|
|
fn feed_return(&mut self, ty: WpType) -> Result<(), E>;
|
|
|
|
|
|
|
|
/// Adds a parameter to the function.
|
|
|
|
fn feed_param(&mut self, ty: WpType) -> Result<(), E>;
|
|
|
|
|
|
|
|
/// Adds `n` locals to the function.
|
|
|
|
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), E>;
|
|
|
|
|
|
|
|
/// Called before the first call to `feed_opcode`.
|
2019-05-03 05:14:25 +00:00
|
|
|
fn begin_body(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
|
2019-04-27 04:31:04 +00:00
|
|
|
|
|
|
|
/// Called for each operator.
|
|
|
|
fn feed_event(&mut self, op: Event, module_info: &ModuleInfo) -> Result<(), E>;
|
|
|
|
|
|
|
|
/// Finalizes the function.
|
|
|
|
fn finalize(&mut self) -> Result<(), E>;
|
|
|
|
}
|