diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-12-19 15:47:31 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-19 15:47:31 -0500 |
| commit | 0fb53bd245e258f69654119e5a1913d6d42dc181 (patch) | |
| tree | b2fe08ac32aa43c42cf05d29985c326d20b57994 /src/codegen | |
| parent | 3542dbf0ea5bc1ddb1c5e1c856745dc07e6c0a18 (diff) | |
| parent | 0768115b01f01ab1c75da3e42ffcdc99078eaad2 (diff) | |
| download | zig-0fb53bd245e258f69654119e5a1913d6d42dc181.tar.gz zig-0fb53bd245e258f69654119e5a1913d6d42dc181.zip | |
Merge pull request #14000 from jacobly0/zero-bit-fields
codegen: fix taking the address of a field in a zero-bit struct
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/c.zig | 6 | ||||
| -rw-r--r-- | src/codegen/llvm.zig | 29 |
2 files changed, 22 insertions, 13 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index a7a2b2cf2a..2cd24f69b3 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -653,7 +653,7 @@ pub const DeclGen = struct { } try writer.print("{ }", .{fmtIdent(field_info.name)}); } else { - try dg.renderParentPtr(writer, field_ptr.container_ptr, field_info.ty); + try dg.renderParentPtr(writer, field_ptr.container_ptr, container_ptr_ty); } }, .elem_ptr => { @@ -5131,7 +5131,9 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc .begin, .end => { try writer.writeByte('('); try f.writeCValue(writer, struct_ptr, .Other); - try writer.print(")[{}]", .{@boolToInt(field_loc == .end)}); + try writer.print(")[{}]", .{ + @boolToInt(field_loc == .end and struct_ty.hasRuntimeBitsIgnoreComptime()), + }); }, .field => |field| if (extra_name != .none) { try f.writeCValueDerefMember(writer, struct_ptr, extra_name); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 7af987f4d6..3b180b4c50 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -2969,7 +2969,7 @@ pub const DeclGen = struct { comptime assert(struct_layout_version == 2); var offset: u64 = 0; - var big_align: u32 = 0; + var big_align: u32 = 1; var any_underaligned_fields = false; for (struct_obj.fields.values()) |field| { @@ -4033,16 +4033,23 @@ pub const DeclGen = struct { const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0); break :blk field_addr.constIntToPtr(final_llvm_ty); } - bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); var ty_buf: Type.Payload.Pointer = undefined; - const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?; - const indices: [2]*llvm.Value = .{ - llvm_u32.constInt(0, .False), - llvm_u32.constInt(llvm_field_index, .False), - }; + const parent_llvm_ty = try dg.lowerType(parent_ty); - break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + if (llvmFieldIndex(parent_ty, field_index, target, &ty_buf)) |llvm_field_index| { + bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); + const indices: [2]*llvm.Value = .{ + llvm_u32.constInt(0, .False), + llvm_u32.constInt(llvm_field_index, .False), + }; + break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + } else { + bitcast_needed = !parent_ty.eql(ptr_child_ty, dg.module); + const llvm_index = llvm_u32.constInt(@boolToInt(parent_ty.hasRuntimeBitsIgnoreComptime()), .False); + const indices: [1]*llvm.Value = .{llvm_index}; + break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + } }, .Pointer => { assert(parent_ty.isSlice()); @@ -4117,7 +4124,7 @@ pub const DeclGen = struct { else => unreachable, }; if (bitcast_needed) { - return llvm_ptr.constBitCast((try dg.lowerType(ptr_child_ty)).pointerType(0)); + return llvm_ptr.constBitCast((try dg.lowerPtrElemTy(ptr_child_ty)).pointerType(0)); } else { return llvm_ptr; } @@ -9766,8 +9773,8 @@ pub const FuncGen = struct { // end of the struct. Treat our struct pointer as an array of two and get // the index to the element at index `1` to get a pointer to the end of // the struct. - const llvm_usize = try self.dg.lowerType(Type.usize); - const llvm_index = llvm_usize.constInt(1, .False); + const llvm_u32 = self.dg.context.intType(32); + const llvm_index = llvm_u32.constInt(@boolToInt(struct_ty.hasRuntimeBitsIgnoreComptime()), .False); const indices: [1]*llvm.Value = .{llvm_index}; return self.builder.buildInBoundsGEP(struct_llvm_ty, struct_ptr, &indices, indices.len, ""); } |
