mirror of
https://github.com/ziglang/zig.git
synced 2025-12-29 17:43:17 +00:00
Merge pull request #14000 from jacobly0/zero-bit-fields
codegen: fix taking the address of a field in a zero-bit struct
This commit is contained in:
commit
0fb53bd245
@ -3291,7 +3291,7 @@ pub fn nativeToBig(comptime T: type, x: T) T {
|
||||
/// - The delta required to align the pointer is not a multiple of the pointee's
|
||||
/// type.
|
||||
pub fn alignPointerOffset(ptr: anytype, align_to: usize) ?usize {
|
||||
assert(align_to != 0 and @popCount(align_to) == 1);
|
||||
assert(isValidAlign(align_to));
|
||||
|
||||
const T = @TypeOf(ptr);
|
||||
const info = @typeInfo(T);
|
||||
@ -3751,6 +3751,7 @@ pub fn alignForwardLog2(addr: usize, log2_alignment: u8) usize {
|
||||
/// The alignment must be a power of 2 and greater than 0.
|
||||
/// Asserts that rounding up the address does not cause integer overflow.
|
||||
pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {
|
||||
assert(isValidAlignGeneric(T, alignment));
|
||||
return alignBackwardGeneric(T, addr + (alignment - 1), alignment);
|
||||
}
|
||||
|
||||
@ -3846,7 +3847,7 @@ test "alignForward" {
|
||||
/// Round an address down to the previous (or current) aligned address.
|
||||
/// Unlike `alignBackward`, `alignment` can be any positive number, not just a power of 2.
|
||||
pub fn alignBackwardAnyAlign(i: usize, alignment: usize) usize {
|
||||
if (@popCount(alignment) == 1)
|
||||
if (isValidAlign(alignment))
|
||||
return alignBackward(i, alignment);
|
||||
assert(alignment != 0);
|
||||
return i - @mod(i, alignment);
|
||||
@ -3861,7 +3862,7 @@ pub fn alignBackward(addr: usize, alignment: usize) usize {
|
||||
/// Round an address down to the previous (or current) aligned address.
|
||||
/// The alignment must be a power of 2 and greater than 0.
|
||||
pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T {
|
||||
assert(@popCount(alignment) == 1);
|
||||
assert(isValidAlignGeneric(T, alignment));
|
||||
// 000010000 // example alignment
|
||||
// 000001111 // subtract 1
|
||||
// 111110000 // binary not
|
||||
@ -3871,11 +3872,17 @@ pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T {
|
||||
/// Returns whether `alignment` is a valid alignment, meaning it is
|
||||
/// a positive power of 2.
|
||||
pub fn isValidAlign(alignment: usize) bool {
|
||||
return @popCount(alignment) == 1;
|
||||
return isValidAlignGeneric(usize, alignment);
|
||||
}
|
||||
|
||||
/// Returns whether `alignment` is a valid alignment, meaning it is
|
||||
/// a positive power of 2.
|
||||
pub fn isValidAlignGeneric(comptime T: type, alignment: T) bool {
|
||||
return alignment > 0 and std.math.isPowerOfTwo(alignment);
|
||||
}
|
||||
|
||||
pub fn isAlignedAnyAlign(i: usize, alignment: usize) bool {
|
||||
if (@popCount(alignment) == 1)
|
||||
if (isValidAlign(alignment))
|
||||
return isAligned(i, alignment);
|
||||
assert(alignment != 0);
|
||||
return 0 == @mod(i, alignment);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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, "");
|
||||
}
|
||||
|
||||
@ -2915,8 +2915,16 @@ pub const Value = extern union {
|
||||
.field_ptr => val.castTag(.field_ptr).?.data.container_ptr.isVariable(mod),
|
||||
.eu_payload_ptr => val.castTag(.eu_payload_ptr).?.data.container_ptr.isVariable(mod),
|
||||
.opt_payload_ptr => val.castTag(.opt_payload_ptr).?.data.container_ptr.isVariable(mod),
|
||||
.decl_ref => mod.declPtr(val.castTag(.decl_ref).?.data).val.isVariable(mod),
|
||||
.decl_ref_mut => mod.declPtr(val.castTag(.decl_ref_mut).?.data.decl_index).val.isVariable(mod),
|
||||
.decl_ref => {
|
||||
const decl = mod.declPtr(val.castTag(.decl_ref).?.data);
|
||||
assert(decl.has_tv);
|
||||
return decl.val.isVariable(mod);
|
||||
},
|
||||
.decl_ref_mut => {
|
||||
const decl = mod.declPtr(val.castTag(.decl_ref_mut).?.data.decl_index);
|
||||
assert(decl.has_tv);
|
||||
return decl.val.isVariable(mod);
|
||||
},
|
||||
|
||||
.variable => true,
|
||||
else => false,
|
||||
|
||||
@ -39,5 +39,7 @@ test "fixed" {
|
||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
|
||||
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_llvm and
|
||||
builtin.cpu.arch == .aarch64 and builtin.os.tag == .windows) return error.SkipZigTest;
|
||||
ArgSerializer.serializeCommand(GET.init("banana"));
|
||||
}
|
||||
|
||||
@ -1359,23 +1359,53 @@ test "under-aligned struct field" {
|
||||
try expect(result == 1234);
|
||||
}
|
||||
|
||||
test "address of zero-bit field is equal to address of only field" {
|
||||
test "fieldParentPtr of a zero-bit field" {
|
||||
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
|
||||
|
||||
{
|
||||
const A = struct { b: void = {}, u: u8 };
|
||||
var a = A{ .u = 0 };
|
||||
const a_ptr = @fieldParentPtr(A, "b", &a.b);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
{
|
||||
const A = struct { u: u8, b: void = {} };
|
||||
var a = A{ .u = 0 };
|
||||
const a_ptr = @fieldParentPtr(A, "b", &a.b);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
const S = struct {
|
||||
fn testStruct(comptime A: type) !void {
|
||||
{
|
||||
const a = A{ .u = 0 };
|
||||
const b_ptr = &a.b;
|
||||
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
{
|
||||
var a = A{ .u = 0 };
|
||||
const b_ptr = &a.b;
|
||||
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
}
|
||||
fn testNestedStruct(comptime A: type) !void {
|
||||
{
|
||||
const a = A{ .u = 0 };
|
||||
const c_ptr = &a.b.c;
|
||||
const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr);
|
||||
try std.testing.expectEqual(&a.b, b_ptr);
|
||||
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
{
|
||||
var a = A{ .u = 0 };
|
||||
const c_ptr = &a.b.c;
|
||||
const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr);
|
||||
try std.testing.expectEqual(&a.b, b_ptr);
|
||||
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||
try std.testing.expectEqual(&a, a_ptr);
|
||||
}
|
||||
}
|
||||
fn doTheTest() !void {
|
||||
try testStruct(struct { b: void = {}, u: u8 });
|
||||
try testStruct(struct { u: u8, b: void = {} });
|
||||
try testNestedStruct(struct { b: struct { c: void = {} } = .{}, u: u8 });
|
||||
try testNestedStruct(struct { u: u8, b: struct { c: void = {} } = .{} });
|
||||
}
|
||||
};
|
||||
try S.doTheTest();
|
||||
comptime try S.doTheTest();
|
||||
}
|
||||
|
||||
test "struct field has a pointer to an aligned version of itself" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user