diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-07-01 01:26:15 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-07-01 01:26:15 +0200 |
| commit | 23b1ae89d351aabbdc8b7d6dfa362cc31165501e (patch) | |
| tree | 5ab8e43a2ef7891d203e5925aedd0ddf44489884 /src/zisp/value.zig | |
| parent | d5259ed20eae38bb4f732d829b927dd54cfac5f5 (diff) | |
Big changes. Incomplete. Won't compile yet.
Diffstat (limited to 'src/zisp/value.zig')
| -rw-r--r-- | src/zisp/value.zig | 198 |
1 files changed, 98 insertions, 100 deletions
diff --git a/src/zisp/value.zig b/src/zisp/value.zig index c16b937..9262a17 100644 --- a/src/zisp/value.zig +++ b/src/zisp/value.zig @@ -8,12 +8,14 @@ const gc = @import("gc.zig"); pub const double = @import("value/double.zig"); pub const fixnum = @import("value/fixnum.zig"); -pub const ptr = @import("value/ptr.zig"); pub const list = @import("value/list.zig"); +pub const hptr = @import("value/hptr.zig"); pub const istr = @import("value/istr.zig"); pub const sstr = @import("value/sstr.zig"); pub const srat = @import("value/srat.zig"); pub const rune = @import("value/rune.zig"); +pub const sval = @import("value/sval.zig"); + pub const char = @import("value/char.zig"); pub const misc = @import("value/misc.zig"); @@ -47,8 +49,19 @@ pub fn runeXsstr(v: Value) Value { return @bitCast(v.bits ^ (1 << 50)); } -// Make sure false/true only differ in LSb. -pub const MiscValue = enum(u8) { f, t, nil, eof, none }; +pub const MiscValue = enum(u8) { + // Make f/t only differ in one bit, to make `bool?` more efficient. + /// False + f = 0, + /// True + t = 1, + /// Empty list + nil, + /// End of file + eof, + /// Stand-in for "literally no value" (never exposed to Zisp code) + none, +}; // zig fmt: off pub const f = Value{ .misc = .{ .value = .f } }; @@ -59,14 +72,10 @@ pub const none = Value{ .misc = .{ .value = .none } }; // zig fmt: on /// A plain (unpacked, untagged) pointer into the Zisp heap. -pub const Zptr = *align(16) anyopaque; +pub const HeapPtr = *anyopaque; /// Values for the lowest 4 bits of a heap pointer, indicating the heap type. -pub const HeapType = enum(u4) { - /// Unused so the 48-bit payload of a NaN-packed pointer is never zero and - /// cannot be confused for an actual NaN even in case of a null index. - _unused = 0, - +pub const HeapType = enum(u8) { /// Array of various types: see `ArrayPtr`. array, @@ -78,19 +87,31 @@ pub const HeapType = enum(u4) { } }; -/// Make a "pointer etc." high 16-bit pattern with a 3-bit sub-range tag. -fn ptrEtc(comptime tag: u3) u16 { - return @as(u16, 0x7ff8) + tag; +fn hi16(comptime tag: u4) u16 { + return @as(u16, 0x7ff0) + tag; } -const hi16_tag_ptr = ptrEtc(0b000); -const hi16_tag_list = ptrEtc(0b001); -const hi16_tag_istr = ptrEtc(0b010); -const hi16_tag_sstr = ptrEtc(0b011); -const hi15_tag_srat = @as(u15, @intCast(ptrEtc(0b100) >> 1)); -const hi16_tag_rune = ptrEtc(0b111); - -/// Represents a Zisp value. +pub const hi16_list = hi16(0b0000); +pub const hi16_hptr = hi16(0b0001); +pub const hi16_istr = hi16(0b0010); +pub const hi16_sstr = hi16(0b0011); +pub const hi16_rpos = hi16(0b0100); +pub const hi16_rneg = hi16(0b0101); +pub const hi16_ndef = hi16(0b0110); +pub const hi16_rune = hi16(0b0111); + +pub const hi15_srat: u15 = @intCast(hi16(0b0100) >> 1); + +pub const hi16_const_list = hi16(0b1000); +pub const hi16_const_hptr = hi16(0b1001); +pub const hi16_const_istr = hi16(0b1010); +pub const hi16_const_sstr = hi16(0b1011); +pub const hi16_vm_op_list = hi16(0b1100); +pub const hi16_vm_op_heap = hi16(0b1101); +pub const hi16_vm_var_loc = hi16(0b1110); +pub const hi16_vm_var_lex = hi16(0b1111); + +/// Represents any Zisp value. pub const Value = packed union { /// To get an agnostic value for direct comparison with == i.e. eq? as well /// as manual bit-fiddling to test for and extract packed values. @@ -113,66 +134,77 @@ pub const Value = packed union { sign: bool, }, - /// 52-bit signed fixnum + /// Signed integer fixnum: packed struct { code: u51, - negative: bool, + sign: bool, _exp: u11 = max(u11), _is_fixnum: bool = true, }, - /// Ordinary heap pointer - ptr: packed struct { - index: u44, - htype: HeapType, - _hi16_tag: u16 = hi16_tag_ptr, - }, - /// List pointer list: packed struct { - len_tagged_ptr: u48, - _hi16_tag: u16 = hi16_tag_list, + idx: u32, + _gc: u8 = 0, + len: u8, + _hi: u16 = hi16_list, + }, + + /// Heap pointer + hptr: packed struct { + idx: u32, + _gc: u8 = 0, + typ: HeapType, + _hi: u16 = hi16_hptr, }, /// Istr pointer istr: packed struct { - ptr: u48, - _hi16_tag: u16 = hi16_tag_istr, + idx: u32, + _gc: u8 = 0, + len: u8, + _hi: u16 = hi16_istr, }, /// Short string sstr: packed struct { // actually [6]u8 but packed struct cannot contain arrays bytes: u48, - _hi16_tag: u16 = hi16_tag_sstr, + _hi: u16 = hi16_sstr, }, /// Small rat (rational number) srat: packed struct { q: u24, p: i25, - _hi16_tag: u15 = hi15_tag_srat, + _hi: u15 = hi15_srat, + }, + + /// Undefined, for now + ndef: packed struct { + _: u48, + _hi: u16 = hi16_ndef, }, /// Rune (6-byte ASCII string) rune: packed struct { // actually [6]u8 but packed struct cannot contain arrays name: u48, - _hi16_tag: u16 = hi16_tag_rune, + _hi: u16 = hi16_rune, }, // TODO: Use a general Small Value type registration mechanism. char: packed struct { value: u24, _sv_tag: u24 = 0x000080, - _hi16_tag: u16 = hi16_tag_rune, + _hi: u16 = hi16_rune, }, // TODO: Use a general Small Value type registration mechanism. misc: packed struct { value: MiscValue, _sv_tag: u40 = 0x0000000080, - _hi16_tag: u16 = hi16_tag_rune, + _hi: u16 = hi16_rune, }, /// Dumps the value for inspection. @@ -200,17 +232,10 @@ pub const Value = packed union { return v1.bits == v2.bits; } - // It would be great if we could just write the most readable code in the - // following functions, using the packed struct definitions above, but the - // optimizer isn't smart enough, so manual bit fiddling it is. + // We do some manual bit-fiddling for optimal codegen. - /// Checks for a double, including: +nan, -nan, +inf, -inf. + /// Checks for a regular double, including +nan, -nan, +inf, -inf. pub fn isDouble(v: Value) bool { - // Readable version: - // - // return v.ieee.exp != max(u11) or v.ieee.rest == 0; - // - // Optimized: // // 1. Shift the 12 highest bits all the way to the right, then mask out // the sign bit. Allows efficiently checking if the exponent is all @@ -219,21 +244,13 @@ pub const Value = packed union { // 2. Shift out 13 high bits (sign, expt, quiet) so we can easily check // if the rest bits are zero. // - const expt = v.bits >> 52 & 0x7ff; - const rest = v.bits << 13; + const expt: u16 = @intCast(v.bits >> 52 & 0x7ff); + const rest: u64 = v.bits << 13; return expt != 0x7ff or rest == 0; } - // Imagine there's an isPacked() implemented as !isDouble() here, used to - // make the following functions more readable. - - /// Checks for a fixnum integer. + /// Checks for a signed integer. pub fn isFixnum(v: Value) bool { - // Readable version: - // - // return v.isPacked() and v.ieee.sign; - // - // Optimized: // // 1. Shift the 12 highest bits all the way to the right; these need to // be all set, since the sign bit (and all exponent bits) being set @@ -242,66 +259,47 @@ pub const Value = packed union { // 2. But also verify that the lowest 51 bits aren't zero, since that // would be a cqNaN or Infinity. // - const expt = v.bits >> 52; - const rest = v.bits << 13; + const expt: u16 = @intCast(v.bits >> 52); + const rest: u64 = v.bits << 13; return expt == 0xfff and rest != 0; } - /// Checks if the value is any type-tagged pointer. You probably want to - /// use getPtr() or getPtrAny() instead, so you can combine the check with - /// the extraction of the pointer and type tag. - pub fn isPtrAny(v: Value) bool { - const hi: u16 = @intCast(v.bits >> 48); - const lo: u48 = @truncate(v.bits); - return hi == hi16_tag_ptr and lo != 0; - } - - /// Checks for a pointer with a given type tag, returning null on failure - /// and the pointer value otherwise. - pub fn getPtr(v: Value, comptime ht: HeapType) ?ht.PtrType() { - // Check if the 20 highest bits equal the combination of the 16-bit - // pattern marking a pointer, followed by the 4-bit heap type tag. - const hi20_bits: u20 = @intCast(v.bits >> 44); - const ptr_bits: u20 = hi16_tag_ptr; - const tag_bits: u20 = @intFromEnum(ht); - if (hi20_bits != ptr_bits << 4 | tag_bits) return null; - return @ptrFromInt(v.bits << 20 >> 16); + /// Check for list pointer. + pub fn isList(v: Value) bool { + return v.list._hi == hi16_istr; } - /// Checks for a pointer and returns the value and tag separately, or null - /// if this isn't a pointer at all. Could be useful for a dispatch table. - /// Note that unlike getPtr(), this can actually return a null pointer as - /// such, not conflating it with the "not a heap pointer" case. - pub fn getPtrAny(v: Value) ?struct { HeapType, ?Zptr } { - const hi16_bits: u16 = @intCast(v.bits >> 48); - if (hi16_bits != hi16_tag_ptr) return null; - - const ht: u4 = @intCast(v.bits << 16 >> 60); - const pval: u48 = @intCast(v.bits << 20 >> 16); - return .{ @enumFromInt(ht), @ptrFromInt(pval) }; + /// Check for heap pointer (any type). + pub fn isHptrAny(v: Value) bool { + return v.hptr._hi == hi16_hptr; } - /// Checks if the value is a list pointer. - pub fn isList(v: Value) bool { - return v.bits >> 48 == hi16_tag_list; + /// Check for heap pointer (given type). + pub fn isHptrTyp(v: Value, comptime htype: HeapType) bool { + return v.hptr._hi == hi16_hptr and v.htptr.typ == htype; } - /// Checks if the value is an istr pointer. + /// Check for istr pointer. pub fn isIstr(v: Value) bool { - return v.bits >> 48 == hi16_tag_istr; + return v.istr._hi == hi16_istr; } - /// Checks if the value is a short string. + /// Check for short string. pub fn isSstr(v: Value) bool { - return v.bits >> 48 == hi16_tag_sstr; + return v.sstr._hi == hi16_sstr; } - /// Checks if the value is a small rat (rational number). + /// Check for small rat (rational number). pub fn isSrat(v: Value) bool { - return v.bits >> 49 == hi15_tag_srat; + return v.srat._hi == hi15_srat; + } + + /// Placeholder: Check for (undefined). + pub fn isNdef(v: Value) bool { + return v.ndef._hi == hi15_ndef; } - /// Checks for a rune. + /// Check for rune. pub fn isRune(v: Value) bool { // This check isn't used much, since one typically checks for specific // runes via direct equality, so the efficiency here is unimportant. |
