| ... | @@ -496,8 +496,6 @@ pub fn allocateDeclIndexes(self: *Wasm, decl: *Module.Decl) !void { | ... | @@ -496,8 +496,6 @@ pub fn allocateDeclIndexes(self: *Wasm, decl: *Module.Decl) !void { |
| 496 | atom.sym_index = @intCast(u32, self.symbols.items.len); | 496 | atom.sym_index = @intCast(u32, self.symbols.items.len); |
| 497 | self.symbols.appendAssumeCapacity(symbol); | 497 | self.symbols.appendAssumeCapacity(symbol); |
| 498 | } | 498 | } |
| 499 | | | |
| 500 | try self.resolved_symbols.putNoClobber(self.base.allocator, atom.symbolLoc(), {}); | | |
| 501 | try self.symbol_atom.putNoClobber(self.base.allocator, atom.symbolLoc(), atom); | 499 | try self.symbol_atom.putNoClobber(self.base.allocator, atom.symbolLoc(), atom); |
| 502 | } | 500 | } |
| 503 | | 501 | |
| ... | @@ -552,7 +550,7 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { | ... | @@ -552,7 +550,7 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 552 | decl.link.wasm.clear(); | 550 | decl.link.wasm.clear(); |
| 553 | | 551 | |
| 554 | if (decl.isExtern()) { | 552 | if (decl.isExtern()) { |
| 555 | return self.addOrUpdateImport(decl); | 553 | return; |
| 556 | } | 554 | } |
| 557 | | 555 | |
| 558 | if (decl.val.castTag(.function)) |_| { | 556 | if (decl.val.castTag(.function)) |_| { |
| ... | @@ -588,10 +586,6 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { | ... | @@ -588,10 +586,6 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 588 | } | 586 | } |
| 589 | | 587 | |
| 590 | fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void { | 588 | fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void { |
| 591 | if (decl.isExtern()) { | | |
| 592 | return self.addOrUpdateImport(decl); | | |
| 593 | } | | |
| 594 | | | |
| 595 | if (code.len == 0) return; | 589 | if (code.len == 0) return; |
| 596 | const atom: *Atom = &decl.link.wasm; | 590 | const atom: *Atom = &decl.link.wasm; |
| 597 | atom.size = @intCast(u32, code.len); | 591 | atom.size = @intCast(u32, code.len); |
| ... | @@ -602,6 +596,8 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void { | ... | @@ -602,6 +596,8 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void { |
| 602 | defer self.base.allocator.free(full_name); | 596 | defer self.base.allocator.free(full_name); |
| 603 | symbol.name = try self.string_table.put(self.base.allocator, full_name); | 597 | symbol.name = try self.string_table.put(self.base.allocator, full_name); |
| 604 | try atom.code.appendSlice(self.base.allocator, code); | 598 | try atom.code.appendSlice(self.base.allocator, code); |
| | 599 | |
| | 600 | try self.resolved_symbols.put(self.base.allocator, atom.symbolLoc(), {}); |
| 605 | } | 601 | } |
| 606 | | 602 | |
| 607 | /// Lowers a constant typed value to a local symbol and atom. | 603 | /// Lowers a constant typed value to a local symbol and atom. |
| ... | @@ -831,10 +827,10 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { | ... | @@ -831,10 +827,10 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 831 | } | 827 | } |
| 832 | | 828 | |
| 833 | if (decl.isExtern()) { | 829 | if (decl.isExtern()) { |
| 834 | assert(self.imports.remove(atom.symbolLoc())); | 830 | _ = self.imports.remove(atom.symbolLoc()); |
| 835 | } | 831 | } |
| 836 | assert(self.resolved_symbols.swapRemove(atom.symbolLoc())); | 832 | _ = self.resolved_symbols.swapRemove(atom.symbolLoc()); |
| 837 | _ = self.symbol_atom.remove(atom.symbolLoc()); // not all decl's exist in symbol_atom | 833 | _ = self.symbol_atom.remove(atom.symbolLoc()); |
| 838 | atom.deinit(self.base.allocator); | 834 | atom.deinit(self.base.allocator); |
| 839 | } | 835 | } |
| 840 | | 836 | |
| ... | @@ -855,19 +851,19 @@ fn mapFunctionTable(self: *Wasm) void { | ... | @@ -855,19 +851,19 @@ fn mapFunctionTable(self: *Wasm) void { |
| 855 | } | 851 | } |
| 856 | } | 852 | } |
| 857 | | 853 | |
| 858 | fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void { | 854 | pub fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void { |
| 859 | // For the import name itself, we use the decl's name, rather than the fully qualified name | 855 | // For the import name itself, we use the decl's name, rather than the fully qualified name |
| 860 | const decl_name_index = try self.string_table.put(self.base.allocator, mem.sliceTo(decl.name, 0)); | 856 | const decl_name_index = try self.string_table.put(self.base.allocator, mem.sliceTo(decl.name, 0)); |
| 861 | const symbol_index = decl.link.wasm.sym_index; | 857 | const symbol_index = decl.link.wasm.sym_index; |
| 862 | const symbol: *Symbol = &self.symbols.items[symbol_index]; | 858 | const symbol: *Symbol = &self.symbols.items[symbol_index]; |
| 863 | symbol.setUndefined(true); | 859 | symbol.setUndefined(true); |
| 864 | symbol.setGlobal(true); | 860 | symbol.setGlobal(true); |
| 865 | try self.globals.putNoClobber( | 861 | const global_gop = try self.globals.getOrPut(self.base.allocator, decl_name_index); |
| 866 | self.base.allocator, | 862 | if (!global_gop.found_existing) { |
| 867 | decl_name_index, | 863 | const loc: SymbolLoc = .{ .file = null, .index = symbol_index }; |
| 868 | .{ .file = null, .index = symbol_index }, | 864 | global_gop.value_ptr.* = loc; |
| 869 | ); | 865 | try self.resolved_symbols.put(self.base.allocator, loc, {}); |
| 870 | try self.resolved_symbols.put(self.base.allocator, .{ .file = null, .index = symbol_index }, {}); | 866 | } |
| 871 | | 867 | |
| 872 | switch (decl.ty.zigTypeTag()) { | 868 | switch (decl.ty.zigTypeTag()) { |
| 873 | .Fn => { | 869 | .Fn => { |