From e74bc0638022b8058c581025b4b5618475fcd537 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 9 Oct 2017 08:37:04 -0700 Subject: [PATCH] Move start_index out of TranslationResult and into the WasmRuntime. This makes it more consistent with how all the rest of the content of a wasm module is handled. And, now TranslationResult just has a Vec of translated functions, which will make it easier to refactor further. --- lib/wasm/src/module_translator.rs | 38 +++++++---------------------- lib/wasm/src/runtime/dummy.rs | 9 +++++++ lib/wasm/src/runtime/spec.rs | 4 +++ lib/wasm/src/sections_translator.rs | 17 +++++++++++++ 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/lib/wasm/src/module_translator.rs b/lib/wasm/src/module_translator.rs index e3395c0b9b..baab08b63b 100644 --- a/lib/wasm/src/module_translator.rs +++ b/lib/wasm/src/module_translator.rs @@ -2,9 +2,9 @@ //! to deal with each part of it. use wasmparser::{ParserState, SectionCode, ParserInput, Parser, WasmDecoder, BinaryReaderError}; use sections_translator::{SectionParsingError, parse_function_signatures, parse_import_section, - parse_function_section, parse_export_section, parse_memory_section, - parse_global_section, parse_table_section, parse_elements_section, - parse_data_section}; + parse_function_section, parse_export_section, parse_start_section, + parse_memory_section, parse_global_section, parse_table_section, + parse_elements_section, parse_data_section}; use translation_utils::FunctionIndex; use cretonne::ir::{Function, FunctionName}; use func_translator::FuncTranslator; @@ -16,11 +16,6 @@ use runtime::WasmRuntime; pub struct TranslationResult { /// The translated functions. pub functions: Vec, - /// When present, the index of the function defined as `start` of the module. - /// - /// Note that this is a WebAssembly function index and not an index into the `functions` vector - /// above. The imported functions are numbered before the local functions. - pub start_index: Option, } /// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cretonne IL @@ -42,7 +37,6 @@ pub fn translate_module( let mut exports: HashMap = HashMap::new(); let mut next_input = ParserInput::Default; let mut function_index: FunctionIndex = 0; - let mut start_index: Option = None; loop { match *parser.read_with_input(next_input) { ParserState::BeginSection { code: SectionCode::Type, .. } => { @@ -108,15 +102,11 @@ pub fn translate_module( next_input = ParserInput::Default; } ParserState::BeginSection { code: SectionCode::Start, .. } => { - match *parser.read() { - ParserState::StartSectionEntry(index) => { - start_index = Some(index as FunctionIndex) + match parse_start_section(&mut parser, runtime) { + Ok(()) => (), + Err(SectionParsingError::WrongSectionContent(s)) => { + return Err(format!("wrong content in the start section: {}", s)) } - _ => return Err(String::from("wrong content in the start section")), - } - match *parser.read() { - ParserState::EndSection => {} - _ => return Err(String::from("wrong content in the start section")), } next_input = ParserInput::Default; } @@ -136,12 +126,7 @@ pub fn translate_module( ParserState::EndSection => { next_input = ParserInput::Default; } - ParserState::EndWasm => { - return Ok(TranslationResult { - functions: Vec::new(), - start_index: None, - }) - } + ParserState::EndWasm => return Ok(TranslationResult { functions: Vec::new() }), ParserState::BeginSection { code: SectionCode::Data, .. } => { match parse_data_section(&mut parser, runtime) { Ok(()) => (), @@ -188,12 +173,7 @@ pub fn translate_module( } } } - ParserState::EndWasm => { - return Ok(TranslationResult { - functions: il_functions, - start_index, - }) - } + ParserState::EndWasm => return Ok(TranslationResult { functions: il_functions }), _ => (), } } diff --git a/lib/wasm/src/runtime/dummy.rs b/lib/wasm/src/runtime/dummy.rs index 6ecbce6cd7..810decce1e 100644 --- a/lib/wasm/src/runtime/dummy.rs +++ b/lib/wasm/src/runtime/dummy.rs @@ -22,6 +22,9 @@ pub struct DummyRuntime { // Compilation setting flags. flags: settings::Flags, + + // The start function. + start_func: Option, } impl DummyRuntime { @@ -38,6 +41,7 @@ impl DummyRuntime { func_types: Vec::new(), imported_funcs: Vec::new(), flags, + start_func: None, } } } @@ -177,6 +181,11 @@ impl WasmRuntime for DummyRuntime { Ok(()) } + fn declare_start_func(&mut self, func_index: FunctionIndex) { + debug_assert!(self.start_func.is_none()); + self.start_func = Some(func_index); + } + fn begin_translation(&mut self) { // We do nothing } diff --git a/lib/wasm/src/runtime/spec.rs b/lib/wasm/src/runtime/spec.rs index 8d92dd9ed3..8c11901f19 100644 --- a/lib/wasm/src/runtime/spec.rs +++ b/lib/wasm/src/runtime/spec.rs @@ -189,6 +189,10 @@ pub trait WasmRuntime: FuncEnvironment { offset: usize, data: &[u8], ) -> Result<(), String>; + + /// Declares a start function. + fn declare_start_func(&mut self, index: FunctionIndex); + /// Call this function after having declared all the runtime elements but prior to the /// function body translation. fn begin_translation(&mut self); diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index b6feca54e4..04338af3e1 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -154,6 +154,23 @@ pub fn parse_export_section( Ok(exports) } +/// Retrieves the start function index from the start section +pub fn parse_start_section( + parser: &mut Parser, + runtime: &mut WasmRuntime, +) -> Result<(), SectionParsingError> { + loop { + match *parser.read() { + ParserState::StartSectionEntry(index) => { + runtime.declare_start_func(index as FunctionIndex); + } + ParserState::EndSection => break, + ref s => return Err(SectionParsingError::WrongSectionContent(format!("{:?}", s))), + }; + } + Ok(()) +} + /// Retrieves the size and maximum fields of memories from the memory section pub fn parse_memory_section( parser: &mut Parser,