Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 1cca6de

Browse files
chore: cleanup simd stack code
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent bcb8ebf commit 1cca6de

File tree

16 files changed

+108
-109
lines changed

16 files changed

+108
-109
lines changed

crates/parser/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ tinywasm-types={version="0.7.0", path="../types", default-features=false}
1616
default=["std", "logging"]
1717
logging=["log"]
1818
std=["tinywasm-types/std", "wasmparser/std"]
19-
nightly=[]

crates/tinywasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ logging=["_log", "tinywasm-parser?/logging", "tinywasm-types/logging"]
3333
std=["tinywasm-parser?/std", "tinywasm-types/std"]
3434
parser=["tinywasm-parser"]
3535
archive=["tinywasm-types/archive"]
36-
nightly=[]
36+
simd=[]
3737

3838
[[test]]
3939
name="test-mvp"

crates/tinywasm/src/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::runtime::{CallFrame, Stack, WasmValueRepr};
1+
use crate::runtime::{CallFrame, Stack};
22
use crate::{log, runtime::RawWasmValue, unlikely, Function};
33
use crate::{Error, FuncContext, Result, Store};
44
use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec};

crates/tinywasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
))]
66
#![allow(unexpected_cfgs, clippy::reserve_after_initialization)]
77
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
8-
#![cfg_attr(nightly, feature(error_in_core))]
8+
#![cfg_attr(nightly, feature(error_in_core, portable_simd))]
99
#![forbid(unsafe_code)]
1010

1111
//! A tiny WebAssembly Runtime written in Rust

crates/tinywasm/src/runtime/interpreter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use alloc::string::ToString;
33
use core::ops::{BitAnd, BitOr, BitXor, Neg};
44
use tinywasm_types::{BlockArgs, ElementKind, Instruction, ValType};
55

6+
use super::stack::{BlockFrame, BlockType};
67
use super::{InterpreterRuntime, RawWasmValue, Stack};
7-
use crate::runtime::{BlockFrame, BlockType, CallFrame};
8+
use crate::runtime::CallFrame;
89
use crate::{cold, unlikely};
910
use crate::{Error, FuncContext, ModuleInstance, Result, Store, Trap};
1011

@@ -724,7 +725,6 @@ impl<'store, 'stack> Executor<'store, 'stack> {
724725
instr_ptr,
725726
end_instr_offset,
726727
stack_ptr: self.stack.values.len() as u32 - params as u32,
727-
large_stack_ptr: 0,
728728
results,
729729
params,
730730
ty,

crates/tinywasm/src/runtime/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
mod interpreter;
22
mod stack;
3-
mod value;
3+
4+
mod raw;
5+
6+
#[cfg(all(nightly, feature = "simd"))]
7+
mod raw_simd;
48

59
use crate::Result;
6-
pub use stack::*;
7-
pub use value::{LargeRawWasmValue, RawWasmValue, WasmValueRepr};
10+
11+
pub use raw::RawWasmValue;
12+
pub(crate) use stack::CallFrame;
13+
pub(crate) use stack::Stack;
814

915
#[allow(rustdoc::private_intra_doc_links)]
1016
/// A WebAssembly runtime.
1117
///
1218
/// See <https://webassembly.github.io/spec/core/exec/runtime.html>
1319
pub trait Runtime {
1420
/// Execute all call-frames on the stack until the stack is empty.
15-
fn exec(&self, store: &mut crate::Store, stack: &mut crate::runtime::Stack) -> Result<()>;
21+
fn exec(&self, store: &mut crate::Store, stack: &mut Stack) -> Result<()>;
1622
}
1723

1824
/// The main TinyWasm runtime.

crates/tinywasm/src/runtime/value.rs renamed to crates/tinywasm/src/runtime/raw.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,28 @@ use tinywasm_types::{ValType, WasmValue};
99
#[derive(Clone, Copy, Default, PartialEq, Eq)]
1010
pub struct RawWasmValue([u8; 8]);
1111

12-
/// A large raw wasm value, used for 128-bit values.
13-
///
14-
/// This is the internal representation of vector values.
15-
///
16-
/// See [`WasmValue`] for the public representation.
17-
#[derive(Clone, Copy, Default, PartialEq, Eq)]
18-
pub struct LargeRawWasmValue([u8; 16]);
19-
2012
impl Debug for RawWasmValue {
2113
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2214
write!(f, "RawWasmValue({})", 0)
2315
}
2416
}
2517

26-
impl Debug for LargeRawWasmValue {
27-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28-
write!(f, "LargeRawWasmValue({})", 0)
18+
impl RawWasmValue {
19+
#[inline(always)]
20+
/// Get the raw value
21+
pub fn raw_value(&self) -> [u8; 8] {
22+
self.0
2923
}
30-
}
31-
32-
pub trait WasmValueRepr {
33-
fn attach_type(self, ty: ValType) -> WasmValue;
34-
}
3524

36-
impl WasmValueRepr for RawWasmValue {
3725
#[inline]
38-
fn attach_type(self, ty: ValType) -> WasmValue {
26+
/// Attach a type to the raw value (does not support simd values)
27+
pub fn attach_type(self, ty: ValType) -> WasmValue {
3928
match ty {
4029
ValType::I32 => WasmValue::I32(self.into()),
4130
ValType::I64 => WasmValue::I64(self.into()),
4231
ValType::F32 => WasmValue::F32(f32::from_bits(self.into())),
4332
ValType::F64 => WasmValue::F64(f64::from_bits(self.into())),
33+
ValType::V128 => panic!("RawWasmValue cannot be converted to V128"),
4434
ValType::RefExtern => match i64::from(self) {
4535
v if v < 0 => WasmValue::RefNull(ValType::RefExtern),
4636
addr => WasmValue::RefExtern(addr as u32),
@@ -53,13 +43,6 @@ impl WasmValueRepr for RawWasmValue {
5343
}
5444
}
5545

56-
impl RawWasmValue {
57-
#[inline(always)]
58-
pub fn raw_value(&self) -> [u8; 8] {
59-
self.0
60-
}
61-
}
62-
6346
impl From<WasmValue> for RawWasmValue {
6447
#[inline]
6548
fn from(v: WasmValue) -> Self {
@@ -68,6 +51,7 @@ impl From<WasmValue> for RawWasmValue {
6851
WasmValue::I64(i) => Self::from(i),
6952
WasmValue::F32(i) => Self::from(i),
7053
WasmValue::F64(i) => Self::from(i),
54+
WasmValue::V128(_) => panic!("RawWasmValue cannot be converted to V128"),
7155
WasmValue::RefExtern(v) => Self::from(v as i64),
7256
WasmValue::RefFunc(v) => Self::from(v as i64),
7357
WasmValue::RefNull(_) => Self::from(-1i64),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// A large raw wasm value, used for 128-bit values.
2+
///
3+
/// This is the internal representation of vector values.
4+
///
5+
/// See [`WasmValue`] for the public representation.
6+
#[derive(Clone, Copy, Default, PartialEq, Eq)]
7+
pub struct RawSimdWasmValue([u8; 16]);
8+
9+
impl Debug for RawSimdWasmValue {
10+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11+
write!(f, "LargeRawWasmValue({})", 0)
12+
}
13+
}

crates/tinywasm/src/runtime/stack/block_stack.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ pub(crate) struct BlockFrame {
5555
pub(crate) instr_ptr: usize, // position of the instruction pointer when the block was entered
5656
pub(crate) end_instr_offset: u32, // position of the end instruction of the block
5757

58-
pub(crate) stack_ptr: u32, // position of the stack pointer when the block was entered
59-
pub(crate) large_stack_ptr: u32, // position of the large stack pointer when the block was entered
58+
pub(crate) stack_ptr: u32, // position of the stack pointer when the block was entered
59+
60+
#[cfg(all(nightly, feature = "simd"))]
61+
pub(crate) simd_stack_ptr: u32, // position of the large stack pointer when the block was entered
6062

6163
pub(crate) results: u8,
6264
pub(crate) params: u8,

crates/tinywasm/src/runtime/stack/call_stack.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::runtime::{BlockType, RawWasmValue};
1+
use crate::runtime::RawWasmValue;
22
use crate::{cold, unlikely};
33
use crate::{Error, Result, Trap};
44
use alloc::{boxed::Box, rc::Rc, vec::Vec};
55
use tinywasm_types::{Instruction, LocalAddr, ModuleInstanceAddr, WasmFunction};
66

7+
use super::BlockType;
8+
79
const CALL_STACK_SIZE: usize = 1024;
810

911
#[derive(Debug)]
@@ -72,7 +74,7 @@ impl CallFrame {
7274
pub(crate) fn break_to(
7375
&mut self,
7476
break_to_relative: u32,
75-
values: &mut super::ValueStack<RawWasmValue>,
77+
values: &mut super::ValueStack,
7678
blocks: &mut super::BlockStack,
7779
) -> Option<()> {
7880
let break_to = blocks.get_relative_to(break_to_relative, self.block_ptr)?;

0 commit comments

Comments
 (0)