authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-16 15:34:00-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
log761387dc556e065e39d021bf96fb4d4c8873a145
treeac16bdf0970469411e2b1ea73334a543adbc5cfc
parent70414c1f434dea4d749f1d1445816dffc88a7a68

wasm linker: implement type index method


2 files changed, 28 insertions(+), 14 deletions(-)

src/link/Wasm.zig+28-11
......@@ -199,7 +199,7 @@ global_exports_len: u32 = 0,
199199functions: std.AutoArrayHashMapUnmanaged(FunctionImport.Resolution, void) = .empty,
200200/// Tracks the value at the end of prelink, at which point `functions`
201201/// contains only object file functions, and nothing from the Zcu yet.
202functions_len: u32 = 0,
202functions_end_prelink: u32 = 0,
203203/// Immutable after prelink. The undefined functions coming only from all object files.
204204/// The Zcu must satisfy these.
205205function_imports_init_keys: []String = &.{},
......@@ -214,7 +214,7 @@ function_imports: std.AutoArrayHashMapUnmanaged(String, FunctionImportId) = .emp
214214globals: std.AutoArrayHashMapUnmanaged(GlobalImport.Resolution, void) = .empty,
215215/// Tracks the value at the end of prelink, at which point `globals`
216216/// contains only object file globals, and nothing from the Zcu yet.
217globals_len: u32 = 0,
217globals_end_prelink: u32 = 0,
218218global_imports_init_keys: []String = &.{},
219219global_imports_init_vals: []GlobalImportId = &.{},
220220global_imports: std.AutoArrayHashMapUnmanaged(String, GlobalImportId) = .empty,
......@@ -620,6 +620,17 @@ pub const ZcuFunc = extern struct {
620620 const nav = ip.getNav(func.owner_nav);
621621 return nav.fqn.toSlice(ip);
622622 }
623
624 pub fn typeIndex(i: @This(), wasm: *Wasm) ?FunctionType.Index {
625 const comp = wasm.base.comp;
626 const zcu = comp.zcu.?;
627 const target = &comp.root_mod.resolved_target.result;
628 const ip = &zcu.intern_pool;
629 const func = ip.toFunc(i.key(wasm).*);
630 const fn_ty = zcu.navValue(func.owner_nav).typeOf(zcu);
631 const fn_info = zcu.typeToFunc(fn_ty).?;
632 return wasm.getExistingFunctionType(fn_info.cc, fn_info.param_types.get(ip), .fromInterned(fn_info.return_type), target);
633 }
623634 };
624635};
625636
......@@ -730,7 +741,7 @@ pub const FunctionImport = extern struct {
730741 };
731742 }
732743
733 pub fn typeIndex(r: Resolution, wasm: *const Wasm) FunctionType.Index {
744 pub fn typeIndex(r: Resolution, wasm: *Wasm) FunctionType.Index {
734745 return switch (unpack(r, wasm)) {
735746 .unresolved => unreachable,
736747 .__wasm_apply_global_tls_relocs => @panic("TODO"),
......@@ -739,7 +750,7 @@ pub const FunctionImport = extern struct {
739750 .__wasm_init_tls => @panic("TODO"),
740751 .__zig_error_names => @panic("TODO"),
741752 .object_function => |i| i.ptr(wasm).type_index,
742 .zcu_func => @panic("TODO"),
753 .zcu_func => |i| i.typeIndex(wasm).?,
743754 };
744755 }
745756
......@@ -2247,7 +2258,7 @@ pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!v
22472258 try markFunction(wasm, name, import, @enumFromInt(i));
22482259 }
22492260 }
2250 wasm.functions_len = @intCast(wasm.functions.entries.len);
2261 wasm.functions_end_prelink = @intCast(wasm.functions.entries.len);
22512262 wasm.function_imports_init_keys = try gpa.dupe(String, wasm.function_imports.keys());
22522263 wasm.function_imports_init_vals = try gpa.dupe(FunctionImportId, wasm.function_imports.values());
22532264 wasm.function_exports_len = @intCast(wasm.function_exports.items.len);
......@@ -2257,7 +2268,7 @@ pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!v
22572268 try markGlobal(wasm, name, import, @enumFromInt(i));
22582269 }
22592270 }
2260 wasm.globals_len = @intCast(wasm.globals.entries.len);
2271 wasm.globals_end_prelink = @intCast(wasm.globals.entries.len);
22612272 wasm.global_imports_init_keys = try gpa.dupe(String, wasm.global_imports.keys());
22622273 wasm.global_imports_init_vals = try gpa.dupe(GlobalImportId, wasm.global_imports.values());
22632274 wasm.global_exports_len = @intCast(wasm.global_exports.items.len);
......@@ -2451,7 +2462,14 @@ pub fn flushModule(
24512462 const sub_prog_node = prog_node.start("Wasm Flush", 0);
24522463 defer sub_prog_node.end();
24532464
2465 const functions_end_zcu: u32 = @intCast(wasm.functions.entries.len);
2466 defer wasm.functions.shrinkRetainingCapacity(functions_end_zcu);
2467
2468 const globals_end_zcu: u32 = @intCast(wasm.globals.entries.len);
2469 defer wasm.globals.shrinkRetainingCapacity(globals_end_zcu);
2470
24542471 wasm.flush_buffer.clear();
2472
24552473 return wasm.flush_buffer.finish(wasm) catch |err| switch (err) {
24562474 error.OutOfMemory => return error.OutOfMemory,
24572475 error.LinkFailure => return error.LinkFailure,
......@@ -2932,7 +2950,7 @@ pub fn addFuncType(wasm: *Wasm, ft: FunctionType) Allocator.Error!FunctionType.I
29322950 return @enumFromInt(gop.index);
29332951}
29342952
2935pub fn getExistingFuncType(wasm: *Wasm, ft: FunctionType) ?FunctionType.Index {
2953pub fn getExistingFuncType(wasm: *const Wasm, ft: FunctionType) ?FunctionType.Index {
29362954 const index = wasm.func_types.getIndex(ft) orelse return null;
29372955 return @enumFromInt(index);
29382956}
......@@ -2944,7 +2962,7 @@ pub fn internFunctionType(
29442962 return_type: ZcuType,
29452963 target: *const std.Target,
29462964) Allocator.Error!FunctionType.Index {
2947 try convertZcuFnType(wasm, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch);
2965 try convertZcuFnType(wasm.base.comp, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch);
29482966 return wasm.addFuncType(.{
29492967 .params = try wasm.internValtypeList(wasm.params_scratch.items),
29502968 .returns = try wasm.internValtypeList(wasm.returns_scratch.items),
......@@ -2958,7 +2976,7 @@ pub fn getExistingFunctionType(
29582976 return_type: ZcuType,
29592977 target: *const std.Target,
29602978) ?FunctionType.Index {
2961 convertZcuFnType(wasm, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch) catch |err| switch (err) {
2979 convertZcuFnType(wasm.base.comp, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch) catch |err| switch (err) {
29622980 error.OutOfMemory => return null,
29632981 };
29642982 return wasm.getExistingFuncType(.{
......@@ -3008,7 +3026,7 @@ pub fn navSymbolIndex(wasm: *Wasm, nav_index: InternPool.Nav.Index) Allocator.Er
30083026}
30093027
30103028fn convertZcuFnType(
3011 wasm: *Wasm,
3029 comp: *Compilation,
30123030 cc: std.builtin.CallingConvention,
30133031 params: []const InternPool.Index,
30143032 return_type: ZcuType,
......@@ -3019,7 +3037,6 @@ fn convertZcuFnType(
30193037 params_buffer.clearRetainingCapacity();
30203038 returns_buffer.clearRetainingCapacity();
30213039
3022 const comp = wasm.base.comp;
30233040 const gpa = comp.gpa;
30243041 const zcu = comp.zcu.?;
30253042
src/link/Wasm/Flush.zig-3
......@@ -129,9 +129,6 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
129129
130130 if (diags.hasErrors()) return error.LinkFailure;
131131
132 wasm.functions.shrinkRetainingCapacity(wasm.functions_len);
133 wasm.globals.shrinkRetainingCapacity(wasm.globals_len);
134
135132 // TODO only include init functions for objects with must_link=true or
136133 // which have any alive functions inside them.
137134 if (wasm.object_init_funcs.items.len > 0) {