mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-01 09:31:32 +00:00
Improved ModuleInstance automatic compilation
This commit is contained in:
@ -8,6 +8,8 @@ use cranelift_codegen::ir::{self, InstBuilder, FuncRef, ExtFuncData, ExternalNam
|
|||||||
ArgumentPurpose, ArgumentLoc, ArgumentExtension, Function};
|
ArgumentPurpose, ArgumentLoc, ArgumentExtension, Function};
|
||||||
use cranelift_codegen::settings;
|
use cranelift_codegen::settings;
|
||||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||||
|
|
||||||
|
use super::errors::ErrorKind;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use target_lexicon::{Triple, PointerWidth};
|
use target_lexicon::{Triple, PointerWidth};
|
||||||
@ -15,7 +17,7 @@ use cranelift_wasm::{
|
|||||||
FuncTranslator,
|
FuncTranslator,
|
||||||
FuncEnvironment as FuncEnvironmentTrait, GlobalVariable, ModuleEnvironment, ReturnMode, WasmResult,
|
FuncEnvironment as FuncEnvironmentTrait, GlobalVariable, ModuleEnvironment, ReturnMode, WasmResult,
|
||||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table,
|
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table,
|
||||||
TableIndex,
|
TableIndex, translate_module
|
||||||
};
|
};
|
||||||
|
|
||||||
// use alloc::vec::Vec;
|
// use alloc::vec::Vec;
|
||||||
@ -178,27 +180,44 @@ pub struct ModuleInstance {
|
|||||||
|
|
||||||
impl ModuleInstance {
|
impl ModuleInstance {
|
||||||
/// Allocates the data structures with default flags.
|
/// Allocates the data structures with default flags.
|
||||||
pub fn with_triple(triple: Triple) -> Self {
|
|
||||||
Self::with_triple_flags(
|
// pub fn with_triple(triple: Triple) -> Self {
|
||||||
triple,
|
// Self::with_triple_flags(
|
||||||
settings::Flags::new(settings::builder()),
|
// triple,
|
||||||
ReturnMode::NormalReturns,
|
// settings::Flags::new(settings::builder()),
|
||||||
)
|
// ReturnMode::NormalReturns,
|
||||||
}
|
// )
|
||||||
|
// }
|
||||||
|
|
||||||
/// Allocates the data structures with the given triple.
|
/// Allocates the data structures with the given triple.
|
||||||
pub fn with_triple_flags(
|
// pub fn with_triple_flags(
|
||||||
triple: Triple,
|
// triple: Triple,
|
||||||
flags: settings::Flags,
|
// flags: settings::Flags,
|
||||||
return_mode: ReturnMode,
|
// return_mode: ReturnMode,
|
||||||
) -> Self {
|
// ) -> Self {
|
||||||
let mut x = Self {
|
// Self {
|
||||||
|
// info: ModuleInfo::with_triple_flags(triple, flags),
|
||||||
|
// trans: FuncTranslator::new(),
|
||||||
|
// func_bytecode_sizes: Vec::new(),
|
||||||
|
// return_mode,
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn from_bytes(buffer_source: Vec<u8>, triple: Triple, flags: Option<settings::Flags>) -> Result<Self, ErrorKind> {
|
||||||
|
let return_mode = ReturnMode::NormalReturns;
|
||||||
|
let flags = flags.unwrap_or_else(|| {
|
||||||
|
settings::Flags::new(settings::builder())
|
||||||
|
});
|
||||||
|
let mut module = Self {
|
||||||
info: ModuleInfo::with_triple_flags(triple, flags),
|
info: ModuleInfo::with_triple_flags(triple, flags),
|
||||||
trans: FuncTranslator::new(),
|
trans: FuncTranslator::new(),
|
||||||
func_bytecode_sizes: Vec::new(),
|
func_bytecode_sizes: Vec::new(),
|
||||||
return_mode,
|
return_mode,
|
||||||
};
|
};
|
||||||
x
|
// We iterate through the source bytes, generating the compiled module
|
||||||
|
translate_module(&buffer_source, &mut module).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a `FuncEnvironment` for translating functions within this
|
/// Return a `FuncEnvironment` for translating functions within this
|
||||||
|
@ -73,24 +73,21 @@ pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>)
|
|||||||
if !validate(&buffer_source) {
|
if !validate(&buffer_source) {
|
||||||
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let module =
|
||||||
|
ModuleInstance::from_bytes(buffer_source, triple!("riscv64"), None)?;
|
||||||
|
|
||||||
let flags = Flags::new(settings::builder());
|
// let isa = isa::lookup(module.info.triple)
|
||||||
let return_mode = ReturnMode::NormalReturns;
|
// .unwrap()
|
||||||
let mut environ =
|
// .finish(module.info.flags);
|
||||||
ModuleInstance::with_triple_flags(triple!("riscv64"), flags.clone(), return_mode);
|
|
||||||
|
|
||||||
translate_module(&buffer_source, &mut environ).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
|
// for func in module.info.function_bodies.values() {
|
||||||
|
// verifier::verify_function(func, &*isa)
|
||||||
let isa = isa::lookup(environ.info.triple)
|
// .map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
||||||
.unwrap()
|
// .unwrap();
|
||||||
.finish(environ.info.flags);
|
// };
|
||||||
|
|
||||||
for func in environ.info.function_bodies.values() {
|
Ok(module)
|
||||||
verifier::verify_function(func, &*isa)
|
|
||||||
.map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
|
||||||
.unwrap();
|
|
||||||
};
|
|
||||||
unimplemented!()
|
|
||||||
// Ok(environ)
|
// Ok(environ)
|
||||||
// let now = Instant::now();
|
// let now = Instant::now();
|
||||||
// let isa = construct_isa();
|
// let isa = construct_isa();
|
||||||
|
Reference in New Issue
Block a user