Starting integration.

This commit is contained in:
losfair
2019-02-14 00:53:36 +08:00
parent 7df7204e4b
commit 8d8db4aa09
5 changed files with 86 additions and 9 deletions

1
Cargo.lock generated
View File

@ -896,6 +896,7 @@ version = "0.1.4"
dependencies = [ dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wasmer-clif-backend 0.1.2", "wasmer-clif-backend 0.1.2",
"wasmer-dynasm-backend 0.1.0",
"wasmer-runtime-core 0.1.2", "wasmer-runtime-core 0.1.2",
] ]

View File

@ -10,3 +10,75 @@ mod codegen;
mod codegen_x64; mod codegen_x64;
mod parse; mod parse;
mod stack; mod stack;
use std::ptr::NonNull;
use wasmer_runtime_core::{
backend::{Backend, Compiler, FuncResolver, ProtectedCaller, Token, UserTrapper},
error::{CompileError, CompileResult, RuntimeResult},
module::{ModuleInfo, ModuleInner, StringTable},
structures::{Map, TypedIndex},
types::{
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, MemoryIndex, SigIndex, TableIndex, Type,
Value,
},
vm::{self, ImportBacking},
};
struct Placeholder;
impl FuncResolver for Placeholder {
fn get(
&self,
_module: &ModuleInner,
_local_func_index: LocalFuncIndex,
) -> Option<NonNull<vm::Func>> {
None
}
}
impl ProtectedCaller for Placeholder {
fn call(
&self,
_module: &ModuleInner,
_func_index: FuncIndex,
_params: &[Value],
_import_backing: &ImportBacking,
_vmctx: *mut vm::Ctx,
_: Token,
) -> RuntimeResult<Vec<Value>> {
Ok(vec![])
}
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
pub struct Trapper;
impl UserTrapper for Trapper {
unsafe fn do_early_trap(&self, msg: String) -> ! {
panic!("{}", msg);
}
}
Box::new(Trapper)
}
}
pub struct SinglePassCompiler {}
impl Compiler for SinglePassCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
let mut mcg = codegen_x64::X64ModuleCodeGenerator::new();
let info = match parse::read_module(wasm, Backend::Dynasm, &mut mcg) {
Ok(x) => x,
Err(e) => {
return Err(CompileError::InternalError {
msg: format!("{:?}", e),
})
}
};
Ok(ModuleInner {
func_resolver: Box::new(Placeholder),
protected_caller: Box::new(Placeholder),
info: info,
})
}
}

View File

@ -41,7 +41,7 @@ impl From<CodegenError> for LoadError {
pub fn read_module<MCG: ModuleCodeGenerator<FCG>, FCG: FunctionCodeGenerator>( pub fn read_module<MCG: ModuleCodeGenerator<FCG>, FCG: FunctionCodeGenerator>(
wasm: &[u8], wasm: &[u8],
backend: Backend, backend: Backend,
mut mcg: MCG, mcg: &mut MCG,
) -> Result<ModuleInfo, LoadError> { ) -> Result<ModuleInfo, LoadError> {
let mut info = ModuleInfo { let mut info = ModuleInfo {
memories: Map::new(), memories: Map::new(),
@ -72,6 +72,7 @@ pub fn read_module<MCG: ModuleCodeGenerator<FCG>, FCG: FunctionCodeGenerator>(
loop { loop {
if reader.eof() { if reader.eof() {
mcg.finalize()?;
return Ok(info); return Ok(info);
} }
@ -269,12 +270,14 @@ pub fn read_module<MCG: ModuleCodeGenerator<FCG>, FCG: FunctionCodeGenerator>(
for i in 0..code_reader.get_count() { for i in 0..code_reader.get_count() {
let item = code_reader.read()?; let item = code_reader.read()?;
let mut fcg = mcg.next_function()?; let mut fcg = mcg.next_function()?;
for param in info let sig = info
.signatures .signatures
.get(*info.func_assoc.get(FuncIndex::new(i as usize)).unwrap()) .get(*info.func_assoc.get(FuncIndex::new(i as usize)).unwrap())
.unwrap() .unwrap();
.params() for ret in sig.returns() {
{ fcg.feed_return(type_to_wp_type(*ret))?;
}
for param in sig.params() {
fcg.feed_param(type_to_wp_type(*param))?; fcg.feed_param(type_to_wp_type(*param))?;
} }
for local in item.get_locals_reader()? { for local in item.get_locals_reader()? {

View File

@ -11,10 +11,11 @@ readme = "README.md"
[dependencies] [dependencies]
wasmer-runtime-core = { path = "../runtime-core", version = "0.1.2" } wasmer-runtime-core = { path = "../runtime-core", version = "0.1.2" }
wasmer-clif-backend = { path = "../clif-backend", version = "0.1.2", optional = true } wasmer-clif-backend = { path = "../clif-backend", version = "0.1.2", optional = true }
wasmer-dynasm-backend = { path = "../dynasm-backend", optional = true }
lazy_static = "1.2.0" lazy_static = "1.2.0"
[features] [features]
default = ["default-compiler", "cache"] default = ["default-compiler"]
default-compiler = ["wasmer-clif-backend/cache", "wasmer-runtime-core/cache"] default-compiler = ["wasmer-clif-backend", "wasmer-dynasm-backend"]
cache = ["default-compiler"] cache = ["default-compiler"]
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"] debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]

View File

@ -188,10 +188,10 @@ pub fn compile_cache(wasm: &[u8]) -> error::CompileResult<Cache> {
#[cfg(feature = "default-compiler")] #[cfg(feature = "default-compiler")]
fn default_compiler() -> &'static dyn Compiler { fn default_compiler() -> &'static dyn Compiler {
use lazy_static::lazy_static; use lazy_static::lazy_static;
use wasmer_clif_backend::CraneliftCompiler; use wasmer_dynasm_backend::SinglePassCompiler;
lazy_static! { lazy_static! {
static ref DEFAULT_COMPILER: CraneliftCompiler = { CraneliftCompiler::new() }; static ref DEFAULT_COMPILER: SinglePassCompiler = { SinglePassCompiler {} };
} }
&*DEFAULT_COMPILER as &dyn Compiler &*DEFAULT_COMPILER as &dyn Compiler