Skip to content

Commit

Permalink
Middleware for cranelift.
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Jun 15, 2020
1 parent c413101 commit 1144222
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ impl Compiler for CraneliftCompiler {
input.module_offset,
&mut context.func,
&mut func_env,
*i,
&self.config,
)?;

let mut code_buf: Vec<u8> = Vec::new();
Expand Down
15 changes: 14 additions & 1 deletion lib/compiler-cranelift/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::compiler::CraneliftCompiler;
use cranelift_codegen::isa::{lookup, TargetIsa};
use cranelift_codegen::settings::{self, Configurable};
use wasmer_compiler::{Architecture, Compiler, CompilerConfig, CpuFeature, Features, Target};
use std::sync::Arc;
use wasmer_compiler::{
Architecture, Compiler, CompilerConfig, CpuFeature, Features, FunctionMiddlewareGenerator,
Target,
};

// Runtime Environment

Expand Down Expand Up @@ -44,6 +48,9 @@ pub struct CraneliftConfig {

features: Features,
target: Target,

/// The middleware chain.
pub(crate) middlewares: Vec<Arc<dyn FunctionMiddlewareGenerator>>,
}

impl CraneliftConfig {
Expand All @@ -57,6 +64,7 @@ impl CraneliftConfig {
enable_pic: false,
features,
target,
middlewares: vec![],
}
}

Expand Down Expand Up @@ -193,6 +201,11 @@ impl CompilerConfig for CraneliftConfig {
fn compiler(&self) -> Box<dyn Compiler + Send> {
Box::new(CraneliftCompiler::new(&self))
}

/// Pushes a middleware onto the back of the middleware chain.
fn push_middleware(&mut self, middleware: Arc<dyn FunctionMiddlewareGenerator>) {
self.middlewares.push(middleware);
}
}

impl Default for CraneliftConfig {
Expand Down
20 changes: 13 additions & 7 deletions lib/compiler-cranelift/src/translator/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use super::code_translator::{bitcast_arguments, translate_operator, wasm_param_t
use super::func_environ::{FuncEnvironment, ReturnMode};
use super::func_state::FuncTranslationState;
use super::translation_utils::get_vmctx_value_label;
use crate::config::CraneliftConfig;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::{self, Block, InstBuilder, ValueLabel};
use cranelift_codegen::timing;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
use tracing::info;
use wasm_common::LocalFunctionIndex;
use wasmer_compiler::wasmparser;
use wasmer_compiler::{
to_wasm_error, wasm_unsupported, MiddlewareBinaryReader, ModuleTranslationState, WasmResult,
to_wasm_error, wasm_unsupported, GenerateMiddlewareChain, MiddlewareBinaryReader,
ModuleTranslationState, WasmResult,
};

/// WebAssembly to Cranelift IR function translator.
Expand Down Expand Up @@ -62,13 +65,16 @@ impl FuncTranslator {
code_offset: usize,
func: &mut ir::Function,
environ: &mut FE,
local_function_index: LocalFunctionIndex,
config: &CraneliftConfig,
) -> WasmResult<()> {
self.translate_from_reader(
module_translation_state,
MiddlewareBinaryReader::new_with_offset(code, code_offset),
func,
environ,
)
let mut reader = MiddlewareBinaryReader::new_with_offset(code, code_offset);
reader.set_middleware_chain(
config
.middlewares
.generate_middleware_chain(local_function_index),
);
self.translate_from_reader(module_translation_state, reader, func, environ)
}

/// Translate a binary WebAssembly function from a `MiddlewareBinaryReader`.
Expand Down

0 comments on commit 1144222

Please sign in to comment.