Implementing NaN Boxing in a Stack-Based VM
4 points
2 days ago
| 0 comments
| HN
## My Implementation

I'm using a 64-bit layout:

- Bits 63-51: Quiet NaN signature (0x7FFC...)

- Bits 50-18: 32-bit payload (integers, string pool indices, etc.)

- Bits 17-3: Unused/ (15 bits)

- Bits 2-0: 3-bit type tag

So it allows me to have 5 tagged types: `TRUE_VAL`, `FALSE_VAL`, `STRING_VAL`, `CALLDATA_VAL`, `U32_VAL`

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:

- String pooling & instructions pooling (indices stored as NaN-boxed values) - Call stack for task instructions execution.

code is here : https://github.com/tracyspacy/spacydo/blob/main/src/values.rs

No one has commented on this post.