Skip to content

Commit dc36616

Browse files
chore: simplify runtime, parser, types, and SIMD internals
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent fe43e48 commit dc36616

27 files changed

Lines changed: 749 additions & 1697 deletions

‎crates/parser/src/conversion.rs‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,11 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
5858
pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Import> {
5959
let kind = match import.ty {
6060
wasmparser::TypeRef::Func(ty) => ImportKind::Function(ty),
61-
wasmparser::TypeRef::Table(ty) => ImportKind::Table(TableType {
62-
element_type: convert_reftype(ty.element_type)?,
63-
size_initial: ty.initial.try_into().map_err(|_| {
64-
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", ty.initial))
65-
})?,
66-
size_max: match ty.maximum {
67-
Some(max) => Some(max.try_into().map_err(|_| {
68-
crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {max}"))
69-
})?),
70-
None => None,
71-
},
72-
}),
61+
wasmparser::TypeRef::Table(ty) => {
62+
let element_type = convert_reftype(ty.element_type)?;
63+
let (size_initial, size_max) = convert_table_limits(ty)?;
64+
ImportKind::Table(TableType { element_type, size_initial, size_max })
65+
}
7366
wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)),
7467
wasmparser::TypeRef::Global(ty) => {
7568
ImportKind::Global(GlobalType::new(convert_valtype(&ty.content_type)?, ty.mutable))
@@ -95,16 +88,24 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> MemoryTyp
9588
}
9689

9790
pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result<TableType> {
98-
let size_initial = table.ty.initial.try_into().map_err(|_| {
99-
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", table.ty.initial))
100-
})?;
101-
102-
let size_max = table.ty.maximum.map(|max| max.try_into()).transpose();
103-
let size_max =
104-
size_max.map_err(|e| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {e}")))?;
91+
let (size_initial, size_max) = convert_table_limits(table.ty)?;
10592
Ok(TableType { element_type: convert_reftype(table.ty.element_type)?, size_initial, size_max })
10693
}
10794

95+
fn convert_table_limits(table: wasmparser::TableType) -> Result<(u32, Option<u32>)> {
96+
let size_initial = table.initial.try_into().map_err(|_| {
97+
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", table.initial))
98+
})?;
99+
let size_max = table
100+
.maximum
101+
.map(|max| {
102+
u32::try_from(max)
103+
.map_err(|_| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {max}")))
104+
})
105+
.transpose()?;
106+
Ok((size_initial, size_max))
107+
}
108+
108109
pub(crate) fn convert_module_globals(
109110
globals: wasmparser::SectionLimited<'_, wasmparser::Global<'_>>,
110111
) -> Result<Box<[Global]>> {
@@ -153,7 +154,7 @@ pub(crate) fn convert_module_code(
153154

154155
for i in 0..validator.len_locals() {
155156
match validator.get_local_type(i) {
156-
Some(wasmparser::ValType::I32 | wasmparser::ValType::F32) => {
157+
Some(wasmparser::ValType::I32 | wasmparser::ValType::F32 | wasmparser::ValType::Ref(_)) => {
157158
local_addr_map.push(local_counts.c32);
158159
local_counts.c32 += 1;
159160
}
@@ -165,10 +166,6 @@ pub(crate) fn convert_module_code(
165166
local_addr_map.push(local_counts.c128);
166167
local_counts.c128 += 1;
167168
}
168-
Some(wasmparser::ValType::Ref(_)) => {
169-
local_addr_map.push(local_counts.c32);
170-
local_counts.c32 += 1;
171-
}
172169
None => return Err(crate::ParseError::UnsupportedOperator("Unknown local type".to_string())),
173170
}
174171
}

‎crates/parser/src/lib.rs‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,15 @@ impl Parser {
250250

251251
#[cfg(not(parallel_parser))]
252252
let _ = defer;
253-
254-
buffer.drain(..consumed);
255253
}
256254
wasmparser::Payload::CodeSectionEntry(function) => {
257255
reader.process_inline_code_section_entry(function, &mut validator, &self.options)?;
258-
buffer.drain(..consumed);
259256
}
260257
payload => {
261258
reader.process_payload(payload, &mut validator)?;
262-
buffer.drain(..consumed);
263259
}
264260
}
261+
buffer.drain(..consumed);
265262

266263
#[cfg(parallel_parser)]
267264
if let Some((count, body_offset, section_size)) = deferred_code_section {
@@ -296,12 +293,9 @@ impl Parser {
296293
return Err(ParseError::Other("trailing bytes after end of module".into()));
297294
}
298295
}
299-
300-
reader.process_pending_functions(&self.options)?;
301-
return reader.into_module(&self.options);
302296
}
303297

304-
if eof {
298+
if reader.end_reached || eof {
305299
reader.process_pending_functions(&self.options)?;
306300
return reader.into_module(&self.options);
307301
}
@@ -321,20 +315,17 @@ impl TryFrom<ModuleReader<'_>> for Module {
321315

322316
/// Parse a module from bytes
323317
pub fn parse_bytes(wasm: &[u8]) -> Result<Module> {
324-
let data = Parser::new().parse_module_bytes(wasm)?;
325-
Ok(data)
318+
Parser::new().parse_module_bytes(wasm)
326319
}
327320

328321
#[cfg(feature = "std")]
329322
/// Parse a module from a file. Requires the `std` feature.
330323
pub fn parse_file(path: impl AsRef<crate::std::path::Path> + Clone) -> Result<Module> {
331-
let data = Parser::new().parse_module_file(path)?;
332-
Ok(data)
324+
Parser::new().parse_module_file(path)
333325
}
334326

335327
#[cfg(feature = "std")]
336328
/// Parse a module from a stream. Requires `parser` and `std` features.
337329
pub fn parse_stream(stream: impl crate::std::io::Read) -> Result<Module> {
338-
let data = Parser::new().parse_module_stream(stream)?;
339-
Ok(data)
330+
Parser::new().parse_module_stream(stream)
340331
}

‎crates/parser/src/macros.rs‎

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -105,61 +105,18 @@ pub(crate) mod visit {
105105

106106
pub(crate) mod optimize {
107107
macro_rules! replace {
108-
($instructions:ident, $read:ident, 1 => [$a:expr $(,)?]) => {{
109-
$instructions[$read - 1] = Instruction::Nop;
110-
$instructions[$read] = $a;
111-
}};
112-
($instructions:ident, $read:ident, 1 => [$a:expr, $b:expr $(,)?]) => {{
113-
$instructions[$read - 1] = $a;
114-
$instructions[$read] = $b;
115-
}};
116-
($instructions:ident, $read:ident, 2 => [$a:expr $(,)?]) => {{
117-
$instructions[$read - 2] = Instruction::Nop;
118-
$instructions[$read - 1] = Instruction::Nop;
119-
$instructions[$read] = $a;
120-
}};
121-
($instructions:ident, $read:ident, 2 => [$a:expr, $b:expr $(,)?]) => {{
122-
$instructions[$read - 2] = Instruction::Nop;
123-
$instructions[$read - 1] = $a;
124-
$instructions[$read] = $b;
125-
}};
126-
($instructions:ident, $read:ident, 2 => [$a:expr, $b:expr, $c:expr $(,)?]) => {{
127-
$instructions[$read - 2] = $a;
128-
$instructions[$read - 1] = $b;
129-
$instructions[$read] = $c;
130-
}};
131-
($instructions:ident, $read:ident, 3 => [$a:expr $(,)?]) => {{
132-
$instructions[$read - 3] = Instruction::Nop;
133-
$instructions[$read - 2] = Instruction::Nop;
134-
$instructions[$read - 1] = Instruction::Nop;
135-
$instructions[$read] = $a;
136-
}};
137-
($instructions:ident, $read:ident, 3 => [$a:expr, $b:expr $(,)?]) => {{
138-
$instructions[$read - 3] = Instruction::Nop;
139-
$instructions[$read - 2] = Instruction::Nop;
140-
$instructions[$read - 1] = $a;
141-
$instructions[$read] = $b;
142-
}};
143-
($instructions:ident, $read:ident, 3 => [$a:expr, $b:expr, $c:expr $(,)?]) => {{
144-
$instructions[$read - 3] = Instruction::Nop;
145-
$instructions[$read - 2] = $a;
146-
$instructions[$read - 1] = $b;
147-
$instructions[$read] = $c;
148-
}};
149-
($instructions:ident, $read:ident, 3 => [$a:expr, $b:expr, $c:expr, $d:expr $(,)?]) => {{
150-
$instructions[$read - 3] = $a;
151-
$instructions[$read - 2] = $b;
152-
$instructions[$read - 1] = $c;
153-
$instructions[$read] = $d;
108+
($instructions:ident, $read:ident, $consumed:literal => [$($out:expr),+ $(,)?]) => {{
109+
const {
110+
assert!($consumed >= 1 && $consumed <= 3);
111+
assert!([$(stringify!($out)),+].len() <= $consumed + 1);
112+
}
113+
let replacements = [$($out),+];
114+
let replacement_start = $read + 1 - replacements.len();
115+
$instructions[$read - $consumed..replacement_start].fill(Instruction::Nop);
116+
$instructions[replacement_start..=$read].copy_from_slice(&replacements);
154117
}};
155-
($instructions:ident, $read:ident, 1 => $out:expr) => {
156-
replace!($instructions, $read, 1 => [$out]);
157-
};
158-
($instructions:ident, $read:ident, 2 => $out:expr) => {
159-
replace!($instructions, $read, 2 => [$out]);
160-
};
161-
($instructions:ident, $read:ident, 3 => $out:expr) => {
162-
replace!($instructions, $read, 3 => [$out]);
118+
($instructions:ident, $read:ident, $consumed:literal => $out:expr) => {
119+
replace!($instructions, $read, $consumed => [$out]);
163120
};
164121
}
165122

0 commit comments

Comments
 (0)