authorgravatar for jonathan.haehne@hotmail.comTau <jonathan.haehne@hotmail.com> 2024-06-29 16:19:55+02:00
committergravatar for jonathan.haehne@hotmail.comTau <jonathan.haehne@hotmail.com> 2024-07-19 17:51:38+02:00
log94cf4d2d8193efb0ae642d475fb8437eb4c7e8c1
tree7e1f334cbf1941e1a9f2452bf42679cd467da9bb
parent177b3359a16016caf080ff95ba4b9359251d04b8

llvm: add pass-by-reference info to debug types

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.

2 files changed, 44 insertions(+), 3 deletions(-)

src/codegen/llvm.zig+10
...@@ -2068,6 +2068,7 @@ pub const Object = struct {...@@ -2068,6 +2068,7 @@ pub const Object = struct {
2068 debug_ptr_type,2068 debug_ptr_type,
2069 debug_len_type,2069 debug_len_type,
2070 }),2070 }),
2071 isByRef(ty, pt),
2071 );2072 );
20722073
2073 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_slice_type);2074 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_slice_type);
...@@ -2237,6 +2238,7 @@ pub const Object = struct {...@@ -2237,6 +2238,7 @@ pub const Object = struct {
2237 debug_data_type,2238 debug_data_type,
2238 debug_some_type,2239 debug_some_type,
2239 }),2240 }),
2241 isByRef(ty, pt),
2240 );2242 );
22412243
2242 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_optional_type);2244 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_optional_type);
...@@ -2313,6 +2315,7 @@ pub const Object = struct {...@@ -2313,6 +2315,7 @@ pub const Object = struct {
2313 ty.abiSize(pt) * 8,2315 ty.abiSize(pt) * 8,
2314 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,2316 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2315 try o.builder.debugTuple(&fields),2317 try o.builder.debugTuple(&fields),
2318 isByRef(ty, pt),
2316 );2319 );
23172320
2318 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_error_union_type);2321 o.builder.debugForwardReferenceSetType(debug_fwd_ref, debug_error_union_type);
...@@ -2542,6 +2545,7 @@ pub const Object = struct {...@@ -2542,6 +2545,7 @@ pub const Object = struct {
2542 0, // Size2545 0, // Size
2543 0, // Align2546 0, // Align
2544 .none, // Fields2547 .none, // Fields
2548 false, // ByRef
2545 );2549 );
2546 break :res debug_opaque_type;2550 break :res debug_opaque_type;
2547 },2551 },
...@@ -2601,6 +2605,7 @@ pub const Object = struct {...@@ -2601,6 +2605,7 @@ pub const Object = struct {
2601 ty.abiSize(pt) * 8,2605 ty.abiSize(pt) * 8,
2602 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,2606 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2603 try o.builder.debugTuple(fields.items),2607 try o.builder.debugTuple(fields.items),
2608 isByRef(ty, pt),
2604 );2609 );
26052610
2606 break :res debug_struct_type;2611 break :res debug_struct_type;
...@@ -2656,6 +2661,7 @@ pub const Object = struct {...@@ -2656,6 +2661,7 @@ pub const Object = struct {
2656 ty.abiSize(pt) * 8,2661 ty.abiSize(pt) * 8,
2657 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,2662 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2658 try o.builder.debugTuple(fields.items),2663 try o.builder.debugTuple(fields.items),
2664 isByRef(ty, pt),
2659 );2665 );
26602666
2661 break :res debug_struct_type;2667 break :res debug_struct_type;
...@@ -2683,6 +2689,7 @@ pub const Object = struct {...@@ -2683,6 +2689,7 @@ pub const Object = struct {
2683 try o.builder.debugTuple(2689 try o.builder.debugTuple(
2684 &.{try o.lowerDebugType(Type.fromInterned(union_type.enum_tag_ty), required_by_runtime)},2690 &.{try o.lowerDebugType(Type.fromInterned(union_type.enum_tag_ty), required_by_runtime)},
2685 ),2691 ),
2692 isByRef(ty, pt),
2686 );2693 );
26872694
2688 break :res debug_union_type;2695 break :res debug_union_type;
...@@ -2736,6 +2743,7 @@ pub const Object = struct {...@@ -2736,6 +2743,7 @@ pub const Object = struct {
2736 ty.abiSize(pt) * 8,2743 ty.abiSize(pt) * 8,
2737 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,2744 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2738 try o.builder.debugTuple(fields.items),2745 try o.builder.debugTuple(fields.items),
2746 isByRef(ty, pt),
2739 );2747 );
27402748
2741 if (layout.tag_size == 0) {2749 if (layout.tag_size == 0) {
...@@ -2791,6 +2799,7 @@ pub const Object = struct {...@@ -2791,6 +2799,7 @@ pub const Object = struct {
2791 ty.abiSize(pt) * 8,2799 ty.abiSize(pt) * 8,
2792 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,2800 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2793 try o.builder.debugTuple(&full_fields),2801 try o.builder.debugTuple(&full_fields),
2802 isByRef(ty, pt),
2794 );2803 );
27952804
2796 break :res debug_tagged_union_type;2805 break :res debug_tagged_union_type;
...@@ -2824,6 +2833,7 @@ pub const Object = struct {...@@ -2824,6 +2833,7 @@ pub const Object = struct {
2824 0,2833 0,
2825 0,2834 0,
2826 if (fields.len == 0) .none else try o.builder.debugTuple(fields),2835 if (fields.len == 0) .none else try o.builder.debugTuple(fields),
2836 false, // is_byref
2827 );2837 );
2828 }2838 }
28292839
src/codegen/llvm/Builder.zig+34-3
...@@ -7911,6 +7911,10 @@ pub const Metadata = enum(u32) {...@@ -7911,6 +7911,10 @@ pub const Metadata = enum(u32) {
7911 align_in_bits_lo: u32,7911 align_in_bits_lo: u32,
7912 align_in_bits_hi: u32,7912 align_in_bits_hi: u32,
7913 fields_tuple: Metadata,7913 fields_tuple: Metadata,
7914 flags: packed struct(u32) {
7915 is_byref: bool,
7916 pad: u31 = 0,
7917 },
79147918
7915 pub fn bitSize(self: CompositeType) u64 {7919 pub fn bitSize(self: CompositeType) u64 {
7916 return @as(u64, self.size_in_bits_hi) << 32 | self.size_in_bits_lo;7920 return @as(u64, self.size_in_bits_hi) << 32 | self.size_in_bits_lo;
...@@ -11638,7 +11642,17 @@ fn addMetadataExtraAssumeCapacity(self: *Builder, extra: anytype) Metadata.Item....@@ -11638,7 +11642,17 @@ fn addMetadataExtraAssumeCapacity(self: *Builder, extra: anytype) Metadata.Item.
11638 u32 => value,11642 u32 => value,
11639 MetadataString, Metadata, Variable.Index, Value => @intFromEnum(value),11643 MetadataString, Metadata, Variable.Index, Value => @intFromEnum(value),
11640 Metadata.DIFlags => @bitCast(value),11644 Metadata.DIFlags => @bitCast(value),
11641 else => @compileError("bad field type: " ++ @typeName(field.type)),11645 else => blk: {
11646 switch (@typeInfo(field.type)) {
11647 .Struct => |s| {
11648 if (s.backing_integer == u32)
11649 break :blk @bitCast(value);
11650 @compileLog(s.layout, s.backing_integer);
11651 },
11652 else => {},
11653 }
11654 @compileError("bad field type: " ++ @typeName(field.type));
11655 },
11642 });11656 });
11643 }11657 }
11644 return result;11658 return result;
...@@ -11677,7 +11691,7 @@ fn metadataExtraDataTrail(...@@ -11677,7 +11691,7 @@ fn metadataExtraDataTrail(
11677 u32 => value,11691 u32 => value,
11678 MetadataString, Metadata, Variable.Index, Value => @enumFromInt(value),11692 MetadataString, Metadata, Variable.Index, Value => @enumFromInt(value),
11679 Metadata.DIFlags => @bitCast(value),11693 Metadata.DIFlags => @bitCast(value),
11680 else => @compileError("bad field type: " ++ @typeName(field.type)),11694 else => @bitCast(value),
11681 };11695 };
11682 return .{11696 return .{
11683 .data = result,11697 .data = result,
...@@ -11844,6 +11858,7 @@ pub fn debugStructType(...@@ -11844,6 +11858,7 @@ pub fn debugStructType(
11844 size_in_bits: u64,11858 size_in_bits: u64,
11845 align_in_bits: u64,11859 align_in_bits: u64,
11846 fields_tuple: Metadata,11860 fields_tuple: Metadata,
11861 is_byref: bool,
11847) Allocator.Error!Metadata {11862) Allocator.Error!Metadata {
11848 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);11863 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11849 return self.debugStructTypeAssumeCapacity(11864 return self.debugStructTypeAssumeCapacity(
...@@ -11855,6 +11870,7 @@ pub fn debugStructType(...@@ -11855,6 +11870,7 @@ pub fn debugStructType(
11855 size_in_bits,11870 size_in_bits,
11856 align_in_bits,11871 align_in_bits,
11857 fields_tuple,11872 fields_tuple,
11873 is_byref,
11858 );11874 );
11859}11875}
1186011876
...@@ -11868,6 +11884,7 @@ pub fn debugUnionType(...@@ -11868,6 +11884,7 @@ pub fn debugUnionType(
11868 size_in_bits: u64,11884 size_in_bits: u64,
11869 align_in_bits: u64,11885 align_in_bits: u64,
11870 fields_tuple: Metadata,11886 fields_tuple: Metadata,
11887 is_byref: bool,
11871) Allocator.Error!Metadata {11888) Allocator.Error!Metadata {
11872 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);11889 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11873 return self.debugUnionTypeAssumeCapacity(11890 return self.debugUnionTypeAssumeCapacity(
...@@ -11879,6 +11896,7 @@ pub fn debugUnionType(...@@ -11879,6 +11896,7 @@ pub fn debugUnionType(
11879 size_in_bits,11896 size_in_bits,
11880 align_in_bits,11897 align_in_bits,
11881 fields_tuple,11898 fields_tuple,
11899 is_byref,
11882 );11900 );
11883}11901}
1188411902
...@@ -12400,6 +12418,7 @@ fn debugStructTypeAssumeCapacity(...@@ -12400,6 +12418,7 @@ fn debugStructTypeAssumeCapacity(
12400 size_in_bits: u64,12418 size_in_bits: u64,
12401 align_in_bits: u64,12419 align_in_bits: u64,
12402 fields_tuple: Metadata,12420 fields_tuple: Metadata,
12421 is_byref: bool,
12403) Metadata {12422) Metadata {
12404 assert(!self.strip);12423 assert(!self.strip);
12405 return self.debugCompositeTypeAssumeCapacity(12424 return self.debugCompositeTypeAssumeCapacity(
...@@ -12412,6 +12431,7 @@ fn debugStructTypeAssumeCapacity(...@@ -12412,6 +12431,7 @@ fn debugStructTypeAssumeCapacity(
12412 size_in_bits,12431 size_in_bits,
12413 align_in_bits,12432 align_in_bits,
12414 fields_tuple,12433 fields_tuple,
12434 is_byref,
12415 );12435 );
12416}12436}
1241712437
...@@ -12425,6 +12445,7 @@ fn debugUnionTypeAssumeCapacity(...@@ -12425,6 +12445,7 @@ fn debugUnionTypeAssumeCapacity(
12425 size_in_bits: u64,12445 size_in_bits: u64,
12426 align_in_bits: u64,12446 align_in_bits: u64,
12427 fields_tuple: Metadata,12447 fields_tuple: Metadata,
12448 is_byref: bool,
12428) Metadata {12449) Metadata {
12429 assert(!self.strip);12450 assert(!self.strip);
12430 return self.debugCompositeTypeAssumeCapacity(12451 return self.debugCompositeTypeAssumeCapacity(
...@@ -12437,6 +12458,7 @@ fn debugUnionTypeAssumeCapacity(...@@ -12437,6 +12458,7 @@ fn debugUnionTypeAssumeCapacity(
12437 size_in_bits,12458 size_in_bits,
12438 align_in_bits,12459 align_in_bits,
12439 fields_tuple,12460 fields_tuple,
12461 is_byref,
12440 );12462 );
12441}12463}
1244212464
...@@ -12462,6 +12484,7 @@ fn debugEnumerationTypeAssumeCapacity(...@@ -12462,6 +12484,7 @@ fn debugEnumerationTypeAssumeCapacity(
12462 size_in_bits,12484 size_in_bits,
12463 align_in_bits,12485 align_in_bits,
12464 fields_tuple,12486 fields_tuple,
12487 false, // is_byref
12465 );12488 );
12466}12489}
1246712490
...@@ -12487,6 +12510,7 @@ fn debugArrayTypeAssumeCapacity(...@@ -12487,6 +12510,7 @@ fn debugArrayTypeAssumeCapacity(
12487 size_in_bits,12510 size_in_bits,
12488 align_in_bits,12511 align_in_bits,
12489 fields_tuple,12512 fields_tuple,
12513 size_in_bits > 0, // is_byref
12490 );12514 );
12491}12515}
1249212516
...@@ -12512,6 +12536,7 @@ fn debugVectorTypeAssumeCapacity(...@@ -12512,6 +12536,7 @@ fn debugVectorTypeAssumeCapacity(
12512 size_in_bits,12536 size_in_bits,
12513 align_in_bits,12537 align_in_bits,
12514 fields_tuple,12538 fields_tuple,
12539 false,
12515 );12540 );
12516}12541}
1251712542
...@@ -12526,6 +12551,7 @@ fn debugCompositeTypeAssumeCapacity(...@@ -12526,6 +12551,7 @@ fn debugCompositeTypeAssumeCapacity(
12526 size_in_bits: u64,12551 size_in_bits: u64,
12527 align_in_bits: u64,12552 align_in_bits: u64,
12528 fields_tuple: Metadata,12553 fields_tuple: Metadata,
12554 is_byref: bool,
12529) Metadata {12555) Metadata {
12530 assert(!self.strip);12556 assert(!self.strip);
12531 return self.metadataSimpleAssumeCapacity(tag, Metadata.CompositeType{12557 return self.metadataSimpleAssumeCapacity(tag, Metadata.CompositeType{
...@@ -12539,6 +12565,7 @@ fn debugCompositeTypeAssumeCapacity(...@@ -12539,6 +12565,7 @@ fn debugCompositeTypeAssumeCapacity(
12539 .align_in_bits_lo = @truncate(align_in_bits),12565 .align_in_bits_lo = @truncate(align_in_bits),
12540 .align_in_bits_hi = @truncate(align_in_bits >> 32),12566 .align_in_bits_hi = @truncate(align_in_bits >> 32),
12541 .fields_tuple = fields_tuple,12567 .fields_tuple = fields_tuple,
12568 .flags = .{ .is_byref = is_byref },
12542 });12569 });
12543}12570}
1254412571
...@@ -13973,7 +14000,11 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -13973,7 +14000,11 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
13973 .underlying_type = extra.underlying_type,14000 .underlying_type = extra.underlying_type,
13974 .size_in_bits = extra.bitSize(),14001 .size_in_bits = extra.bitSize(),
13975 .align_in_bits = extra.bitAlign(),14002 .align_in_bits = extra.bitAlign(),
13976 .flags = if (kind == .composite_vector_type) .{ .Vector = true } else .{},14003 .flags = .{
14004 .Vector = kind == .composite_vector_type,
14005 .EnumClass = kind == .composite_enumeration_type,
14006 .TypePassbyReference = extra.flags.is_byref,
14007 },
13977 .elements = extra.fields_tuple,14008 .elements = extra.fields_tuple,
13978 }, metadata_adapter);14009 }, metadata_adapter);
13979 },14010 },