//! See /doc/0/0-value.md to understand the NaN-packing strategy. const builtin = @import("builtin"); const std = @import("std"); 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 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 char = @import("value/char.zig"); pub const misc = @import("value/misc.zig"); pub const array = @import("value/array.zig"); pub const boole = @import("value/boole.zig"); pub const string = @import("value/string.zig"); const endian = builtin.target.cpu.arch.endian(); const max = std.math.maxInt; /// Used to find the length of a rune or short string. pub fn sstrLen(x: u64) u8 { const bytes: @Vector(8, u8) = @bitCast(x); const nulls: @Vector(8, u8) = @splat(0); const comps: u8 = @bitCast(bytes == nulls); // Two bits will always be 0, since the actual short string starts at the // third byte; third lowest or third highest depending on endianness. So, // depending on endianness, either cut off the two leading bits and ensure // that the second-last is set, or ensure that the second highest set, to // limit the length to 6. return switch (endian) { .big => @clz(comps << 2 | 2), .little => @ctz(comps | 64), }; } /// Transform back and forth between rune and sstr 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 }; // zig fmt: off pub const f = Value{ .misc = .{ .value = .f } }; pub const t = Value{ .misc = .{ .value = .t } }; pub const nil = Value{ .misc = .{ .value = .nil } }; pub const eof = Value{ .misc = .{ .value = .eof } }; 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; /// 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, /// Array of various types (see `Array` type for details) array, pub fn PtrType(self: HeapType) type { return switch (self) { .array => array.ArrayPtr, else => @panic("not implemented"), }; } }; /// 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; } 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 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. bits: u64, /// Regular double, including +nan, -nan, +inf, -inf. double: f64, // Some of the structs below are just for inspection, whereas others are to // initialize a new value of that category, as well as to read it as such. // Some of these are not used at all and are merely demonstrative. // Note: Zig packed struct fields are ordered from LSb to MSb. /// Inspection through the lens of the general IEEE 754 double layout. ieee: packed struct { rest: u51, quiet: bool, exp: u11, sign: bool, }, /// 52-bit signed fixnum fixnum: packed struct { code: u51, negative: 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, }, /// Istr pointer istr: packed struct { ptr: u48, _hi16_tag: u16 = hi16_tag_istr, }, /// Short string sstr: packed struct { // actually [6]u8 but packed struct cannot contain arrays bytes: u48, _hi16_tag: u16 = hi16_tag_sstr, }, /// Small rat (rational number) srat: packed struct { q: u24, p: i25, _hi16_tag: u15 = hi15_tag_srat, }, /// 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, }, // TODO: Use a general Small Value type registration mechanism. char: packed struct { value: u24, _sv_tag: u24 = 0x000080, _hi16_tag: u16 = hi16_tag_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, }, /// Dumps the value for inspection. pub fn dump(v: Value) void { std.debug.print( \\value: 0x{x} \\sign: {}, exp: {b}, quiet: {} \\rest: 0x{x} \\ , .{ v.bits, v.ieee.sign, v.ieee.exp, v.ieee.quiet, v.ieee.rest }); } /// To treat the 64-bit value as an 8-byte const array. pub fn bufRO(v: *const Value) *const [8]u8 { return @ptrCast(v); } /// To treat the 64-bit value as an 8-byte mutable array. pub fn bufRW(v: *Value) *[8]u8 { return @ptrCast(v); } /// Checks for bit-equality i.e. == comprison. pub fn eq(v1: Value, v2: Value) bool { 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. /// Checks for a 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 // set by comparing to 0x7ff. // // 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; 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. 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 // marks a fixnum. // // 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; 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); } /// Checks for a pointer and returns the value and tag separately, or null /// if this isn't a pointer or null. Could be useful for a dispatch table. pub fn getPtrAny(v: Value) ?struct { Zptr, HeapType } { const hi16_bits: u16 = @intCast(v.bits >> 48); if (hi16_bits != hi16_tag_ptr) return null; const ptr_val: u48 = @intCast(v.bits << 20 >> 16); if (ptr_val == 0) return null; const ht_val: u4 = @intCast(v.bits << 16 >> 60); return .{ @ptrFromInt(ptr_val), @enumFromInt(ht_val) }; } /// Checks if the value is a list pointer. pub fn isList(v: Value) bool { return v.bits >> 48 == hi16_tag_list; } /// Checks if the value is an istr pointer. pub fn isIstr(v: Value) bool { return v.bits >> 48 == hi16_tag_istr; } /// Checks if the value is a short string. pub fn isSstr(v: Value) bool { return v.bits >> 48 == hi16_tag_sstr; } /// Checks if the value is a small rat (rational number). pub fn isSrat(v: Value) bool { return v.bits >> 49 == hi15_tag_srat; } /// Checks for a 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. const hi = v.bits >> 48; const lo = v.bits << 16; const msb_mask = 0x7f7f7f7f7f7f0000; return hi == 0x7fff and lo != 0 and lo & msb_mask == lo; } // TODO: Use a general Small Value type registration mechanism. pub fn isChar(v: Value) bool { return v.bits >> 24 == 0x7fff000080; } // TODO: Use a general Small Value type registration mechanism. pub fn isMisc(v: Value) bool { return v.bits >> 8 == 0x7fff0000000080; } };