authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-29 16:02:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-29 19:14:17-07:00
log0d841e827ac120725eef1100b79f1807a2bc04bd
tree4710c9c306f7cc0768c2340aee55be63b493d26f
parent101df768a06ef85753efdd6dc558bca68d50d1a5

C backend: remove unneeded ordering mechanism

This logic to lower snippets of C code in a dependency order is no longer needed. Simplify the logic by deleting the mechanism.

1 files changed, 6 insertions(+), 27 deletions(-)

src/link/C.zig+6-27
......@@ -322,29 +322,19 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
322322 self.lazy_code_buf.clearRetainingCapacity();
323323 try self.flushErrDecls(&f.lazy_ctypes);
324324
325 // Unlike other backends, the .c code we are emitting has order-dependent decls.
325326 // `CType`s, forward decls, and non-functions first.
326 // Unlike other backends, the .c code we are emitting is order-dependent. Therefore
327 // we must traverse the set of Decls that we are emitting according to their dependencies.
328 // Our strategy is to populate a set of remaining decls, pop Decls one by one,
329 // recursively chasing their dependencies.
330 try f.remaining_decls.ensureUnusedCapacity(gpa, self.decl_table.count());
331
332 const decl_keys = self.decl_table.keys();
333 const decl_values = self.decl_table.values();
334 for (decl_keys) |decl_index| {
335 assert(module.declPtr(decl_index).has_tv);
336 f.remaining_decls.putAssumeCapacityNoClobber(decl_index, {});
337 }
338327
339328 {
340329 var export_names: std.AutoHashMapUnmanaged(InternPool.NullTerminatedString, void) = .{};
341330 defer export_names.deinit(gpa);
342 try export_names.ensureTotalCapacity(gpa, @as(u32, @intCast(module.decl_exports.entries.len)));
331 try export_names.ensureTotalCapacity(gpa, @intCast(module.decl_exports.entries.len));
343332 for (module.decl_exports.values()) |exports| for (exports.items) |@"export"|
344333 try export_names.put(gpa, @"export".opts.name, {});
345334
346 while (f.remaining_decls.popOrNull()) |kv| {
347 const decl_index = kv.key;
335 const decl_keys = self.decl_table.keys();
336 for (decl_keys) |decl_index| {
337 assert(module.declPtr(decl_index).has_tv);
348338 try self.flushDecl(&f, decl_index, export_names);
349339 }
350340 }
......@@ -374,6 +364,7 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
374364 f.file_size += lazy_fwd_decl_len;
375365
376366 // Now the code.
367 const decl_values = self.decl_table.values();
377368 try f.all_buffers.ensureUnusedCapacity(gpa, 1 + decl_values.len);
378369 f.appendBufAssumeCapacity(self.lazy_code_buf.items);
379370 for (decl_values) |decl| f.appendBufAssumeCapacity(self.getString(decl.code));
......@@ -384,8 +375,6 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
384375}
385376
386377const Flush = struct {
387 remaining_decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, void) = .{},
388
389378 ctypes: codegen.CType.Store = .{},
390379 ctypes_map: std.ArrayListUnmanaged(codegen.CType.Index) = .{},
391380 ctypes_buf: std.ArrayListUnmanaged(u8) = .{},
......@@ -416,7 +405,6 @@ const Flush = struct {
416405 f.ctypes_buf.deinit(gpa);
417406 f.ctypes_map.deinit(gpa);
418407 f.ctypes.deinit(gpa);
419 f.remaining_decls.deinit(gpa);
420408 }
421409};
422410
......@@ -591,7 +579,6 @@ fn flushLazyFns(self: *C, f: *Flush, lazy_fns: codegen.LazyFnMap) FlushDeclError
591579 }
592580}
593581
594/// Assumes `decl` was in the `remaining_decls` set, and has already been removed.
595582fn flushDecl(
596583 self: *C,
597584 f: *Flush,
......@@ -601,14 +588,6 @@ fn flushDecl(
601588 const gpa = self.base.allocator;
602589 const mod = self.base.options.module.?;
603590 const decl = mod.declPtr(decl_index);
604 // Before flushing any particular Decl we must ensure its
605 // dependencies are already flushed, so that the order in the .c
606 // file comes out correctly.
607 for (decl.dependencies.keys()) |dep| {
608 if (f.remaining_decls.swapRemove(dep)) {
609 try flushDecl(self, f, dep, export_names);
610 }
611 }
612591
613592 const decl_block = self.decl_table.getPtr(decl_index).?;
614593