authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 22:48:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 22:48:28-07:00
logbd2154da3d90daa4520ee7ef69dac42f9049ed92
tree2b541e218121779de830b7d372975c790b469014
parentb2682237dbe90306b569cb36914f8823cd7b0431

stage2: the code is compiling again

(with a lot of things commented out)

5 files changed, 46 insertions(+), 14 deletions(-)

src/Module.zig+18-6
......@@ -923,6 +923,20 @@ pub const Scope = struct {
923923 try block.instructions.append(block.sema.gpa, &inst.base);
924924 return &inst.base;
925925 }
926
927 pub fn addDbgStmt(block: *Scope.Block, src: LazySrcLoc, abs_byte_off: u32) !*ir.Inst {
928 const inst = try block.sema.arena.create(ir.Inst.DbgStmt);
929 inst.* = .{
930 .base = .{
931 .tag = .dbg_stmt,
932 .ty = Type.initTag(.void),
933 .src = src,
934 },
935 .byte_offset = abs_byte_off,
936 };
937 try block.instructions.append(block.sema.gpa, &inst.base);
938 return &inst.base;
939 }
926940 };
927941
928942 /// This is a temporary structure; references to it are valid only
......@@ -1330,14 +1344,12 @@ pub const SrcLoc = struct {
13301344 .byte_abs => |byte_index| return byte_index,
13311345
13321346 .token_abs => |tok_index| {
1333 const file_scope = src_loc.container.file_scope;
1334 const tree = try mod.getAstTree(file_scope);
1347 const tree = src_loc.container.file_scope.base.tree();
13351348 const token_starts = tree.tokens.items(.start);
13361349 return token_starts[tok_index];
13371350 },
13381351 .node_abs => |node_index| {
1339 const file_scope = src_loc.container.file_scope;
1340 const tree = try mod.getAstTree(file_scope);
1352 const tree = src_loc.container.file_scope.base.tree();
13411353 const token_starts = tree.tokens.items(.start);
13421354 const tok_index = tree.firstToken(node_index);
13431355 return token_starts[tok_index];
......@@ -1349,14 +1361,14 @@ pub const SrcLoc = struct {
13491361 .token_offset => |tok_off| {
13501362 const decl = src_loc.container.decl;
13511363 const tok_index = decl.srcToken() + tok_off;
1352 const tree = try mod.getAstTree(decl.container.file_scope);
1364 const tree = src_loc.container.file_scope.base.tree();
13531365 const token_starts = tree.tokens.items(.start);
13541366 return token_starts[tok_index];
13551367 },
13561368 .node_offset => |node_off| {
13571369 const decl = src_loc.container.decl;
13581370 const node_index = decl.srcNode() + node_off;
1359 const tree = try mod.getAstTree(decl.container.file_scope);
1371 const tree = src_loc.container.file_scope.base.tree();
13601372 const tok_index = tree.firstToken(node_index);
13611373 const token_starts = tree.tokens.items(.start);
13621374 return token_starts[tok_index];
src/Sema.zig+3-1
......@@ -1003,7 +1003,9 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
10031003
10041004 const src_node = sema.code.instructions.items(.data)[inst].node;
10051005 const src: LazySrcLoc = .{ .node_offset = src_node };
1006 return block.addNoOp(src, Type.initTag(.void), .dbg_stmt);
1006 const src_loc = src.toSrcLoc(&block.base);
1007 const abs_byte_off = try src_loc.byteOffset(sema.mod);
1008 return block.addDbgStmt(src, abs_byte_off);
10071009}
10081010
10091011fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
src/codegen.zig+9-5
......@@ -792,8 +792,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
792792 }
793793 }
794794
795 fn dbgAdvancePCAndLine(self: *Self, src: usize) InnerError!void {
796 self.prev_di_src = src;
795 fn dbgAdvancePCAndLine(self: *Self, abs_byte_off: usize) InnerError!void {
796 self.prev_di_src = abs_byte_off;
797797 self.prev_di_pc = self.code.items.len;
798798 switch (self.debug_output) {
799799 .dwarf => |dbg_out| {
......@@ -801,7 +801,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
801801 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
802802 // this involves scanning over the source code for newlines
803803 // (but only from the previous byte offset to the new one).
804 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
804 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, abs_byte_off);
805805 const delta_pc = self.code.items.len - self.prev_di_pc;
806806 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit
807807 // single-byte opcodes that add different numbers to both the PC and the line number
......@@ -2315,8 +2315,12 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
23152315 }
23162316 }
23172317
2318 fn genDbgStmt(self: *Self, inst: *ir.Inst.NoOp) !MCValue {
2319 try self.dbgAdvancePCAndLine(inst.base.src);
2318 fn genDbgStmt(self: *Self, inst: *ir.Inst.DbgStmt) !MCValue {
2319 // TODO when reworking tzir memory layout, rework source locations here as
2320 // well to be more efficient, as well as support inlined function calls correctly.
2321 // For now we convert LazySrcLoc to absolute byte offset, to match what the
2322 // existing codegen code expects.
2323 try self.dbgAdvancePCAndLine(inst.byte_offset);
23202324 assert(inst.base.isUnused());
23212325 return MCValue.dead;
23222326 }
src/codegen/c.zig+1-1
......@@ -760,7 +760,7 @@ fn genCall(o: *Object, inst: *Inst.Call) !CValue {
760760 }
761761}
762762
763fn genDbgStmt(o: *Object, inst: *Inst.NoOp) !CValue {
763fn genDbgStmt(o: *Object, inst: *Inst.DbgStmt) !CValue {
764764 // TODO emit #line directive here with line number and filename
765765 return CValue.none;
766766}
src/ir.zig+15-1
......@@ -138,7 +138,6 @@ pub const Inst = struct {
138138 .retvoid,
139139 .unreach,
140140 .breakpoint,
141 .dbg_stmt,
142141 => NoOp,
143142
144143 .ref,
......@@ -198,6 +197,7 @@ pub const Inst = struct {
198197 .loop => Loop,
199198 .varptr => VarPtr,
200199 .switchbr => SwitchBr,
200 .dbg_stmt => DbgStmt,
201201 };
202202 }
203203
......@@ -584,6 +584,20 @@ pub const Inst = struct {
584584 return (self.deaths + self.else_index)[0..self.else_deaths];
585585 }
586586 };
587
588 pub const DbgStmt = struct {
589 pub const base_tag = Tag.dbg_stmt;
590
591 base: Inst,
592 byte_offset: u32,
593
594 pub fn operandCount(self: *const DbgStmt) usize {
595 return 0;
596 }
597 pub fn getOperand(self: *const DbgStmt, index: usize) ?*Inst {
598 return null;
599 }
600 };
587601};
588602
589603pub const Body = struct {