Commit 5944e89016

Jakub Konka <kubkon@jakubkonka.com>
2022-02-05 15:55:17
stage2: lower unnamed constants in Elf and MachO
* link: add a virtual function `lowerUnnamedConsts`, similar to `updateFunc` or `updateDecl` which needs to be implemented by the linker backend in order to be used with the `CodeGen` code * elf: implement `lowerUnnamedConsts` specialization where we lower unnamed constants to `.rodata` section. We keep track of the atoms encompassing the lowered unnamed consts in a global table indexed by parent `Decl`. When the `Decl` is updated or destroyed, we clear the unnamed consts referenced within the `Decl`. * macho: implement `lowerUnnamedConsts` specialization where we lower unnamed constants to `__TEXT,__const` section. We keep track of the atoms encompassing the lowered unnamed consts in a global table indexed by parent `Decl`. When the `Decl` is updated or destroyed, we clear the unnamed consts referenced within the `Decl`. * x64: change `MCValue.linker_sym_index` into two `MCValue`s: `.got_load` and `.direct_load`. The former signifies to the emitter that it should emit a GOT load relocation, while the latter that it should emit a direct load (`SIGNED`) relocation. * x64: lower `struct` instantiations
1 parent 2113538
src/arch/x86_64/CodeGen.zig
@@ -118,10 +118,14 @@ pub const MCValue = union(enum) {
     /// The value is in memory at a hard-coded address.
     /// If the type is a pointer, it means the pointer address is at this memory location.
     memory: u64,
-    /// The value is in memory but not allocated an address yet by the linker, so we store
-    /// the symbol index instead.
-    /// If the type is a pointer, it means the pointer is the symbol.
-    linker_sym_index: u32,
+    /// The value is in memory referenced indirectly via a GOT entry index.
+    /// If the type is a pointer, it means the pointer is referenced indirectly via GOT.
+    /// When lowered, linker will emit a relocation of type X86_64_RELOC_GOT.
+    got_load: u32,
+    /// The value is in memory referenced directly via symbol index.
+    /// If the type is a pointer, it means the pointer is referenced directly via symbol index.
+    /// When lowered, linker will emit a relocation of type X86_64_RELOC_SIGNED.
+    direct_load: u32,
     /// The value is one of the stack variables.
     /// If the type is a pointer, it means the pointer address is in the stack at this offset.
     stack_offset: i32,
@@ -1691,7 +1695,8 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
             }
         },
         .memory,
-        .linker_sym_index,
+        .got_load,
+        .direct_load,
         => {
             const reg = try self.copyToTmpRegister(ptr_ty, ptr);
             try self.load(dst_mcv, .{ .register = reg }, ptr_ty);
@@ -1823,7 +1828,8 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
                 },
             }
         },
-        .linker_sym_index,
+        .got_load,
+        .direct_load,
         .memory,
         => {
             value.freezeIfRegister(&self.register_manager);
@@ -1831,15 +1837,22 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
 
             const addr_reg: Register = blk: {
                 switch (ptr) {
-                    .linker_sym_index => |sym_index| {
+                    .got_load,
+                    .direct_load,
+                    => |sym_index| {
+                        const flags: u2 = switch (ptr) {
+                            .got_load => 0b00,
+                            .direct_load => 0b01,
+                            else => unreachable,
+                        };
                         const addr_reg = try self.register_manager.allocReg(null);
                         _ = try self.addInst(.{
-                            .tag = .lea,
+                            .tag = .lea_pie,
                             .ops = (Mir.Ops{
                                 .reg1 = addr_reg.to64(),
-                                .flags = 0b10,
+                                .flags = flags,
                             }).encode(),
-                            .data = .{ .got_entry = sym_index },
+                            .data = .{ .linker_sym_index = sym_index },
                         });
                         break :blk addr_reg;
                     },
@@ -2160,7 +2173,7 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
                 .embedded_in_code, .memory => {
                     return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{});
                 },
-                .linker_sym_index => {
+                .got_load, .direct_load => {
                     return self.fail("TODO implement x86 ADD/SUB/CMP source symbol at index in linker", .{});
                 },
                 .stack_offset => |off| {
@@ -2247,7 +2260,7 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
                 .embedded_in_code, .memory, .stack_offset => {
                     return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{});
                 },
-                .linker_sym_index => {
+                .got_load, .direct_load => {
                     return self.fail("TODO implement x86 ADD/SUB/CMP source symbol at index in linker", .{});
                 },
                 .compare_flags_unsigned => {
@@ -2261,7 +2274,7 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
         .embedded_in_code, .memory => {
             return self.fail("TODO implement x86 ADD/SUB/CMP destination memory", .{});
         },
-        .linker_sym_index => {
+        .got_load, .direct_load => {
             return self.fail("TODO implement x86 ADD/SUB/CMP destination symbol at index", .{});
         },
     }
@@ -2317,7 +2330,7 @@ fn genIMulOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !
                 .embedded_in_code, .memory, .stack_offset => {
                     return self.fail("TODO implement x86 multiply source memory", .{});
                 },
-                .linker_sym_index => {
+                .got_load, .direct_load => {
                     return self.fail("TODO implement x86 multiply source symbol at index in linker", .{});
                 },
                 .compare_flags_unsigned => {
@@ -2358,7 +2371,7 @@ fn genIMulOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !
                 .embedded_in_code, .memory, .stack_offset => {
                     return self.fail("TODO implement x86 multiply source memory", .{});
                 },
-                .linker_sym_index => {
+                .got_load, .direct_load => {
                     return self.fail("TODO implement x86 multiply source symbol at index in linker", .{});
                 },
                 .compare_flags_unsigned => {
@@ -2372,7 +2385,7 @@ fn genIMulOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !
         .embedded_in_code, .memory => {
             return self.fail("TODO implement x86 multiply destination memory", .{});
         },
-        .linker_sym_index => {
+        .got_load, .direct_load => {
             return self.fail("TODO implement x86 multiply destination symbol at index in linker", .{});
         },
     }
@@ -2478,7 +2491,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
             .dead => unreachable,
             .embedded_in_code => unreachable,
             .memory => unreachable,
-            .linker_sym_index => unreachable,
+            .got_load => unreachable,
+            .direct_load => unreachable,
             .compare_flags_signed => unreachable,
             .compare_flags_unsigned => unreachable,
         }
@@ -2540,7 +2554,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
             if (func_value.castTag(.function)) |func_payload| {
                 const func = func_payload.data;
                 try self.genSetReg(Type.initTag(.usize), .rax, .{
-                    .linker_sym_index = func.owner_decl.link.macho.local_sym_index,
+                    .got_load = func.owner_decl.link.macho.local_sym_index,
                 });
                 // callq *%rax
                 _ = try self.addInst(.{
@@ -3576,7 +3590,8 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue) InnerErro
         },
         .memory,
         .embedded_in_code,
-        .linker_sym_index,
+        .got_load,
+        .direct_load,
         => {
             if (ty.abiSize(self.target.*) <= 8) {
                 const reg = try self.copyToTmpRegister(ty, mcv);
@@ -3982,14 +3997,21 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
                 .data = undefined,
             });
         },
-        .linker_sym_index => |sym_index| {
+        .got_load,
+        .direct_load,
+        => |sym_index| {
+            const flags: u2 = switch (mcv) {
+                .got_load => 0b00,
+                .direct_load => 0b01,
+                else => unreachable,
+            };
             _ = try self.addInst(.{
-                .tag = .lea,
+                .tag = .lea_pie,
                 .ops = (Mir.Ops{
                     .reg1 = reg,
-                    .flags = 0b10,
+                    .flags = flags,
                 }).encode(),
-                .data = .{ .got_entry = sym_index },
+                .data = .{ .linker_sym_index = sym_index },
             });
             // MOV reg, [reg]
             _ = try self.addInst(.{
@@ -4316,7 +4338,7 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa
     } else if (self.bin_file.cast(link.File.MachO)) |_| {
         // Because MachO is PIE-always-on, we defer memory address resolution until
         // the linker has enough info to perform relocations.
-        return MCValue{ .linker_sym_index = decl.link.macho.local_sym_index };
+        return MCValue{ .got_load = decl.link.macho.local_sym_index };
     } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
         const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
         return MCValue{ .memory = got_addr };
@@ -4331,6 +4353,24 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa
     _ = tv;
 }
 
+fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
+    const local_sym_index = self.bin_file.lowerUnnamedConst(tv, self.mod_fn.owner_decl) catch |err| {
+        return self.fail("lowering unnamed constant failed: {s}", .{@errorName(err)});
+    };
+    if (self.bin_file.cast(link.File.Elf)) |elf_file| {
+        const vaddr = elf_file.local_symbols.items[local_sym_index].st_value;
+        return MCValue{ .memory = vaddr };
+    } else if (self.bin_file.cast(link.File.MachO)) |_| {
+        return MCValue{ .direct_load = local_sym_index };
+    } else if (self.bin_file.cast(link.File.Coff)) |_| {
+        return self.fail("TODO lower unnamed const in COFF", .{});
+    } else if (self.bin_file.cast(link.File.Plan9)) |_| {
+        return self.fail("TODO lower unnamed const in Plan9", .{});
+    } else {
+        return self.fail("TODO lower unnamed const", .{});
+    }
+}
+
 fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
     if (typed_value.val.isUndef())
         return MCValue{ .undef = {} };
@@ -4446,6 +4486,9 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
 
             return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty});
         },
+        .Struct => {
+            return self.lowerUnnamedConst(typed_value);
+        },
         else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}),
     }
 }
src/arch/x86_64/Emit.zig
@@ -131,6 +131,7 @@ pub fn lowerMir(emit: *Emit) InnerError!void {
             .movabs => try emit.mirMovabs(inst),
 
             .lea => try emit.mirLea(inst),
+            .lea_pie => try emit.mirLeaPie(inst),
 
             .imul_complex => try emit.mirIMulComplex(inst),
 
@@ -706,36 +707,6 @@ fn mirLea(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
             mem.writeIntLittle(i32, emit.code.items[end_offset - 4 ..][0..4], disp);
         },
         0b10 => {
-            // lea reg1, [rip + reloc]
-            // RM
-            try lowerToRmEnc(
-                .lea,
-                ops.reg1,
-                RegisterOrMemory.rip(Memory.PtrSize.fromBits(ops.reg1.size()), 0),
-                emit.code,
-            );
-            const end_offset = emit.code.items.len;
-            const got_entry = emit.mir.instructions.items(.data)[inst].got_entry;
-            if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
-                // TODO I think the reloc might be in the wrong place.
-                const decl = macho_file.active_decl.?;
-                try decl.link.macho.relocs.append(emit.bin_file.allocator, .{
-                    .offset = @intCast(u32, end_offset - 4),
-                    .target = .{ .local = got_entry },
-                    .addend = 0,
-                    .subtractor = null,
-                    .pcrel = true,
-                    .length = 2,
-                    .@"type" = @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_GOT),
-                });
-            } else {
-                return emit.fail(
-                    "TODO implement lea reg, [rip + reloc] for linking backends different than MachO",
-                    .{},
-                );
-            }
-        },
-        0b11 => {
             // lea reg, [rbp + rcx + imm32]
             const imm = emit.mir.instructions.items(.data)[inst].imm;
             const src_reg: ?Register = if (ops.reg2 == .none) null else ops.reg2;
@@ -754,6 +725,46 @@ fn mirLea(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
                 emit.code,
             );
         },
+        0b11 => return emit.fail("TODO unused LEA variant 0b11", .{}),
+    }
+}
+
+fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
+    const tag = emit.mir.instructions.items(.tag)[inst];
+    assert(tag == .lea_pie);
+    const ops = Mir.Ops.decode(emit.mir.instructions.items(.ops)[inst]);
+
+    // lea reg1, [rip + reloc]
+    // RM
+    try lowerToRmEnc(
+        .lea,
+        ops.reg1,
+        RegisterOrMemory.rip(Memory.PtrSize.fromBits(ops.reg1.size()), 0),
+        emit.code,
+    );
+    const end_offset = emit.code.items.len;
+    const reloc_type = switch (ops.flags) {
+        0b00 => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_GOT),
+        0b01 => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_SIGNED),
+        else => return emit.fail("TODO unused LEA PIE variants 0b10 and 0b11", .{}),
+    };
+    const sym_index = emit.mir.instructions.items(.data)[inst].linker_sym_index;
+    if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
+        const decl = macho_file.active_decl.?;
+        try decl.link.macho.relocs.append(emit.bin_file.allocator, .{
+            .offset = @intCast(u32, end_offset - 4),
+            .target = .{ .local = sym_index },
+            .addend = 0,
+            .subtractor = null,
+            .pcrel = true,
+            .length = 2,
+            .@"type" = reloc_type,
+        });
+    } else {
+        return emit.fail(
+            "TODO implement lea reg, [rip + reloc] for linking backends different than MachO",
+            .{},
+        );
     }
 }
 
src/arch/x86_64/Mir.zig
@@ -202,13 +202,16 @@ pub const Inst = struct {
         ///      0b00  reg1, [reg2 + imm32]
         ///      0b00  reg1, [ds:imm32]
         ///      0b01  reg1, [rip + imm32]
-        ///      0b10  reg1, [rip + reloc]
-        ///      0b11  reg1, [reg2 + rcx + imm32]
-        /// Notes:
-        /// * if flags are 0b10, `Data` contains `got_entry` for the linker to generate
-        /// a valid relocation for.
+        ///      0b10  reg1, [reg2 + rcx + imm32]
         lea,
 
+        /// ops flags: form:
+        ///      0b00  reg1, [rip + reloc] // via GOT emits X86_64_RELOC_GOT relocation
+        ///      0b01  reg1, [rip + reloc] // direct load emits X86_64_RELOC_SIGNED relocation
+        /// Notes:
+        /// * `Data` contains `linker_sym_index` 
+        lea_pie,
+
         /// ops flags: form:
         ///      0bX0  reg1
         ///      0bX1  [reg1 + imm32]
@@ -342,8 +345,8 @@ pub const Inst = struct {
         /// An extern function.
         /// Index into the linker's string table.
         extern_fn: u32,
-        /// Entry in the GOT table by index.
-        got_entry: u32,
+        /// Entry in the linker's symbol table.
+        linker_sym_index: u32,
         /// Index into `extra`. Meaning of what can be found there is context-dependent.
         payload: u32,
     };
src/arch/x86_64/PrintMir.zig
@@ -119,6 +119,7 @@ pub fn printMir(print: *const Print, w: anytype, mir_to_air_map: std.AutoHashMap
             .movabs => try print.mirMovabs(inst, w),
 
             .lea => try print.mirLea(inst, w),
+            .lea_pie => try print.mirLeaPie(inst, w),
 
             .imul_complex => try print.mirIMulComplex(inst, w),
 
@@ -412,7 +413,7 @@ fn mirLea(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void {
             } else {
                 try w.print("ds:", .{});
             }
-            try w.print("{d}]\n", .{imm});
+            try w.print("{d}]", .{imm});
         },
         0b01 => {
             try w.print("{s}, ", .{@tagName(ops.reg1)});
@@ -429,6 +430,7 @@ fn mirLea(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void {
             try w.print("target@{x}", .{imm});
         },
         0b10 => {
+            const imm = print.mir.instructions.items(.data)[inst].imm;
             try w.print("{s}, ", .{@tagName(ops.reg1)});
             switch (ops.reg1.size()) {
                 8 => try w.print("byte ptr ", .{}),
@@ -437,23 +439,37 @@ fn mirLea(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void {
                 64 => try w.print("qword ptr ", .{}),
                 else => unreachable,
             }
-            try w.print("[rip + 0x0] ", .{});
-            const got_entry = print.mir.instructions.items(.data)[inst].got_entry;
-            if (print.bin_file.cast(link.File.MachO)) |macho_file| {
-                const target = macho_file.locals.items[got_entry];
-                const target_name = macho_file.getString(target.n_strx);
-                try w.print("target@{s}", .{target_name});
-            } else {
-                try w.writeAll("TODO lea reg, [rip + reloc] for linking backends different than MachO");
-            }
+            try w.print("[rbp + rcx + {d}]", .{imm});
         },
         0b11 => {
-            try w.writeAll("unused variant\n");
+            try w.writeAll("unused variant");
         },
     }
     try w.writeAll("\n");
 }
 
+fn mirLeaPie(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void {
+    const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]);
+    try w.print("lea {s}, ", .{@tagName(ops.reg1)});
+    switch (ops.reg1.size()) {
+        8 => try w.print("byte ptr ", .{}),
+        16 => try w.print("word ptr ", .{}),
+        32 => try w.print("dword ptr ", .{}),
+        64 => try w.print("qword ptr ", .{}),
+        else => unreachable,
+    }
+    try w.print("[rip + 0x0] ", .{});
+    const sym_index = print.mir.instructions.items(.data)[inst].linker_sym_index;
+    if (print.bin_file.cast(link.File.MachO)) |macho_file| {
+        const target = macho_file.locals.items[sym_index];
+        const target_name = macho_file.getString(target.n_strx);
+        try w.print("target@{s}", .{target_name});
+    } else {
+        try w.print("TODO lea PIE for other backends", .{});
+    }
+    return w.writeByte('\n');
+}
+
 fn mirCallExtern(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void {
     _ = print;
     _ = inst;
src/link/MachO/Atom.zig
@@ -545,28 +545,11 @@ fn addPtrBindingOrRebase(
 }
 
 fn addTlvPtrEntry(target: Relocation.Target, context: RelocContext) !void {
-    if (context.macho_file.tlv_ptr_entries_map.contains(target)) return;
-
-    const value_ptr = blk: {
-        if (context.macho_file.tlv_ptr_entries_map_free_list.popOrNull()) |i| {
-            log.debug("reusing __thread_ptrs entry index {d} for {}", .{ i, target });
-            context.macho_file.tlv_ptr_entries_map.keys()[i] = target;
-            const value_ptr = context.macho_file.tlv_ptr_entries_map.getPtr(target).?;
-            break :blk value_ptr;
-        } else {
-            const res = try context.macho_file.tlv_ptr_entries_map.getOrPut(
-                context.macho_file.base.allocator,
-                target,
-            );
-            log.debug("creating new __thread_ptrs entry at index {d} for {}", .{
-                context.macho_file.tlv_ptr_entries_map.getIndex(target).?,
-                target,
-            });
-            break :blk res.value_ptr;
-        }
-    };
+    if (context.macho_file.tlv_ptr_entries_table.contains(target)) return;
+
+    const index = try context.macho_file.allocateTlvPtrEntry(target);
     const atom = try context.macho_file.createTlvPtrAtom(target);
-    value_ptr.* = atom;
+    context.macho_file.tlv_ptr_entries.items[index].atom = atom;
 
     const match = (try context.macho_file.getMatchingSection(.{
         .segname = MachO.makeStaticString("__DATA"),
@@ -586,28 +569,11 @@ fn addTlvPtrEntry(target: Relocation.Target, context: RelocContext) !void {
 }
 
 fn addGotEntry(target: Relocation.Target, context: RelocContext) !void {
-    if (context.macho_file.got_entries_map.contains(target)) return;
-
-    const value_ptr = blk: {
-        if (context.macho_file.got_entries_map_free_list.popOrNull()) |i| {
-            log.debug("reusing GOT entry index {d} for {}", .{ i, target });
-            context.macho_file.got_entries_map.keys()[i] = target;
-            const value_ptr = context.macho_file.got_entries_map.getPtr(target).?;
-            break :blk value_ptr;
-        } else {
-            const res = try context.macho_file.got_entries_map.getOrPut(
-                context.macho_file.base.allocator,
-                target,
-            );
-            log.debug("creating new GOT entry at index {d} for {}", .{
-                context.macho_file.got_entries_map.getIndex(target).?,
-                target,
-            });
-            break :blk res.value_ptr;
-        }
-    };
+    if (context.macho_file.got_entries_table.contains(target)) return;
+
+    const index = try context.macho_file.allocateGotEntry(target);
     const atom = try context.macho_file.createGotAtom(target);
-    value_ptr.* = atom;
+    context.macho_file.got_entries.items[index].atom = atom;
 
     const match = MachO.MatchingSection{
         .seg = context.macho_file.data_const_segment_cmd_index.?,
@@ -627,30 +593,13 @@ fn addGotEntry(target: Relocation.Target, context: RelocContext) !void {
 
 fn addStub(target: Relocation.Target, context: RelocContext) !void {
     if (target != .global) return;
-    if (context.macho_file.stubs_map.contains(target.global)) return;
+    if (context.macho_file.stubs_table.contains(target.global)) return;
     // If the symbol has been resolved as defined globally elsewhere (in a different translation unit),
     // then skip creating stub entry.
     // TODO Is this the correct for the incremental?
     if (context.macho_file.symbol_resolver.get(target.global).?.where == .global) return;
 
-    const value_ptr = blk: {
-        if (context.macho_file.stubs_map_free_list.popOrNull()) |i| {
-            log.debug("reusing stubs entry index {d} for {}", .{ i, target });
-            context.macho_file.stubs_map.keys()[i] = target.global;
-            const value_ptr = context.macho_file.stubs_map.getPtr(target.global).?;
-            break :blk value_ptr;
-        } else {
-            const res = try context.macho_file.stubs_map.getOrPut(
-                context.macho_file.base.allocator,
-                target.global,
-            );
-            log.debug("creating new stubs entry at index {d} for {}", .{
-                context.macho_file.stubs_map.getIndex(target.global).?,
-                target,
-            });
-            break :blk res.value_ptr;
-        }
-    };
+    const stub_index = try context.macho_file.allocateStubEntry(target.global);
 
     // TODO clean this up!
     const stub_helper_atom = atom: {
@@ -707,7 +656,7 @@ fn addStub(target: Relocation.Target, context: RelocContext) !void {
     } else {
         try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
     }
-    value_ptr.* = atom;
+    context.macho_file.stubs.items[stub_index] = atom;
 }
 
 pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
@@ -741,7 +690,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
             };
 
             if (is_via_got) {
-                const atom = macho_file.got_entries_map.get(rel.target) orelse {
+                const got_index = macho_file.got_entries_table.get(rel.target) orelse {
                     const n_strx = switch (rel.target) {
                         .local => |sym_index| macho_file.locals.items[sym_index].n_strx,
                         .global => |n_strx| n_strx,
@@ -750,6 +699,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
                     log.err("  this is an internal linker error", .{});
                     return error.FailedToResolveRelocationTarget;
                 };
+                const atom = macho_file.got_entries.items[got_index].atom;
                 break :blk macho_file.locals.items[atom.local_sym_index].n_value;
             }
 
@@ -795,15 +745,17 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
                     switch (resolv.where) {
                         .global => break :blk macho_file.globals.items[resolv.where_index].n_value,
                         .undef => {
-                            break :blk if (macho_file.stubs_map.get(n_strx)) |atom|
-                                macho_file.locals.items[atom.local_sym_index].n_value
-                            else inner: {
-                                if (macho_file.tlv_ptr_entries_map.get(rel.target)) |atom| {
+                            if (macho_file.stubs_table.get(n_strx)) |stub_index| {
+                                const atom = macho_file.stubs.items[stub_index];
+                                break :blk macho_file.locals.items[atom.local_sym_index].n_value;
+                            } else {
+                                if (macho_file.tlv_ptr_entries_table.get(rel.target)) |tlv_ptr_index| {
                                     is_via_thread_ptrs = true;
-                                    break :inner macho_file.locals.items[atom.local_sym_index].n_value;
+                                    const atom = macho_file.tlv_ptr_entries.items[tlv_ptr_index].atom;
+                                    break :blk macho_file.locals.items[atom.local_sym_index].n_value;
                                 }
-                                break :inner 0;
-                            };
+                                break :blk 0;
+                            }
                         },
                     }
                 },
src/link/Coff.zig
@@ -21,6 +21,7 @@ const mingw = @import("../mingw.zig");
 const Air = @import("../Air.zig");
 const Liveness = @import("../Liveness.zig");
 const LlvmObject = @import("../codegen/llvm.zig").Object;
+const TypedValue = @import("../TypedValue.zig");
 
 const allocation_padding = 4 / 3;
 const minimum_text_block_size = 64 * allocation_padding;
@@ -697,6 +698,14 @@ pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, live
     return self.finishUpdateDecl(module, func.owner_decl, code);
 }
 
+pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl: *Module.Decl) !u32 {
+    _ = self;
+    _ = tv;
+    _ = decl;
+    log.debug("TODO lowerUnnamedConst for Coff", .{});
+    return error.AnalysisFail;
+}
+
 pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {
     if (build_options.skip_non_native and builtin.object_format != .coff) {
         @panic("Attempted to compile for object format that was disabled by build configuration");
src/link/Elf.zig
@@ -19,6 +19,7 @@ const trace = @import("../tracy.zig").trace;
 const Package = @import("../Package.zig");
 const Value = @import("../value.zig").Value;
 const Type = @import("../type.zig").Type;
+const TypedValue = @import("../TypedValue.zig");
 const link = @import("../link.zig");
 const File = link.File;
 const build_options = @import("build_options");
@@ -110,6 +111,9 @@ debug_line_header_dirty: bool = false,
 
 error_flags: File.ErrorFlags = File.ErrorFlags{},
 
+/// Pointer to the last allocated atom
+atoms: std.AutoHashMapUnmanaged(u16, *TextBlock) = .{},
+
 /// A list of text blocks that have surplus capacity. This list can have false
 /// positives, as functions grow and shrink over time, only sometimes being added
 /// or removed from the freelist.
@@ -125,10 +129,42 @@ error_flags: File.ErrorFlags = File.ErrorFlags{},
 /// overcapacity can be negative. A simple way to have negative overcapacity is to
 /// allocate a fresh text block, which will have ideal capacity, and then grow it
 /// by 1 byte. It will then have -1 overcapacity.
-atoms: std.AutoHashMapUnmanaged(u16, *TextBlock) = .{},
 atom_free_lists: std.AutoHashMapUnmanaged(u16, std.ArrayListUnmanaged(*TextBlock)) = .{},
+
+/// Table of Decls that are currently alive.
+/// We store them here so that we can properly dispose of any allocated
+/// memory within the atom in the incremental linker.
+/// TODO consolidate this.
 decls: std.AutoHashMapUnmanaged(*Module.Decl, ?u16) = .{},
 
+/// List of atoms that are owned directly by the linker.
+/// Currently these are only atoms that are the result of linking
+/// object files. Atoms which take part in incremental linking are
+/// at present owned by Module.Decl.
+/// TODO consolidate this.
+managed_atoms: std.ArrayListUnmanaged(*TextBlock) = .{},
+
+/// Table of unnamed constants associated with a parent `Decl`.
+/// We store them here so that we can free the constants whenever the `Decl`
+/// needs updating or is freed.
+///
+/// For example,
+///
+/// ```zig
+/// const Foo = struct{
+///     a: u8,
+/// };
+/// 
+/// pub fn main() void {
+///     var foo = Foo{ .a = 1 };
+///     _ = foo;
+/// }
+/// ```
+///
+/// value assigned to label `foo` is an unnamed constant belonging/associated
+/// with `Decl` `main`, and lives as long as that `Decl`.
+unnamed_const_atoms: UnnamedConstTable = .{},
+
 /// A list of `SrcFn` whose Line Number Programs have surplus capacity.
 /// This is the same concept as `text_block_free_list`; see those doc comments.
 dbg_line_fn_free_list: std.AutoHashMapUnmanaged(*SrcFn, void) = .{},
@@ -141,6 +177,8 @@ dbg_info_decl_free_list: std.AutoHashMapUnmanaged(*TextBlock, void) = .{},
 dbg_info_decl_first: ?*TextBlock = null,
 dbg_info_decl_last: ?*TextBlock = null,
 
+const UnnamedConstTable = std.AutoHashMapUnmanaged(*Module.Decl, std.ArrayListUnmanaged(*TextBlock));
+
 /// When allocating, the ideal_capacity is calculated by
 /// actual_capacity + (actual_capacity / ideal_factor)
 const ideal_factor = 3;
@@ -342,6 +380,19 @@ pub fn deinit(self: *Elf) void {
         }
         self.atom_free_lists.deinit(self.base.allocator);
     }
+
+    for (self.managed_atoms.items) |atom| {
+        self.base.allocator.destroy(atom);
+    }
+    self.managed_atoms.deinit(self.base.allocator);
+
+    {
+        var it = self.unnamed_const_atoms.valueIterator();
+        while (it.next()) |atoms| {
+            atoms.deinit(self.base.allocator);
+        }
+        self.unnamed_const_atoms.deinit(self.base.allocator);
+    }
 }
 
 pub fn getDeclVAddr(self: *Elf, decl: *const Module.Decl) u64 {
@@ -2166,6 +2217,11 @@ fn writeElfHeader(self: *Elf) !void {
 }
 
 fn freeTextBlock(self: *Elf, text_block: *TextBlock, phdr_index: u16) void {
+    const local_sym = self.local_symbols.items[text_block.local_sym_index];
+    const name_str_index = local_sym.st_name;
+    const name = self.getString(name_str_index);
+    log.debug("freeTextBlock {*} ({s})", .{ text_block, name });
+
     const free_list = self.atom_free_lists.getPtr(phdr_index).?;
     var already_have_free_list_node = false;
     {
@@ -2376,23 +2432,43 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al
     return vaddr;
 }
 
+fn allocateLocalSymbol(self: *Elf) !u32 {
+    try self.local_symbols.ensureUnusedCapacity(self.base.allocator, 1);
+
+    const index = blk: {
+        if (self.local_symbol_free_list.popOrNull()) |index| {
+            log.debug("  (reusing symbol index {d})", .{index});
+            break :blk index;
+        } else {
+            log.debug("  (allocating symbol index {d})", .{self.local_symbols.items.len});
+            const index = @intCast(u32, self.local_symbols.items.len);
+            _ = self.local_symbols.addOneAssumeCapacity();
+            break :blk index;
+        }
+    };
+
+    self.local_symbols.items[index] = .{
+        .st_name = 0,
+        .st_info = 0,
+        .st_other = 0,
+        .st_shndx = 0,
+        .st_value = 0,
+        .st_size = 0,
+    };
+
+    return index;
+}
+
 pub fn allocateDeclIndexes(self: *Elf, decl: *Module.Decl) !void {
     if (self.llvm_object) |_| return;
 
     if (decl.link.elf.local_sym_index != 0) return;
 
-    try self.local_symbols.ensureUnusedCapacity(self.base.allocator, 1);
     try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);
     try self.decls.putNoClobber(self.base.allocator, decl, null);
 
-    if (self.local_symbol_free_list.popOrNull()) |i| {
-        log.debug("reusing symbol index {d} for {s}", .{ i, decl.name });
-        decl.link.elf.local_sym_index = i;
-    } else {
-        log.debug("allocating symbol index {d} for {s}", .{ self.local_symbols.items.len, decl.name });
-        decl.link.elf.local_sym_index = @intCast(u32, self.local_symbols.items.len);
-        _ = self.local_symbols.addOneAssumeCapacity();
-    }
+    log.debug("allocating symbol indexes for {s}", .{decl.name});
+    decl.link.elf.local_sym_index = try self.allocateLocalSymbol();
 
     if (self.offset_table_free_list.popOrNull()) |i| {
         decl.link.elf.offset_table_index = i;
@@ -2401,18 +2477,19 @@ pub fn allocateDeclIndexes(self: *Elf, decl: *Module.Decl) !void {
         _ = self.offset_table.addOneAssumeCapacity();
         self.offset_table_count_dirty = true;
     }
-
-    self.local_symbols.items[decl.link.elf.local_sym_index] = .{
-        .st_name = 0,
-        .st_info = 0,
-        .st_other = 0,
-        .st_shndx = 0,
-        .st_value = 0,
-        .st_size = 0,
-    };
     self.offset_table.items[decl.link.elf.offset_table_index] = 0;
 }
 
+fn freeUnnamedConsts(self: *Elf, decl: *Module.Decl) void {
+    const unnamed_consts = self.unnamed_const_atoms.getPtr(decl) orelse return;
+    for (unnamed_consts.items) |atom| {
+        self.freeTextBlock(atom, self.phdr_load_ro_index.?);
+        self.local_symbol_free_list.append(self.base.allocator, atom.local_sym_index) catch {};
+        self.local_symbols.items[atom.local_sym_index].st_info = 0;
+    }
+    unnamed_consts.clearAndFree(self.base.allocator);
+}
+
 pub fn freeDecl(self: *Elf, decl: *Module.Decl) void {
     if (build_options.have_llvm) {
         if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl);
@@ -2421,6 +2498,7 @@ pub fn freeDecl(self: *Elf, decl: *Module.Decl) void {
     const kv = self.decls.fetchRemove(decl);
     if (kv.?.value) |index| {
         self.freeTextBlock(&decl.link.elf, index);
+        self.freeUnnamedConsts(decl);
     }
 
     // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
@@ -2528,7 +2606,6 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8
         const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);
         errdefer self.freeTextBlock(&decl.link.elf, phdr_index);
         log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr });
-        errdefer self.freeTextBlock(&decl.link.elf, phdr_index);
 
         local_sym.* = .{
             .st_name = name_str_index,
@@ -2632,6 +2709,8 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
     defer deinitRelocs(self.base.allocator, &dbg_info_type_relocs);
 
     const decl = func.owner_decl;
+    self.freeUnnamedConsts(decl);
+
     log.debug("updateFunc {s}{*}", .{ decl.name, func.owner_decl });
     log.debug("  (decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d})", .{
         decl.src_line,
@@ -2859,6 +2938,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
         }
     }
 
+    assert(!self.unnamed_const_atoms.contains(decl));
+
     var code_buffer = std.ArrayList(u8).init(self.base.allocator);
     defer code_buffer.deinit();
 
@@ -2897,6 +2978,74 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
     return self.finishUpdateDecl(module, decl, &dbg_info_type_relocs, &dbg_info_buffer);
 }
 
+pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl: *Module.Decl) !u32 {
+    var code_buffer = std.ArrayList(u8).init(self.base.allocator);
+    defer code_buffer.deinit();
+
+    const module = self.base.options.module.?;
+    const gop = try self.unnamed_const_atoms.getOrPut(self.base.allocator, decl);
+    if (!gop.found_existing) {
+        gop.value_ptr.* = .{};
+    }
+    const unnamed_consts = gop.value_ptr;
+
+    const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
+        .none = .{},
+    });
+    const code = switch (res) {
+        .externally_managed => |x| x,
+        .appended => code_buffer.items,
+        .fail => |em| {
+            decl.analysis = .codegen_failure;
+            try module.failed_decls.put(module.gpa, decl, em);
+            return error.AnalysisFail;
+        },
+    };
+
+    const atom = try self.base.allocator.create(TextBlock);
+    errdefer self.base.allocator.destroy(atom);
+    atom.* = TextBlock.empty;
+    try self.managed_atoms.append(self.base.allocator, atom);
+
+    const name_str_index = blk: {
+        const index = unnamed_consts.items.len;
+        const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl.name, index });
+        defer self.base.allocator.free(name);
+        break :blk try self.makeString(name);
+    };
+    const name = self.getString(name_str_index);
+
+    log.debug("allocating symbol indexes for {s}", .{name});
+    atom.local_sym_index = try self.allocateLocalSymbol();
+
+    const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
+    const phdr_index = self.phdr_load_ro_index.?;
+    const shdr_index = self.phdr_shdr_table.get(phdr_index).?;
+    const vaddr = try self.allocateTextBlock(atom, code.len, required_alignment, phdr_index);
+    errdefer self.freeTextBlock(atom, phdr_index);
+
+    log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });
+
+    const local_sym = &self.local_symbols.items[atom.local_sym_index];
+    local_sym.* = .{
+        .st_name = name_str_index,
+        .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
+        .st_other = 0,
+        .st_shndx = shdr_index,
+        .st_value = vaddr,
+        .st_size = code.len,
+    };
+
+    try self.writeSymbol(atom.local_sym_index);
+    try unnamed_consts.append(self.base.allocator, atom);
+
+    const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
+    const file_offset = self.sections.items[shdr_index].sh_offset + section_offset;
+    try self.base.file.?.pwriteAll(code, file_offset);
+
+    return atom.local_sym_index;
+}
+
 /// Asserts the type has codegen bits.
 fn addDbgInfoType(
     self: *Elf,
src/link/MachO.zig
@@ -39,6 +39,7 @@ const StringIndexAdapter = std.hash_map.StringIndexAdapter;
 const StringIndexContext = std.hash_map.StringIndexContext;
 const Trie = @import("MachO/Trie.zig");
 const Type = @import("../type.zig").Type;
+const TypedValue = @import("../TypedValue.zig");
 
 pub const TextBlock = Atom;
 
@@ -166,14 +167,17 @@ stub_helper_preamble_atom: ?*Atom = null,
 strtab: std.ArrayListUnmanaged(u8) = .{},
 strtab_dir: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
 
-tlv_ptr_entries_map: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, *Atom) = .{},
-tlv_ptr_entries_map_free_list: std.ArrayListUnmanaged(u32) = .{},
+tlv_ptr_entries: std.ArrayListUnmanaged(Entry) = .{},
+tlv_ptr_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
+tlv_ptr_entries_table: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, u32) = .{},
 
-got_entries_map: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, *Atom) = .{},
-got_entries_map_free_list: std.ArrayListUnmanaged(u32) = .{},
+got_entries: std.ArrayListUnmanaged(Entry) = .{},
+got_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
+got_entries_table: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, u32) = .{},
 
-stubs_map: std.AutoArrayHashMapUnmanaged(u32, *Atom) = .{},
-stubs_map_free_list: std.ArrayListUnmanaged(u32) = .{},
+stubs: std.ArrayListUnmanaged(*Atom) = .{},
+stubs_free_list: std.ArrayListUnmanaged(u32) = .{},
+stubs_table: std.AutoArrayHashMapUnmanaged(u32, u32) = .{},
 
 error_flags: File.ErrorFlags = File.ErrorFlags{},
 
@@ -217,6 +221,27 @@ atoms: std.AutoHashMapUnmanaged(MatchingSection, *Atom) = .{},
 /// TODO consolidate this.
 managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
 
+/// Table of unnamed constants associated with a parent `Decl`.
+/// We store them here so that we can free the constants whenever the `Decl`
+/// needs updating or is freed.
+///
+/// For example,
+///
+/// ```zig
+/// const Foo = struct{
+///     a: u8,
+/// };
+/// 
+/// pub fn main() void {
+///     var foo = Foo{ .a = 1 };
+///     _ = foo;
+/// }
+/// ```
+///
+/// value assigned to label `foo` is an unnamed constant belonging/associated
+/// with `Decl` `main`, and lives as long as that `Decl`.
+unnamed_const_atoms: UnnamedConstTable = .{},
+
 /// Table of Decls that are currently alive.
 /// We store them here so that we can properly dispose of any allocated
 /// memory within the atom in the incremental linker.
@@ -229,6 +254,13 @@ decls: std.AutoArrayHashMapUnmanaged(*Module.Decl, ?MatchingSection) = .{},
 /// somewhere else in the codegen.
 active_decl: ?*Module.Decl = null,
 
+const Entry = struct {
+    target: Atom.Relocation.Target,
+    atom: *Atom,
+};
+
+const UnnamedConstTable = std.AutoHashMapUnmanaged(*Module.Decl, std.ArrayListUnmanaged(*Atom));
+
 const PendingUpdate = union(enum) {
     resolve_undef: u32,
     add_stub_entry: u32,
@@ -661,16 +693,15 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
                                 sym.n_desc = 0;
                             },
                         }
-                        if (self.got_entries_map.getIndex(.{ .global = entry.key })) |i| {
-                            self.got_entries_map_free_list.append(
-                                self.base.allocator,
-                                @intCast(u32, i),
-                            ) catch {};
-                            self.got_entries_map.keys()[i] = .{ .local = 0 };
+                        if (self.got_entries_table.get(.{ .global = entry.key })) |i| {
+                            self.got_entries_free_list.append(self.base.allocator, @intCast(u32, i)) catch {};
+                            self.got_entries.items[i] = .{ .target = .{ .local = 0 }, .atom = undefined };
+                            _ = self.got_entries_table.swapRemove(.{ .global = entry.key });
                         }
-                        if (self.stubs_map.getIndex(entry.key)) |i| {
-                            self.stubs_map_free_list.append(self.base.allocator, @intCast(u32, i)) catch {};
-                            self.stubs_map.keys()[i] = 0;
+                        if (self.stubs_table.get(entry.key)) |i| {
+                            self.stubs_free_list.append(self.base.allocator, @intCast(u32, i)) catch {};
+                            self.stubs.items[i] = undefined;
+                            _ = self.stubs_table.swapRemove(entry.key);
                         }
                     }
                 }
@@ -2948,7 +2979,7 @@ fn resolveSymbolsInDylibs(self: *MachO) !void {
                     .none => {},
                     .got => return error.TODOGotHint,
                     .stub => {
-                        if (self.stubs_map.contains(sym.n_strx)) break :outer_blk;
+                        if (self.stubs_table.contains(sym.n_strx)) break :outer_blk;
                         const stub_helper_atom = blk: {
                             const match = MatchingSection{
                                 .seg = self.text_segment_cmd_index.?,
@@ -2991,7 +3022,9 @@ fn resolveSymbolsInDylibs(self: *MachO) !void {
                             atom_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
                             break :blk atom;
                         };
-                        try self.stubs_map.putNoClobber(self.base.allocator, sym.n_strx, stub_atom);
+                        const stub_index = @intCast(u32, self.stubs.items.len);
+                        try self.stubs.append(self.base.allocator, stub_atom);
+                        try self.stubs_table.putNoClobber(self.base.allocator, sym.n_strx, stub_index);
                     },
                 }
             }
@@ -3086,7 +3119,9 @@ fn resolveDyldStubBinder(self: *MachO) !void {
     // Add dyld_stub_binder as the final GOT entry.
     const target = Atom.Relocation.Target{ .global = n_strx };
     const atom = try self.createGotAtom(target);
-    try self.got_entries_map.putNoClobber(self.base.allocator, target, atom);
+    const got_index = @intCast(u32, self.got_entries.items.len);
+    try self.got_entries.append(self.base.allocator, .{ .target = target, .atom = atom });
+    try self.got_entries_table.putNoClobber(self.base.allocator, target, got_index);
     const match = MatchingSection{
         .seg = self.data_const_segment_cmd_index.?,
         .sect = self.got_section_index.?,
@@ -3339,12 +3374,15 @@ pub fn deinit(self: *MachO) void {
     }
 
     self.section_ordinals.deinit(self.base.allocator);
-    self.tlv_ptr_entries_map.deinit(self.base.allocator);
-    self.tlv_ptr_entries_map_free_list.deinit(self.base.allocator);
-    self.got_entries_map.deinit(self.base.allocator);
-    self.got_entries_map_free_list.deinit(self.base.allocator);
-    self.stubs_map.deinit(self.base.allocator);
-    self.stubs_map_free_list.deinit(self.base.allocator);
+    self.tlv_ptr_entries.deinit(self.base.allocator);
+    self.tlv_ptr_entries_free_list.deinit(self.base.allocator);
+    self.tlv_ptr_entries_table.deinit(self.base.allocator);
+    self.got_entries.deinit(self.base.allocator);
+    self.got_entries_free_list.deinit(self.base.allocator);
+    self.got_entries_table.deinit(self.base.allocator);
+    self.stubs.deinit(self.base.allocator);
+    self.stubs_free_list.deinit(self.base.allocator);
+    self.stubs_table.deinit(self.base.allocator);
     self.strtab_dir.deinit(self.base.allocator);
     self.strtab.deinit(self.base.allocator);
     self.undefs.deinit(self.base.allocator);
@@ -3395,6 +3433,14 @@ pub fn deinit(self: *MachO) void {
         decl.link.macho.deinit(self.base.allocator);
     }
     self.decls.deinit(self.base.allocator);
+
+    {
+        var it = self.unnamed_const_atoms.valueIterator();
+        while (it.next()) |atoms| {
+            atoms.deinit(self.base.allocator);
+        }
+        self.unnamed_const_atoms.deinit(self.base.allocator);
+    }
 }
 
 pub fn closeFiles(self: MachO) void {
@@ -3409,9 +3455,11 @@ pub fn closeFiles(self: MachO) void {
     }
 }
 
-fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection) void {
+fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection, owns_atom: bool) void {
     log.debug("freeAtom {*}", .{atom});
-    atom.deinit(self.base.allocator);
+    if (!owns_atom) {
+        atom.deinit(self.base.allocator);
+    }
 
     const free_list = self.atom_free_lists.getPtr(match).?;
     var already_have_free_list_node = false;
@@ -3502,23 +3550,22 @@ fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, match
     return self.allocateAtom(atom, new_atom_size, alignment, match);
 }
 
-pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
-    if (self.llvm_object) |_| return;
-    if (decl.link.macho.local_sym_index != 0) return;
-
+fn allocateLocalSymbol(self: *MachO) !u32 {
     try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
-    try self.decls.putNoClobber(self.base.allocator, decl, null);
 
-    if (self.locals_free_list.popOrNull()) |i| {
-        log.debug("reusing symbol index {d} for {s}", .{ i, decl.name });
-        decl.link.macho.local_sym_index = i;
-    } else {
-        log.debug("allocating symbol index {d} for {s}", .{ self.locals.items.len, decl.name });
-        decl.link.macho.local_sym_index = @intCast(u32, self.locals.items.len);
-        _ = self.locals.addOneAssumeCapacity();
-    }
+    const index = blk: {
+        if (self.locals_free_list.popOrNull()) |index| {
+            log.debug("  (reusing symbol index {d})", .{index});
+            break :blk index;
+        } else {
+            log.debug("  (allocating symbol index {d})", .{self.locals.items.len});
+            const index = @intCast(u32, self.locals.items.len);
+            _ = self.locals.addOneAssumeCapacity();
+            break :blk index;
+        }
+    };
 
-    self.locals.items[decl.link.macho.local_sym_index] = .{
+    self.locals.items[index] = .{
         .n_strx = 0,
         .n_type = 0,
         .n_sect = 0,
@@ -3526,24 +3573,86 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
         .n_value = 0,
     };
 
-    // TODO try popping from free list first before allocating a new GOT atom.
-    const target = Atom.Relocation.Target{ .local = decl.link.macho.local_sym_index };
-    const value_ptr = blk: {
-        if (self.got_entries_map_free_list.popOrNull()) |i| {
-            log.debug("reusing GOT entry index {d} for {s}", .{ i, decl.name });
-            self.got_entries_map.keys()[i] = target;
-            const value_ptr = self.got_entries_map.getPtr(target).?;
-            break :blk value_ptr;
+    return index;
+}
+
+pub fn allocateGotEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
+    try self.got_entries.ensureUnusedCapacity(self.base.allocator, 1);
+
+    const index = blk: {
+        if (self.got_entries_free_list.popOrNull()) |index| {
+            log.debug("  (reusing GOT entry index {d})", .{index});
+            break :blk index;
         } else {
-            const res = try self.got_entries_map.getOrPut(self.base.allocator, target);
-            log.debug("creating new GOT entry at index {d} for {s}", .{
-                self.got_entries_map.getIndex(target).?,
-                decl.name,
-            });
-            break :blk res.value_ptr;
+            log.debug("  (allocating GOT entry at index {d})", .{self.got_entries.items.len});
+            const index = @intCast(u32, self.got_entries.items.len);
+            _ = self.got_entries.addOneAssumeCapacity();
+            break :blk index;
+        }
+    };
+
+    self.got_entries.items[index] = .{
+        .target = target,
+        .atom = undefined,
+    };
+    try self.got_entries_table.putNoClobber(self.base.allocator, target, index);
+
+    return index;
+}
+
+pub fn allocateStubEntry(self: *MachO, n_strx: u32) !u32 {
+    try self.stubs.ensureUnusedCapacity(self.base.allocator, 1);
+
+    const index = blk: {
+        if (self.stubs_free_list.popOrNull()) |index| {
+            log.debug("  (reusing stub entry index {d})", .{index});
+            break :blk index;
+        } else {
+            log.debug("  (allocating stub entry at index {d})", .{self.stubs.items.len});
+            const index = @intCast(u32, self.stubs.items.len);
+            _ = self.stubs.addOneAssumeCapacity();
+            break :blk index;
+        }
+    };
+
+    self.stubs.items[index] = undefined;
+    try self.stubs_table.putNoClobber(self.base.allocator, n_strx, index);
+
+    return index;
+}
+
+pub fn allocateTlvPtrEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
+    try self.tlv_ptr_entries.ensureUnusedCapacity(self.base.allocator, 1);
+
+    const index = blk: {
+        if (self.tlv_ptr_entries_free_list.popOrNull()) |index| {
+            log.debug("  (reusing TLV ptr entry index {d})", .{index});
+            break :blk index;
+        } else {
+            log.debug("  (allocating TLV ptr entry at index {d})", .{self.tlv_ptr_entries.items.len});
+            const index = @intCast(u32, self.tlv_ptr_entries.items.len);
+            _ = self.tlv_ptr_entries.addOneAssumeCapacity();
+            break :blk index;
         }
     };
-    value_ptr.* = try self.createGotAtom(target);
+
+    self.tlv_ptr_entries.items[index] = .{ .target = target, .atom = undefined };
+    try self.tlv_ptr_entries_table.putNoClobber(self.base.allocator, target, index);
+
+    return index;
+}
+
+pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
+    if (self.llvm_object) |_| return;
+    if (decl.link.macho.local_sym_index != 0) return;
+
+    decl.link.macho.local_sym_index = try self.allocateLocalSymbol();
+    try self.decls.putNoClobber(self.base.allocator, decl, null);
+
+    const got_target = .{ .local = decl.link.macho.local_sym_index };
+    const got_index = try self.allocateGotEntry(got_target);
+    const got_atom = try self.createGotAtom(got_target);
+    self.got_entries.items[got_index].atom = got_atom;
 }
 
 pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
@@ -3557,6 +3666,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
     defer tracy.end();
 
     const decl = func.owner_decl;
+    self.freeUnnamedConsts(decl);
     // TODO clearing the code and relocs buffer should probably be orchestrated
     // in a different, smarter, more automatic way somewhere else, in a more centralised
     // way than this.
@@ -3624,6 +3734,70 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
     try self.updateDeclExports(module, decl, decl_exports);
 }
 
+pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl: *Module.Decl) !u32 {
+    var code_buffer = std.ArrayList(u8).init(self.base.allocator);
+    defer code_buffer.deinit();
+
+    const module = self.base.options.module.?;
+    const gop = try self.unnamed_const_atoms.getOrPut(self.base.allocator, decl);
+    if (!gop.found_existing) {
+        gop.value_ptr.* = .{};
+    }
+    const unnamed_consts = gop.value_ptr;
+
+    const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
+        .none = .{},
+    });
+    const code = switch (res) {
+        .externally_managed => |x| x,
+        .appended => code_buffer.items,
+        .fail => |em| {
+            decl.analysis = .codegen_failure;
+            try module.failed_decls.put(module.gpa, decl, em);
+            return error.AnalysisFail;
+        },
+    };
+
+    const name_str_index = blk: {
+        const index = unnamed_consts.items.len;
+        const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl.name, index });
+        defer self.base.allocator.free(name);
+        break :blk try self.makeString(name);
+    };
+    const name = self.getString(name_str_index);
+
+    log.debug("allocating symbol indexes for {s}", .{name});
+
+    const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
+    const match = (try self.getMatchingSection(.{
+        .segname = makeStaticString("__TEXT"),
+        .sectname = makeStaticString("__const"),
+        .size = code.len,
+        .@"align" = math.log2(required_alignment),
+    })).?;
+    const local_sym_index = try self.allocateLocalSymbol();
+    const atom = try self.createEmptyAtom(local_sym_index, code.len, math.log2(required_alignment));
+    mem.copy(u8, atom.code.items, code);
+    const addr = try self.allocateAtom(atom, code.len, required_alignment, match);
+
+    log.debug("allocated atom for {s} at 0x{x}", .{ name, addr });
+
+    errdefer self.freeAtom(atom, match, true);
+
+    const symbol = &self.locals.items[atom.local_sym_index];
+    symbol.* = .{
+        .n_strx = name_str_index,
+        .n_type = macho.N_SECT,
+        .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).?) + 1,
+        .n_desc = 0,
+        .n_value = addr,
+    };
+
+    try unnamed_consts.append(self.base.allocator, atom);
+
+    return atom.local_sym_index;
+}
+
 pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
     if (build_options.skip_non_native and builtin.object_format != .macho) {
         @panic("Attempted to compile for object format that was disabled by build configuration");
@@ -3879,7 +4053,8 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
 
             if (vaddr != symbol.n_value) {
                 log.debug(" (writing new GOT entry)", .{});
-                const got_atom = self.got_entries_map.get(.{ .local = decl.link.macho.local_sym_index }).?;
+                const got_index = self.got_entries_table.get(.{ .local = decl.link.macho.local_sym_index }).?;
+                const got_atom = self.got_entries.items[got_index].atom;
                 const got_sym = &self.locals.items[got_atom.local_sym_index];
                 const got_vaddr = try self.allocateAtom(got_atom, @sizeOf(u64), 8, .{
                     .seg = self.data_const_segment_cmd_index.?,
@@ -3920,7 +4095,7 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
 
         log.debug("allocated atom for {s} at 0x{x}", .{ decl_name, addr });
 
-        errdefer self.freeAtom(&decl.link.macho, match);
+        errdefer self.freeAtom(&decl.link.macho, match, false);
 
         symbol.* = .{
             .n_strx = name_str_index,
@@ -3929,7 +4104,8 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
             .n_desc = 0,
             .n_value = addr,
         };
-        const got_atom = self.got_entries_map.get(.{ .local = decl.link.macho.local_sym_index }).?;
+        const got_index = self.got_entries_table.get(.{ .local = decl.link.macho.local_sym_index }).?;
+        const got_atom = self.got_entries.items[got_index].atom;
         const got_sym = &self.locals.items[got_atom.local_sym_index];
         const vaddr = try self.allocateAtom(got_atom, @sizeOf(u64), 8, .{
             .seg = self.data_const_segment_cmd_index.?,
@@ -4103,6 +4279,19 @@ pub fn deleteExport(self: *MachO, exp: Export) void {
     global.n_value = 0;
 }
 
+fn freeUnnamedConsts(self: *MachO, decl: *Module.Decl) void {
+    const unnamed_consts = self.unnamed_const_atoms.getPtr(decl) orelse return;
+    for (unnamed_consts.items) |atom| {
+        self.freeAtom(atom, .{
+            .seg = self.text_segment_cmd_index.?,
+            .sect = self.text_const_section_index.?,
+        }, true);
+        self.locals_free_list.append(self.base.allocator, atom.local_sym_index) catch {};
+        self.locals.items[atom.local_sym_index].n_type = 0;
+    }
+    unnamed_consts.clearAndFree(self.base.allocator);
+}
+
 pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {
     if (build_options.have_llvm) {
         if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl);
@@ -4110,15 +4299,19 @@ pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {
     log.debug("freeDecl {*}", .{decl});
     const kv = self.decls.fetchSwapRemove(decl);
     if (kv.?.value) |match| {
-        self.freeAtom(&decl.link.macho, match);
+        self.freeAtom(&decl.link.macho, match, false);
+        self.freeUnnamedConsts(decl);
     }
     // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
     if (decl.link.macho.local_sym_index != 0) {
         self.locals_free_list.append(self.base.allocator, decl.link.macho.local_sym_index) catch {};
 
-        // Try freeing GOT atom
-        const got_index = self.got_entries_map.getIndex(.{ .local = decl.link.macho.local_sym_index }).?;
-        self.got_entries_map_free_list.append(self.base.allocator, @intCast(u32, got_index)) catch {};
+        // Try freeing GOT atom if this decl had one
+        if (self.got_entries_table.get(.{ .local = decl.link.macho.local_sym_index })) |got_index| {
+            self.got_entries_free_list.append(self.base.allocator, @intCast(u32, got_index)) catch {};
+            self.got_entries.items[got_index] = .{ .target = .{ .local = 0 }, .atom = undefined };
+            _ = self.got_entries_table.swapRemove(.{ .local = decl.link.macho.local_sym_index });
+        }
 
         self.locals.items[decl.link.macho.local_sym_index].n_type = 0;
         decl.link.macho.local_sym_index = 0;
@@ -5932,8 +6125,8 @@ fn writeSymbolTable(self: *MachO) !void {
     const data_segment = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
     const la_symbol_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?];
 
-    const nstubs = @intCast(u32, self.stubs_map.keys().len);
-    const ngot_entries = @intCast(u32, self.got_entries_map.keys().len);
+    const nstubs = @intCast(u32, self.stubs_table.keys().len);
+    const ngot_entries = @intCast(u32, self.got_entries_table.keys().len);
 
     dysymtab.indirectsymoff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize);
     dysymtab.nindirectsyms = nstubs * 2 + ngot_entries;
@@ -5953,7 +6146,7 @@ fn writeSymbolTable(self: *MachO) !void {
     var writer = stream.writer();
 
     stubs.reserved1 = 0;
-    for (self.stubs_map.keys()) |key| {
+    for (self.stubs_table.keys()) |key| {
         const resolv = self.symbol_resolver.get(key).?;
         switch (resolv.where) {
             .global => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
@@ -5962,7 +6155,7 @@ fn writeSymbolTable(self: *MachO) !void {
     }
 
     got.reserved1 = nstubs;
-    for (self.got_entries_map.keys()) |key| {
+    for (self.got_entries_table.keys()) |key| {
         switch (key) {
             .local => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
             .global => |n_strx| {
@@ -5976,7 +6169,7 @@ fn writeSymbolTable(self: *MachO) !void {
     }
 
     la_symbol_ptr.reserved1 = got.reserved1 + ngot_entries;
-    for (self.stubs_map.keys()) |key| {
+    for (self.stubs_table.keys()) |key| {
         const resolv = self.symbol_resolver.get(key).?;
         switch (resolv.where) {
             .global => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
@@ -6348,7 +6541,7 @@ fn snapshotState(self: *MachO) !void {
                     };
 
                     if (is_via_got) {
-                        const got_atom = self.got_entries_map.get(rel.target) orelse break :blk 0;
+                        const got_atom = self.got_entries_table.get(rel.target) orelse break :blk 0;
                         break :blk self.locals.items[got_atom.local_sym_index].n_value;
                     }
 
@@ -6380,10 +6573,11 @@ fn snapshotState(self: *MachO) !void {
                             switch (resolv.where) {
                                 .global => break :blk self.globals.items[resolv.where_index].n_value,
                                 .undef => {
-                                    break :blk if (self.stubs_map.get(n_strx)) |stub_atom|
-                                        self.locals.items[stub_atom.local_sym_index].n_value
-                                    else
-                                        0;
+                                    if (self.stubs_table.get(n_strx)) |stub_index| {
+                                        const stub_atom = self.stubs.items[stub_index];
+                                        break :blk self.locals.items[stub_atom.local_sym_index].n_value;
+                                    }
+                                    break :blk 0;
                                 },
                             }
                         },
@@ -6508,15 +6702,20 @@ fn logSymtab(self: MachO) void {
     }
 
     log.debug("GOT entries:", .{});
-    for (self.got_entries_map.keys()) |key| {
+    for (self.got_entries_table.values()) |value| {
+        const key = self.got_entries.items[value].target;
+        const atom = self.got_entries.items[value].atom;
         switch (key) {
-            .local => |sym_index| log.debug("  {} => {d}", .{ key, sym_index }),
+            .local => {
+                const sym = self.locals.items[atom.local_sym_index];
+                log.debug("  {} => {s}", .{ key, self.getString(sym.n_strx) });
+            },
             .global => |n_strx| log.debug("  {} => {s}", .{ key, self.getString(n_strx) }),
         }
     }
 
     log.debug("__thread_ptrs entries:", .{});
-    for (self.tlv_ptr_entries_map.keys()) |key| {
+    for (self.tlv_ptr_entries_table.keys()) |key| {
         switch (key) {
             .local => unreachable,
             .global => |n_strx| log.debug("  {} => {s}", .{ key, self.getString(n_strx) }),
@@ -6524,7 +6723,7 @@ fn logSymtab(self: MachO) void {
     }
 
     log.debug("stubs:", .{});
-    for (self.stubs_map.keys()) |key| {
+    for (self.stubs_table.keys()) |key| {
         log.debug("  {} => {s}", .{ key, self.getString(key) });
     }
 }
src/link/Plan9.zig
@@ -12,6 +12,7 @@ const File = link.File;
 const build_options = @import("build_options");
 const Air = @import("../Air.zig");
 const Liveness = @import("../Liveness.zig");
+const TypedValue = @import("../TypedValue.zig");
 
 const std = @import("std");
 const builtin = @import("builtin");
@@ -275,6 +276,14 @@ pub fn updateFunc(self: *Plan9, module: *Module, func: *Module.Fn, air: Air, liv
     return self.updateFinish(decl);
 }
 
+pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl: *Module.Decl) !u32 {
+    _ = self;
+    _ = tv;
+    _ = decl;
+    log.debug("TODO lowerUnnamedConst for Plan9", .{});
+    return error.AnalysisFail;
+}
+
 pub fn updateDecl(self: *Plan9, module: *Module, decl: *Module.Decl) !void {
     if (decl.val.tag() == .extern_fn) {
         return; // TODO Should we do more when front-end analyzed extern decl?
src/link.zig
@@ -17,6 +17,7 @@ const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
 const wasi_libc = @import("wasi_libc.zig");
 const Air = @import("Air.zig");
 const Liveness = @import("Liveness.zig");
+const TypedValue = @import("TypedValue.zig");
 
 pub const SystemLib = struct {
     needed: bool = false,
@@ -429,6 +430,25 @@ pub const File = struct {
         CurrentWorkingDirectoryUnlinked,
     };
 
+    /// Called from within the CodeGen to lower a local variable instantion as an unnamed
+    /// constant. Returns the symbol index of the lowered constant in the read-only section
+    /// of the final binary.
+    pub fn lowerUnnamedConst(base: *File, tv: TypedValue, decl: *Module.Decl) UpdateDeclError!u32 {
+        log.debug("lowerUnnamedConst {*} ({s})", .{ decl, decl.name });
+        switch (base.tag) {
+            // zig fmt: off
+            .coff  => return @fieldParentPtr(Coff,  "base", base).lowerUnnamedConst(tv, decl),
+            .elf   => return @fieldParentPtr(Elf,   "base", base).lowerUnnamedConst(tv, decl),
+            .macho => return @fieldParentPtr(MachO, "base", base).lowerUnnamedConst(tv, decl),
+            .plan9 => return @fieldParentPtr(Plan9, "base", base).lowerUnnamedConst(tv, decl),
+            .spirv => unreachable,
+            .c     => unreachable,
+            .wasm  => unreachable,
+            .nvptx => unreachable,
+            // zig fmt: on
+        }
+    }
+
     /// May be called before or after updateDeclExports but must be called
     /// after allocateDeclIndexes for any given Decl.
     pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) UpdateDeclError!void {
test/behavior/align.zig
@@ -7,6 +7,7 @@ var foo: u8 align(4) = 100;
 
 test "global variable alignment" {
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .macos) return error.SkipZigTest;
 
     comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
     comptime try expect(@TypeOf(&foo) == *align(4) u8);
test/behavior/cast.zig
@@ -78,16 +78,19 @@ test "comptime_int @intToFloat" {
         try expect(@TypeOf(result) == f64);
         try expect(result == 1234.0);
     }
-    {
-        const result = @intToFloat(f128, 1234);
-        try expect(@TypeOf(result) == f128);
-        try expect(result == 1234.0);
-    }
-    // big comptime_int (> 64 bits) to f128 conversion
-    {
-        const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
-        try expect(@TypeOf(result) == f128);
-        try expect(result == 0x1_0000_0000_0000_0000.0);
+    if (builtin.zig_backend != .stage2_x86_64 or builtin.os.tag != .macos) {
+        // TODO investigate why this traps on x86_64-macos
+        {
+            const result = @intToFloat(f128, 1234);
+            try expect(@TypeOf(result) == f128);
+            try expect(result == 1234.0);
+        }
+        // big comptime_int (> 64 bits) to f128 conversion
+        {
+            const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
+            try expect(@TypeOf(result) == f128);
+            try expect(result == 0x1_0000_0000_0000_0000.0);
+        }
     }
 }
 
test/behavior/struct.zig
@@ -51,6 +51,27 @@ test "non-packed struct has fields padded out to the required alignment" {
     try expect(foo.fourth() == 2);
 }
 
+const SmallStruct = struct {
+    a: u8,
+    b: u32,
+
+    fn first(self: *SmallStruct) u8 {
+        return self.a;
+    }
+
+    fn second(self: *SmallStruct) u32 {
+        return self.b;
+    }
+};
+
+test "lower unnamed constants" {
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+
+    var foo = SmallStruct{ .a = 1, .b = 255 };
+    try expect(foo.first() == 1);
+    try expect(foo.second() == 255);
+}
+
 const StructWithNoFields = struct {
     fn add(a: i32, b: i32) i32 {
         return a + b;
test/stage2/x86_64.zig
@@ -1844,6 +1844,94 @@ pub fn addCases(ctx: *TestContext) !void {
                 \\}
             , "");
         }
+
+        {
+            var case = ctx.exe("lower unnamed constants - structs", target);
+            case.addCompareOutput(
+                \\const Foo = struct {
+                \\    a: u8,
+                \\    b: u32,
+                \\
+                \\    fn first(self: *Foo) u8 {
+                \\        return self.a;
+                \\    }
+                \\
+                \\    fn second(self: *Foo) u32 {
+                \\        return self.b;
+                \\    }
+                \\};
+                \\
+                \\pub fn main() void {
+                \\    var foo = Foo{ .a = 1, .b = 5 };
+                \\    assert(foo.first() == 1);
+                \\    assert(foo.second() == 5);
+                \\}
+                \\
+                \\fn assert(ok: bool) void {
+                \\    if (!ok) unreachable;
+                \\}
+            , "");
+
+            case.addCompareOutput(
+                \\const Foo = struct {
+                \\    a: u8,
+                \\    b: u32,
+                \\
+                \\    fn first(self: *Foo) u8 {
+                \\        return self.a;
+                \\    }
+                \\
+                \\    fn second(self: *Foo) u32 {
+                \\        return self.b;
+                \\    }
+                \\};
+                \\
+                \\pub fn main() void {
+                \\    var foo = Foo{ .a = 1, .b = 5 };
+                \\    assert(foo.first() == 1);
+                \\    assert(foo.second() == 5);
+                \\
+                \\    foo.a = 10;
+                \\    foo.b = 255;
+                \\
+                \\    assert(foo.first() == 10);
+                \\    assert(foo.second() == 255);
+                \\
+                \\    var foo2 = Foo{ .a = 15, .b = 255 };
+                \\    assert(foo2.first() == 15);
+                \\    assert(foo2.second() == 255);
+                \\}
+                \\
+                \\fn assert(ok: bool) void {
+                \\    if (!ok) unreachable;
+                \\}
+            , "");
+
+            case.addCompareOutput(
+                \\const Foo = struct {
+                \\    a: u8,
+                \\    b: u32,
+                \\
+                \\    fn first(self: *Foo) u8 {
+                \\        return self.a;
+                \\    }
+                \\
+                \\    fn second(self: *Foo) u32 {
+                \\        return self.b;
+                \\    }
+                \\};
+                \\
+                \\pub fn main() void {
+                \\    var foo2 = Foo{ .a = 15, .b = 255 };
+                \\    assert(foo2.first() == 15);
+                \\    assert(foo2.second() == 255);
+                \\}
+                \\
+                \\fn assert(ok: bool) void {
+                \\    if (!ok) unreachable;
+                \\}
+            , "");
+        }
     }
 }