authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-18 12:31:22+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-19 11:20:38+02:00
loga8520fbd0f73c57d133c28c44d97e1173c6fe381
tree6b5480c45c1bf1317fe7a3d82a1da046835cac38
parentad5770eba40e0cc425c7a1eab4d37c6f9788d670

stage2: add dbg_block_{begin,end} instruction


14 files changed, 119 insertions(+), 1 deletions(-)

src/Air.zig+6
......@@ -326,6 +326,10 @@ pub const Inst = struct {
326326 /// Result type is always void.
327327 /// Uses the `dbg_stmt` field.
328328 dbg_stmt,
329 /// Marks the beginning of a semantic scope for debug info variables.
330 dbg_block_begin,
331 /// Marks the end of a semantic scope for debug info variables.
332 dbg_block_end,
329333 /// Marks the start of an inline call.
330334 /// Uses `ty_pl` with the payload being the index of a Value.Function in air.values.
331335 dbg_inline_begin,
......@@ -990,6 +994,8 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
990994 .dbg_stmt,
991995 .dbg_inline_begin,
992996 .dbg_inline_end,
997 .dbg_block_begin,
998 .dbg_block_end,
993999 .dbg_var_ptr,
9941000 .dbg_var_val,
9951001 .store,
src/AstGen.zig+12
......@@ -2052,6 +2052,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod
20522052 const tree = astgen.tree;
20532053 const node_tags = tree.nodes.items(.tag);
20542054
2055 _ = try gz.add(.{ .tag = .extended, .data = .{ .extended = .{
2056 .opcode = .dbg_block_begin,
2057 .small = undefined,
2058 .operand = undefined
2059 } } });
2060
20552061 var block_arena = std.heap.ArenaAllocator.init(gz.astgen.gpa);
20562062 defer block_arena.deinit();
20572063 const block_arena_allocator = block_arena.allocator();
......@@ -2105,6 +2111,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod
21052111 }
21062112 }
21072113
2114 _ = try gz.add(.{ .tag = .extended, .data = .{ .extended = .{
2115 .opcode = .dbg_block_end,
2116 .small = undefined,
2117 .operand = undefined
2118 } } });
2119
21082120 try genDefers(gz, parent_scope, scope, .normal_only);
21092121 try checkUsed(gz, parent_scope, scope);
21102122}
src/Liveness.zig+2
......@@ -316,6 +316,8 @@ fn analyzeInst(
316316 .dbg_stmt,
317317 .dbg_inline_begin,
318318 .dbg_inline_end,
319 .dbg_block_begin,
320 .dbg_block_end,
319321 .unreach,
320322 .fence,
321323 .ret_addr,
src/Sema.zig+25-1
......@@ -1216,6 +1216,8 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
12161216 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
12171217 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
12181218 .prefetch => return sema.zirPrefetch( block, extended),
1219 .dbg_block_begin => return sema.zirDbgBlockBegin( block),
1220 .dbg_block_end => return sema.zirDbgBlockEnd( block),
12191221 // zig fmt: on
12201222 }
12211223}
......@@ -4215,6 +4217,26 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
42154217 });
42164218}
42174219
4220fn zirDbgBlockBegin(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
4221 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return .void_value;
4222
4223 _ = try block.addInst(.{
4224 .tag = .dbg_block_begin,
4225 .data = undefined,
4226 });
4227 return .void_value;
4228}
4229
4230fn zirDbgBlockEnd(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
4231 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return .void_value;
4232
4233 _ = try block.addInst(.{
4234 .tag = .dbg_block_end,
4235 .data = undefined,
4236 });
4237 return .void_value;
4238}
4239
42184240fn zirDbgVar(
42194241 sema: *Sema,
42204242 block: *Block,
......@@ -5224,7 +5246,9 @@ fn emitDbgInline(
52245246 new_func_ty: Type,
52255247 tag: Air.Inst.Tag,
52265248) CompileError!void {
5227 // No change of file; no dbg_inline needed.
5249 if (sema.mod.comp.bin_file.options.strip) return;
5250
5251 // Recursive inline call; no dbg_inline needed.
52285252 if (old_func == new_func) return;
52295253
52305254 try sema.air_values.append(sema.gpa, try Value.Tag.function.create(sema.arena, new_func));
src/Zir.zig+4
......@@ -1646,6 +1646,10 @@ pub const Inst = struct {
16461646 /// The `@prefetch` builtin.
16471647 /// `operand` is payload index to `BinNode`.
16481648 prefetch,
1649 /// Marks the beginning of a semantic scope for debug info variables.
1650 dbg_block_begin,
1651 /// Marks the end of a semantic scope for debug info variables.
1652 dbg_block_end,
16491653
16501654 pub const InstData = struct {
16511655 opcode: Extended,
src/arch/aarch64/CodeGen.zig+9
......@@ -654,6 +654,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
654654 .dbg_inline_end,
655655 => try self.airDbgInline(inst),
656656
657 .dbg_block_begin,
658 .dbg_block_end,
659 => try self.airDbgBlock(inst),
660
657661 .call => try self.airCall(inst, .auto),
658662 .call_always_tail => try self.airCall(inst, .always_tail),
659663 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -2731,6 +2735,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
27312735 return self.finishAir(inst, .dead, .{ .none, .none, .none });
27322736}
27332737
2738fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
2739 // TODO emit debug info lexical block
2740 return self.finishAir(inst, .dead, .{ .none, .none, .none });
2741}
2742
27342743fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
27352744 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
27362745 const name = self.air.nullTerminatedString(pl_op.payload);
src/arch/arm/CodeGen.zig+9
......@@ -644,6 +644,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
644644 .dbg_inline_end,
645645 => try self.airDbgInline(inst),
646646
647 .dbg_block_begin,
648 .dbg_block_end,
649 => try self.airDbgBlock(inst),
650
647651 .call => try self.airCall(inst, .auto),
648652 .call_always_tail => try self.airCall(inst, .always_tail),
649653 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -2948,6 +2952,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
29482952 return self.finishAir(inst, .dead, .{ .none, .none, .none });
29492953}
29502954
2955fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
2956 // TODO emit debug info lexical block
2957 return self.finishAir(inst, .dead, .{ .none, .none, .none });
2958}
2959
29512960fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
29522961 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
29532962 const name = self.air.nullTerminatedString(pl_op.payload);
src/arch/riscv64/CodeGen.zig+9
......@@ -618,6 +618,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
618618 .dbg_inline_end,
619619 => try self.airDbgInline(inst),
620620
621 .dbg_block_begin,
622 .dbg_block_end,
623 => try self.airDbgBlock(inst),
624
621625 .call => try self.airCall(inst, .auto),
622626 .call_always_tail => try self.airCall(inst, .always_tail),
623627 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -1653,6 +1657,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
16531657 return self.finishAir(inst, .dead, .{ .none, .none, .none });
16541658}
16551659
1660fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
1661 // TODO emit debug info lexical block
1662 return self.finishAir(inst, .dead, .{ .none, .none, .none });
1663}
1664
16561665fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
16571666 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
16581667 const name = self.air.nullTerminatedString(pl_op.payload);
src/arch/wasm/CodeGen.zig+2
......@@ -1330,6 +1330,8 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
13301330 .dbg_stmt,
13311331 .dbg_inline_begin,
13321332 .dbg_inline_end,
1333 .dbg_block_begin,
1334 .dbg_block_end,
13331335 .dbg_var_ptr,
13341336 .dbg_var_val,
13351337 => WValue.none,
src/arch/x86_64/CodeGen.zig+9
......@@ -735,6 +735,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
735735 .dbg_inline_end,
736736 => try self.airDbgInline(inst),
737737
738 .dbg_block_begin,
739 .dbg_block_end,
740 => try self.airDbgBlock(inst),
741
738742 .call => try self.airCall(inst, .auto),
739743 .call_always_tail => try self.airCall(inst, .always_tail),
740744 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -3683,6 +3687,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
36833687 return self.finishAir(inst, .dead, .{ .none, .none, .none });
36843688}
36853689
3690fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
3691 // TODO emit debug info lexical block
3692 return self.finishAir(inst, .dead, .{ .none, .none, .none });
3693}
3694
36863695fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
36873696 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
36883697 const name = self.air.nullTerminatedString(pl_op.payload);
src/codegen/c.zig+4
......@@ -1786,6 +1786,10 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
17861786 .dbg_inline_end,
17871787 => try airDbgInline(f, inst),
17881788
1789 .dbg_block_begin,
1790 .dbg_block_end,
1791 => CValue{ .none = {} },
1792
17891793 .call => try airCall(f, inst, .auto),
17901794 .call_always_tail => try airCall(f, inst, .always_tail),
17911795 .call_never_tail => try airCall(f, inst, .never_tail),
src/codegen/llvm.zig+22
......@@ -3220,6 +3220,10 @@ pub const FuncGen = struct {
32203220 /// Stack of locations where a call was inlined.
32213221 dbg_inlined: std.ArrayListUnmanaged(DbgState) = .{},
32223222
3223 /// Stack of `DILexicalBlock`s. dbg_block instructions cannot happend accross
3224 /// dbg_inline instructions so no special handling there is required.
3225 dbg_block_stack: std.ArrayListUnmanaged(*llvm.DIScope) = .{},
3226
32233227 /// This stores the LLVM values used in a function, such that they can be referred to
32243228 /// in other instructions. This table is cleared before every function is generated.
32253229 func_inst_table: std.AutoHashMapUnmanaged(Air.Inst.Ref, *const llvm.Value),
......@@ -3254,6 +3258,7 @@ pub const FuncGen = struct {
32543258 fn deinit(self: *FuncGen) void {
32553259 self.builder.dispose();
32563260 self.dbg_inlined.deinit(self.gpa);
3261 self.dbg_block_stack.deinit(self.gpa);
32573262 self.func_inst_table.deinit(self.gpa);
32583263 self.blocks.deinit(self.gpa);
32593264 }
......@@ -3475,6 +3480,8 @@ pub const FuncGen = struct {
34753480 .dbg_stmt => self.airDbgStmt(inst),
34763481 .dbg_inline_begin => try self.airDbgInlineBegin(inst),
34773482 .dbg_inline_end => try self.airDbgInlineEnd(inst),
3483 .dbg_block_begin => try self.airDbgBlockBegin(),
3484 .dbg_block_end => try self.airDbgBlockEnd(),
34783485 .dbg_var_ptr => try self.airDbgVarPtr(inst),
34793486 .dbg_var_val => try self.airDbgVarVal(inst),
34803487 // zig fmt: on
......@@ -4256,6 +4263,21 @@ pub const FuncGen = struct {
42564263 return null;
42574264 }
42584265
4266 fn airDbgBlockBegin(self: *FuncGen) !?*const llvm.Value {
4267 const dib = self.dg.object.di_builder orelse return null;
4268 const old_scope = self.di_scope.?;
4269 try self.dbg_block_stack.append(self.gpa, old_scope);
4270 const lexical_block = dib.createLexicalBlock(old_scope, self.di_file.?, self.prev_dbg_line, self.prev_dbg_column);
4271 self.di_scope = lexical_block.toScope();
4272 return null;
4273 }
4274
4275 fn airDbgBlockEnd(self: *FuncGen) !?*const llvm.Value {
4276 if (self.dg.object.di_builder == null) return null;
4277 self.di_scope = self.dbg_block_stack.pop();
4278 return null;
4279 }
4280
42594281 fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
42604282 const dib = self.dg.object.di_builder orelse return null;
42614283 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
src/print_air.zig+2
......@@ -272,6 +272,8 @@ const Writer = struct {
272272 .mul_with_overflow,
273273 .shl_with_overflow,
274274 => try w.writeOverflow(s, inst),
275
276 .dbg_block_begin, .dbg_block_end => {},
275277 }
276278 }
277279
src/print_zir.zig+4
......@@ -463,6 +463,10 @@ const Writer = struct {
463463 .builtin_src,
464464 => try self.writeExtNode(stream, extended),
465465
466 .dbg_block_begin,
467 .dbg_block_end,
468 => try stream.writeAll("))"),
469
466470 .@"asm" => try self.writeAsm(stream, extended),
467471 .func => try self.writeFuncExtended(stream, extended),
468472 .variable => try self.writeVarExtended(stream, extended),