summaryrefslogtreecommitdiff
path: root/crates/types
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-03-31 20:21:18 +0200
committerHenry <mail@henrygressmann.de>2026-03-31 20:21:18 +0200
commit2e40140c053c481820f99ce982201f8a68d21a54 (patch)
treed79fe1d13a2b6c4f52aab4242ae0ee92a93c0138 /crates/types
parent99f5168bbe5d1dc2bc24e2c5e76e1be4599cabde (diff)
chore: move locals to stack
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/types')
-rw-r--r--crates/types/src/instructions.rs24
-rw-r--r--crates/types/src/lib.rs37
-rw-r--r--crates/types/src/value.rs30
3 files changed, 53 insertions, 38 deletions
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs
index f3acbf7..b884319 100644
--- a/crates/types/src/instructions.rs
+++ b/crates/types/src/instructions.rs
@@ -4,22 +4,26 @@ use crate::{ConstIdx, DataAddr, ElemAddr, ExternAddr, MemAddr};
/// Represents a memory immediate in a WebAssembly memory instruction.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))]
-pub struct MemoryArg([u8; 12]);
+#[repr(Rust, packed)]
+pub struct MemoryArg {
+ offset: u64,
+ mem_addr: MemAddr,
+}
impl MemoryArg {
- pub fn new(offset: u64, mem_addr: MemAddr) -> Self {
- let mut bytes = [0; 12];
- bytes[0..8].copy_from_slice(&offset.to_le_bytes());
- bytes[8..12].copy_from_slice(&mem_addr.to_le_bytes());
- Self(bytes)
+ #[inline]
+ pub const fn new(offset: u64, mem_addr: MemAddr) -> Self {
+ Self { offset, mem_addr }
}
- pub fn offset(&self) -> u64 {
- u64::from_le_bytes(self.0[0..8].try_into().expect("invalid offset"))
+ #[inline]
+ pub const fn offset(self) -> u64 {
+ self.offset
}
- pub fn mem_addr(&self) -> MemAddr {
- MemAddr::from_le_bytes(self.0[8..12].try_into().expect("invalid mem_addr"))
+ #[inline]
+ pub const fn mem_addr(self) -> MemAddr {
+ self.mem_addr
}
}
diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs
index ba2cb56..7112834 100644
--- a/crates/types/src/lib.rs
+++ b/crates/types/src/lib.rs
@@ -169,7 +169,8 @@ pub enum ExternVal {
}
impl ExternVal {
- pub fn kind(&self) -> ExternalKind {
+ #[inline]
+ pub const fn kind(&self) -> ExternalKind {
match self {
Self::Func(_) => ExternalKind::Func,
Self::Table(_) => ExternalKind::Table,
@@ -178,7 +179,8 @@ impl ExternVal {
}
}
- pub fn new(kind: ExternalKind, addr: Addr) -> Self {
+ #[inline]
+ pub const fn new(kind: ExternalKind, addr: Addr) -> Self {
match kind {
ExternalKind::Func => Self::Func(addr),
ExternalKind::Table => Self::Table(addr),
@@ -217,6 +219,7 @@ pub struct ValueCountsSmall {
}
impl<'a, T: IntoIterator<Item = &'a ValType>> From<T> for ValueCounts {
+ #[inline]
fn from(types: T) -> Self {
let mut counts = Self::default();
for ty in types {
@@ -232,6 +235,7 @@ impl<'a, T: IntoIterator<Item = &'a ValType>> From<T> for ValueCounts {
}
impl<'a, T: IntoIterator<Item = &'a ValType>> From<T> for ValueCountsSmall {
+ #[inline]
fn from(types: T) -> Self {
let mut counts = Self::default();
for ty in types {
@@ -363,31 +367,42 @@ pub struct MemoryType {
}
impl MemoryType {
- pub fn new(arch: MemoryArch, page_count_initial: u64, page_count_max: Option<u64>, page_size: Option<u64>) -> Self {
+ pub const fn new(
+ arch: MemoryArch,
+ page_count_initial: u64,
+ page_count_max: Option<u64>,
+ page_size: Option<u64>,
+ ) -> Self {
Self { arch, page_count_initial, page_count_max, page_size }
}
- pub fn arch(&self) -> MemoryArch {
+ #[inline]
+ pub const fn arch(&self) -> MemoryArch {
self.arch
}
- pub fn page_count_initial(&self) -> u64 {
+ #[inline]
+ pub const fn page_count_initial(&self) -> u64 {
self.page_count_initial
}
- pub fn page_count_max(&self) -> u64 {
- self.page_count_max.unwrap_or_else(|| max_page_count(self.page_size()))
+ #[inline]
+ pub const fn page_count_max(&self) -> u64 {
+ if let Some(page_count_max) = self.page_count_max { page_count_max } else { max_page_count(self.page_size()) }
}
- pub fn page_size(&self) -> u64 {
- self.page_size.unwrap_or(MEM_PAGE_SIZE)
+ #[inline]
+ pub const fn page_size(&self) -> u64 {
+ if let Some(page_size) = self.page_size { page_size } else { MEM_PAGE_SIZE }
}
- pub fn initial_size(&self) -> u64 {
+ #[inline]
+ pub const fn initial_size(&self) -> u64 {
self.page_count_initial * self.page_size()
}
- pub fn max_size(&self) -> u64 {
+ #[inline]
+ pub const fn max_size(&self) -> u64 {
self.page_count_max() * self.page_size()
}
}
diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs
index aa9e8c5..7b58948 100644
--- a/crates/types/src/value.rs
+++ b/crates/types/src/value.rs
@@ -106,7 +106,7 @@ impl ExternRef {
impl WasmValue {
#[doc(hidden)]
#[inline]
- pub fn const_instr(&self) -> ConstInstruction {
+ pub const fn const_instr(&self) -> ConstInstruction {
match self {
Self::I32(i) => ConstInstruction::I32Const(*i),
Self::I64(i) => ConstInstruction::I64Const(*i),
@@ -114,13 +114,13 @@ impl WasmValue {
Self::F64(i) => ConstInstruction::F64Const(*i),
Self::V128(i) => ConstInstruction::V128Const(*i),
Self::RefFunc(i) => ConstInstruction::RefFunc(i.addr()),
- Self::RefExtern(_) => unimplemented!("no const_instr for RefExtern"),
+ Self::RefExtern(i) => ConstInstruction::RefExtern(i.addr()),
}
}
/// Get the default value for a given type.
#[inline]
- pub fn default_for(ty: ValType) -> Self {
+ pub const fn default_for(ty: ValType) -> Self {
match ty {
ValType::I32 => Self::I32(0),
ValType::I64 => Self::I64(0),
@@ -212,7 +212,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_i32(&self) -> Option<i32> {
+ pub const fn as_i32(&self) -> Option<i32> {
match self {
Self::I32(i) => Some(*i),
_ => None,
@@ -220,7 +220,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_i64(&self) -> Option<i64> {
+ pub const fn as_i64(&self) -> Option<i64> {
match self {
Self::I64(i) => Some(*i),
_ => None,
@@ -228,7 +228,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_f32(&self) -> Option<f32> {
+ pub const fn as_f32(&self) -> Option<f32> {
match self {
Self::F32(i) => Some(*i),
_ => None,
@@ -236,7 +236,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_f64(&self) -> Option<f64> {
+ pub const fn as_f64(&self) -> Option<f64> {
match self {
Self::F64(i) => Some(*i),
_ => None,
@@ -244,7 +244,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_v128(&self) -> Option<i128> {
+ pub const fn as_v128(&self) -> Option<i128> {
match self {
Self::V128(i) => Some(*i),
_ => None,
@@ -252,7 +252,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_ref_extern(&self) -> Option<ExternRef> {
+ pub const fn as_ref_extern(&self) -> Option<ExternRef> {
match self {
Self::RefExtern(ref_extern) => Some(*ref_extern),
_ => None,
@@ -260,7 +260,7 @@ impl WasmValue {
}
#[doc(hidden)]
- pub fn as_ref_func(&self) -> Option<FuncRef> {
+ pub const fn as_ref_func(&self) -> Option<FuncRef> {
match self {
Self::RefFunc(ref_func) => Some(*ref_func),
_ => None,
@@ -268,9 +268,6 @@ impl WasmValue {
}
}
-#[cold]
-fn cold() {}
-
impl Debug for WasmValue {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
@@ -288,7 +285,7 @@ impl Debug for WasmValue {
impl WasmValue {
/// Get the type of a [`WasmValue`]
#[inline]
- pub fn val_type(&self) -> ValType {
+ pub const fn val_type(&self) -> ValType {
match self {
Self::I32(_) => ValType::I32,
Self::I64(_) => ValType::I64,
@@ -323,13 +320,13 @@ pub enum ValType {
impl ValType {
#[inline]
- pub fn default_value(&self) -> WasmValue {
+ pub const fn default_value(&self) -> WasmValue {
WasmValue::default_for(*self)
}
#[doc(hidden)]
#[inline]
- pub fn is_simd(&self) -> bool {
+ pub const fn is_simd(&self) -> bool {
matches!(self, Self::V128)
}
}
@@ -354,7 +351,6 @@ macro_rules! impl_conversion_for_wasmvalue {
if let WasmValue::$variant(i) = value {
Ok(i)
} else {
- cold();
Err(())
}
}