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...@@ -828,7 +828,13 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
828828
829 .slice_open => {829 .slice_open => {
830 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);830 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
831 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);836 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
837 try emitDbgStmt(gz, line, column);
832 const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{838 const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
833 .lhs = lhs,839 .lhs = lhs,
834 .start = start,840 .start = start,
...@@ -837,9 +843,15 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -837,9 +843,15 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
837 },843 },
838 .slice => {844 .slice => {
839 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);845 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
840 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.Slice);851 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.Slice);
841 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);852 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
842 const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);853 const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);
854 try emitDbgStmt(gz, line, column);
843 const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{855 const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
844 .lhs = lhs,856 .lhs = lhs,
845 .start = start,857 .start = start,
...@@ -849,10 +861,16 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -849,10 +861,16 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
849 },861 },
850 .slice_sentinel => {862 .slice_sentinel => {
851 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);863 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
852 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SliceSentinel);869 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SliceSentinel);
853 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);870 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
854 const end = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;871 const end = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
855 const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);872 const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
873 try emitDbgStmt(gz, line, column);
856 const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{874 const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
857 .lhs = lhs,875 .lhs = lhs,
858 .start = start,876 .start = start,
...@@ -883,16 +901,26 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -883,16 +901,26 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
883 return rvalue(gz, ri, result, node);901 return rvalue(gz, ri, result, node);
884 },902 },
885 .unwrap_optional => switch (ri.rl) {903 .unwrap_optional => switch (ri.rl) {
886 .ref => return gz.addUnNode(904 .ref => {
887 .optional_payload_safe_ptr,905 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
888 try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs),906
889 node,907 maybeAdvanceSourceCursorToMainToken(gz, node);
890 ),908 const line = gz.astgen.source_line - gz.decl_line;
891 else => return rvalue(gz, ri, try gz.addUnNode(909 const column = gz.astgen.source_column;
892 .optional_payload_safe,910 try emitDbgStmt(gz, line, column);
893 try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),911
894 node,912 return gz.addUnNode(.optional_payload_safe_ptr, lhs, node);
895 ), 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 },
896 },924 },
897 .block_two, .block_two_semicolon => {925 .block_two, .block_two_semicolon => {
898 const statements = [2]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };926 const statements = [2]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
...@@ -3241,10 +3269,27 @@ fn assignOp(...@@ -3241,10 +3269,27 @@ fn assignOp(
3241 const node_datas = tree.nodes.items(.data);3269 const node_datas = tree.nodes.items(.data);
32423270
3243 const lhs_ptr = try lvalExpr(gz, scope, node_datas[infix_node].lhs);3271 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 }
3244 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);3283 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);
3245 const lhs_type = try gz.addUnNode(.typeof, lhs, infix_node);3284 const lhs_type = try gz.addUnNode(.typeof, lhs, infix_node);
3246 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = lhs_type } }, node_datas[infix_node].rhs);3285 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 }
3248 const result = try gz.addPlNode(op_inst_tag, infix_node, Zir.Inst.Bin{3293 const result = try gz.addPlNode(op_inst_tag, infix_node, Zir.Inst.Bin{
3249 .lhs = lhs,3294 .lhs = lhs,
3250 .rhs = rhs,3295 .rhs = rhs,
...@@ -5475,9 +5520,15 @@ fn addFieldAccess(...@@ -5475,9 +5520,15 @@ fn addFieldAccess(
5475 const dot_token = main_tokens[node];5520 const dot_token = main_tokens[node];
5476 const field_ident = dot_token + 1;5521 const field_ident = dot_token + 1;
5477 const str_index = try astgen.identAsString(field_ident);5522 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
5479 return gz.addPlNode(tag, node, Zir.Inst.Field{5530 return gz.addPlNode(tag, node, Zir.Inst.Field{
5480 .lhs = try expr(gz, scope, lhs_ri, object_node),5531 .lhs = lhs,
5481 .field_name_start = str_index,5532 .field_name_start = str_index,
5482 });5533 });
5483}5534}
...@@ -5488,18 +5539,33 @@ fn arrayAccess(...@@ -5488,18 +5539,33 @@ fn arrayAccess(
5488 ri: ResultInfo,5539 ri: ResultInfo,
5489 node: Ast.Node.Index,5540 node: Ast.Node.Index,
5490) InnerError!Zir.Inst.Ref {5541) InnerError!Zir.Inst.Ref {
5491 const astgen = gz.astgen;5542 const tree = gz.astgen.tree;
5492 const tree = astgen.tree;
5493 const node_datas = tree.nodes.items(.data);5543 const node_datas = tree.nodes.items(.data);
5494 switch (ri.rl) {5544 switch (ri.rl) {
5495 .ref => return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{5545 .ref => {
5496 .lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs),5546 const 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),5547
5498 }),5548 maybeAdvanceSourceCursorToMainToken(gz, node);
5499 else => return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{5549 const line = gz.astgen.source_line - gz.decl_line;
5500 .lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),5550 const column = gz.astgen.source_column;
5501 .rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs),5551
5502 }), node),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 },
5503 }5569 }
5504}5570}
55055571
...@@ -5514,10 +5580,26 @@ fn simpleBinOp(...@@ -5514,10 +5580,26 @@ fn simpleBinOp(
5514 const tree = astgen.tree;5580 const tree = astgen.tree;
5515 const node_datas = tree.nodes.items(.data);5581 const node_datas = tree.nodes.items(.data);
55165582
5517 const result = try gz.addPlNode(op_inst_tag, node, Zir.Inst.Bin{5583 const lhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].lhs, node);
5518 .lhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].lhs, node),5584 var line: u32 = undefined;
5519 .rhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].rhs, node),5585 var column: u32 = undefined;
5520 });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 });
5521 return rvalue(gz, ri, result, node);5603 return rvalue(gz, ri, result, node);
5522}5604}
55235605
...@@ -7858,9 +7940,7 @@ fn builtinCall(...@@ -7858,9 +7940,7 @@ fn builtinCall(
7858 },7940 },
78597941
7860 .src => {7942 .src => {
7861 const token_starts = tree.tokens.items(.start);7943 maybeAdvanceSourceCursorToMainToken(gz, node);
7862 const node_start = token_starts[tree.firstToken(node)];
7863 astgen.advanceSourceCursor(node_start);
7864 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.Src{7944 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.Src{
7865 .node = gz.nodeIndexToRelative(node),7945 .node = gz.nodeIndexToRelative(node),
7866 .line = astgen.source_line,7946 .line = astgen.source_line,
...@@ -7975,6 +8055,8 @@ fn builtinCall(...@@ -7975,6 +8055,8 @@ fn builtinCall(
7975 return rvalue(gz, ri, result, node);8055 return rvalue(gz, ri, result, node);
7976 },8056 },
7977 .err_set_cast => {8057 .err_set_cast => {
8058 try emitDbgNode(gz, node);
8059
7978 const result = try gz.addExtendedPayload(.err_set_cast, Zir.Inst.BinNode{8060 const result = try gz.addExtendedPayload(.err_set_cast, Zir.Inst.BinNode{
7979 .lhs = try typeExpr(gz, scope, params[0]),8061 .lhs = try typeExpr(gz, scope, params[0]),
7980 .rhs = try expr(gz, scope, .{ .rl = .none }, params[1]),8062 .rhs = try expr(gz, scope, .{ .rl = .none }, params[1]),
...@@ -8280,6 +8362,8 @@ fn typeCast(...@@ -8280,6 +8362,8 @@ fn typeCast(
8280 rhs_node: Ast.Node.Index,8362 rhs_node: Ast.Node.Index,
8281 tag: Zir.Inst.Tag,8363 tag: Zir.Inst.Tag,
8282) InnerError!Zir.Inst.Ref {8364) InnerError!Zir.Inst.Ref {
8365 try emitDbgNode(gz, node);
8366
8283 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{8367 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
8284 .lhs = try typeExpr(gz, scope, lhs_node),8368 .lhs = try typeExpr(gz, scope, lhs_node),
8285 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),8369 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),
...@@ -8309,6 +8393,10 @@ fn simpleUnOp(...@@ -8309,6 +8393,10 @@ fn simpleUnOp(
8309 operand_node: Ast.Node.Index,8393 operand_node: Ast.Node.Index,
8310 tag: Zir.Inst.Tag,8394 tag: Zir.Inst.Tag,
8311) InnerError!Zir.Inst.Ref {8395) InnerError!Zir.Inst.Ref {
8396 switch (tag) {
8397 .tag_name, .error_name, .ptr_to_int => try emitDbgNode(gz, node),
8398 else => {},
8399 }
8312 const operand = try expr(gz, scope, operand_ri, operand_node);8400 const operand = try expr(gz, scope, operand_ri, operand_node);
8313 const result = try gz.addUnNode(tag, operand, node);8401 const result = try gz.addUnNode(tag, operand, node);
8314 return rvalue(gz, ri, result, node);8402 return rvalue(gz, ri, result, node);
...@@ -8381,6 +8469,8 @@ fn divBuiltin(...@@ -8381,6 +8469,8 @@ fn divBuiltin(
8381 rhs_node: Ast.Node.Index,8469 rhs_node: Ast.Node.Index,
8382 tag: Zir.Inst.Tag,8470 tag: Zir.Inst.Tag,
8383) InnerError!Zir.Inst.Ref {8471) InnerError!Zir.Inst.Ref {
8472 try emitDbgNode(gz, node);
8473
8384 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{8474 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
8385 .lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node),8475 .lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node),
8386 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),8476 .rhs = try expr(gz, scope, .{ .rl = .none }, rhs_node),
...@@ -8434,8 +8524,15 @@ fn shiftOp(...@@ -8434,8 +8524,15 @@ fn shiftOp(
8434 tag: Zir.Inst.Tag,8524 tag: Zir.Inst.Tag,
8435) InnerError!Zir.Inst.Ref {8525) InnerError!Zir.Inst.Ref {
8436 const lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node);8526 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
8437 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);8532 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);
8438 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = log2_int_type }, .ctx = .shift_op }, rhs_node);8533 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = log2_int_type }, .ctx = .shift_op }, rhs_node);
8534
8535 try emitDbgStmt(gz, line, column);
8439 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{8536 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
8440 .lhs = lhs,8537 .lhs = lhs,
8441 .rhs = rhs,8538 .rhs = rhs,
...@@ -12065,6 +12162,18 @@ fn detectLocalShadowing(...@@ -12065,6 +12162,18 @@ fn detectLocalShadowing(
12065 };12162 };
12066}12163}
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
12068/// Advances the source cursor to the beginning of `node`.12177/// Advances the source cursor to the beginning of `node`.
12069fn advanceSourceCursorToNode(astgen: *AstGen, node: Ast.Node.Index) void {12178fn advanceSourceCursorToNode(astgen: *AstGen, node: Ast.Node.Index) void {
12070 const tree = astgen.tree;12179 const tree = astgen.tree;