diff --git a/src/function_body.rs b/src/function_body.rs index 213da6ee59..d0f83fab4b 100644 --- a/src/function_body.rs +++ b/src/function_body.rs @@ -1,4 +1,5 @@ use backend::*; +use module::TranslationContext; use error::Error; use wasmparser::{FuncType, FunctionBody, Operator, Type}; @@ -88,6 +89,7 @@ impl ControlFrame { pub fn translate( session: &mut CodeGenSession, + translation_ctx: &TranslationContext, func_type: &FuncType, body: &FunctionBody, ) -> Result<(), Error> { diff --git a/src/module.rs b/src/module.rs index 6a900a2695..e2c71ba989 100644 --- a/src/module.rs +++ b/src/module.rs @@ -2,7 +2,7 @@ use backend::TranslatedCodeSection; use error::Error; use std::mem; use translate_sections; -use wasmparser::{ModuleReader, SectionCode}; +use wasmparser::{FuncType, ModuleReader, SectionCode}; #[derive(Default)] pub struct TranslatedModule { @@ -27,6 +27,20 @@ impl TranslatedModule { } } +#[derive(Default)] +pub struct TranslationContext { + types: Vec, + func_ty_indicies: Vec, +} + +impl TranslationContext { + pub fn func_type(&self, func_idx: u32) -> &FuncType { + // TODO: This assumes that there is no imported functions. + let func_ty_idx = self.func_ty_indicies[func_idx as usize]; + &self.types[func_ty_idx as usize] + } +} + /// Translate from a slice of bytes holding a wasm module. pub fn translate(data: &[u8]) -> Result { let mut reader = ModuleReader::new(data)?; @@ -37,12 +51,12 @@ pub fn translate(data: &[u8]) -> Result { return Ok(output); } let mut section = reader.read()?; - let mut types = vec![]; - let mut func_ty_indicies = vec![]; + + let mut ctx = TranslationContext::default(); if let SectionCode::Type = section.code { let types_reader = section.get_type_section_reader()?; - types = translate_sections::type_(types_reader)?; + ctx.types = translate_sections::type_(types_reader)?; reader.skip_custom_sections()?; if reader.eof() { @@ -64,7 +78,7 @@ pub fn translate(data: &[u8]) -> Result { if let SectionCode::Function = section.code { let functions = section.get_function_section_reader()?; - func_ty_indicies = translate_sections::function(functions)?; + ctx.func_ty_indicies = translate_sections::function(functions)?; reader.skip_custom_sections()?; if reader.eof() { @@ -142,7 +156,7 @@ pub fn translate(data: &[u8]) -> Result { if let SectionCode::Code = section.code { let code = section.get_code_section_reader()?; output.translated_code_section = - Some(translate_sections::code(code, &types, &func_ty_indicies)?); + Some(translate_sections::code(code, &ctx)?); reader.skip_custom_sections()?; if reader.eof() { diff --git a/src/translate_sections.rs b/src/translate_sections.rs index db97713a7a..431285b42f 100644 --- a/src/translate_sections.rs +++ b/src/translate_sections.rs @@ -1,6 +1,7 @@ use backend::{CodeGenSession, TranslatedCodeSection}; use error::Error; use function_body; +use module::TranslationContext; #[allow(unused_imports)] // for now use wasmparser::{ CodeSectionReader, Data, DataSectionReader, Element, ElementSectionReader, Export, @@ -84,15 +85,12 @@ pub fn element(elements: ElementSectionReader) -> Result<(), Error> { /// Parses the Code section of the wasm module. pub fn code( code: CodeSectionReader, - types: &[FuncType], - func_ty_indicies: &[u32], + translation_ctx: &TranslationContext ) -> Result { let mut session = CodeGenSession::new(); for (idx, body) in code.into_iter().enumerate() { - let func_ty_idx = func_ty_indicies[idx]; - let func_ty = &types[func_ty_idx as usize]; - - function_body::translate(&mut session, &func_ty, &body?)?; + let func_ty = translation_ctx.func_type(idx as u32); + function_body::translate(&mut session, translation_ctx, &func_ty, &body?)?; } Ok(session.into_translated_code_section()?) }