diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/tinywasm/src/imports.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/instance.rs | 6 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/executor.rs | 6 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/simd/instructions.rs | 12 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/mod.rs | 58 |
5 files changed, 32 insertions, 54 deletions
diff --git a/crates/tinywasm/src/imports.rs b/crates/tinywasm/src/imports.rs index e5fda8c..18b8f0a 100644 --- a/crates/tinywasm/src/imports.rs +++ b/crates/tinywasm/src/imports.rs @@ -443,7 +443,7 @@ impl Imports { ResolvedExtern::Extern(ex) => match (ex, &import.kind) { (Extern::Global { ty, val }, ImportKind::Global(import_ty)) => { Self::compare_types(import, &ty, import_ty)?; - imports.globals.push(store.add_global(ty, val.into(), idx)?); + imports.globals.push(store.add_global(ty, val.into(), idx)); } (Extern::Table { ty, init }, ImportKind::Table(import_ty)) => { Self::compare_table_types(import, &ty, import_ty)?; @@ -462,7 +462,7 @@ impl Imports { .ok_or_else(|| LinkingError::incompatible_import_type(import))?; Self::compare_types(import, extern_func.ty(), import_func_type)?; - imports.funcs.push(store.add_func(extern_func, idx)?); + imports.funcs.push(store.add_func(extern_func, idx)); } _ => return Err(LinkingError::incompatible_import_type(import).into()), }, diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index a301fe4..bb9ca28 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -146,9 +146,9 @@ impl ModuleInstance { let idx = store.next_module_instance_idx(); let mut addrs = imports.unwrap_or_default().link(store, &module, idx)?; - addrs.funcs.extend(store.init_funcs(&module.0.funcs, idx)?); - addrs.tables.extend(store.init_tables(&module.0.table_types, idx)?); - addrs.memories.extend(store.init_memories(&module.0.memory_types, idx)?); + addrs.funcs.extend(store.init_funcs(&module.0.funcs, idx)); + addrs.tables.extend(store.init_tables(&module.0.table_types, idx)); + addrs.memories.extend(store.init_memories(&module.0.memory_types, idx)); let global_addrs = store.init_globals(addrs.globals, &module.0.globals, &addrs.funcs, idx)?; let (elem_addrs, elem_trapped) = store.init_elements(&addrs.tables, &addrs.funcs, &global_addrs, &module.0.elements, idx)?; diff --git a/crates/tinywasm/src/interpreter/executor.rs b/crates/tinywasm/src/interpreter/executor.rs index d2eee68..3236c0f 100644 --- a/crates/tinywasm/src/interpreter/executor.rs +++ b/crates/tinywasm/src/interpreter/executor.rs @@ -8,9 +8,9 @@ use alloc::{rc::Rc, string::ToString}; use interpreter::stack::CallFrame; use tinywasm_types::*; +use super::ExecState; use super::num_helpers::*; use super::values::*; -use super::ExecState; use crate::engine::FuelPolicy; use crate::instance::ModuleInstanceInner; use crate::interpreter::Value128; @@ -939,7 +939,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { let (dst_memory, src_memory) = self .store .state - .get_mems_mut(self.module.resolve_mem_addr(dst_mem), self.module.resolve_mem_addr(src_mem))?; + .get_mems_mut(self.module.resolve_mem_addr(dst_mem), self.module.resolve_mem_addr(src_mem)); dst_memory.copy_from_slice(dst as usize, src_memory.load(src as usize, size as usize)?)?; } @@ -1002,7 +1002,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { let (dst_table_ref, src_table_ref) = self .store .state - .get_tables_mut(self.module.resolve_table_addr(dst_table), self.module.resolve_table_addr(src_table))?; + .get_tables_mut(self.module.resolve_table_addr(dst_table), self.module.resolve_table_addr(src_table)); dst_table_ref.copy_from_slice(dst as usize, src_table_ref.load(src as usize, size as usize)?) } } diff --git a/crates/tinywasm/src/interpreter/simd/instructions.rs b/crates/tinywasm/src/interpreter/simd/instructions.rs index 97e2360..44e50df 100644 --- a/crates/tinywasm/src/interpreter/simd/instructions.rs +++ b/crates/tinywasm/src/interpreter/simd/instructions.rs @@ -555,7 +555,7 @@ impl Value128 { pub fn i16x8_extadd_pairwise_i8x16_s(self) -> Self { let lanes = self.as_i8x16(); let mut out = [0i16; 8]; - for (dst, pair) in out.iter_mut().zip(lanes.chunks_exact(2)) { + for (dst, pair) in out.iter_mut().zip(lanes.as_chunks::<2>().0) { *dst = pair[0] as i16 + pair[1] as i16; } Self::from_i16x8(out) @@ -565,7 +565,7 @@ impl Value128 { pub fn i16x8_extadd_pairwise_i8x16_u(self) -> Self { let lanes = self.as_u8x16(); let mut out = [0u16; 8]; - for (dst, pair) in out.iter_mut().zip(lanes.chunks_exact(2)) { + for (dst, pair) in out.iter_mut().zip(lanes.as_chunks::<2>().0) { *dst = pair[0] as u16 + pair[1] as u16; } Self::from_u16x8(out) @@ -575,7 +575,7 @@ impl Value128 { pub fn i32x4_extadd_pairwise_i16x8_s(self) -> Self { let lanes = self.as_i16x8(); let mut out = [0i32; 4]; - for (dst, pair) in out.iter_mut().zip(lanes.chunks_exact(2)) { + for (dst, pair) in out.iter_mut().zip(lanes.as_chunks::<2>().0) { *dst = pair[0] as i32 + pair[1] as i32; } Self::from_i32x4(out) @@ -585,7 +585,7 @@ impl Value128 { pub fn i32x4_extadd_pairwise_i16x8_u(self) -> Self { let lanes = self.as_u16x8(); let mut out = [0u32; 4]; - for (dst, pair) in out.iter_mut().zip(lanes.chunks_exact(2)) { + for (dst, pair) in out.iter_mut().zip(lanes.as_chunks::<2>().0) { *dst = pair[0] as u32 + pair[1] as u32; } Self::from_u32x4(out) @@ -712,7 +712,7 @@ impl Value128 { let a = self.as_i16x8(); let b = rhs.as_i16x8(); let mut out = [0i32; 4]; - for (dst, (a_pair, b_pair)) in out.iter_mut().zip(a.chunks_exact(2).zip(b.chunks_exact(2))) { + for (dst, (a_pair, b_pair)) in out.iter_mut().zip(a.as_chunks::<2>().0.iter().zip(b.as_chunks::<2>().0)) { *dst = (a_pair[0] as i32) .wrapping_mul(b_pair[0] as i32) .wrapping_add((a_pair[1] as i32).wrapping_mul(b_pair[1] as i32)); @@ -751,7 +751,7 @@ impl Value128 { let b = rhs.as_i8x16(); let mut out = [0i16; 8]; - for (dst, (a_pair, b_pair)) in out.iter_mut().zip(a.chunks_exact(2).zip(b.chunks_exact(2))) { + for (dst, (a_pair, b_pair)) in out.iter_mut().zip(a.as_chunks::<2>().0.iter().zip(b.as_chunks::<2>().0)) { let prod0 = (a_pair[0] as i16) * (b_pair[0] as i16); let prod1 = (a_pair[1] as i16) * (b_pair[1] as i16); *dst = prod0.wrapping_add(prod1); diff --git a/crates/tinywasm/src/store/mod.rs b/crates/tinywasm/src/store/mod.rs index 9301e9b..8c6289c 100644 --- a/crates/tinywasm/src/store/mod.rs +++ b/crates/tinywasm/src/store/mod.rs @@ -144,14 +144,10 @@ impl State { } /// Get the memory at the actual index in the store - pub(crate) fn get_mems_mut( - &mut self, - addr: MemAddr, - addr2: MemAddr, - ) -> Result<(&mut MemoryInstance, &mut MemoryInstance)> { - match get_pair_mut(&mut self.memories, addr as usize, addr2 as usize) { - Some(mems) => Ok(mems), - None => unreachable!("memory {addr} or {addr2} not found. This should be unreachable"), + pub(crate) fn get_mems_mut(&mut self, addr: MemAddr, addr2: MemAddr) -> (&mut MemoryInstance, &mut MemoryInstance) { + match self.memories.get_disjoint_mut([addr as usize, addr2 as usize]) { + Ok([mem_a, mem_b]) => (mem_a, mem_b), + Err(_) => unreachable!("memory {addr} or {addr2} not found. This should be unreachable"), } } @@ -176,10 +172,10 @@ impl State { &mut self, addr: TableAddr, addr2: TableAddr, - ) -> Result<(&mut TableInstance, &mut TableInstance)> { - match get_pair_mut(&mut self.tables, addr as usize, addr2 as usize) { - Some(tables) => Ok(tables), - None => unreachable!("table {addr} or {addr2} not found. This should be unreachable"), + ) -> (&mut TableInstance, &mut TableInstance) { + match self.tables.get_disjoint_mut([addr as usize, addr2 as usize]) { + Ok([table_a, table_b]) => (table_a, table_b), + Err(_) => unreachable!("table {addr} or {addr2} not found. This should be unreachable"), } } @@ -263,36 +259,36 @@ impl Store { // Linking related functions impl Store { /// Add functions to the store, returning their addresses in the store - pub(crate) fn init_funcs(&mut self, funcs: &[WasmFunction], idx: ModuleInstanceAddr) -> Result<Vec<FuncAddr>> { + pub(crate) fn init_funcs(&mut self, funcs: &[WasmFunction], idx: ModuleInstanceAddr) -> Vec<FuncAddr> { let func_count = self.state.funcs.len(); let mut func_addrs = Vec::with_capacity(func_count); for (i, func) in funcs.iter().enumerate() { self.state.funcs.push(FunctionInstance::new_wasm(func.clone(), idx)); func_addrs.push((i + func_count) as FuncAddr); } - Ok(func_addrs) + func_addrs } /// Add tables to the store, returning their addresses in the store - pub(crate) fn init_tables(&mut self, tables: &[TableType], _idx: ModuleInstanceAddr) -> Result<Vec<TableAddr>> { + pub(crate) fn init_tables(&mut self, tables: &[TableType], _idx: ModuleInstanceAddr) -> Vec<TableAddr> { let table_count = self.state.tables.len(); let mut table_addrs = Vec::with_capacity(table_count); for (i, table) in tables.iter().enumerate() { self.state.tables.push(TableInstance::new(table.clone())); table_addrs.push((i + table_count) as TableAddr); } - Ok(table_addrs) + table_addrs } /// Add memories to the store, returning their addresses in the store - pub(crate) fn init_memories(&mut self, memories: &[MemoryType], _idx: ModuleInstanceAddr) -> Result<Vec<MemAddr>> { + pub(crate) fn init_memories(&mut self, memories: &[MemoryType], _idx: ModuleInstanceAddr) -> Vec<MemAddr> { let mem_count = self.state.memories.len(); let mut mem_addrs = Vec::with_capacity(mem_count); for (i, mem) in memories.iter().enumerate() { self.state.memories.push(MemoryInstance::new(*mem)); mem_addrs.push((i + mem_count) as MemAddr); } - Ok(mem_addrs) + mem_addrs } /// Add globals to the store, returning their addresses in the store @@ -427,14 +423,9 @@ impl Store { Ok((data_addrs.into_boxed_slice(), None)) } - pub(crate) fn add_global( - &mut self, - ty: GlobalType, - value: TinyWasmValue, - _idx: ModuleInstanceAddr, - ) -> Result<Addr> { + pub(crate) fn add_global(&mut self, ty: GlobalType, value: TinyWasmValue, _idx: ModuleInstanceAddr) -> Addr { self.state.globals.push(GlobalInstance::new(ty, value)); - Ok(self.state.globals.len() as Addr - 1) + self.state.globals.len() as Addr - 1 } pub(crate) fn add_table( @@ -461,9 +452,9 @@ impl Store { Ok(self.state.memories.len() as MemAddr - 1) } - pub(crate) fn add_func(&mut self, func: Function, idx: ModuleInstanceAddr) -> Result<FuncAddr> { + pub(crate) fn add_func(&mut self, func: Function, idx: ModuleInstanceAddr) -> FuncAddr { self.state.funcs.push(FunctionInstance { func, owner: idx }); - Ok(self.state.funcs.len() as FuncAddr - 1) + self.state.funcs.len() as FuncAddr - 1 } /// Evaluate a constant expression that's either a i32 or a i64 as a global or a const instruction @@ -593,16 +584,3 @@ impl Store { } } } - -// remove this when the `get_many_mut` function is stabilized -fn get_pair_mut<T>(slice: &mut [T], i: usize, j: usize) -> Option<(&mut T, &mut T)> { - let (first, second) = (core::cmp::min(i, j), core::cmp::max(i, j)); - if i == j || second >= slice.len() { - return None; - } - let (_, tmp) = slice.split_at_mut(first); - let (x, rest) = tmp.split_at_mut(1); - let (_, y) = rest.split_at_mut(second - first - 1); - let pair = if i < j { (&mut x[0], &mut y[0]) } else { (&mut y[0], &mut x[0]) }; - Some(pair) -} |
