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 {...@@ -326,6 +326,10 @@ pub const Inst = struct {
326 /// Result type is always void.326 /// Result type is always void.
327 /// Uses the `dbg_stmt` field.327 /// Uses the `dbg_stmt` field.
328 dbg_stmt,328 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,
329 /// Marks the start of an inline call.333 /// Marks the start of an inline call.
330 /// Uses `ty_pl` with the payload being the index of a Value.Function in air.values.334 /// Uses `ty_pl` with the payload being the index of a Value.Function in air.values.
331 dbg_inline_begin,335 dbg_inline_begin,
...@@ -990,6 +994,8 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {...@@ -990,6 +994,8 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
990 .dbg_stmt,994 .dbg_stmt,
991 .dbg_inline_begin,995 .dbg_inline_begin,
992 .dbg_inline_end,996 .dbg_inline_end,
997 .dbg_block_begin,
998 .dbg_block_end,
993 .dbg_var_ptr,999 .dbg_var_ptr,
994 .dbg_var_val,1000 .dbg_var_val,
995 .store,1001 .store,
src/AstGen.zig+12
...@@ -2052,6 +2052,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod...@@ -2052,6 +2052,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod
2052 const tree = astgen.tree;2052 const tree = astgen.tree;
2053 const node_tags = tree.nodes.items(.tag);2053 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
2055 var block_arena = std.heap.ArenaAllocator.init(gz.astgen.gpa);2061 var block_arena = std.heap.ArenaAllocator.init(gz.astgen.gpa);
2056 defer block_arena.deinit();2062 defer block_arena.deinit();
2057 const block_arena_allocator = block_arena.allocator();2063 const block_arena_allocator = block_arena.allocator();
...@@ -2105,6 +2111,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod...@@ -2105,6 +2111,12 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod
2105 }2111 }
2106 }2112 }
21072113
2114 _ = try gz.add(.{ .tag = .extended, .data = .{ .extended = .{
2115 .opcode = .dbg_block_end,
2116 .small = undefined,
2117 .operand = undefined
2118 } } });
2119
2108 try genDefers(gz, parent_scope, scope, .normal_only);2120 try genDefers(gz, parent_scope, scope, .normal_only);
2109 try checkUsed(gz, parent_scope, scope);2121 try checkUsed(gz, parent_scope, scope);
2110}2122}
src/Liveness.zig+2
...@@ -316,6 +316,8 @@ fn analyzeInst(...@@ -316,6 +316,8 @@ fn analyzeInst(
316 .dbg_stmt,316 .dbg_stmt,
317 .dbg_inline_begin,317 .dbg_inline_begin,
318 .dbg_inline_end,318 .dbg_inline_end,
319 .dbg_block_begin,
320 .dbg_block_end,
319 .unreach,321 .unreach,
320 .fence,322 .fence,
321 .ret_addr,323 .ret_addr,
src/Sema.zig+25-1
...@@ -1216,6 +1216,8 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -1216,6 +1216,8 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1216 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),1216 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
1217 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),1217 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
1218 .prefetch => return sema.zirPrefetch( block, extended),1218 .prefetch => return sema.zirPrefetch( block, extended),
1219 .dbg_block_begin => return sema.zirDbgBlockBegin( block),
1220 .dbg_block_end => return sema.zirDbgBlockEnd( block),
1219 // zig fmt: on1221 // zig fmt: on
1220 }1222 }
1221}1223}
...@@ -4215,6 +4217,26 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi...@@ -4215,6 +4217,26 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
4215 });4217 });
4216}4218}
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
4218fn zirDbgVar(4240fn zirDbgVar(
4219 sema: *Sema,4241 sema: *Sema,
4220 block: *Block,4242 block: *Block,
...@@ -5224,7 +5246,9 @@ fn emitDbgInline(...@@ -5224,7 +5246,9 @@ fn emitDbgInline(
5224 new_func_ty: Type,5246 new_func_ty: Type,
5225 tag: Air.Inst.Tag,5247 tag: Air.Inst.Tag,
5226) CompileError!void {5248) 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.
5228 if (old_func == new_func) return;5252 if (old_func == new_func) return;
52295253
5230 try sema.air_values.append(sema.gpa, try Value.Tag.function.create(sema.arena, new_func));5254 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 {...@@ -1646,6 +1646,10 @@ pub const Inst = struct {
1646 /// The `@prefetch` builtin.1646 /// The `@prefetch` builtin.
1647 /// `operand` is payload index to `BinNode`.1647 /// `operand` is payload index to `BinNode`.
1648 prefetch,1648 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
1650 pub const InstData = struct {1654 pub const InstData = struct {
1651 opcode: Extended,1655 opcode: Extended,
src/arch/aarch64/CodeGen.zig+9
...@@ -654,6 +654,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {...@@ -654,6 +654,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
654 .dbg_inline_end,654 .dbg_inline_end,
655 => try self.airDbgInline(inst),655 => try self.airDbgInline(inst),
656656
657 .dbg_block_begin,
658 .dbg_block_end,
659 => try self.airDbgBlock(inst),
660
657 .call => try self.airCall(inst, .auto),661 .call => try self.airCall(inst, .auto),
658 .call_always_tail => try self.airCall(inst, .always_tail),662 .call_always_tail => try self.airCall(inst, .always_tail),
659 .call_never_tail => try self.airCall(inst, .never_tail),663 .call_never_tail => try self.airCall(inst, .never_tail),
...@@ -2731,6 +2735,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {...@@ -2731,6 +2735,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
2731 return self.finishAir(inst, .dead, .{ .none, .none, .none });2735 return self.finishAir(inst, .dead, .{ .none, .none, .none });
2732}2736}
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
2734fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {2743fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
2735 const pl_op = self.air.instructions.items(.data)[inst].pl_op;2744 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
2736 const name = self.air.nullTerminatedString(pl_op.payload);2745 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 {...@@ -644,6 +644,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
644 .dbg_inline_end,644 .dbg_inline_end,
645 => try self.airDbgInline(inst),645 => try self.airDbgInline(inst),
646646
647 .dbg_block_begin,
648 .dbg_block_end,
649 => try self.airDbgBlock(inst),
650
647 .call => try self.airCall(inst, .auto),651 .call => try self.airCall(inst, .auto),
648 .call_always_tail => try self.airCall(inst, .always_tail),652 .call_always_tail => try self.airCall(inst, .always_tail),
649 .call_never_tail => try self.airCall(inst, .never_tail),653 .call_never_tail => try self.airCall(inst, .never_tail),
...@@ -2948,6 +2952,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {...@@ -2948,6 +2952,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
2948 return self.finishAir(inst, .dead, .{ .none, .none, .none });2952 return self.finishAir(inst, .dead, .{ .none, .none, .none });
2949}2953}
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
2951fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {2960fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
2952 const pl_op = self.air.instructions.items(.data)[inst].pl_op;2961 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
2953 const name = self.air.nullTerminatedString(pl_op.payload);2962 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 {...@@ -618,6 +618,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
618 .dbg_inline_end,618 .dbg_inline_end,
619 => try self.airDbgInline(inst),619 => try self.airDbgInline(inst),
620620
621 .dbg_block_begin,
622 .dbg_block_end,
623 => try self.airDbgBlock(inst),
624
621 .call => try self.airCall(inst, .auto),625 .call => try self.airCall(inst, .auto),
622 .call_always_tail => try self.airCall(inst, .always_tail),626 .call_always_tail => try self.airCall(inst, .always_tail),
623 .call_never_tail => try self.airCall(inst, .never_tail),627 .call_never_tail => try self.airCall(inst, .never_tail),
...@@ -1653,6 +1657,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {...@@ -1653,6 +1657,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
1653 return self.finishAir(inst, .dead, .{ .none, .none, .none });1657 return self.finishAir(inst, .dead, .{ .none, .none, .none });
1654}1658}
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
1656fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {1665fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
1657 const pl_op = self.air.instructions.items(.data)[inst].pl_op;1666 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
1658 const name = self.air.nullTerminatedString(pl_op.payload);1667 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 {...@@ -1330,6 +1330,8 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
1330 .dbg_stmt,1330 .dbg_stmt,
1331 .dbg_inline_begin,1331 .dbg_inline_begin,
1332 .dbg_inline_end,1332 .dbg_inline_end,
1333 .dbg_block_begin,
1334 .dbg_block_end,
1333 .dbg_var_ptr,1335 .dbg_var_ptr,
1334 .dbg_var_val,1336 .dbg_var_val,
1335 => WValue.none,1337 => WValue.none,
src/arch/x86_64/CodeGen.zig+9
...@@ -735,6 +735,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {...@@ -735,6 +735,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
735 .dbg_inline_end,735 .dbg_inline_end,
736 => try self.airDbgInline(inst),736 => try self.airDbgInline(inst),
737737
738 .dbg_block_begin,
739 .dbg_block_end,
740 => try self.airDbgBlock(inst),
741
738 .call => try self.airCall(inst, .auto),742 .call => try self.airCall(inst, .auto),
739 .call_always_tail => try self.airCall(inst, .always_tail),743 .call_always_tail => try self.airCall(inst, .always_tail),
740 .call_never_tail => try self.airCall(inst, .never_tail),744 .call_never_tail => try self.airCall(inst, .never_tail),
...@@ -3683,6 +3687,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {...@@ -3683,6 +3687,11 @@ fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
3683 return self.finishAir(inst, .dead, .{ .none, .none, .none });3687 return self.finishAir(inst, .dead, .{ .none, .none, .none });
3684}3688}
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
3686fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {3695fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
3687 const pl_op = self.air.instructions.items(.data)[inst].pl_op;3696 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
3688 const name = self.air.nullTerminatedString(pl_op.payload);3697 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...@@ -1786,6 +1786,10 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
1786 .dbg_inline_end,1786 .dbg_inline_end,
1787 => try airDbgInline(f, inst),1787 => try airDbgInline(f, inst),
17881788
1789 .dbg_block_begin,
1790 .dbg_block_end,
1791 => CValue{ .none = {} },
1792
1789 .call => try airCall(f, inst, .auto),1793 .call => try airCall(f, inst, .auto),
1790 .call_always_tail => try airCall(f, inst, .always_tail),1794 .call_always_tail => try airCall(f, inst, .always_tail),
1791 .call_never_tail => try airCall(f, inst, .never_tail),1795 .call_never_tail => try airCall(f, inst, .never_tail),
src/codegen/llvm.zig+22
...@@ -3220,6 +3220,10 @@ pub const FuncGen = struct {...@@ -3220,6 +3220,10 @@ pub const FuncGen = struct {
3220 /// Stack of locations where a call was inlined.3220 /// Stack of locations where a call was inlined.
3221 dbg_inlined: std.ArrayListUnmanaged(DbgState) = .{},3221 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
3223 /// This stores the LLVM values used in a function, such that they can be referred to3227 /// This stores the LLVM values used in a function, such that they can be referred to
3224 /// in other instructions. This table is cleared before every function is generated.3228 /// in other instructions. This table is cleared before every function is generated.
3225 func_inst_table: std.AutoHashMapUnmanaged(Air.Inst.Ref, *const llvm.Value),3229 func_inst_table: std.AutoHashMapUnmanaged(Air.Inst.Ref, *const llvm.Value),
...@@ -3254,6 +3258,7 @@ pub const FuncGen = struct {...@@ -3254,6 +3258,7 @@ pub const FuncGen = struct {
3254 fn deinit(self: *FuncGen) void {3258 fn deinit(self: *FuncGen) void {
3255 self.builder.dispose();3259 self.builder.dispose();
3256 self.dbg_inlined.deinit(self.gpa);3260 self.dbg_inlined.deinit(self.gpa);
3261 self.dbg_block_stack.deinit(self.gpa);
3257 self.func_inst_table.deinit(self.gpa);3262 self.func_inst_table.deinit(self.gpa);
3258 self.blocks.deinit(self.gpa);3263 self.blocks.deinit(self.gpa);
3259 }3264 }
...@@ -3475,6 +3480,8 @@ pub const FuncGen = struct {...@@ -3475,6 +3480,8 @@ pub const FuncGen = struct {
3475 .dbg_stmt => self.airDbgStmt(inst),3480 .dbg_stmt => self.airDbgStmt(inst),
3476 .dbg_inline_begin => try self.airDbgInlineBegin(inst),3481 .dbg_inline_begin => try self.airDbgInlineBegin(inst),
3477 .dbg_inline_end => try self.airDbgInlineEnd(inst),3482 .dbg_inline_end => try self.airDbgInlineEnd(inst),
3483 .dbg_block_begin => try self.airDbgBlockBegin(),
3484 .dbg_block_end => try self.airDbgBlockEnd(),
3478 .dbg_var_ptr => try self.airDbgVarPtr(inst),3485 .dbg_var_ptr => try self.airDbgVarPtr(inst),
3479 .dbg_var_val => try self.airDbgVarVal(inst),3486 .dbg_var_val => try self.airDbgVarVal(inst),
3480 // zig fmt: on3487 // zig fmt: on
...@@ -4256,6 +4263,21 @@ pub const FuncGen = struct {...@@ -4256,6 +4263,21 @@ pub const FuncGen = struct {
4256 return null;4263 return null;
4257 }4264 }
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
4259 fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {4281 fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
4260 const dib = self.dg.object.di_builder orelse return null;4282 const dib = self.dg.object.di_builder orelse return null;
4261 const pl_op = self.air.instructions.items(.data)[inst].pl_op;4283 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
src/print_air.zig+2
...@@ -272,6 +272,8 @@ const Writer = struct {...@@ -272,6 +272,8 @@ const Writer = struct {
272 .mul_with_overflow,272 .mul_with_overflow,
273 .shl_with_overflow,273 .shl_with_overflow,
274 => try w.writeOverflow(s, inst),274 => try w.writeOverflow(s, inst),
275
276 .dbg_block_begin, .dbg_block_end => {},
275 }277 }
276 }278 }
277279
src/print_zir.zig+4
...@@ -463,6 +463,10 @@ const Writer = struct {...@@ -463,6 +463,10 @@ const Writer = struct {
463 .builtin_src,463 .builtin_src,
464 => try self.writeExtNode(stream, extended),464 => try self.writeExtNode(stream, extended),
465465
466 .dbg_block_begin,
467 .dbg_block_end,
468 => try stream.writeAll("))"),
469
466 .@"asm" => try self.writeAsm(stream, extended),470 .@"asm" => try self.writeAsm(stream, extended),
467 .func => try self.writeFuncExtended(stream, extended),471 .func => try self.writeFuncExtended(stream, extended),
468 .variable => try self.writeVarExtended(stream, extended),472 .variable => try self.writeVarExtended(stream, extended),