authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-20 00:05:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:53-07:00
logbe78a12d7d5ac0a711fdf7237d7ccefba42be83c
treeb0a748977d64801d587ae624dba3dfb0cb814fec
parentc4735941148eb32eee16307ba13876ae21606fba

Sema: port Value.decl_ptr to InternPool


2 files changed, 50 insertions(+), 34 deletions(-)

src/InternPool.zig+34-13
......@@ -575,9 +575,10 @@ pub const Key = union(enum) {
575575 std.hash.autoHash(hasher, ptr.ty);
576576 // Int-to-ptr pointers are hashed separately than decl-referencing pointers.
577577 // This is sound due to pointer provenance rules.
578 std.hash.autoHash(hasher, @as(@typeInfo(Key.Ptr.Addr).Union.tag_type.?, ptr.addr));
578579 switch (ptr.addr) {
580 .decl => |decl| std.hash.autoHash(hasher, decl),
579581 .int => |int| std.hash.autoHash(hasher, int),
580 .decl => @panic("TODO"),
581582 }
582583 },
583584
......@@ -690,19 +691,14 @@ pub const Key = union(enum) {
690691
691692 .ptr => |a_info| {
692693 const b_info = b.ptr;
694 if (a_info.ty != b_info.ty) return false;
693695
694 if (a_info.ty != b_info.ty)
695 return false;
696 const AddrTag = @typeInfo(Key.Ptr.Addr).Union.tag_type.?;
697 if (@as(AddrTag, a_info.addr) != @as(AddrTag, b_info.addr)) return false;
696698
697699 return switch (a_info.addr) {
698 .int => |a_int| switch (b_info.addr) {
699 .int => |b_int| a_int == b_int,
700 .decl => false,
701 },
702 .decl => |a_decl| switch (b_info.addr) {
703 .int => false,
704 .decl => |b_decl| a_decl == b_decl,
705 },
700 .decl => |a_decl| a_decl == b_info.addr.decl,
701 .int => |a_int| a_int == b_info.addr.int,
706702 };
707703 },
708704
......@@ -1334,7 +1330,10 @@ pub const Tag = enum(u8) {
13341330 /// A value that can be represented with only an enum tag.
13351331 /// data is SimpleValue enum value.
13361332 simple_value,
1337 /// A pointer to an integer value.
1333 /// A pointer to a decl.
1334 /// data is extra index of PtrDecl, which contains the type and address.
1335 ptr_decl,
1336 /// A pointer with an integer value.
13381337 /// data is extra index of PtrInt, which contains the type and address.
13391338 /// Only pointer types are allowed to have this encoding. Optional types must use
13401339 /// `opt_payload` or `opt_null`.
......@@ -1673,6 +1672,11 @@ pub const PackedU64 = packed struct(u64) {
16731672 }
16741673};
16751674
1675pub const PtrDecl = struct {
1676 ty: Index,
1677 decl: Module.Decl.Index,
1678};
1679
16761680pub const PtrInt = struct {
16771681 ty: Index,
16781682 addr: Index,
......@@ -1990,6 +1994,13 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
19901994 .val = payload_val,
19911995 } };
19921996 },
1997 .ptr_decl => {
1998 const info = ip.extraData(PtrDecl, data);
1999 return .{ .ptr = .{
2000 .ty = info.ty,
2001 .addr = .{ .decl = info.decl },
2002 } };
2003 },
19932004 .ptr_int => {
19942005 const info = ip.extraData(PtrInt, data);
19952006 return .{ .ptr = .{
......@@ -2462,7 +2473,16 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
24622473 .extern_func => @panic("TODO"),
24632474
24642475 .ptr => |ptr| switch (ptr.addr) {
2465 .decl => @panic("TODO"),
2476 .decl => |decl| {
2477 assert(ptr.ty != .none);
2478 ip.items.appendAssumeCapacity(.{
2479 .tag = .ptr_decl,
2480 .data = try ip.addExtra(gpa, PtrDecl{
2481 .ty = ptr.ty,
2482 .decl = decl,
2483 }),
2484 });
2485 },
24662486 .int => |int| {
24672487 assert(ptr.ty != .none);
24682488 ip.items.appendAssumeCapacity(.{
......@@ -3465,6 +3485,7 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
34653485 .undef => 0,
34663486 .simple_type => 0,
34673487 .simple_value => 0,
3488 .ptr_decl => @sizeOf(PtrDecl),
34683489 .ptr_int => @sizeOf(PtrInt),
34693490 .opt_null => 0,
34703491 .opt_payload => 0,
src/Sema.zig+16-21
......@@ -29294,33 +29294,28 @@ fn analyzeDeclRef(sema: *Sema, decl_index: Decl.Index) CompileError!Air.Inst.Ref
2929429294/// decl_ref to end up in runtime code, the function body must be analyzed: `analyzeDeclRef` wraps
2929529295/// this function with `analyze_fn_body` set to true.
2929629296fn analyzeDeclRefInner(sema: *Sema, decl_index: Decl.Index, analyze_fn_body: bool) CompileError!Air.Inst.Ref {
29297 try sema.mod.declareDeclDependency(sema.owner_decl_index, decl_index);
29297 const mod = sema.mod;
29298 try mod.declareDeclDependency(sema.owner_decl_index, decl_index);
2929829299 try sema.ensureDeclAnalyzed(decl_index);
2929929300
29300 const decl = sema.mod.declPtr(decl_index);
29301 const decl = mod.declPtr(decl_index);
2930129302 const decl_tv = try decl.typedValue();
29302 if (decl_tv.val.castTag(.variable)) |payload| {
29303 const variable = payload.data;
29304 const ty = try Type.ptr(sema.arena, sema.mod, .{
29305 .pointee_type = decl_tv.ty,
29306 .mutable = variable.is_mutable,
29307 .@"addrspace" = decl.@"addrspace",
29308 .@"align" = decl.@"align",
29309 });
29310 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl_index));
29311 }
29303 const ptr_ty = try mod.ptrType(.{
29304 .elem_type = decl_tv.ty.ip_index,
29305 .alignment = InternPool.Alignment.fromByteUnits(decl.@"align"),
29306 .is_const = if (decl_tv.val.castTag(.variable)) |payload|
29307 !payload.data.is_mutable
29308 else
29309 false,
29310 .address_space = decl.@"addrspace",
29311 });
2931229312 if (analyze_fn_body) {
2931329313 try sema.maybeQueueFuncBodyAnalysis(decl_index);
2931429314 }
29315 return sema.addConstant(
29316 try Type.ptr(sema.arena, sema.mod, .{
29317 .pointee_type = decl_tv.ty,
29318 .mutable = false,
29319 .@"addrspace" = decl.@"addrspace",
29320 .@"align" = decl.@"align",
29321 }),
29322 try Value.Tag.decl_ref.create(sema.arena, decl_index),
29323 );
29315 return sema.addConstant(ptr_ty, (try mod.intern(.{ .ptr = .{
29316 .ty = ptr_ty.ip_index,
29317 .addr = .{ .decl = decl_index },
29318 } })).toValue());
2932429319}
2932529320
2932629321fn maybeQueueFuncBodyAnalysis(sema: *Sema, decl_index: Decl.Index) !void {