Implementing NaN Boxing in a Stack-Based VM
## My Implementation<p>I'm using a 64-bit layout:<p>- Bits 63-51: Quiet NaN signature (0x7FFC...)<p>- Bits 50-18: 32-bit payload (integers, string pool indices, etc.)<p>- Bits 17-3: Unused/ (15 bits)<p>- Bits 2-0: 3-bit type tag<p>So it allows me to have 5 tagged types: `TRUE_VAL`, `FALSE_VAL`, `STRING_VAL`, `CALLDATA_VAL`, `U32_VAL`<p>This is for a domain-specific VM I'm building for programmable task management (think "Vim for todo apps" - small core with scriptable behaviors). The VM is stack-based with:<p>- String pooling & instructions pooling (indices stored as NaN-boxed values)
- Call stack for task instructions execution.<p>code is here : https://github.com/tracyspacy/spacydo/blob/main/src/values.rs
查看原文
## My Implementation<p>I'm using a 64-bit layout:<p>- Bits 63-51: Quiet NaN signature (0x7FFC...)<p>- Bits 50-18: 32-bit payload (integers, string pool indices, etc.)<p>- Bits 17-3: Unused/ (15 bits)<p>- Bits 2-0: 3-bit type tag<p>So it allows me to have 5 tagged types: `TRUE_VAL`, `FALSE_VAL`, `STRING_VAL`, `CALLDATA_VAL`, `U32_VAL`<p>This is for a domain-specific VM I'm building for programmable task management (think "Vim for todo apps" - small core with scriptable behaviors). The VM is stack-based with:<p>- String pooling & instructions pooling (indices stored as NaN-boxed values)
- Call stack for task instructions execution.<p>code is here : https://github.com/tracyspacy/spacydo/blob/main/src/values.rs