Add progress on next_event

This commit is contained in:
Brandon Fish
2019-05-18 16:31:08 -05:00
parent ac3fafae9a
commit 74a758d39e
7 changed files with 405 additions and 67 deletions

69
Cargo.lock generated
View File

@ -309,67 +309,67 @@ dependencies = [
[[package]]
name = "cranelift-bforest"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
]
[[package]]
name = "cranelift-codegen"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cranelift-bforest 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen-meta 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-bforest 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-codegen-meta 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cranelift-codegen-meta"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
]
[[package]]
name = "cranelift-entity"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
[[package]]
name = "cranelift-frontend"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cranelift-codegen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cranelift-native"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cranelift-codegen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cranelift-wasm"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
dependencies = [
"cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-frontend 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-frontend 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1861,7 +1861,7 @@ dependencies = [
[[package]]
name = "target-lexicon"
version = "0.3.0"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2250,10 +2250,11 @@ name = "wasmer-clif-backend"
version = "0.4.1"
dependencies = [
"byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-native 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-wasm 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-frontend 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-native 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"cranelift-wasm 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"nix 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2262,7 +2263,7 @@ dependencies = [
"serde-bench 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_bytes 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"target-lexicon 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wasmer-runtime-core 0.4.1",
"wasmer-win-exception-handler 0.4.1",
"wasmparser 0.29.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2609,13 +2610,13 @@ dependencies = [
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
"checksum core-foundation 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "286e0b41c3a20da26536c6000a280585d519fd07b3956b43aed8a79e9edce980"
"checksum core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "716c271e8613ace48344f723b60b900a93150271e5be206212d052bbc0883efa"
"checksum cranelift-bforest 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e5a357d20666bf4a8c2d626a19f1b59dbca66cd844fb1e66c5612254fd0f7505"
"checksum cranelift-codegen 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab00cb149a5bb0f7e6dd391357356a5d71c335a431e8eece94f32da2d5a043f7"
"checksum cranelift-codegen-meta 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e3797a2f450ac71297e083dd440d0cdd0d3bceabe4a3ca6bcb9e4077e9c0327d"
"checksum cranelift-entity 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b66e28877b75b3d2b31250f780bb5db8f68ae3df681cd56add803b2567ac4fd"
"checksum cranelift-frontend 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b72d55fd732b1f7a99d043a36c54a5679b6ec8bc777c8d954fb97c4fa0fce7eb"
"checksum cranelift-native 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0239f34836621a127c2132980b2f5c32a1be1c40e2d1a9a1a9bd5af33c12aee"
"checksum cranelift-wasm 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740ebfba28c8433f06750f84819f1eb663ea9f5e4b9a81c01f4e52262d868b56"
"checksum cranelift-bforest 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-codegen-meta 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-frontend 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-native 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum cranelift-wasm 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)" = "<none>"
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
"checksum criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1c6e5ee5b9652d4f851418c448af105642e1f99e9a2741a8ff45c0d2c911b1e0"
@ -2783,7 +2784,7 @@ dependencies = [
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
"checksum tar 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)" = "c2167ff53da2a661702b3299f71a91b61b1dffef36b4b2884b1f9c67254c0133"
"checksum target-lexicon 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d6923974ce4eb5bd28814756256d8ab71c28dd6e7483313fe7ab6614306bf633"
"checksum target-lexicon 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1b0ab4982b8945c35cc1c46a83a9094c414f6828a099ce5dcaa8ee2b04642dcb"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a"
"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f"

View File

@ -9,12 +9,20 @@ edition = "2018"
[dependencies]
wasmer-runtime-core = { path = "../runtime-core", version = "0.4.1" }
cranelift-native = "0.30.0"
cranelift-codegen = "0.30.0"
cranelift-entity = "0.30.0"
cranelift-wasm = "0.30.0"
# cranelift-native = { path = "../../../cranelift/cranelift-native" }
# cranelift-codegen = { path = "../../../cranelift/cranelift-codegen" }
# cranelift-entity = { path = "../../../cranelift/cranelift-entity" }
# cranelift-frontend = { path = "../../../cranelift/cranelift-frontend" }
# cranelift-wasm = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
cranelift-native = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
cranelift-codegen = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
cranelift-entity = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
cranelift-frontend = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
cranelift-wasm = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
hashbrown = "0.1"
target-lexicon = "0.3.0"
target-lexicon = "0.4.0"
wasmparser = "0.29.2"
byteorder = "1"
nix = "0.13.0"

View File

@ -1,29 +1,51 @@
use crate::signal::Caller;
use crate::{func_env::FuncEnv, module::{Converter, Module}, signal::Caller, get_isa};
use std::sync::Arc;
use wasmer_runtime_core::{
backend::{Backend, CacheGen, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
module::{ModuleInfo, ModuleInner},
structures::Map,
types::{FuncIndex, FuncSig, SigIndex},
structures::{TypedIndex, Map},
types::{
FuncSig, FuncIndex,
ElementType, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
},
};
use cranelift_codegen::isa;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::{self, Ebb, InstBuilder, ValueLabel};
use cranelift_codegen::timing;
use wasmparser::Type as WpType;
use cranelift_wasm::{translate_operator, TranslationState, get_vmctx_value_label};
use cranelift_wasm::{self, translate_module, FuncTranslator, ModuleEnvironment};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
pub struct CraneliftModuleCodeGenerator {
isa: Box<isa::TargetIsa>,
signatures: Option<Arc<Map<SigIndex, FuncSig>>>,
pub clif_signatures: Map<SigIndex, ir::Signature>,
function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>,
functions: Vec<CraneliftFunctionCodeGenerator>,
func_bodies: Map<LocalFuncIndex, ir::Function>,
}
pub struct ClifFuncEnv {
}
impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
for CraneliftModuleCodeGenerator
{
fn new() -> Self {
let isa = get_isa();
CraneliftModuleCodeGenerator {
isa,
clif_signatures: Map::new(), // TODO FIX
functions: vec![],
function_signatures: None,
signatures: None,
func_bodies: Map::new(),
}
}
@ -35,9 +57,233 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
Ok(())
}
fn next_function(&mut self) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
let code = CraneliftFunctionCodeGenerator {};
self.functions.push(code);
fn next_function(&mut self, module_info: &ModuleInfo) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
// define_function_body(
let mut func_translator = FuncTranslator::new();
// let func_body = {
// let mut func_env = FuncEnv::new(self);
let func_index = self.func_bodies.next_index();
let name = ir::ExternalName::user(0, func_index.index() as u32);
let sig = generate_signature(self,
self.get_func_type(&module_info, Converter(func_index.convert_up(&module_info)).into()),
);
let mut func = ir::Function::with_name_signature(name, sig);
//func_translator.translate(body_bytes, body_offset, &mut func, &mut func_env)?;
// This clears the `FunctionBuilderContext`.
let mut func_env = CraneliftFunctionCodeGenerator {
builder: None,
func_body: func,
func_translator,
// translator:
};
let builder = FunctionBuilder::new(&mut func_env.func_body, &mut func_env.func_translator.func_ctx);
func_env.builder = Some(builder);
let mut builder = func_env.builder.as_ref().unwrap();
// TODO srcloc
//builder.set_srcloc(cur_srcloc(&reader));
let entry_block = builder.create_ebb();
builder.append_ebb_params_for_function_params(entry_block);
builder.switch_to_block(entry_block); // This also creates values for the arguments.
builder.seal_block(entry_block);
// Make sure the entry block is inserted in the layout before we make any callbacks to
// `environ`. The callback functions may need to insert things in the entry block.
builder.ensure_inserted_ebb();
let num_params = declare_wasm_parameters(&mut builder, entry_block);
// Set up the translation state with a single pushed control block representing the whole
// function and its return values.
let exit_block = builder.create_ebb();
builder.append_ebb_params_for_function_returns(exit_block);
func_translator.state.initialize(&builder.func.signature, exit_block);
#[cfg(feature = "debug")]
{
use cranelift_codegen::cursor::{Cursor, FuncCursor};
use cranelift_codegen::ir::InstBuilder;
let entry_ebb = func.layout.entry_block().unwrap();
let ebb = func.dfg.make_ebb();
func.layout.insert_ebb(ebb, entry_ebb);
let mut pos = FuncCursor::new(&mut func).at_first_insertion_point(ebb);
let params = pos.func.dfg.ebb_params(entry_ebb).to_vec();
let new_ebb_params: Vec<_> = params
.iter()
.map(|&param| {
pos.func
.dfg
.append_ebb_param(ebb, pos.func.dfg.value_type(param))
})
.collect();
let start_debug = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
ir::AbiParam::new(ir::types::I32),
],
returns: vec![],
});
let name = ir::ExternalName::testcase("strtdbug");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let end_debug = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![ir::AbiParam::special(
ir::types::I64,
ir::ArgumentPurpose::VMContext,
)],
returns: vec![],
});
let name = ir::ExternalName::testcase("enddbug");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let i32_print = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
ir::AbiParam::new(ir::types::I32),
],
returns: vec![],
});
let name = ir::ExternalName::testcase("i32print");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let i64_print = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
ir::AbiParam::new(ir::types::I64),
],
returns: vec![],
});
let name = ir::ExternalName::testcase("i64print");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let f32_print = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
ir::AbiParam::new(ir::types::F32),
],
returns: vec![],
});
let name = ir::ExternalName::testcase("f32print");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let f64_print = {
let signature = pos.func.import_signature(ir::Signature {
call_conv: self.target_config().default_call_conv,
params: vec![
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
ir::AbiParam::new(ir::types::F64),
],
returns: vec![],
});
let name = ir::ExternalName::testcase("f64print");
pos.func.import_function(ir::ExtFuncData {
name,
signature,
colocated: false,
})
};
let vmctx = pos
.func
.special_param(ir::ArgumentPurpose::VMContext)
.expect("missing vmctx parameter");
let func_index = pos.ins().iconst(
ir::types::I32,
func_index.index() as i64 + self.module.info.imported_functions.len() as i64,
);
pos.ins().call(start_debug, &[vmctx, func_index]);
for param in new_ebb_params.iter().cloned() {
match pos.func.dfg.value_type(param) {
ir::types::I32 => pos.ins().call(i32_print, &[vmctx, param]),
ir::types::I64 => pos.ins().call(i64_print, &[vmctx, param]),
ir::types::F32 => pos.ins().call(f32_print, &[vmctx, param]),
ir::types::F64 => pos.ins().call(f64_print, &[vmctx, param]),
_ => unimplemented!(),
};
}
pos.ins().call(end_debug, &[vmctx]);
pos.ins().jump(entry_ebb, new_ebb_params.as_slice());
}
// func
// };
// Add function body to list of function bodies.
// self.func_bodies.push(func);
self.functions.push(func_env);
Ok(self.functions.last_mut().unwrap())
}
@ -70,7 +316,11 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
}
}
pub struct CraneliftFunctionCodeGenerator {}
pub struct CraneliftFunctionCodeGenerator {
func_body: ir::Function,
builder: Option<FunctionBuilder<'static>>,
func_translator: FuncTranslator,
}
impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
fn feed_return(&mut self, _ty: WpType) -> Result<(), CodegenError> {
@ -89,11 +339,22 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
Ok(())
}
fn feed_event(&mut self, _op: Event, _module_info: &ModuleInfo) -> Result<(), CodegenError> {
fn feed_event(&mut self, event: Event, _module_info: &ModuleInfo) -> Result<(), CodegenError> {
let op = match event {
Event::Wasm(x) => x,
Event::Internal(_x) => {
return Ok(());
}
};
let builder = &self.builder;
//let func_environment = FuncEnv::new();
//let state = TranslationState::new();
//translate_operator(*op, builder, state, func_environment);
Ok(())
}
fn finalize(&mut self) -> Result<(), CodegenError> {
self.builder.as_mut().unwrap().finalize();
Ok(())
}
}
@ -102,3 +363,70 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
pub struct CodegenError {
pub message: String,
}
impl CraneliftModuleCodeGenerator {
/// Return the signature index for the given function index.
pub fn get_func_type(
&self,
module_info: &ModuleInfo,
func_index: cranelift_wasm::FuncIndex,
) -> cranelift_wasm::SignatureIndex {
let sig_index: SigIndex = module_info.func_assoc[Converter(func_index).into()];
Converter(sig_index).into()
}
}
/// Creates a signature with VMContext as the last param
fn generate_signature(
env: &CraneliftModuleCodeGenerator,
clif_sig_index: cranelift_wasm::SignatureIndex,
) -> ir::Signature {
// Get signature
let mut signature = env.clif_signatures[Converter(clif_sig_index).into()].clone();
// Add the vmctx parameter type to it
signature.params.insert(
0,
ir::AbiParam::special(pointer_type(env), ir::ArgumentPurpose::VMContext),
);
// Return signature
signature
}
fn pointer_type(mcg: &CraneliftModuleCodeGenerator) -> ir::Type {
ir::Type::int(u16::from(mcg.isa.frontend_config().pointer_bits())).unwrap()
}
/// Declare local variables for the signature parameters that correspond to WebAssembly locals.
///
/// Return the number of local variables declared.
fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> usize {
let sig_len = builder.func.signature.params.len();
let mut next_local = 0;
for i in 0..sig_len {
let param_type = builder.func.signature.params[i];
// There may be additional special-purpose parameters following the normal WebAssembly
// signature parameters. For example, a `vmctx` pointer.
if param_type.purpose == ir::ArgumentPurpose::Normal {
// This is a normal WebAssembly signature parameter, so create a local for it.
let local = Variable::new(next_local);
builder.declare_var(local, param_type.value_type);
next_local += 1;
let param_value = builder.ebb_params(entry_block)[i];
builder.def_var(local, param_value);
}
if param_type.purpose == ir::ArgumentPurpose::VMContext {
let param_value = builder.ebb_params(entry_block)[i];
builder.set_val_label(param_value, get_vmctx_value_label());
}
}
next_local
}

View File

@ -1,4 +1,5 @@
use crate::{module::Converter, module_env::ModuleEnv, relocation::call_names};
use crate::{module::Converter,
module_env::ModuleEnv, relocation::call_names};
use cranelift_codegen::{
cursor::FuncCursor,
ir::{self, InstBuilder},
@ -68,7 +69,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
&mut self,
func: &mut ir::Function,
clif_global_index: cranelift_wasm::GlobalIndex,
) -> cranelift_wasm::GlobalVariable {
) -> cranelift_wasm::WasmResult<cranelift_wasm::GlobalVariable> {
let global_index: GlobalIndex = Converter(clif_global_index).into();
// Create VMContext value.
@ -124,11 +125,11 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
}
};
cranelift_wasm::GlobalVariable::Memory {
Ok(cranelift_wasm::GlobalVariable::Memory {
gv: local_global_addr,
offset: (vm::LocalGlobal::offset_data() as i32).into(),
ty: self.env.get_global(clif_global_index).ty,
}
})
}
/// Sets up the necessary preamble definitions in `func` to access the linear memory identified
@ -139,7 +140,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
&mut self,
func: &mut ir::Function,
clif_mem_index: cranelift_wasm::MemoryIndex,
) -> ir::Heap {
) -> cranelift_wasm::WasmResult<ir::Heap> {
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
// Create VMContext value.
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
@ -217,7 +218,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
readonly: false,
});
func.create_heap(ir::HeapData {
Ok(func.create_heap(ir::HeapData {
base: local_memory_base,
min_size: (description.minimum.bytes().0 as u64).into(),
offset_guard_size: mem_type.guard_size().into(),
@ -225,9 +226,9 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
bound_gv: local_memory_bound,
},
index_type: ir::types::I32,
})
}))
}
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => func
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => Ok(func
.create_heap(ir::HeapData {
base: local_memory_base,
min_size: (description.minimum.bytes().0 as u64).into(),
@ -236,7 +237,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
bound: mem_type.bounds().unwrap().into(),
},
index_type: ir::types::I32,
}),
})),
}
}
@ -248,7 +249,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
&mut self,
func: &mut ir::Function,
clif_table_index: cranelift_wasm::TableIndex,
) -> ir::Table {
) -> cranelift_wasm::WasmResult<ir::Table> {
let table_index: TableIndex = Converter(clif_table_index).into();
// Create VMContext value.
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
@ -326,13 +327,13 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
readonly: false,
});
func.create_table(ir::TableData {
Ok(func.create_table(ir::TableData {
base_gv: table_base,
min_size: (description.minimum as u64).into(),
bound_gv: table_count,
element_size: (vm::Anyfunc::size() as u64).into(),
index_type: ir::types::I32,
})
}))
}
/// Sets up a signature definition in `func`'s preamble.
@ -343,9 +344,9 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
&mut self,
func: &mut ir::Function,
clif_sig_index: cranelift_wasm::SignatureIndex,
) -> ir::SigRef {
) -> cranelift_wasm::WasmResult<ir::SigRef> {
// Create a signature reference out of specified signature (with VMContext param added).
func.import_signature(self.generate_signature(clif_sig_index))
Ok(func.import_signature(self.generate_signature(clif_sig_index)))
}
/// Sets up an external function definition in the preamble of `func` that can be used to
@ -356,7 +357,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
&mut self,
func: &mut ir::Function,
func_index: cranelift_wasm::FuncIndex,
) -> ir::FuncRef {
) -> cranelift_wasm::WasmResult<ir::FuncRef> {
// Get signature of function.
let signature_index = self.env.get_func_type(func_index);
@ -367,12 +368,12 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
let name = ir::ExternalName::user(0, func_index.as_u32());
// Create function reference from fuction data.
func.import_function(ir::ExtFuncData {
Ok(func.import_function(ir::ExtFuncData {
name,
signature,
// Make this colocated so all calls between local functions are relative.
colocated: true,
})
}))
}
/// Generates an indirect call IR with `callee` and `call_args`.

View File

@ -1,4 +1,4 @@
#![deny(unused_imports, unused_variables, unused_unsafe, unreachable_patterns)]
#![deny(unused_unsafe, unreachable_patterns)]
mod cache;
mod code;

View File

@ -47,7 +47,7 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
/// Creates a new function and returns the function-scope code generator for it.
fn next_function(&mut self) -> Result<&mut FCG, E>;
fn next_function(&mut self, module_info: &ModuleInfo) -> Result<&mut FCG, E>;
fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>;
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>;

View File

@ -201,7 +201,7 @@ pub fn read_module<
}
let fcg = mcg
.next_function()
.next_function(&info)
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
middlewares
.run(