authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-02-03 16:46:55+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-03 22:55:46+01:00
logc9b957c937ef457083f1a00c1343239086ef8796
treee7d974f5bcc87fbf70f1fa88d59feee50baf37fb
parent60935decd318498529a016eeb1379d943a7e830d

link: remove `FnData` and make it self-owned

This finishes the work started in #14502 where atoms are owned by the linker themselves. This now makes debug atoms fully owned by dwarf, and no information is left stored on the decl.

3 files changed, 22 insertions(+), 28 deletions(-)

src/Module.zig-12
......@@ -531,9 +531,6 @@ pub const Decl = struct {
531531 /// What kind of a declaration is this.
532532 kind: Kind,
533533
534 /// TODO remove this once Wasm backend catches up
535 fn_link: ?link.File.Wasm.FnData = null,
536
537534 /// The shallow set of other decls whose typed_value could possibly change if this Decl's
538535 /// typed_value is modified.
539536 dependants: DepsTable = .{},
......@@ -5247,11 +5244,6 @@ pub fn clearDecl(
52475244 if (decl.has_tv) {
52485245 if (decl.ty.isFnOrHasRuntimeBits()) {
52495246 mod.comp.bin_file.freeDecl(decl_index);
5250
5251 decl.fn_link = switch (mod.comp.bin_file.tag) {
5252 .wasm => link.File.Wasm.FnData.empty,
5253 else => null,
5254 };
52555247 }
52565248 if (decl.getInnerNamespace()) |namespace| {
52575249 try namespace.deleteAllDecls(mod, outdated_decls);
......@@ -5652,10 +5644,6 @@ pub fn allocateNewDecl(
56525644 .deletion_flag = false,
56535645 .zir_decl_index = 0,
56545646 .src_scope = src_scope,
5655 .fn_link = switch (mod.comp.bin_file.tag) {
5656 .wasm => link.File.Wasm.FnData.empty,
5657 else => null,
5658 },
56595647 .generation = 0,
56605648 .is_pub = false,
56615649 .is_exported = false,
src/arch/wasm/CodeGen.zig+3-3
......@@ -1194,7 +1194,7 @@ fn genFunc(func: *CodeGen) InnerError!void {
11941194 const fn_info = func.decl.ty.fnInfo();
11951195 var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, func.target);
11961196 defer func_type.deinit(func.gpa);
1197 func.decl.fn_link.?.type_index = try func.bin_file.putOrGetFuncType(func_type);
1197 _ = try func.bin_file.storeDeclType(func.decl_index, func_type);
11981198
11991199 var cc_result = try func.resolveCallingConventionValues(func.decl.ty);
12001200 defer cc_result.deinit(func.gpa);
......@@ -2131,12 +2131,12 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
21312131 defer func_type.deinit(func.gpa);
21322132 const atom_index = try func.bin_file.getOrCreateAtomForDecl(extern_fn.data.owner_decl);
21332133 const atom = func.bin_file.getAtomPtr(atom_index);
2134 ext_decl.fn_link.?.type_index = try func.bin_file.putOrGetFuncType(func_type);
2134 const type_index = try func.bin_file.storeDeclType(extern_fn.data.owner_decl, func_type);
21352135 try func.bin_file.addOrUpdateImport(
21362136 mem.sliceTo(ext_decl.name, 0),
21372137 atom.getSymbolIndex().?,
21382138 ext_decl.getExternFn().?.lib_name,
2139 ext_decl.fn_link.?.type_index,
2139 type_index,
21402140 );
21412141 break :blk extern_fn.data.owner_decl;
21422142 } else if (func_val.castTag(.decl_ref)) |decl_ref| {
src/link/Wasm.zig+19-13
......@@ -46,6 +46,9 @@ host_name: []const u8 = "env",
4646/// List of all `Decl` that are currently alive.
4747/// Each index maps to the corresponding `Atom.Index`.
4848decls: std.AutoHashMapUnmanaged(Module.Decl.Index, Atom.Index) = .{},
49/// Mapping between an `Atom` and its type index representing the Wasm
50/// type of the function signature.
51atom_types: std.AutoHashMapUnmanaged(Atom.Index, u32) = .{},
4952/// List of all symbols generated by Zig code.
5053symbols: std.ArrayListUnmanaged(Symbol) = .{},
5154/// List of symbol indexes which are free to be used.
......@@ -175,15 +178,6 @@ pub const Segment = struct {
175178 offset: u32,
176179};
177180
178pub const FnData = struct {
179 /// Reference to the wasm type that represents this function.
180 type_index: u32,
181
182 pub const empty: FnData = .{
183 .type_index = undefined,
184 };
185};
186
187181pub const Export = struct {
188182 sym_index: ?u32 = null,
189183};
......@@ -961,6 +955,7 @@ pub fn deinit(wasm: *Wasm) void {
961955 }
962956
963957 wasm.decls.deinit(gpa);
958 wasm.atom_types.deinit(gpa);
964959 wasm.symbols.deinit(gpa);
965960 wasm.symbols_free_list.deinit(gpa);
966961 wasm.globals.deinit(gpa);
......@@ -1607,7 +1602,7 @@ const Kind = union(enum) {
16071602 initialized,
16081603 synthetic,
16091604 },
1610 function: FnData,
1605 function: void,
16111606
16121607 /// Returns the segment name the data kind represents.
16131608 /// Asserts `kind` has its active tag set to `data`.
......@@ -1626,12 +1621,13 @@ fn parseAtom(wasm: *Wasm, atom_index: Atom.Index, kind: Kind) !void {
16261621 const atom = wasm.getAtomPtr(atom_index);
16271622 const symbol = (SymbolLoc{ .file = null, .index = atom.sym_index }).getSymbol(wasm);
16281623 const final_index: u32 = switch (kind) {
1629 .function => |fn_data| result: {
1624 .function => result: {
16301625 const index = @intCast(u32, wasm.functions.count() + wasm.imported_functions_count);
1626 const type_index = wasm.atom_types.get(atom_index).?;
16311627 try wasm.functions.putNoClobber(
16321628 wasm.base.allocator,
16331629 .{ .file = null, .index = index },
1634 .{ .type_index = fn_data.type_index },
1630 .{ .type_index = type_index },
16351631 );
16361632 symbol.tag = .function;
16371633 symbol.index = index;
......@@ -2829,7 +2825,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
28292825 if (decl.isExtern()) continue;
28302826 const atom_index = entry.value_ptr.*;
28312827 if (decl.ty.zigTypeTag() == .Fn) {
2832 try wasm.parseAtom(atom_index, .{ .function = decl.fn_link.? });
2828 try wasm.parseAtom(atom_index, .function);
28332829 } else if (decl.getVariable()) |variable| {
28342830 if (!variable.is_mutable) {
28352831 try wasm.parseAtom(atom_index, .{ .data = .read_only });
......@@ -4172,3 +4168,13 @@ pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 {
41724168 });
41734169 return index;
41744170}
4171
4172/// For the given `decl_index`, stores the corresponding type representing the function signature.
4173/// Asserts declaration has an associated `Atom`.
4174/// Returns the index into the list of types.
4175pub fn storeDeclType(wasm: *Wasm, decl_index: Module.Decl.Index, func_type: std.wasm.Type) !u32 {
4176 const atom_index = wasm.decls.get(decl_index).?;
4177 const index = try wasm.putOrGetFuncType(func_type);
4178 try wasm.atom_types.put(wasm.base.allocator, atom_index, index);
4179 return index;
4180}