From 0f4eea96be63061ca1471587a25516f48af16414 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 2 Dec 2023 13:34:43 +0100 Subject: [PATCH 1/2] Some more minor (forgotten) cleanups (#821) * rename compiled_funcs_2 field * remove unnecessary get_compiled_func_2 method --- crates/wasmi/src/engine/translator/visit.rs | 4 ++-- crates/wasmi/src/module/builder.rs | 20 ++++---------------- crates/wasmi/src/module/mod.rs | 2 +- crates/wasmi/src/module/parser.rs | 2 +- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/crates/wasmi/src/engine/translator/visit.rs b/crates/wasmi/src/engine/translator/visit.rs index c5a84e48d0..2e76955504 100644 --- a/crates/wasmi/src/engine/translator/visit.rs +++ b/crates/wasmi/src/engine/translator/visit.rs @@ -624,7 +624,7 @@ impl<'a> VisitOperator<'a> for FuncTranslator<'a> { let provider_params = &mut self.alloc.buffer; self.alloc.stack.pop_n(params.len(), provider_params); let results = self.alloc.stack.push_dynamic_n(results.len())?; - let instr = match self.res.get_compiled_func_2(func_idx) { + let instr = match self.res.get_compiled_func(func_idx) { Some(compiled_func) => { // Case: We are calling an internal function and can optimize // this case by using the special instruction for it. @@ -700,7 +700,7 @@ impl<'a> VisitOperator<'a> for FuncTranslator<'a> { let params = func_type.params(); let provider_params = &mut self.alloc.buffer; self.alloc.stack.pop_n(params.len(), provider_params); - let instr = match self.res.get_compiled_func_2(func_idx) { + let instr = match self.res.get_compiled_func(func_idx) { Some(compiled_func) => { // Case: We are calling an internal function and can optimize // this case by using the special instruction for it. diff --git a/crates/wasmi/src/module/builder.rs b/crates/wasmi/src/module/builder.rs index f71da64640..35dcc461c7 100644 --- a/crates/wasmi/src/module/builder.rs +++ b/crates/wasmi/src/module/builder.rs @@ -36,7 +36,7 @@ pub struct ModuleBuilder<'engine> { pub globals_init: Vec, pub exports: BTreeMap, ExternIdx>, pub start: Option, - pub compiled_funcs_2: Vec, + pub compiled_funcs: Vec, pub element_segments: Vec, pub data_segments: Vec, } @@ -103,19 +103,7 @@ impl<'a> ModuleResources<'a> { let index = index.checked_sub(len_imported)?; // Note: It is a bug if this index access is out of bounds // therefore we panic here instead of using `get`. - Some(self.res.compiled_funcs_2[index]) - } - - /// Returns the [`CompiledFunc`] for the given [`FuncIdx`]. - /// - /// Returns `None` if [`FuncIdx`] refers to an imported function. - pub fn get_compiled_func_2(&self, func_idx: FuncIdx) -> Option { - let index = func_idx.into_u32() as usize; - let len_imported = self.res.imports.len_funcs(); - let index = index.checked_sub(len_imported)?; - // Note: It is a bug if this index access is out of bounds - // therefore we panic here instead of using `get`. - Some(self.res.compiled_funcs_2[index]) + Some(self.res.compiled_funcs[index]) } /// Returns the global variable type and optional initial value. @@ -148,7 +136,7 @@ impl<'engine> ModuleBuilder<'engine> { globals_init: Vec::new(), exports: BTreeMap::new(), start: None, - compiled_funcs_2: Vec::new(), + compiled_funcs: Vec::new(), element_segments: Vec::new(), data_segments: Vec::new(), } @@ -245,7 +233,7 @@ impl<'engine> ModuleBuilder<'engine> { let func_type_idx = func?; let func_type = self.func_types[func_type_idx.into_u32() as usize]; self.funcs.push(func_type); - self.compiled_funcs_2.push(self.engine.alloc_func()); + self.compiled_funcs.push(self.engine.alloc_func()); } Ok(()) } diff --git a/crates/wasmi/src/module/mod.rs b/crates/wasmi/src/module/mod.rs index 83fb6d5844..d2691f7217 100644 --- a/crates/wasmi/src/module/mod.rs +++ b/crates/wasmi/src/module/mod.rs @@ -166,7 +166,7 @@ impl Module { globals_init: builder.globals_init.into(), exports: builder.exports, start: builder.start, - compiled_funcs: builder.compiled_funcs_2.into(), + compiled_funcs: builder.compiled_funcs.into(), element_segments: builder.element_segments.into(), data_segments: builder.data_segments.into(), } diff --git a/crates/wasmi/src/module/parser.rs b/crates/wasmi/src/module/parser.rs index 89c63bd6fb..7f114d79c9 100644 --- a/crates/wasmi/src/module/parser.rs +++ b/crates/wasmi/src/module/parser.rs @@ -487,7 +487,7 @@ impl<'engine> ModuleParser<'engine> { /// Returns the next `FuncIdx` for processing of its function body. fn next_func(&mut self) -> (FuncIdx, CompiledFunc) { let index = self.compiled_funcs; - let compiled_func_2 = self.builder.compiled_funcs_2[index as usize]; + let compiled_func_2 = self.builder.compiled_funcs[index as usize]; self.compiled_funcs += 1; // We have to adjust the initial func reference to the first // internal function before we process any of the internal functions. From 6abc9510fa7fd3ec0cb3e624d8f5a1dc4c650c10 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 2 Dec 2023 17:57:16 +0100 Subject: [PATCH 2/2] cleanup parameters in translate (#822) * cleanup parameters in translate * apply rustfmt --- crates/wasmi/src/module/compile/mod.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/wasmi/src/module/compile/mod.rs b/crates/wasmi/src/module/compile/mod.rs index 12fe951b5c..ee4500f77d 100644 --- a/crates/wasmi/src/module/compile/mod.rs +++ b/crates/wasmi/src/module/compile/mod.rs @@ -23,21 +23,14 @@ mod block_type; /// If the function body fails to validate. pub fn translate<'parser>( func: FuncIdx, - compiled_func_2: CompiledFunc, + compiled_func: CompiledFunc, func_body: FunctionBody<'parser>, validator: FuncValidator, res: ModuleResources<'parser>, allocations: FuncTranslatorAllocations, ) -> Result { - FunctionTranslator::new( - func, - compiled_func_2, - func_body, - validator, - res, - allocations, - )? - .translate() + FunctionTranslator::new(func, compiled_func, func_body, validator, res, allocations)? + .translate() } /// Translates Wasm bytecode into `wasmi` bytecode for a single Wasm function. @@ -52,14 +45,14 @@ impl<'parser> FunctionTranslator<'parser> { /// Creates a new Wasm to `wasmi` bytecode function translator. fn new( func: FuncIdx, - compiled_func_2: CompiledFunc, + compiled_func: CompiledFunc, func_body: FunctionBody<'parser>, validator: FuncValidator, res: ModuleResources<'parser>, allocations: FuncTranslatorAllocations, ) -> Result { let func_builder = - ValidatingFuncTranslator::new(func, compiled_func_2, res, validator, allocations)?; + ValidatingFuncTranslator::new(func, compiled_func, res, validator, allocations)?; Ok(Self { func_body, func_builder,