diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-07-02 06:14:48 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-07-02 06:14:48 +0200 |
| commit | 86e77fc35b3a9146a31b872643a85f8f6a486a07 (patch) | |
| tree | 056a35081a7f5d87d351c710ae6f34ee7c0c5e9c /src/zisp/value/array.zig | |
| parent | b0043e83d3bf2f4d49bf1f153d65c48b4fabb4f6 (diff) | |
Exciting things are happening. - Vague Posting King
Diffstat (limited to 'src/zisp/value/array.zig')
| -rw-r--r-- | src/zisp/value/array.zig | 137 |
1 files changed, 79 insertions, 58 deletions
diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig index cdc5f75..88baf41 100644 --- a/src/zisp/value/array.zig +++ b/src/zisp/value/array.zig @@ -1,8 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); -const Alloc = std.mem.Allocator; - const gc = @import("../gc.zig"); const value = @import("../value.zig"); @@ -12,25 +10,24 @@ const Value = value.Value; /// /// The low 48 bits are either the length (element count, not buffer size) of /// the array contents that follow immediately, or a pointer to the header of -/// another array whose memory is shared with this one, or a null pointer that -/// has special meaning; see below. +/// another array whose memory is shared with this one, or null, which has a +/// special meaning; see below. /// /// NOTE: For strings, the length is in fact the size in bytes of the buffer. /// To get the "length" of a string according to other definitions of length, -/// such as count of Unicode Scalar Values, or count of Grapheme Clusters, +/// such as count of Unicode Scalar Values, count of UTF-16 code units, etc., /// different encoding-specific string APIs must be used. /// /// If this is a pointer (`is_ptr` is set) it means it's a re-interpretation of /// the contents of the array that is being pointed to. (But see below.) /// /// If this is a slice (`is_slice` is set) then two more u64 values follow this -/// one, marking the start and end of the slice of the pointed-to array that +/// one, marking the start and end byte offsets of the slice of the array that /// this one represents. (But see next paragraph.) /// /// If this is a pointer or slice, but the pointer value is null, then another -/// u64 follows this one immediately, and points directly to a memory buffer -/// (not array head) whose contents are used. In this case, if it's a slice, -/// the start and end u64 values come after that pointer instead. +/// u64 is found at the end, which is a direct pointer to a memory buffer (not +/// array head) whose contents are used. /// /// If this header encodes a non-slice, non-array, direct buffer pointer, then /// there is no length information, so the count of elements is unknown and @@ -48,7 +45,19 @@ const Value = value.Value; /// another array with the same buffer pointer and different type info. /// /// Other remaining bits provide information about element type and size. -pub const ArrayPtr = *align(@alignOf(value.Zptr)) ArrayHeader; +pub const ArrayPtr = *align(@alignOf(value.HeapPtr)) ArrayHeader; + +const ArrayType = enum(u2) { int, flt, val, str }; + +const Endian = enum(u1) { + little, + big, + + const native: Endian = switch (builtin.target.cpu.arch.endian()) { + .little => .little, + .big => .big, + }; +}; // Important: We may or may not use a hack one day in which an algorithm, like // for GC purposes, scans through certain memory regions looking for NaN-packed @@ -71,7 +80,7 @@ pub const ArrayHeader = packed struct(u64) { }, flt: packed struct(u12) { endian: Endian = .native, - _DONTUSE: bool = false + _DONTUSE: bool = false, size: u10, }, val: packed struct(u12) { @@ -98,26 +107,39 @@ pub const ArrayHeader = packed struct(u64) { return @ptrCast(self); } - fn bufContent(self: *Self) [*]u8 { + fn bufOfDirect(self: *Self) [*]u8 { std.debug.assert(!self.is_ptr); - return @ptrCast(self.bufU64() + 1); + return @ptrCast(&self.bufU64()[1]); } - fn bufPointer(self: *Self) [*]u8 { + fn bufOfPointer(self: *Self) [*]u8 { std.debug.assert(self.is_ptr); std.debug.assert(self.len_or_ptr == 0); return @ptrFromInt(self.bufU64()[1]); } + fn sliceInfo(self: *Self) [2]u64 { + std.debug.assert(self.is_slice); + const buf = self.bufU64(); + return .{ buf[1], buf[2] }; + } + + fn bufOfSlice(self: *Self) [*]u8 { + std.debug.assert(self.is_slice); + std.debug.assert(self.len_or_ptr == 0); + return @ptrFromInt(self.bufU64()[3]); + } + fn eltSize(self: *Self) u16 { std.debug.assert(!self.is_ptr); return switch (self.type) { .str => 1, + .val => 8, else => @panic("not implemented"), }; } - fn size(self: *Self) usize { + fn sizeInBytes(self: *Self) usize { std.debug.assert(!self.is_ptr); return self.len_or_ptr * self.eltSize(); } @@ -128,77 +150,76 @@ pub const ArrayHeader = packed struct(u64) { return if (p != 0) @ptrFromInt(p) else null; } - fn sliceInfo(self: *Self) [2]u64 { - std.debug.assert(self.is_slice); - const ptr = self.len_or_ptr; - const buf = self.bufU64(); - if (ptr != 0) { - return .{ buf[1], buf[2] }; - } else { - return .{ buf[2], buf[3] }; - } - } - - pub fn bufU8(self: *Self) [*]u8 { + /// Get a pointer to the array's contents as a u8 multi-pointer. + pub fn bufU8RW(self: *Self) [*]u8 { if (self.is_ptr) { - if (self.arrPointer()) |a| { - return a.bufContent(); + if (self.arrPointer()) |dest| { + std.debug.assert(!dest.is_ptr); + return dest.bufOfDirect(); + } else if (self.is_slice) { + return self.bufOfSlice(); } else { - return self.bufPointer(); + return self.bufOfPointer(); } - } else { - return self.bufContent(); } + return self.bufOfDirect(); + } + + /// Get a pointer to the array's contents as a const u8 multi-pointer. + pub fn bufU8RO(self: *Self) [*]const u8 { + return self.bufU8RW(); } - pub fn bytes(self: *Self) []u8 { + /// Get a u8 slice of the array's contents. + pub fn sliceU8RW(self: *Self) []u8 { if (self.is_slice) { - const buf = self.bufU8(); + std.debug.assert(self.is_ptr); const start, const end = self.sliceInfo(); + var buf = undefined; + if (self.arrPointer()) |dest| { + std.debug.assert(!dest.is_ptr); + std.debug.assert(end <= dest.sizeInBytes()); + buf = dest.bufOfDirect(); + } else { + buf = self.bufOfSlice(); + } return buf[start..end]; } var arr = self; if (self.is_ptr) { arr = self.arrPointer() orelse { - @panic("Called bytes() on array with direct buffer pointer."); + @panic("Array lacks length information; can't take slice."); }; } - return arr.bufContent()[0..arr.size()]; + return arr.bufContent()[0..arr.sizeInBytes()]; } - pub fn bytesRO(self: *Self) []const u8 { - return self.bytes(); + /// Get a const u8 slice of the array's contents. + pub fn sliceU8RO(self: *Self) []const u8 { + return self.sliceU8RW(); } }; -const ArrayType = enum(u2) { int, flt, val, str }; - -const Endian = enum(u1) { - little, - big, - - const native: Endian = switch (builtin.target.cpu.arch.endian()) { - .little => .little, - .big => .big, - }; -}; - -pub fn newString(alloc: Alloc, s: []const u8) !Value { +pub fn newString(s: []const u8) !Value { std.debug.assert(s.len <= std.math.maxInt(u48)); - const algn = std.mem.Alignment.of(ArrayPtr); - const size = @sizeOf(ArrayHeader) + s.len; - const arr: ArrayPtr = @ptrCast(try alloc.alignedAlloc(u8, algn, size)); + const len = @sizeOf(ArrayHeader) + s.len; + const arr = try gc.allocHeap(.array, len); arr.* = .{ .len_or_ptr = @intCast(s.len), .type = .str, .info = .{ .str = .{} }, }; - const buf = arr.bufContent(); + const buf = arr.bufOfDirect(); @memcpy(buf[0..s.len], s); - return value.ptr.pack(.array, arr); + return gc.packHeap(arr); +} + +pub fn checkAny(v: Value) ?ArrayPtr { + if (!v.isHptrTyp(.array)) return null; + return @ptrCast(gc.heapPtrFromIdx(v.hptr.idx)); } pub fn check(comptime t: ArrayType, v: Value) ?ArrayPtr { - if (v.getPtr(.array)) |p| if (p.type == t) return p; - return null; + const ptr = checkAny(v); + return if (ptr.type == t) ptr else null; } |
