//! Comptime reflection of a sketch's memory into `abi.Layout`. //! //! Structs are flattened into dotted leaf paths; everything else (arrays, //! optionals, unions) is one leaf. Each leaf carries a structural hash of its //! type, so a host can tell, across a reload, which bytes still mean the same //! thing. //! //! State is saved to disk and snapshotted, so it may not hold pointers at all. //! Session memory lives only in the host process and may point into memory //! the host owns (its heap, mapped files), never into the sketch library. const std = @import("std"); const abi = @import("abi.zig"); pub const Pointers = enum { forbidden, host_only }; pub fn of(comptime T: type, comptime pointers: Pointers) abi.Layout { return comptime blk: { @setEvalBranchQuota(1_000_000); if (pointers == .forbidden) noPointers(T, @typeName(T)); const Leaves = struct { const list = flatten(T, "", 0); const fields: [list.len]abi.Field = list[0..list.len].*; }; break :blk .{ .size = @sizeOf(T), .alignment = @alignOf(T), .hash = hash(T), .fields = &Leaves.fields, }; }; } fn flatten(comptime T: type, comptime prefix: []const u8, comptime base: usize) []const abi.Field { comptime { if (@typeInfo(T) != .@"struct") return &.{leaf(T, if (prefix.len == 0) "value" else prefix, base)}; var leaves: []const abi.Field = &.{}; for (@typeInfo(T).@"struct".fields) |field| { if (field.is_comptime or @sizeOf(field.type) == 0) continue; const path = if (prefix.len == 0) field.name else prefix ++ "." ++ field.name; const offset = base + @offsetOf(T, field.name); leaves = leaves ++ if (@typeInfo(field.type) == .@"struct" and @typeInfo(field.type).@"struct".backing_integer == null) flatten(field.type, path, offset) else &[_]abi.Field{leaf(field.type, path, offset)}; } return leaves; } } fn leaf(comptime T: type, comptime path: []const u8, comptime offset: usize) abi.Field { comptime { const kind: abi.Field.Kind = switch (@typeInfo(T)) { .int => |i| if (i.signedness == .signed) .int else .uint, .float => .float, .bool => .bool, .@"enum" => .@"enum", .array => .array, else => .bytes, }; return .{ .path = std.fmt.comptimePrint("{s}", .{path}), .offset = offset, .size = @sizeOf(T), .kind = kind, .hash = hash(T), .tags = if (kind == .@"enum") tags(T) else "", }; } } fn noPointers(comptime T: type, comptime path: []const u8) void { comptime switch (@typeInfo(T)) { .pointer => @compileError("State cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Store handles, indices or offsets, or keep it in Session."), .array => |a| noPointers(a.child, path ++ "[]"), .vector => |v| noPointers(v.child, path ++ "[]"), .optional => |o| noPointers(o.child, path), .@"struct" => |s| for (s.fields) |f| noPointers(f.type, path ++ "." ++ f.name), .@"union" => |u| for (u.fields) |f| noPointers(f.type, path ++ "." ++ f.name), else => {}, }; } fn tags(comptime E: type) [:0]const u8 { comptime { var text: []const u8 = ""; for (@typeInfo(E).@"enum".fields, 0..) |field, i| { text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ field.name, field.value }); } return std.fmt.comptimePrint("{s}", .{text}); } } /// Equal hashes mean the bytes can be copied and keep their meaning. Enum /// leaves also carry their tags, so a host can remap them by name. pub fn hash(comptime T: type) u64 { return comptime blk: { @setEvalBranchQuota(1_000_000); break :blk std.hash.Wyhash.hash(0, signature(T)); }; } pub fn signature(comptime T: type) []const u8 { comptime { return switch (@typeInfo(T)) { .int => |i| std.fmt.comptimePrint("{c}{d}", .{ if (i.signedness == .signed) 'i' else 'u', i.bits }), .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), .bool => "b", .void => "v", .@"enum" => |e| "e(" ++ signature(e.tag_type) ++ ":" ++ tags(T) ++ ")", .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ signature(a.child), .vector => |v| std.fmt.comptimePrint("@{d}", .{v.len}) ++ signature(v.child), .optional => |o| "?" ++ signature(o.child), .@"struct" => |s| blk: { var text: []const u8 = std.fmt.comptimePrint("s{d}{{", .{@sizeOf(T)}); for (s.fields) |field| { if (field.is_comptime) continue; text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ field.name, @offsetOf(T, field.name) }) ++ signature(field.type) ++ ";"; } break :blk text ++ "}"; }, .@"union" => |u| blk: { var text: []const u8 = std.fmt.comptimePrint("u{d}{{", .{@sizeOf(T)}); if (u.tag_type) |tag| text = text ++ signature(tag) ++ "|"; for (u.fields) |field| text = text ++ field.name ++ ":" ++ signature(field.type) ++ ";"; break :blk text ++ "}"; }, .pointer => |p| std.fmt.comptimePrint("*{s}:{s}", .{ @tagName(p.size), @typeName(p.child) }), .@"opaque" => "opaque", .@"fn" => @typeName(T), else => @compileError("unsupported type in sketch memory: " ++ @typeName(T)), }; } } test "leaves flatten structs, keep arrays whole and reject nothing pointer-free" { const Inner = extern struct { a: u8 = 0, b: enum(u8) { x, y = 5 } = .x }; const S = extern struct { inner: Inner = .{}, list: [4]f32 = @splat(0), flag: bool = false }; const l = of(S, .forbidden); const fields = l.fields; try std.testing.expectEqual(4, fields.len); try std.testing.expectEqualStrings("inner.a", std.mem.span(fields[0].path)); try std.testing.expectEqualStrings("inner.b", std.mem.span(fields[1].path)); try std.testing.expectEqualStrings("x=0,y=5", std.mem.span(fields[1].tags)); try std.testing.expectEqual(abi.Field.Kind.array, fields[2].kind); try std.testing.expectEqual(@as(u32, 4), fields[2].offset); try std.testing.expect(hash([4]f32) != hash([5]f32)); try std.testing.expect(hash(enum(u8) { a }) != hash(enum(u8) { a, b })); }