authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-09-08 20:08:44-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-09-10 12:27:57-04:00
loge0469773542e49c9a76df6746afa10f22d44dae4
tree439c5f47cef779ece68ab57c812975241715620a
parent6459212ebe20da1607ea3b56bff37b7ace019343

codegen: implement output to the `.debug_info` section


20 files changed, 339 insertions(+), 248 deletions(-)

src/arch/aarch64/CodeGen.zig+2-3
......@@ -25,7 +25,6 @@ const Alignment = InternPool.Alignment;
2525
2626const CodeGenError = codegen.CodeGenError;
2727const Result = codegen.Result;
28const DebugInfoOutput = codegen.DebugInfoOutput;
2928
3029const bits = @import("bits.zig");
3130const abi = @import("abi.zig");
......@@ -48,7 +47,7 @@ pt: Zcu.PerThread,
4847air: Air,
4948liveness: Liveness,
5049bin_file: *link.File,
51debug_output: DebugInfoOutput,
50debug_output: link.File.DebugInfoOutput,
5251target: *const std.Target,
5352func_index: InternPool.Index,
5453owner_nav: InternPool.Nav.Index,
......@@ -327,7 +326,7 @@ pub fn generate(
327326 air: Air,
328327 liveness: Liveness,
329328 code: *std.ArrayList(u8),
330 debug_output: DebugInfoOutput,
329 debug_output: link.File.DebugInfoOutput,
331330) CodeGenError!Result {
332331 const zcu = pt.zcu;
333332 const gpa = zcu.gpa;
src/arch/aarch64/Emit.zig+1-2
......@@ -13,11 +13,10 @@ const assert = std.debug.assert;
1313const Instruction = bits.Instruction;
1414const Register = bits.Register;
1515const log = std.log.scoped(.aarch64_emit);
16const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
1716
1817mir: Mir,
1918bin_file: *link.File,
20debug_output: DebugInfoOutput,
19debug_output: link.File.DebugInfoOutput,
2120target: *const std.Target,
2221err_msg: ?*ErrorMsg = null,
2322src_loc: Zcu.LazySrcLoc,
src/arch/arm/CodeGen.zig+2-3
......@@ -25,7 +25,6 @@ const Alignment = InternPool.Alignment;
2525
2626const Result = codegen.Result;
2727const CodeGenError = codegen.CodeGenError;
28const DebugInfoOutput = codegen.DebugInfoOutput;
2928
3029const bits = @import("bits.zig");
3130const abi = @import("abi.zig");
......@@ -49,7 +48,7 @@ pt: Zcu.PerThread,
4948air: Air,
5049liveness: Liveness,
5150bin_file: *link.File,
52debug_output: DebugInfoOutput,
51debug_output: link.File.DebugInfoOutput,
5352target: *const std.Target,
5453func_index: InternPool.Index,
5554err_msg: ?*ErrorMsg,
......@@ -335,7 +334,7 @@ pub fn generate(
335334 air: Air,
336335 liveness: Liveness,
337336 code: *std.ArrayList(u8),
338 debug_output: DebugInfoOutput,
337 debug_output: link.File.DebugInfoOutput,
339338) CodeGenError!Result {
340339 const zcu = pt.zcu;
341340 const gpa = zcu.gpa;
src/arch/arm/Emit.zig+1-2
......@@ -16,12 +16,11 @@ const assert = std.debug.assert;
1616const Instruction = bits.Instruction;
1717const Register = bits.Register;
1818const log = std.log.scoped(.aarch32_emit);
19const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
2019const CodeGen = @import("CodeGen.zig");
2120
2221mir: Mir,
2322bin_file: *link.File,
24debug_output: DebugInfoOutput,
23debug_output: link.File.DebugInfoOutput,
2524target: *const std.Target,
2625err_msg: ?*ErrorMsg = null,
2726src_loc: Zcu.LazySrcLoc,
src/arch/riscv64/CodeGen.zig+3-4
......@@ -32,7 +32,6 @@ const Alignment = InternPool.Alignment;
3232
3333const CodeGenError = codegen.CodeGenError;
3434const Result = codegen.Result;
35const DebugInfoOutput = codegen.DebugInfoOutput;
3635
3736const bits = @import("bits.zig");
3837const abi = @import("abi.zig");
......@@ -61,7 +60,7 @@ gpa: Allocator,
6160
6261mod: *Package.Module,
6362target: *const std.Target,
64debug_output: DebugInfoOutput,
63debug_output: link.File.DebugInfoOutput,
6564err_msg: ?*ErrorMsg,
6665args: []MCValue,
6766ret_mcv: InstTracking,
......@@ -760,7 +759,7 @@ pub fn generate(
760759 air: Air,
761760 liveness: Liveness,
762761 code: *std.ArrayList(u8),
763 debug_output: DebugInfoOutput,
762 debug_output: link.File.DebugInfoOutput,
764763) CodeGenError!Result {
765764 const zcu = pt.zcu;
766765 const comp = zcu.comp;
......@@ -928,7 +927,7 @@ pub fn generateLazy(
928927 src_loc: Zcu.LazySrcLoc,
929928 lazy_sym: link.File.LazySymbol,
930929 code: *std.ArrayList(u8),
931 debug_output: DebugInfoOutput,
930 debug_output: link.File.DebugInfoOutput,
932931) CodeGenError!Result {
933932 const comp = bin_file.comp;
934933 const gpa = comp.gpa;
src/arch/riscv64/Emit.zig+1-2
......@@ -2,7 +2,7 @@
22
33bin_file: *link.File,
44lower: Lower,
5debug_output: DebugInfoOutput,
5debug_output: link.File.DebugInfoOutput,
66code: *std.ArrayList(u8),
77
88prev_di_line: u32,
......@@ -216,7 +216,6 @@ const log = std.log.scoped(.emit);
216216const mem = std.mem;
217217const std = @import("std");
218218
219const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
220219const Emit = @This();
221220const Lower = @import("Lower.zig");
222221const Mir = @import("Mir.zig");
src/arch/sparc64/CodeGen.zig+2-3
......@@ -22,7 +22,6 @@ const Liveness = @import("../../Liveness.zig");
2222const Type = @import("../../Type.zig");
2323const CodeGenError = codegen.CodeGenError;
2424const Result = @import("../../codegen.zig").Result;
25const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
2625const Endian = std.builtin.Endian;
2726const Alignment = InternPool.Alignment;
2827
......@@ -57,7 +56,7 @@ bin_file: *link.File,
5756target: *const std.Target,
5857func_index: InternPool.Index,
5958code: *std.ArrayList(u8),
60debug_output: DebugInfoOutput,
59debug_output: link.File.DebugInfoOutput,
6160err_msg: ?*ErrorMsg,
6261args: []MCValue,
6362ret_mcv: MCValue,
......@@ -268,7 +267,7 @@ pub fn generate(
268267 air: Air,
269268 liveness: Liveness,
270269 code: *std.ArrayList(u8),
271 debug_output: DebugInfoOutput,
270 debug_output: link.File.DebugInfoOutput,
272271) CodeGenError!Result {
273272 const zcu = pt.zcu;
274273 const gpa = zcu.gpa;
src/arch/sparc64/Emit.zig+1-2
......@@ -9,7 +9,6 @@ const Zcu = @import("../../Zcu.zig");
99const ErrorMsg = Zcu.ErrorMsg;
1010const Liveness = @import("../../Liveness.zig");
1111const log = std.log.scoped(.sparcv9_emit);
12const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
1312
1413const Emit = @This();
1514const Mir = @import("Mir.zig");
......@@ -19,7 +18,7 @@ const Register = bits.Register;
1918
2019mir: Mir,
2120bin_file: *link.File,
22debug_output: DebugInfoOutput,
21debug_output: link.File.DebugInfoOutput,
2322target: *const std.Target,
2423err_msg: ?*ErrorMsg = null,
2524src_loc: Zcu.LazySrcLoc,
src/arch/wasm/CodeGen.zig+2-2
......@@ -648,7 +648,7 @@ block_depth: u32 = 0,
648648air: Air,
649649liveness: Liveness,
650650gpa: mem.Allocator,
651debug_output: codegen.DebugInfoOutput,
651debug_output: link.File.DebugInfoOutput,
652652func_index: InternPool.Index,
653653/// Contains a list of current branches.
654654/// When we return from a branch, the branch will be popped from this list,
......@@ -1211,7 +1211,7 @@ pub fn generate(
12111211 air: Air,
12121212 liveness: Liveness,
12131213 code: *std.ArrayList(u8),
1214 debug_output: codegen.DebugInfoOutput,
1214 debug_output: link.File.DebugInfoOutput,
12151215) codegen.CodeGenError!codegen.Result {
12161216 const zcu = pt.zcu;
12171217 const gpa = zcu.gpa;
src/arch/wasm/Emit.zig+1-1
......@@ -26,7 +26,7 @@ owner_nav: InternPool.Nav.Index,
2626
2727// Debug information
2828/// Holds the debug information for this emission
29dbg_output: codegen.DebugInfoOutput,
29dbg_output: link.File.DebugInfoOutput,
3030/// Previous debug info line
3131prev_di_line: u32,
3232/// Previous debug info column
src/arch/x86_64/CodeGen.zig+3-4
......@@ -17,7 +17,6 @@ const Air = @import("../../Air.zig");
1717const Allocator = mem.Allocator;
1818const CodeGenError = codegen.CodeGenError;
1919const Compilation = @import("../../Compilation.zig");
20const DebugInfoOutput = codegen.DebugInfoOutput;
2120const ErrorMsg = Zcu.ErrorMsg;
2221const Result = codegen.Result;
2322const Emit = @import("Emit.zig");
......@@ -53,7 +52,7 @@ pt: Zcu.PerThread,
5352air: Air,
5453liveness: Liveness,
5554bin_file: *link.File,
56debug_output: DebugInfoOutput,
55debug_output: link.File.DebugInfoOutput,
5756target: *const std.Target,
5857owner: Owner,
5958inline_func: InternPool.Index,
......@@ -819,7 +818,7 @@ pub fn generate(
819818 air: Air,
820819 liveness: Liveness,
821820 code: *std.ArrayList(u8),
822 debug_output: DebugInfoOutput,
821 debug_output: link.File.DebugInfoOutput,
823822) CodeGenError!Result {
824823 const zcu = pt.zcu;
825824 const comp = zcu.comp;
......@@ -1000,7 +999,7 @@ pub fn generateLazy(
1000999 src_loc: Zcu.LazySrcLoc,
10011000 lazy_sym: link.File.LazySymbol,
10021001 code: *std.ArrayList(u8),
1003 debug_output: DebugInfoOutput,
1002 debug_output: link.File.DebugInfoOutput,
10041003) CodeGenError!Result {
10051004 const comp = bin_file.comp;
10061005 const gpa = comp.gpa;
src/arch/x86_64/Emit.zig+1-2
......@@ -3,7 +3,7 @@
33air: Air,
44lower: Lower,
55atom_index: u32,
6debug_output: DebugInfoOutput,
6debug_output: link.File.DebugInfoOutput,
77code: *std.ArrayList(u8),
88
99prev_di_line: u32,
......@@ -546,7 +546,6 @@ const log = std.log.scoped(.emit);
546546const std = @import("std");
547547
548548const Air = @import("../../Air.zig");
549const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
550549const Emit = @This();
551550const Lower = @import("Lower.zig");
552551const Mir = @import("Mir.zig");
src/codegen.zig+34-60
......@@ -38,12 +38,6 @@ pub const CodeGenError = error{
3838 CodegenFail,
3939} || link.File.UpdateDebugInfoError;
4040
41pub const DebugInfoOutput = union(enum) {
42 dwarf: *link.File.Dwarf.WipNav,
43 plan9: *link.File.Plan9.DebugInfoOutput,
44 none,
45};
46
4741fn devFeatureForBackend(comptime backend: std.builtin.CompilerBackend) dev.Feature {
4842 comptime assert(mem.startsWith(u8, @tagName(backend), "stage2_"));
4943 return @field(dev.Feature, @tagName(backend)["stage2_".len..] ++ "_backend");
......@@ -69,7 +63,7 @@ pub fn generateFunction(
6963 air: Air,
7064 liveness: Liveness,
7165 code: *std.ArrayList(u8),
72 debug_output: DebugInfoOutput,
66 debug_output: link.File.DebugInfoOutput,
7367) CodeGenError!Result {
7468 const zcu = pt.zcu;
7569 const func = zcu.funcInfo(func_index);
......@@ -95,7 +89,7 @@ pub fn generateLazyFunction(
9589 src_loc: Zcu.LazySrcLoc,
9690 lazy_sym: link.File.LazySymbol,
9791 code: *std.ArrayList(u8),
98 debug_output: DebugInfoOutput,
92 debug_output: link.File.DebugInfoOutput,
9993) CodeGenError!Result {
10094 const zcu = pt.zcu;
10195 const file = Type.fromInterned(lazy_sym.ty).typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(&zcu.intern_pool);
......@@ -127,10 +121,10 @@ pub fn generateLazySymbol(
127121 // TODO don't use an "out" parameter like this; put it in the result instead
128122 alignment: *Alignment,
129123 code: *std.ArrayList(u8),
130 debug_output: DebugInfoOutput,
131 reloc_info: RelocInfo,
124 debug_output: link.File.DebugInfoOutput,
125 reloc_parent: link.File.RelocInfo.Parent,
132126) CodeGenError!Result {
133 _ = reloc_info;
127 _ = reloc_parent;
134128
135129 const tracy = trace(@src());
136130 defer tracy.end();
......@@ -192,8 +186,7 @@ pub fn generateSymbol(
192186 src_loc: Zcu.LazySrcLoc,
193187 val: Value,
194188 code: *std.ArrayList(u8),
195 debug_output: DebugInfoOutput,
196 reloc_info: RelocInfo,
189 reloc_parent: link.File.RelocInfo.Parent,
197190) CodeGenError!Result {
198191 const tracy = trace(@src());
199192 defer tracy.end();
......@@ -290,7 +283,7 @@ pub fn generateSymbol(
290283 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (error_union.val) {
291284 .err_name => try pt.intern(.{ .undef = payload_ty.toIntern() }),
292285 .payload => |payload| payload,
293 }), code, debug_output, reloc_info)) {
286 }), code, reloc_parent)) {
294287 .ok => {},
295288 .fail => |em| return .{ .fail = em },
296289 }
......@@ -318,7 +311,7 @@ pub fn generateSymbol(
318311 },
319312 .enum_tag => |enum_tag| {
320313 const int_tag_ty = ty.intTagType(zcu);
321 switch (try generateSymbol(bin_file, pt, src_loc, try pt.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, debug_output, reloc_info)) {
314 switch (try generateSymbol(bin_file, pt, src_loc, try pt.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, reloc_parent)) {
322315 .ok => {},
323316 .fail => |em| return .{ .fail = em },
324317 }
......@@ -334,16 +327,16 @@ pub fn generateSymbol(
334327 },
335328 .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)),
336329 },
337 .ptr => switch (try lowerPtr(bin_file, pt, src_loc, val.toIntern(), code, debug_output, reloc_info, 0)) {
330 .ptr => switch (try lowerPtr(bin_file, pt, src_loc, val.toIntern(), code, reloc_parent, 0)) {
338331 .ok => {},
339332 .fail => |em| return .{ .fail = em },
340333 },
341334 .slice => |slice| {
342 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.ptr), code, debug_output, reloc_info)) {
335 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.ptr), code, reloc_parent)) {
343336 .ok => {},
344337 .fail => |em| return .{ .fail = em },
345338 }
346 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.len), code, debug_output, reloc_info)) {
339 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.len), code, reloc_parent)) {
347340 .ok => {},
348341 .fail => |em| return .{ .fail = em },
349342 }
......@@ -355,7 +348,7 @@ pub fn generateSymbol(
355348
356349 if (ty.optionalReprIsPayload(zcu)) {
357350 if (payload_val) |value| {
358 switch (try generateSymbol(bin_file, pt, src_loc, value, code, debug_output, reloc_info)) {
351 switch (try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent)) {
359352 .ok => {},
360353 .fail => |em| return Result{ .fail = em },
361354 }
......@@ -368,7 +361,7 @@ pub fn generateSymbol(
368361 const value = payload_val orelse Value.fromInterned(try pt.intern(.{
369362 .undef = payload_type.toIntern(),
370363 }));
371 switch (try generateSymbol(bin_file, pt, src_loc, value, code, debug_output, reloc_info)) {
364 switch (try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent)) {
372365 .ok => {},
373366 .fail => |em| return Result{ .fail = em },
374367 }
......@@ -390,7 +383,7 @@ pub fn generateSymbol(
390383 elem
391384 else
392385 array_type.sentinel,
393 }), code, debug_output, reloc_info)) {
386 }), code, reloc_parent)) {
394387 .ok => {},
395388 .fail => |em| return .{ .fail = em },
396389 }
......@@ -449,7 +442,7 @@ pub fn generateSymbol(
449442 math.cast(usize, index) orelse return error.Overflow
450443 ],
451444 .repeated_elem => |elem| elem,
452 }), code, debug_output, reloc_info)) {
445 }), code, reloc_parent)) {
453446 .ok => {},
454447 .fail => |em| return .{ .fail = em },
455448 }
......@@ -482,7 +475,7 @@ pub fn generateSymbol(
482475 .repeated_elem => |elem| elem,
483476 };
484477
485 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
478 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent)) {
486479 .ok => {},
487480 .fail => |em| return Result{ .fail = em },
488481 }
......@@ -524,7 +517,7 @@ pub fn generateSymbol(
524517 return error.Overflow;
525518 var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size);
526519 defer tmp_list.deinit();
527 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), &tmp_list, debug_output, reloc_info)) {
520 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), &tmp_list, reloc_parent)) {
528521 .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items),
529522 .fail => |em| return Result{ .fail = em },
530523 }
......@@ -559,7 +552,7 @@ pub fn generateSymbol(
559552 ) orelse return error.Overflow;
560553 if (padding > 0) try code.appendNTimes(0, padding);
561554
562 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
555 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent)) {
563556 .ok => {},
564557 .fail => |em| return Result{ .fail = em },
565558 }
......@@ -583,12 +576,12 @@ pub fn generateSymbol(
583576 const layout = ty.unionGetLayout(zcu);
584577
585578 if (layout.payload_size == 0) {
586 return generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info);
579 return generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent);
587580 }
588581
589582 // Check if we should store the tag first.
590583 if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) {
591 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
584 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent)) {
592585 .ok => {},
593586 .fail => |em| return Result{ .fail = em },
594587 }
......@@ -601,7 +594,7 @@ pub fn generateSymbol(
601594 if (!field_ty.hasRuntimeBits(zcu)) {
602595 try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);
603596 } else {
604 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
597 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent)) {
605598 .ok => {},
606599 .fail => |em| return Result{ .fail = em },
607600 }
......@@ -612,14 +605,14 @@ pub fn generateSymbol(
612605 }
613606 }
614607 } else {
615 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
608 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent)) {
616609 .ok => {},
617610 .fail => |em| return Result{ .fail = em },
618611 }
619612 }
620613
621614 if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) {
622 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
615 switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent)) {
623616 .ok => {},
624617 .fail => |em| return Result{ .fail = em },
625618 }
......@@ -640,40 +633,29 @@ fn lowerPtr(
640633 src_loc: Zcu.LazySrcLoc,
641634 ptr_val: InternPool.Index,
642635 code: *std.ArrayList(u8),
643 debug_output: DebugInfoOutput,
644 reloc_info: RelocInfo,
636 reloc_parent: link.File.RelocInfo.Parent,
645637 prev_offset: u64,
646638) CodeGenError!Result {
647639 const zcu = pt.zcu;
648640 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;
649641 const offset: u64 = prev_offset + ptr.byte_offset;
650642 return switch (ptr.base_addr) {
651 .nav => |nav| try lowerNavRef(bin_file, pt, src_loc, nav, code, debug_output, reloc_info, offset),
652 .uav => |uav| try lowerUavRef(bin_file, pt, src_loc, uav, code, debug_output, reloc_info, offset),
653 .int => try generateSymbol(bin_file, pt, src_loc, try pt.intValue(Type.usize, offset), code, debug_output, reloc_info),
643 .nav => |nav| try lowerNavRef(bin_file, pt, src_loc, nav, code, reloc_parent, offset),
644 .uav => |uav| try lowerUavRef(bin_file, pt, src_loc, uav, code, reloc_parent, offset),
645 .int => try generateSymbol(bin_file, pt, src_loc, try pt.intValue(Type.usize, offset), code, reloc_parent),
654646 .eu_payload => |eu_ptr| try lowerPtr(
655647 bin_file,
656648 pt,
657649 src_loc,
658650 eu_ptr,
659651 code,
660 debug_output,
661 reloc_info,
652 reloc_parent,
662653 offset + errUnionPayloadOffset(
663654 Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu).errorUnionPayload(zcu),
664655 zcu,
665656 ),
666657 ),
667 .opt_payload => |opt_ptr| try lowerPtr(
668 bin_file,
669 pt,
670 src_loc,
671 opt_ptr,
672 code,
673 debug_output,
674 reloc_info,
675 offset,
676 ),
658 .opt_payload => |opt_ptr| try lowerPtr(bin_file, pt, src_loc, opt_ptr, code, reloc_parent, offset),
677659 .field => |field| {
678660 const base_ptr = Value.fromInterned(field.base);
679661 const base_ty = base_ptr.typeOf(zcu).childType(zcu);
......@@ -692,27 +674,21 @@ fn lowerPtr(
692674 },
693675 else => unreachable,
694676 };
695 return lowerPtr(bin_file, pt, src_loc, field.base, code, debug_output, reloc_info, offset + field_off);
677 return lowerPtr(bin_file, pt, src_loc, field.base, code, reloc_parent, offset + field_off);
696678 },
697679 .arr_elem, .comptime_field, .comptime_alloc => unreachable,
698680 };
699681}
700682
701const RelocInfo = struct {
702 parent_atom_index: u32,
703};
704
705683fn lowerUavRef(
706684 lf: *link.File,
707685 pt: Zcu.PerThread,
708686 src_loc: Zcu.LazySrcLoc,
709687 uav: InternPool.Key.Ptr.BaseAddr.Uav,
710688 code: *std.ArrayList(u8),
711 debug_output: DebugInfoOutput,
712 reloc_info: RelocInfo,
689 reloc_parent: link.File.RelocInfo.Parent,
713690 offset: u64,
714691) CodeGenError!Result {
715 _ = debug_output;
716692 const zcu = pt.zcu;
717693 const ip = &zcu.intern_pool;
718694 const target = lf.comp.root_mod.resolved_target.result;
......@@ -735,7 +711,7 @@ fn lowerUavRef(
735711 }
736712
737713 const vaddr = try lf.getUavVAddr(uav_val, .{
738 .parent_atom_index = reloc_info.parent_atom_index,
714 .parent = reloc_parent,
739715 .offset = code.items.len,
740716 .addend = @intCast(offset),
741717 });
......@@ -756,12 +732,10 @@ fn lowerNavRef(
756732 src_loc: Zcu.LazySrcLoc,
757733 nav_index: InternPool.Nav.Index,
758734 code: *std.ArrayList(u8),
759 debug_output: DebugInfoOutput,
760 reloc_info: RelocInfo,
735 reloc_parent: link.File.RelocInfo.Parent,
761736 offset: u64,
762737) CodeGenError!Result {
763738 _ = src_loc;
764 _ = debug_output;
765739 const zcu = pt.zcu;
766740 const ip = &zcu.intern_pool;
767741 const target = zcu.navFileScope(nav_index).mod.resolved_target.result;
......@@ -775,7 +749,7 @@ fn lowerNavRef(
775749 }
776750
777751 const vaddr = try lf.getNavVAddr(pt, nav_index, .{
778 .parent_atom_index = reloc_info.parent_atom_index,
752 .parent = reloc_parent,
779753 .offset = code.items.len,
780754 .addend = @intCast(offset),
781755 });
src/link.zig+11-1
......@@ -330,6 +330,11 @@ pub const File = struct {
330330 }
331331 }
332332
333 pub const DebugInfoOutput = union(enum) {
334 dwarf: *Dwarf.WipNav,
335 plan9: *Plan9.DebugInfoOutput,
336 none,
337 };
333338 pub const UpdateDebugInfoError = Dwarf.UpdateError;
334339 pub const FlushDebugInfoError = Dwarf.FlushError;
335340
......@@ -673,9 +678,14 @@ pub const File = struct {
673678 }
674679
675680 pub const RelocInfo = struct {
676 parent_atom_index: u32,
681 parent: Parent,
677682 offset: u64,
678683 addend: u32,
684
685 pub const Parent = union(enum) {
686 atom_index: u32,
687 debug_output: DebugInfoOutput,
688 };
679689 };
680690
681691 /// Get allocated `Nav`'s address in virtual memory.
src/link/Coff.zig+12-7
......@@ -1163,8 +1163,8 @@ fn lowerConst(
11631163 try self.setSymbolName(sym, name);
11641164 sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_id + 1));
11651165
1166 const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .none, .{
1167 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
1166 const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{
1167 .atom_index = self.getAtom(atom_index).getSymbolIndex().?,
11681168 });
11691169 const code = switch (res) {
11701170 .ok => code_buffer.items,
......@@ -1235,8 +1235,7 @@ pub fn updateNav(
12351235 zcu.navSrcLoc(nav_index),
12361236 nav_init,
12371237 &code_buffer,
1238 .none,
1239 .{ .parent_atom_index = atom.getSymbolIndex().? },
1238 .{ .atom_index = atom.getSymbolIndex().? },
12401239 );
12411240 const code = switch (res) {
12421241 .ok => code_buffer.items,
......@@ -1284,7 +1283,7 @@ fn updateLazySymbolAtom(
12841283 &required_alignment,
12851284 &code_buffer,
12861285 .none,
1287 .{ .parent_atom_index = local_sym_index },
1286 .{ .atom_index = local_sym_index },
12881287 );
12891288 const code = switch (res) {
12901289 .ok => code_buffer.items,
......@@ -1823,7 +1822,10 @@ pub fn getNavVAddr(
18231822 .@"extern" => |@"extern"| try self.getGlobalSymbol(nav.name.toSlice(ip), @"extern".lib_name.toSlice(ip)),
18241823 else => self.getAtom(try self.getOrCreateAtomForNav(nav_index)).getSymbolIndex().?,
18251824 };
1826 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
1825 const atom_index = self.getAtomIndexForSymbol(.{
1826 .sym_index = reloc_info.parent.atom_index,
1827 .file = null,
1828 }).?;
18271829 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
18281830 try Atom.addRelocation(self, atom_index, .{
18291831 .type = .direct,
......@@ -1901,7 +1903,10 @@ pub fn getUavVAddr(
19011903
19021904 const this_atom_index = self.uavs.get(uav).?.atom;
19031905 const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
1904 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
1906 const atom_index = self.getAtomIndexForSymbol(.{
1907 .sym_index = reloc_info.parent.atom_index,
1908 .file = null,
1909 }).?;
19051910 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
19061911 try Atom.addRelocation(self, atom_index, .{
19071912 .type = .direct,
src/link/Dwarf.zig+129-69
......@@ -19,8 +19,8 @@ debug_rnglists: DebugRngLists,
1919debug_str: StringSection,
2020
2121pub const UpdateError = error{
22 CodegenFail,
2223 ReinterpretDeclRef,
23 IllDefinedMemoryLayout,
2424 Unimplemented,
2525 OutOfMemory,
2626 EndOfStream,
......@@ -1584,6 +1584,18 @@ pub const WipNav = struct {
15841584 wip_nav.func = func;
15851585 }
15861586
1587 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1588 try sec.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(wip_nav.dwarf.gpa, reloc);
1589 }
1590
1591 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1592 try wip_nav.externalReloc(&wip_nav.dwarf.debug_info.section, reloc);
1593 }
1594
1595 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1596 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);
1597 }
1598
15871599 fn abbrevCode(wip_nav: *WipNav, abbrev_code: AbbrevCode) UpdateError!void {
15881600 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), try wip_nav.dwarf.refAbbrevCode(abbrev_code));
15891601 }
......@@ -1660,12 +1672,11 @@ pub const WipNav = struct {
16601672 }
16611673
16621674 fn infoAddrSym(wip_nav: *WipNav, sym_index: u32) UpdateError!void {
1663 const dwarf = wip_nav.dwarf;
1664 try dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{
1675 try wip_nav.infoExternalReloc(.{
16651676 .source_off = @intCast(wip_nav.debug_info.items.len),
16661677 .target_sym = sym_index,
16671678 });
1668 try wip_nav.debug_info.appendNTimes(dwarf.gpa, 0, @intFromEnum(dwarf.address_size));
1679 try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
16691680 }
16701681
16711682 fn frameExprloc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
......@@ -1692,12 +1703,11 @@ pub const WipNav = struct {
16921703 }
16931704
16941705 fn frameAddrSym(wip_nav: *WipNav, sym_index: u32) UpdateError!void {
1695 const dwarf = wip_nav.dwarf;
1696 try dwarf.debug_frame.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{
1706 try wip_nav.frameExternalReloc(.{
16971707 .source_off = @intCast(wip_nav.debug_frame.items.len),
16981708 .target_sym = sym_index,
16991709 });
1700 try wip_nav.debug_frame.appendNTimes(dwarf.gpa, 0, @intFromEnum(dwarf.address_size));
1710 try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
17011711 }
17021712
17031713 fn getTypeEntry(wip_nav: *WipNav, ty: Type) UpdateError!struct { Unit.Index, Entry.Index } {
......@@ -1749,6 +1759,27 @@ pub const WipNav = struct {
17491759 reloc.target_off = @intCast(wip_nav.debug_info.items.len);
17501760 }
17511761
1762 fn blockValue(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc, val: Value) UpdateError!void {
1763 const ty = val.typeOf(wip_nav.pt.zcu);
1764 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
1765 const bytes = ty.abiSize(wip_nav.pt.zcu);
1766 try uleb128(diw, bytes);
1767 if (bytes == 0) return;
1768 var dim = wip_nav.debug_info.toManaged(wip_nav.dwarf.gpa);
1769 defer wip_nav.debug_info = dim.moveToUnmanaged();
1770 switch (try codegen.generateSymbol(
1771 wip_nav.dwarf.bin_file,
1772 wip_nav.pt,
1773 src_loc,
1774 val,
1775 &dim,
1776 .{ .debug_output = .{ .dwarf = wip_nav } },
1777 )) {
1778 .ok => assert(dim.items.len == wip_nav.debug_info.items.len + bytes),
1779 .fail => unreachable,
1780 }
1781 }
1782
17521783 fn enumConstValue(
17531784 wip_nav: *WipNav,
17541785 loaded_enum: InternPool.LoadedEnumType,
......@@ -1814,8 +1845,8 @@ pub const WipNav = struct {
18141845 }
18151846 }
18161847
1817 fn flush(wip_nav: *WipNav) UpdateError!void {
1818 while (wip_nav.pending_types.popOrNull()) |ty| try wip_nav.dwarf.updateType(wip_nav.pt, ty, &wip_nav.pending_types);
1848 fn flush(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc) UpdateError!void {
1849 while (wip_nav.pending_types.popOrNull()) |ty| try wip_nav.dwarf.updateType(wip_nav.pt, src_loc, ty, &wip_nav.pending_types);
18191850 }
18201851};
18211852
......@@ -2171,15 +2202,15 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
21712202 try diw.writeByte(accessibility);
21722203 try wip_nav.strp(nav.name.toSlice(ip));
21732204 try wip_nav.strp(nav.fqn.toSlice(ip));
2174 const ty = nav_val.typeOf(zcu);
2175 const ty_reloc_index = try wip_nav.refForward();
2205 const nav_ty = nav_val.typeOf(zcu);
2206 const nav_ty_reloc_index = try wip_nav.refForward();
21762207 try wip_nav.exprloc(.{ .addr = .{ .sym = sym_index } });
21772208 try uleb128(diw, nav.status.resolved.alignment.toByteUnits() orelse
2178 ty.abiAlignment(zcu).toByteUnits().?);
2209 nav_ty.abiAlignment(zcu).toByteUnits().?);
21792210 try diw.writeByte(@intFromBool(false));
2180 wip_nav.finishForward(ty_reloc_index);
2211 wip_nav.finishForward(nav_ty_reloc_index);
21812212 try wip_nav.abbrevCode(.is_const);
2182 try wip_nav.refType(ty);
2213 try wip_nav.refType(nav_ty);
21832214 },
21842215 .variable => |variable| {
21852216 assert(file.zir_loaded);
......@@ -2288,20 +2319,16 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
22882319 .source_off = @intCast(wip_nav.debug_frame.items.len),
22892320 });
22902321 try dfw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2291 try entry.external_relocs.append(dwarf.gpa, .{
2292 .source_off = @intCast(wip_nav.debug_frame.items.len),
2293 .target_sym = sym_index,
2294 });
2295 try dfw.writeByteNTimes(0, @intFromEnum(dwarf.address_size));
2322 try wip_nav.frameAddrSym(sym_index);
22962323 try dfw.writeByteNTimes(undefined, @intFromEnum(dwarf.address_size));
22972324 },
22982325 .eh_frame => {
22992326 try dfw.writeInt(u32, undefined, dwarf.endian);
2300 try entry.external_relocs.append(dwarf.gpa, .{
2327 try wip_nav.frameExternalReloc(.{
23012328 .source_off = @intCast(wip_nav.debug_frame.items.len),
23022329 .target_sym = sym_index,
23032330 });
2304 try dfw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2331 try dfw.writeInt(u32, 0, dwarf.endian);
23052332 try dfw.writeInt(u32, undefined, dwarf.endian);
23062333 try uleb128(dfw, 0);
23072334 },
......@@ -2481,12 +2508,13 @@ pub fn finishWipNav(
24812508 }
24822509 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
24832510
2484 try wip_nav.flush();
2511 try wip_nav.flush(zcu.navSrcLoc(nav_index));
24852512}
24862513
24872514pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) UpdateError!void {
24882515 const zcu = pt.zcu;
24892516 const ip = &zcu.intern_pool;
2517 const nav_src_loc = zcu.navSrcLoc(nav_index);
24902518 const nav_val = zcu.navValue(nav_index);
24912519
24922520 const nav = ip.getNav(nav_index);
......@@ -2548,7 +2576,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
25482576 const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index);
25492577 errdefer _ = dwarf.navs.pop();
25502578
2551 const tag: enum { done, decl_alias } = switch (ip.indexToKey(nav_val.toIntern())) {
2579 const tag: enum { done, decl_alias, decl_const } = switch (ip.indexToKey(nav_val.toIntern())) {
25522580 .int_type,
25532581 .ptr_type,
25542582 .array_type,
......@@ -2623,7 +2651,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26232651 }
26242652 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
26252653 try wip_nav.refType(field_type);
2626 if (!is_comptime) {
2654 if (is_comptime) try wip_nav.blockValue(
2655 nav_src_loc,
2656 Value.fromInterned(loaded_struct.fieldInit(ip, field_index)),
2657 ) else {
26272658 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
26282659 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
26292660 field_type.abiAlignment(zcu).toByteUnits().?);
......@@ -2850,7 +2881,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
28502881 },
28512882 .undef,
28522883 .simple_value,
2853 .variable,
28542884 .@"extern",
28552885 .int,
28562886 .err,
......@@ -2864,10 +2894,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
28642894 .opt,
28652895 .aggregate,
28662896 .un,
2867 => {
2868 _ = dwarf.navs.pop();
2869 return;
2870 },
2897 => .decl_const,
2898 .variable => unreachable,
28712899 .func => |func| tag: {
28722900 if (nav_gop.found_existing) {
28732901 const unit_ptr = dwarf.debug_info.section.getUnit(wip_nav.unit);
......@@ -2918,14 +2946,16 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29182946 // memoization, not types
29192947 .memoized_call => unreachable,
29202948 };
2949 if (tag != .done) {
2950 if (nav_gop.found_existing)
2951 dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2952 else
2953 nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
2954 wip_nav.entry = nav_gop.value_ptr.*;
2955 }
29212956 switch (tag) {
29222957 .done => {},
29232958 .decl_alias => {
2924 if (nav_gop.found_existing)
2925 dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2926 else
2927 nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
2928 wip_nav.entry = nav_gop.value_ptr.*;
29292959 const diw = wip_nav.debug_info.writer(dwarf.gpa);
29302960 try wip_nav.abbrevCode(.decl_alias);
29312961 try wip_nav.refType(Type.fromInterned(parent_type));
......@@ -2936,14 +2966,35 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29362966 try wip_nav.strp(nav.name.toSlice(ip));
29372967 try wip_nav.refType(nav_val.toType());
29382968 },
2969 .decl_const => {
2970 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2971 try wip_nav.abbrevCode(.decl_const);
2972 try wip_nav.refType(Type.fromInterned(parent_type));
2973 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2974 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2975 try uleb128(diw, loc.column + 1);
2976 try diw.writeByte(accessibility);
2977 try wip_nav.strp(nav.name.toSlice(ip));
2978 try wip_nav.strp(nav.fqn.toSlice(ip));
2979 const nav_ty = nav_val.typeOf(zcu);
2980 const nav_ty_reloc_index = try wip_nav.refForward();
2981 try wip_nav.blockValue(nav_src_loc, nav_val);
2982 try uleb128(diw, nav.status.resolved.alignment.toByteUnits() orelse
2983 nav_ty.abiAlignment(zcu).toByteUnits().?);
2984 try diw.writeByte(@intFromBool(false));
2985 wip_nav.finishForward(nav_ty_reloc_index);
2986 try wip_nav.abbrevCode(.is_const);
2987 try wip_nav.refType(nav_ty);
2988 },
29392989 }
29402990 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
2941 try wip_nav.flush();
2991 try wip_nav.flush(nav_src_loc);
29422992}
29432993
29442994fn updateType(
29452995 dwarf: *Dwarf,
29462996 pt: Zcu.PerThread,
2997 src_loc: Zcu.LazySrcLoc,
29472998 type_index: InternPool.Index,
29482999 pending_types: *std.ArrayListUnmanaged(InternPool.Index),
29493000) UpdateError!void {
......@@ -3000,15 +3051,10 @@ fn updateType(
30003051 const ptr_child_type = Type.fromInterned(ptr_type.child);
30013052 try wip_nav.abbrevCode(if (ptr_type.sentinel == .none) .ptr_type else .ptr_sentinel_type);
30023053 try wip_nav.strp(name);
3003 if (ptr_type.sentinel != .none) {
3004 const bytes = ptr_child_type.abiSize(zcu);
3005 try uleb128(diw, bytes);
3006 const mem = try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, @intCast(bytes));
3007 Value.fromInterned(ptr_type.sentinel).writeToMemory(pt, mem) catch |err| switch (err) {
3008 error.IllDefinedMemoryLayout => @memset(mem, 0),
3009 else => |e| return e,
3010 };
3011 }
3054 if (ptr_type.sentinel != .none) try wip_nav.blockValue(
3055 src_loc,
3056 Value.fromInterned(ptr_type.sentinel),
3057 );
30123058 try uleb128(diw, ptr_type.flags.alignment.toByteUnits() orelse
30133059 ptr_child_type.abiAlignment(zcu).toByteUnits().?);
30143060 try diw.writeByte(@intFromEnum(ptr_type.flags.address_space));
......@@ -3054,15 +3100,10 @@ fn updateType(
30543100 const array_child_type = Type.fromInterned(array_type.child);
30553101 try wip_nav.abbrevCode(if (array_type.sentinel == .none) .array_type else .array_sentinel_type);
30563102 try wip_nav.strp(name);
3057 if (array_type.sentinel != .none) {
3058 const bytes = array_child_type.abiSize(zcu);
3059 try uleb128(diw, bytes);
3060 const mem = try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, @intCast(bytes));
3061 Value.fromInterned(array_type.sentinel).writeToMemory(pt, mem) catch |err| switch (err) {
3062 error.IllDefinedMemoryLayout => @memset(mem, 0),
3063 else => |e| return e,
3064 };
3065 }
3103 if (array_type.sentinel != .none) try wip_nav.blockValue(
3104 src_loc,
3105 Value.fromInterned(array_type.sentinel),
3106 );
30663107 try wip_nav.refType(array_child_type);
30673108 try wip_nav.abbrevCode(.array_index);
30683109 try wip_nav.refType(Type.usize);
......@@ -3292,7 +3333,10 @@ fn updateType(
32923333 }
32933334 const field_type = Type.fromInterned(anon_struct_type.types.get(ip)[field_index]);
32943335 try wip_nav.refType(field_type);
3295 if (comptime_value == .none) {
3336 if (comptime_value != .none) try wip_nav.blockValue(
3337 src_loc,
3338 Value.fromInterned(comptime_value),
3339 ) else {
32963340 const field_align = field_type.abiAlignment(zcu);
32973341 field_byte_offset = field_align.forward(field_byte_offset);
32983342 try uleb128(diw, field_byte_offset);
......@@ -3359,16 +3403,13 @@ fn updateType(
33593403 }
33603404 if (error_set_type.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
33613405 },
3362 .inferred_error_set_type => |func| switch (ip.funcIesResolvedUnordered(func)) {
3363 .none => {
3364 try wip_nav.abbrevCode(.void_type);
3365 try wip_nav.strp(name);
3366 },
3367 else => |ies| {
3368 try wip_nav.abbrevCode(.inferred_error_set_type);
3369 try wip_nav.strp(name);
3370 try wip_nav.refType(Type.fromInterned(ies));
3371 },
3406 .inferred_error_set_type => |func| {
3407 try wip_nav.abbrevCode(.inferred_error_set_type);
3408 try wip_nav.strp(name);
3409 try wip_nav.refType(Type.fromInterned(switch (ip.funcIesResolvedUnordered(func)) {
3410 .none => .anyerror_type,
3411 else => |ies| ies,
3412 }));
33723413 },
33733414
33743415 // values, not types
......@@ -3400,6 +3441,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
34003441 const zcu = pt.zcu;
34013442 const ip = &zcu.intern_pool;
34023443 const ty = Type.fromInterned(type_index);
3444 const ty_src_loc = ty.srcLoc(zcu);
34033445 log.debug("updateContainerType({}({d}))", .{ ty.fmt(pt), @intFromEnum(type_index) });
34043446
34053447 const inst_info = ty.typeDeclInst(zcu).?.resolveFull(ip).?;
......@@ -3447,7 +3489,10 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
34473489 }
34483490 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
34493491 try wip_nav.refType(field_type);
3450 if (!is_comptime) {
3492 if (is_comptime) try wip_nav.blockValue(
3493 ty_src_loc,
3494 Value.fromInterned(loaded_struct.fieldInit(ip, field_index)),
3495 ) else {
34513496 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
34523497 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
34533498 field_type.abiAlignment(zcu).toByteUnits().?);
......@@ -3457,7 +3502,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
34573502 }
34583503
34593504 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
3460 try wip_nav.flush();
3505 try wip_nav.flush(ty_src_loc);
34613506 } else {
34623507 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
34633508 assert(decl_inst.tag == .extended);
......@@ -3515,7 +3560,10 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
35153560 }
35163561 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
35173562 try wip_nav.refType(field_type);
3518 if (!is_comptime) {
3563 if (is_comptime) try wip_nav.blockValue(
3564 ty_src_loc,
3565 Value.fromInterned(loaded_struct.fieldInit(ip, field_index)),
3566 ) else {
35193567 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
35203568 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
35213569 field_type.abiAlignment(zcu).toByteUnits().?);
......@@ -3616,7 +3664,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
36163664 }
36173665 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
36183666 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
3619 try wip_nav.flush();
3667 try wip_nav.flush(ty_src_loc);
36203668 }
36213669}
36223670
......@@ -4085,6 +4133,7 @@ const AbbrevCode = enum {
40854133 decl_packed_struct,
40864134 decl_union,
40874135 decl_var,
4136 decl_const,
40884137 decl_func,
40894138 decl_empty_func,
40904139 decl_func_generic,
......@@ -4220,6 +4269,16 @@ const AbbrevCode = enum {
42204269 .{ .external, .flag },
42214270 },
42224271 },
4272 .decl_const = .{
4273 .tag = .constant,
4274 .attrs = decl_abbrev_common_attrs ++ .{
4275 .{ .linkage_name, .strp },
4276 .{ .type, .ref_addr },
4277 .{ .const_value, .block },
4278 .{ .alignment, .udata },
4279 .{ .external, .flag },
4280 },
4281 },
42234282 .decl_func = .{
42244283 .tag = .subprogram,
42254284 .children = true,
......@@ -4339,9 +4398,10 @@ const AbbrevCode = enum {
43394398 .struct_field_comptime = .{
43404399 .tag = .member,
43414400 .attrs = &.{
4401 .{ .const_expr, .flag_present },
43424402 .{ .name, .strp },
43434403 .{ .type, .ref_addr },
4344 .{ .const_expr, .flag_present },
4404 .{ .default_value, .block },
43454405 },
43464406 },
43474407 .packed_struct_field = .{
src/link/Elf/ZigObject.zig+43-20
......@@ -957,13 +957,26 @@ pub fn getNavVAddr(
957957 };
958958 const this_sym = self.symbol(this_sym_index);
959959 const vaddr = this_sym.address(.{}, elf_file);
960 const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(elf_file).?;
961 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
962 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
963 .r_offset = reloc_info.offset,
964 .r_info = (@as(u64, @intCast(this_sym_index)) << 32) | r_type,
965 .r_addend = reloc_info.addend,
966 }, self);
960 switch (reloc_info.parent) {
961 .atom_index => |atom_index| {
962 const parent_atom = self.symbol(atom_index).atom(elf_file).?;
963 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
964 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
965 .r_offset = reloc_info.offset,
966 .r_info = (@as(u64, @intCast(this_sym_index)) << 32) | r_type,
967 .r_addend = reloc_info.addend,
968 }, self);
969 },
970 .debug_output => |debug_output| switch (debug_output) {
971 .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
972 .source_off = @intCast(reloc_info.offset),
973 .target_sym = this_sym_index,
974 .target_off = reloc_info.addend,
975 }),
976 .plan9 => unreachable,
977 .none => unreachable,
978 },
979 }
967980 return @intCast(vaddr);
968981}
969982
......@@ -976,13 +989,26 @@ pub fn getUavVAddr(
976989 const sym_index = self.uavs.get(uav).?.symbol_index;
977990 const sym = self.symbol(sym_index);
978991 const vaddr = sym.address(.{}, elf_file);
979 const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(elf_file).?;
980 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
981 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
982 .r_offset = reloc_info.offset,
983 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
984 .r_addend = reloc_info.addend,
985 }, self);
992 switch (reloc_info.parent) {
993 .atom_index => |atom_index| {
994 const parent_atom = self.symbol(atom_index).atom(elf_file).?;
995 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
996 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
997 .r_offset = reloc_info.offset,
998 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
999 .r_addend = reloc_info.addend,
1000 }, self);
1001 },
1002 .debug_output => |debug_output| switch (debug_output) {
1003 .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
1004 .source_off = @intCast(reloc_info.offset),
1005 .target_sym = sym_index,
1006 .target_off = reloc_info.addend,
1007 }),
1008 .plan9 => unreachable,
1009 .none => unreachable,
1010 },
1011 }
9861012 return @intCast(vaddr);
9871013}
9881014
......@@ -1600,15 +1626,13 @@ pub fn updateNav(
16001626 var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, sym_index) else null;
16011627 defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit();
16021628
1603 // TODO implement .debug_info for global variables
16041629 const res = try codegen.generateSymbol(
16051630 &elf_file.base,
16061631 pt,
16071632 zcu.navSrcLoc(nav_index),
16081633 Value.fromInterned(nav_init),
16091634 &code_buffer,
1610 if (debug_wip_nav) |*wip_nav| .{ .dwarf = wip_nav } else .none,
1611 .{ .parent_atom_index = sym_index },
1635 .{ .atom_index = sym_index },
16121636 );
16131637
16141638 const code = switch (res) {
......@@ -1691,7 +1715,7 @@ fn updateLazySymbol(
16911715 &required_alignment,
16921716 &code_buffer,
16931717 .none,
1694 .{ .parent_atom_index = symbol_index },
1718 .{ .atom_index = symbol_index },
16951719 );
16961720 const code = switch (res) {
16971721 .ok => code_buffer.items,
......@@ -1780,8 +1804,7 @@ fn lowerConst(
17801804 src_loc,
17811805 val,
17821806 &code_buffer,
1783 .{ .none = {} },
1784 .{ .parent_atom_index = sym_index },
1807 .{ .atom_index = sym_index },
17851808 );
17861809 const code = switch (res) {
17871810 .ok => code_buffer.items,
src/link/MachO/ZigObject.zig+62-34
......@@ -633,20 +633,33 @@ pub fn getNavVAddr(
633633 };
634634 const sym = self.symbols.items[sym_index];
635635 const vaddr = sym.getAddress(.{}, macho_file);
636 const parent_atom = self.symbols.items[reloc_info.parent_atom_index].getAtom(macho_file).?;
637 try parent_atom.addReloc(macho_file, .{
638 .tag = .@"extern",
639 .offset = @intCast(reloc_info.offset),
640 .target = sym_index,
641 .addend = reloc_info.addend,
642 .type = .unsigned,
643 .meta = .{
644 .pcrel = false,
645 .has_subtractor = false,
646 .length = 3,
647 .symbolnum = @intCast(sym.nlist_idx),
636 switch (reloc_info.parent) {
637 .atom_index => |atom_index| {
638 const parent_atom = self.symbols.items[atom_index].getAtom(macho_file).?;
639 try parent_atom.addReloc(macho_file, .{
640 .tag = .@"extern",
641 .offset = @intCast(reloc_info.offset),
642 .target = sym_index,
643 .addend = reloc_info.addend,
644 .type = .unsigned,
645 .meta = .{
646 .pcrel = false,
647 .has_subtractor = false,
648 .length = 3,
649 .symbolnum = @intCast(sym.nlist_idx),
650 },
651 });
648652 },
649 });
653 .debug_output => |debug_output| switch (debug_output) {
654 .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
655 .source_off = @intCast(reloc_info.offset),
656 .target_sym = sym_index,
657 .target_off = reloc_info.addend,
658 }),
659 .plan9 => unreachable,
660 .none => unreachable,
661 },
662 }
650663 return vaddr;
651664}
652665
......@@ -659,20 +672,33 @@ pub fn getUavVAddr(
659672 const sym_index = self.uavs.get(uav).?.symbol_index;
660673 const sym = self.symbols.items[sym_index];
661674 const vaddr = sym.getAddress(.{}, macho_file);
662 const parent_atom = self.symbols.items[reloc_info.parent_atom_index].getAtom(macho_file).?;
663 try parent_atom.addReloc(macho_file, .{
664 .tag = .@"extern",
665 .offset = @intCast(reloc_info.offset),
666 .target = sym_index,
667 .addend = reloc_info.addend,
668 .type = .unsigned,
669 .meta = .{
670 .pcrel = false,
671 .has_subtractor = false,
672 .length = 3,
673 .symbolnum = @intCast(sym.nlist_idx),
675 switch (reloc_info.parent) {
676 .atom_index => |atom_index| {
677 const parent_atom = self.symbols.items[atom_index].getAtom(macho_file).?;
678 try parent_atom.addReloc(macho_file, .{
679 .tag = .@"extern",
680 .offset = @intCast(reloc_info.offset),
681 .target = sym_index,
682 .addend = reloc_info.addend,
683 .type = .unsigned,
684 .meta = .{
685 .pcrel = false,
686 .has_subtractor = false,
687 .length = 3,
688 .symbolnum = @intCast(sym.nlist_idx),
689 },
690 });
674691 },
675 });
692 .debug_output => |debug_output| switch (debug_output) {
693 .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
694 .source_off = @intCast(reloc_info.offset),
695 .target_sym = sym_index,
696 .target_off = reloc_info.addend,
697 }),
698 .plan9 => unreachable,
699 .none => unreachable,
700 },
701 }
676702 return vaddr;
677703}
678704
......@@ -903,8 +929,7 @@ pub fn updateNav(
903929 zcu.navSrcLoc(nav_index),
904930 Value.fromInterned(nav_init),
905931 &code_buffer,
906 if (debug_wip_nav) |*wip_nav| .{ .dwarf = wip_nav } else .none,
907 .{ .parent_atom_index = sym_index },
932 .{ .atom_index = sym_index },
908933 );
909934
910935 const code = switch (res) {
......@@ -1212,11 +1237,14 @@ fn lowerConst(
12121237 const name_str = try self.addString(gpa, name);
12131238 const sym_index = try self.newSymbolWithAtom(gpa, name_str, macho_file);
12141239
1215 const res = try codegen.generateSymbol(&macho_file.base, pt, src_loc, val, &code_buffer, .{
1216 .none = {},
1217 }, .{
1218 .parent_atom_index = sym_index,
1219 });
1240 const res = try codegen.generateSymbol(
1241 &macho_file.base,
1242 pt,
1243 src_loc,
1244 val,
1245 &code_buffer,
1246 .{ .atom_index = sym_index },
1247 );
12201248 const code = switch (res) {
12211249 .ok => code_buffer.items,
12221250 .fail => |em| return .{ .fail = em },
......@@ -1378,7 +1406,7 @@ fn updateLazySymbol(
13781406 &required_alignment,
13791407 &code_buffer,
13801408 .none,
1381 .{ .parent_atom_index = symbol_index },
1409 .{ .atom_index = symbol_index },
13821410 );
13831411 const code = switch (res) {
13841412 .ok => code_buffer.items,
src/link/Plan9.zig+17-18
......@@ -422,10 +422,7 @@ pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index,
422422 );
423423 const code = switch (res) {
424424 .ok => try code_buffer.toOwnedSlice(),
425 .fail => |em| {
426 try zcu.failed_codegen.put(gpa, func.owner_nav, em);
427 return;
428 },
425 .fail => |em| return zcu.failed_codegen.put(gpa, func.owner_nav, em),
429426 };
430427 self.getAtomPtr(atom_idx).code = .{
431428 .code_ptr = null,
......@@ -463,15 +460,17 @@ pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde
463460 var code_buffer = std.ArrayList(u8).init(gpa);
464461 defer code_buffer.deinit();
465462 // TODO we need the symbol index for symbol in the table of locals for the containing atom
466 const res = try codegen.generateSymbol(&self.base, pt, zcu.navSrcLoc(nav_index), nav_init, &code_buffer, .none, .{
467 .parent_atom_index = @intCast(atom_idx),
468 });
463 const res = try codegen.generateSymbol(
464 &self.base,
465 pt,
466 zcu.navSrcLoc(nav_index),
467 nav_init,
468 &code_buffer,
469 .{ .atom_index = @intCast(atom_idx) },
470 );
469471 const code = switch (res) {
470472 .ok => code_buffer.items,
471 .fail => |em| {
472 try zcu.failed_codegen.put(gpa, nav_index, em);
473 return;
474 },
473 .fail => |em| return zcu.failed_codegen.put(gpa, nav_index, em),
475474 };
476475 try self.data_nav_table.ensureUnusedCapacity(gpa, 1);
477476 const duped_code = try gpa.dupe(u8, code);
......@@ -1116,7 +1115,7 @@ fn updateLazySymbolAtom(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol, a
11161115 &required_alignment,
11171116 &code_buffer,
11181117 .none,
1119 .{ .parent_atom_index = @as(Atom.Index, @intCast(atom_index)) },
1118 .{ .atom_index = @intCast(atom_index) },
11201119 );
11211120 const code = switch (res) {
11221121 .ok => code_buffer.items,
......@@ -1373,21 +1372,21 @@ pub fn getNavVAddr(
13731372 log.debug("getDeclVAddr for {}", .{nav.name.fmt(ip)});
13741373 if (ip.indexToKey(nav.status.resolved.val) == .@"extern") {
13751374 if (nav.name.eqlSlice("etext", ip)) {
1376 try self.addReloc(reloc_info.parent_atom_index, .{
1375 try self.addReloc(reloc_info.parent.atom_index, .{
13771376 .target = undefined,
13781377 .offset = reloc_info.offset,
13791378 .addend = reloc_info.addend,
13801379 .type = .special_etext,
13811380 });
13821381 } else if (nav.name.eqlSlice("edata", ip)) {
1383 try self.addReloc(reloc_info.parent_atom_index, .{
1382 try self.addReloc(reloc_info.parent.atom_index, .{
13841383 .target = undefined,
13851384 .offset = reloc_info.offset,
13861385 .addend = reloc_info.addend,
13871386 .type = .special_edata,
13881387 });
13891388 } else if (nav.name.eqlSlice("end", ip)) {
1390 try self.addReloc(reloc_info.parent_atom_index, .{
1389 try self.addReloc(reloc_info.parent.atom_index, .{
13911390 .target = undefined,
13921391 .offset = reloc_info.offset,
13931392 .addend = reloc_info.addend,
......@@ -1400,7 +1399,7 @@ pub fn getNavVAddr(
14001399 // otherwise, we just add a relocation
14011400 const atom_index = try self.seeNav(pt, nav_index);
14021401 // the parent_atom_index in this case is just the decl_index of the parent
1403 try self.addReloc(reloc_info.parent_atom_index, .{
1402 try self.addReloc(reloc_info.parent.atom_index, .{
14041403 .target = atom_index,
14051404 .offset = reloc_info.offset,
14061405 .addend = reloc_info.addend,
......@@ -1435,7 +1434,7 @@ pub fn lowerUav(
14351434 gop.value_ptr.* = index;
14361435 // we need to free name latex
14371436 var code_buffer = std.ArrayList(u8).init(gpa);
1438 const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .none = {} }, .{ .parent_atom_index = index });
1437 const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .atom_index = index });
14391438 const code = switch (res) {
14401439 .ok => code_buffer.items,
14411440 .fail => |em| return .{ .fail = em },
......@@ -1459,7 +1458,7 @@ pub fn lowerUav(
14591458
14601459pub fn getUavVAddr(self: *Plan9, uav: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 {
14611460 const atom_index = self.uavs.get(uav).?;
1462 try self.addReloc(reloc_info.parent_atom_index, .{
1461 try self.addReloc(reloc_info.parent.atom_index, .{
14631462 .target = atom_index,
14641463 .offset = reloc_info.offset,
14651464 .addend = reloc_info.addend,
src/link/Wasm/ZigObject.zig+11-9
......@@ -277,8 +277,7 @@ pub fn updateNav(
277277 zcu.navSrcLoc(nav_index),
278278 nav_init,
279279 &code_writer,
280 .none,
281 .{ .parent_atom_index = @intFromEnum(atom.sym_index) },
280 .{ .atom_index = @intFromEnum(atom.sym_index) },
282281 );
283282
284283 const code = switch (res) {
......@@ -520,10 +519,7 @@ fn lowerConst(
520519 src_loc,
521520 val,
522521 &value_bytes,
523 .none,
524 .{
525 .parent_atom_index = @intFromEnum(atom.sym_index),
526 },
522 .{ .atom_index = @intFromEnum(atom.sym_index) },
527523 );
528524 break :code switch (result) {
529525 .ok => value_bytes.items,
......@@ -762,8 +758,11 @@ pub fn getNavVAddr(
762758 else => {},
763759 }
764760
765 std.debug.assert(reloc_info.parent_atom_index != 0);
766 const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = @enumFromInt(reloc_info.parent_atom_index) }).?;
761 std.debug.assert(reloc_info.parent.atom_index != 0);
762 const atom_index = wasm_file.symbol_atom.get(.{
763 .file = zig_object.index,
764 .index = @enumFromInt(reloc_info.parent.atom_index),
765 }).?;
767766 const atom = wasm_file.getAtomPtr(atom_index);
768767 const is_wasm32 = target.cpu.arch == .wasm32;
769768 if (ip.isFunctionType(ip.getNav(nav_index).typeOf(ip))) {
......@@ -800,7 +799,10 @@ pub fn getUavVAddr(
800799 const atom_index = zig_object.uavs.get(uav).?;
801800 const target_symbol_index = @intFromEnum(wasm_file.getAtom(atom_index).sym_index);
802801
803 const parent_atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = @enumFromInt(reloc_info.parent_atom_index) }).?;
802 const parent_atom_index = wasm_file.symbol_atom.get(.{
803 .file = zig_object.index,
804 .index = @enumFromInt(reloc_info.parent.atom_index),
805 }).?;
804806 const parent_atom = wasm_file.getAtomPtr(parent_atom_index);
805807 const is_wasm32 = target.cpu.arch == .wasm32;
806808 const zcu = wasm_file.base.comp.zcu.?;