From 375f9aa32b117a42c7a9c7f29d3c592b81d966be Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 19 Dec 2023 14:37:51 +0100 Subject: feat: parse memory and table sections Signed-off-by: Henry Gressmann --- crates/parser/src/conversion.rs | 43 ++++++++++++++++++++++++++- crates/parser/src/lib.rs | 13 +++++---- crates/parser/src/module.rs | 65 +++++++++++++++++++++-------------------- 3 files changed, 83 insertions(+), 38 deletions(-) (limited to 'crates/parser/src') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 9f4930a..fb48321 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,12 +1,53 @@ use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use log::info; use tinywasm_types::{ - BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, ValType, + BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, MemoryArch, MemoryType, + TableType, ValType, }; use wasmparser::{FuncValidator, ValidatorResources}; use crate::{module::CodeSection, Result}; +pub(crate) fn convert_module_memories>>( + memory_types: T, +) -> Result> { + let memory_type = memory_types + .into_iter() + .map(|memory| { + let memory = memory?; + Ok(MemoryType { + arch: match memory.memory64 { + true => MemoryArch::I64, + false => MemoryArch::I32, + }, + page_count_initial: memory.initial, + page_count_max: memory.maximum, + }) + }) + .collect::>>()?; + + Ok(memory_type) +} + +pub(crate) fn convert_module_tables>>( + table_types: T, +) -> Result> { + let table_type = table_types + .into_iter() + .map(|table| { + let table = table?; + let ty = convert_valtype(&table.element_type); + Ok(TableType { + element_type: ty, + size_initial: table.initial, + size_max: table.maximum, + }) + }) + .collect::>>()?; + + Ok(table_type) +} + pub(crate) fn convert_module_globals<'a, T: IntoIterator>>>( globals: T, ) -> Result> { diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index bcaef76..36deb7f 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -102,9 +102,9 @@ impl TryFrom for TinyWasmModule { return Err(ParseError::EndNotReached); } - let func_types = reader.function_section; + let func_types = reader.func_addrs; let funcs = reader - .code_section + .code .into_iter() .zip(func_types) .map(|(f, ty)| Function { @@ -114,15 +114,18 @@ impl TryFrom for TinyWasmModule { }) .collect::>(); - let globals = reader.global_section; + let globals = reader.globals; + let table_types = reader.table_types; Ok(TinyWasmModule { version: reader.version, start_func: reader.start_func, - types: reader.type_section.into_boxed_slice(), + func_types: reader.func_types.into_boxed_slice(), funcs: funcs.into_boxed_slice(), - exports: reader.export_section.into_boxed_slice(), + exports: reader.exports.into_boxed_slice(), globals: globals.into_boxed_slice(), + table_types: table_types.into_boxed_slice(), + memory_types: reader.memory_types.into_boxed_slice(), }) } } diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index 6f823de..ea6aaad 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -1,7 +1,7 @@ use crate::log::debug; use alloc::{boxed::Box, format, vec::Vec}; use core::fmt::Debug; -use tinywasm_types::{Export, FuncType, Global, Instruction, ValType}; +use tinywasm_types::{Export, FuncType, Global, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; use crate::{conversion, ParseError, Result}; @@ -17,14 +17,14 @@ pub struct ModuleReader { pub version: Option, pub start_func: Option, - pub type_section: Vec, - pub function_section: Vec, - pub export_section: Vec, - pub code_section: Vec, - pub global_section: Vec, + pub func_types: Vec, + pub func_addrs: Vec, + pub exports: Vec, + pub code: Vec, + pub globals: Vec, + pub table_types: Vec, + pub memory_types: Vec, - // pub table_section: Option>, - // pub memory_section: Option>, // pub element_section: Option>, // pub data_section: Option>, // pub import_section: Option>, @@ -35,13 +35,13 @@ impl Debug for ModuleReader { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { f.debug_struct("ModuleReader") .field("version", &self.version) - .field("type_section", &self.type_section) - .field("function_section", &self.function_section) - .field("code_section", &self.code_section) - .field("export_section", &self.export_section) - .field("global_section", &self.global_section) - // .field("table_section", &self.table_section) - // .field("memory_section", &self.memory_section) + .field("func_types", &self.func_types) + .field("func_addrs", &self.func_addrs) + .field("code", &self.code) + .field("exports", &self.exports) + .field("globals", &self.globals) + .field("table_types", &self.table_types) + .field("memory_types", &self.memory_types) // .field("element_section", &self.element_section) // .field("data_section", &self.data_section) // .field("import_section", &self.import_section) @@ -74,7 +74,7 @@ impl ModuleReader { TypeSection(reader) => { debug!("Found type section"); validator.type_section(&reader)?; - self.type_section = reader + self.func_types = reader .into_iter() .map(|t| conversion::convert_module_type(t?)) .collect::>>()?; @@ -82,26 +82,23 @@ impl ModuleReader { FunctionSection(reader) => { debug!("Found function section"); validator.function_section(&reader)?; - self.function_section = reader.into_iter().map(|f| Ok(f?)).collect::>>()?; + self.func_addrs = reader.into_iter().map(|f| Ok(f?)).collect::>>()?; } GlobalSection(reader) => { debug!("Found global section"); validator.global_section(&reader)?; - self.global_section = conversion::convert_module_globals(reader)?; + self.globals = conversion::convert_module_globals(reader)?; } - TableSection(_reader) => { - return Err(ParseError::UnsupportedSection("Table section".into())); - // debug!("Found table section"); - // validator.table_section(&reader)?; - // self.table_section = Some(reader); + TableSection(reader) => { + debug!("Found table section"); + validator.table_section(&reader)?; + self.table_types = conversion::convert_module_tables(reader)?; } - MemorySection(_reader) => { - return Err(ParseError::UnsupportedSection("Memory section".into())); - // debug!("Found memory section"); - // validator.memory_section(&reader)?; - // self.memory_section = Some(reader); + MemorySection(reader) => { + debug!("Found memory section"); + validator.memory_section(&reader)?; + self.memory_types = conversion::convert_module_memories(reader)?; } - ElementSection(_reader) => { return Err(ParseError::UnsupportedSection("Element section".into())); // debug!("Found element section"); @@ -116,7 +113,7 @@ impl ModuleReader { } CodeSectionStart { count, range, .. } => { debug!("Found code section ({} functions)", count); - if !self.code_section.is_empty() { + if !self.code.is_empty() { return Err(ParseError::DuplicateSection("Code section".into())); } @@ -127,7 +124,7 @@ impl ModuleReader { let v = validator.code_section_entry(&function)?; let func_validator = v.into_validator(Default::default()); - self.code_section + self.code .push(conversion::convert_module_code(function, func_validator)?); } ImportSection(_reader) => { @@ -140,7 +137,7 @@ impl ModuleReader { ExportSection(reader) => { debug!("Found export section"); validator.export_section(&reader)?; - self.export_section = reader + self.exports = reader .into_iter() .map(|e| conversion::convert_module_export(e?)) .collect::>>()?; @@ -158,6 +155,10 @@ impl ModuleReader { debug!("Found custom section"); debug!("Skipping custom section: {:?}", reader.name()); } + // TagSection(tag) => { + // debug!("Found tag section"); + // validator.tag_section(&tag)?; + // } UnknownSection { .. } => return Err(ParseError::UnsupportedSection("Unknown section".into())), section => { return Err(ParseError::UnsupportedSection(format!( -- cgit v1.3.1