diff options
| author | Henry <mail@henrygressmann.de> | 2026-09-14 10:48:32 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-09-14 10:49:30 +0200 |
| commit | 437a77c8d9e9286a7ca7993b6c8b85e7feb6ebdd (patch) | |
| tree | 443640431d4a0f230474274f5adb0b98cdfad60e /crates/types/src | |
| parent | 795ec871806ea189818bbf2fd2a69455384d2004 (diff) | |
perf: optimize nightly tail dispatch
Similar to #55 but with only safe code. LLVM seems so optimize the discriminant conversion away.
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/types/src')
| -rw-r--r-- | crates/types/src/instructions.rs | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index 6a357ea..3bc71a2 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -500,16 +500,45 @@ pub enum BinOp128 { I64x2Mul, } -/// A TinyWasm bytecode instruction. -/// -/// These instructions are an internal, version-specific representation and do not -/// map one-to-one to WebAssembly instructions. Their variants and serialized form -/// may change between TinyWasm releases. -#[rustfmt::skip] -#[derive(Clone, Copy, PartialEq)] -#[cfg_attr(feature = "debug", derive(Debug))] -#[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] -pub enum Instruction { +macro_rules! define_instructions { + ($($variant:ident $(($($field:ty),* $(,)?))? $({ $($name:ident: $named_field:ty),* $(,)? })?),* $(,)?) => { + /// A TinyWasm bytecode instruction. + /// + /// These instructions are an internal, version-specific representation and do not + /// map one-to-one to WebAssembly instructions. Their variants and serialized form + /// may change between TinyWasm releases. + #[derive(Clone, Copy, PartialEq)] + #[cfg_attr(feature = "debug", derive(Debug))] + #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] + pub enum Instruction { + $($variant $(($($field),*))? $({ $($name: $named_field),* })?),* + } + + #[doc(hidden)] + #[repr(u16)] + #[derive(Clone, Copy)] + pub enum InstructionOpcode { + $($variant,)* + } + + impl InstructionOpcode { + #[doc(hidden)] + pub const COUNT: usize = [$(Self::$variant),*].len(); + } + + impl Instruction { + /// Returns the compact opcode used to dispatch this instruction. + #[inline(always)] + pub const fn opcode(&self) -> InstructionOpcode { + match self { + $(Self::$variant { .. } => InstructionOpcode::$variant,)* + } + } + } + }; +} + +define_instructions! { LocalCopy32(LocalAddr, LocalAddr), LocalCopy64(LocalAddr, LocalAddr), LocalCopy128(LocalAddr, LocalAddr), AddConst32(i32), AndConst32(i32), XorConst32(i32), ShrUConst32(i32), AddConst64(Operand64Idx<i64>), IncLocal32(I32LocalArg), IncLocal64(PackedOp64<LocalAddr, i64>), |
