authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-01-27 19:24:15+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-01-27 19:24:15+01:00
logb25efb86e1b1b2a9e8aa269bf83b717d54f7e276
tree774b958f096b5eb6363b0326b294ca8c9b57e4af
parentcc1d7a0e315ba63b0d8c0cd647b4c7e92a571bf2
signaturelock-open Commit is signed but in an unrecognized format.

wasm: migrate to new non-allocateDeclIndexes API


5 files changed, 53 insertions(+), 44 deletions(-)

src/Module.zig+1
......@@ -5328,6 +5328,7 @@ pub fn deleteUnusedDecl(mod: *Module, decl_index: Decl.Index) void {
53285328 .elf,
53295329 .macho,
53305330 .c,
5331 .wasm,
53315332 => {}, // this linker backend has already migrated to the new API
53325333
53335334 else => if (decl.has_tv) {
src/arch/wasm/CodeGen.zig+10-3
......@@ -2120,22 +2120,28 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
21202120 const module = func.bin_file.base.options.module.?;
21212121
21222122 if (func_val.castTag(.function)) |function| {
2123 break :blk module.declPtr(function.data.owner_decl);
2123 const decl = module.declPtr(function.data.owner_decl);
2124 try decl.link.wasm.ensureInitialized(func.bin_file);
2125 break :blk decl;
21242126 } else if (func_val.castTag(.extern_fn)) |extern_fn| {
21252127 const ext_decl = module.declPtr(extern_fn.data.owner_decl);
21262128 const ext_info = ext_decl.ty.fnInfo();
21272129 var func_type = try genFunctype(func.gpa, ext_info.cc, ext_info.param_types, ext_info.return_type, func.target);
21282130 defer func_type.deinit(func.gpa);
2131 const atom = &ext_decl.link.wasm;
2132 try atom.ensureInitialized(func.bin_file);
21292133 ext_decl.fn_link.wasm.type_index = try func.bin_file.putOrGetFuncType(func_type);
21302134 try func.bin_file.addOrUpdateImport(
21312135 mem.sliceTo(ext_decl.name, 0),
2132 ext_decl.link.wasm.sym_index,
2136 atom.getSymbolIndex().?,
21332137 ext_decl.getExternFn().?.lib_name,
21342138 ext_decl.fn_link.wasm.type_index,
21352139 );
21362140 break :blk ext_decl;
21372141 } else if (func_val.castTag(.decl_ref)) |decl_ref| {
2138 break :blk module.declPtr(decl_ref.data);
2142 const decl = module.declPtr(decl_ref.data);
2143 try decl.link.wasm.ensureInitialized(func.bin_file);
2144 break :blk decl;
21392145 }
21402146 return func.fail("Expected a function, but instead found type '{}'", .{func_val.tag()});
21412147 };
......@@ -2752,6 +2758,7 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: Module.Decl.Ind
27522758 }
27532759
27542760 module.markDeclAlive(decl);
2761 try decl.link.wasm.ensureInitialized(func.bin_file);
27552762
27562763 const target_sym_index = decl.link.wasm.sym_index;
27572764 if (decl.ty.zigTypeTag() == .Fn) {
src/link.zig+1-1
......@@ -615,7 +615,6 @@ pub const File = struct {
615615 return;
616616 }
617617 switch (base.tag) {
618 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),
619618 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl_index),
620619
621620 .coff,
......@@ -624,6 +623,7 @@ pub const File = struct {
624623 .c,
625624 .spirv,
626625 .nvptx,
626 .wasm,
627627 => {},
628628 }
629629 }
src/link/Wasm.zig+30-40
......@@ -986,31 +986,23 @@ pub fn deinit(wasm: *Wasm) void {
986986 }
987987}
988988
989pub fn allocateDeclIndexes(wasm: *Wasm, decl_index: Module.Decl.Index) !void {
990 if (wasm.llvm_object) |_| return;
991 const decl = wasm.base.options.module.?.declPtr(decl_index);
992 if (decl.link.wasm.sym_index != 0) return;
993
989/// Allocates a new symbol and returns its index.
990/// Will re-use slots when a symbol was freed at an earlier stage.
991pub fn allocateSymbol(wasm: *Wasm) !u32 {
994992 try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1);
995 try wasm.decls.putNoClobber(wasm.base.allocator, decl_index, {});
996
997 const atom = &decl.link.wasm;
998
999993 var symbol: Symbol = .{
1000994 .name = undefined, // will be set after updateDecl
1001995 .flags = @enumToInt(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
1002996 .tag = undefined, // will be set after updateDecl
1003997 .index = undefined, // will be set after updateDecl
1004998 };
1005
1006999 if (wasm.symbols_free_list.popOrNull()) |index| {
1007 atom.sym_index = index;
10081000 wasm.symbols.items[index] = symbol;
1009 } else {
1010 atom.sym_index = @intCast(u32, wasm.symbols.items.len);
1011 wasm.symbols.appendAssumeCapacity(symbol);
1001 return index;
10121002 }
1013 try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom);
1003 const index = @intCast(u32, wasm.symbols.items.len);
1004 wasm.symbols.appendAssumeCapacity(symbol);
1005 return index;
10141006}
10151007
10161008pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
......@@ -1026,9 +1018,12 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
10261018
10271019 const decl_index = func.owner_decl;
10281020 const decl = mod.declPtr(decl_index);
1029 assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes()
1030
1031 decl.link.wasm.clear();
1021 const atom = &decl.link.wasm;
1022 try atom.ensureInitialized(wasm);
1023 const gop = try wasm.decls.getOrPut(wasm.base.allocator, decl_index);
1024 if (gop.found_existing) {
1025 atom.clear();
1026 } else gop.value_ptr.* = {};
10321027
10331028 var decl_state: ?Dwarf.DeclState = if (wasm.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl_index) else null;
10341029 defer if (decl_state) |*ds| ds.deinit();
......@@ -1083,16 +1078,19 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
10831078 defer tracy.end();
10841079
10851080 const decl = mod.declPtr(decl_index);
1086 assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes()
1087
1088 decl.link.wasm.clear();
1089
10901081 if (decl.val.castTag(.function)) |_| {
10911082 return;
10921083 } else if (decl.val.castTag(.extern_fn)) |_| {
10931084 return;
10941085 }
10951086
1087 const atom = &decl.link.wasm;
1088 try atom.ensureInitialized(wasm);
1089 const gop = try wasm.decls.getOrPut(wasm.base.allocator, decl_index);
1090 if (gop.found_existing) {
1091 atom.clear();
1092 } else gop.value_ptr.* = {};
1093
10961094 if (decl.isExtern()) {
10971095 const variable = decl.getVariable().?;
10981096 const name = mem.sliceTo(decl.name, 0);
......@@ -1148,8 +1146,8 @@ fn finishUpdateDecl(wasm: *Wasm, decl: *Module.Decl, code: []const u8) !void {
11481146 try atom.code.appendSlice(wasm.base.allocator, code);
11491147 try wasm.resolved_symbols.put(wasm.base.allocator, atom.symbolLoc(), {});
11501148
1151 if (code.len == 0) return;
11521149 atom.size = @intCast(u32, code.len);
1150 if (code.len == 0) return;
11531151 atom.alignment = decl.ty.abiAlignment(wasm.base.options.target);
11541152}
11551153
......@@ -1211,28 +1209,19 @@ pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: Module.Decl.In
12111209 defer wasm.base.allocator.free(fqdn);
12121210 const name = try std.fmt.allocPrintZ(wasm.base.allocator, "__unnamed_{s}_{d}", .{ fqdn, local_index });
12131211 defer wasm.base.allocator.free(name);
1214 var symbol: Symbol = .{
1215 .name = try wasm.string_table.put(wasm.base.allocator, name),
1216 .flags = 0,
1217 .tag = .data,
1218 .index = undefined,
1219 };
1220 symbol.setFlag(.WASM_SYM_BINDING_LOCAL);
12211212
12221213 const atom = try decl.link.wasm.locals.addOne(wasm.base.allocator);
12231214 atom.* = Atom.empty;
1215 try atom.ensureInitialized(wasm);
12241216 atom.alignment = tv.ty.abiAlignment(wasm.base.options.target);
1225 try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1);
1217 wasm.symbols.items[atom.sym_index] = .{
1218 .name = try wasm.string_table.put(wasm.base.allocator, name),
1219 .flags = @enumToInt(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
1220 .tag = .data,
1221 .index = undefined,
1222 };
12261223
1227 if (wasm.symbols_free_list.popOrNull()) |index| {
1228 atom.sym_index = index;
1229 wasm.symbols.items[index] = symbol;
1230 } else {
1231 atom.sym_index = @intCast(u32, wasm.symbols.items.len);
1232 wasm.symbols.appendAssumeCapacity(symbol);
1233 }
12341224 try wasm.resolved_symbols.putNoClobber(wasm.base.allocator, atom.symbolLoc(), {});
1235 try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom);
12361225
12371226 var value_bytes = std.ArrayList(u8).init(wasm.base.allocator);
12381227 defer value_bytes.deinit();
......@@ -1304,8 +1293,8 @@ pub fn getDeclVAddr(
13041293) !u64 {
13051294 const mod = wasm.base.options.module.?;
13061295 const decl = mod.declPtr(decl_index);
1296 try decl.link.wasm.ensureInitialized(wasm);
13071297 const target_symbol_index = decl.link.wasm.sym_index;
1308 assert(target_symbol_index != 0);
13091298 assert(reloc_info.parent_atom_index != 0);
13101299 const atom = wasm.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?;
13111300 const is_wasm32 = wasm.base.options.target.cpu.arch == .wasm32;
......@@ -1363,6 +1352,7 @@ pub fn updateDeclExports(
13631352 }
13641353
13651354 const decl = mod.declPtr(decl_index);
1355 if (decl.link.wasm.getSymbolIndex() == null) return; // unititialized
13661356
13671357 for (exports) |exp| {
13681358 if (exp.options.section) |section| {
src/link/Wasm/Atom.zig+11
......@@ -95,6 +95,17 @@ pub fn symbolLoc(atom: Atom) Wasm.SymbolLoc {
9595 return .{ .file = atom.file, .index = atom.sym_index };
9696}
9797
98pub fn ensureInitialized(atom: *Atom, wasm_bin: *Wasm) !void {
99 if (atom.getSymbolIndex() != null) return; // already initialized
100 atom.sym_index = try wasm_bin.allocateSymbol();
101 try wasm_bin.symbol_atom.putNoClobber(wasm_bin.base.allocator, atom.symbolLoc(), atom);
102}
103
104pub fn getSymbolIndex(atom: Atom) ?u32 {
105 if (atom.sym_index == 0) return null;
106 return atom.sym_index;
107}
108
98109/// Returns the virtual address of the `Atom`. This is the address starting
99110/// from the first entry within a section.
100111pub fn getVA(atom: Atom, wasm: *const Wasm, symbol: *const Symbol) u32 {