//! 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 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"); 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(v: *const Value) u8 { const bytes: @Vector(8, u8) = @bitCast(v.bits); 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)); } 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 } }; 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 HeapPtr = *anyopaque; /// Values for the lowest 4 bits of a heap pointer, indicating the heap type. pub const HeapType = enum(u8) { /// Array of various types: see `ArrayPtr`. array, pub fn PtrType(self: HeapType) type { return switch (self) { .array => array.ArrayPtr, else => @panic("Invalid HeapType."), }; } }; fn hi16(comptime tag: u4) u16 { return @as(u16, 0x7ff0) + tag; } 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. 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, }, /// Signed integer fixnum: packed struct { code: u51, sign: bool, _exp: u11 = max(u11), _is_fixnum: bool = true, }, /// List pointer list: packed struct { 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 { 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, _hi: u16 = hi16_sstr, }, /// Small rat (rational number) srat: packed struct { q: u24, p: i25, _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, _hi: u16 = hi16_rune, }, // TODO: Use a general Small Value type registration mechanism. char: packed struct { value: u24, _sv_tag: u24 = 0x000080, _hi: u16 = hi16_rune, }, // TODO: Use a general Small Value type registration mechanism. misc: packed struct { value: MiscValue, _sv_tag: u40 = 0x0000000080, _hi: u16 = hi16_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; } // We do some manual bit-fiddling for optimal codegen. /// Checks for a regular double, including +nan, -nan, +inf, -inf. pub fn isDouble(v: Value) bool { // // 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: u16 = @intCast(v.bits >> 52 & 0x7ff); const rest: u64 = v.bits << 13; return expt != 0x7ff or rest == 0; } /// Checks for a signed integer. pub fn isFixnum(v: Value) bool { // // 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: u16 = @intCast(v.bits >> 52); const rest: u64 = v.bits << 13; return expt == 0xfff and rest != 0; } /// Check for list pointer. pub fn isList(v: Value) bool { return v.list._hi == hi16_istr; } /// Check for heap pointer (any type). pub fn isHptrAny(v: Value) bool { return v.hptr._hi == hi16_hptr; } /// 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; } /// Check for istr pointer. pub fn isIstr(v: Value) bool { return v.istr._hi == hi16_istr; } /// Check for short string. pub fn isSstr(v: Value) bool { return v.sstr._hi == hi16_sstr; } /// Check for small rat (rational number). pub fn isSrat(v: Value) bool { return v.srat._hi == hi15_srat; } /// Placeholder: Check for (undefined). pub fn isNdef(v: Value) bool { return v.ndef._hi == hi15_ndef; } /// 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. 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; } };