summaryrefslogtreecommitdiff
path: root/crates/types/src
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2024-02-27 17:04:23 +0100
committerGitHub <noreply@github.com>2024-02-27 17:04:23 +0100
commitab2ae9b445268d503bde3dbb7a91886c55c19144 (patch)
treeb2644fc05d37b320035a2997867de61f804f5b6c /crates/types/src
parent43e6d23ae8806c813dd5fa0663c17c1772ebf5a8 (diff)
parent417bb70c0f8c8f8781f675481e5336affd209183 (diff)
feat: rework instructions (#4)
Diffstat (limited to 'crates/types/src')
-rw-r--r--crates/types/src/archive.rs2
-rw-r--r--crates/types/src/instructions.rs166
-rw-r--r--crates/types/src/value.rs31
3 files changed, 166 insertions, 33 deletions
diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs
index bbd2206..273d6ed 100644
--- a/crates/types/src/archive.rs
+++ b/crates/types/src/archive.rs
@@ -7,10 +7,8 @@ use rkyv::{
Deserialize,
};
-// 16 bytes
const TWASM_MAGIC_PREFIX: &[u8; 4] = b"TWAS";
const TWASM_VERSION: &[u8; 2] = b"01";
-
#[rustfmt::skip]
const TWASM_MAGIC: [u8; 16] = [ TWASM_MAGIC_PREFIX[0], TWASM_MAGIC_PREFIX[1], TWASM_MAGIC_PREFIX[2], TWASM_MAGIC_PREFIX[3], TWASM_VERSION[0], TWASM_VERSION[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs
index fc12b54..9923116 100644
--- a/crates/types/src/instructions.rs
+++ b/crates/types/src/instructions.rs
@@ -9,20 +9,50 @@ pub enum BlockArgs {
FuncType(u32),
}
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))]
+/// A packed representation of BlockArgs
+/// This is needed to keep the size of the Instruction enum small.
+/// Sadly, using #[repr(u8)] on BlockArgs itself is not possible because of the FuncType variant.
+pub struct BlockArgsPacked([u8; 5]); // Modifying this directly can cause runtime errors, but no UB
+impl BlockArgsPacked {
+ pub fn new(args: BlockArgs) -> Self {
+ let mut packed = [0; 5];
+ match args {
+ BlockArgs::Empty => packed[0] = 0,
+ BlockArgs::Type(t) => {
+ packed[0] = 1;
+ packed[1] = t.to_byte();
+ }
+ BlockArgs::FuncType(t) => {
+ packed[0] = 2;
+ packed[1..].copy_from_slice(&t.to_le_bytes());
+ }
+ }
+ Self(packed)
+ }
+ pub fn unpack(&self) -> BlockArgs {
+ match self.0[0] {
+ 0 => BlockArgs::Empty,
+ 1 => BlockArgs::Type(ValType::from_byte(self.0[1]).unwrap()),
+ 2 => BlockArgs::FuncType(u32::from_le_bytes(self.0[1..].try_into().unwrap())),
+ _ => unreachable!(),
+ }
+ }
+}
+
/// Represents a memory immediate in a WebAssembly memory instruction.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))]
pub struct MemoryArg {
pub offset: u64,
pub mem_addr: MemAddr,
- pub align: u8,
- pub align_max: u8,
}
type BrTableDefault = u32;
-type BrTableLen = usize;
-type EndOffset = usize;
-type ElseOffset = usize;
+type BrTableLen = u32;
+type EndOffset = u32;
+type ElseOffset = u32;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))]
@@ -48,19 +78,47 @@ pub enum ConstInstruction {
/// This makes it easier to implement the label stack iteratively.
///
/// See <https://webassembly.github.io/spec/core/binary/instructions.html>
-#[derive(Debug, Clone, Copy, PartialEq)]
+#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))]
+// should be kept as small as possible (16 bytes max)
pub enum Instruction {
// Custom Instructions
BrLabel(LabelAddr),
+ // Not implemented yet
+ // LocalGet + I32Const + I32Add
+ // One of the most common patterns in the Rust compiler output
+ // I32LocalGetConstAdd(LocalAddr, i32),
+
+ // Not implemented yet
+ // LocalGet + I32Const + I32Store => I32LocalGetConstStore + I32Const
+ // Also common, helps us skip the stack entirely.
+ // Has to be followed by an I32Const instruction
+ // I32StoreLocal { local: LocalAddr, offset: i32, mem_addr: MemAddr },
+
+ // I64Xor + I64Const + I64RotL
+ // Commonly used by a few crypto libraries
+ I64XorConstRotl(i64),
+
+ // LocalTee + LocalGet
+ LocalTeeGet(LocalAddr, LocalAddr),
+ LocalGet2(LocalAddr, LocalAddr),
+ LocalGet3(LocalAddr, LocalAddr, LocalAddr),
+ LocalGetSet(LocalAddr, LocalAddr),
+
+ // Not implemented yet
+ // I32AddConst(i32),
+ // I32SubConst(i32),
+ // I64AddConst(i64),
+ // I64SubConst(i64),
+
// Control Instructions
// See <https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions>
Unreachable,
Nop,
Block(BlockArgs, EndOffset),
Loop(BlockArgs, EndOffset),
- If(BlockArgs, Option<ElseOffset>, EndOffset),
+ If(BlockArgsPacked, ElseOffset, EndOffset), // If else offset is 0 if there is no else block
Else(EndOffset),
EndBlockFrame,
EndFunc,
@@ -85,29 +143,29 @@ pub enum Instruction {
GlobalSet(GlobalAddr),
// Memory Instructions
- I32Load(MemoryArg),
- I64Load(MemoryArg),
- F32Load(MemoryArg),
- F64Load(MemoryArg),
- I32Load8S(MemoryArg),
- I32Load8U(MemoryArg),
- I32Load16S(MemoryArg),
- I32Load16U(MemoryArg),
- I64Load8S(MemoryArg),
- I64Load8U(MemoryArg),
- I64Load16S(MemoryArg),
- I64Load16U(MemoryArg),
- I64Load32S(MemoryArg),
- I64Load32U(MemoryArg),
- I32Store(MemoryArg),
- I64Store(MemoryArg),
- F32Store(MemoryArg),
- F64Store(MemoryArg),
- I32Store8(MemoryArg),
- I32Store16(MemoryArg),
- I64Store8(MemoryArg),
- I64Store16(MemoryArg),
- I64Store32(MemoryArg),
+ I32Load { offset: u64, mem_addr: MemAddr },
+ I64Load { offset: u64, mem_addr: MemAddr },
+ F32Load { offset: u64, mem_addr: MemAddr },
+ F64Load { offset: u64, mem_addr: MemAddr },
+ I32Load8S { offset: u64, mem_addr: MemAddr },
+ I32Load8U { offset: u64, mem_addr: MemAddr },
+ I32Load16S { offset: u64, mem_addr: MemAddr },
+ I32Load16U { offset: u64, mem_addr: MemAddr },
+ I64Load8S { offset: u64, mem_addr: MemAddr },
+ I64Load8U { offset: u64, mem_addr: MemAddr },
+ I64Load16S { offset: u64, mem_addr: MemAddr },
+ I64Load16U { offset: u64, mem_addr: MemAddr },
+ I64Load32S { offset: u64, mem_addr: MemAddr },
+ I64Load32U { offset: u64, mem_addr: MemAddr },
+ I32Store { offset: u64, mem_addr: MemAddr },
+ I64Store { offset: u64, mem_addr: MemAddr },
+ F32Store { offset: u64, mem_addr: MemAddr },
+ F64Store { offset: u64, mem_addr: MemAddr },
+ I32Store8 { offset: u64, mem_addr: MemAddr },
+ I32Store16 { offset: u64, mem_addr: MemAddr },
+ I64Store8 { offset: u64, mem_addr: MemAddr },
+ I64Store16 { offset: u64, mem_addr: MemAddr },
+ I64Store32 { offset: u64, mem_addr: MemAddr },
MemorySize(MemAddr, u8),
MemoryGrow(MemAddr, u8),
@@ -276,3 +334,51 @@ pub enum Instruction {
MemoryFill(MemAddr),
DataDrop(DataAddr),
}
+
+#[cfg(test)]
+mod test_blockargs_packed {
+ use super::*;
+
+ #[test]
+ fn test_empty() {
+ let args = BlockArgs::Empty;
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::Empty);
+ }
+
+ #[test]
+ fn test_val_type_i32() {
+ let args = BlockArgs::Type(ValType::I32);
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::Type(ValType::I32));
+ }
+
+ #[test]
+ fn test_val_type_i64() {
+ let args = BlockArgs::Type(ValType::I64);
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::Type(ValType::I64));
+ }
+
+ #[test]
+ fn test_val_type_f32() {
+ let args = BlockArgs::Type(ValType::F32);
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::Type(ValType::F32));
+ }
+
+ #[test]
+ fn test_val_type_f64() {
+ let args = BlockArgs::Type(ValType::F64);
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::Type(ValType::F64));
+ }
+
+ #[test]
+ fn test_func_type() {
+ let func_type = 123; // Use an arbitrary u32 value
+ let args = BlockArgs::FuncType(func_type);
+ let packed = BlockArgsPacked::new(args);
+ assert_eq!(packed.unpack(), BlockArgs::FuncType(func_type));
+ }
+}
diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs
index 24fed3b..bcd43e5 100644
--- a/crates/types/src/value.rs
+++ b/crates/types/src/value.rs
@@ -16,7 +16,8 @@ pub enum WasmValue {
F32(f32),
/// A 64-bit float.
F64(f64),
-
+ // /// A 128-bit vector
+ // V128(u128),
RefExtern(ExternAddr),
RefFunc(FuncAddr),
RefNull(ValType),
@@ -47,6 +48,7 @@ impl WasmValue {
ValType::I64 => Self::I64(0),
ValType::F32 => Self::F32(0.0),
ValType::F64 => Self::F64(0.0),
+ // ValType::V128 => Self::V128(0),
ValType::RefFunc => Self::RefNull(ValType::RefFunc),
ValType::RefExtern => Self::RefNull(ValType::RefExtern),
}
@@ -89,6 +91,7 @@ impl Debug for WasmValue {
WasmValue::I64(i) => write!(f, "i64({})", i),
WasmValue::F32(i) => write!(f, "f32({})", i),
WasmValue::F64(i) => write!(f, "f64({})", i),
+ // WasmValue::V128(i) => write!(f, "v128.half({:?})", i),
WasmValue::RefExtern(addr) => write!(f, "ref.extern({:?})", addr),
WasmValue::RefFunc(addr) => write!(f, "ref.func({:?})", addr),
WasmValue::RefNull(ty) => write!(f, "ref.null({:?})", ty),
@@ -105,6 +108,7 @@ impl WasmValue {
Self::I64(_) => ValType::I64,
Self::F32(_) => ValType::F32,
Self::F64(_) => ValType::F64,
+ // Self::V128(_) => ValType::V128,
Self::RefExtern(_) => ValType::RefExtern,
Self::RefFunc(_) => ValType::RefFunc,
Self::RefNull(ty) => *ty,
@@ -124,6 +128,8 @@ pub enum ValType {
F32,
/// A 64-bit float.
F64,
+ /// A 128-bit vector
+ // V128,
/// A reference to a function.
RefFunc,
/// A reference to an external value.
@@ -135,6 +141,29 @@ impl ValType {
pub fn default_value(&self) -> WasmValue {
WasmValue::default_for(*self)
}
+
+ pub(crate) fn to_byte(self) -> u8 {
+ match self {
+ ValType::I32 => 0x7F,
+ ValType::I64 => 0x7E,
+ ValType::F32 => 0x7D,
+ ValType::F64 => 0x7C,
+ ValType::RefFunc => 0x70,
+ ValType::RefExtern => 0x6F,
+ }
+ }
+
+ pub(crate) fn from_byte(byte: u8) -> Option<Self> {
+ match byte {
+ 0x7F => Some(ValType::I32),
+ 0x7E => Some(ValType::I64),
+ 0x7D => Some(ValType::F32),
+ 0x7C => Some(ValType::F64),
+ 0x70 => Some(ValType::RefFunc),
+ 0x6F => Some(ValType::RefExtern),
+ _ => None,
+ }
+ }
}
macro_rules! impl_conversion_for_wasmvalue {