authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-12 12:40:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-12 12:40:32-07:00
log28dd9d478d24190ab5c8c4b892d7dfc16c380ae0
treef717633b02bdd9cf1c7bc724908e8d19ef097981
parent03156e589939993bba339162d27d24fd511601c6

C backend: TypedefMap is now ArrayHashMap

The C backend depends on insertion order into this map so that type definitions will be declared before they are used.

5 files changed, 22 insertions(+), 14 deletions(-)

src/codegen/c.zig+3-3
......@@ -39,11 +39,11 @@ const BlockData = struct {
3939};
4040
4141pub const CValueMap = std.AutoHashMap(*Inst, CValue);
42pub const TypedefMap = std.HashMap(
42pub const TypedefMap = std.ArrayHashMap(
4343 Type,
4444 struct { name: []const u8, rendered: []u8 },
45 Type.HashContext,
46 std.hash_map.default_max_load_percentage,
45 Type.HashContext32,
46 true,
4747);
4848
4949fn formatTypeAsCIdentifier(
src/codegen/spirv.zig+1-1
......@@ -18,7 +18,7 @@ const Inst = ir.Inst;
1818pub const Word = u32;
1919pub const ResultId = u32;
2020
21pub const TypeMap = std.HashMap(Type, u32, Type.HashContext, std.hash_map.default_max_load_percentage);
21pub const TypeMap = std.HashMap(Type, u32, Type.HashContext64, std.hash_map.default_max_load_percentage);
2222pub const InstMap = std.AutoHashMap(*Inst, ResultId);
2323
2424const IncomingBlock = struct {
src/link.zig+1-1
......@@ -168,7 +168,7 @@ pub const File = struct {
168168 };
169169
170170 /// For DWARF .debug_info.
171 pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.HashContext, std.hash_map.default_max_load_percentage);
171 pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.HashContext64, std.hash_map.default_max_load_percentage);
172172
173173 /// For DWARF .debug_info.
174174 pub const DbgInfoTypeReloc = struct {
src/link/C.zig+5-8
......@@ -89,8 +89,7 @@ pub fn freeDecl(self: *C, decl: *Module.Decl) void {
8989fn deinitDecl(gpa: *Allocator, decl: *Module.Decl) void {
9090 decl.link.c.code.deinit(gpa);
9191 decl.fn_link.c.fwd_decl.deinit(gpa);
92 var it = decl.fn_link.c.typedefs.valueIterator();
93 while (it.next()) |value| {
92 for (decl.fn_link.c.typedefs.values()) |value| {
9493 gpa.free(value.rendered);
9594 }
9695 decl.fn_link.c.typedefs.deinit(gpa);
......@@ -108,8 +107,7 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void {
108107 const code = &decl.link.c.code;
109108 fwd_decl.shrinkRetainingCapacity(0);
110109 {
111 var it = typedefs.valueIterator();
112 while (it.next()) |value| {
110 for (typedefs.values()) |value| {
113111 module.gpa.free(value.rendered);
114112 }
115113 }
......@@ -135,8 +133,7 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void {
135133 object.blocks.deinit(module.gpa);
136134 object.code.deinit();
137135 object.dg.fwd_decl.deinit();
138 var it = object.dg.typedefs.valueIterator();
139 while (it.next()) |value| {
136 for (object.dg.typedefs.values()) |value| {
140137 module.gpa.free(value.rendered);
141138 }
142139 object.dg.typedefs.deinit();
......@@ -207,7 +204,7 @@ pub fn flushModule(self: *C, comp: *Compilation) !void {
207204 }
208205
209206 var fn_count: usize = 0;
210 var typedefs = std.HashMap(Type, void, Type.HashContext, std.hash_map.default_max_load_percentage).init(comp.gpa);
207 var typedefs = std.HashMap(Type, void, Type.HashContext64, std.hash_map.default_max_load_percentage).init(comp.gpa);
211208 defer typedefs.deinit();
212209
213210 // Typedefs, forward decls and non-functions first.
......@@ -217,7 +214,7 @@ pub fn flushModule(self: *C, comp: *Compilation) !void {
217214 if (!decl.has_tv) continue;
218215 const buf = buf: {
219216 if (decl.val.castTag(.function)) |_| {
220 try typedefs.ensureUnusedCapacity(decl.fn_link.c.typedefs.count());
217 try typedefs.ensureUnusedCapacity(@intCast(u32, decl.fn_link.c.typedefs.count()));
221218 var it = decl.fn_link.c.typedefs.iterator();
222219 while (it.next()) |new| {
223220 const gop = typedefs.getOrPutAssumeCapacity(new.key_ptr.*);
src/type.zig+12-1
......@@ -602,7 +602,7 @@ pub const Type = extern union {
602602 return hasher.final();
603603 }
604604
605 pub const HashContext = struct {
605 pub const HashContext64 = struct {
606606 pub fn hash(self: @This(), t: Type) u64 {
607607 _ = self;
608608 return t.hash();
......@@ -613,6 +613,17 @@ pub const Type = extern union {
613613 }
614614 };
615615
616 pub const HashContext32 = struct {
617 pub fn hash(self: @This(), t: Type) u32 {
618 _ = self;
619 return @truncate(u32, t.hash());
620 }
621 pub fn eql(self: @This(), a: Type, b: Type) bool {
622 _ = self;
623 return a.eql(b);
624 }
625 };
626
616627 pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type {
617628 if (self.tag_if_small_enough < Tag.no_payload_count) {
618629 return Type{ .tag_if_small_enough = self.tag_if_small_enough };