mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-30 17:11:33 +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};
|
||||
use cranelift_codegen::settings;
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
|
||||
use super::errors::ErrorKind;
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
use target_lexicon::{Triple, PointerWidth};
|
||||
@ -15,7 +17,7 @@ use cranelift_wasm::{
|
||||
FuncTranslator,
|
||||
FuncEnvironment as FuncEnvironmentTrait, GlobalVariable, ModuleEnvironment, ReturnMode, WasmResult,
|
||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table,
|
||||
TableIndex,
|
||||
TableIndex, translate_module
|
||||
};
|
||||
|
||||
// use alloc::vec::Vec;
|
||||
@ -178,27 +180,44 @@ pub struct ModuleInstance {
|
||||
|
||||
impl ModuleInstance {
|
||||
/// Allocates the data structures with default flags.
|
||||
pub fn with_triple(triple: Triple) -> Self {
|
||||
Self::with_triple_flags(
|
||||
triple,
|
||||
settings::Flags::new(settings::builder()),
|
||||
ReturnMode::NormalReturns,
|
||||
)
|
||||
}
|
||||
|
||||
// pub fn with_triple(triple: Triple) -> Self {
|
||||
// Self::with_triple_flags(
|
||||
// triple,
|
||||
// settings::Flags::new(settings::builder()),
|
||||
// ReturnMode::NormalReturns,
|
||||
// )
|
||||
// }
|
||||
|
||||
/// Allocates the data structures with the given triple.
|
||||
pub fn with_triple_flags(
|
||||
triple: Triple,
|
||||
flags: settings::Flags,
|
||||
return_mode: ReturnMode,
|
||||
) -> Self {
|
||||
let mut x = Self {
|
||||
// pub fn with_triple_flags(
|
||||
// triple: Triple,
|
||||
// flags: settings::Flags,
|
||||
// return_mode: ReturnMode,
|
||||
// ) -> 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),
|
||||
trans: FuncTranslator::new(),
|
||||
func_bytecode_sizes: Vec::new(),
|
||||
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
|
||||
|
@ -73,24 +73,21 @@ pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>)
|
||||
if !validate(&buffer_source) {
|
||||
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 return_mode = ReturnMode::NormalReturns;
|
||||
let mut environ =
|
||||
ModuleInstance::with_triple_flags(triple!("riscv64"), flags.clone(), return_mode);
|
||||
// let isa = isa::lookup(module.info.triple)
|
||||
// .unwrap()
|
||||
// .finish(module.info.flags);
|
||||
|
||||
translate_module(&buffer_source, &mut environ).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
|
||||
|
||||
let isa = isa::lookup(environ.info.triple)
|
||||
.unwrap()
|
||||
.finish(environ.info.flags);
|
||||
|
||||
for func in environ.info.function_bodies.values() {
|
||||
verifier::verify_function(func, &*isa)
|
||||
.map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
||||
.unwrap();
|
||||
};
|
||||
unimplemented!()
|
||||
// for func in module.info.function_bodies.values() {
|
||||
// verifier::verify_function(func, &*isa)
|
||||
// .map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
||||
// .unwrap();
|
||||
// };
|
||||
|
||||
Ok(module)
|
||||
// Ok(environ)
|
||||
// let now = Instant::now();
|
||||
// let isa = construct_isa();
|
||||
|
Reference in New Issue
Block a user