| ... | ... | @@ -13,7 +13,9 @@ decls_map: std.AutoHashMapUnmanaged(InternPool.DeclIndex, DeclInfo) = .{}, |
| 13 | 13 | func_types: std.ArrayListUnmanaged(std.wasm.Type) = .{}, |
| 14 | 14 | /// List of `std.wasm.Func`. Each entry contains the function signature, |
| 15 | 15 | /// rather than the actual body. |
| 16 | | functions: std.AutoHashMapUnmanaged(u32, std.wasm.Func) = .{}, |
| 16 | functions: std.ArrayListUnmanaged(std.wasm.Func) = .{}, |
| 17 | /// List of indexes pointing to an entry within the `functions` list which has been removed. |
| 18 | functions_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 17 | 19 | /// Map of symbol locations, represented by its `types.Import`. |
| 18 | 20 | imports: std.AutoHashMapUnmanaged(u32, types.Import) = .{}, |
| 19 | 21 | /// List of WebAssembly globals. |
| ... | ... | @@ -320,11 +322,7 @@ fn finishUpdateDecl( |
| 320 | 322 | |
| 321 | 323 | switch (decl.ty.zigTypeTag(mod)) { |
| 322 | 324 | .Fn => { |
| 323 | | try zig_object.functions.put( |
| 324 | | gpa, |
| 325 | | atom.sym_index, |
| 326 | | .{ .type_index = zig_object.atom_types.get(atom_index).? }, |
| 327 | | ); |
| 325 | sym.index = try zig_object.appendFunction(gpa, .{ .type_index = zig_object.atom_types.get(atom_index).? }); |
| 328 | 326 | sym.tag = .function; |
| 329 | 327 | }, |
| 330 | 328 | else => { |
| ... | ... | @@ -689,6 +687,9 @@ pub fn addOrUpdateImport( |
| 689 | 687 | }; |
| 690 | 688 | zig_object.imported_functions_count += 1; |
| 691 | 689 | } |
| 690 | sym.tag = .function; |
| 691 | } else { |
| 692 | sym.tag = .data; |
| 692 | 693 | } |
| 693 | 694 | } |
| 694 | 695 | |
| ... | ... | @@ -821,10 +822,6 @@ pub fn deleteDeclExport( |
| 821 | 822 | std.debug.assert(zig_object.global_syms.remove(sym.name)); |
| 822 | 823 | std.debug.assert(wasm_file.symbol_atom.remove(.{ .file = zig_object.index, .index = sym_index })); |
| 823 | 824 | zig_object.symbols_free_list.append(wasm_file.base.comp.gpa, sym_index) catch {}; |
| 824 | | |
| 825 | | if (sym.tag == .function) { |
| 826 | | std.debug.assert(zig_object.functions.remove(sym_index)); |
| 827 | | } |
| 828 | 825 | sym.tag = .dead; |
| 829 | 826 | } |
| 830 | 827 | } |
| ... | ... | @@ -867,17 +864,6 @@ pub fn updateExports( |
| 867 | 864 | else index: { |
| 868 | 865 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 869 | 866 | try decl_info.appendExport(gpa, sym_index); |
| 870 | | |
| 871 | | // For functions, we also need to put the alias in the function section. |
| 872 | | // We simply copy the aliased function. |
| 873 | | // The final linakge will deduplicate these functions. |
| 874 | | if (decl.ty.zigTypeTag(mod) == .Fn) { |
| 875 | | try zig_object.functions.putNoClobber( |
| 876 | | gpa, |
| 877 | | sym_index, |
| 878 | | zig_object.functions.get(atom.sym_index).?, |
| 879 | | ); |
| 880 | | } |
| 881 | 867 | break :index sym_index; |
| 882 | 868 | }; |
| 883 | 869 | |
| ... | ... | @@ -969,7 +955,7 @@ pub fn freeDecl(zig_object: *ZigObject, wasm_file: *Wasm, decl_index: InternPool |
| 969 | 955 | } |
| 970 | 956 | switch (decl.ty.zigTypeTag(mod)) { |
| 971 | 957 | .Fn => { |
| 972 | | std.debug.assert(zig_object.functions.remove(atom.sym_index)); |
| 958 | zig_object.functions_free_list.append(gpa, sym.index) catch {}; |
| 973 | 959 | std.debug.assert(zig_object.atom_types.remove(atom_index)); |
| 974 | 960 | }, |
| 975 | 961 | else => { |
| ... | ... | @@ -1221,7 +1207,7 @@ pub fn createFunction( |
| 1221 | 1207 | sym.tag = .function; |
| 1222 | 1208 | sym.name = try zig_object.string_table.insert(gpa, symbol_name); |
| 1223 | 1209 | const type_index = try zig_object.putOrGetFuncType(gpa, func_ty); |
| 1224 | | try zig_object.functions.putNoClobber(gpa, sym_index, .{ .type_index = type_index }); |
| 1210 | sym.index = try zig_object.appendFunction(gpa, .{ .type_index = type_index }); |
| 1225 | 1211 | |
| 1226 | 1212 | const atom_index = try wasm_file.createAtom(sym_index, zig_object.index); |
| 1227 | 1213 | const atom = wasm_file.getAtomPtr(atom_index); |
| ... | ... | @@ -1232,6 +1218,20 @@ pub fn createFunction( |
| 1232 | 1218 | return sym_index; |
| 1233 | 1219 | } |
| 1234 | 1220 | |
| 1221 | /// Appends a new `std.wasm.Func` to the list of functions and returns its index. |
| 1222 | fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm.Func) !u32 { |
| 1223 | const index: u32 = if (zig_object.functions_free_list.popOrNull()) |idx| |
| 1224 | idx |
| 1225 | else idx: { |
| 1226 | const len: u32 = @intCast(zig_object.functions.items.len); |
| 1227 | _ = try zig_object.functions.addOne(gpa); |
| 1228 | break :idx len; |
| 1229 | }; |
| 1230 | zig_object.functions.items[index] = func; |
| 1231 | |
| 1232 | return index; |
| 1233 | } |
| 1234 | |
| 1235 | 1235 | const build_options = @import("build_options"); |
| 1236 | 1236 | const builtin = @import("builtin"); |
| 1237 | 1237 | const codegen = @import("../../codegen.zig"); |