Skip to content

Commit

Permalink
Use ModuleInfo instead of ModuleInner when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Feb 18, 2019
1 parent 7602071 commit e381bbd
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 277 deletions.
12 changes: 6 additions & 6 deletions lib/clif-backend/src/func_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_type = self.pointer_type();

let local_global_addr = match global_index.local_or_import(self.env.module) {
let local_global_addr = match global_index.local_or_import(&self.env.module.info) {
LocalOrImport::Local(local_global_index) => {
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_type = self.pointer_type();

let (local_memory_ptr_ptr, description) = match mem_index.local_or_import(self.env.module) {
let (local_memory_ptr_ptr, description) = match mem_index.local_or_import(&self.env.module.info) {
LocalOrImport::Local(local_mem_index) => {
let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx,
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_type = self.pointer_type();

let (table_struct_ptr_ptr, description) = match table_index.local_or_import(self.env.module)
let (table_struct_ptr_ptr, description) = match table_index.local_or_import(&self.env.module.info)
{
LocalOrImport::Local(local_table_index) => {
let tables_base = func.create_global_value(ir::GlobalValueData::Load {
Expand Down Expand Up @@ -476,7 +476,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
) -> cranelift_wasm::WasmResult<ir::Inst> {
let callee_index: FuncIndex = Converter(clif_callee_index).into();

match callee_index.local_or_import(self.env.module) {
match callee_index.local_or_import(&self.env.module.info) {
LocalOrImport::Local(_) => {
// this is an internal function
let vmctx = pos
Expand Down Expand Up @@ -568,7 +568,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {

let mem_index: MemoryIndex = Converter(clif_mem_index).into();

let (namespace, mem_index, description) = match mem_index.local_or_import(self.env.module) {
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.env.module.info) {
LocalOrImport::Local(local_mem_index) => (
call_names::LOCAL_NAMESPACE,
local_mem_index.index(),
Expand Down Expand Up @@ -631,7 +631,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {

let mem_index: MemoryIndex = Converter(clif_mem_index).into();

let (namespace, mem_index, description) = match mem_index.local_or_import(self.env.module) {
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.env.module.info) {
LocalOrImport::Local(local_mem_index) => (
call_names::LOCAL_NAMESPACE,
local_mem_index.index(),
Expand Down
4 changes: 2 additions & 2 deletions lib/clif-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Compiler for CraneliftCompiler {

let isa = get_isa();

let mut module = module::Module::empty();
let mut module = module::Module::new(wasm);
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);

let func_bodies = module_env.translate(wasm)?;
Expand All @@ -72,7 +72,7 @@ impl Compiler for CraneliftCompiler {

let isa = get_isa();

let mut module = module::Module::empty();
let mut module = module::Module::new(wasm);
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);

let func_bodies = module_env.translate(wasm)?;
Expand Down
130 changes: 42 additions & 88 deletions lib/clif-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,83 @@ use cranelift_codegen::{ir, isa};
use cranelift_entity::EntityRef;
use cranelift_wasm;
use hashbrown::HashMap;
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};

#[cfg(feature = "cache")]
use wasmer_runtime_core::{
backend::sys::Memory,
cache::{Cache, Error as CacheError},
};

use wasmer_runtime_core::{
backend::{Backend, FuncResolver, ProtectedCaller, Token, UserTrapper},
error::{CompileResult, RuntimeResult},
module::{ModuleInfo, ModuleInner, StringTable},
backend::Backend,
error::CompileResult,
module::{ModuleInfo, ModuleInner, StringTable, WasmHash},
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> {
unimplemented!()
}
}

/// This contains all of the items in a `ModuleInner` except the `func_resolver`.
pub struct Module {
pub module: ModuleInner,
pub info: ModuleInfo,
}

impl Module {
pub fn empty() -> Self {
pub fn new(wasm: &[u8]) -> Self {
Self {
module: ModuleInner {
// this is a placeholder
func_resolver: Box::new(Placeholder),
protected_caller: Box::new(Placeholder),
info: ModuleInfo {
memories: Map::new(),
globals: Map::new(),
tables: Map::new(),

info: ModuleInfo {
memories: Map::new(),
globals: Map::new(),
tables: Map::new(),
imported_functions: Map::new(),
imported_memories: Map::new(),
imported_tables: Map::new(),
imported_globals: Map::new(),

imported_functions: Map::new(),
imported_memories: Map::new(),
imported_tables: Map::new(),
imported_globals: Map::new(),
exports: HashMap::new(),

exports: HashMap::new(),
data_initializers: Vec::new(),
elem_initializers: Vec::new(),

data_initializers: Vec::new(),
elem_initializers: Vec::new(),
start_func: None,

start_func: None,
func_assoc: Map::new(),
signatures: Map::new(),
backend: Backend::Cranelift,

func_assoc: Map::new(),
signatures: Map::new(),
backend: Backend::Cranelift,
namespace_table: StringTable::new(),
name_table: StringTable::new(),

namespace_table: StringTable::new(),
name_table: StringTable::new(),
},
wasm_hash: WasmHash::generate(wasm),
},
}
}

pub fn compile(
mut self,
self,
isa: &isa::TargetIsa,
functions: Map<LocalFuncIndex, ir::Function>,
) -> CompileResult<ModuleInner> {
let (func_resolver_builder, handler_data) =
FuncResolverBuilder::new(isa, functions, &self.module.info)?;
FuncResolverBuilder::new(isa, functions, &self.info)?;


self.module.func_resolver =
Box::new(func_resolver_builder.finalize(&self.module.info.signatures)?);
let func_resolver =
Box::new(func_resolver_builder.finalize(&self.info.signatures)?);

let trampolines = Trampolines::new(isa, &self.module.info);
let trampolines = Trampolines::new(isa, &self.info);

self.module.protected_caller =
Box::new(Caller::new(&self.module.info, handler_data, trampolines));
let protected_caller =
Box::new(Caller::new(&self.info, handler_data, trampolines));

Ok(self.module)
Ok(ModuleInner {
func_resolver,
protected_caller,

info: self.info,
})
}

#[cfg(feature = "cache")]
Expand All @@ -124,16 +92,16 @@ impl Module {
functions: Map<LocalFuncIndex, ir::Function>,
) -> CompileResult<(ModuleInfo, BackendCache, Memory)> {
let (func_resolver_builder, handler_data) =
FuncResolverBuilder::new(isa, functions, &self.module.info)?;
FuncResolverBuilder::new(isa, functions, &self.info)?;

let trampolines = Trampolines::new(isa, &self.module.info);
let trampolines = Trampolines::new(isa, &self.info);

let trampoline_cache = trampolines.to_trampoline_cache();

let (backend_cache, compiled_code) =
func_resolver_builder.to_backend_cache(trampoline_cache, handler_data);

Ok((self.module.info, backend_cache, compiled_code))
Ok((self.info, backend_cache, compiled_code))
}

#[cfg(feature = "cache")]
Expand All @@ -159,20 +127,6 @@ impl Module {
}
}

impl Deref for Module {
type Target = ModuleInner;

fn deref(&self) -> &ModuleInner {
&self.module
}
}

impl DerefMut for Module {
fn deref_mut(&mut self) -> &mut ModuleInner {
&mut self.module
}
}

pub struct Converter<T>(pub T);

macro_rules! convert_clif_to_runtime_index {
Expand Down
8 changes: 4 additions & 4 deletions lib/clif-backend/src/module_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
// assert!(!desc.mutable);
let global_index: GlobalIndex = Converter(global_index).into();
let imported_global_index = global_index
.local_or_import(self.module)
.local_or_import(&self.module.info)
.import()
.expect("invalid global initializer when declaring an imported global");
Initializer::GetGlobal(imported_global_index)
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
let base = match base {
Some(global_index) => {
let global_index: GlobalIndex = Converter(global_index).into();
Initializer::GetGlobal(match global_index.local_or_import(self.module) {
Initializer::GetGlobal(match global_index.local_or_import(&self.module.info) {
LocalOrImport::Import(imported_global_index) => imported_global_index,
LocalOrImport::Local(_) => {
panic!("invalid global initializer when declaring an imported global")
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
let base = match base {
Some(global_index) => {
let global_index: GlobalIndex = Converter(global_index).into();
Initializer::GetGlobal(match global_index.local_or_import(self.module) {
Initializer::GetGlobal(match global_index.local_or_import(&self.module.info) {
LocalOrImport::Import(imported_global_index) => imported_global_index,
LocalOrImport::Local(_) => {
panic!("invalid global initializer when declaring an imported global")
Expand Down Expand Up @@ -389,7 +389,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
let name = ir::ExternalName::user(0, func_index.index() as u32);

let sig = func_env.generate_signature(
self.get_func_type(Converter(func_index.convert_up(self.module)).into()),
self.get_func_type(Converter(func_index.convert_up(&self.module.info)).into()),
);

let mut func = ir::Function::with_name_signature(name, sig);
Expand Down
2 changes: 1 addition & 1 deletion lib/clif-backend/src/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn get_func_from_index(
.get(func_index)
.expect("broken invariant, incorrect func index");

let (func_ptr, ctx) = match func_index.local_or_import(module) {
let (func_ptr, ctx) = match func_index.local_or_import(&module.info) {
LocalOrImport::Local(local_func_index) => (
module
.func_resolver
Expand Down
4 changes: 4 additions & 0 deletions lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ pub trait FuncResolver: Send + Sync {
local_func_index: LocalFuncIndex,
) -> Option<NonNull<vm::Func>>;
}

pub trait CacheGen: Send + Sync {
fn generate_cache(&self, module: &ModuleInner) -> Result<(Box<ModuleInfo>, Box<[u8]>, Memory), CacheError>;
}
10 changes: 5 additions & 5 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl LocalBacking {
}
} as usize;

match init.memory_index.local_or_import(module) {
match init.memory_index.local_or_import(&module.info) {
LocalOrImport::Local(local_memory_index) => {
let memory_desc = module.info.memories[local_memory_index];
let data_top = init_base + init.data.len();
Expand Down Expand Up @@ -159,7 +159,7 @@ impl LocalBacking {
}
} as usize;

match init.table_index.local_or_import(module) {
match init.table_index.local_or_import(&module.info) {
LocalOrImport::Local(local_table_index) => {
let table = &tables[local_table_index];

Expand All @@ -177,7 +177,7 @@ impl LocalBacking {
SigRegistry.lookup_sig_index(Arc::clone(&signature)).index() as u32,
);

let (func, ctx) = match func_index.local_or_import(module) {
let (func, ctx) = match func_index.local_or_import(&module.info) {
LocalOrImport::Local(local_func_index) => (
module
.func_resolver
Expand Down Expand Up @@ -215,7 +215,7 @@ impl LocalBacking {
SigRegistry.lookup_sig_index(Arc::clone(&signature)).index() as u32,
);

let (func, ctx) = match func_index.local_or_import(module) {
let (func, ctx) = match func_index.local_or_import(&module.info) {
LocalOrImport::Local(local_func_index) => (
module
.func_resolver
Expand Down Expand Up @@ -364,7 +364,7 @@ fn import_functions(
},
) in &module.info.imported_functions
{
let sig_index = module.info.func_assoc[index.convert_up(module)];
let sig_index = module.info.func_assoc[index.convert_up(&module.info)];
let expected_sig = &module.info.signatures[sig_index];

let namespace = module.info.namespace_table.get(*namespace_index);
Expand Down
Loading

0 comments on commit e381bbd

Please sign in to comment.