Skip to content

Commit

Permalink
llvm: add pass-by-reference info to debug types
Browse files Browse the repository at this point in the history
Without this data, debugger expressions try to pass structs by-value,
which mostly just crashes.
Also: mark enums as enum classes to prevent the enumerators from
shadowing other identifiers.
  • Loading branch information
tau-dev committed Jul 7, 2024
1 parent 41f1f35 commit f90cc32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,7 @@ pub const Object = struct {
debug_ptr_type,
debug_len_type,
}),
isByRef(ty, zcu),
);

o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_slice_type);
Expand Down Expand Up @@ -2227,6 +2228,7 @@ pub const Object = struct {
debug_data_type,
debug_some_type,
}),
isByRef(ty, zcu),
);

o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_optional_type);
Expand Down Expand Up @@ -2303,6 +2305,7 @@ pub const Object = struct {
ty.abiSize(zcu) * 8,
(ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8,
try o.builder.debugTuple(&fields),
isByRef(ty, zcu),
);

o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_error_union_type);
Expand Down Expand Up @@ -2531,6 +2534,7 @@ pub const Object = struct {
0, // Size
0, // Align
.none, // Fields
false, // ByRef
);
break :res debug_opaque_type;
},
Expand Down Expand Up @@ -2590,6 +2594,7 @@ pub const Object = struct {
ty.abiSize(zcu) * 8,
(ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8,
try o.builder.debugTuple(fields.items),
isByRef(ty, zcu),
);

break :res debug_struct_type;
Expand Down Expand Up @@ -2645,6 +2650,7 @@ pub const Object = struct {
ty.abiSize(zcu) * 8,
(ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8,
try o.builder.debugTuple(fields.items),
isByRef(ty, zcu),
);

break :res debug_struct_type;
Expand Down Expand Up @@ -2672,6 +2678,7 @@ pub const Object = struct {
try o.builder.debugTuple(
&.{try o.lowerDebugType(Type.fromInterned(union_type.enum_tag_ty), required_by_runtime)},
),
isByRef(ty, zcu),
);

break :res debug_union_type;
Expand Down Expand Up @@ -2725,6 +2732,7 @@ pub const Object = struct {
ty.abiSize(zcu) * 8,
(ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8,
try o.builder.debugTuple(fields.items),
isByRef(ty, zcu),
);

if (layout.tag_size == 0) {
Expand Down Expand Up @@ -2780,6 +2788,7 @@ pub const Object = struct {
ty.abiSize(zcu) * 8,
(ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8,
try o.builder.debugTuple(&full_fields),
isByRef(ty, zcu),
);

break :res debug_tagged_union_type;
Expand Down Expand Up @@ -2813,6 +2822,7 @@ pub const Object = struct {
0,
0,
if (fields.len == 0) .none else try o.builder.debugTuple(fields),
false, // is_byref
);
}

Expand Down
37 changes: 34 additions & 3 deletions src/codegen/llvm/Builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7911,6 +7911,10 @@ pub const Metadata = enum(u32) {
align_in_bits_lo: u32,
align_in_bits_hi: u32,
fields_tuple: Metadata,
flags: packed struct(u32) {
is_byref: bool,
pad: u31 = 0,
},

pub fn bitSize(self: CompositeType) u64 {
return @as(u64, self.size_in_bits_hi) << 32 | self.size_in_bits_lo;
Expand Down Expand Up @@ -11638,7 +11642,17 @@ fn addMetadataExtraAssumeCapacity(self: *Builder, extra: anytype) Metadata.Item.
u32 => value,
MetadataString, Metadata, Variable.Index, Value => @intFromEnum(value),
Metadata.DIFlags => @bitCast(value),
else => @compileError("bad field type: " ++ @typeName(field.type)),
else => blk: {
switch (@typeInfo(field.type)) {
.Struct => |s| {
if (s.backing_integer == u32)
break :blk @bitCast(value);
@compileLog(s.layout, s.backing_integer);
},
else => {},
}
@compileError("bad field type: " ++ @typeName(field.type));
},
});
}
return result;
Expand Down Expand Up @@ -11677,7 +11691,7 @@ fn metadataExtraDataTrail(
u32 => value,
MetadataString, Metadata, Variable.Index, Value => @enumFromInt(value),
Metadata.DIFlags => @bitCast(value),
else => @compileError("bad field type: " ++ @typeName(field.type)),
else => @bitCast(value),
};
return .{
.data = result,
Expand Down Expand Up @@ -11844,6 +11858,7 @@ pub fn debugStructType(
size_in_bits: u64,
align_in_bits: u64,
fields_tuple: Metadata,
is_byref: bool,
) Allocator.Error!Metadata {
try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
return self.debugStructTypeAssumeCapacity(
Expand All @@ -11855,6 +11870,7 @@ pub fn debugStructType(
size_in_bits,
align_in_bits,
fields_tuple,
is_byref,
);
}

Expand All @@ -11868,6 +11884,7 @@ pub fn debugUnionType(
size_in_bits: u64,
align_in_bits: u64,
fields_tuple: Metadata,
is_byref: bool,
) Allocator.Error!Metadata {
try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
return self.debugUnionTypeAssumeCapacity(
Expand All @@ -11879,6 +11896,7 @@ pub fn debugUnionType(
size_in_bits,
align_in_bits,
fields_tuple,
is_byref,
);
}

Expand Down Expand Up @@ -12400,6 +12418,7 @@ fn debugStructTypeAssumeCapacity(
size_in_bits: u64,
align_in_bits: u64,
fields_tuple: Metadata,
is_byref: bool,
) Metadata {
assert(!self.strip);
return self.debugCompositeTypeAssumeCapacity(
Expand All @@ -12412,6 +12431,7 @@ fn debugStructTypeAssumeCapacity(
size_in_bits,
align_in_bits,
fields_tuple,
is_byref,
);
}

Expand All @@ -12425,6 +12445,7 @@ fn debugUnionTypeAssumeCapacity(
size_in_bits: u64,
align_in_bits: u64,
fields_tuple: Metadata,
is_byref: bool,
) Metadata {
assert(!self.strip);
return self.debugCompositeTypeAssumeCapacity(
Expand All @@ -12437,6 +12458,7 @@ fn debugUnionTypeAssumeCapacity(
size_in_bits,
align_in_bits,
fields_tuple,
is_byref,
);
}

Expand All @@ -12462,6 +12484,7 @@ fn debugEnumerationTypeAssumeCapacity(
size_in_bits,
align_in_bits,
fields_tuple,
false, // is_byref
);
}

Expand All @@ -12487,6 +12510,7 @@ fn debugArrayTypeAssumeCapacity(
size_in_bits,
align_in_bits,
fields_tuple,
size_in_bits > 0, // is_byref
);
}

Expand All @@ -12512,6 +12536,7 @@ fn debugVectorTypeAssumeCapacity(
size_in_bits,
align_in_bits,
fields_tuple,
false,
);
}

Expand All @@ -12526,6 +12551,7 @@ fn debugCompositeTypeAssumeCapacity(
size_in_bits: u64,
align_in_bits: u64,
fields_tuple: Metadata,
is_byref: bool,
) Metadata {
assert(!self.strip);
return self.metadataSimpleAssumeCapacity(tag, Metadata.CompositeType{
Expand All @@ -12539,6 +12565,7 @@ fn debugCompositeTypeAssumeCapacity(
.align_in_bits_lo = @truncate(align_in_bits),
.align_in_bits_hi = @truncate(align_in_bits >> 32),
.fields_tuple = fields_tuple,
.flags = .{ .is_byref = is_byref },
});
}

Expand Down Expand Up @@ -13973,7 +14000,11 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
.underlying_type = extra.underlying_type,
.size_in_bits = extra.bitSize(),
.align_in_bits = extra.bitAlign(),
.flags = if (kind == .composite_vector_type) .{ .Vector = true } else .{},
.flags = .{
.Vector = kind == .composite_vector_type,
.EnumClass = kind == .composite_enumeration_type,
.TypePassbyReference = extra.flags.is_byref,
},
.elements = extra.fields_tuple,
}, metadata_adapter);
},
Expand Down

0 comments on commit f90cc32

Please sign in to comment.