stage2: integer-backed packed structs

This implements #10113 for the self-hosted compiler only. It removes the
ability to override alignment of packed struct fields, and removes the
ability to put pointers and arrays inside packed structs.

After this commit, nearly all the behavior tests pass for the stage2 llvm
backend that involve packed structs.

I didn't implement the compile errors or compile error tests yet. I'm
waiting until we have stage2 building itself and then I want to rework
the compile error test harness with inspiration from Vexu's arocc test
harness. At that point it should be a much nicer dev experience to work
on compile errors.
This commit is contained in:
Andrew Kelley
2022-02-23 23:27:49 -07:00
parent 65c0475970
commit 6249a24e81
13 changed files with 514 additions and 644 deletions

View File

@@ -886,15 +886,6 @@ pub const Struct = struct {
offset: u32,
is_comptime: bool,
/// Returns the field alignment, assuming the struct is packed.
pub fn packedAlignment(field: Field) u32 {
if (field.abi_align.tag() == .abi_align_default) {
return 0;
} else {
return @intCast(u32, field.abi_align.toUnsignedInt());
}
}
/// Returns the field alignment, assuming the struct is not packed.
pub fn normalAlignment(field: Field, target: Target) u32 {
if (field.abi_align.tag() == .abi_align_default) {
@@ -985,6 +976,31 @@ pub const Struct = struct {
=> true,
};
}
pub fn packedFieldBitOffset(s: Struct, target: Target, index: usize) u16 {
assert(s.layout == .Packed);
assert(s.haveFieldTypes());
var bit_sum: u64 = 0;
for (s.fields.values()) |field, i| {
if (i == index) {
return @intCast(u16, bit_sum);
}
bit_sum += field.ty.bitSize(target);
}
return @intCast(u16, bit_sum);
}
pub fn packedIntegerBits(s: Struct, target: Target) u16 {
return s.packedFieldBitOffset(target, s.fields.count());
}
pub fn packedIntegerType(s: Struct, target: Target, buf: *Type.Payload.Bits) Type {
buf.* = .{
.base = .{ .tag = .int_unsigned },
.data = s.packedIntegerBits(target),
};
return Type.initPayload(&buf.base);
}
};
/// Represents the data that an enum declaration provides, when the fields