From 9c3670fc930d21215e91849ce5a1fc44c9410fd0 Mon Sep 17 00:00:00 2001 From: mlugg Date: Fri, 22 Mar 2024 23:39:44 +0000 Subject: compiler: implement analysis-local comptime-mutable memory This commit changes how we represent comptime-mutable memory (`comptime var`) in the compiler in order to implement the intended behavior that references to such memory can only exist at comptime. It does *not* clean up the representation of mutable values, improve the representation of comptime-known pointers, or fix the many bugs in the comptime pointer access code. These will be future enhancements. Comptime memory lives for the duration of a single Sema, and is not permitted to escape that one analysis, either by becoming runtime-known or by becoming comptime-known to other analyses. These restrictions mean that we can represent comptime allocations not via Decl, but with state local to Sema - specifically, the new `Sema.comptime_allocs` field. All comptime-mutable allocations, as well as any comptime-known const allocs containing references to such memory, live in here. This allows for relatively fast checking of whether a value references any comptime-mtuable memory, since we need only traverse values up to pointers: pointers to Decls can never reference comptime-mutable memory, and pointers into `Sema.comptime_allocs` always do. This change exposed some faulty pointer access logic in `Value.zig`. I've fixed the important cases, but there are some TODOs I've put in which are definitely possible to hit with sufficiently esoteric code. I plan to resolve these by auditing all direct accesses to pointers (most of them ought to use Sema to perform the pointer access!), but for now this is sufficient for all realistic code and to get tests passing. This change eliminates `Zcu.tmp_hack_arena`, instead using the Sema arena for comptime memory mutations, which is possible since comptime memory is now local to the current Sema. This change should allow `Decl` to store only an `InternPool.Index` rather than a full-blown `ty: Type, val: Value`. This commit does not perform this refactor. --- lib/std/Target.zig | 3 ++- lib/std/enums.zig | 3 ++- lib/std/fmt.zig | 3 ++- lib/std/meta.zig | 6 ++++-- lib/std/unicode.zig | 3 ++- lib/std/zig/AstGen.zig | 30 +++++++++++++++++++----------- lib/std/zig/Zir.zig | 12 ++++++++++-- 7 files changed, 41 insertions(+), 19 deletions(-) (limited to 'lib/std') diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 4f616eefd2..842442e37b 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1317,7 +1317,8 @@ pub const Cpu = struct { for (decls, 0..) |decl, i| { array[i] = &@field(cpus, decl.name); } - return &array; + const finalized = array; + return &finalized; } }; diff --git a/lib/std/enums.zig b/lib/std/enums.zig index 79c69c8510..c00bf52aa6 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -41,7 +41,8 @@ pub inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumFi for (&result, fields) |*r, f| { r.* = @enumFromInt(f.value); } - return &result; + const final = result; + return &final; } } diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index df8397fb93..37c9f77fec 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1829,7 +1829,8 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou var buf: [count(fmt, args):0]u8 = undefined; _ = bufPrint(&buf, fmt, args) catch unreachable; buf[buf.len] = 0; - return &buf; + const final = buf; + return &final; } } diff --git a/lib/std/meta.zig b/lib/std/meta.zig index da0f629748..3ad2c2de13 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -465,7 +465,8 @@ pub fn fieldNames(comptime T: type) *const [fields(T).len][:0]const u8 { var names: [fieldInfos.len][:0]const u8 = undefined; // This concat can be removed with the next zig1 update. for (&names, fieldInfos) |*name, field| name.* = field.name ++ ""; - break :blk &names; + const final = names; + break :blk &final; }; } @@ -506,7 +507,8 @@ pub fn tags(comptime T: type) *const [fields(T).len]T { for (fieldInfos, 0..) |field, i| { res[i] = @field(T, field.name); } - break :blk &res; + const final = res; + break :blk &final; }; } diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 8a26c3383e..9d1f52fb2d 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -1358,7 +1358,8 @@ pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16Le var utf16le: [len:0]u16 = [_:0]u16{0} ** len; const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err); assert(len == utf16le_len); - break :blk &utf16le; + const final = utf16le; + break :blk &final; }; } diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig index 1bbfc2001e..2cab0fe7ca 100644 --- a/lib/std/zig/AstGen.zig +++ b/lib/std/zig/AstGen.zig @@ -8296,22 +8296,27 @@ fn localVarRef( }); } - const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure( - gz, - ident, - num_namespaces_out, - .{ .ref = local_ptr.ptr }, - .{ .token = local_ptr.token_src }, - ) else local_ptr.ptr; - switch (ri.rl) { .ref, .ref_coerced_ty => { + const ptr_inst = if (num_namespaces_out != 0) try tunnelThroughClosure( + gz, + ident, + num_namespaces_out, + .{ .ref = local_ptr.ptr }, + .{ .token = local_ptr.token_src }, + ) else local_ptr.ptr; local_ptr.used_as_lvalue = true; return ptr_inst; }, else => { - const loaded = try gz.addUnNode(.load, ptr_inst, ident); - return rvalueNoCoercePreRef(gz, ri, loaded, ident); + const val_inst = if (num_namespaces_out != 0) try tunnelThroughClosure( + gz, + ident, + num_namespaces_out, + .{ .ref_load = local_ptr.ptr }, + .{ .token = local_ptr.token_src }, + ) else try gz.addUnNode(.load, local_ptr.ptr, ident); + return rvalueNoCoercePreRef(gz, ri, val_inst, ident); }, } } @@ -8390,6 +8395,7 @@ fn tunnelThroughClosure( /// The value being captured. value: union(enum) { ref: Zir.Inst.Ref, + ref_load: Zir.Inst.Ref, decl_val: Zir.NullTerminatedString, decl_ref: Zir.NullTerminatedString, }, @@ -8400,7 +8406,8 @@ fn tunnelThroughClosure( }, ) !Zir.Inst.Ref { switch (value) { - .ref => |v| if (v.toIndex() == null) return v, // trivia value; do not need tunnel + .ref => |v| if (v.toIndex() == null) return v, // trivial value; do not need tunnel + .ref_load => |v| assert(v.toIndex() != null), // there are no constant pointer refs .decl_val, .decl_ref => {}, } @@ -8433,6 +8440,7 @@ fn tunnelThroughClosure( // captures as required, starting with the outermost namespace. const root_capture = Zir.Inst.Capture.wrap(switch (value) { .ref => |v| .{ .instruction = v.toIndex().? }, + .ref_load => |v| .{ .instruction_load = v.toIndex().? }, .decl_val => |str| .{ .decl_val = str }, .decl_ref => |str| .{ .decl_ref = str }, }); diff --git a/lib/std/zig/Zir.zig b/lib/std/zig/Zir.zig index f196155a1f..8aa4c0c8c5 100644 --- a/lib/std/zig/Zir.zig +++ b/lib/std/zig/Zir.zig @@ -3058,20 +3058,23 @@ pub const Inst = struct { /// Represents a single value being captured in a type declaration's closure. pub const Capture = packed struct(u32) { - tag: enum(u2) { + tag: enum(u3) { /// `data` is a `u16` index into the parent closure. nested, /// `data` is a `Zir.Inst.Index` to an instruction whose value is being captured. instruction, + /// `data` is a `Zir.Inst.Index` to an instruction representing an alloc whose contents is being captured. + instruction_load, /// `data` is a `NullTerminatedString` to a decl name. decl_val, /// `data` is a `NullTerminatedString` to a decl name. decl_ref, }, - data: u30, + data: u29, pub const Unwrapped = union(enum) { nested: u16, instruction: Zir.Inst.Index, + instruction_load: Zir.Inst.Index, decl_val: NullTerminatedString, decl_ref: NullTerminatedString, }; @@ -3085,6 +3088,10 @@ pub const Inst = struct { .tag = .instruction, .data = @intCast(@intFromEnum(inst)), }, + .instruction_load => |inst| .{ + .tag = .instruction_load, + .data = @intCast(@intFromEnum(inst)), + }, .decl_val => |str| .{ .tag = .decl_val, .data = @intCast(@intFromEnum(str)), @@ -3099,6 +3106,7 @@ pub const Inst = struct { return switch (cap.tag) { .nested => .{ .nested = @intCast(cap.data) }, .instruction => .{ .instruction = @enumFromInt(cap.data) }, + .instruction_load => .{ .instruction_load = @enumFromInt(cap.data) }, .decl_val => .{ .decl_val = @enumFromInt(cap.data) }, .decl_ref => .{ .decl_ref = @enumFromInt(cap.data) }, }; -- cgit v1.2.3