Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: compact value stack #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [UNRELEASED]

### Changed

- Improved support for WebAssembly 2.0 features
- Simplify and optimize the interpreter loop
- Use a seperate stack and locals for 32, 64 and 128 bit values and references (#21)
- Updated to latest wasmparser version

## [0.7.0] - 2024-05-15

**All Commits**: https://github.com/explodingcamera/tinywasm/compare/v0.6.0...v0.7.0
Expand Down
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ argh="0.1"
color-eyre={version="0.6", default-features=false}
log="0.4"
pretty_env_logger="0.5"
wast={version="211.0", optional=true}
wast={version="212.0", optional=true}

[features]
default=["wat"]
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository.workspace=true
rust-version.workspace=true

[dependencies]
wasmparser={version="0.211", default-features=false, features=["validate"]}
wasmparser={version="0.212", default-features=false, features=["validate"]}
log={version="0.4", optional=true}
tinywasm-types={version="0.7.0", path="../types", default-features=false}

Expand Down
48 changes: 33 additions & 15 deletions crates/parser/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::Result;
use crate::{module::Code, visit::process_operators_and_validate};
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
use tinywasm_types::*;
use wasmparser::{FuncValidator, OperatorsReader, ValidatorResources};
use wasmparser::{FuncValidator, FuncValidatorAllocations, OperatorsReader, ValidatorResources};

pub(crate) fn convert_module_elements<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Element<'a>>>>(
elements: T,
Expand Down Expand Up @@ -168,27 +168,45 @@ pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Ex

pub(crate) fn convert_module_code(
func: wasmparser::FunctionBody<'_>,
validator: &mut FuncValidator<ValidatorResources>,
) -> Result<Code> {
mut validator: FuncValidator<ValidatorResources>,
) -> Result<(Code, FuncValidatorAllocations)> {
let locals_reader = func.get_locals_reader()?;
let count = locals_reader.get_count();
let pos = locals_reader.original_position();

let locals = {
let mut locals = Vec::new();
locals.reserve_exact(count as usize);
for (i, local) in locals_reader.into_iter().enumerate() {
let local = local?;
validator.define_locals(pos + i, local.0, local.1)?;
for _ in 0..local.0 {
locals.push(convert_valtype(&local.1));
// maps a local's address to the index in the type's locals array
let mut local_addr_map = Vec::with_capacity(count as usize);
let mut local_counts = LocalCounts::default();

for (i, local) in locals_reader.into_iter().enumerate() {
let local = local?;
validator.define_locals(pos + i, local.0, local.1)?;
}

for i in 0..validator.len_locals() {
match validator.get_local_type(i) {
Some(wasmparser::ValType::I32) | Some(wasmparser::ValType::F32) => {
local_addr_map.push(local_counts.local_32);
local_counts.local_32 += 1;
}
Some(wasmparser::ValType::I64) | Some(wasmparser::ValType::F64) => {
local_addr_map.push(local_counts.local_64);
local_counts.local_64 += 1;
}
Some(wasmparser::ValType::V128) => {
local_addr_map.push(local_counts.local_128);
local_counts.local_128 += 1;
}
Some(wasmparser::ValType::Ref(_)) => {
local_addr_map.push(local_counts.local_ref);
local_counts.local_ref += 1;
}
None => return Err(crate::ParseError::UnsupportedOperator("Unknown local type".to_string())),
}
locals.into_boxed_slice()
};
}

let body = process_operators_and_validate(validator, func)?;
Ok((body, locals))
let (body, allocations) = process_operators_and_validate(validator, func, local_addr_map)?;
Ok(((body, local_counts), allocations))
}

pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result<FuncType> {
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Parser {
component_model: false,
component_model_nested_names: false,
component_model_values: false,
component_model_more_flags: false,
exceptions: false,
extended_const: false,
gc: false,
Expand Down
11 changes: 6 additions & 5 deletions crates/parser/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use crate::{conversion, ParseError, Result};
use alloc::string::ToString;
use alloc::{boxed::Box, format, vec::Vec};
use tinywasm_types::{
Data, Element, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, TinyWasmModule, ValType,
Data, Element, Export, FuncType, Global, Import, Instruction, LocalCounts, MemoryType, TableType, TinyWasmModule,
WasmFunction,
};
use wasmparser::{FuncValidatorAllocations, Payload, Validator};

pub(crate) type Code = (Box<[Instruction]>, Box<[ValType]>);
pub(crate) type Code = (Box<[Instruction]>, LocalCounts);

#[derive(Default)]
pub(crate) struct ModuleReader {
Expand Down Expand Up @@ -135,9 +135,10 @@ impl ModuleReader {
CodeSectionEntry(function) => {
debug!("Found code section entry");
let v = validator.code_section_entry(&function)?;
let mut func_validator = v.into_validator(self.func_validator_allocations.take().unwrap_or_default());
self.code.push(conversion::convert_module_code(function, &mut func_validator)?);
self.func_validator_allocations = Some(func_validator.into_allocations());
let func_validator = v.into_validator(self.func_validator_allocations.take().unwrap_or_default());
let (code, allocations) = conversion::convert_module_code(function, func_validator)?;
self.code.push(code);
self.func_validator_allocations = Some(allocations);
}
ImportSection(reader) => {
if !self.imports.is_empty() {
Expand Down
Loading