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 {...@@ -531,9 +531,6 @@ pub const Decl = struct {
531 /// What kind of a declaration is this.531 /// What kind of a declaration is this.
532 kind: Kind,532 kind: Kind,
533533
534 /// TODO remove this once Wasm backend catches up
535 fn_link: ?link.File.Wasm.FnData = null,
536
537 /// The shallow set of other decls whose typed_value could possibly change if this Decl's534 /// The shallow set of other decls whose typed_value could possibly change if this Decl's
538 /// typed_value is modified.535 /// typed_value is modified.
539 dependants: DepsTable = .{},536 dependants: DepsTable = .{},
...@@ -5247,11 +5244,6 @@ pub fn clearDecl(...@@ -5247,11 +5244,6 @@ pub fn clearDecl(
5247 if (decl.has_tv) {5244 if (decl.has_tv) {
5248 if (decl.ty.isFnOrHasRuntimeBits()) {5245 if (decl.ty.isFnOrHasRuntimeBits()) {
5249 mod.comp.bin_file.freeDecl(decl_index);5246 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 };
5255 }5247 }
5256 if (decl.getInnerNamespace()) |namespace| {5248 if (decl.getInnerNamespace()) |namespace| {
5257 try namespace.deleteAllDecls(mod, outdated_decls);5249 try namespace.deleteAllDecls(mod, outdated_decls);
...@@ -5652,10 +5644,6 @@ pub fn allocateNewDecl(...@@ -5652,10 +5644,6 @@ pub fn allocateNewDecl(
5652 .deletion_flag = false,5644 .deletion_flag = false,
5653 .zir_decl_index = 0,5645 .zir_decl_index = 0,
5654 .src_scope = src_scope,5646 .src_scope = src_scope,
5655 .fn_link = switch (mod.comp.bin_file.tag) {
5656 .wasm => link.File.Wasm.FnData.empty,
5657 else => null,
5658 },
5659 .generation = 0,5647 .generation = 0,
5660 .is_pub = false,5648 .is_pub = false,
5661 .is_exported = false,5649 .is_exported = false,
src/arch/wasm/CodeGen.zig+3-3
...@@ -1194,7 +1194,7 @@ fn genFunc(func: *CodeGen) InnerError!void {...@@ -1194,7 +1194,7 @@ fn genFunc(func: *CodeGen) InnerError!void {
1194 const fn_info = func.decl.ty.fnInfo();1194 const fn_info = func.decl.ty.fnInfo();
1195 var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, func.target);1195 var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, func.target);
1196 defer func_type.deinit(func.gpa);1196 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
1199 var cc_result = try func.resolveCallingConventionValues(func.decl.ty);1199 var cc_result = try func.resolveCallingConventionValues(func.decl.ty);
1200 defer cc_result.deinit(func.gpa);1200 defer cc_result.deinit(func.gpa);
...@@ -2131,12 +2131,12 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -2131,12 +2131,12 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
2131 defer func_type.deinit(func.gpa);2131 defer func_type.deinit(func.gpa);
2132 const atom_index = try func.bin_file.getOrCreateAtomForDecl(extern_fn.data.owner_decl);2132 const atom_index = try func.bin_file.getOrCreateAtomForDecl(extern_fn.data.owner_decl);
2133 const atom = func.bin_file.getAtomPtr(atom_index);2133 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);
2135 try func.bin_file.addOrUpdateImport(2135 try func.bin_file.addOrUpdateImport(
2136 mem.sliceTo(ext_decl.name, 0),2136 mem.sliceTo(ext_decl.name, 0),
2137 atom.getSymbolIndex().?,2137 atom.getSymbolIndex().?,
2138 ext_decl.getExternFn().?.lib_name,2138 ext_decl.getExternFn().?.lib_name,
2139 ext_decl.fn_link.?.type_index,2139 type_index,
2140 );2140 );
2141 break :blk extern_fn.data.owner_decl;2141 break :blk extern_fn.data.owner_decl;
2142 } else if (func_val.castTag(.decl_ref)) |decl_ref| {2142 } else if (func_val.castTag(.decl_ref)) |decl_ref| {
src/link/Wasm.zig+19-13
...@@ -46,6 +46,9 @@ host_name: []const u8 = "env",...@@ -46,6 +46,9 @@ host_name: []const u8 = "env",
46/// List of all `Decl` that are currently alive.46/// List of all `Decl` that are currently alive.
47/// Each index maps to the corresponding `Atom.Index`.47/// Each index maps to the corresponding `Atom.Index`.
48decls: std.AutoHashMapUnmanaged(Module.Decl.Index, Atom.Index) = .{},48decls: 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) = .{},
49/// List of all symbols generated by Zig code.52/// List of all symbols generated by Zig code.
50symbols: std.ArrayListUnmanaged(Symbol) = .{},53symbols: std.ArrayListUnmanaged(Symbol) = .{},
51/// List of symbol indexes which are free to be used.54/// List of symbol indexes which are free to be used.
...@@ -175,15 +178,6 @@ pub const Segment = struct {...@@ -175,15 +178,6 @@ pub const Segment = struct {
175 offset: u32,178 offset: u32,
176};179};
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
187pub const Export = struct {181pub const Export = struct {
188 sym_index: ?u32 = null,182 sym_index: ?u32 = null,
189};183};
...@@ -961,6 +955,7 @@ pub fn deinit(wasm: *Wasm) void {...@@ -961,6 +955,7 @@ pub fn deinit(wasm: *Wasm) void {
961 }955 }
962956
963 wasm.decls.deinit(gpa);957 wasm.decls.deinit(gpa);
958 wasm.atom_types.deinit(gpa);
964 wasm.symbols.deinit(gpa);959 wasm.symbols.deinit(gpa);
965 wasm.symbols_free_list.deinit(gpa);960 wasm.symbols_free_list.deinit(gpa);
966 wasm.globals.deinit(gpa);961 wasm.globals.deinit(gpa);
...@@ -1607,7 +1602,7 @@ const Kind = union(enum) {...@@ -1607,7 +1602,7 @@ const Kind = union(enum) {
1607 initialized,1602 initialized,
1608 synthetic,1603 synthetic,
1609 },1604 },
1610 function: FnData,1605 function: void,
16111606
1612 /// Returns the segment name the data kind represents.1607 /// Returns the segment name the data kind represents.
1613 /// Asserts `kind` has its active tag set to `data`.1608 /// Asserts `kind` has its active tag set to `data`.
...@@ -1626,12 +1621,13 @@ fn parseAtom(wasm: *Wasm, atom_index: Atom.Index, kind: Kind) !void {...@@ -1626,12 +1621,13 @@ fn parseAtom(wasm: *Wasm, atom_index: Atom.Index, kind: Kind) !void {
1626 const atom = wasm.getAtomPtr(atom_index);1621 const atom = wasm.getAtomPtr(atom_index);
1627 const symbol = (SymbolLoc{ .file = null, .index = atom.sym_index }).getSymbol(wasm);1622 const symbol = (SymbolLoc{ .file = null, .index = atom.sym_index }).getSymbol(wasm);
1628 const final_index: u32 = switch (kind) {1623 const final_index: u32 = switch (kind) {
1629 .function => |fn_data| result: {1624 .function => result: {
1630 const index = @intCast(u32, wasm.functions.count() + wasm.imported_functions_count);1625 const index = @intCast(u32, wasm.functions.count() + wasm.imported_functions_count);
1626 const type_index = wasm.atom_types.get(atom_index).?;
1631 try wasm.functions.putNoClobber(1627 try wasm.functions.putNoClobber(
1632 wasm.base.allocator,1628 wasm.base.allocator,
1633 .{ .file = null, .index = index },1629 .{ .file = null, .index = index },
1634 .{ .type_index = fn_data.type_index },1630 .{ .type_index = type_index },
1635 );1631 );
1636 symbol.tag = .function;1632 symbol.tag = .function;
1637 symbol.index = index;1633 symbol.index = index;
...@@ -2829,7 +2825,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -2829,7 +2825,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
2829 if (decl.isExtern()) continue;2825 if (decl.isExtern()) continue;
2830 const atom_index = entry.value_ptr.*;2826 const atom_index = entry.value_ptr.*;
2831 if (decl.ty.zigTypeTag() == .Fn) {2827 if (decl.ty.zigTypeTag() == .Fn) {
2832 try wasm.parseAtom(atom_index, .{ .function = decl.fn_link.? });2828 try wasm.parseAtom(atom_index, .function);
2833 } else if (decl.getVariable()) |variable| {2829 } else if (decl.getVariable()) |variable| {
2834 if (!variable.is_mutable) {2830 if (!variable.is_mutable) {
2835 try wasm.parseAtom(atom_index, .{ .data = .read_only });2831 try wasm.parseAtom(atom_index, .{ .data = .read_only });
...@@ -4172,3 +4168,13 @@ pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 {...@@ -4172,3 +4168,13 @@ pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 {
4172 });4168 });
4173 return index;4169 return index;
4174}4170}
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}