authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-08-19 23:43:02-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-03 12:59:38-04:00
logc41b7d1b850ba998bd253461536d25b06bcb41cb
tree6f2f495542a469c15f32faf6a656b938e487c856
parent84c2fd3d781b8843dfd3565139282fddf60e4e97

Dwarf2: start implementing decl debug info


7 files changed, 992 insertions(+), 460 deletions(-)

lib/std/dwarf/TAG.zig+2-2
......@@ -40,8 +40,8 @@ pub const namelist = 0x2b;
4040pub const namelist_item = 0x2c;
4141pub const packed_type = 0x2d;
4242pub const subprogram = 0x2e;
43pub const template_type_param = 0x2f;
44pub const template_value_param = 0x30;
43pub const template_type_parameter = 0x2f;
44pub const template_value_parameter = 0x30;
4545pub const thrown_type = 0x31;
4646pub const try_block = 0x32;
4747pub const variant_part = 0x33;
src/Type.zig+16-8
......@@ -109,6 +109,20 @@ pub const Class = enum(u3) {
109109 /// Then, aggregates containing fully-comptime types may themselves be either fully-comptime or
110110 /// partially-comptime; see the doc comment on `.partially_comptime` for details.
111111 fully_comptime,
112
113 pub fn hasRuntimeBits(class: Class) bool {
114 return switch (class) {
115 .no_possible_value, .one_possible_value, .fully_comptime => false,
116 .runtime, .partially_comptime => true,
117 };
118 }
119
120 pub fn comptimeOnly(class: Class) bool {
121 return switch (class) {
122 .no_possible_value, .one_possible_value, .runtime => false,
123 .partially_comptime, .fully_comptime => true,
124 };
125 }
112126};
113127
114128/// Returns the `Class` for the type `ty`. Asserts that the layout of `ty` is resolved.
......@@ -761,10 +775,7 @@ pub fn toValue(self: Type) Value {
761775///
762776/// * All other types contain some runtime state, so have runtime bits and a non-zero ABI size.
763777pub fn hasRuntimeBits(ty: Type, zcu: *const Zcu) bool {
764 return switch (ty.classify(zcu)) {
765 .no_possible_value, .one_possible_value, .fully_comptime => false,
766 .runtime, .partially_comptime => true,
767 };
778 return ty.classify(zcu).hasRuntimeBits();
768779}
769780
770781/// Returns `true` iff the memory layout of `ty` is defined by the Zig language specification.
......@@ -2195,10 +2206,7 @@ pub fn onePossibleValue(ty: Type, pt: Zcu.PerThread) !?Value {
21952206pub fn comptimeOnly(ty: Type, zcu: *const Zcu) bool {
21962207 if (ty.toIntern() == .generic_poison_type) return false;
21972208 if (ty.zigTypeTag(zcu) == .error_union and ty.errorUnionPayload(zcu).toIntern() == .generic_poison_type) return false;
2198 return switch (ty.classify(zcu)) {
2199 .no_possible_value, .one_possible_value, .runtime => false,
2200 .partially_comptime, .fully_comptime => true,
2201 };
2209 return ty.classify(zcu).comptimeOnly();
22022210}
22032211
22042212pub fn isVector(ty: Type, zcu: *const Zcu) bool {
src/link.zig+2-1
......@@ -26,9 +26,10 @@ const target_util = @import("target.zig");
2626const codegen = @import("codegen.zig");
2727const crash_report = @import("crash_report.zig");
2828
29pub const ConstPool = @import("link/ConstPool.zig");
2930pub const LdScript = @import("link/LdScript.zig");
31pub const MappedFile = @import("link/MappedFile.zig");
3032pub const Queue = @import("link/Queue.zig");
31pub const ConstPool = @import("link/ConstPool.zig");
3233
3334pub const aarch64 = @import("link/aarch64.zig");
3435pub const loongarch = @import("link/loongarch.zig");
src/link/ConstPool.zig+1-1
......@@ -134,7 +134,7 @@ pub fn updateContainerType(
134134 const gpa = pt.zcu.comp.gpa;
135135 try pool.complete_containers.put(gpa, container_ty, {});
136136 } else {
137 _ = pool.complete_containers.fetchSwapRemove(container_ty);
137 _ = pool.complete_containers.swapRemove(container_ty);
138138 }
139139 var opt_dep = pool.container_deps.get(container_ty);
140140 while (opt_dep) |dep| : (opt_dep = dep.ptr(pool).next.unwrap()) {
src/link/Dwarf.zig+49-49
......@@ -2202,7 +2202,7 @@ pub const WipNav = struct {
22022202 wip_nav: *WipNav,
22032203 abbrev_code: struct {
22042204 decl: AbbrevCode,
2205 decl_abstract: AbbrevCode,
2205 decl_specification: AbbrevCode,
22062206 decl_instance: AbbrevCode,
22072207 },
22082208 nav: *const InternPool.Nav,
......@@ -2216,11 +2216,11 @@ pub const WipNav = struct {
22162216
22172217 const orig_entry = wip_nav.entry;
22182218 defer wip_nav.entry = orig_entry;
2219 const parent_type, const is_abstract = if (nav.analysis) |analysis| parent_info: {
2219 const parent_type, const is_specification = if (nav.analysis) |analysis| parent_info: {
22202220 const parent_type: Type = .fromInterned(zcu.namespacePtr(analysis.namespace).owner_type);
22212221 const decl_gop = try dwarf.decls.getOrPut(dwarf.gpa, analysis.zir_index);
22222222 errdefer _ = if (!decl_gop.found_existing) dwarf.decls.pop();
2223 const was_abstract = decl_gop.found_existing and
2223 const was_specification = decl_gop.found_existing and
22242224 switch (try dwarf.debug_info.declAbbrevCode(wip_nav.unit, decl_gop.value_ptr.*)) {
22252225 .null,
22262226 .decl_alias,
......@@ -2242,9 +2242,9 @@ pub const WipNav = struct {
22422242 .decl_extern_nullary_func,
22432243 .decl_extern_func,
22442244 => false,
2245 .decl_abstract_var,
2246 .decl_abstract_const,
2247 .decl_abstract_func,
2245 .decl_specification_var,
2246 .decl_specification_const,
2247 .decl_specification_func,
22482248 => true,
22492249
22502250 // This comes from a decl which was previously generated as an incomplete value
......@@ -2255,11 +2255,11 @@ pub const WipNav = struct {
22552255 else => |t| std.debug.panic("bad decl abbrev code: {t}", .{t}),
22562256 };
22572257 if (parent_type.getCaptures(zcu).len == 0) {
2258 if (was_abstract) try dwarf.freeCommonEntry(wip_nav.unit, decl_gop.value_ptr.*);
2258 if (was_specification) try dwarf.freeCommonEntry(wip_nav.unit, decl_gop.value_ptr.*);
22592259 decl_gop.value_ptr.* = orig_entry;
22602260 break :parent_info .{ parent_type, false };
22612261 } else {
2262 if (was_abstract)
2262 if (was_specification)
22632263 dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(decl_gop.value_ptr.*).clear()
22642264 else
22652265 decl_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
......@@ -2268,8 +2268,8 @@ pub const WipNav = struct {
22682268 }
22692269 } else .{ null, false };
22702270
2271 try wip_nav.abbrevCode(if (is_abstract) abbrev_code.decl_abstract else abbrev_code.decl);
2272 try wip_nav.refType((if (is_abstract) null else parent_type) orelse
2271 try wip_nav.abbrevCode(if (is_specification) abbrev_code.decl_specification else abbrev_code.decl);
2272 try wip_nav.refType((if (is_specification) null else parent_type) orelse
22732273 .fromInterned(zcu.fileRootType(file)));
22742274 assert(diw.end == DebugInfo.declEntryLineOff(dwarf));
22752275 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
......@@ -2277,14 +2277,14 @@ pub const WipNav = struct {
22772277 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
22782278 try wip_nav.strp(nav.name.toSlice(ip));
22792279
2280 if (!is_abstract) return;
2281 const abstract_entry = wip_nav.entry;
2282 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, abstract_entry, dwarf, wip_nav.debug_info.written());
2280 if (!is_specification) return;
2281 const specification_entry = wip_nav.entry;
2282 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, specification_entry, dwarf, wip_nav.debug_info.written());
22832283 wip_nav.debug_info.clearRetainingCapacity();
22842284 wip_nav.entry = orig_entry;
22852285 try wip_nav.abbrevCode(abbrev_code.decl_instance);
22862286 try wip_nav.refType(parent_type.?);
2287 try wip_nav.infoSectionOffset(.debug_info, wip_nav.unit, abstract_entry, 0);
2287 try wip_nav.infoSectionOffset(.debug_info, wip_nav.unit, specification_entry, 0);
22882288 }
22892289};
22902290
......@@ -2680,11 +2680,11 @@ fn initWipNavInner(
26802680 const diw = &wip_nav.debug_info.writer;
26812681 try wip_nav.declCommon(if (func_type.param_types.len > 0 or func_type.is_var_args) .{
26822682 .decl = .decl_extern_func,
2683 .decl_abstract = .decl_abstract_func,
2683 .decl_specification = .decl_specification_func,
26842684 .decl_instance = .decl_instance_extern_func,
26852685 } else .{
26862686 .decl = .decl_extern_nullary_func,
2687 .decl_abstract = .decl_abstract_func,
2687 .decl_specification = .decl_specification_func,
26882688 .decl_instance = .decl_instance_extern_nullary_func,
26892689 }, &nav, inst_info.file, &decl);
26902690 try wip_nav.strp(@"extern".name.toSlice(ip));
......@@ -2705,7 +2705,7 @@ fn initWipNavInner(
27052705 .func => |func| if (func.owner_nav != nav_index) {
27062706 try wip_nav.declCommon(.{
27072707 .decl = .decl_alias,
2708 .decl_abstract = .decl_abstract_const,
2708 .decl_specification = .decl_specification_const,
27092709 .decl_instance = .decl_instance_alias,
27102710 }, &nav, inst_info.file, &decl);
27112711 try wip_nav.refNav(func.owner_nav);
......@@ -2758,7 +2758,7 @@ fn initWipNavInner(
27582758 const diw = &wip_nav.debug_info.writer;
27592759 try wip_nav.declCommon(.{
27602760 .decl = .decl_func,
2761 .decl_abstract = .decl_abstract_func,
2761 .decl_specification = .decl_specification_func,
27622762 .decl_instance = .decl_instance_func,
27632763 }, &nav, inst_info.file, &decl);
27642764 try wip_nav.strp(switch (decl.linkage) {
......@@ -2817,10 +2817,10 @@ fn initWipNavInner(
28172817 const diw = &wip_nav.debug_info.writer;
28182818 try wip_nav.declCommon(.{
28192819 .decl = .decl_var,
2820 .decl_abstract = switch (decl.kind) {
2820 .decl_specification = switch (decl.kind) {
28212821 .unnamed_test, .@"test", .decltest, .@"comptime" => unreachable,
2822 .@"const" => .decl_abstract_const,
2823 .@"var" => .decl_abstract_var,
2822 .@"const" => .decl_specification_const,
2823 .@"var" => .decl_specification_var,
28242824 },
28252825 .decl_instance = .decl_instance_var,
28262826 }, &nav, inst_info.file, &decl);
......@@ -3181,7 +3181,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31813181 .alias => {
31823182 try wip_nav.declCommon(.{
31833183 .decl = .decl_alias,
3184 .decl_abstract = .decl_abstract_const,
3184 .decl_specification = .decl_specification_const,
31853185 .decl_instance = .decl_instance_alias,
31863186 }, &nav, inst_info.file, &decl);
31873187 try wip_nav.refType(nav_val.toType());
......@@ -3189,7 +3189,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31893189 .@"var" => {
31903190 try wip_nav.declCommon(.{
31913191 .decl = .decl_var,
3192 .decl_abstract = .decl_abstract_var,
3192 .decl_specification = .decl_specification_var,
31933193 .decl_instance = .decl_instance_var,
31943194 }, &nav, inst_info.file, &decl);
31953195 try wip_nav.strp(switch (decl.linkage) {
......@@ -3209,19 +3209,19 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32093209 const has_comptime_state = nav_ty.comptimeOnly(zcu);
32103210 try wip_nav.declCommon(if (has_runtime_bits and has_comptime_state) .{
32113211 .decl = .decl_const_runtime_bits_comptime_state,
3212 .decl_abstract = .decl_abstract_const,
3212 .decl_specification = .decl_specification_const,
32133213 .decl_instance = .decl_instance_const_runtime_bits_comptime_state,
32143214 } else if (has_comptime_state) .{
32153215 .decl = .decl_const_comptime_state,
3216 .decl_abstract = .decl_abstract_const,
3216 .decl_specification = .decl_specification_const,
32173217 .decl_instance = .decl_instance_const_comptime_state,
32183218 } else if (has_runtime_bits) .{
32193219 .decl = .decl_const_runtime_bits,
3220 .decl_abstract = .decl_abstract_const,
3220 .decl_specification = .decl_specification_const,
32213221 .decl_instance = .decl_instance_const_runtime_bits,
32223222 } else .{
32233223 .decl = .decl_const,
3224 .decl_abstract = .decl_abstract_const,
3224 .decl_specification = .decl_specification_const,
32253225 .decl_instance = .decl_instance_const,
32263226 }, &nav, inst_info.file, &decl);
32273227 try wip_nav.strp(switch (decl.linkage) {
......@@ -3245,11 +3245,11 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32453245 } else true;
32463246 try wip_nav.declCommon(if (is_nullary) .{
32473247 .decl = .decl_nullary_func_generic,
3248 .decl_abstract = .decl_abstract_func,
3248 .decl_specification = .decl_specification_func,
32493249 .decl_instance = .decl_instance_nullary_func_generic,
32503250 } else .{
32513251 .decl = .decl_func_generic,
3252 .decl_abstract = .decl_abstract_func,
3252 .decl_specification = .decl_specification_func,
32533253 .decl_instance = .decl_instance_func_generic,
32543254 }, &nav, inst_info.file, &decl);
32553255 try wip_nav.refType(.fromInterned(func_type.return_type));
......@@ -3267,7 +3267,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32673267 .func_alias => |owner_nav| {
32683268 try wip_nav.declCommon(.{
32693269 .decl = .decl_alias,
3270 .decl_abstract = .decl_abstract_const,
3270 .decl_specification = .decl_specification_const,
32713271 .decl_instance = .decl_instance_alias,
32723272 }, &nav, inst_info.file, &decl);
32733273 try wip_nav.refNav(owner_nav);
......@@ -3463,7 +3463,7 @@ fn emitIncompleteContainerType(
34633463 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
34643464 try wip_nav.declCommon(.{
34653465 .decl = .decl_namespace_struct,
3466 .decl_abstract = .decl_abstract_const,
3466 .decl_specification = .decl_specification_const,
34673467 .decl_instance = .decl_instance_namespace_struct,
34683468 }, &nav, file, &decl);
34693469 try wip_nav.debug_info.writer.writeByte(@intFromBool(true));
......@@ -3881,11 +3881,11 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
38813881 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
38823882 try wip_nav.declCommon(if (loaded_struct.field_types.len == 0) .{
38833883 .decl = .decl_namespace_struct,
3884 .decl_abstract = .decl_abstract_const,
3884 .decl_specification = .decl_specification_const,
38853885 .decl_instance = .decl_instance_namespace_struct,
38863886 } else .{
38873887 .decl = .decl_struct,
3888 .decl_abstract = .decl_abstract_const,
3888 .decl_specification = .decl_specification_const,
38893889 .decl_instance = .decl_instance_struct,
38903890 }, &nav, file, &decl);
38913891 } else {
......@@ -3960,7 +3960,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
39603960 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
39613961 try wip_nav.declCommon(.{
39623962 .decl = .decl_packed_struct,
3963 .decl_abstract = .decl_abstract_const,
3963 .decl_specification = .decl_specification_const,
39643964 .decl_instance = .decl_instance_packed_struct,
39653965 }, &nav, file, &decl);
39663966 break :t true;
......@@ -3997,7 +3997,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
39973997 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
39983998 try wip_nav.declCommon(.{
39993999 .decl = .decl_union,
4000 .decl_abstract = .decl_abstract_const,
4000 .decl_specification = .decl_specification_const,
40014001 .decl_instance = .decl_instance_union,
40024002 }, &nav, file, &decl);
40034003 break :t true;
......@@ -4059,7 +4059,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
40594059 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
40604060 try wip_nav.declCommon(.{
40614061 .decl = .decl_packed_union,
4062 .decl_abstract = .decl_abstract_const,
4062 .decl_specification = .decl_specification_const,
40634063 .decl_instance = .decl_instance_packed_union,
40644064 }, &nav, file, &decl);
40654065 break :t true;
......@@ -4092,11 +4092,11 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
40924092 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
40934093 try wip_nav.declCommon(if (loaded_enum.field_names.len > 0) .{
40944094 .decl = .decl_enum,
4095 .decl_abstract = .decl_abstract_const,
4095 .decl_specification = .decl_specification_const,
40964096 .decl_instance = .decl_instance_enum,
40974097 } else .{
40984098 .decl = .decl_empty_enum,
4099 .decl_abstract = .decl_abstract_const,
4099 .decl_specification = .decl_specification_const,
41004100 .decl_instance = .decl_instance_empty_enum,
41014101 }, &nav, file, &decl);
41024102 } else {
......@@ -4134,7 +4134,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
41344134 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
41354135 try wip_nav.declCommon(.{
41364136 .decl = .decl_namespace_struct,
4137 .decl_abstract = .decl_abstract_const,
4137 .decl_specification = .decl_specification_const,
41384138 .decl_instance = .decl_instance_namespace_struct,
41394139 }, &nav, file, &decl);
41404140 } else {
......@@ -5136,9 +5136,9 @@ const AbbrevCode = enum {
51365136 decl_func_generic,
51375137 decl_extern_nullary_func,
51385138 decl_extern_func,
5139 decl_abstract_var,
5140 decl_abstract_const,
5141 decl_abstract_func,
5139 decl_specification_var,
5140 decl_specification_const,
5141 decl_specification_func,
51425142 decl_instance_alias,
51435143 decl_instance_empty_enum,
51445144 decl_instance_enum,
......@@ -5262,7 +5262,7 @@ const AbbrevCode = enum {
52625262 .{ .accessibility, .data1 },
52635263 .{ .name, .strp },
52645264 };
5265 const decl_abstract_abbrev_common_attrs = decl_abbrev_common_attrs ++ &[_]Attr{
5265 const decl_specification_abbrev_common_attrs = decl_abbrev_common_attrs ++ &[_]Attr{
52665266 .{ .declaration, .flag_present },
52675267 };
52685268 const decl_instance_abbrev_common_attrs = &[_]Attr{
......@@ -5447,17 +5447,17 @@ const AbbrevCode = enum {
54475447 .{ .noreturn, .flag },
54485448 },
54495449 },
5450 .decl_abstract_var = .{
5450 .decl_specification_var = .{
54515451 .tag = .variable,
5452 .attrs = decl_abstract_abbrev_common_attrs,
5452 .attrs = decl_specification_abbrev_common_attrs,
54535453 },
5454 .decl_abstract_const = .{
5454 .decl_specification_const = .{
54555455 .tag = .constant,
5456 .attrs = decl_abstract_abbrev_common_attrs,
5456 .attrs = decl_specification_abbrev_common_attrs,
54575457 },
5458 .decl_abstract_func = .{
5458 .decl_specification_func = .{
54595459 .tag = .subprogram,
5460 .attrs = decl_abstract_abbrev_common_attrs,
5460 .attrs = decl_specification_abbrev_common_attrs,
54615461 },
54625462 .decl_instance_alias = .{
54635463 .tag = .imported_declaration,
src/link/Dwarf2.zig+784-351
......@@ -9,7 +9,11 @@ units: []Unit,
99consts: std.ArrayList(Const),
1010globals: std.array_hash_map.Auto(InternPool.Nav.Index, Global),
1111funcs: std.array_hash_map.Auto(InternPool.Nav.Index, Func),
12decls: std.array_hash_map.Auto(InternPool.TrackedInst.Index, MappedFile.Node.Index),
12decls: std.array_hash_map.Auto(InternPool.TrackedInst.Index, Decl),
13pending_decl: ?struct {
14 di: Decl.Index,
15 instance_val: InternPool.Index,
16},
1317
1418debug_abbrev: Abbrev,
1519frame: Frame,
......@@ -135,6 +139,22 @@ pub const Func = struct {
135139 };
136140};
137141
142pub const Decl = struct {
143 debug_info_ni: MappedFile.Node.Index.Optional,
144
145 pub const Index = enum(u32) {
146 _,
147
148 pub fn srcInst(di: Decl.Index, dwarf: *Dwarf) InternPool.TrackedInst.Index {
149 return dwarf.decls.keys()[@backingInt(di)];
150 }
151
152 pub fn get(di: Decl.Index, dwarf: *Dwarf) *Decl {
153 return &dwarf.decls.values()[@backingInt(di)];
154 }
155 };
156};
157
138158pub const Frame = struct {
139159 header: Header,
140160
......@@ -632,29 +652,33 @@ pub const WipNav = struct {
632652 }
633653 fn startFuncDebugInfoInner(debug: *Debug) link.EmitError!void {
634654 const dwarf = debug.wip_nav.dwarf;
635 const zcu = debug.pt.zcu;
655 const pt = debug.pt;
656 const zcu = pt.zcu;
636657 const ip = &zcu.intern_pool;
637658 const func = zcu.funcInfo(debug.wip_nav.func);
659 const nav = ip.getNav(func.owner_nav);
638660 const func_type = ip.indexToKey(func.ty).func_type;
639 const inst_info = ip.getNav(func.owner_nav).srcInst(ip).resolveFull(ip).?;
661 const inst_info = nav.srcInst(ip).resolveFull(ip).?;
640662 const zf = zcu.fileByIndex(inst_info.file);
641 const mod = zf.mod.?;
642 const target = &mod.resolved_target.result;
663 const target = &zf.mod.?.resolved_target.result;
643664 const decl = zf.zir.?.getDeclaration(inst_info.inst);
644 const nav = ip.getNav(func.owner_nav);
645 const diw = &debug.info_writer.interface;
665 const di_nw = &debug.info_writer;
666 const diw = &di_nw.interface;
646667 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_func));
647 try debug.refType(.fromInterned(zcu.fileRootType(inst_info.file)));
668 try dwarf.refConst(pt, di_nw, .fromInterned(ip.namespacePtr(switch (func.generic_owner) {
669 .none => nav,
670 else => |generic_owner| ip.getNav(zcu.funcInfo(generic_owner).owner_nav),
671 }.analysis.?.namespace).owner_type));
648672 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
649673 try diw.writeUleb128(decl.src_column + 1);
650674 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
651 try debug.strp(nav.name.toSlice(ip));
652 try debug.strp(switch (decl.linkage) {
675 try dwarf.strp(&dwarf.debug_str, di_nw, nav.name.toSlice(ip));
676 try dwarf.strp(&dwarf.debug_str, di_nw, switch (decl.linkage) {
653677 .normal => nav.fqn,
654678 .@"extern", .@"export" => nav.name,
655679 }.toSlice(ip));
656 try debug.refType(.fromInterned(func_type.return_type));
657 try dwarf.symbolAddress(&debug.info_writer, debug.wip_nav.func_si, 0);
680 try dwarf.refConst(pt, di_nw, .fromInterned(func_type.return_type));
681 try dwarf.symbolAddress(di_nw, debug.wip_nav.func_si, 0);
658682 debug.info_func_length_offset = diw.end;
659683 try diw.writeInt(u32, undefined, dwarf.endian);
660684 try diw.writeUleb128(
......@@ -756,12 +780,14 @@ pub const WipNav = struct {
756780 loc: Loc,
757781 ) link.EmitError!void {
758782 assert(debug.wip_nav.func != .none);
759 try debug.abbrevCode(switch (tag) {
783 const dwarf = debug.wip_nav.dwarf;
784 const di_nw = &debug.info_writer;
785 try di_nw.interface.writeUleb128(try dwarf.refAbbrevCode(switch (tag) {
760786 .arg => if (opt_name) |_| .arg else .unnamed_arg,
761787 .local_var => if (opt_name) |_| .local_var else unreachable,
762 });
763 if (opt_name) |name| try debug.strp(name);
764 if (false) try debug.refType(ty);
788 }));
789 if (opt_name) |name| try dwarf.strp(&dwarf.debug_str, di_nw, name);
790 try dwarf.refConst(debug.pt, di_nw, ty.toValue());
765791 try debug.infoExprLoc(loc);
766792 debug.any_children = true;
767793 }
......@@ -785,27 +811,38 @@ pub const WipNav = struct {
785811 val: Value,
786812 ) link.EmitError!void {
787813 assert(debug.wip_nav.func != .none);
814 const dwarf = debug.wip_nav.dwarf;
815 const pt = debug.pt;
788816 const zcu = debug.pt.zcu;
789817 const ty = val.typeOf(zcu);
790 const has_runtime_bits = ty.hasRuntimeBits(zcu);
791 const has_comptime_state = ty.comptimeOnly(zcu);
792 try debug.abbrevCode(if (has_runtime_bits and has_comptime_state) switch (tag) {
793 .comptime_arg => if (opt_name) |_| .comptime_arg_runtime_bits_comptime_state else .unnamed_comptime_arg_runtime_bits_comptime_state,
794 .local_const => if (opt_name) |_| .local_const_runtime_bits_comptime_state else unreachable,
795 } else if (has_comptime_state) switch (tag) {
796 .comptime_arg => if (opt_name) |_| .comptime_arg_comptime_state else .unnamed_comptime_arg_comptime_state,
797 .local_const => if (opt_name) |_| .local_const_comptime_state else unreachable,
798 } else if (has_runtime_bits) switch (tag) {
799 .comptime_arg => if (opt_name) |_| .comptime_arg_runtime_bits else .unnamed_comptime_arg_runtime_bits,
800 .local_const => if (opt_name) |_| .local_const_runtime_bits else unreachable,
801 } else switch (tag) {
802 .comptime_arg => if (opt_name) |_| .comptime_arg else .unnamed_comptime_arg,
803 .local_const => if (opt_name) |_| .local_const else unreachable,
804 });
805 if (opt_name) |name| try debug.strp(name);
806 if (false) try debug.refType(ty);
807 if (has_runtime_bits) try debug.blockValue(val);
808 if (false and has_comptime_state) try debug.refValue(val);
818 const ty_class = ty.classify(zcu);
819 const di_nw = &debug.info_writer;
820 try di_nw.interface.writeUleb128(try dwarf.refAbbrevCode(switch (tag) {
821 .comptime_arg => if (opt_name) |_| switch (ty_class) {
822 .no_possible_value => unreachable,
823 .one_possible_value => .comptime_arg,
824 .runtime => .comptime_arg_fully_runtime,
825 .partially_comptime => .comptime_arg_partially_comptime,
826 .fully_comptime => .comptime_arg_fully_comptime,
827 } else switch (ty_class) {
828 .no_possible_value => unreachable,
829 .one_possible_value => .unnamed_comptime_arg,
830 .runtime => .unnamed_comptime_arg_fully_runtime,
831 .partially_comptime => .unnamed_comptime_arg_partially_comptime,
832 .fully_comptime => .unnamed_comptime_arg_fully_comptime,
833 },
834 .local_const => if (opt_name) |_| switch (ty_class) {
835 .no_possible_value => unreachable,
836 .one_possible_value => .local_const,
837 .runtime => .local_const_fully_runtime,
838 .partially_comptime => .local_const_partially_comptime,
839 .fully_comptime => .local_const_fully_comptime,
840 } else unreachable,
841 }));
842 if (opt_name) |name| try dwarf.strp(&dwarf.debug_str, di_nw, name);
843 try dwarf.refConst(pt, di_nw, ty.toValue());
844 if (ty_class.hasRuntimeBits()) try dwarf.blockConst(pt, di_nw, val);
845 if (ty_class.comptimeOnly()) try dwarf.refConst(pt, di_nw, val);
809846 debug.any_children = true;
810847 }
811848
......@@ -817,7 +854,7 @@ pub const WipNav = struct {
817854 }
818855 fn genVarArgsDebugInfoInner(debug: *Debug) link.EmitError!void {
819856 assert(debug.wip_nav.func != .none);
820 try debug.abbrevCode(.is_var_args);
857 try debug.info_writer.interface.writeUleb128(try debug.wip_nav.dwarf.refAbbrevCode(.is_var_args));
821858 debug.any_children = true;
822859 }
823860
......@@ -927,11 +964,11 @@ pub const WipNav = struct {
927964 }
928965 fn enterBlockInner(debug: *Debug, code_off: usize) link.EmitError!void {
929966 const dwarf = debug.wip_nav.dwarf;
930 const diw = &debug.info_writer.interface;
931967 const block = try debug.blocks.addOne(dwarf.lf.comp.gpa);
932968
969 const diw = &debug.info_writer.interface;
933970 block.abbrev_code = @intCast(diw.end);
934 try debug.abbrevCode(.block);
971 try diw.writeUleb128(try dwarf.refAbbrevCode(.block));
935972 block.low_pc_off = code_off;
936973 try debug.infoAddrSym(debug.wip_nav.func_si, code_off);
937974 block.high_pc = @intCast(diw.end);
......@@ -947,14 +984,14 @@ pub const WipNav = struct {
947984 }
948985 fn leaveBlockInner(debug: *Debug, code_off: usize) link.EmitError!void {
949986 const dwarf = debug.wip_nav.dwarf;
950 const block_bytes = comptime uleb128Size(@backingInt(AbbrevCode.block));
987 const block_size = comptime uleb128Size(@backingInt(AbbrevCode.block));
951988 const block = debug.blocks.pop().?;
952989 if (debug.any_children)
953990 try debug.info_writer.interface.writeUleb128(@backingInt(AbbrevCode.null))
954991 else
955992 std.leb.writeUnsignedFixed(
956 block_bytes,
957 debug.info_writer.interface.buffered()[block.abbrev_code..][0..block_bytes],
993 block_size,
994 debug.info_writer.interface.buffered()[block.abbrev_code..][0..block_size],
958995 @intCast(try dwarf.refAbbrevCode(.empty_block)),
959996 );
960997 std.mem.writeInt(
......@@ -987,11 +1024,11 @@ pub const WipNav = struct {
9871024 ) link.EmitError!void {
9881025 const dwarf = debug.wip_nav.dwarf;
9891026 const zcu = debug.pt.zcu;
990 const diw = &debug.info_writer.interface;
9911027 const block = try debug.blocks.addOne(zcu.gpa);
9921028
1029 const diw = &debug.info_writer.interface;
9931030 block.abbrev_code = @intCast(diw.end);
994 try debug.abbrevCode(.inlined_func);
1031 try diw.writeUleb128(try dwarf.refAbbrevCode(.inlined_func));
9951032 try debug.refFunc(func);
9961033 try diw.writeUleb128((if (zcu.comp.config.incremental)
9971034 0
......@@ -1019,15 +1056,15 @@ pub const WipNav = struct {
10191056 code_off: usize,
10201057 ) link.EmitError!void {
10211058 const dwarf = debug.wip_nav.dwarf;
1022 const inlined_func_bytes = comptime uleb128Size(@backingInt(AbbrevCode.inlined_func));
1059 const inlined_func_size = comptime uleb128Size(@backingInt(AbbrevCode.inlined_func));
10231060 const block = debug.blocks.pop().?;
10241061 const diw = &debug.info_writer.interface;
10251062 if (debug.any_children)
10261063 try diw.writeUleb128(@backingInt(AbbrevCode.null))
10271064 else
10281065 std.leb.writeUnsignedFixed(
1029 inlined_func_bytes,
1030 diw.buffered()[block.abbrev_code..][0..inlined_func_bytes],
1066 inlined_func_size,
1067 diw.buffered()[block.abbrev_code..][0..inlined_func_size],
10311068 @intCast(try dwarf.refAbbrevCode(.empty_inlined_func)),
10321069 );
10331070 std.mem.writeInt(
......@@ -1091,24 +1128,6 @@ pub const WipNav = struct {
10911128 debug.wip_nav.func = func;
10921129 }
10931130
1094 fn abbrevCode(debug: *Debug, abbrev_code: AbbrevCode) link.EmitError!void {
1095 try debug.info_writer.interface.writeUleb128(
1096 try debug.wip_nav.dwarf.refAbbrevCode(abbrev_code),
1097 );
1098 }
1099
1100 fn strp(debug: *Debug, str: []const u8) link.EmitError!void {
1101 const dwarf = debug.wip_nav.dwarf;
1102 try dwarf.strp(&dwarf.debug_str, &debug.info_writer, str);
1103 }
1104
1105 fn strpFmt(debug: *Debug, comptime fmt: []const u8, args: anytype) link.EmitError!void {
1106 const gpa = debug.pt.zcu.gpa;
1107 const str = try gpa.print(fmt, args);
1108 defer gpa.free(str);
1109 try debug.strp(str);
1110 }
1111
11121131 fn infoExprLoc(debug: *Debug, loc: Loc) link.EmitError!void {
11131132 var buf: [64]u8 = undefined;
11141133 var counter: ExprLocCounter = .init(debug.wip_nav.dwarf, &buf);
......@@ -1146,45 +1165,6 @@ pub const WipNav = struct {
11461165 0,
11471166 );
11481167 }
1149
1150 fn refType(debug: *Debug, ty: Type) link.EmitError!void {
1151 return debug.refValue(ty.toValue());
1152 }
1153
1154 fn refValue(debug: *Debug, val: Value) link.EmitError!void {
1155 const dwarf = debug.wip_nav.dwarf;
1156 const cpi = try dwarf.getConst(debug.pt, val);
1157 try dwarf.sectionOffset(
1158 &debug.info_writer,
1159 Const.get(cpi, dwarf).debug_info_ni.unwrap().?,
1160 0,
1161 );
1162 }
1163
1164 fn blockValue(debug: *Debug, val: Value) link.EmitError!void {
1165 const ty = val.typeOf(debug.pt.zcu);
1166 const diw = &debug.info_writer.interface;
1167 const size = ty.abiSize(debug.pt.zcu);
1168 try diw.writeUleb128(size);
1169 if (size == 0) return;
1170 const offset = diw.end;
1171 try codegen.generateSymbol(
1172 debug.wip_nav.dwarf.lf,
1173 debug.pt,
1174 val,
1175 diw,
1176 .{ .debug_output = .{ .dwarf2 = debug } },
1177 );
1178 if (offset + size != diw.end) {
1179 std.debug.print("{f} [{}]: {} != {}\n", .{
1180 ty.fmt(debug.pt),
1181 ty.toIntern(),
1182 size,
1183 diw.end - offset,
1184 });
1185 unreachable;
1186 }
1187 }
11881168 };
11891169
11901170 pub fn deinit(wip_nav: *WipNav) void {
......@@ -1271,15 +1251,12 @@ pub const WipNav = struct {
12711251
12721252 const ExprLocCounter = struct {
12731253 dw: Writer.Discarding,
1274 section_offset_bytes: u32,
1254 section_offset_size: usize,
12751255 address_size: AddressSize,
12761256 fn init(dwarf: *Dwarf, buf: []u8) ExprLocCounter {
12771257 return .{
12781258 .dw = .init(buf),
1279 .section_offset_bytes = switch (dwarf.format) {
1280 .@"32" => 4,
1281 .@"64" => 8,
1282 },
1259 .section_offset_size = dwarf.sectionOffsetSize(),
12831260 .address_size = dwarf.address_size,
12841261 };
12851262 }
......@@ -1293,7 +1270,7 @@ pub const WipNav = struct {
12931270 try counter.dw.writer.splatByteAll(undefined, @backingInt(counter.address_size));
12941271 }
12951272 fn infoEntry(counter: *ExprLocCounter, _: MappedFile.Node.Index) Writer.Error!void {
1296 try counter.dw.writer.splatByteAll(undefined, counter.section_offset_bytes);
1273 try counter.dw.writer.splatByteAll(undefined, counter.section_offset_size);
12971274 }
12981275 };
12991276
......@@ -1363,6 +1340,7 @@ pub fn init(lf: *link.File, format: DW.Format) Dwarf {
13631340 .globals = .empty,
13641341 .funcs = .empty,
13651342 .decls = .empty,
1343 .pending_decl = null,
13661344
13671345 .debug_abbrev = .{
13681346 .ni = .none,
......@@ -1486,9 +1464,7 @@ pub fn getUnit(dwarf: *Dwarf, mod: *Module) Unit.Index {
14861464}
14871465
14881466pub fn getConst(dwarf: *Dwarf, pt: Zcu.PerThread, val: Value) link.Error!link.ConstPool.Index {
1489 const zcu = pt.zcu;
1490 const ty = val.typeOf(zcu);
1491 if (ty.toIntern() != .type_type) assert(ty.comptimeOnly(zcu));
1467 assert(val.typeOf(pt.zcu).comptimeOnly(pt.zcu));
14921468 return dwarf.const_pool.get(pt, .{ .elf2 = dwarf.lf.cast(.elf2).? }, val.toIntern());
14931469}
14941470
......@@ -1505,7 +1481,11 @@ pub fn getGlobal(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Global.Ind
15051481 assert(!mod.strip);
15061482 const elf = dwarf.lf.cast(.elf2).?;
15071483 try elf.nodes.ensureUnusedCapacity(gpa, 1);
1508 try elf.dwarf_globals.ensureUnusedCapacity(gpa, 1);
1484 try elf.dwarf_globals.append(gpa, .{
1485 .debug_info_first_target_reloc = .none,
1486 .debug_info_first_node_reloc = .none,
1487 .debug_info_first_symbol_reloc = .none,
1488 });
15091489 const unit = dwarf.getUnit(mod).get(dwarf);
15101490 global_gop.value_ptr.debug_info_ni = .wrap(elf.addNodeAssumeCapacity(
15111491 unit.debug_info_ni.unwrap().?.addFloatingChild(gpa, &elf.mf, .{
......@@ -1518,11 +1498,6 @@ pub fn getGlobal(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Global.Ind
15181498 },
15191499 .{ .global_debug_info = gi },
15201500 ));
1521 elf.dwarf_globals.addOneAssumeCapacity().* = .{
1522 .debug_info_first_target_reloc = .none,
1523 .debug_info_first_node_reloc = .none,
1524 .debug_info_first_symbol_reloc = .none,
1525 };
15261501 return gi;
15271502}
15281503pub fn getGlobalIfExists(dwarf: *Dwarf, nav: InternPool.Nav.Index) ?Global.Index {
......@@ -1542,9 +1517,8 @@ pub fn getFunc(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Func.Index {
15421517 if (func_gop.value_ptr.debug_info_ni != .none) return fi;
15431518 const mod = comp.zcu.?.navFileScope(nav).mod.?;
15441519 const elf = dwarf.lf.cast(.elf2).?;
1545 try elf.dwarf_funcs.ensureUnusedCapacity(gpa, 1);
15461520 try elf.nodes.ensureUnusedCapacity(gpa, 1);
1547 elf.dwarf_funcs.addOneAssumeCapacity().* = .{
1521 try elf.dwarf_funcs.append(gpa, .{
15481522 .frame_fde_first_symbol_reloc = .none,
15491523 .frame_fde_first_node_reloc = .none,
15501524 .debug_info_first_target_reloc = .none,
......@@ -1552,7 +1526,7 @@ pub fn getFunc(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Func.Index {
15521526 .debug_info_first_node_reloc = .none,
15531527 .debug_line_first_symbol_reloc = .none,
15541528 .debug_line_first_node_reloc = .none,
1555 };
1529 });
15561530 if (mod.strip) return fi;
15571531 const unit = dwarf.getUnit(mod).get(dwarf);
15581532 func_gop.value_ptr.debug_info_ni = .wrap(elf.addNodeAssumeCapacity(
......@@ -1572,6 +1546,86 @@ pub fn getFuncIfExists(dwarf: *Dwarf, nav: InternPool.Nav.Index) ?Func.Index {
15721546 return @fromBackingInt(@intCast(dwarf.funcs.getIndex(nav) orelse return null));
15731547}
15741548
1549pub fn getDeclInst(dwarf: *Dwarf, val: InternPool.Index) ?InternPool.TrackedInst.Index {
1550 const ip = &dwarf.lf.comp.zcu.?.intern_pool;
1551 switch (ip.indexToKey(val)) {
1552 else => unreachable,
1553 .struct_type => {
1554 const loaded_struct = ip.loadStructType(val);
1555 if (loaded_struct.captures.len == 0) return null;
1556 return ip.getNav(loaded_struct.name_nav.unwrap() orelse
1557 return loaded_struct.zir_index).srcInst(ip);
1558 },
1559 .enum_type => {
1560 const loaded_enum = ip.loadEnumType(val);
1561 if (loaded_enum.captures.len == 0) return null;
1562 return ip.getNav(loaded_enum.name_nav.unwrap() orelse
1563 return loaded_enum.zir_index.unwrap().?).srcInst(ip);
1564 },
1565 .union_type => {
1566 const loaded_union = ip.loadUnionType(val);
1567 if (loaded_union.captures.len == 0) return null;
1568 return ip.getNav(loaded_union.name_nav.unwrap() orelse
1569 return loaded_union.zir_index).srcInst(ip);
1570 },
1571 .opaque_type => {
1572 const loaded_opaque = ip.loadOpaqueType(val);
1573 if (loaded_opaque.captures.len == 0) return null;
1574 return ip.getNav(loaded_opaque.name_nav.unwrap() orelse
1575 return loaded_opaque.zir_index).srcInst(ip);
1576 },
1577 .func => |func| return ip.getNav(switch (func.generic_owner) {
1578 .none => func.owner_nav,
1579 else => |generic_owner| ip.indexToKey(generic_owner).func.owner_nav,
1580 }).srcInst(ip),
1581 }
1582}
1583pub fn getDecl(
1584 dwarf: *Dwarf,
1585 pt: Zcu.PerThread,
1586 instance_val: InternPool.Index,
1587) link.Error!MappedFile.Node.Index {
1588 assert(dwarf.pending_decl == null);
1589 const comp = dwarf.lf.comp;
1590 const gpa = comp.gpa;
1591 const zcu = pt.zcu;
1592 const ip = &zcu.intern_pool;
1593 const inst = dwarf.getDeclInst(instance_val) orelse {
1594 const cpi = try dwarf.getConst(pt, .fromInterned(instance_val));
1595 return Const.get(cpi, dwarf).debug_info_ni.unwrap().?;
1596 };
1597 const decl_gop = try dwarf.decls.getOrPut(gpa, inst);
1598 if (!decl_gop.found_existing) decl_gop.value_ptr.* = .{
1599 .debug_info_ni = .none,
1600 };
1601 const di: Decl.Index = @fromBackingInt(@intCast(decl_gop.index));
1602 if (decl_gop.value_ptr.debug_info_ni.unwrap()) |debug_info_ni| return debug_info_ni;
1603 dwarf.pending_decl = .{ .di = di, .instance_val = instance_val };
1604 const elf = dwarf.lf.cast(.elf2).?;
1605 try elf.nodes.ensureUnusedCapacity(gpa, 1);
1606 try elf.dwarf_decls.putNoClobber(gpa, di, .{
1607 .debug_info_first_target_reloc = .none,
1608 .debug_info_first_node_reloc = .none,
1609 });
1610 const unit = dwarf.getUnit(zcu.fileByIndex(di.srcInst(dwarf).resolveFile(ip)).mod.?).get(dwarf);
1611 const debug_info_ni = elf.addNodeAssumeCapacity(
1612 unit.debug_info_ni.unwrap().?.addFloatingChild(gpa, &elf.mf, .{
1613 .enable_next_moved = true,
1614 }) catch |err| switch (err) {
1615 else => |e| return e,
1616 error.MappedFileIo => return comp.link_diags.fail("failed to write output file: {t}", .{
1617 elf.mf.io_err.?,
1618 }),
1619 },
1620 .{ .decl_debug_info = di },
1621 );
1622 decl_gop.value_ptr.debug_info_ni = .wrap(debug_info_ni);
1623 return debug_info_ni;
1624}
1625pub fn getDeclIfExists(dwarf: *Dwarf, inst: InternPool.TrackedInst.Index) ?Decl.Index {
1626 return @fromBackingInt(@intCast(dwarf.decls.getIndex(inst) orelse return null));
1627}
1628
15751629pub fn unitLengthSize(dwarf: *Dwarf) usize {
15761630 return switch (dwarf.format) {
15771631 .@"32" => 4,
......@@ -1976,33 +2030,33 @@ pub fn updateComptimeNav(
19762030 .unnamed_test, .@"test", .decltest => return,
19772031 .@"comptime", .@"const", .@"var" => {},
19782032 }
1979 emit: switch (ip.indexToKey(nav_val.toIntern())) {
2033 done: switch (ip.indexToKey(nav_val.toIntern())) {
19802034 .struct_type => {
19812035 const loaded_struct = ip.loadStructType(nav_val.toIntern());
19822036 if (nav_index.toOptional() == loaded_struct.name_nav) {
19832037 _ = try dwarf.const_pool.get(pt, .{ .elf2 = dwarf.lf.cast(.elf2).? }, nav_val.toIntern());
1984 break :emit;
2038 break :done;
19852039 }
19862040 },
19872041 .enum_type => {
19882042 const loaded_enum = ip.loadEnumType(nav_val.toIntern());
19892043 if (nav_index.toOptional() == loaded_enum.name_nav) {
19902044 _ = try dwarf.const_pool.get(pt, .{ .elf2 = dwarf.lf.cast(.elf2).? }, nav_val.toIntern());
1991 break :emit;
2045 break :done;
19922046 }
19932047 },
19942048 .union_type => {
19952049 const loaded_union = ip.loadUnionType(nav_val.toIntern());
19962050 if (nav_index.toOptional() == loaded_union.name_nav) {
19972051 _ = try dwarf.const_pool.get(pt, .{ .elf2 = dwarf.lf.cast(.elf2).? }, nav_val.toIntern());
1998 break :emit;
2052 break :done;
19992053 }
20002054 },
20012055 .opaque_type => {
20022056 const loaded_opaque = ip.loadOpaqueType(nav_val.toIntern());
20032057 if (nav_index.toOptional() == loaded_opaque.name_nav) {
20042058 _ = try dwarf.const_pool.get(pt, .{ .elf2 = dwarf.lf.cast(.elf2).? }, nav_val.toIntern());
2005 break :emit;
2059 break :done;
20062060 }
20072061 },
20082062 .func => |func| if (func.owner_nav == nav_index and func.generic_owner == .none) {
......@@ -2012,12 +2066,11 @@ pub fn updateComptimeNav(
20122066 var di_nw: MappedFile.Node.Writer = undefined;
20132067 f.debug_info_ni.unwrap().?.writer(zcu.gpa, &dwarf.lf.cast(.elf2).?.mf, &di_nw);
20142068 defer di_nw.deinit();
2015 const parent_cpi = try dwarf.getConst(pt, .fromInterned(zcu.fileRootType(inst_info.file)));
20162069 dwarf.genDeclFuncGeneric(
20172070 pt,
20182071 &di_nw,
20192072 zir,
2020 parent_cpi,
2073 .fromInterned(ip.namespacePtr(nav.analysis.?.namespace).owner_type),
20212074 &decl,
20222075 &ip.indexToKey(func.ty).func_type,
20232076 nav.name.toSlice(ip),
......@@ -2040,7 +2093,7 @@ fn genDeclFuncGeneric(
20402093 pt: Zcu.PerThread,
20412094 di_nw: *MappedFile.Node.Writer,
20422095 zir: *const std.zig.Zir,
2043 parent_cpi: link.ConstPool.Index,
2096 parent_ty: Type,
20442097 decl: *const std.zig.Zir.Inst.Declaration.Unwrapped,
20452098 fn_ty: *const InternPool.Key.FuncType,
20462099 name: []const u8,
......@@ -2048,7 +2101,7 @@ fn genDeclFuncGeneric(
20482101) link.EmitError!void {
20492102 const diw = &di_nw.interface;
20502103 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_func_generic));
2051 try dwarf.sectionOffset(di_nw, Const.get(parent_cpi, dwarf).debug_info_ni.unwrap().?, 0);
2104 try dwarf.refConst(pt, di_nw, parent_ty.toValue());
20522105 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
20532106 try diw.writeUleb128(decl.src_column + 1);
20542107 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
......@@ -2062,10 +2115,7 @@ fn genDeclFuncGeneric(
20622115 try dwarf.strp(&dwarf.debug_str, di_nw, zir.nullTerminatedString(param_name));
20632116 },
20642117 }
2065 try dwarf.sectionOffset(di_nw, Const.get(try dwarf.getConst(
2066 pt,
2067 .fromInterned(fn_ty.param_types.get(&pt.zcu.intern_pool)[param_index]),
2068 ), dwarf).debug_info_ni.unwrap().?, 0);
2118 try dwarf.refConst(pt, di_nw, .fromInterned(fn_ty.param_types.get(&pt.zcu.intern_pool)[param_index]));
20692119 param_index += 1;
20702120 }
20712121 if (fn_ty.is_var_args) try diw.writeUleb128(try dwarf.refAbbrevCode(.is_var_args));
......@@ -2079,16 +2129,14 @@ pub fn addConst(
20792129 val: InternPool.Index,
20802130 addConstNode: *const fn (
20812131 lf: *link.File,
2082 ui: Dwarf.Unit.Index,
2132 ui: Unit.Index,
20832133 cpi: link.ConstPool.Index,
20842134 ) link.Error!MappedFile.Node.Index,
20852135) link.Error!void {
2086 const comp = dwarf.lf.comp;
2087 const zcu = comp.zcu.?;
2136 const zcu = dwarf.lf.comp.zcu.?;
20882137 const ip = &zcu.intern_pool;
2089
20902138 assert(@backingInt(cpi) == dwarf.consts.items.len);
2091 try dwarf.consts.append(comp.gpa, .{
2139 dwarf.consts.appendAssumeCapacity(.{
20922140 .debug_info_ni = debug_info_ni: switch (ip.indexToKey(val)) {
20932141 else => try addConstNode(dwarf.lf, dwarf.getUnit(zcu.root_mod), cpi),
20942142 .func => |func| {
......@@ -2144,112 +2192,73 @@ fn updateConstInner(
21442192 const diw = &di_nw.interface;
21452193 switch (ip.indexToKey(val)) {
21462194 else => return,
2147 .struct_type => {
2148 const loaded_struct = ip.loadStructType(val);
2195 .int_type => |int_type| {
21492196 const ty: Type = .fromInterned(val);
2150 const file = loaded_struct.zir_index.resolveFile(ip);
2151 switch (loaded_struct.layout) {
2152 .auto, .@"extern" => {
2153 const struct_is_file: bool = if (loaded_struct.zir_index.resolve(ip)) |inst|
2154 inst == .main_struct_inst
2155 else
2156 false;
2157 if (loaded_struct.name_nav.unwrap()) |nav_index| {
2158 assert(!struct_is_file);
2159 const nav = ip.getNav(nav_index);
2160 const decl_inst = nav.srcInst(ip).resolve(ip).?;
2161 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
2162 try diw.writeUleb128(try dwarf.refAbbrevCode(
2163 if (loaded_struct.field_types.len == 0) .decl_namespace_struct else .decl_struct,
2164 ));
2165 try dwarf.sectionOffset(di_nw, Const.get(
2166 try dwarf.getConst(pt, .fromInterned(zcu.fileRootType(file))),
2167 dwarf,
2168 ).debug_info_ni.unwrap().?, 0);
2169 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2170 try diw.writeUleb128(decl.src_column + 1);
2171 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
2172 try dwarf.strp(&dwarf.debug_str, di_nw, nav.name.toSlice(ip));
2173 } else {
2174 const zfi = loaded_struct.zir_index.resolveFile(ip);
2175 const ui = dwarf.getUnit(zcu.fileByIndex(zfi).mod.?);
2176 _, const fi = try ui.get(dwarf).getFile(zcu.gpa, ui, zfi);
2177 try diw.writeUleb128(try dwarf.refAbbrevCode(switch (loaded_struct.field_types.len) {
2178 0 => if (struct_is_file) .empty_file else .empty_struct_type,
2179 else => if (struct_is_file) .file else .struct_type,
2180 }));
2181 try diw.writeUleb128(@backingInt(fi));
2182 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_struct.name.toSlice(ip));
2183 }
2184 if (loaded_struct.field_types.len == 0) {
2185 if (!struct_is_file) try diw.writeByte(@intFromBool(false));
2186 } else {
2187 try diw.writeUleb128(ty.abiSize(zcu));
2188 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
2189 for (0..loaded_struct.field_types.len) |field_index| {
2190 const is_comptime = loaded_struct.field_is_comptime_bits.get(ip, field_index);
2191 // TODO: we currently don't emit information about default values for
2192 // non-`comptime` fields, because these default values are resolved at a
2193 // separate time in the compiler frontend. To emit this information, the
2194 // frontend needs to tell us when the default values are available: like
2195 // how `Zcu.PerThread.ensureTypeLayoutUpToDate` enqueues a link task to
2196 // indicate completion of the type's layout, a task should be enqueued
2197 // by `Zcu.PerThread.ensureStructDefaultsUpToDate`, and upon receiving
2198 // it we should patch the correct default field values in.
2199 const field_init: InternPool.Index = if (is_comptime) loaded_struct.field_defaults.getOrNone(ip, field_index) else .none;
2200 assert(!(is_comptime and field_init == .none));
2201 const field_type: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
2202 const has_runtime_bits, const has_comptime_state = switch (field_init) {
2203 .none => .{ false, false },
2204 else => .{
2205 field_type.hasRuntimeBits(zcu),
2206 field_type.comptimeOnly(zcu),
2207 },
2208 };
2209 try diw.writeUleb128(try dwarf.refAbbrevCode(if (is_comptime)
2210 if (has_comptime_state)
2211 .field_comptime_comptime_state
2212 else if (has_runtime_bits)
2213 .field_comptime_runtime_bits
2214 else
2215 .field_comptime
2216 else if (field_init != .none)
2217 if (has_comptime_state)
2218 .field_default_comptime_state
2219 else if (has_runtime_bits)
2220 .field_default_runtime_bits
2221 else
2222 .field
2223 else
2224 .field));
2225 try dwarf.strp(
2226 &dwarf.debug_str,
2227 di_nw,
2228 loaded_struct.field_names.get(ip)[field_index].toSlice(ip),
2229 );
2230 try dwarf.sectionOffset(di_nw, Const.get(
2231 try dwarf.getConst(pt, field_type.toValue()),
2232 dwarf,
2233 ).debug_info_ni.unwrap().?, 0);
2234 if (!is_comptime) {
2235 try diw.writeUleb128(loaded_struct.field_offsets.get(ip)[field_index]);
2236 try diw.writeUleb128(loaded_struct.field_aligns.getOrNone(ip, field_index).toByteUnits() orelse
2237 field_type.abiAlignment(zcu).toByteUnits().?);
2238 }
2239 if (has_comptime_state)
2240 try dwarf.sectionOffset(di_nw, Const.get(
2241 try dwarf.getConst(pt, .fromInterned(field_init)),
2242 dwarf,
2243 ).debug_info_ni.unwrap().?, 0)
2244 else if (has_runtime_bits)
2245 //try wip_nav.blockValue(.fromInterned(field_init));
2246 try diw.writeUleb128(0);
2247 }
2248 try diw.writeUleb128(@backingInt(AbbrevCode.null));
2249 }
2250 },
2251 .@"packed" => return,
2252 }
2197 try diw.writeUleb128(try dwarf.refAbbrevCode(.numeric_type));
2198 var name_buf: [std.fmt.count("i{d}", .{std.math.maxInt(u16)})]u8 = undefined;
2199 try dwarf.strp(&dwarf.debug_str, di_nw, std.mem.print(&name_buf, "{f}", .{ty.fmt(pt)}) catch unreachable);
2200 try diw.writeByte(switch (int_type.signedness) {
2201 .signed => DW.ATE.signed,
2202 .unsigned => DW.ATE.unsigned,
2203 });
2204 try diw.writeUleb128(int_type.bits);
2205 try diw.writeUleb128(ty.abiSize(zcu));
2206 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
2207 },
2208 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
2209 .one, .many, .c => {
2210 const ty: Type = .fromInterned(val);
2211 const ptr_child_ty: Type = .fromInterned(ptr_type.child);
2212 try diw.writeUleb128(try dwarf.refAbbrevCode(switch (ptr_type.flags.alignment) {
2213 .none => if (ptr_type.sentinel == .none) .ptr_type else .ptr_sentinel_type,
2214 else => if (ptr_type.sentinel == .none) .ptr_aligned_type else .ptr_aligned_sentinel_type,
2215 }));
2216 {
2217 const name = try zcu.gpa.print("{f}", .{ty.fmt(pt)});
2218 defer zcu.gpa.free(name);
2219 try dwarf.strp(&dwarf.debug_str, di_nw, name);
2220 }
2221 if (ptr_type.sentinel != .none) try dwarf.blockConst(pt, di_nw, .fromInterned(ptr_type.sentinel));
2222 if (ptr_type.flags.alignment.toByteUnits()) |a| try diw.writeUleb128(a);
2223 try diw.writeByte(@backingInt(ptr_type.flags.address_space));
2224 if (ptr_type.flags.is_const or ptr_type.flags.is_volatile) try dwarf.sectionOffset(
2225 di_nw,
2226 di_nw.ni,
2227 diw.end + dwarf.sectionOffsetSize(),
2228 );
2229 if (ptr_type.flags.is_const) {
2230 try diw.writeUleb128(try dwarf.refAbbrevCode(.is_const));
2231 if (ptr_type.flags.is_volatile) try dwarf.sectionOffset(
2232 di_nw,
2233 di_nw.ni,
2234 diw.end + dwarf.sectionOffsetSize(),
2235 );
2236 }
2237 if (ptr_type.flags.is_volatile) try diw.writeUleb128(try dwarf.refAbbrevCode(.is_volatile));
2238 try dwarf.refConst(pt, di_nw, ptr_child_ty.toValue());
2239 },
2240 .slice => {
2241 const ty: Type = .fromInterned(val);
2242 try diw.writeUleb128(try dwarf.refAbbrevCode(.generated_struct_type));
2243 {
2244 const name = try zcu.gpa.print("{f}", .{ty.fmt(pt)});
2245 defer zcu.gpa.free(name);
2246 try dwarf.strp(&dwarf.debug_str, di_nw, name);
2247 }
2248 try diw.writeUleb128(ty.abiSize(zcu));
2249 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
2250 try diw.writeUleb128(try dwarf.refAbbrevCode(.generated_field));
2251 try dwarf.strp(&dwarf.debug_str, di_nw, "ptr");
2252 const ptr_field_ty = ty.slicePtrFieldType(zcu);
2253 try dwarf.refConst(pt, di_nw, ptr_field_ty.toValue());
2254 try diw.writeUleb128(0);
2255 try diw.writeUleb128(try dwarf.refAbbrevCode(.generated_field));
2256 try dwarf.strp(&dwarf.debug_str, di_nw, "len");
2257 const len_field_ty: Type = .usize;
2258 try dwarf.refConst(pt, di_nw, len_field_ty.toValue());
2259 try diw.writeUleb128(len_field_ty.abiAlignment(zcu).forward(ptr_field_ty.abiSize(zcu)));
2260 try diw.writeUleb128(@backingInt(AbbrevCode.null));
2261 },
22532262 },
22542263 .simple_type => |simple_type| switch (simple_type) {
22552264 .f16,
......@@ -2298,21 +2307,138 @@ fn updateConstInner(
22982307 .comptime_int,
22992308 .comptime_float,
23002309 .noreturn,
2310 .null,
2311 .undefined,
2312 .enum_literal,
23012313 => {
2314 const ty: Type = .fromInterned(val);
23022315 try diw.writeUleb128(try dwarf.refAbbrevCode(.void_type));
2303 try dwarf.strp(&dwarf.debug_str, di_nw, @tagName(simple_type));
2304 },
2305 inline .null, .undefined => |tag| {
2306 try diw.writeUleb128(try dwarf.refAbbrevCode(.void_type));
2307 try dwarf.strp(&dwarf.debug_str, di_nw, "@TypeOf(" ++ @tagName(tag) ++ ")");
2308 },
2309 .enum_literal => {
2310 try diw.writeUleb128(try dwarf.refAbbrevCode(.void_type));
2311 try dwarf.strp(&dwarf.debug_str, di_nw, "@EnumLiteral()");
2316 var name_buf: ["@TypeOf(undefined)".len]u8 = undefined;
2317 try dwarf.strp(&dwarf.debug_str, di_nw, std.mem.print(&name_buf, "{f}", .{ty.fmt(pt)}) catch unreachable);
23122318 },
23132319 .anyerror => return,
23142320 .adhoc_inferred_error_set => unreachable,
23152321 },
2322 .struct_type => {
2323 const loaded_struct = ip.loadStructType(val);
2324 const zfi = loaded_struct.zir_index.resolveFile(ip);
2325 const zf = zcu.fileByIndex(zfi);
2326 switch (loaded_struct.layout) {
2327 .auto, .@"extern" => {
2328 const struct_is_file = loaded_struct.zir_index.resolve(ip) == .main_struct_inst;
2329 if (loaded_struct.name_nav.unwrap()) |name_ni| {
2330 assert(!struct_is_file);
2331 const name_nav = ip.getNav(name_ni);
2332 const decl = zf.zir.?.getDeclaration(name_nav.srcInst(ip).resolve(ip).?);
2333 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2334 name_nav.analysis.?.namespace,
2335 ).owner_type);
2336 try diw.writeUleb128(try dwarf.refAbbrevCode(
2337 if (loaded_struct.field_types.len > 0) .decl_struct else .decl_namespace_struct,
2338 ));
2339 try dwarf.sectionOffset(di_nw, parent_ni, 0);
2340 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2341 try diw.writeUleb128(decl.src_column + 1);
2342 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
2343 try dwarf.strp(&dwarf.debug_str, di_nw, name_nav.name.toSlice(ip));
2344 } else {
2345 const ui = dwarf.getUnit(zf.mod.?);
2346 _, const fi = try ui.get(dwarf).getFile(zcu.gpa, ui, zfi);
2347 try diw.writeUleb128(try dwarf.refAbbrevCode(switch (loaded_struct.field_types.len) {
2348 0 => if (struct_is_file) .empty_file else .empty_struct_type,
2349 else => if (struct_is_file) .file else .struct_type,
2350 }));
2351 try diw.writeUleb128(@backingInt(fi));
2352 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_struct.name.toSlice(ip));
2353 }
2354 if (loaded_struct.field_types.len > 0) {
2355 const ty: Type = .fromInterned(val);
2356 try diw.writeUleb128(ty.abiSize(zcu));
2357 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
2358 for (0..loaded_struct.field_types.len) |field_index| {
2359 const is_comptime = loaded_struct.field_is_comptime_bits.get(ip, field_index);
2360 // TODO: we currently don't emit information about default values for
2361 // non-`comptime` fields, because these default values are resolved at a
2362 // separate time in the compiler frontend. To emit this information, the
2363 // frontend needs to tell us when the default values are available: like
2364 // how `Zcu.PerThread.ensureTypeLayoutUpToDate` enqueues a link task to
2365 // indicate completion of the type's layout, a task should be enqueued
2366 // by `Zcu.PerThread.ensureStructDefaultsUpToDate`, and upon receiving
2367 // it we should patch the correct default field values in.
2368 const field_default: InternPool.Index =
2369 if (is_comptime) loaded_struct.field_defaults.getOrNone(ip, field_index) else .none;
2370 assert(!(is_comptime and field_default == .none));
2371 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
2372 const field_default_class = switch (field_default) {
2373 .none => .no_possible_value,
2374 else => field_ty.classify(zcu),
2375 };
2376 try diw.writeUleb128(try dwarf.refAbbrevCode(switch (field_default_class) {
2377 .no_possible_value, .one_possible_value => if (is_comptime) .field_comptime else .field,
2378 .runtime => if (is_comptime) .field_comptime_fully_runtime else .field_default_fully_runtime,
2379 .partially_comptime => if (is_comptime) .field_comptime_partially_comptime else .field_default_partially_comptime,
2380 .fully_comptime => if (is_comptime) .field_comptime_fully_comptime else .field_default_fully_comptime,
2381 }));
2382 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_struct.field_names.get(ip)[field_index].toSlice(ip));
2383 try dwarf.refConst(pt, di_nw, field_ty.toValue());
2384 if (!is_comptime) {
2385 try diw.writeUleb128(loaded_struct.field_offsets.get(ip)[field_index]);
2386 try diw.writeUleb128(loaded_struct.field_aligns.getOrNone(ip, field_index).toByteUnits() orelse
2387 field_ty.abiAlignment(zcu).toByteUnits().?);
2388 }
2389 if (field_default_class.hasRuntimeBits()) try dwarf.blockConst(pt, di_nw, .fromInterned(field_default));
2390 if (field_default_class.comptimeOnly()) try dwarf.refConst(pt, di_nw, .fromInterned(field_default));
2391 }
2392 try diw.writeUleb128(@backingInt(AbbrevCode.null));
2393 } else try diw.writeByte(@intFromBool(false));
2394 },
2395 .@"packed" => return,
2396 }
2397 },
2398 .enum_type => {
2399 const loaded_enum = ip.loadEnumType(val);
2400 if (loaded_enum.zir_index.unwrap()) |zir_index| {
2401 assert(loaded_enum.owner_union == .none);
2402 const zfi = zir_index.resolveFile(ip);
2403 const zf = zcu.fileByIndex(zfi);
2404 if (loaded_enum.name_nav.unwrap()) |name_ni| {
2405 const name_nav = ip.getNav(name_ni);
2406 const decl = zf.zir.?.getDeclaration(name_nav.srcInst(ip).resolve(ip).?);
2407 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2408 name_nav.analysis.?.namespace,
2409 ).owner_type);
2410 try diw.writeUleb128(try dwarf.refAbbrevCode(
2411 if (loaded_enum.field_names.len > 0) .decl_enum else .decl_empty_enum,
2412 ));
2413 try dwarf.sectionOffset(di_nw, parent_ni, 0);
2414 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2415 try diw.writeUleb128(decl.src_column + 1);
2416 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
2417 try dwarf.strp(&dwarf.debug_str, di_nw, name_nav.name.toSlice(ip));
2418 } else {
2419 const ui = dwarf.getUnit(zf.mod.?);
2420 _, const fi = try ui.get(dwarf).getFile(zcu.gpa, ui, zfi);
2421 try diw.writeUleb128(try dwarf.refAbbrevCode(
2422 if (loaded_enum.field_names.len > 0) .enum_type else .empty_enum_type,
2423 ));
2424 try diw.writeUleb128(@backingInt(fi));
2425 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.name.toSlice(ip));
2426 }
2427 } else {
2428 assert(loaded_enum.owner_union != .none);
2429 try diw.writeUleb128(try dwarf.refAbbrevCode(
2430 if (loaded_enum.field_names.len == 0) .generated_empty_enum_type else .generated_enum_type,
2431 ));
2432 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.name.toSlice(ip));
2433 }
2434 try dwarf.refConst(pt, di_nw, .fromInterned(loaded_enum.int_tag_type));
2435 for (0..loaded_enum.field_names.len) |field_index| {
2436 try diw.writeUleb128(try dwarf.refAbbrevCode(.enum_field));
2437 try dwarf.enumConstValue(diw, loaded_enum, field_index);
2438 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.field_names.get(ip)[field_index].toSlice(ip));
2439 }
2440 if (loaded_enum.field_names.len > 0) try diw.writeUleb128(@backingInt(AbbrevCode.null));
2441 },
23162442 }
23172443 try dwarf.genDebugInfoPadding(diw, diw.unusedCapacityLen());
23182444}
......@@ -2339,45 +2465,90 @@ fn updateConstIncompleteInner(
23392465 const ip = &zcu.intern_pool;
23402466 const diw = &di_nw.interface;
23412467 done: {
2342 const zir_index, const name, const maybe_name_nav = container: switch (ip.indexToKey(val)) {
2468 const src_inst, const zf, const capture_names, const captures, const name, const maybe_name_nav, const namespace = container: switch (ip.indexToKey(val)) {
23432469 .struct_type => {
23442470 const loaded_struct = ip.loadStructType(val);
2345 if (loaded_struct.zir_index.resolveFull(ip)) |src_inst| switch (src_inst.inst) {
2471 const src_inst = loaded_struct.zir_index.resolveFull(ip) orelse {
2472 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
2473 break :done;
2474 };
2475 const zf = zcu.fileByIndex(src_inst.file);
2476 switch (src_inst.inst) {
23462477 .main_struct_inst => {
2347 const ui = dwarf.getUnit(zcu.fileByIndex(src_inst.file).mod.?);
2478 const ui = dwarf.getUnit(zf.mod.?);
23482479 _, const fi = try ui.get(dwarf).getFile(zcu.gpa, ui, src_inst.file);
23492480 try diw.writeUleb128(try dwarf.refAbbrevCode(.empty_file));
23502481 try diw.writeUleb128(@backingInt(fi));
23512482 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_struct.name.toSlice(ip));
2483 try diw.writeByte(@intFromBool(true));
23522484 break :done;
23532485 },
2354 else => {},
2355 };
2356 break :container .{
2357 loaded_struct.zir_index,
2358 loaded_struct.name,
2359 loaded_struct.name_nav,
2360 };
2486 else => break :container .{
2487 src_inst,
2488 zf,
2489 zf.zir.?.getStructDecl(src_inst.inst).capture_names,
2490 loaded_struct.captures,
2491 loaded_struct.name,
2492 loaded_struct.name_nav,
2493 loaded_struct.namespace,
2494 },
2495 }
23612496 },
23622497 .union_type => {
23632498 const loaded_union = ip.loadUnionType(val);
2364 break :container .{ loaded_union.zir_index, loaded_union.name, loaded_union.name_nav };
2499 const src_inst = loaded_union.zir_index.resolveFull(ip) orelse {
2500 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
2501 break :done;
2502 };
2503 const zf = zcu.fileByIndex(src_inst.file);
2504 break :container .{
2505 src_inst,
2506 zf,
2507 zf.zir.?.getUnionDecl(src_inst.inst).capture_names,
2508 loaded_union.captures,
2509 loaded_union.name,
2510 loaded_union.name_nav,
2511 loaded_union.namespace,
2512 };
23652513 },
23662514 .enum_type => {
23672515 const loaded_enum = ip.loadEnumType(val);
2368 if (loaded_enum.zir_index.unwrap()) |zir_index|
2369 break :container .{ zir_index, loaded_enum.name, loaded_enum.name_nav };
2370 try diw.writeUleb128(try dwarf.refAbbrevCode(.generated_empty_struct_type));
2371 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.name.toSlice(ip));
2372 try diw.writeByte(@intFromBool(true));
2373 break :done;
2516 const zir_index = loaded_enum.zir_index.unwrap() orelse {
2517 try diw.writeUleb128(try dwarf.refAbbrevCode(.generated_empty_struct_type));
2518 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.name.toSlice(ip));
2519 try diw.writeByte(@intFromBool(true));
2520 break :done;
2521 };
2522 const src_inst = zir_index.resolveFull(ip) orelse {
2523 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
2524 break :done;
2525 };
2526 const zf = zcu.fileByIndex(src_inst.file);
2527 break :container .{
2528 src_inst,
2529 zf,
2530 zf.zir.?.getEnumDecl(src_inst.inst).capture_names,
2531 loaded_enum.captures,
2532 loaded_enum.name,
2533 loaded_enum.name_nav,
2534 loaded_enum.namespace,
2535 };
23742536 },
23752537 .opaque_type => {
23762538 const loaded_opaque = ip.loadOpaqueType(val);
2539 const src_inst = loaded_opaque.zir_index.resolveFull(ip) orelse {
2540 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
2541 break :done;
2542 };
2543 const zf = zcu.fileByIndex(src_inst.file);
23772544 break :container .{
2378 loaded_opaque.zir_index,
2545 src_inst,
2546 zf,
2547 zf.zir.?.getOpaqueDecl(src_inst.inst).capture_names,
2548 loaded_opaque.captures,
23792549 loaded_opaque.name,
23802550 loaded_opaque.name_nav,
2551 loaded_opaque.namespace,
23812552 };
23822553 },
23832554 else => |val_key| break :done switch (val_key.typeOf()) {
......@@ -2389,37 +2560,133 @@ fn updateConstIncompleteInner(
23892560 try diw.writeByte(@intFromBool(true));
23902561 },
23912562 else => |ty| {
2392 const ty_cpi = try dwarf.getConst(pt, Value.fromInterned(ty));
23932563 try diw.writeUleb128(try dwarf.refAbbrevCode(.undefined_comptime_value));
2394 try dwarf.sectionOffset(
2395 di_nw,
2396 Const.get(ty_cpi, dwarf).debug_info_ni.unwrap().?,
2397 0,
2398 );
2564 try dwarf.refConst(pt, di_nw, .fromInterned(ty));
23992565 },
24002566 },
24012567 };
2402 const src_inst = zir_index.resolveFull(ip) orelse {
2403 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
2404 break :done;
2405 };
2406 if (maybe_name_nav.unwrap()) |name_nav| {
2407 const name_src_inst = ip.getNav(name_nav).srcInst(ip).resolve(ip).?;
2408 const decl = zcu.fileByIndex(src_inst.file).zir.?.getDeclaration(name_src_inst);
2409 const parent_cpi = try dwarf.getConst(pt, .fromInterned(zcu.fileRootType(src_inst.file)));
2410 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_namespace_struct));
2411 try dwarf.sectionOffset(di_nw, Const.get(parent_cpi, dwarf).debug_info_ni.unwrap().?, 0);
2568 const capturing = for (captures.get(ip)) |capture| switch (capture.tag) {
2569 .@"comptime", .runtime => break true,
2570 .nav_val, .nav_ref => {},
2571 } else false;
2572 const spec_di = if (false and capturing) try dwarf.getDecl(pt, val) else undefined;
2573 _ = spec_di;
2574 if (maybe_name_nav.unwrap()) |name_ni| {
2575 const name_nav = ip.getNav(name_ni);
2576 const name_src_inst = name_nav.srcInst(ip).resolve(ip).?;
2577 const decl = zf.zir.?.getDeclaration(name_src_inst);
2578 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2579 name_nav.analysis.?.namespace,
2580 ).owner_type);
2581 try diw.writeUleb128(try dwarf.refAbbrevCode(
2582 if (capturing) .decl_capturing_namespace_struct else .decl_namespace_struct,
2583 ));
2584 try dwarf.sectionOffset(di_nw, parent_ni, 0);
24122585 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
24132586 try diw.writeUleb128(decl.src_column + 1);
24142587 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
2588 try dwarf.strp(&dwarf.debug_str, di_nw, name_nav.name.toSlice(ip));
24152589 } else {
2416 const ui = dwarf.getUnit(zcu.fileByIndex(src_inst.file).mod.?);
2590 const ui = dwarf.getUnit(zf.mod.?);
24172591 _, const fi = try ui.get(dwarf).getFile(zcu.gpa, ui, src_inst.file);
2418 try diw.writeUleb128(try dwarf.refAbbrevCode(.empty_struct_type));
2592 const parent_ni = if (false) try dwarf.getDecl(pt, ip.namespacePtr(
2593 ip.namespacePtr(namespace).parent.unwrap().?,
2594 ).owner_type);
2595 _ = parent_ni;
2596 try diw.writeUleb128(try dwarf.refAbbrevCode(
2597 if (capturing) .capturing_empty_struct_type else .empty_struct_type,
2598 ));
24192599 try diw.writeUleb128(@backingInt(fi));
2600 try dwarf.strp(&dwarf.debug_str, di_nw, name.toSlice(ip));
24202601 }
2421 try dwarf.strp(&dwarf.debug_str, di_nw, name.toSlice(ip));
24222602 try diw.writeByte(@intFromBool(true));
2603 if (capturing) {
2604 for (capture_names, captures.get(ip)) |capture_name, capture| switch (capture.unwrap()) {
2605 .@"comptime" => |capture_val| {
2606 const ty: Type = .fromInterned(ip.typeOf(capture_val));
2607 const ty_class = ty.classify(zcu);
2608 try diw.writeUleb128(try dwarf.refAbbrevCode(switch (ty_class) {
2609 .no_possible_value => unreachable,
2610 .one_possible_value => .comptime_capture,
2611 .runtime => .comptime_capture_runtime,
2612 .partially_comptime => .comptime_capture_partially_comptime,
2613 .fully_comptime => .comptime_capture_fully_comptime,
2614 }));
2615 try dwarf.strp(&dwarf.debug_str, di_nw, zf.zir.?.nullTerminatedString(capture_name));
2616 try dwarf.refConst(pt, di_nw, ty.toValue());
2617 if (ty_class.hasRuntimeBits()) try dwarf.blockConst(pt, di_nw, .fromInterned(capture_val));
2618 if (ty_class.comptimeOnly()) try dwarf.refConst(pt, di_nw, .fromInterned(capture_val));
2619 },
2620 .runtime => |capture_ty| {
2621 try diw.writeUleb128(try dwarf.refAbbrevCode(.runtime_capture));
2622 try dwarf.strp(&dwarf.debug_str, di_nw, zf.zir.?.nullTerminatedString(capture_name));
2623 try dwarf.refConst(pt, di_nw, .fromInterned(capture_ty));
2624 },
2625 .nav_val, .nav_ref => {},
2626 };
2627 try diw.writeByte(@backingInt(AbbrevCode.null));
2628 }
2629 }
2630 try dwarf.genDebugInfoPadding(diw, diw.unusedCapacityLen());
2631}
2632
2633pub fn genDecl(
2634 dwarf: *Dwarf,
2635 pt: Zcu.PerThread,
2636 di_nw: *MappedFile.Node.Writer,
2637 instance_val: InternPool.Index,
2638) link.Error!void {
2639 dwarf.genDeclInner(pt, di_nw, instance_val) catch |err| switch (err) {
2640 else => |e| return e,
2641 error.WriteFailed => return dwarf.reportWriteError(di_nw),
2642 };
2643}
2644fn genDeclInner(
2645 dwarf: *Dwarf,
2646 pt: Zcu.PerThread,
2647 di_nw: *MappedFile.Node.Writer,
2648 instance_val: InternPool.Index,
2649) link.EmitError!void {
2650 const ip = &dwarf.lf.comp.zcu.?.intern_pool;
2651 const diw = &di_nw.interface;
2652 switch (ip.indexToKey(instance_val)) {
2653 else => unreachable,
2654 .struct_type => {
2655 const loaded_struct = ip.loadStructType(instance_val);
2656 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2657 ip.namespacePtr(loaded_struct.namespace).parent.unwrap().?,
2658 ).owner_type);
2659 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_specification_struct));
2660 try dwarf.sectionOffset(di_nw, parent_ni, 0);
2661 try diw.writeInt(u32, 0, dwarf.endian);
2662 try diw.writeUleb128(0);
2663 try diw.writeByte(DW.ACCESS.public);
2664 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_struct.name.toSlice(ip));
2665 },
2666 .enum_type => {
2667 const loaded_enum = ip.loadEnumType(instance_val);
2668 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2669 ip.namespacePtr(loaded_enum.namespace).parent.unwrap().?,
2670 ).owner_type);
2671 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_specification_enum));
2672 try dwarf.sectionOffset(di_nw, parent_ni, 0);
2673 try diw.writeInt(u32, 0, dwarf.endian);
2674 try diw.writeUleb128(0);
2675 try diw.writeByte(DW.ACCESS.public);
2676 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_enum.name.toSlice(ip));
2677 },
2678 .union_type => {
2679 const loaded_union = ip.loadUnionType(instance_val);
2680 const parent_ni = try dwarf.getDecl(pt, ip.namespacePtr(
2681 ip.namespacePtr(loaded_union.namespace).parent.unwrap().?,
2682 ).owner_type);
2683 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_specification_union));
2684 try dwarf.sectionOffset(di_nw, parent_ni, 0);
2685 try diw.writeInt(u32, 0, dwarf.endian);
2686 try diw.writeUleb128(0);
2687 try diw.writeByte(DW.ACCESS.public);
2688 try dwarf.strp(&dwarf.debug_str, di_nw, loaded_union.name.toSlice(ip));
2689 },
24232690 }
24242691 try dwarf.genDebugInfoPadding(diw, diw.unusedCapacityLen());
24252692}
......@@ -2430,8 +2697,13 @@ pub fn updateLineNumber(
24302697 inst: InternPool.TrackedInst.Index,
24312698 line: u32,
24322699) void {
2433 const decl_ni = dwarf.decls.get(inst) orelse return;
2434 std.mem.writeInt(u32, decl_ni.slice(mf)[AbbrevCode.decl_bytes..][0..4], line + 1, dwarf.endian);
2700 const di = dwarf.getDeclIfExists(inst) orelse return;
2701 const decl_ni = di.get(dwarf).debug_info_ni.unwrap() orelse return;
2702 std.mem.writeInt(u32, decl_ni.slice(mf)[AbbrevCode.decl_size..][0..4], line + 1, dwarf.endian);
2703}
2704
2705pub fn lostTracking(dwarf: *Dwarf, diw: *Writer) link.EmitError!void {
2706 try diw.writeUleb128(try dwarf.refAbbrevCode(.decl_lost));
24352707}
24362708
24372709fn refAbbrevCodeIfExists(
......@@ -2442,7 +2714,7 @@ fn refAbbrevCodeIfExists(
24422714 return if (dwarf.debug_abbrev.set.contains(abbrev_code)) @backingInt(abbrev_code) else null;
24432715}
24442716
2445pub fn refAbbrevCode(
2717fn refAbbrevCode(
24462718 dwarf: *Dwarf,
24472719 abbrev_code: AbbrevCode,
24482720) link.Error!@typeInfo(AbbrevCode).@"enum".tag_type {
......@@ -2517,6 +2789,75 @@ fn symbolAddress(
25172789 try elf.addReloc(@bitCast(nw.ni), offset, target_si, @bitCast(@as(u64, addend)), .absAddr(elf));
25182790}
25192791
2792fn blockConst(dwarf: *Dwarf, pt: Zcu.PerThread, nw: *MappedFile.Node.Writer, val: Value) link.EmitError!void {
2793 const ty = val.typeOf(pt.zcu);
2794 const size = ty.abiSize(pt.zcu);
2795 try nw.interface.writeUleb128(size);
2796 const start = nw.interface.end;
2797 if (size > 0) try codegen.generateSymbol(
2798 dwarf.lf,
2799 pt,
2800 val,
2801 &nw.interface,
2802 .{ .atom_index = @bitCast(nw.ni) },
2803 );
2804 assert(start + size == nw.interface.end);
2805}
2806
2807fn refConst(dwarf: *Dwarf, pt: Zcu.PerThread, nw: *MappedFile.Node.Writer, val: Value) link.EmitError!void {
2808 try dwarf.sectionOffset(nw, Const.get(try dwarf.getConst(pt, val), dwarf).debug_info_ni.unwrap().?, 0);
2809}
2810
2811fn bigIntConstValue(dwarf: *Dwarf, diw: *Writer, ty: Type, big_int: std.math.big.int.Const) link.EmitError!void {
2812 const zcu = dwarf.lf.comp.zcu.?;
2813 const signedness = switch (ty.toIntern()) {
2814 .comptime_int_type => .signed,
2815 else => ty.intInfo(zcu).signedness,
2816 };
2817 const bits = @max(1, big_int.bitCountTwosCompForSignedness(signedness));
2818 if (bits <= 64) {
2819 try diw.writeUleb128(@as(u13, switch (signedness) {
2820 .signed => DW.FORM.sdata,
2821 .unsigned => DW.FORM.udata,
2822 }));
2823 var bit: usize = 0;
2824 var carry: u1 = 1;
2825 for (try diw.writableSlice(@divCeil(bits, 7))) |*byte| {
2826 const limb_bits = @typeInfo(std.math.big.Limb).int.bits;
2827 const limb_index = bit / limb_bits;
2828 const limb_shift: std.math.Log2Int(std.math.big.Limb) = @intCast(bit % limb_bits);
2829 const low_abs_part: u7 = @truncate(big_int.limbs[limb_index] >> limb_shift);
2830 const abs_part = if (limb_shift > limb_bits - 7 and limb_index + 1 < big_int.limbs.len) abs_part: {
2831 const high_abs_part: u7 = @truncate(big_int.limbs[limb_index + 1] << -%limb_shift);
2832 break :abs_part high_abs_part | low_abs_part;
2833 } else low_abs_part;
2834 const twos_comp_part = if (big_int.positive) abs_part else twos_comp_part: {
2835 const twos_comp_part, carry = @addWithOverflow(~abs_part, carry);
2836 break :twos_comp_part twos_comp_part;
2837 };
2838 bit += 7;
2839 byte.* = @as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part;
2840 }
2841 } else {
2842 try diw.writeUleb128(DW.FORM.block);
2843 const size = switch (ty.toIntern()) {
2844 .comptime_int_type => @divCeil(bits, 8),
2845 else => ty.abiSize(zcu),
2846 };
2847 try diw.writeUleb128(size);
2848 big_int.writeTwosComplement(try diw.writableSlice(@intCast(size)), dwarf.endian);
2849 }
2850}
2851
2852fn enumConstValue(dwarf: *Dwarf, diw: *Writer, loaded_enum: InternPool.LoadedEnumType, field_index: usize) link.EmitError!void {
2853 const zcu = dwarf.lf.comp.zcu.?;
2854 var big_int_space: Value.BigIntSpace = undefined;
2855 try dwarf.bigIntConstValue(diw, .fromInterned(loaded_enum.int_tag_type), if (loaded_enum.field_values.len > 0)
2856 Value.fromInterned(loaded_enum.field_values.get(&zcu.intern_pool)[field_index]).toBigInt(&big_int_space, zcu)
2857 else
2858 std.math.big.int.Mutable.init(&big_int_space.limbs, field_index).toConst());
2859}
2860
25202861fn strp(dwarf: *Dwarf, s: *Str, nw: *MappedFile.Node.Writer, str: []const u8) link.EmitError!void {
25212862 const comp = dwarf.lf.comp;
25222863 const mf = &dwarf.lf.cast(.elf2).?.mf;
......@@ -2571,6 +2912,7 @@ pub const AbbrevCode = enum {
25712912 decl_empty_enum,
25722913 decl_enum,
25732914 decl_namespace_struct,
2915 decl_capturing_namespace_struct,
25742916 decl_struct,
25752917 decl_packed_struct,
25762918 decl_union,
......@@ -2587,6 +2929,7 @@ pub const AbbrevCode = enum {
25872929 decl_extern_nullary_func,
25882930 decl_extern_func,
25892931 decl_specification_struct,
2932 decl_specification_enum,
25902933 decl_specification_union,
25912934 decl_specification_func,
25922935 decl_instance_alias,
......@@ -2619,11 +2962,13 @@ pub const AbbrevCode = enum {
26192962 enum_field,
26202963 generated_field,
26212964 field,
2622 field_default_runtime_bits,
2623 field_default_comptime_state,
2965 field_default_fully_runtime,
2966 field_default_partially_comptime,
2967 field_default_fully_comptime,
26242968 field_comptime,
2625 field_comptime_runtime_bits,
2626 field_comptime_comptime_state,
2969 field_comptime_fully_runtime,
2970 field_comptime_partially_comptime,
2971 field_comptime_fully_comptime,
26272972 packed_field,
26282973 tagged_union,
26292974 tagged_union_field,
......@@ -2655,6 +3000,7 @@ pub const AbbrevCode = enum {
26553000 empty_enum_type,
26563001 enum_type,
26573002 empty_struct_type,
3003 capturing_empty_struct_type,
26583004 struct_type,
26593005 empty_packed_struct_type,
26603006 packed_struct_type,
......@@ -2662,6 +3008,11 @@ pub const AbbrevCode = enum {
26623008 union_type,
26633009 empty_packed_union_type,
26643010 packed_union_type,
3011 comptime_capture,
3012 comptime_capture_runtime,
3013 comptime_capture_partially_comptime,
3014 comptime_capture_fully_comptime,
3015 runtime_capture,
26653016 builtin_extern_nullary_func,
26663017 builtin_extern_func,
26673018 builtin_extern_var,
......@@ -2672,19 +3023,19 @@ pub const AbbrevCode = enum {
26723023 arg,
26733024 unnamed_arg,
26743025 comptime_arg,
3026 comptime_arg_fully_runtime,
3027 comptime_arg_partially_comptime,
3028 comptime_arg_fully_comptime,
26753029 unnamed_comptime_arg,
2676 comptime_arg_runtime_bits,
2677 unnamed_comptime_arg_runtime_bits,
2678 comptime_arg_comptime_state,
2679 unnamed_comptime_arg_comptime_state,
2680 comptime_arg_runtime_bits_comptime_state,
2681 unnamed_comptime_arg_runtime_bits_comptime_state,
3030 unnamed_comptime_arg_fully_runtime,
3031 unnamed_comptime_arg_partially_comptime,
3032 unnamed_comptime_arg_fully_comptime,
26823033 extern_param,
26833034 local_var,
26843035 local_const,
2685 local_const_runtime_bits,
2686 local_const_comptime_state,
2687 local_const_runtime_bits_comptime_state,
3036 local_const_fully_runtime,
3037 local_const_partially_comptime,
3038 local_const_fully_comptime,
26883039 undefined_comptime_value,
26893040 comptime_value,
26903041 location_comptime_value,
......@@ -2696,11 +3047,11 @@ pub const AbbrevCode = enum {
26963047 comptime_value_elem_runtime_bits,
26973048 comptime_value_elem_comptime_state,
26983049
2699 const decl_bytes = uleb128Size(@backingInt(AbbrevCode.decl_instance_extern_func));
3050 const decl_size = uleb128Size(@backingInt(AbbrevCode.decl_instance_extern_func));
27003051 comptime {
27013052 assert(uleb128Size(@backingInt(AbbrevCode.pad_1)) == 1);
27023053 assert(uleb128Size(@backingInt(AbbrevCode.pad_n)) == 1);
2703 assert(uleb128Size(@backingInt(AbbrevCode.decl_alias)) == decl_bytes);
3054 assert(uleb128Size(@backingInt(AbbrevCode.decl_alias)) == decl_size);
27043055 }
27053056
27063057 const Attr = struct {
......@@ -2766,6 +3117,13 @@ pub const AbbrevCode = enum {
27663117 .{ .declaration, .flag },
27673118 },
27683119 },
3120 .decl_capturing_namespace_struct = .{
3121 .tag = .structure_type,
3122 .children = true,
3123 .attrs = decl_attrs ++ .{
3124 .{ .declaration, .flag },
3125 },
3126 },
27693127 .decl_struct = .{
27703128 .tag = .structure_type,
27713129 .children = true,
......@@ -2908,11 +3266,15 @@ pub const AbbrevCode = enum {
29083266 },
29093267 },
29103268 .decl_specification_struct = .{
2911 .tag = .variable,
3269 .tag = .structure_type,
3270 .attrs = decl_specification_attrs,
3271 },
3272 .decl_specification_enum = .{
3273 .tag = .enumeration_type,
29123274 .attrs = decl_specification_attrs,
29133275 },
29143276 .decl_specification_union = .{
2915 .tag = .constant,
3277 .tag = .union_type,
29163278 .attrs = decl_specification_attrs,
29173279 },
29183280 .decl_specification_func = .{
......@@ -2940,6 +3302,7 @@ pub const AbbrevCode = enum {
29403302 },
29413303 .decl_instance_namespace_struct = .{
29423304 .tag = .structure_type,
3305 .children = true,
29433306 .attrs = decl_instance_attrs ++ .{
29443307 .{ .declaration, .flag },
29453308 },
......@@ -3118,6 +3481,7 @@ pub const AbbrevCode = enum {
31183481 .attrs = &.{
31193482 .{ .decl_file, .udata },
31203483 .{ .name, .strp },
3484 .{ .declaration, .flag },
31213485 },
31223486 },
31233487 .file = .{
......@@ -3161,7 +3525,17 @@ pub const AbbrevCode = enum {
31613525 .{ .alignment, .udata },
31623526 },
31633527 },
3164 .field_default_runtime_bits = .{
3528 .field_default_fully_runtime = .{
3529 .tag = .member,
3530 .attrs = &.{
3531 .{ .name, .strp },
3532 .{ .type, .ref_addr },
3533 .{ .data_member_location, .udata },
3534 .{ .alignment, .udata },
3535 .{ .default_value, .block },
3536 },
3537 },
3538 .field_default_partially_comptime = .{
31653539 .tag = .member,
31663540 .attrs = &.{
31673541 .{ .name, .strp },
......@@ -3169,9 +3543,10 @@ pub const AbbrevCode = enum {
31693543 .{ .data_member_location, .udata },
31703544 .{ .alignment, .udata },
31713545 .{ .default_value, .block },
3546 .{ .ZIG_comptime_value, .ref_addr },
31723547 },
31733548 },
3174 .field_default_comptime_state = .{
3549 .field_default_fully_comptime = .{
31753550 .tag = .member,
31763551 .attrs = &.{
31773552 .{ .name, .strp },
......@@ -3189,7 +3564,7 @@ pub const AbbrevCode = enum {
31893564 .{ .type, .ref_addr },
31903565 },
31913566 },
3192 .field_comptime_runtime_bits = .{
3567 .field_comptime_fully_runtime = .{
31933568 .tag = .member,
31943569 .attrs = &.{
31953570 .{ .const_expr, .flag_present },
......@@ -3198,7 +3573,17 @@ pub const AbbrevCode = enum {
31983573 .{ .const_value, .block },
31993574 },
32003575 },
3201 .field_comptime_comptime_state = .{
3576 .field_comptime_partially_comptime = .{
3577 .tag = .member,
3578 .attrs = &.{
3579 .{ .const_expr, .flag_present },
3580 .{ .name, .strp },
3581 .{ .type, .ref_addr },
3582 .{ .const_value, .block },
3583 .{ .ZIG_comptime_value, .ref_addr },
3584 },
3585 },
3586 .field_comptime_fully_comptime = .{
32023587 .tag = .member,
32033588 .attrs = &.{
32043589 .{ .const_expr, .flag_present },
......@@ -3441,6 +3826,15 @@ pub const AbbrevCode = enum {
34413826 .{ .declaration, .flag },
34423827 },
34433828 },
3829 .capturing_empty_struct_type = .{
3830 .tag = .structure_type,
3831 .children = true,
3832 .attrs = &.{
3833 .{ .decl_file, .udata },
3834 .{ .name, .strp },
3835 .{ .declaration, .flag },
3836 },
3837 },
34443838 .struct_type = .{
34453839 .tag = .structure_type,
34463840 .children = true,
......@@ -3504,6 +3898,45 @@ pub const AbbrevCode = enum {
35043898 .{ .type, .ref_addr },
35053899 },
35063900 },
3901 .comptime_capture = .{
3902 .tag = .template_value_parameter,
3903 .attrs = &.{
3904 .{ .name, .strp },
3905 .{ .type, .ref_addr },
3906 },
3907 },
3908 .comptime_capture_runtime = .{
3909 .tag = .template_value_parameter,
3910 .attrs = &.{
3911 .{ .name, .strp },
3912 .{ .type, .ref_addr },
3913 .{ .const_value, .block },
3914 },
3915 },
3916 .comptime_capture_partially_comptime = .{
3917 .tag = .template_value_parameter,
3918 .attrs = &.{
3919 .{ .name, .strp },
3920 .{ .type, .ref_addr },
3921 .{ .const_value, .block },
3922 .{ .ZIG_comptime_value, .ref_addr },
3923 },
3924 },
3925 .comptime_capture_fully_comptime = .{
3926 .tag = .template_value_parameter,
3927 .attrs = &.{
3928 .{ .name, .strp },
3929 .{ .type, .ref_addr },
3930 .{ .ZIG_comptime_value, .ref_addr },
3931 },
3932 },
3933 .runtime_capture = .{
3934 .tag = .template_type_parameter,
3935 .attrs = &.{
3936 .{ .name, .strp },
3937 .{ .type, .ref_addr },
3938 },
3939 },
35073940 .builtin_extern_nullary_func = .{
35083941 .tag = .subprogram,
35093942 .attrs = &.{
......@@ -3577,14 +4010,14 @@ pub const AbbrevCode = enum {
35774010 .tag = .formal_parameter,
35784011 .attrs = &.{
35794012 .{ .name, .strp },
3580 //.{ .type, .ref_addr },
4013 .{ .type, .ref_addr },
35814014 .{ .location, .exprloc },
35824015 },
35834016 },
35844017 .unnamed_arg = .{
35854018 .tag = .formal_parameter,
35864019 .attrs = &.{
3587 //.{ .type, .ref_addr },
4020 .{ .type, .ref_addr },
35884021 .{ .location, .exprloc },
35894022 },
35904023 },
......@@ -3593,80 +4026,80 @@ pub const AbbrevCode = enum {
35934026 .attrs = &.{
35944027 .{ .const_expr, .flag_present },
35954028 .{ .name, .strp },
3596 //.{ .type, .ref_addr },
4029 .{ .type, .ref_addr },
35974030 },
35984031 },
3599 .unnamed_comptime_arg = .{
4032 .comptime_arg_fully_runtime = .{
36004033 .tag = .formal_parameter,
36014034 .attrs = &.{
36024035 .{ .const_expr, .flag_present },
3603 //.{ .type, .ref_addr },
4036 .{ .name, .strp },
4037 .{ .type, .ref_addr },
4038 .{ .const_value, .block },
36044039 },
36054040 },
3606 .comptime_arg_runtime_bits = .{
4041 .comptime_arg_partially_comptime = .{
36074042 .tag = .formal_parameter,
36084043 .attrs = &.{
36094044 .{ .const_expr, .flag_present },
36104045 .{ .name, .strp },
3611 //.{ .type, .ref_addr },
4046 .{ .type, .ref_addr },
36124047 .{ .const_value, .block },
4048 .{ .ZIG_comptime_value, .ref_addr },
36134049 },
36144050 },
3615 .unnamed_comptime_arg_runtime_bits = .{
4051 .comptime_arg_fully_comptime = .{
36164052 .tag = .formal_parameter,
36174053 .attrs = &.{
36184054 .{ .const_expr, .flag_present },
3619 //.{ .type, .ref_addr },
3620 .{ .const_value, .block },
4055 .{ .name, .strp },
4056 .{ .type, .ref_addr },
4057 .{ .ZIG_comptime_value, .ref_addr },
36214058 },
36224059 },
3623 .comptime_arg_comptime_state = .{
4060 .unnamed_comptime_arg = .{
36244061 .tag = .formal_parameter,
36254062 .attrs = &.{
36264063 .{ .const_expr, .flag_present },
3627 .{ .name, .strp },
3628 //.{ .type, .ref_addr },
3629 //.{ .ZIG_comptime_value, .ref_addr },
4064 .{ .type, .ref_addr },
36304065 },
36314066 },
3632 .unnamed_comptime_arg_comptime_state = .{
4067 .unnamed_comptime_arg_fully_runtime = .{
36334068 .tag = .formal_parameter,
36344069 .attrs = &.{
36354070 .{ .const_expr, .flag_present },
3636 //.{ .type, .ref_addr },
3637 //.{ .ZIG_comptime_value, .ref_addr },
4071 .{ .type, .ref_addr },
4072 .{ .const_value, .block },
36384073 },
36394074 },
3640 .comptime_arg_runtime_bits_comptime_state = .{
4075 .unnamed_comptime_arg_partially_comptime = .{
36414076 .tag = .formal_parameter,
36424077 .attrs = &.{
36434078 .{ .const_expr, .flag_present },
3644 .{ .name, .strp },
3645 //.{ .type, .ref_addr },
4079 .{ .type, .ref_addr },
36464080 .{ .const_value, .block },
3647 //.{ .ZIG_comptime_value, .ref_addr },
4081 .{ .ZIG_comptime_value, .ref_addr },
36484082 },
36494083 },
3650 .unnamed_comptime_arg_runtime_bits_comptime_state = .{
4084 .unnamed_comptime_arg_fully_comptime = .{
36514085 .tag = .formal_parameter,
36524086 .attrs = &.{
36534087 .{ .const_expr, .flag_present },
3654 //.{ .type, .ref_addr },
3655 .{ .const_value, .block },
3656 //.{ .ZIG_comptime_value, .ref_addr },
4088 .{ .type, .ref_addr },
4089 .{ .ZIG_comptime_value, .ref_addr },
36574090 },
36584091 },
36594092 .extern_param = .{
36604093 .tag = .formal_parameter,
36614094 .attrs = &.{
3662 //.{ .type, .ref_addr },
4095 .{ .type, .ref_addr },
36634096 },
36644097 },
36654098 .local_var = .{
36664099 .tag = .variable,
36674100 .attrs = &.{
36684101 .{ .name, .strp },
3669 //.{ .type, .ref_addr },
4102 .{ .type, .ref_addr },
36704103 .{ .location, .exprloc },
36714104 },
36724105 },
......@@ -3674,32 +4107,32 @@ pub const AbbrevCode = enum {
36744107 .tag = .constant,
36754108 .attrs = &.{
36764109 .{ .name, .strp },
3677 //.{ .type, .ref_addr },
4110 .{ .type, .ref_addr },
36784111 },
36794112 },
3680 .local_const_runtime_bits = .{
4113 .local_const_fully_runtime = .{
36814114 .tag = .constant,
36824115 .attrs = &.{
36834116 .{ .name, .strp },
3684 //.{ .type, .ref_addr },
4117 .{ .type, .ref_addr },
36854118 .{ .const_value, .block },
36864119 },
36874120 },
3688 .local_const_comptime_state = .{
4121 .local_const_partially_comptime = .{
36894122 .tag = .constant,
36904123 .attrs = &.{
36914124 .{ .name, .strp },
3692 //.{ .type, .ref_addr },
3693 //.{ .ZIG_comptime_value, .ref_addr },
4125 .{ .type, .ref_addr },
4126 .{ .const_value, .block },
4127 .{ .ZIG_comptime_value, .ref_addr },
36944128 },
36954129 },
3696 .local_const_runtime_bits_comptime_state = .{
4130 .local_const_fully_comptime = .{
36974131 .tag = .constant,
36984132 .attrs = &.{
36994133 .{ .name, .strp },
3700 //.{ .type, .ref_addr },
3701 .{ .const_value, .block },
3702 //.{ .ZIG_comptime_value, .ref_addr },
4134 .{ .type, .ref_addr },
4135 .{ .ZIG_comptime_value, .ref_addr },
37034136 },
37044137 },
37054138 .undefined_comptime_value = .{
src/link/Elf2.zig+138-48
......@@ -220,6 +220,7 @@ dwarf_units: []dwarf_relocs.Unit,
220220dwarf_consts: std.array_hash_map.Auto(link.ConstPool.Index, dwarf_relocs.Const),
221221dwarf_globals: std.ArrayList(dwarf_relocs.Global),
222222dwarf_funcs: std.ArrayList(dwarf_relocs.Func),
223dwarf_decls: std.array_hash_map.Auto(Dwarf.Decl.Index, dwarf_relocs.Decl),
223224
224225overflowed_reloc_count: u32,
225226misaligned_reloc_count: u32,
......@@ -300,6 +301,7 @@ const Node = union(enum) {
300301 func_frame_fde: Dwarf.Func.Index,
301302 func_debug_info: Dwarf.Func.Index,
302303 func_debug_line: Dwarf.Func.Index,
304 decl_debug_info: Dwarf.Decl.Index,
303305
304306 pub const InputIndex = enum(u32) {
305307 _,
......@@ -884,6 +886,10 @@ const dwarf_relocs = struct {
884886 debug_line_first_symbol_reloc: SymbolReloc.Index,
885887 debug_line_first_node_reloc: NodeReloc.Index,
886888 };
889 const Decl = struct {
890 debug_info_first_target_reloc: NodeReloc.Index,
891 debug_info_first_node_reloc: NodeReloc.Index,
892 };
887893};
888894
889895pub const MachineRelocType = union {
......@@ -1874,6 +1880,7 @@ const NodeReloc = struct {
18741880 .const_debug_info => |cpi| &elf.dwarf_consts.getPtr(cpi).?.debug_info_first_target_reloc,
18751881 .global_debug_info => |gi| &elf.dwarf_globals.items[@backingInt(gi)].debug_info_first_target_reloc,
18761882 .func_debug_info => |fi| &elf.dwarf_funcs.items[@backingInt(fi)].debug_info_first_target_reloc,
1883 .decl_debug_info => |di| &elf.dwarf_decls.getPtr(di).?.debug_info_first_target_reloc,
18771884 };
18781885 first_target_reloc.* = reloc.next;
18791886 },
......@@ -3330,6 +3337,7 @@ pub fn symbolForAtom(elf: *Elf, atom: link.File.AtomId) link.File.SymbolId {
33303337 .func_frame_fde,
33313338 .func_debug_info,
33323339 .func_debug_line,
3340 .decl_debug_info,
33333341 => unreachable,
33343342 inline .nav,
33353343 .uav,
......@@ -3838,6 +3846,7 @@ fn create(
38383846 .dwarf_consts = .empty,
38393847 .dwarf_globals = .empty,
38403848 .dwarf_funcs = .empty,
3849 .dwarf_decls = .empty,
38413850
38423851 .overflowed_reloc_count = 0,
38433852 .misaligned_reloc_count = 0,
......@@ -3893,6 +3902,7 @@ pub fn deinit(elf: *Elf) void {
38933902 elf.dwarf_consts.deinit(gpa);
38943903 elf.dwarf_globals.deinit(gpa);
38953904 elf.dwarf_funcs.deinit(gpa);
3905 elf.dwarf_decls.deinit(gpa);
38963906
38973907 elf.* = undefined;
38983908}
......@@ -5193,6 +5203,7 @@ fn getNodeShndx(elf: *const Elf, ni: MappedFile.Node.Index) Section.Index {
51935203 .func_frame_fde,
51945204 .func_debug_info,
51955205 .func_debug_line,
5206 .decl_debug_info,
51965207 => elf.getNode(ni.parent(&elf.mf).unwrap().?.parent(&elf.mf).unwrap().?).section,
51975208 };
51985209}
......@@ -5230,6 +5241,7 @@ fn getNodeVAddr(elf: *Elf, ni: MappedFile.Node.Index) u64 {
52305241 .func_frame_fde,
52315242 .func_debug_info,
52325243 .func_debug_line,
5244 .decl_debug_info,
52335245 => elf.computeNodeVAddr(ni),
52345246 };
52355247}
......@@ -5269,6 +5281,7 @@ fn computeNodeVAddr(elf: *Elf, ni: MappedFile.Node.Index) u64 {
52695281 .func_frame_fde,
52705282 .func_debug_info,
52715283 .func_debug_line,
5284 .decl_debug_info,
52725285 => unreachable,
52735286 };
52745287 const offset, _ = ni.location(&elf.mf).resolve(&elf.mf);
......@@ -5304,6 +5317,7 @@ fn computeNodeSectionOffset(elf: *Elf, ni: MappedFile.Node.Index) u64 {
53045317 .func_frame_fde,
53055318 .func_debug_info,
53065319 .func_debug_line,
5320 .decl_debug_info,
53075321 => unreachable,
53085322 };
53095323 const offset, _ = ni.location(&elf.mf).resolve(&elf.mf);
......@@ -5394,6 +5408,9 @@ fn resetNodeRelocs(elf: *Elf, ni: MappedFile.Node.Index) void {
53945408 .first_node_reloc = &elf.dwarf_funcs.items[@backingInt(fi)].debug_line_first_node_reloc,
53955409 .skip_node_relocs = fi.get(&elf.dwarf).debug_info_ni,
53965410 },
5411 .decl_debug_info => |di| .{
5412 .first_node_reloc = &elf.dwarf_decls.getPtr(di).?.debug_info_first_node_reloc,
5413 },
53975414 };
53985415
53995416 if (opts.first_symbol_reloc) |ptr| {
......@@ -8150,6 +8167,7 @@ fn addNodeRelocAssumeCapacity(
81508167 .const_debug_info => |cpi| &elf.dwarf_consts.getPtr(cpi).?.debug_info_first_target_reloc,
81518168 .global_debug_info => |gi| &elf.dwarf_globals.items[@backingInt(gi)].debug_info_first_target_reloc,
81528169 .func_debug_info => |fi| &elf.dwarf_funcs.items[@backingInt(fi)].debug_info_first_target_reloc,
8170 .decl_debug_info => |di| &elf.dwarf_decls.getPtr(di).?.debug_info_first_target_reloc,
81538171 };
81548172 const next = first_target_reloc.*;
81558173 const ri: NodeReloc.Index = @fromBackingInt(@intCast(elf.node_relocs.items.len));
......@@ -8260,6 +8278,7 @@ fn addGotRelocAssumeCapacity(
82608278 .func_frame_fde,
82618279 .func_debug_info,
82628280 .func_debug_line,
8281 .decl_debug_info,
82638282 => unreachable, // cannot contain relocs,
82648283 .section,
82658284 .section_manual_size,
......@@ -8607,7 +8626,10 @@ pub fn updateContainerTypeInner(
86078626) Error!void {
86088627 switch (elf.base.comp.config.debug_format) {
86098628 .strip => {},
8610 .dwarf => try elf.dwarf.const_pool.updateContainerType(pt, .{ .elf2 = elf }, ty, success),
8629 .dwarf => {
8630 try elf.dwarf.const_pool.updateContainerType(pt, .{ .elf2 = elf }, ty, success);
8631 try elf.dwarf.const_pool.flushPending(pt, .{ .elf2 = elf });
8632 },
86118633 .code_view => unreachable,
86128634 }
86138635 if (!success) return;
......@@ -8630,13 +8652,9 @@ pub fn addConst(
86308652 .dwarf => {
86318653 const gpa = elf.base.comp.gpa;
86328654 try elf.nodes.ensureUnusedCapacity(gpa, 1);
8655 try elf.dwarf.consts.ensureUnusedCapacity(gpa, 1);
86338656 try elf.dwarf_consts.ensureUnusedCapacity(gpa, 1);
86348657 try elf.dwarf.addConst(cpi, val, &addConstNode);
8635 elf.dwarf_consts.putAssumeCapacity(cpi, .{
8636 .debug_info_first_target_reloc = .none,
8637 .debug_info_first_symbol_reloc = .none,
8638 .debug_info_first_node_reloc = .none,
8639 });
86408658 },
86418659 .code_view => unreachable,
86428660 }
......@@ -8644,7 +8662,7 @@ pub fn addConst(
86448662fn addConstNode(lf: *link.File, ui: Dwarf.Unit.Index, cpi: link.ConstPool.Index) link.Error!MappedFile.Node.Index {
86458663 const elf = lf.cast(.elf2).?;
86468664 const unit = ui.get(&elf.dwarf);
8647 return elf.addNodeAssumeCapacity(
8665 const debug_info_ni = elf.addNodeAssumeCapacity(
86488666 unit.debug_info_ni.unwrap().?.addFloatingChild(lf.comp.gpa, &elf.mf, .{
86498667 .enable_next_moved = true,
86508668 }) catch |err| switch (err) {
......@@ -8655,6 +8673,12 @@ fn addConstNode(lf: *link.File, ui: Dwarf.Unit.Index, cpi: link.ConstPool.Index)
86558673 },
86568674 .{ .const_debug_info = cpi },
86578675 );
8676 elf.dwarf_consts.putAssumeCapacityNoClobber(cpi, .{
8677 .debug_info_first_target_reloc = .none,
8678 .debug_info_first_symbol_reloc = .none,
8679 .debug_info_first_node_reloc = .none,
8680 });
8681 return debug_info_ni;
86588682}
86598683
86608684pub fn updateConst(
......@@ -8674,18 +8698,20 @@ fn updateConstInner(
86748698 cpi: link.ConstPool.Index,
86758699 val: InternPool.Index,
86768700) link.Error!void {
8677 if (val == .anyerror_type) return; // handled in `updateErrorData` instead
86788701 switch (elf.base.comp.config.debug_format) {
86798702 .strip => {},
86808703 .dwarf => {
8681 const gpa = elf.base.comp.gpa;
8682 const debug_info_ni = Dwarf.Const.get(cpi, &elf.dwarf).debug_info_ni.unwrap().?;
8683 try debug_info_ni.moved(gpa, &elf.mf);
8684 var di_nw: MappedFile.Node.Writer = undefined;
8685 debug_info_ni.writer(gpa, &elf.mf, &di_nw);
8686 defer di_nw.deinit();
8687 elf.resetNodeRelocs(debug_info_ni);
8688 try elf.dwarf.updateConst(pt, &di_nw, val);
8704 {
8705 const gpa = elf.base.comp.gpa;
8706 const debug_info_ni = Dwarf.Const.get(cpi, &elf.dwarf).debug_info_ni.unwrap().?;
8707 try debug_info_ni.moved(gpa, &elf.mf);
8708 var di_nw: MappedFile.Node.Writer = undefined;
8709 debug_info_ni.writer(gpa, &elf.mf, &di_nw);
8710 defer di_nw.deinit();
8711 elf.resetNodeRelocs(debug_info_ni);
8712 try elf.dwarf.updateConst(pt, &di_nw, val);
8713 }
8714 try elf.genPending(pt);
86898715 },
86908716 .code_view => unreachable,
86918717 }
......@@ -8700,14 +8726,17 @@ pub fn updateConstIncomplete(
87008726 switch (elf.base.comp.config.debug_format) {
87018727 .strip => {},
87028728 .dwarf => {
8703 const gpa = elf.base.comp.gpa;
8704 const debug_info_ni = Dwarf.Const.get(cpi, &elf.dwarf).debug_info_ni.unwrap().?;
8705 try debug_info_ni.moved(gpa, &elf.mf);
8706 var di_nw: MappedFile.Node.Writer = undefined;
8707 debug_info_ni.writer(gpa, &elf.mf, &di_nw);
8708 defer di_nw.deinit();
8709 elf.resetNodeRelocs(debug_info_ni);
8710 try elf.dwarf.updateConstIncomplete(pt, &di_nw, val);
8729 {
8730 const gpa = elf.base.comp.gpa;
8731 const debug_info_ni = Dwarf.Const.get(cpi, &elf.dwarf).debug_info_ni.unwrap().?;
8732 try debug_info_ni.moved(gpa, &elf.mf);
8733 var di_nw: MappedFile.Node.Writer = undefined;
8734 debug_info_ni.writer(gpa, &elf.mf, &di_nw);
8735 defer di_nw.deinit();
8736 elf.resetNodeRelocs(debug_info_ni);
8737 try elf.dwarf.updateConstIncomplete(pt, &di_nw, val);
8738 }
8739 try elf.genPending(pt);
87118740 },
87128741 .code_view => unreachable,
87138742 }
......@@ -8843,7 +8872,9 @@ fn updateFuncInner(
88438872 debug.blocks = .empty;
88448873
88458874 const debug_info_ni = dwarf_func.debug_info_ni.unwrap().?;
8846 try dwarf.decls.put(zcu.comp.gpa, src_inst, debug_info_ni);
8875 try dwarf.decls.put(zcu.comp.gpa, src_inst, .{
8876 .debug_info_ni = debug_info_ni.toOptional(),
8877 });
88478878 try debug_info_ni.moved(gpa, &elf.mf);
88488879 try debug_info_ni.nextMoved(gpa, &elf.mf);
88498880 debug_info_ni.writer(gpa, &elf.mf, &debug.info_writer);
......@@ -8983,31 +9014,28 @@ pub fn lostTracking(
89839014 _: Zcu.PerThread,
89849015 inst: InternPool.TrackedInst.Index,
89859016) link.Error!void {
8986 const decl_ni = elf.dwarf.decls.get(inst) orelse return;
9017 const di = elf.dwarf.getDeclIfExists(inst) orelse return;
9018 const decl_ni = di.get(&elf.dwarf).debug_info_ni.unwrap() orelse return;
89879019 const comp = elf.base.comp;
89889020 var diw: std.Io.Writer = .fixed(decl_ni.slice(&elf.mf));
89899021 elf.resetNodeRelocs(decl_ni);
8990 diw.writeUleb128(try elf.dwarf.refAbbrevCode(.decl_lost)) catch unreachable;
9022 elf.dwarf.lostTracking(&diw) catch |err| switch (err) {
9023 else => |e| return e,
9024 error.WriteFailed => unreachable,
9025 };
89919026 decl_ni.resizeLeaf(comp.gpa, &elf.mf, diw.end) catch |err| switch (err) {
9027 else => |e| return e,
89929028 error.MappedFileIo => return comp.link_diags.fail("failed to write output file: {t}", .{
89939029 elf.mf.io_err.?,
89949030 }),
8995 else => |e| return e,
89969031 };
89979032}
89989033
89999034pub fn updateErrorData(elf: *Elf, pt: Zcu.PerThread) link.Error!void {
9000 const comp = elf.base.comp;
9001 if (elf.lazy.getPtr(.const_data).map.getIndex(.anyerror_type)) |lmi| elf.genLazyInner(pt, .{
9035 if (elf.lazy.getPtr(.const_data).map.getIndex(.anyerror_type)) |lmi| try elf.genLazyInner(pt, .{
90029036 .kind = .const_data,
90039037 .index = @intCast(lmi),
9004 }) catch |err| switch (err) {
9005 else => |e| return e,
9006 error.MappedFileIo => return comp.link_diags.fail(
9007 "failed to write output file: {t}",
9008 .{elf.mf.io_err.?},
9009 ),
9010 };
9038 });
90119039 if (elf.dwarf.const_pool.getIfExists(.anyerror_type)) |cpi|
90129040 try elf.updateConstInner(pt, cpi, .anyerror_type);
90139041}
......@@ -9287,8 +9315,9 @@ fn idleProgNode(
92879315 Value.fromInterned(umi.uavValue(elf)).fmtValue(.{ .zcu = elf.base.comp.zcu.?, .tid = tid }),
92889316 }) catch &name,
92899317 .debug_shared => |ss| switch (ss) {
9290 .debug_abbrev, .debug_str, .debug_str_offsets => "debug info",
9291 .debug_line_str => "line info",
9318 .debug_abbrev => "debug info abbrevs",
9319 .debug_str, .debug_str_offsets => "debug info strings",
9320 .debug_line_str => "line info strings",
92929321 },
92939322 .unit_frame,
92949323 .unit_frame_cie,
......@@ -9330,10 +9359,17 @@ fn idleProgNode(
93309359 ip.getNav(fi.nav(&elf.dwarf)).fqn.fmt(ip),
93319360 }) catch &name;
93329361 },
9362 .decl_debug_info => |di| {
9363 const comp = elf.base.comp;
9364 const zcu = comp.zcu.?;
9365 break :name std.mem.print(&name, "debug info for {f}", .{
9366 zcu.fileByIndex(di.srcInst(&elf.dwarf).resolveFile(&zcu.intern_pool)).path.fmt(comp),
9367 }) catch &name;
9368 },
93339369 }, 0);
93349370}
93359371
9336fn genPending(elf: *Elf, pt: Zcu.PerThread) Error!void {
9372fn genPending(elf: *Elf, pt: Zcu.PerThread) link.Error!void {
93379373 while (elf.pending_uavs.pop()) |umi| {
93389374 var prog_name_buf: [std.Progress.Node.max_name_len]u8 = undefined;
93399375 const prog_name = std.mem.print(&prog_name_buf, "{f}", .{
......@@ -9350,7 +9386,20 @@ fn genPending(elf: *Elf, pt: Zcu.PerThread) Error!void {
93509386 };
93519387 switch (elf.base.comp.config.debug_format) {
93529388 .strip => {},
9353 .dwarf => try elf.dwarf.const_pool.flushPending(pt, .{ .elf2 = elf }),
9389 .dwarf => {
9390 const gpa = elf.base.comp.gpa;
9391 while (elf.dwarf.pending_decl) |pending| {
9392 elf.dwarf.pending_decl = null;
9393 const debug_info_ni = pending.di.get(&elf.dwarf).debug_info_ni.unwrap().?;
9394 try debug_info_ni.moved(gpa, &elf.mf);
9395 var di_nw: MappedFile.Node.Writer = undefined;
9396 debug_info_ni.writer(gpa, &elf.mf, &di_nw);
9397 defer di_nw.deinit();
9398 elf.resetNodeRelocs(debug_info_ni);
9399 try elf.dwarf.genDecl(pt, &di_nw, pending.instance_val);
9400 }
9401 try elf.dwarf.const_pool.flushPending(pt, .{ .elf2 = elf });
9402 },
93549403 .code_view => unreachable,
93559404 }
93569405}
......@@ -9359,8 +9408,9 @@ fn genUav(
93599408 elf: *Elf,
93609409 pt: Zcu.PerThread,
93619410 umi: Node.UavMapIndex,
9362) Error!void {
9363 const gpa = elf.base.comp.gpa;
9411) link.Error!void {
9412 const comp = elf.base.comp;
9413 const gpa = comp.gpa;
93649414
93659415 const uav_val = umi.uavValue(elf);
93669416 const ni = umi.symbol(elf).index().ptr(elf).node.unwrap().?;
......@@ -9377,7 +9427,10 @@ fn genUav(
93779427 .{ .atom_index = Node.toAtom(ni) },
93789428 ) catch |err| switch (err) {
93799429 else => |e| return e,
9380 error.WriteFailed => return nw.err.?,
9430 error.WriteFailed => switch (nw.err.?) {
9431 else => |e| return e,
9432 error.MappedFileIo => return comp.link_diags.fail("failed to write output file: {t}", .{elf.mf.io_err.?}),
9433 },
93819434 };
93829435 switch (elf.symPtr(umi.symbol(elf).index())) {
93839436 inline else => |sym| elf.targetStore(&sym.size, @intCast(nw.interface.end)),
......@@ -9387,7 +9440,7 @@ fn genUav(
93879440 assert(ni.hasMoved(&elf.mf));
93889441}
93899442
9390fn genLazy(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) Error!void {
9443fn genLazy(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) link.Error!void {
93919444 const lazy = lmr.lazySymbol(elf);
93929445 if (lazy.ty == .anyerror_type) return;
93939446 const lazy_ty: Type = .fromInterned(lazy.ty);
......@@ -9404,7 +9457,7 @@ fn genLazy(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) Error!void {
94049457 defer prog_node.end();
94059458 try elf.genLazyInner(pt, lmr);
94069459}
9407fn genLazyInner(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) Error!void {
9460fn genLazyInner(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) link.Error!void {
94089461 const zcu = pt.zcu;
94099462 const gpa = zcu.gpa;
94109463
......@@ -9430,7 +9483,13 @@ fn genLazyInner(elf: *Elf, pt: Zcu.PerThread, lmr: Node.LazyMapRef) Error!void {
94309483 .{ .atom_index = Node.toAtom(ni) },
94319484 ) catch |err| switch (err) {
94329485 else => |e| return e,
9433 error.WriteFailed => return nw.err.?,
9486 error.WriteFailed => return switch (nw.err.?) {
9487 else => |e| return e,
9488 error.MappedFileIo => return elf.base.comp.link_diags.fail(
9489 "failed to write output file: {t}",
9490 .{elf.mf.io_err.?},
9491 ),
9492 },
94349493 };
94359494 switch (elf.symPtr(lmr.symbol(elf).index())) {
94369495 inline else => |sym| elf.targetStore(&sym.size, @intCast(nw.interface.end)),
......@@ -9873,6 +9932,20 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
98739932 .skip_node_relocs = fi.get(&elf.dwarf).debug_info_ni,
98749933 });
98759934 },
9935 .decl_debug_info => |di| {
9936 const dwarf_decl = &elf.dwarf_decls.get(di).?;
9937 const target_section_offset = elf.computeNodeSectionOffset(ni);
9938 var target_ri = dwarf_decl.debug_info_first_target_reloc;
9939 while (target_ri != .none) {
9940 const target_reloc = target_ri.get(elf);
9941 assert(target_reloc.target == ni);
9942 target_reloc.flushMovedTarget(elf, target_section_offset);
9943 target_ri = target_reloc.next;
9944 }
9945 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
9946 .first_node_reloc = dwarf_decl.debug_info_first_node_reloc,
9947 });
9948 },
98769949 }
98779950 try ni.childrenMoved(elf.base.comp.gpa, &elf.mf);
98789951}
......@@ -10124,6 +10197,7 @@ fn flushResized(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!vo
1012410197 .func_frame_fde,
1012510198 .func_debug_info,
1012610199 .func_debug_line,
10200 .decl_debug_info,
1012710201 => {},
1012810202 }
1012910203}
......@@ -10188,6 +10262,7 @@ fn flushNextMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!
1018810262 .func_frame_fde,
1018910263 .func_debug_info,
1019010264 .func_debug_line,
10265 .decl_debug_info,
1019110266 => |_, tag| {
1019210267 const offset, const size = ni.location(&elf.mf).resolve(&elf.mf);
1019310268 const parent_ni = ni.parent(&elf.mf).unwrap().?;
......@@ -10240,6 +10315,7 @@ fn flushNextMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!
1024010315 .global_debug_info,
1024110316 .func_debug_info,
1024210317 .func_debug_line,
10318 .decl_debug_info,
1024310319 => {
1024410320 const parent_offset, _ = parent_ni.location(&elf.mf).resolve(&elf.mf);
1024510321 const debug_ni = parent_ni.parent(&elf.mf).unwrap().?;
......@@ -10255,6 +10331,7 @@ fn flushNextMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!
1025510331 .const_debug_info,
1025610332 .global_debug_info,
1025710333 .func_debug_info,
10334 .decl_debug_info,
1025810335 => for (0..2) |_| fw.writeUleb128(@backingInt(Dwarf.AbbrevCode.null)) catch unreachable,
1025910336 .unit_debug_line_header, .func_debug_line => {},
1026010337 }
......@@ -10266,7 +10343,7 @@ fn flushNextMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!
1026610343 elf.dwarf.updateUnitLength(fw.buffer, fw.buffer.len);
1026710344 switch (tag) {
1026810345 else => unreachable,
10269 .unit_debug_info_header, .const_debug_info, .global_debug_info, .func_debug_info => {
10346 .unit_debug_info_header, .const_debug_info, .global_debug_info, .func_debug_info, .decl_debug_info => {
1027010347 comptime assert(Dwarf.uleb128Size(@backingInt(Dwarf.AbbrevCode.null)) == 1);
1027110348 @memset(fw.unusedCapacitySlice(), @backingInt(Dwarf.AbbrevCode.null));
1027210349 },
......@@ -10293,6 +10370,7 @@ fn flushNextMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!
1029310370 .const_debug_info,
1029410371 .global_debug_info,
1029510372 .func_debug_info,
10373 .decl_debug_info,
1029610374 => elf.dwarf.genDebugInfoPadding(&fw, fw.buffer.len) catch unreachable,
1029710375 .unit_debug_line_header,
1029810376 .func_debug_line,
......@@ -10864,6 +10942,18 @@ pub fn printNode(
1086410942 nav.fqn.fmt(ip),
1086510943 });
1086610944 },
10945 .decl_debug_info => |di| {
10946 const comp = elf.base.comp;
10947 const zcu = comp.zcu.?;
10948 const ip = &zcu.intern_pool;
10949 const src_inst = di.srcInst(&elf.dwarf);
10950 try w.print("({f}, ", .{zcu.fileByIndex(src_inst.resolveFile(ip)).path.fmt(comp)});
10951 if (src_inst.resolve(ip)) |inst|
10952 try w.print("%{d}", .{inst})
10953 else
10954 try w.writeAll("lost");
10955 try w.writeByte(')');
10956 },
1086710957 }
1086810958 {
1086910959 const mf_node = &elf.mf.nodes.items[@backingInt(ni)];