diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index a3704789e2..4a5d9323a1 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -503,7 +503,7 @@ pub fn updateFunc(self: *Wasm, module: *Module, func: *Module.Fn, air: Air, live const decl = func.owner_decl; assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() - decl.link.wasm.clear(); + decl.link.wasm.clear(self.base.allocator); var codegen_: CodeGen = .{ .gpa = self.base.allocator, @@ -544,7 +544,7 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() - decl.link.wasm.clear(); + decl.link.wasm.clear(self.base.allocator); if (decl.isExtern()) { return self.addOrUpdateImport(decl); @@ -607,7 +607,9 @@ pub fn lowerUnnamedConst(self: *Wasm, decl: *Module.Decl, tv: TypedValue) !u32 { // Create and initialize a new local symbol and atom const local_index = decl.link.wasm.locals.items.len; - const name = try std.fmt.allocPrintZ(self.base.allocator, "__unnamed_{s}_{d}", .{ decl.name, local_index }); + const fqdn = try decl.getFullyQualifiedName(self.base.allocator); + defer self.base.allocator.free(fqdn); + const name = try std.fmt.allocPrintZ(self.base.allocator, "__unnamed_{s}_{d}", .{ fqdn, local_index }); defer self.base.allocator.free(name); var symbol: Symbol = .{ .name = try self.string_table.put(self.base.allocator, name), @@ -636,27 +638,25 @@ pub fn lowerUnnamedConst(self: *Wasm, decl: *Module.Decl, tv: TypedValue) !u32 { defer value_bytes.deinit(); const module = self.base.options.module.?; - var decl_gen: CodeGen.DeclGen = .{ - .bin_file = self, - .decl = decl, - .err_msg = undefined, - .gpa = self.base.allocator, - .module = module, - .code = &value_bytes, - .symbol_index = atom.sym_index, - }; - - const result = decl_gen.genTypedValue(tv.ty, tv.val) catch |err| switch (err) { - error.CodegenFail => { + const result = try codegen.generateSymbol( + &self.base, + decl.srcLoc(), + tv, + &value_bytes, + .none, + .{ + .parent_atom_index = atom.sym_index, + .addend = null, + }, + ); + const code = switch (result) { + .externally_managed => |x| x, + .appended => value_bytes.items, + .fail => |em| { decl.analysis = .codegen_failure; - try module.failed_decls.put(module.gpa, decl, decl_gen.err_msg); + try module.failed_decls.put(module.gpa, decl, em); return error.AnalysisFail; }, - else => |e| return e, - }; - const code = switch (result) { - .appended => value_bytes.items, - .externally_managed => |data| data, }; atom.size = @intCast(u32, code.len); @@ -989,6 +989,7 @@ fn allocateAtoms(self: *Wasm) !void { atom.size, }); offset += atom.size; + self.symbol_atom.putAssumeCapacity(atom.symbolLoc(), atom); // Update atom pointers atom = atom.next orelse break; } } diff --git a/src/link/Wasm/Atom.zig b/src/link/Wasm/Atom.zig index a3e1c25190..e89ed37123 100644 --- a/src/link/Wasm/Atom.zig +++ b/src/link/Wasm/Atom.zig @@ -62,9 +62,14 @@ pub fn deinit(self: *Atom, gpa: Allocator) void { /// Sets the length of relocations and code to '0', /// effectively resetting them and allowing them to be re-populated. -pub fn clear(self: *Atom) void { +pub fn clear(self: *Atom, gpa: Allocator) void { self.relocs.clearRetainingCapacity(); self.code.clearRetainingCapacity(); + + // locals will be re-generated + for (self.locals.items) |*local| { + local.deinit(gpa); + } } pub fn format(self: Atom, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { diff --git a/test/behavior/align.zig b/test/behavior/align.zig index a0f05e5c1c..429169a3fe 100644 --- a/test/behavior/align.zig +++ b/test/behavior/align.zig @@ -149,6 +149,7 @@ test "return error union with 128-bit integer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; try expect(3 == try give()); } diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index f68a34d341..a3138ff88a 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -1142,6 +1142,7 @@ test "cast u128 to f128 and back" { if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO comptime try testCast128(); try testCast128(); diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 32e374d3d5..f23d67a580 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -856,6 +856,7 @@ test "non-packed struct with u128 entry in union" { if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO const U = union(enum) { Num: u128,