Skip to content

Commit

Permalink
Drop code in module_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
kper committed Sep 14, 2021
1 parent 3d96be5 commit 757c6d8
Show file tree
Hide file tree
Showing 47 changed files with 57 additions and 5,210 deletions.
7 changes: 5 additions & 2 deletions src/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::engine::import_resolver::{Import, ImportResolver};
use crate::engine::memory::MemoryInstance;
use crate::engine::store::Store;
use crate::engine::*;
use crate::engine::module::Functions;
use crate::value::Value;
use wasm_parser::core::*;
use wasm_parser::Module;
Expand All @@ -13,6 +14,7 @@ use anyhow::{anyhow, Context, Result};
pub fn allocate(
m: &Module,
mod_instance: &mut ModuleInstance,
functions: &Functions,
store: &mut Store,
imports: &[Import],
) -> Result<()> {
Expand All @@ -24,7 +26,7 @@ pub fn allocate(
let imports = create_import_resolver(&imports_entries, imports)?;

// Step 2a and 6
allocate_functions(m, mod_instance, store).context("Allocating function instances failed")?;
allocate_functions(m, mod_instance, functions, store).context("Allocating function instances failed")?;
//TODO host functions

// Step 3a and 7
Expand Down Expand Up @@ -90,6 +92,7 @@ fn create_import_resolver(_entries: &[&ImportEntry], imports: &[Import]) -> Resu
fn allocate_functions(
m: &Module,
mod_instance: &mut ModuleInstance,
functions: &Functions,
store: &mut Store,
) -> Result<()> {
debug!("allocate function");
Expand Down Expand Up @@ -118,7 +121,7 @@ fn allocate_functions(
if code_index < 0 {
None
} else {
borrow.lookup_code(code_index as usize)
functions.get(code_index as usize)
}
};

Expand Down
13 changes: 8 additions & 5 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use crate::debugger::{ProgramCounter, RelativeProgramCounter};
use crate::engine::func::FuncInstance;
use crate::engine::import_resolver::Import;
use crate::engine::module::ModuleInstance;
use crate::engine::module::Functions;
pub use crate::engine::store::GlobalInstance;
pub use crate::engine::table::TableInstance;
use crate::operations::*;
Expand All @@ -36,6 +37,7 @@ pub use wasm_parser::Module;
#[derive(Debug)]
pub struct Engine {
pub module_instance: ModuleInstance,
/// `started` declares if the engine was already run.
pub started: bool,
pub store: Store,
debugger: Box<dyn ProgramCounter>,
Expand Down Expand Up @@ -259,6 +261,7 @@ macro_rules! store_memory_n {
impl Engine {
pub fn new(
mi: ModuleInstance,
functions: &Functions,
module: &Module,
debugger: Box<dyn ProgramCounter>,
imports: &[Import],
Expand All @@ -270,7 +273,7 @@ impl Engine {
debugger,
};

e.allocate(module, &imports)
e.allocate(module, functions, &imports)
.context("Allocation instance failed")?;
e.instantiation(module).context("Instantiation failed")?;

Expand All @@ -292,9 +295,9 @@ impl Engine {
res
}

fn allocate(&mut self, m: &Module, imports: &[Import]) -> Result<()> {
fn allocate(&mut self, m: &Module, functions: &Functions, imports: &[Import]) -> Result<()> {
info!("Allocation");
crate::allocation::allocate(m, &mut self.module_instance, &mut self.store, imports)
crate::allocation::allocate(m, &mut self.module_instance, functions, &mut self.store, imports)
.context("Allocation failed")?;

Ok(())
Expand Down Expand Up @@ -342,7 +345,7 @@ impl Engine {
/// Adding new function to the engine
/// It will allocate the function in store and add it to the module's code.
pub(crate) fn add_function(&mut self, signature: FunctionSignature, body: FunctionBody) -> Result<()> {
self.module_instance.add_code(body.clone())?;
//self.module_instance.add_code(body.clone())?;
self.store.allocate_func_instance(signature, body);

Ok(())
Expand Down Expand Up @@ -433,7 +436,7 @@ impl Engine {
let mut locals = args;

// All parameters are `locals`, but
// we can additionaly define more of them.
// we can additionally define more of them.
// This is done in the function definition of locals
// It is very important to use the correct type
debug!("Adding additional locals");
Expand Down
44 changes: 28 additions & 16 deletions src/engine/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wasm_parser::Module;
#[derive(Debug, Default, Clone)]
pub struct ModuleInstance {
/// Contains all functions of a module
code: Vec<FunctionBody>,
//code: Vec<FunctionBody>,
/// Declares the function signatures of the functions
fn_types: Vec<FunctionSignature>,
/// Keeps the indexes of the function
Expand All @@ -21,21 +21,31 @@ pub struct ModuleInstance {
exports: Vec<ExportInstance>,
}

pub type Functions = Vec<FunctionBody>;

impl ModuleInstance {
pub fn new(m: &Module) -> Self {
let mut mi = ModuleInstance {
code: Vec::new(),
fn_types: Vec::new(),
func_addrs: Vec::new(),
table_addrs: Vec::new(),
mem_addrs: Vec::new(),
global_addrs: Vec::new(),
exports: Vec::new(),
};
pub fn new(m: &Module) -> (Self, Functions) {
let mut mi = ModuleInstance::default();

let mut functions = Vec::new();

let code_sections = m.sections.iter().filter(|x| match x {
Section::Code(..) => true,
_ => false
}).count();

let type_sections = m.sections.iter().filter(|x| match x {
Section::Type(..) => true,
_ => false
}).count();

assert!(!(code_sections > 1), "A module cannot have multiple code sections.");
assert!(!(type_sections > 1), "A module cannot have multiple type sections.");

for section in m.sections.iter() {
match section {
Section::Code(CodeSection { entries: x }) => {
mi.code = x.clone();
functions = x.clone();
}
Section::Type(TypeSection { entries: x }) => {
mi.fn_types = x.clone();
Expand All @@ -44,7 +54,7 @@ impl ModuleInstance {
}
}

mi
(mi, functions)
}

/// Adding a new function type.
Expand Down Expand Up @@ -91,10 +101,10 @@ impl ModuleInstance {
self.fn_types.get(*index as usize)
}

/// Looking up the code with given index.
pub fn lookup_code(&self, index: usize) -> Option<&FunctionBody> {
// /// Looking up the code with given index.
/*pub fn lookup_code(&self, index: usize) -> Option<&FunctionBody> {
self.code.get(index)
}
}*/

/// Storing a new function addr.
pub fn store_func_addr(&mut self, new_addr: FuncAddr) -> Result<()> {
Expand Down Expand Up @@ -153,6 +163,7 @@ impl ModuleInstance {
self.exports.get(idx)
}

/*
/// Add a code.
pub fn add_code(&mut self, body: FunctionBody) -> Result<()> {
self.code.push(body);
Expand All @@ -164,6 +175,7 @@ impl ModuleInstance {
pub fn get_code(&self) -> &[FunctionBody] {
&self.code
}
*/

pub fn get_fn_types(&self) -> &[FunctionSignature] {
&self.fn_types
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ fn main() {
return;
}

let mi = ModuleInstance::new(&module);
let (mi, functions) = ModuleInstance::new(&module);
info!("Constructing engine");

let e = Engine::new(
mi,
&functions,
&module,
Box::new(RelativeProgramCounter::default()),
&Imports::new(),
Expand Down
23 changes: 0 additions & 23 deletions src/tests/snapshots/funky__tests__wasm__add.wasm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@ expression: "format!(\"{:#?}\", engine)"
---
Engine {
module_instance: ModuleInstance {
code: [
FunctionBody {
locals: [],
code: [
InstructionWrapper {
instruction_id: 1,
instruction: OP_LOCAL_GET(
0,
),
},
InstructionWrapper {
instruction_id: 2,
instruction: OP_LOCAL_GET(
1,
),
},
InstructionWrapper {
instruction_id: 3,
instruction: OP_I32_ADD,
},
],
},
],
fn_types: [
FunctionSignature {
param_types: [
Expand Down
Loading

0 comments on commit 757c6d8

Please sign in to comment.