Skip to content

Commit

Permalink
llvm debug info builder: mark packed fields as BitFields
Browse files Browse the repository at this point in the history
Now you can use them perfectly from GDB! (And imperfectly from LLDB)
  • Loading branch information
tau-dev committed Jul 22, 2024
1 parent 8c50a79 commit 7ac9698
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ pub const Object = struct {
ptr_size * 8,
(ptr_align.toByteUnits() orelse 0) * 8,
0, // Offset
false, // Bitfield
);

const debug_len_type = try o.builder.debugMemberType(
Expand All @@ -2048,6 +2049,7 @@ pub const Object = struct {
len_size * 8,
(len_align.toByteUnits() orelse 0) * 8,
len_offset * 8,
false, // Bitfield
);

const debug_slice_type = try o.builder.debugStructType(
Expand Down Expand Up @@ -2207,6 +2209,7 @@ pub const Object = struct {
payload_size * 8,
(payload_align.toByteUnits() orelse 0) * 8,
0, // Offset
false, // Bitfield
);

const debug_some_type = try o.builder.debugMemberType(
Expand All @@ -2218,6 +2221,7 @@ pub const Object = struct {
non_null_size * 8,
(non_null_align.toByteUnits() orelse 0) * 8,
non_null_offset * 8,
false, // Bitfield
);

const debug_optional_type = try o.builder.debugStructType(
Expand Down Expand Up @@ -2288,6 +2292,7 @@ pub const Object = struct {
error_size * 8,
(error_align.toByteUnits() orelse 0) * 8,
error_offset * 8,
false, // Bitfield
);
fields[payload_index] = try o.builder.debugMemberType(
try o.builder.metadataString("value"),
Expand All @@ -2298,6 +2303,7 @@ pub const Object = struct {
payload_size * 8,
(payload_align.toByteUnits() orelse 0) * 8,
payload_offset * 8,
false, // Bitfield
);

const debug_error_union_type = try o.builder.debugStructType(
Expand Down Expand Up @@ -2599,6 +2605,7 @@ pub const Object = struct {
field_size,
field_align,
field_offset,
is_packed,
));
}

Expand Down Expand Up @@ -2656,6 +2663,7 @@ pub const Object = struct {
bit_size,
1,
bit_offset,
true,
));
}
} else {
Expand All @@ -2682,6 +2690,7 @@ pub const Object = struct {
field_ty.abiSize(pt) * 8,
(byte_alignment.toByteUnits() orelse 0) * 8,
ty.structFieldOffset(field_index, pt) * 8,
false,
));
}
}
Expand Down Expand Up @@ -2758,6 +2767,7 @@ pub const Object = struct {
field_size * 8,
(field_align.toByteUnits() orelse 0) * 8,
0, // Offset
false, // Bitfield
));
}

Expand Down Expand Up @@ -2805,6 +2815,7 @@ pub const Object = struct {
layout.tag_size * 8,
(layout.tag_align.toByteUnits() orelse 0) * 8,
tag_offset * 8,
false, // Bitfield
);

const debug_payload_type = try o.builder.debugMemberType(
Expand All @@ -2816,6 +2827,7 @@ pub const Object = struct {
layout.payload_size * 8,
(layout.payload_align.toByteUnits() orelse 0) * 8,
payload_offset * 8,
false, // Bitfield
);

const full_fields: [2]Builder.Metadata =
Expand Down
44 changes: 27 additions & 17 deletions src/codegen/llvm/Builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7651,6 +7651,7 @@ pub const Metadata = enum(u32) {
composite_vector_type,
derived_pointer_type,
derived_member_type,
derived_member_type_bitfield,
derived_typedef,
imported_declaration,
subroutine_type,
Expand Down Expand Up @@ -7698,6 +7699,7 @@ pub const Metadata = enum(u32) {
.composite_vector_type,
.derived_pointer_type,
.derived_member_type,
.derived_member_type_bitfield,
.derived_typedef,
.imported_declaration,
.subroutine_type,
Expand Down Expand Up @@ -10116,6 +10118,7 @@ pub fn printUnbuffered(
},
.derived_pointer_type,
.derived_member_type,
.derived_member_type_bitfield,
.derived_typedef,
=> |kind| {
const extra = self.metadataExtraData(Metadata.DerivedType, metadata_item.data);
Expand All @@ -10126,7 +10129,7 @@ pub fn printUnbuffered(
DW_TAG_typedef,
}, switch (kind) {
.derived_pointer_type => .DW_TAG_pointer_type,
.derived_member_type => .DW_TAG_member,
.derived_member_type, .derived_member_type_bitfield => .DW_TAG_member,
.derived_typedef => .DW_TAG_typedef,
else => unreachable,
}),
Expand All @@ -10144,7 +10147,7 @@ pub fn printUnbuffered(
0 => null,
else => |bit_offset| bit_offset,
},
.flags = null,
.flags = Metadata.DIFlags{ .BitField = metadata_item.tag == .derived_member_type_bitfield },
.extraData = null,
.dwarfAddressSpace = null,
.annotations = null,
Expand Down Expand Up @@ -12006,6 +12009,7 @@ pub fn debugMemberType(
size_in_bits: u64,
align_in_bits: u64,
offset_in_bits: u64,
is_bitffield: bool,
) Allocator.Error!Metadata {
try self.ensureUnusedMetadataCapacity(1, Metadata.DerivedType, 0);
return self.debugMemberTypeAssumeCapacity(
Expand All @@ -12017,6 +12021,7 @@ pub fn debugMemberType(
size_in_bits,
align_in_bits,
offset_in_bits,
is_bitffield,
);
}

Expand Down Expand Up @@ -12606,21 +12611,25 @@ fn debugMemberTypeAssumeCapacity(
size_in_bits: u64,
align_in_bits: u64,
offset_in_bits: u64,
is_bitffield: bool,
) Metadata {
assert(!self.strip);
return self.metadataSimpleAssumeCapacity(.derived_member_type, Metadata.DerivedType{
.name = name,
.file = file,
.scope = scope,
.line = line,
.underlying_type = underlying_type,
.size_in_bits_lo = @truncate(size_in_bits),
.size_in_bits_hi = @truncate(size_in_bits >> 32),
.align_in_bits_lo = @truncate(align_in_bits),
.align_in_bits_hi = @truncate(align_in_bits >> 32),
.offset_in_bits_lo = @truncate(offset_in_bits),
.offset_in_bits_hi = @truncate(offset_in_bits >> 32),
});
return self.metadataSimpleAssumeCapacity(
if (is_bitffield) .derived_member_type_bitfield else .derived_member_type,
Metadata.DerivedType{
.name = name,
.file = file,
.scope = scope,
.line = line,
.underlying_type = underlying_type,
.size_in_bits_lo = @truncate(size_in_bits),
.size_in_bits_hi = @truncate(size_in_bits >> 32),
.align_in_bits_lo = @truncate(align_in_bits),
.align_in_bits_hi = @truncate(align_in_bits >> 32),
.offset_in_bits_lo = @truncate(offset_in_bits),
.offset_in_bits_hi = @truncate(offset_in_bits >> 32),
},
);
}

fn debugSubroutineTypeAssumeCapacity(
Expand Down Expand Up @@ -14010,13 +14019,14 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
},
.derived_pointer_type,
.derived_member_type,
.derived_member_type_bitfield,
.derived_typedef,
=> |kind| {
const extra = self.metadataExtraData(Metadata.DerivedType, data);
try metadata_block.writeAbbrevAdapted(MetadataBlock.DerivedType{
.tag = switch (kind) {
.derived_pointer_type => DW.TAG.pointer_type,
.derived_member_type => DW.TAG.member,
.derived_member_type, .derived_member_type_bitfield => DW.TAG.member,
.derived_typedef => DW.TAG.typedef,
else => unreachable,
},
Expand All @@ -14029,7 +14039,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
.align_in_bits = extra.bitAlign(),
.offset_in_bits = extra.bitOffset(),
.flags = .{
.StaticMember = false,
.BitField = tag == .derived_member_type_bitfield,
},
}, metadata_adapter);
},
Expand Down

0 comments on commit 7ac9698

Please sign in to comment.