authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-13 01:54:57+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-16 11:26:33+00:00
log434537213e406191959818f573b24ca45c1b0e45
tree5892fc8f71a48ef1188cee96112a5d7a5706c14f
parentaba29f9789aff0c1ace2a1d0031818ec2c04070c
signaturelock-open Commit is signed but in an unrecognized format.

Sema: eliminate `src` field

`sema.src` is a failed experiment. It introduces complexity, and makes often unwarranted assumptions about the existence of instructions providing source locations, requiring an unreasonable amount of caution in AstGen for correctness. Eliminating it simplifies the whole frontend. This required adding source locations to a few instructions, but the cost in ZIR bytes should be counteracted by the other work on this branch.

5 files changed, 221 insertions(+), 204 deletions(-)

src/AstGen.zig+79-61
......@@ -2122,7 +2122,7 @@ fn restoreErrRetIndex(
21222122 else => .none, // always restore/pop
21232123 },
21242124 };
2125 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op });
2125 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op }, node);
21262126}
21272127
21282128fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
......@@ -2179,7 +2179,7 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
21792179
21802180 // As our last action before the break, "pop" the error trace if needed
21812181 if (!block_gz.is_comptime)
2182 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2182 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, node);
21832183
21842184 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
21852185 return Zir.Inst.Ref.unreachable_value;
......@@ -2271,7 +2271,7 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
22712271
22722272 // As our last action before the continue, "pop" the error trace if needed
22732273 if (!gen_zir.is_comptime)
2274 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always);
2274 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always, node);
22752275
22762276 _ = try parent_gz.addBreak(break_tag, continue_block, .void_value);
22772277 return Zir.Inst.Ref.unreachable_value;
......@@ -2331,7 +2331,7 @@ fn blockExpr(
23312331
23322332 if (!block_scope.endsWithNoReturn()) {
23332333 // As our last action before the break, "pop" the error trace if needed
2334 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2334 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
23352335 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
23362336 }
23372337
......@@ -2426,7 +2426,7 @@ fn labeledBlockExpr(
24262426 try blockExprStmts(&block_scope, &block_scope.base, statements);
24272427 if (!block_scope.endsWithNoReturn()) {
24282428 // As our last action before the return, "pop" the error trace if needed
2429 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2429 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
24302430 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
24312431 }
24322432
......@@ -2818,7 +2818,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
28182818 .export_value,
28192819 .set_eval_branch_quota,
28202820 .atomic_store,
2821 .store,
28222821 .store_node,
28232822 .store_to_inferred_ptr,
28242823 .resolve_inferred_alloc,
......@@ -2829,7 +2828,8 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
28292828 .validate_deref,
28302829 .validate_destructure,
28312830 .save_err_ret_index,
2832 .restore_err_ret_index,
2831 .restore_err_ret_index_unconditional,
2832 .restore_err_ret_index_fn_entry,
28332833 .validate_struct_init_ty,
28342834 .validate_struct_init_result_ty,
28352835 .validate_ptr_struct_init,
......@@ -3692,7 +3692,10 @@ fn assignOp(
36923692 .lhs = lhs,
36933693 .rhs = rhs,
36943694 });
3695 _ = try gz.addBin(.store, lhs_ptr, result);
3695 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3696 .lhs = lhs_ptr,
3697 .rhs = result,
3698 });
36963699}
36973700
36983701fn assignShift(
......@@ -3715,7 +3718,10 @@ fn assignShift(
37153718 .lhs = lhs,
37163719 .rhs = rhs,
37173720 });
3718 _ = try gz.addBin(.store, lhs_ptr, result);
3721 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3722 .lhs = lhs_ptr,
3723 .rhs = result,
3724 });
37193725}
37203726
37213727fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {
......@@ -3733,7 +3739,10 @@ fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerE
37333739 .lhs = lhs,
37343740 .rhs = rhs,
37353741 });
3736 _ = try gz.addBin(.store, lhs_ptr, result);
3742 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3743 .lhs = lhs_ptr,
3744 .rhs = result,
3745 });
37373746}
37383747
37393748fn ptrType(
......@@ -4294,7 +4303,7 @@ fn fnDecl(
42944303
42954304 if (!fn_gz.endsWithNoReturn()) {
42964305 // As our last action before the return, "pop" the error trace if needed
4297 _ = try gz.addRestoreErrRetIndex(.ret, .always);
4306 _ = try gz.addRestoreErrRetIndex(.ret, .always, decl_node);
42984307
42994308 // Add implicit return at end of function.
43004309 _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
......@@ -4742,7 +4751,7 @@ fn testDecl(
47424751 if (fn_block.isEmpty() or !fn_block.refIsNoReturn(block_result)) {
47434752
47444753 // As our last action before the return, "pop" the error trace if needed
4745 _ = try gz.addRestoreErrRetIndex(.ret, .always);
4754 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
47464755
47474756 // Add implicit return at end of function.
47484757 _ = try fn_block.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
......@@ -6149,7 +6158,7 @@ fn boolBinOp(
61496158 const node_datas = tree.nodes.items(.data);
61506159
61516160 const lhs = try expr(gz, scope, bool_ri, node_datas[node].lhs);
6152 const bool_br = try gz.addBoolBr(zir_tag, lhs);
6161 const bool_br = (try gz.addPlNodePayloadIndex(zir_tag, node, undefined)).toIndex().?;
61536162
61546163 var rhs_scope = gz.makeSubBlock(scope);
61556164 defer rhs_scope.unstack();
......@@ -6157,7 +6166,7 @@ fn boolBinOp(
61576166 if (!gz.refIsNoReturn(rhs)) {
61586167 _ = try rhs_scope.addBreakWithSrcNode(.break_inline, bool_br, rhs, node_datas[node].rhs);
61596168 }
6160 try rhs_scope.setBoolBrBody(bool_br);
6169 try rhs_scope.setBoolBrBody(bool_br, lhs);
61616170
61626171 const block_ref = bool_br.toRef();
61636172 return rvalue(gz, ri, block_ref, node);
......@@ -6725,7 +6734,10 @@ fn forExpr(
67256734 const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;
67266735 const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);
67276736 // initialize to zero
6728 _ = try parent_gz.addBin(.store, index_ptr, .zero_usize);
6737 _ = try parent_gz.addPlNode(.store_node, node, Zir.Inst.Bin{
6738 .lhs = index_ptr,
6739 .rhs = .zero_usize,
6740 });
67296741 break :blk index_ptr;
67306742 };
67316743
......@@ -6955,7 +6967,10 @@ fn forExpr(
69556967 .lhs = index,
69566968 .rhs = .one_usize,
69576969 });
6958 _ = try loop_scope.addBin(.store, index_ptr, index_plus_one);
6970 _ = try loop_scope.addPlNode(.store_node, node, Zir.Inst.Bin{
6971 .lhs = index_ptr,
6972 .rhs = index_plus_one,
6973 });
69596974 const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;
69606975 _ = try loop_scope.addNode(repeat_tag, node);
69616976
......@@ -8008,7 +8023,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80088023 try genDefers(gz, defer_outer, scope, .normal_only);
80098024
80108025 // As our last action before the return, "pop" the error trace if needed
8011 _ = try gz.addRestoreErrRetIndex(.ret, .always);
8026 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80128027
80138028 _ = try gz.addUnNode(.ret_node, .void_value, node);
80148029 return Zir.Inst.Ref.unreachable_value;
......@@ -8051,7 +8066,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80518066 try genDefers(gz, defer_outer, scope, .normal_only);
80528067
80538068 // As our last action before the return, "pop" the error trace if needed
8054 _ = try gz.addRestoreErrRetIndex(.ret, .always);
8069 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80558070
80568071 try emitDbgStmt(gz, ret_lc);
80578072 try gz.addRet(ri, operand, node);
......@@ -8074,7 +8089,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80748089
80758090 // As our last action before the return, "pop" the error trace if needed
80768091 const result = if (ri.rl == .ptr) try gz.addUnNode(.load, ri.rl.ptr.inst, node) else operand;
8077 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result });
8092 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result }, node);
80788093
80798094 try gz.addRet(ri, operand, node);
80808095 return Zir.Inst.Ref.unreachable_value;
......@@ -8091,7 +8106,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80918106 try genDefers(&then_scope, defer_outer, scope, .normal_only);
80928107
80938108 // As our last action before the return, "pop" the error trace if needed
8094 _ = try then_scope.addRestoreErrRetIndex(.ret, .always);
8109 _ = try then_scope.addRestoreErrRetIndex(.ret, .always, node);
80958110
80968111 try emitDbgStmt(&then_scope, ret_lc);
80978112 try then_scope.addRet(ri, operand, node);
......@@ -10979,7 +10994,10 @@ fn rvalueInner(
1097910994 return .void_value;
1098010995 },
1098110996 .inferred_ptr => |alloc| {
10982 _ = try gz.addBin(.store_to_inferred_ptr, alloc, result);
10997 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
10998 .lhs = alloc,
10999 .rhs = result,
11000 });
1098311001 return .void_value;
1098411002 },
1098511003 .destructure => |destructure| {
......@@ -11006,7 +11024,10 @@ fn rvalueInner(
1100611024 });
1100711025 },
1100811026 .inferred_ptr => |ptr_inst| {
11009 _ = try gz.addBin(.store_to_inferred_ptr, ptr_inst, elem_val);
11027 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
11028 .lhs = ptr_inst,
11029 .rhs = elem_val,
11030 });
1101011031 },
1101111032 .discard => unreachable,
1101211033 }
......@@ -11828,19 +11849,20 @@ const GenZir = struct {
1182811849 }
1182911850
1183011851 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
11831 fn setBoolBrBody(gz: *GenZir, inst: Zir.Inst.Index) !void {
11852 fn setBoolBrBody(gz: *GenZir, bool_br: Zir.Inst.Index, bool_br_lhs: Zir.Inst.Ref) !void {
1183211853 const astgen = gz.astgen;
1183311854 const gpa = astgen.gpa;
1183411855 const body = gz.instructionsSlice();
1183511856 const body_len = astgen.countBodyLenAfterFixups(body);
1183611857 try astgen.extra.ensureUnusedCapacity(
1183711858 gpa,
11838 @typeInfo(Zir.Inst.Block).Struct.fields.len + body_len,
11859 @typeInfo(Zir.Inst.BoolBr).Struct.fields.len + body_len,
1183911860 );
1184011861 const zir_datas = astgen.instructions.items(.data);
11841 zir_datas[@intFromEnum(inst)].bool_br.payload_index = astgen.addExtraAssumeCapacity(
11842 Zir.Inst.Block{ .body_len = body_len },
11843 );
11862 zir_datas[@intFromEnum(bool_br)].pl_node.payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.BoolBr{
11863 .lhs = bool_br_lhs,
11864 .body_len = body_len,
11865 });
1184411866 astgen.appendBodyWithFixups(body);
1184511867 gz.unstack();
1184611868 }
......@@ -12225,30 +12247,6 @@ const GenZir = struct {
1222512247 return new_index.toRef();
1222612248 }
1222712249
12228 /// Note that this returns a `Zir.Inst.Index` not a ref.
12229 /// Leaves the `payload_index` field undefined.
12230 fn addBoolBr(
12231 gz: *GenZir,
12232 tag: Zir.Inst.Tag,
12233 lhs: Zir.Inst.Ref,
12234 ) !Zir.Inst.Index {
12235 assert(lhs != .none);
12236 const gpa = gz.astgen.gpa;
12237 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12238 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
12239
12240 const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
12241 gz.astgen.instructions.appendAssumeCapacity(.{
12242 .tag = tag,
12243 .data = .{ .bool_br = .{
12244 .lhs = lhs,
12245 .payload_index = undefined,
12246 } },
12247 });
12248 gz.instructions.appendAssumeCapacity(new_index);
12249 return new_index;
12250 }
12251
1225212250 fn addInt(gz: *GenZir, integer: u64) !Zir.Inst.Ref {
1225312251 return gz.add(.{
1225412252 .tag = .int,
......@@ -12569,17 +12567,37 @@ const GenZir = struct {
1256912567 always: void,
1257012568 if_non_error: Zir.Inst.Ref,
1257112569 },
12570 src_node: Ast.Node.Index,
1257212571 ) !Zir.Inst.Index {
12573 return gz.addAsIndex(.{
12574 .tag = .restore_err_ret_index,
12575 .data = .{ .restore_err_ret_index = .{
12576 .block = switch (bt) {
12577 .ret => .none,
12578 .block => |b| b.toRef(),
12579 },
12580 .operand = if (cond == .if_non_error) cond.if_non_error else .none,
12581 } },
12582 });
12572 switch (cond) {
12573 .always => return gz.addAsIndex(.{
12574 .tag = .restore_err_ret_index_unconditional,
12575 .data = .{ .un_node = .{
12576 .operand = switch (bt) {
12577 .ret => .none,
12578 .block => |b| b.toRef(),
12579 },
12580 .src_node = gz.nodeIndexToRelative(src_node),
12581 } },
12582 }),
12583 .if_non_error => |operand| switch (bt) {
12584 .ret => return gz.addAsIndex(.{
12585 .tag = .restore_err_ret_index_fn_entry,
12586 .data = .{ .un_node = .{
12587 .operand = operand,
12588 .src_node = gz.nodeIndexToRelative(src_node),
12589 } },
12590 }),
12591 .block => |block| return (try gz.addExtendedPayload(
12592 .restore_err_ret_index,
12593 Zir.Inst.RestoreErrRetIndex{
12594 .src_node = gz.nodeIndexToRelative(src_node),
12595 .block = block.toRef(),
12596 .operand = operand,
12597 },
12598 )).toIndex().?,
12599 },
12600 }
1258312601 }
1258412602
1258512603 fn addBreak(
src/Autodoc.zig+3-3
......@@ -1799,7 +1799,8 @@ fn walkInstruction(
17991799 };
18001800 },
18011801 .bool_br_and, .bool_br_or => {
1802 const bool_br = data[@intFromEnum(inst)].bool_br;
1802 const pl_node = data[@intFromEnum(inst)].pl_node;
1803 const extra = file.zir.extraData(Zir.Inst.BoolBr, pl_node.payload_index);
18031804
18041805 const bin_index = self.exprs.items.len;
18051806 try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
......@@ -1808,14 +1809,13 @@ fn walkInstruction(
18081809 file,
18091810 parent_scope,
18101811 parent_src,
1811 bool_br.lhs,
1812 extra.data.lhs,
18121813 false,
18131814 call_ctx,
18141815 );
18151816 const lhs_index = self.exprs.items.len;
18161817 try self.exprs.append(self.arena, lhs.expr);
18171818
1818 const extra = file.zir.extraData(Zir.Inst.Block, bool_br.payload_index);
18191819 const rhs = try self.walkInstruction(
18201820 file,
18211821 parent_scope,
src/Sema.zig+71-86
......@@ -50,11 +50,6 @@ branch_count: u32 = 0,
5050/// Populated when returning `error.ComptimeBreak`. Used to communicate the
5151/// break instruction up the stack to find the corresponding Block.
5252comptime_break_inst: Zir.Inst.Index = undefined,
53/// This field is updated when a new source location becomes active, so that
54/// instructions which do not have explicitly mapped source locations still have
55/// access to the source location set by the previous instruction which did
56/// contain a mapped source location.
57src: LazySrcLoc = .{ .token_offset = 0 },
5853decl_val_table: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Air.Inst.Ref) = .{},
5954/// When doing a generic function instantiation, this array collects a value
6055/// for each parameter of the generic owner. `none` for non-comptime parameters.
......@@ -1006,10 +1001,10 @@ fn analyzeBodyInner(
10061001 const air_inst: Air.Inst.Ref = switch (tags[@intFromEnum(inst)]) {
10071002 // zig fmt: off
10081003 .alloc => try sema.zirAlloc(block, inst),
1009 .alloc_inferred => try sema.zirAllocInferred(block, inst, true),
1010 .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, false),
1011 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(inst, true),
1012 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, false),
1004 .alloc_inferred => try sema.zirAllocInferred(block, true),
1005 .alloc_inferred_mut => try sema.zirAllocInferred(block, false),
1006 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(true),
1007 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(false),
10131008 .alloc_mut => try sema.zirAllocMut(block, inst),
10141009 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
10151010 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
......@@ -1308,6 +1303,11 @@ fn analyzeBodyInner(
13081303 i += 1;
13091304 continue;
13101305 },
1306 .restore_err_ret_index => {
1307 try sema.zirRestoreErrRetIndex(block, extended);
1308 i += 1;
1309 continue;
1310 },
13111311 .value_placeholder => unreachable, // never appears in a body
13121312 };
13131313 },
......@@ -1369,11 +1369,6 @@ fn analyzeBodyInner(
13691369 i += 1;
13701370 continue;
13711371 },
1372 .store => {
1373 try sema.zirStore(block, inst);
1374 i += 1;
1375 continue;
1376 },
13771372 .store_node => {
13781373 try sema.zirStoreNode(block, inst);
13791374 i += 1;
......@@ -1518,8 +1513,15 @@ fn analyzeBodyInner(
15181513 i += 1;
15191514 continue;
15201515 },
1521 .restore_err_ret_index => {
1522 try sema.zirRestoreErrRetIndex(block, inst);
1516 .restore_err_ret_index_unconditional => {
1517 const un_node = datas[@intFromEnum(inst)].un_node;
1518 try sema.restoreErrRetIndex(block, un_node.src(), un_node.operand, .none);
1519 i += 1;
1520 continue;
1521 },
1522 .restore_err_ret_index_fn_entry => {
1523 const un_node = datas[@intFromEnum(inst)].un_node;
1524 try sema.restoreErrRetIndex(block, un_node.src(), .none, un_node.operand);
15231525 i += 1;
15241526 continue;
15251527 },
......@@ -2779,7 +2781,7 @@ fn zirStructDecl(
27792781 const src: LazySrcLoc = if (small.has_src_node) blk: {
27802782 const node_offset: i32 = @bitCast(sema.code.extra[extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len]);
27812783 break :blk LazySrcLoc.nodeOffset(node_offset);
2782 } else sema.src;
2784 } else unreachable; // MLUGG TODO
27832785
27842786 // Because these three things each reference each other, `undefined`
27852787 // placeholders are used before being set after the struct type gains an
......@@ -2945,7 +2947,7 @@ fn zirEnumDecl(
29452947 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
29462948 extra_index += 1;
29472949 break :blk LazySrcLoc.nodeOffset(node_offset);
2948 } else sema.src;
2950 } else unreachable; // MLUGG TODO
29492951 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
29502952
29512953 const tag_type_ref = if (small.has_tag_type) blk: {
......@@ -3218,7 +3220,7 @@ fn zirUnionDecl(
32183220 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
32193221 extra_index += 1;
32203222 break :blk LazySrcLoc.nodeOffset(node_offset);
3221 } else sema.src;
3223 } else unreachable; // MLUGG TODO
32223224
32233225 extra_index += @intFromBool(small.has_tag_type);
32243226 extra_index += @intFromBool(small.has_body_len);
......@@ -3327,7 +3329,7 @@ fn zirOpaqueDecl(
33273329 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
33283330 extra_index += 1;
33293331 break :blk LazySrcLoc.nodeOffset(node_offset);
3330 } else sema.src;
3332 } else unreachable; // MLUGG TODO
33313333
33323334 const decls_len = if (small.has_decls_len) blk: {
33333335 const decls_len = sema.code.extra[extra_index];
......@@ -3977,13 +3979,9 @@ fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Ai
39773979
39783980fn zirAllocInferredComptime(
39793981 sema: *Sema,
3980 inst: Zir.Inst.Index,
39813982 is_const: bool,
39823983) CompileError!Air.Inst.Ref {
39833984 const gpa = sema.gpa;
3984 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
3985 const src = LazySrcLoc.nodeOffset(src_node);
3986 sema.src = src;
39873985
39883986 try sema.air_instructions.append(gpa, .{
39893987 .tag = .inferred_alloc_comptime,
......@@ -4042,16 +4040,12 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
40424040fn zirAllocInferred(
40434041 sema: *Sema,
40444042 block: *Block,
4045 inst: Zir.Inst.Index,
40464043 is_const: bool,
40474044) CompileError!Air.Inst.Ref {
40484045 const tracy = trace(@src());
40494046 defer tracy.end();
40504047
40514048 const gpa = sema.gpa;
4052 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
4053 const src = LazySrcLoc.nodeOffset(src_node);
4054 sema.src = src;
40554049
40564050 if (block.is_comptime) {
40574051 try sema.air_instructions.append(gpa, .{
......@@ -5428,10 +5422,11 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
54285422 const tracy = trace(@src());
54295423 defer tracy.end();
54305424
5431 const src: LazySrcLoc = sema.src;
5432 const bin_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;
5433 const ptr = try sema.resolveInst(bin_inst.lhs);
5434 const operand = try sema.resolveInst(bin_inst.rhs);
5425 const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
5426 const src = pl_node.src();
5427 const bin = sema.code.extraData(Zir.Inst.Bin, pl_node.payload_index).data;
5428 const ptr = try sema.resolveInst(bin.lhs);
5429 const operand = try sema.resolveInst(bin.rhs);
54355430 const ptr_inst = ptr.toIndex().?;
54365431 const air_datas = sema.air_instructions.items(.data);
54375432
......@@ -5496,16 +5491,6 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
54965491 sema.branch_quota = @max(sema.branch_quota, quota);
54975492}
54985493
5499fn zirStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
5500 const tracy = trace(@src());
5501 defer tracy.end();
5502
5503 const bin_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;
5504 const ptr = try sema.resolveInst(bin_inst.lhs);
5505 const value = try sema.resolveInst(bin_inst.rhs);
5506 return sema.storePtr(block, sema.src, ptr, value);
5507}
5508
55095494fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
55105495 const tracy = trace(@src());
55115496 defer tracy.end();
......@@ -5709,7 +5694,6 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.I
57095694fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
57105695 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
57115696 const src = LazySrcLoc.nodeOffset(src_node);
5712 sema.src = src;
57135697 if (block.is_comptime)
57145698 return sema.fail(block, src, "encountered @trap at comptime", .{});
57155699 _ = try block.addNoOp(.trap);
......@@ -6384,10 +6368,6 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
63846368}
63856369
63866370fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6387 // We do not set sema.src here because dbg_stmt instructions are only emitted for
6388 // ZIR code that possibly will need to generate runtime code. So error messages
6389 // and other source locations must not rely on sema.src being set from dbg_stmt
6390 // instructions.
63916371 if (block.is_comptime or block.ownerModule().strip) return;
63926372
63936373 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
......@@ -6632,7 +6612,6 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
66326612pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref {
66336613 const mod = sema.mod;
66346614 const gpa = sema.gpa;
6635 const src = sema.src;
66366615
66376616 if (block.is_comptime or block.is_typeof) {
66386617 const index_val = try mod.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
......@@ -6650,9 +6629,10 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
66506629 else => |e| return e,
66516630 };
66526631 const field_name = try mod.intern_pool.getOrPutString(gpa, "index");
6653 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, src) catch |err| switch (err) {
6654 error.NeededSourceLocation, error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
6655 else => |e| return e,
6632 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, .unneeded) catch |err| switch (err) {
6633 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.StackTrace is corrupt"),
6634 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
6635 error.OutOfMemory => |e| return e,
66566636 };
66576637
66586638 return try block.addInst(.{
......@@ -9900,7 +9880,6 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
99009880 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
99019881 const src = inst_data.src();
99029882 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
9903 sema.src = src;
99049883 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);
99059884}
99069885
......@@ -10855,7 +10834,7 @@ const SwitchProngAnalysis = struct {
1085510834 .address_space = operand_ptr_ty.ptrAddressSpace(mod),
1085610835 },
1085710836 });
10858 if (try sema.resolveDefinedValue(block, sema.src, spa.operand_ptr)) |union_ptr| {
10837 if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |union_ptr| {
1085910838 return Air.internedToRef((try mod.intern(.{ .ptr = .{
1086010839 .ty = ptr_field_ty.toIntern(),
1086110840 .addr = .{ .field = .{
......@@ -10866,7 +10845,7 @@ const SwitchProngAnalysis = struct {
1086610845 }
1086710846 return block.addStructFieldPtr(spa.operand_ptr, field_index, ptr_field_ty);
1086810847 } else {
10869 if (try sema.resolveDefinedValue(block, sema.src, spa.operand)) |union_val| {
10848 if (try sema.resolveDefinedValue(block, operand_src, spa.operand)) |union_val| {
1087010849 const tag_and_val = ip.indexToKey(union_val.toIntern()).un;
1087110850 return Air.internedToRef(tag_and_val.val);
1087210851 }
......@@ -13191,6 +13170,7 @@ fn validateErrSetSwitch(
1319113170 // else => |e| return e,
1319213171 // even if all the possible errors were already handled.
1319313172 const tags = sema.code.instructions.items(.tag);
13173 const datas = sema.code.instructions.items(.data);
1319413174 for (else_case.body) |else_inst| switch (tags[@intFromEnum(else_inst)]) {
1319513175 .dbg_block_begin,
1319613176 .dbg_block_end,
......@@ -13205,11 +13185,16 @@ fn validateErrSetSwitch(
1320513185 .err_union_code,
1320613186 .ret_err_value_code,
1320713187 .save_err_ret_index,
13208 .restore_err_ret_index,
13188 .restore_err_ret_index_unconditional,
13189 .restore_err_ret_index_fn_entry,
1320913190 .is_non_err,
1321013191 .ret_is_non_err,
1321113192 .condbr,
1321213193 => {},
13194 .extended => switch (datas[@intFromEnum(else_inst)].extended.opcode) {
13195 .restore_err_ret_index => {},
13196 else => break,
13197 },
1321313198 else => break,
1321413199 } else break :else_validation;
1321513200
......@@ -13707,7 +13692,6 @@ fn zirShl(
1370713692 const mod = sema.mod;
1370813693 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1370913694 const src = inst_data.src();
13710 sema.src = src;
1371113695 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1371213696 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1371313697 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -13878,7 +13862,6 @@ fn zirShr(
1387813862 const mod = sema.mod;
1387913863 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1388013864 const src = inst_data.src();
13881 sema.src = src;
1388213865 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1388313866 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1388413867 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14014,7 +13997,6 @@ fn zirBitwise(
1401413997 const mod = sema.mod;
1401513998 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1401613999 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14017 sema.src = src;
1401814000 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1401914001 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1402014002 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14795,21 +14777,20 @@ fn zirArithmetic(
1479514777 defer tracy.end();
1479614778
1479714779 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14798 sema.src = .{ .node_offset_bin_op = inst_data.src_node };
14780 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
1479914781 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1480014782 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1480114783 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1480214784 const lhs = try sema.resolveInst(extra.lhs);
1480314785 const rhs = try sema.resolveInst(extra.rhs);
1480414786
14805 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, sema.src, lhs_src, rhs_src, safety);
14787 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, src, lhs_src, rhs_src, safety);
1480614788}
1480714789
1480814790fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1480914791 const mod = sema.mod;
1481014792 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1481114793 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14812 sema.src = src;
1481314794 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1481414795 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1481514796 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14975,7 +14956,6 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1497514956 const mod = sema.mod;
1497614957 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1497714958 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14978 sema.src = src;
1497914959 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1498014960 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1498114961 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15141,7 +15121,6 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1514115121 const mod = sema.mod;
1514215122 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1514315123 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15144 sema.src = src;
1514515124 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1514615125 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1514715126 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15252,7 +15231,6 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1525215231 const mod = sema.mod;
1525315232 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1525415233 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15255 sema.src = src;
1525615234 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1525715235 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1525815236 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15494,7 +15472,6 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1549415472 const mod = sema.mod;
1549515473 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1549615474 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15497 sema.src = src;
1549815475 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1549915476 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1550015477 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15679,7 +15656,6 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1567915656 const mod = sema.mod;
1568015657 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1568115658 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15682 sema.src = src;
1568315659 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1568415660 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1568515661 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15775,7 +15751,6 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1577515751 const mod = sema.mod;
1577615752 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1577715753 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15778 sema.src = src;
1577915754 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1578015755 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1578115756 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -18741,13 +18716,16 @@ fn zirBoolBr(
1874118716 defer tracy.end();
1874218717
1874318718 const mod = sema.mod;
18719 const gpa = sema.gpa;
18720
1874418721 const datas = sema.code.instructions.items(.data);
18745 const inst_data = datas[@intFromEnum(inst)].bool_br;
18746 const lhs = try sema.resolveInst(inst_data.lhs);
18747 const lhs_src = sema.src;
18748 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
18722 const inst_data = datas[@intFromEnum(inst)].pl_node;
18723 const extra = sema.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
18724
18725 const lhs = try sema.resolveInst(extra.data.lhs);
1874918726 const body = sema.code.bodySlice(extra.end, extra.data.body_len);
18750 const gpa = sema.gpa;
18727 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
18728 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1875118729
1875218730 if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| {
1875318731 if (is_bool_or and lhs_val.toBool()) {
......@@ -18795,7 +18773,7 @@ fn zirBoolBr(
1879518773
1879618774 const result = sema.finishCondBr(parent_block, &child_block, &then_block, &else_block, lhs, block_inst);
1879718775 if (!sema.typeOf(rhs_result).isNoReturn(mod)) {
18798 if (try sema.resolveDefinedValue(rhs_block, sema.src, rhs_result)) |rhs_val| {
18776 if (try sema.resolveDefinedValue(rhs_block, rhs_src, rhs_result)) |rhs_val| {
1879918777 if (is_bool_or and rhs_val.toBool()) {
1880018778 return .bool_true;
1880118779 } else if (!is_bool_or and !rhs_val.toBool()) {
......@@ -19375,17 +19353,21 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1937519353 block.error_return_trace_index = try sema.analyzeSaveErrRetIndex(block);
1937619354}
1937719355
19378fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {
19379 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;
19380 const src = sema.src; // TODO
19381
19382 const mod = sema.mod;
19383 const ip = &mod.intern_pool;
19356fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
19357 const extra = sema.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
19358 return sema.restoreErrRetIndex(start_block, extra.src(), extra.block, extra.operand);
19359}
1938419360
19361/// If `operand` is non-error (or is `none`), restores the error return trace to
19362/// its state at the point `block` was reached (or, if `block` is `none`, the
19363/// point this function began execution).
19364fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_block: Zir.Inst.Ref, operand_zir: Zir.Inst.Ref) CompileError!void {
1938519365 const tracy = trace(@src());
1938619366 defer tracy.end();
1938719367
19388 const saved_index = if (inst_data.block.toIndexAllowNone()) |zir_block| b: {
19368 const mod = sema.mod;
19369
19370 const saved_index = if (target_block.toIndexAllowNone()) |zir_block| b: {
1938919371 var block = start_block;
1939019372 while (true) {
1939119373 if (block.label) |label| {
......@@ -19409,7 +19391,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1940919391 return; // No need to restore
1941019392 };
1941119393
19412 const operand = try sema.resolveInstAllowNone(inst_data.operand);
19394 const operand = try sema.resolveInstAllowNone(operand_zir);
1941319395
1941419396 if (start_block.is_comptime or start_block.is_typeof) {
1941519397 const is_non_error = if (operand != .none) blk: {
......@@ -19427,7 +19409,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1942719409 return;
1942819410 }
1942919411
19430 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
19412 if (!mod.intern_pool.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
1943119413 if (!start_block.ownerModule().error_tracing) return;
1943219414
1943319415 assert(saved_index != .none); // The .error_return_trace_index field was dropped somewhere
......@@ -23161,7 +23143,6 @@ fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
2316123143fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 {
2316223144 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2316323145 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
23164 sema.src = src;
2316523146 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
2316623147 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
2316723148 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -26416,12 +26397,16 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
2641626397 try sema.prepareSimplePanic(block);
2641726398
2641826399 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
26419 const msg_decl_index = (try sema.namespaceLookup(
26400 const msg_decl_index = (sema.namespaceLookup(
2642026401 block,
26421 sema.src,
26402 .unneeded,
2642226403 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
2642326404 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
26424 )).?;
26405 ) catch |err| switch (err) {
26406 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
26407 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
26408 error.OutOfMemory => |e| return e,
26409 }).?;
2642526410 try sema.ensureDeclAnalyzed(msg_decl_index);
2642626411 mod.panic_messages[@intFromEnum(panic_id)] = msg_decl_index.toOptional();
2642726412 return msg_decl_index;
src/Zir.zig+52-33
......@@ -303,11 +303,11 @@ pub const Inst = struct {
303303 bool_not,
304304 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
305305 /// is a block, which is evaluated if `lhs` is `true`.
306 /// Uses the `bool_br` union field.
306 /// Uses the `pl_node` union field. Payload is `BoolBr`.
307307 bool_br_and,
308308 /// Short-circuiting boolean `or`. `lhs` is a boolean `Ref` and the other operand
309309 /// is a block, which is evaluated if `lhs` is `false`.
310 /// Uses the `bool_br` union field.
310 /// Uses the `pl_node` union field. Payload is `BoolBr`.
311311 bool_br_or,
312312 /// Return a value from a block.
313313 /// Uses the `break` union field.
......@@ -592,16 +592,12 @@ pub const Inst = struct {
592592 /// Returns a pointer to the subslice.
593593 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`.
594594 slice_length,
595 /// Write a value to a pointer. For loading, see `load`.
596 /// Source location is assumed to be same as previous instruction.
597 /// Uses the `bin` union field.
598 store,
599595 /// Same as `store` except provides a source location.
600596 /// Uses the `pl_node` union field. Payload is `Bin`.
601597 store_node,
602 /// Same as `store` but the type of the value being stored will be used to infer
603 /// the pointer type.
604 /// Uses the `bin` union field.
598 /// Same as `store_node` but the type of the value being stored will be
599 /// used to infer the pointer type of an `alloc_inferred`.
600 /// Uses the `pl_node` union field. Payload is `Bin`.
605601 store_to_inferred_ptr,
606602 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
607603 /// Uses the `str` union field.
......@@ -1036,10 +1032,18 @@ pub const Inst = struct {
10361032 /// block, if the operand is .none or of an error/error-union type.
10371033 /// Uses the `save_err_ret_index` field.
10381034 save_err_ret_index,
1039 /// Sets error return trace to zero if no operand is given,
1040 /// otherwise sets the value to the given amount.
1041 /// Uses the `restore_err_ret_index` union field.
1042 restore_err_ret_index,
1035 /// Specialized form of `Extended.restore_err_ret_index`.
1036 /// Unconditionally restores the error return index to its last saved state
1037 /// in the block referred to by `operand`. If `operand` is `none`, restores
1038 /// to the point of function entry.
1039 /// Uses the `un_node` field.
1040 restore_err_ret_index_unconditional,
1041 /// Specialized form of `Extended.restore_err_ret_index`.
1042 /// Restores the error return index to its state at the entry of
1043 /// the current function conditional on `operand` being a non-error.
1044 /// If `operand` is `none`, restores unconditionally.
1045 /// Uses the `un_node` field.
1046 restore_err_ret_index_fn_entry,
10431047
10441048 /// The ZIR instruction tag is one of the `Extended` ones.
10451049 /// Uses the `extended` union field.
......@@ -1145,7 +1149,6 @@ pub const Inst = struct {
11451149 .shl,
11461150 .shl_sat,
11471151 .shr,
1148 .store,
11491152 .store_node,
11501153 .store_to_inferred_ptr,
11511154 .str,
......@@ -1265,7 +1268,6 @@ pub const Inst = struct {
12651268 .@"defer",
12661269 .defer_err_code,
12671270 .save_err_ret_index,
1268 .restore_err_ret_index,
12691271 .for_len,
12701272 .opt_eu_base_ptr_init,
12711273 .coerce_ptr_elem_ty,
......@@ -1290,6 +1292,8 @@ pub const Inst = struct {
12901292 .array_init_elem_type,
12911293 .array_init_elem_ptr,
12921294 .validate_ref_ty,
1295 .restore_err_ret_index_unconditional,
1296 .restore_err_ret_index_fn_entry,
12931297 => false,
12941298
12951299 .@"break",
......@@ -1338,7 +1342,6 @@ pub const Inst = struct {
13381342 .ensure_err_union_payload_void,
13391343 .set_eval_branch_quota,
13401344 .atomic_store,
1341 .store,
13421345 .store_node,
13431346 .store_to_inferred_ptr,
13441347 .resolve_inferred_alloc,
......@@ -1352,8 +1355,9 @@ pub const Inst = struct {
13521355 .check_comptime_control_flow,
13531356 .@"defer",
13541357 .defer_err_code,
1355 .restore_err_ret_index,
13561358 .save_err_ret_index,
1359 .restore_err_ret_index_unconditional,
1360 .restore_err_ret_index_fn_entry,
13571361 .validate_struct_init_ty,
13581362 .validate_struct_init_result_ty,
13591363 .validate_ptr_struct_init,
......@@ -1635,8 +1639,8 @@ pub const Inst = struct {
16351639 .declaration = .pl_node,
16361640 .suspend_block = .pl_node,
16371641 .bool_not = .un_node,
1638 .bool_br_and = .bool_br,
1639 .bool_br_or = .bool_br,
1642 .bool_br_and = .pl_node,
1643 .bool_br_or = .pl_node,
16401644 .@"break" = .@"break",
16411645 .break_inline = .@"break",
16421646 .check_comptime_control_flow = .un_node,
......@@ -1713,9 +1717,8 @@ pub const Inst = struct {
17131717 .slice_end = .pl_node,
17141718 .slice_sentinel = .pl_node,
17151719 .slice_length = .pl_node,
1716 .store = .bin,
17171720 .store_node = .pl_node,
1718 .store_to_inferred_ptr = .bin,
1721 .store_to_inferred_ptr = .pl_node,
17191722 .str = .str,
17201723 .negate = .un_node,
17211724 .negate_wrap = .un_node,
......@@ -1845,7 +1848,8 @@ pub const Inst = struct {
18451848 .defer_err_code = .defer_err_code,
18461849
18471850 .save_err_ret_index = .save_err_ret_index,
1848 .restore_err_ret_index = .restore_err_ret_index,
1851 .restore_err_ret_index_unconditional = .un_node,
1852 .restore_err_ret_index_fn_entry = .un_node,
18491853
18501854 .struct_init_empty = .un_node,
18511855 .struct_init_empty_result = .un_node,
......@@ -2075,6 +2079,13 @@ pub const Inst = struct {
20752079 /// Implements the `@inComptime` builtin.
20762080 /// `operand` is `src_node: i32`.
20772081 in_comptime,
2082 /// Restores the error return index to its last saved state in a given
2083 /// block. If the block is `.none`, restores to the state from the point
2084 /// of function entry. If the operand is not `.none`, the restore is
2085 /// conditional on the operand value not being an error.
2086 /// `operand` is payload index to `RestoreErrRetIndex`.
2087 /// `small` is undefined.
2088 restore_err_ret_index,
20782089 /// Used as a placeholder instruction which is just a dummy index for Sema to replace
20792090 /// with a specific value. For instance, this is used for the capture of an `errdefer`.
20802091 /// This should never appear in a body.
......@@ -2345,11 +2356,6 @@ pub const Inst = struct {
23452356 return LazySrcLoc.nodeOffset(self.src_node);
23462357 }
23472358 },
2348 bool_br: struct {
2349 lhs: Ref,
2350 /// Points to a `Block`.
2351 payload_index: u32,
2352 },
23532359 @"unreachable": struct {
23542360 /// Offset from Decl AST node index.
23552361 /// `Tag` determines which kind of AST node this points to.
......@@ -2396,10 +2402,6 @@ pub const Inst = struct {
23962402 save_err_ret_index: struct {
23972403 operand: Ref, // If error type (or .none), save new trace index
23982404 },
2399 restore_err_ret_index: struct {
2400 block: Ref, // If restored, the index is from this block's entrypoint
2401 operand: Ref, // If non-error (or .none), then restore the index
2402 },
24032405 elem_val_imm: struct {
24042406 /// The indexable value being accessed.
24052407 operand: Ref,
......@@ -2435,7 +2437,6 @@ pub const Inst = struct {
24352437 float,
24362438 ptr_type,
24372439 int_type,
2438 bool_br,
24392440 @"unreachable",
24402441 @"break",
24412442 dbg_stmt,
......@@ -2444,7 +2445,6 @@ pub const Inst = struct {
24442445 @"defer",
24452446 defer_err_code,
24462447 save_err_ret_index,
2447 restore_err_ret_index,
24482448 elem_val_imm,
24492449 };
24502450 };
......@@ -2630,6 +2630,13 @@ pub const Inst = struct {
26302630 body_len: u32,
26312631 };
26322632
2633 /// Trailing:
2634 /// * inst: Index // for each `body_len`
2635 pub const BoolBr = struct {
2636 lhs: Ref,
2637 body_len: u32,
2638 };
2639
26332640 /// Trailing:
26342641 /// 0. doc_comment: u32 // if `has_doc_comment`; null-terminated string index
26352642 /// 1. align_body_len: u32 // if `has_align_linksection_addrspace`; 0 means no `align`
......@@ -3439,6 +3446,18 @@ pub const Inst = struct {
34393446 /// The RHS of the array multiplication.
34403447 rhs: Ref,
34413448 };
3449
3450 pub const RestoreErrRetIndex = struct {
3451 src_node: i32,
3452 /// If `.none`, restore the trace to its state upon function entry.
3453 block: Ref,
3454 /// If `.none`, restore unconditionally.
3455 operand: Ref,
3456
3457 pub fn src(self: RestoreErrRetIndex) LazySrcLoc {
3458 return LazySrcLoc.nodeOffset(self.src_node);
3459 }
3460 };
34423461};
34433462
34443463pub const SpecialProng = enum { none, @"else", under };
src/print_zir.zig+16-21
......@@ -199,10 +199,6 @@ const Writer = struct {
199199 const tag = tags[@intFromEnum(inst)];
200200 try stream.print("= {s}(", .{@tagName(tags[@intFromEnum(inst)])});
201201 switch (tag) {
202 .store,
203 .store_to_inferred_ptr,
204 => try self.writeBin(stream, inst),
205
206202 .alloc,
207203 .alloc_mut,
208204 .alloc_comptime_mut,
......@@ -280,6 +276,8 @@ const Writer = struct {
280276 .validate_deref,
281277 .check_comptime_control_flow,
282278 .opt_eu_base_ptr_init,
279 .restore_err_ret_index_unconditional,
280 .restore_err_ret_index_fn_entry,
283281 => try self.writeUnNode(stream, inst),
284282
285283 .ref,
......@@ -303,7 +301,6 @@ const Writer = struct {
303301 .int_type => try self.writeIntType(stream, inst),
304302
305303 .save_err_ret_index => try self.writeSaveErrRetIndex(stream, inst),
306 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, inst),
307304
308305 .@"break",
309306 .break_inline,
......@@ -392,6 +389,7 @@ const Writer = struct {
392389 .shr_exact,
393390 .xor,
394391 .store_node,
392 .store_to_inferred_ptr,
395393 .error_union_type,
396394 .merge_error_sets,
397395 .bit_and,
......@@ -615,6 +613,8 @@ const Writer = struct {
615613 .cmpxchg => try self.writeCmpxchg(stream, extended),
616614 .ptr_cast_full => try self.writePtrCastFull(stream, extended),
617615 .ptr_cast_no_dest => try self.writePtrCastNoDest(stream, extended),
616
617 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, extended),
618618 }
619619 }
620620
......@@ -624,14 +624,6 @@ const Writer = struct {
624624 try self.writeSrc(stream, src);
625625 }
626626
627 fn writeBin(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
628 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;
629 try self.writeInstRef(stream, inst_data.lhs);
630 try stream.writeAll(", ");
631 try self.writeInstRef(stream, inst_data.rhs);
632 try stream.writeByte(')');
633 }
634
635627 fn writeArrayInitElemType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
636628 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;
637629 try self.writeInstRef(stream, inst_data.lhs);
......@@ -2505,12 +2497,14 @@ const Writer = struct {
25052497 }
25062498
25072499 fn writeBoolBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2508 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bool_br;
2509 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
2500 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2501 const extra = self.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
25102502 const body = self.code.bodySlice(extra.end, extra.data.body_len);
2511 try self.writeInstRef(stream, inst_data.lhs);
2503 try self.writeInstRef(stream, extra.data.lhs);
25122504 try stream.writeAll(", ");
25132505 try self.writeBracedBody(stream, body);
2506 try stream.writeAll(") ");
2507 try self.writeSrc(stream, inst_data.src());
25142508 }
25152509
25162510 fn writeIntType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
......@@ -2531,13 +2525,14 @@ const Writer = struct {
25312525 try stream.writeAll(")");
25322526 }
25332527
2534 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2535 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;
2528 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
2529 const extra = self.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
25362530
2537 try self.writeInstRef(stream, inst_data.block);
2538 try self.writeInstRef(stream, inst_data.operand);
2531 try self.writeInstRef(stream, extra.block);
2532 try self.writeInstRef(stream, extra.operand);
25392533
2540 try stream.writeAll(")");
2534 try stream.writeAll(") ");
2535 try self.writeSrc(stream, extra.src());
25412536 }
25422537
25432538 fn writeBreak(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {