From 3e6dd0da7a9aae1e6e3d3e0d897a2b3a28bfc043 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 5 May 2023 18:34:05 -0700 Subject: [PATCH] stage2: add tmp_hack_arena for the InternPool transition Temporarily used for some unfortunate allocations made by backends that need to construct pointer types that can't be represented by the InternPool. Once all types are migrated to be stored in the InternPool, this can be removed. --- src/Compilation.zig | 1 + src/InternPool.zig | 1 + src/Module.zig | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/Compilation.zig b/src/Compilation.zig index 9b2128a590d84fc767e53bc3d0062857ef9275e4..75af9362f69e385e176f4813df5a3e8f3cfabfcf 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1316,6 +1316,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .local_zir_cache = local_zir_cache, .emit_h = emit_h, .error_name_list = .{}, + .tmp_hack_arena = std.heap.ArenaAllocator.init(gpa), }; try module.init(); diff --git a/src/InternPool.zig b/src/InternPool.zig index c2d04067162f320cb80f25ef023d49d1c8a6cc69..de69b19dbe2fed83dc7f13236cee23f3c90ed019 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -1040,6 +1040,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { }); }, .ptr_type => |ptr_type| { + assert(ptr_type.elem_type != .none); // TODO introduce more pointer encodings ip.items.appendAssumeCapacity(.{ .tag = .type_pointer, diff --git a/src/Module.zig b/src/Module.zig index bfc06ac5abecae71119b6186de96232ba863754b..e9658ad89f413c592388d9ac5475d454e6850a09 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -98,6 +98,10 @@ string_literal_bytes: ArrayListUnmanaged(u8) = .{}, /// Stores all Type and Value objects; periodically garbage collected. intern_pool: InternPool = .{}, +/// Temporarily used for some unfortunate allocations made by backends that need to construct +/// pointer types that can't be represented by the InternPool. Once all types are migrated +/// to be stored in the InternPool, this can be removed. +tmp_hack_arena: std.heap.ArenaAllocator, /// The set of all the generic function instantiations. This is used so that when a generic /// function is called twice with the same comptime parameter arguments, both calls dispatch @@ -3552,6 +3556,7 @@ pub fn deinit(mod: *Module) void { mod.string_literal_bytes.deinit(gpa); mod.intern_pool.deinit(gpa); + mod.tmp_hack_arena.deinit(); } pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void { @@ -6848,10 +6853,25 @@ pub fn ptrType(mod: *Module, info: InternPool.Key.PtrType) Allocator.Error!Type } pub fn singleMutPtrType(mod: *Module, child_type: Type) Allocator.Error!Type { + if (child_type.ip_index == .none) { + // TODO remove this after all types can be represented via the InternPool + return Type.Tag.pointer.create(mod.tmp_hack_arena.allocator(), .{ + .pointee_type = child_type, + .@"addrspace" = .generic, + }); + } return ptrType(mod, .{ .elem_type = child_type.ip_index }); } pub fn singleConstPtrType(mod: *Module, child_type: Type) Allocator.Error!Type { + if (child_type.ip_index == .none) { + // TODO remove this after all types can be represented via the InternPool + return Type.Tag.pointer.create(mod.tmp_hack_arena.allocator(), .{ + .pointee_type = child_type, + .mutable = false, + .@"addrspace" = .generic, + }); + } return ptrType(mod, .{ .elem_type = child_type.ip_index, .is_const = true }); } -- 2.54.0