| ... | ... | @@ -199,7 +199,7 @@ global_exports_len: u32 = 0, |
| 199 | 199 | functions: std.AutoArrayHashMapUnmanaged(FunctionImport.Resolution, void) = .empty, |
| 200 | 200 | /// Tracks the value at the end of prelink, at which point `functions` |
| 201 | 201 | /// contains only object file functions, and nothing from the Zcu yet. |
| 202 | | functions_len: u32 = 0, |
| 202 | functions_end_prelink: u32 = 0, |
| 203 | 203 | /// Immutable after prelink. The undefined functions coming only from all object files. |
| 204 | 204 | /// The Zcu must satisfy these. |
| 205 | 205 | function_imports_init_keys: []String = &.{}, |
| ... | ... | @@ -214,7 +214,7 @@ function_imports: std.AutoArrayHashMapUnmanaged(String, FunctionImportId) = .emp |
| 214 | 214 | globals: std.AutoArrayHashMapUnmanaged(GlobalImport.Resolution, void) = .empty, |
| 215 | 215 | /// Tracks the value at the end of prelink, at which point `globals` |
| 216 | 216 | /// contains only object file globals, and nothing from the Zcu yet. |
| 217 | | globals_len: u32 = 0, |
| 217 | globals_end_prelink: u32 = 0, |
| 218 | 218 | global_imports_init_keys: []String = &.{}, |
| 219 | 219 | global_imports_init_vals: []GlobalImportId = &.{}, |
| 220 | 220 | global_imports: std.AutoArrayHashMapUnmanaged(String, GlobalImportId) = .empty, |
| ... | ... | @@ -620,6 +620,17 @@ pub const ZcuFunc = extern struct { |
| 620 | 620 | const nav = ip.getNav(func.owner_nav); |
| 621 | 621 | return nav.fqn.toSlice(ip); |
| 622 | 622 | } |
| 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 | } |
| 623 | 634 | }; |
| 624 | 635 | }; |
| 625 | 636 | |
| ... | ... | @@ -730,7 +741,7 @@ pub const FunctionImport = extern struct { |
| 730 | 741 | }; |
| 731 | 742 | } |
| 732 | 743 | |
| 733 | | pub fn typeIndex(r: Resolution, wasm: *const Wasm) FunctionType.Index { |
| 744 | pub fn typeIndex(r: Resolution, wasm: *Wasm) FunctionType.Index { |
| 734 | 745 | return switch (unpack(r, wasm)) { |
| 735 | 746 | .unresolved => unreachable, |
| 736 | 747 | .__wasm_apply_global_tls_relocs => @panic("TODO"), |
| ... | ... | @@ -739,7 +750,7 @@ pub const FunctionImport = extern struct { |
| 739 | 750 | .__wasm_init_tls => @panic("TODO"), |
| 740 | 751 | .__zig_error_names => @panic("TODO"), |
| 741 | 752 | .object_function => |i| i.ptr(wasm).type_index, |
| 742 | | .zcu_func => @panic("TODO"), |
| 753 | .zcu_func => |i| i.typeIndex(wasm).?, |
| 743 | 754 | }; |
| 744 | 755 | } |
| 745 | 756 | |
| ... | ... | @@ -2247,7 +2258,7 @@ pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!v |
| 2247 | 2258 | try markFunction(wasm, name, import, @enumFromInt(i)); |
| 2248 | 2259 | } |
| 2249 | 2260 | } |
| 2250 | | wasm.functions_len = @intCast(wasm.functions.entries.len); |
| 2261 | wasm.functions_end_prelink = @intCast(wasm.functions.entries.len); |
| 2251 | 2262 | wasm.function_imports_init_keys = try gpa.dupe(String, wasm.function_imports.keys()); |
| 2252 | 2263 | wasm.function_imports_init_vals = try gpa.dupe(FunctionImportId, wasm.function_imports.values()); |
| 2253 | 2264 | 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 |
| 2257 | 2268 | try markGlobal(wasm, name, import, @enumFromInt(i)); |
| 2258 | 2269 | } |
| 2259 | 2270 | } |
| 2260 | | wasm.globals_len = @intCast(wasm.globals.entries.len); |
| 2271 | wasm.globals_end_prelink = @intCast(wasm.globals.entries.len); |
| 2261 | 2272 | wasm.global_imports_init_keys = try gpa.dupe(String, wasm.global_imports.keys()); |
| 2262 | 2273 | wasm.global_imports_init_vals = try gpa.dupe(GlobalImportId, wasm.global_imports.values()); |
| 2263 | 2274 | wasm.global_exports_len = @intCast(wasm.global_exports.items.len); |
| ... | ... | @@ -2451,7 +2462,14 @@ pub fn flushModule( |
| 2451 | 2462 | const sub_prog_node = prog_node.start("Wasm Flush", 0); |
| 2452 | 2463 | defer sub_prog_node.end(); |
| 2453 | 2464 | |
| 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 | |
| 2454 | 2471 | wasm.flush_buffer.clear(); |
| 2472 | |
| 2455 | 2473 | return wasm.flush_buffer.finish(wasm) catch |err| switch (err) { |
| 2456 | 2474 | error.OutOfMemory => return error.OutOfMemory, |
| 2457 | 2475 | error.LinkFailure => return error.LinkFailure, |
| ... | ... | @@ -2932,7 +2950,7 @@ pub fn addFuncType(wasm: *Wasm, ft: FunctionType) Allocator.Error!FunctionType.I |
| 2932 | 2950 | return @enumFromInt(gop.index); |
| 2933 | 2951 | } |
| 2934 | 2952 | |
| 2935 | | pub fn getExistingFuncType(wasm: *Wasm, ft: FunctionType) ?FunctionType.Index { |
| 2953 | pub fn getExistingFuncType(wasm: *const Wasm, ft: FunctionType) ?FunctionType.Index { |
| 2936 | 2954 | const index = wasm.func_types.getIndex(ft) orelse return null; |
| 2937 | 2955 | return @enumFromInt(index); |
| 2938 | 2956 | } |
| ... | ... | @@ -2944,7 +2962,7 @@ pub fn internFunctionType( |
| 2944 | 2962 | return_type: ZcuType, |
| 2945 | 2963 | target: *const std.Target, |
| 2946 | 2964 | ) 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); |
| 2948 | 2966 | return wasm.addFuncType(.{ |
| 2949 | 2967 | .params = try wasm.internValtypeList(wasm.params_scratch.items), |
| 2950 | 2968 | .returns = try wasm.internValtypeList(wasm.returns_scratch.items), |
| ... | ... | @@ -2958,7 +2976,7 @@ pub fn getExistingFunctionType( |
| 2958 | 2976 | return_type: ZcuType, |
| 2959 | 2977 | target: *const std.Target, |
| 2960 | 2978 | ) ?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) { |
| 2962 | 2980 | error.OutOfMemory => return null, |
| 2963 | 2981 | }; |
| 2964 | 2982 | return wasm.getExistingFuncType(.{ |
| ... | ... | @@ -3008,7 +3026,7 @@ pub fn navSymbolIndex(wasm: *Wasm, nav_index: InternPool.Nav.Index) Allocator.Er |
| 3008 | 3026 | } |
| 3009 | 3027 | |
| 3010 | 3028 | fn convertZcuFnType( |
| 3011 | | wasm: *Wasm, |
| 3029 | comp: *Compilation, |
| 3012 | 3030 | cc: std.builtin.CallingConvention, |
| 3013 | 3031 | params: []const InternPool.Index, |
| 3014 | 3032 | return_type: ZcuType, |
| ... | ... | @@ -3019,7 +3037,6 @@ fn convertZcuFnType( |
| 3019 | 3037 | params_buffer.clearRetainingCapacity(); |
| 3020 | 3038 | returns_buffer.clearRetainingCapacity(); |
| 3021 | 3039 | |
| 3022 | | const comp = wasm.base.comp; |
| 3023 | 3040 | const gpa = comp.gpa; |
| 3024 | 3041 | const zcu = comp.zcu.?; |
| 3025 | 3042 | |