authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-09 16:49:24+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-11 17:59:53+02:00
log25c850642190bf9939790b872a3657410ec4d19f
tree48d1c8858ef83f74e627b899ac3321285f46a7e1
parent0a188190b3b2b42906d0cc38f101b010bc07b414

AstGen: emit dbg_stmt before (nearly) all operations that have a safety check

All implicit casts can also potentially lead to a panic being emitted but adding a dbg_stmt before every instruction is not feasible. This adds 24k new instructions to the ZIR for Sema.zig increasing its size from 3.8MiB to 4.0MiB. Closes #13488

1 files changed, 137 insertions(+), 28 deletions(-)

src/AstGen.zig+137-28
......@@ -828,7 +828,13 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
828828
829829 .slice_open => {
830830 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
831
832 maybeAdvanceSourceCursorToMainToken(gz, node);
833 const line = gz.astgen.source_line - gz.decl_line;
834 const column = gz.astgen.source_column;
835
831836 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
837 try emitDbgStmt(gz, line, column);
832838 const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
833839 .lhs = lhs,
834840 .start = start,
......@@ -837,9 +843,15 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
837843 },
838844 .slice => {
839845 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
846
847 maybeAdvanceSourceCursorToMainToken(gz, node);
848 const line = gz.astgen.source_line - gz.decl_line;
849 const column = gz.astgen.source_column;
850
840851 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.Slice);
841852 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
842853 const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);
854 try emitDbgStmt(gz, line, column);
843855 const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
844856 .lhs = lhs,
845857 .start = start,
......@@ -849,10 +861,16 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
849861 },
850862 .slice_sentinel => {
851863 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
864
865 maybeAdvanceSourceCursorToMainToken(gz, node);
866 const line = gz.astgen.source_line - gz.decl_line;
867 const column = gz.astgen.source_column;
868
852869 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SliceSentinel);
853870 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
854871 const end = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
855872 const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
873 try emitDbgStmt(gz, line, column);
856874 const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
857875 .lhs = lhs,
858876 .start = start,
......@@ -883,16 +901,26 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
883901 return rvalue(gz, ri, result, node);
884902 },
885903 .unwrap_optional => switch (ri.rl) {
886 .ref => return gz.addUnNode(
887 .optional_payload_safe_ptr,
888 try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs),
889 node,
890 ),
891 else => return rvalue(gz, ri, try gz.addUnNode(
892 .optional_payload_safe,
893 try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),
894 node,
895 ), node),
904 .ref => {
905 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
906
907 maybeAdvanceSourceCursorToMainToken(gz, node);
908 const line = gz.astgen.source_line - gz.decl_line;
909 const column = gz.astgen.source_column;
910 try emitDbgStmt(gz, line, column);
911
912 return gz.addUnNode(.optional_payload_safe_ptr, lhs, node);
913 },
914 else => {
915 const lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs);
916
917 maybeAdvanceSourceCursorToMainToken(gz, node);
918 const line = gz.astgen.source_line - gz.decl_line;
919 const column = gz.astgen.source_column;
920 try emitDbgStmt(gz, line, column);
921
922 return rvalue(gz, ri, try gz.addUnNode(.optional_payload_safe, lhs, node), node);
923 },
896924 },
897925 .block_two, .block_two_semicolon => {
898926 const statements = [2]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
......@@ -3241,10 +3269,27 @@ fn assignOp(
32413269 const node_datas = tree.nodes.items(.data);
32423270
32433271 const lhs_ptr = try lvalExpr(gz, scope, node_datas[infix_node].lhs);
3272
3273 var line: u32 = undefined;
3274 var column: u32 = undefined;
3275 switch (op_inst_tag) {
3276 .add, .sub, .mul, .div, .mod_rem => {
3277 maybeAdvanceSourceCursorToMainToken(gz, infix_node);
3278 line = gz.astgen.source_line - gz.decl_line;
3279 column = gz.astgen.source_column;
3280 },
3281 else => {},
3282 }
32443283 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);
32453284 const lhs_type = try gz.addUnNode(.typeof, lhs, infix_node);
32463285 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = lhs_type } }, node_datas[infix_node].rhs);
32473286
3287 switch (op_inst_tag) {
3288 .add, .sub, .mul, .div, .mod_rem => {
3289 try emitDbgStmt(gz, line, column);
3290 },
3291 else => {},
3292 }
32483293 const result = try gz.addPlNode(op_inst_tag, infix_node, Zir.Inst.Bin{
32493294 .lhs = lhs,
32503295 .rhs = rhs,
......@@ -5475,9 +5520,15 @@ fn addFieldAccess(
54755520 const dot_token = main_tokens[node];
54765521 const field_ident = dot_token + 1;
54775522 const str_index = try astgen.identAsString(field_ident);
5523 const lhs = try expr(gz, scope, lhs_ri, object_node);
5524
5525 maybeAdvanceSourceCursorToMainToken(gz, node);
5526 const line = gz.astgen.source_line - gz.decl_line;
5527 const column = gz.astgen.source_column;
5528 try emitDbgStmt(gz, line, column);
54785529
54795530 return gz.addPlNode(tag, node, Zir.Inst.Field{
5480 .lhs = try expr(gz, scope, lhs_ri, object_node),
5531 .lhs = lhs,
54815532 .field_name_start = str_index,
54825533 });
54835534}
......@@ -5488,18 +5539,33 @@ fn arrayAccess(
54885539 ri: ResultInfo,
54895540 node: Ast.Node.Index,
54905541) InnerError!Zir.Inst.Ref {
5491 const astgen = gz.astgen;
5492 const tree = astgen.tree;
5542 const tree = gz.astgen.tree;
54935543 const node_datas = tree.nodes.items(.data);
54945544 switch (ri.rl) {
5495 .ref => return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{
5496 .lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs),
5497 .rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs),
5498 }),
5499 else => return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{
5500 .lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),
5501 .rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs),
5502 }), node),
5545 .ref => {
5546 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
5547
5548 maybeAdvanceSourceCursorToMainToken(gz, node);
5549 const line = gz.astgen.source_line - gz.decl_line;
5550 const column = gz.astgen.source_column;
5551
5552 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);
5553 try emitDbgStmt(gz, line, column);
5554
5555 return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
5556 },
5557 else => {
5558 const lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs);
5559
5560 maybeAdvanceSourceCursorToMainToken(gz, node);
5561 const line = gz.astgen.source_line - gz.decl_line;
5562 const column = gz.astgen.source_column;
5563
5564 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);
5565 try emitDbgStmt(gz, line, column);
5566
5567 return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs }), node);
5568 },
55035569 }
55045570}
55055571
......@@ -5514,10 +5580,26 @@ fn simpleBinOp(
55145580 const tree = astgen.tree;
55155581 const node_datas = tree.nodes.items(.data);
55165582
5517 const result = try gz.addPlNode(op_inst_tag, node, Zir.Inst.Bin{
5518 .lhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].lhs, node),
5519 .rhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].rhs, node),
5520 });
5583 const lhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].lhs, node);
5584 var line: u32 = undefined;
5585 var column: u32 = undefined;
5586 switch (op_inst_tag) {
5587 .add, .sub, .mul, .div, .mod_rem => {
5588 maybeAdvanceSourceCursorToMainToken(gz, node);
5589 line = gz.astgen.source_line - gz.decl_line;
5590 column = gz.astgen.source_column;
5591 },
5592 else => {},
5593 }
5594 const rhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].rhs, node);
5595
5596 switch (op_inst_tag) {
5597 .add, .sub, .mul, .div, .mod_rem => {
5598 try emitDbgStmt(gz, line, column);
5599 },
5600 else => {},
5601 }
5602 const result = try gz.addPlNode(op_inst_tag, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
55215603 return rvalue(gz, ri, result, node);
55225604}
55235605
......@@ -7858,9 +7940,7 @@ fn builtinCall(
78587940 },
78597941
78607942 .src => {
7861 const token_starts = tree.tokens.items(.start);
7862 const node_start = token_starts[tree.firstToken(node)];
7863 astgen.advanceSourceCursor(node_start);
7943 maybeAdvanceSourceCursorToMainToken(gz, node);
78647944 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.Src{
78657945 .node = gz.nodeIndexToRelative(node),
78667946 .line = astgen.source_line,
......@@ -7975,6 +8055,8 @@ fn builtinCall(
79758055 return rvalue(gz, ri, result, node);
79768056 },
79778057 .err_set_cast => {
8058 try emitDbgNode(gz, node);
8059
79788060 const result = try gz.addExtendedPayload(.err_set_cast, Zir.Inst.BinNode{
79798061 .lhs = try typeExpr(gz, scope, params[0]),
79808062 .rhs = try expr(gz, scope, .{ .rl = .none }, params[1]),
......@@ -8280,6 +8362,8 @@ fn typeCast(
82808362 rhs_node: Ast.Node.Index,
82818363 tag: Zir.Inst.Tag,
82828364) InnerError!Zir.Inst.Ref {
8365 try emitDbgNode(gz, node);
8366
82838367 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
82848368 .lhs = try typeExpr(gz, scope, lhs_node),
82858369 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),
......@@ -8309,6 +8393,10 @@ fn simpleUnOp(
83098393 operand_node: Ast.Node.Index,
83108394 tag: Zir.Inst.Tag,
83118395) InnerError!Zir.Inst.Ref {
8396 switch (tag) {
8397 .tag_name, .error_name, .ptr_to_int => try emitDbgNode(gz, node),
8398 else => {},
8399 }
83128400 const operand = try expr(gz, scope, operand_ri, operand_node);
83138401 const result = try gz.addUnNode(tag, operand, node);
83148402 return rvalue(gz, ri, result, node);
......@@ -8381,6 +8469,8 @@ fn divBuiltin(
83818469 rhs_node: Ast.Node.Index,
83828470 tag: Zir.Inst.Tag,
83838471) InnerError!Zir.Inst.Ref {
8472 try emitDbgNode(gz, node);
8473
83848474 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
83858475 .lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node),
83868476 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),
......@@ -8434,8 +8524,15 @@ fn shiftOp(
84348524 tag: Zir.Inst.Tag,
84358525) InnerError!Zir.Inst.Ref {
84368526 const lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node);
8527
8528 maybeAdvanceSourceCursorToMainToken(gz, node);
8529 const line = gz.astgen.source_line - gz.decl_line;
8530 const column = gz.astgen.source_column;
8531
84378532 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);
84388533 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = log2_int_type }, .ctx = .shift_op }, rhs_node);
8534
8535 try emitDbgStmt(gz, line, column);
84398536 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
84408537 .lhs = lhs,
84418538 .rhs = rhs,
......@@ -12065,6 +12162,18 @@ fn detectLocalShadowing(
1206512162 };
1206612163}
1206712164
12165/// Advances the source cursor to the main token of `node` if not in comptime scope.
12166/// Usually paired with `emitDbgStmt`.
12167fn maybeAdvanceSourceCursorToMainToken(gz: *GenZir, node: Ast.Node.Index) void {
12168 if (gz.force_comptime) return;
12169
12170 const tree = gz.astgen.tree;
12171 const token_starts = tree.tokens.items(.start);
12172 const main_tokens = tree.nodes.items(.main_token);
12173 const node_start = token_starts[main_tokens[node]];
12174 gz.astgen.advanceSourceCursor(node_start);
12175}
12176
1206812177/// Advances the source cursor to the beginning of `node`.
1206912178fn advanceSourceCursorToNode(astgen: *AstGen, node: Ast.Node.Index) void {
1207012179 const tree = astgen.tree;