summaryrefslogtreecommitdiff
path: root/src/zisp/value.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-08 05:54:07 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-08 05:54:07 +0200
commit627a09b58f404b1f690d27869b15b3197207c0af (patch)
tree4e64459ff8462e7f4dfc502158b6ffa629ebb0c8 /src/zisp/value.zig
parent90af1ed17d317435fb56ea041fa0d937f5043726 (diff)
Code cleanup and make heap alloc 16-byte aligned.
Diffstat (limited to 'src/zisp/value.zig')
-rw-r--r--src/zisp/value.zig43
1 files changed, 30 insertions, 13 deletions
diff --git a/src/zisp/value.zig b/src/zisp/value.zig
index 5402b16..50d695c 100644
--- a/src/zisp/value.zig
+++ b/src/zisp/value.zig
@@ -64,7 +64,7 @@
//! Pointers are further subdivided as follows based on the remaining 51 bits,
//! with the first three bits used as a sort of tag:
//!
-//! 000 :: Regular pointer to Zisp heap object (string, vector, etc.)
+//! 000 :: Regular pointer to Zisp heap object
//!
//! 001 :: Weak pointer to Zisp heap object
//!
@@ -72,11 +72,11 @@
//!
//! 1.. :: Undefined
//!
-//! This means pointers to the Zisp heap are 48 bits. Of those, we only really
-//! need 45, since 64-bit platforms are in practice limited to 48-bit addresses,
-//! and Zisp heap allocations happen at 8-byte boundaries, meaning the lowest 3
-//! bits are always unset. Thus, we are able to store yet another 3-bit tag in
-//! those 48-bit pointers alongside the actual, multiple-of-8, 48-bit address.
+//! This means Zisp heap pointers are 48 bits. This is sufficient, since the
+//! address space of user-land applications is effectively 48 bits on 64-bit
+//! systems. Further, Zisp heap objects are allocated at 16-byte boundaries,
+//! meaning the lowest 4 bits are always zero; this is used for type tagging,
+//! providing immediate information about the type of object pointed to.
//!
//! Forbidden Value #3, Positive cqNaN, is avoided thanks to the fact that a
//! regular Zisp heap pointer can never be null. Weak pointers, which can be
@@ -205,10 +205,10 @@ pub const undef = Value{ .misc = .{ .value = .undef } };
/// A plain (unpacked, untagged) pointer into the Zisp heap. May point to any
/// kind of object.
-pub const Zptr = *align(8) anyopaque;
+pub const Zptr = *align(16) anyopaque;
-/// Values for the lowest 3 bits of a heap pointer, indicating the heap type.
-pub const PtrTag = enum(u3) {
+/// Values for the lowest 4 bits of a heap pointer, indicating the heap type.
+pub const PtrTag = enum(u4) {
/// Pair aka cons cell aka *[2]Value
pair,
/// Interned string buffer (8-bit length, then contents)
@@ -218,7 +218,7 @@ pub const PtrTag = enum(u3) {
/// Procedure
proc,
- pub fn ptrType(self: PtrTag) type {
+ pub fn ptype(self: PtrTag) type {
return switch (self) {
.pair => pair.PairPtr,
.istr => istr.IstrPtr,
@@ -324,6 +324,9 @@ pub const Value = packed union {
const mask_rest: u64 = max(u51) ; // 51 rest bits of fraction
// zig fmt: on
+ // Mask for pointer type tag bits
+ const mask_ptr_tag: u48 = max(@typeInfo(PtrTag).@"enum".tag_type);
+
/// Dumps the value for inspection.
pub fn dump(v: Value) void {
const sign: u1 = @intCast((v.bits & mask_sign) >> 63);
@@ -405,7 +408,7 @@ pub const Value = packed union {
/// the "high" pointer tags (49-51) that encode properties like weakness
/// don't matter; we only care if it's some kind of valid heap pointer of
/// the given type, so we only check the type tag in the low bits (0-2).
- pub fn getPtr(v: Value, comptime tag: PtrTag) ?tag.ptrType() {
+ pub fn getPtr(v: Value, comptime tag: PtrTag) ?tag.ptype() {
// Readable version:
//
// if (v.isPacked() and !v.ieee.sign and v.ieee.quiet ...)
@@ -428,14 +431,28 @@ pub const Value = packed union {
// that for example bits 50 and 51 must be zero, or 51 must be zero
// while 50 is still ignored, and so on, as appropriate.
//
- const ptr_val: u48 = @intCast(v.bits & (max(u45) << 3));
const hi_bits: u13 = @intCast(v.bits >> 51);
- const tag_bits: u3 = @intCast(v.bits & 7);
+ const ptr_val: u48 = @intCast(v.bits & ~mask_ptr_tag);
+ const tag_bits: u4 = @intCast(v.bits & mask_ptr_tag);
const is_ptr = hi_bits == 0b0111111111111;
const is_tag = tag_bits == @intFromEnum(tag);
return if (is_ptr and is_tag) @ptrFromInt(ptr_val) else null;
}
+ /// Checks for a pointer and returns the value and tag separately, or null
+ /// if this isn't a pointer at all. This could be useful for a dispatch
+ /// table based on type.
+ pub fn getPtrAny(v: Value) ?struct { Zptr, PtrTag } {
+ // See last function, which is almost identical.
+ const hi_bits: u13 = @intCast(v.bits >> 51);
+ const ptr_val: u48 = @intCast(v.bits & ~mask_ptr_tag);
+ const tag_bits: u4 = @intCast(v.bits & mask_ptr_tag);
+ const is_ptr = hi_bits == 0b0111111111111;
+ const zptr: Zptr = @ptrFromInt(ptr_val);
+ const tag: PtrTag = @enumFromInt(tag_bits);
+ return if (is_ptr) .{ zptr, tag } else null;
+ }
+
/// Checks whether the value is a pointer with certain property bits, not
/// caring about the type tag bits. NOTE: This function doesn't check for
/// +cqNaN, which will be mis-identified as a pointer with zero props and