authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-16 19:09:57+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-16 19:09:57+00:00
log88d4b5cb18bdc68099162f1db8103d614025659c
tree4a60f9dd5ceb0a67d9fc43a4e12f6850f314837d
parent51e96a823ce21a6b027bfdf55d94c3872a103703
parentb2f28a104deb96eead604aa4a1a5a703689e2b61
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18909 from mlugg/random-enhancements

A loose collection of random enhancements: * Eliminate `Sema.src` field * Optimize size of ZIR * Slightly improve some compile errors

7 files changed, 534 insertions(+), 505 deletions(-)

src/AstGen.zig+218-179
...@@ -44,6 +44,9 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},...@@ -44,6 +44,9 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
44/// The topmost block of the current function.44/// The topmost block of the current function.
45fn_block: ?*GenZir = null,45fn_block: ?*GenZir = null,
46fn_var_args: bool = false,46fn_var_args: bool = false,
47/// The return type of the current function. This may be a trivial `Ref`, or
48/// otherwise it refers to a `ret_type` instruction.
49fn_ret_ty: Zir.Inst.Ref = .none,
47/// Maps string table indexes to the first `@import` ZIR instruction50/// Maps string table indexes to the first `@import` ZIR instruction
48/// that uses this string as the operand.51/// that uses this string as the operand.
49imports: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, Ast.TokenIndex) = .{},52imports: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, Ast.TokenIndex) = .{},
...@@ -357,16 +360,11 @@ const ResultInfo = struct {...@@ -357,16 +360,11 @@ const ResultInfo = struct {
357 };360 };
358};361};
359362
360/// TODO: modify Sema to remove in favour of `coerced_align_ri`
361const align_ri: ResultInfo = .{ .rl = .{ .ty = .u29_type } };
362const coerced_align_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .u29_type } };363const coerced_align_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .u29_type } };
363/// TODO: modify Sema to remove in favour of `coerced_addrspace_ri`
364const addrspace_ri: ResultInfo = .{ .rl = .{ .ty = .address_space_type } };
365const coerced_addrspace_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .address_space_type } };364const coerced_addrspace_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .address_space_type } };
366const coerced_linksection_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .slice_const_u8_type } };365const coerced_linksection_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .slice_const_u8_type } };
367const bool_ri: ResultInfo = .{ .rl = .{ .ty = .bool_type } };
368const type_ri: ResultInfo = .{ .rl = .{ .ty = .type_type } };
369const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };366const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };
367const coerced_bool_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .bool_type } };
370368
371fn typeExpr(gz: *GenZir, scope: *Scope, type_node: Ast.Node.Index) InnerError!Zir.Inst.Ref {369fn typeExpr(gz: *GenZir, scope: *Scope, type_node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
372 return comptimeExpr(gz, scope, coerced_type_ri, type_node);370 return comptimeExpr(gz, scope, coerced_type_ri, type_node);
...@@ -783,7 +781,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -783,7 +781,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
783 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),781 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),
784 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),782 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),
785783
786 .bool_not => return simpleUnOp(gz, scope, ri, node, bool_ri, node_datas[node].lhs, .bool_not),784 .bool_not => return simpleUnOp(gz, scope, ri, node, coerced_bool_ri, node_datas[node].lhs, .bool_not),
787 .bit_not => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, node_datas[node].lhs, .bit_not),785 .bit_not => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, node_datas[node].lhs, .bit_not),
788786
789 .negation => return negation(gz, scope, ri, node),787 .negation => return negation(gz, scope, ri, node),
...@@ -1369,7 +1367,7 @@ fn fnProtoExpr(...@@ -1369,7 +1367,7 @@ fn fnProtoExpr(
1369 };1367 };
13701368
1371 const align_ref: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: {1369 const align_ref: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: {
1372 break :inst try expr(&block_scope, scope, align_ri, fn_proto.ast.align_expr);1370 break :inst try expr(&block_scope, scope, coerced_align_ri, fn_proto.ast.align_expr);
1373 };1371 };
13741372
1375 if (fn_proto.ast.addrspace_expr != 0) {1373 if (fn_proto.ast.addrspace_expr != 0) {
...@@ -1384,7 +1382,7 @@ fn fnProtoExpr(...@@ -1384,7 +1382,7 @@ fn fnProtoExpr(
1384 try expr(1382 try expr(
1385 &block_scope,1383 &block_scope,
1386 scope,1384 scope,
1387 .{ .rl = .{ .ty = .calling_convention_type } },1385 .{ .rl = .{ .coerced_ty = .calling_convention_type } },
1388 fn_proto.ast.callconv_expr,1386 fn_proto.ast.callconv_expr,
1389 )1387 )
1390 else1388 else
...@@ -2119,7 +2117,7 @@ fn restoreErrRetIndex(...@@ -2119,7 +2117,7 @@ fn restoreErrRetIndex(
2119 else => .none, // always restore/pop2117 else => .none, // always restore/pop
2120 },2118 },
2121 };2119 };
2122 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op });2120 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op }, node);
2123}2121}
21242122
2125fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref {2123fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
...@@ -2176,7 +2174,7 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn...@@ -2176,7 +2174,7 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
21762174
2177 // As our last action before the break, "pop" the error trace if needed2175 // As our last action before the break, "pop" the error trace if needed
2178 if (!block_gz.is_comptime)2176 if (!block_gz.is_comptime)
2179 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);2177 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, node);
21802178
2181 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);2179 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
2182 return Zir.Inst.Ref.unreachable_value;2180 return Zir.Inst.Ref.unreachable_value;
...@@ -2268,7 +2266,7 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)...@@ -2268,7 +2266,7 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
22682266
2269 // As our last action before the continue, "pop" the error trace if needed2267 // As our last action before the continue, "pop" the error trace if needed
2270 if (!gen_zir.is_comptime)2268 if (!gen_zir.is_comptime)
2271 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always);2269 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always, node);
22722270
2273 _ = try parent_gz.addBreak(break_tag, continue_block, .void_value);2271 _ = try parent_gz.addBreak(break_tag, continue_block, .void_value);
2274 return Zir.Inst.Ref.unreachable_value;2272 return Zir.Inst.Ref.unreachable_value;
...@@ -2328,7 +2326,7 @@ fn blockExpr(...@@ -2328,7 +2326,7 @@ fn blockExpr(
23282326
2329 if (!block_scope.endsWithNoReturn()) {2327 if (!block_scope.endsWithNoReturn()) {
2330 // As our last action before the break, "pop" the error trace if needed2328 // As our last action before the break, "pop" the error trace if needed
2331 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);2329 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
2332 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);2330 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
2333 }2331 }
23342332
...@@ -2423,7 +2421,7 @@ fn labeledBlockExpr(...@@ -2423,7 +2421,7 @@ fn labeledBlockExpr(
2423 try blockExprStmts(&block_scope, &block_scope.base, statements);2421 try blockExprStmts(&block_scope, &block_scope.base, statements);
2424 if (!block_scope.endsWithNoReturn()) {2422 if (!block_scope.endsWithNoReturn()) {
2425 // As our last action before the return, "pop" the error trace if needed2423 // As our last action before the return, "pop" the error trace if needed
2426 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);2424 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
2427 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);2425 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
2428 }2426 }
24292427
...@@ -2815,7 +2813,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As...@@ -2815,7 +2813,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
2815 .export_value,2813 .export_value,
2816 .set_eval_branch_quota,2814 .set_eval_branch_quota,
2817 .atomic_store,2815 .atomic_store,
2818 .store,
2819 .store_node,2816 .store_node,
2820 .store_to_inferred_ptr,2817 .store_to_inferred_ptr,
2821 .resolve_inferred_alloc,2818 .resolve_inferred_alloc,
...@@ -2826,7 +2823,8 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As...@@ -2826,7 +2823,8 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
2826 .validate_deref,2823 .validate_deref,
2827 .validate_destructure,2824 .validate_destructure,
2828 .save_err_ret_index,2825 .save_err_ret_index,
2829 .restore_err_ret_index,2826 .restore_err_ret_index_unconditional,
2827 .restore_err_ret_index_fn_entry,
2830 .validate_struct_init_ty,2828 .validate_struct_init_ty,
2831 .validate_struct_init_result_ty,2829 .validate_struct_init_result_ty,
2832 .validate_ptr_struct_init,2830 .validate_ptr_struct_init,
...@@ -3133,7 +3131,7 @@ fn varDecl(...@@ -3133,7 +3131,7 @@ fn varDecl(
3133 }3131 }
31343132
3135 const align_inst: Zir.Inst.Ref = if (var_decl.ast.align_node != 0)3133 const align_inst: Zir.Inst.Ref = if (var_decl.ast.align_node != 0)
3136 try expr(gz, scope, align_ri, var_decl.ast.align_node)3134 try expr(gz, scope, coerced_align_ri, var_decl.ast.align_node)
3137 else3135 else
3138 .none;3136 .none;
31393137
...@@ -3329,31 +3327,11 @@ fn emitDbgNode(gz: *GenZir, node: Ast.Node.Index) !void {...@@ -3329,31 +3327,11 @@ fn emitDbgNode(gz: *GenZir, node: Ast.Node.Index) !void {
3329 // If the current block will be evaluated only during semantic analysis3327 // If the current block will be evaluated only during semantic analysis
3330 // then no dbg_stmt ZIR instruction is needed.3328 // then no dbg_stmt ZIR instruction is needed.
3331 if (gz.is_comptime) return;3329 if (gz.is_comptime) return;
3332
3333 const astgen = gz.astgen;3330 const astgen = gz.astgen;
3334 astgen.advanceSourceCursorToNode(node);3331 astgen.advanceSourceCursorToNode(node);
3335 const line = astgen.source_line - gz.decl_line;3332 const line = astgen.source_line - gz.decl_line;
3336 const column = astgen.source_column;3333 const column = astgen.source_column;
33373334 try emitDbgStmt(gz, .{ line, column });
3338 if (gz.instructions.items.len > 0) {
3339 const last = gz.instructions.items[gz.instructions.items.len - 1];
3340 const zir_tags = astgen.instructions.items(.tag);
3341 if (zir_tags[@intFromEnum(last)] == .dbg_stmt) {
3342 const zir_datas = astgen.instructions.items(.data);
3343 zir_datas[@intFromEnum(last)].dbg_stmt = .{
3344 .line = line,
3345 .column = column,
3346 };
3347 return;
3348 }
3349 }
3350
3351 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
3352 .dbg_stmt = .{
3353 .line = line,
3354 .column = column,
3355 },
3356 } });
3357}3335}
33583336
3359fn assign(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {3337fn assign(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {
...@@ -3522,7 +3500,7 @@ fn assignDestructureMaybeDecls(...@@ -3522,7 +3500,7 @@ fn assignDestructureMaybeDecls(
3522 const this_lhs_comptime = is_comptime or (is_const and rhs_is_comptime);3500 const this_lhs_comptime = is_comptime or (is_const and rhs_is_comptime);
35233501
3524 const align_inst: Zir.Inst.Ref = if (full.ast.align_node != 0)3502 const align_inst: Zir.Inst.Ref = if (full.ast.align_node != 0)
3525 try expr(gz, scope, align_ri, full.ast.align_node)3503 try expr(gz, scope, coerced_align_ri, full.ast.align_node)
3526 else3504 else
3527 .none;3505 .none;
35283506
...@@ -3709,7 +3687,10 @@ fn assignOp(...@@ -3709,7 +3687,10 @@ fn assignOp(
3709 .lhs = lhs,3687 .lhs = lhs,
3710 .rhs = rhs,3688 .rhs = rhs,
3711 });3689 });
3712 _ = try gz.addBin(.store, lhs_ptr, result);3690 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3691 .lhs = lhs_ptr,
3692 .rhs = result,
3693 });
3713}3694}
37143695
3715fn assignShift(3696fn assignShift(
...@@ -3732,7 +3713,10 @@ fn assignShift(...@@ -3732,7 +3713,10 @@ fn assignShift(
3732 .lhs = lhs,3713 .lhs = lhs,
3733 .rhs = rhs,3714 .rhs = rhs,
3734 });3715 });
3735 _ = try gz.addBin(.store, lhs_ptr, result);3716 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3717 .lhs = lhs_ptr,
3718 .rhs = result,
3719 });
3736}3720}
37373721
3738fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {3722fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {
...@@ -3750,7 +3734,10 @@ fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerE...@@ -3750,7 +3734,10 @@ fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerE
3750 .lhs = lhs,3734 .lhs = lhs,
3751 .rhs = rhs,3735 .rhs = rhs,
3752 });3736 });
3753 _ = try gz.addBin(.store, lhs_ptr, result);3737 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3738 .lhs = lhs_ptr,
3739 .rhs = result,
3740 });
3754}3741}
37553742
3756fn ptrType(3743fn ptrType(
...@@ -3791,7 +3778,7 @@ fn ptrType(...@@ -3791,7 +3778,7 @@ fn ptrType(
3791 gz.astgen.source_line = source_line;3778 gz.astgen.source_line = source_line;
3792 gz.astgen.source_column = source_column;3779 gz.astgen.source_column = source_column;
37933780
3794 addrspace_ref = try expr(gz, scope, addrspace_ri, ptr_info.ast.addrspace_node);3781 addrspace_ref = try expr(gz, scope, coerced_addrspace_ri, ptr_info.ast.addrspace_node);
3795 trailing_count += 1;3782 trailing_count += 1;
3796 }3783 }
3797 if (ptr_info.ast.align_node != 0) {3784 if (ptr_info.ast.align_node != 0) {
...@@ -4184,7 +4171,7 @@ fn fnDecl(...@@ -4184,7 +4171,7 @@ fn fnDecl(
4184 var addrspace_gz = decl_gz.makeSubBlock(params_scope);4171 var addrspace_gz = decl_gz.makeSubBlock(params_scope);
4185 defer addrspace_gz.unstack();4172 defer addrspace_gz.unstack();
4186 const addrspace_ref: Zir.Inst.Ref = if (fn_proto.ast.addrspace_expr == 0) .none else inst: {4173 const addrspace_ref: Zir.Inst.Ref = if (fn_proto.ast.addrspace_expr == 0) .none else inst: {
4187 const inst = try expr(&decl_gz, params_scope, addrspace_ri, fn_proto.ast.addrspace_expr);4174 const inst = try expr(&decl_gz, params_scope, coerced_addrspace_ri, fn_proto.ast.addrspace_expr);
4188 if (addrspace_gz.instructionsSlice().len == 0) {4175 if (addrspace_gz.instructionsSlice().len == 0) {
4189 // In this case we will send a len=0 body which can be encoded more efficiently.4176 // In this case we will send a len=0 body which can be encoded more efficiently.
4190 break :inst inst;4177 break :inst inst;
...@@ -4284,8 +4271,19 @@ fn fnDecl(...@@ -4284,8 +4271,19 @@ fn fnDecl(
4284 fn_gz.instructions_top = ret_gz.instructions.items.len;4271 fn_gz.instructions_top = ret_gz.instructions.items.len;
42854272
4286 const prev_fn_block = astgen.fn_block;4273 const prev_fn_block = astgen.fn_block;
4274 const prev_fn_ret_ty = astgen.fn_ret_ty;
4287 astgen.fn_block = &fn_gz;4275 astgen.fn_block = &fn_gz;
4288 defer astgen.fn_block = prev_fn_block;4276 astgen.fn_ret_ty = if (is_inferred_error or ret_ref.toIndex() != null) r: {
4277 // We're essentially guaranteed to need the return type at some point,
4278 // since the return type is likely not `void` or `noreturn` so there
4279 // will probably be an explicit return requiring RLS. Fetch this
4280 // return type now so the rest of the function can use it.
4281 break :r try fn_gz.addNode(.ret_type, decl_node);
4282 } else ret_ref;
4283 defer {
4284 astgen.fn_block = prev_fn_block;
4285 astgen.fn_ret_ty = prev_fn_ret_ty;
4286 }
42894287
4290 const prev_var_args = astgen.fn_var_args;4288 const prev_var_args = astgen.fn_var_args;
4291 astgen.fn_var_args = is_var_args;4289 astgen.fn_var_args = is_var_args;
...@@ -4300,7 +4298,7 @@ fn fnDecl(...@@ -4300,7 +4298,7 @@ fn fnDecl(
43004298
4301 if (!fn_gz.endsWithNoReturn()) {4299 if (!fn_gz.endsWithNoReturn()) {
4302 // As our last action before the return, "pop" the error trace if needed4300 // As our last action before the return, "pop" the error trace if needed
4303 _ = try gz.addRestoreErrRetIndex(.ret, .always);4301 _ = try gz.addRestoreErrRetIndex(.ret, .always, decl_node);
43044302
4305 // Add implicit return at end of function.4303 // Add implicit return at end of function.
4306 _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));4304 _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
...@@ -4428,7 +4426,7 @@ fn globalVarDecl(...@@ -4428,7 +4426,7 @@ fn globalVarDecl(
4428 try expr(4426 try expr(
4429 &block_scope,4427 &block_scope,
4430 &block_scope.base,4428 &block_scope.base,
4431 .{ .rl = .{ .ty = .type_type } },4429 coerced_type_ri,
4432 var_decl.ast.type_node,4430 var_decl.ast.type_node,
4433 )4431 )
4434 else4432 else
...@@ -4732,8 +4730,13 @@ fn testDecl(...@@ -4732,8 +4730,13 @@ fn testDecl(
4732 defer fn_block.unstack();4730 defer fn_block.unstack();
47334731
4734 const prev_fn_block = astgen.fn_block;4732 const prev_fn_block = astgen.fn_block;
4733 const prev_fn_ret_ty = astgen.fn_ret_ty;
4735 astgen.fn_block = &fn_block;4734 astgen.fn_block = &fn_block;
4736 defer astgen.fn_block = prev_fn_block;4735 astgen.fn_ret_ty = .anyerror_void_error_union_type;
4736 defer {
4737 astgen.fn_block = prev_fn_block;
4738 astgen.fn_ret_ty = prev_fn_ret_ty;
4739 }
47374740
4738 astgen.advanceSourceCursorToNode(body_node);4741 astgen.advanceSourceCursorToNode(body_node);
4739 const lbrace_line = astgen.source_line - decl_block.decl_line;4742 const lbrace_line = astgen.source_line - decl_block.decl_line;
...@@ -4743,7 +4746,7 @@ fn testDecl(...@@ -4743,7 +4746,7 @@ fn testDecl(
4743 if (fn_block.isEmpty() or !fn_block.refIsNoReturn(block_result)) {4746 if (fn_block.isEmpty() or !fn_block.refIsNoReturn(block_result)) {
47444747
4745 // As our last action before the return, "pop" the error trace if needed4748 // As our last action before the return, "pop" the error trace if needed
4746 _ = try gz.addRestoreErrRetIndex(.ret, .always);4749 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
47474750
4748 // Add implicit return at end of function.4751 // Add implicit return at end of function.
4749 _ = try fn_block.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));4752 _ = try fn_block.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
...@@ -5246,7 +5249,7 @@ fn unionDeclInner(...@@ -5246,7 +5249,7 @@ fn unionDeclInner(
5246 return astgen.failNode(member_node, "union field missing type", .{});5249 return astgen.failNode(member_node, "union field missing type", .{});
5247 }5250 }
5248 if (have_align) {5251 if (have_align) {
5249 const align_inst = try expr(&block_scope, &block_scope.base, .{ .rl = .{ .ty = .u32_type } }, member.ast.align_expr);5252 const align_inst = try expr(&block_scope, &block_scope.base, coerced_align_ri, member.ast.align_expr);
5250 wip_members.appendToField(@intFromEnum(align_inst));5253 wip_members.appendToField(@intFromEnum(align_inst));
5251 any_aligned_fields = true;5254 any_aligned_fields = true;
5252 }5255 }
...@@ -5514,7 +5517,7 @@ fn containerDecl(...@@ -5514,7 +5517,7 @@ fn containerDecl(
5514 namespace.base.tag = .enum_namespace;5517 namespace.base.tag = .enum_namespace;
55155518
5516 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)5519 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)
5517 try comptimeExpr(&block_scope, &namespace.base, .{ .rl = .{ .ty = .type_type } }, container_decl.ast.arg)5520 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg)
5518 else5521 else
5519 .none;5522 .none;
55205523
...@@ -6071,7 +6074,7 @@ fn arrayAccess(...@@ -6071,7 +6074,7 @@ fn arrayAccess(
60716074
6072 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);6075 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
60736076
6074 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);6077 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
6075 try emitDbgStmt(gz, cursor);6078 try emitDbgStmt(gz, cursor);
60766079
6077 return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });6080 return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
...@@ -6081,7 +6084,7 @@ fn arrayAccess(...@@ -6081,7 +6084,7 @@ fn arrayAccess(
60816084
6082 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);6085 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
60836086
6084 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);6087 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
6085 try emitDbgStmt(gz, cursor);6088 try emitDbgStmt(gz, cursor);
60866089
6087 return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs }), node);6090 return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs }), node);
...@@ -6149,16 +6152,16 @@ fn boolBinOp(...@@ -6149,16 +6152,16 @@ fn boolBinOp(
6149 const tree = astgen.tree;6152 const tree = astgen.tree;
6150 const node_datas = tree.nodes.items(.data);6153 const node_datas = tree.nodes.items(.data);
61516154
6152 const lhs = try expr(gz, scope, bool_ri, node_datas[node].lhs);6155 const lhs = try expr(gz, scope, coerced_bool_ri, node_datas[node].lhs);
6153 const bool_br = try gz.addBoolBr(zir_tag, lhs);6156 const bool_br = (try gz.addPlNodePayloadIndex(zir_tag, node, undefined)).toIndex().?;
61546157
6155 var rhs_scope = gz.makeSubBlock(scope);6158 var rhs_scope = gz.makeSubBlock(scope);
6156 defer rhs_scope.unstack();6159 defer rhs_scope.unstack();
6157 const rhs = try expr(&rhs_scope, &rhs_scope.base, bool_ri, node_datas[node].rhs);6160 const rhs = try expr(&rhs_scope, &rhs_scope.base, coerced_bool_ri, node_datas[node].rhs);
6158 if (!gz.refIsNoReturn(rhs)) {6161 if (!gz.refIsNoReturn(rhs)) {
6159 _ = try rhs_scope.addBreakWithSrcNode(.break_inline, bool_br, rhs, node_datas[node].rhs);6162 _ = try rhs_scope.addBreakWithSrcNode(.break_inline, bool_br, rhs, node_datas[node].rhs);
6160 }6163 }
6161 try rhs_scope.setBoolBrBody(bool_br);6164 try rhs_scope.setBoolBrBody(bool_br, lhs);
61626165
6163 const block_ref = bool_br.toRef();6166 const block_ref = bool_br.toRef();
6164 return rvalue(gz, ri, block_ref, node);6167 return rvalue(gz, ri, block_ref, node);
...@@ -6222,7 +6225,7 @@ fn ifExpr(...@@ -6222,7 +6225,7 @@ fn ifExpr(
6222 .bool_bit = try block_scope.addUnNode(tag, optional, if_full.ast.cond_expr),6225 .bool_bit = try block_scope.addUnNode(tag, optional, if_full.ast.cond_expr),
6223 };6226 };
6224 } else {6227 } else {
6225 const cond = try expr(&block_scope, &block_scope.base, bool_ri, if_full.ast.cond_expr);6228 const cond = try expr(&block_scope, &block_scope.base, coerced_bool_ri, if_full.ast.cond_expr);
6226 break :c .{6229 break :c .{
6227 .inst = cond,6230 .inst = cond,
6228 .bool_bit = cond,6231 .bool_bit = cond,
...@@ -6468,7 +6471,7 @@ fn whileExpr(...@@ -6468,7 +6471,7 @@ fn whileExpr(
6468 .bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.cond_expr),6471 .bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.cond_expr),
6469 };6472 };
6470 } else {6473 } else {
6471 const cond = try expr(&cond_scope, &cond_scope.base, bool_ri, while_full.ast.cond_expr);6474 const cond = try expr(&cond_scope, &cond_scope.base, coerced_bool_ri, while_full.ast.cond_expr);
6472 break :c .{6475 break :c .{
6473 .inst = cond,6476 .inst = cond,
6474 .bool_bit = cond,6477 .bool_bit = cond,
...@@ -6726,7 +6729,10 @@ fn forExpr(...@@ -6726,7 +6729,10 @@ fn forExpr(
6726 const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;6729 const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;
6727 const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);6730 const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);
6728 // initialize to zero6731 // initialize to zero
6729 _ = try parent_gz.addBin(.store, index_ptr, .zero_usize);6732 _ = try parent_gz.addPlNode(.store_node, node, Zir.Inst.Bin{
6733 .lhs = index_ptr,
6734 .rhs = .zero_usize,
6735 });
6730 break :blk index_ptr;6736 break :blk index_ptr;
6731 };6737 };
67326738
...@@ -6956,7 +6962,10 @@ fn forExpr(...@@ -6956,7 +6962,10 @@ fn forExpr(
6956 .lhs = index,6962 .lhs = index,
6957 .rhs = .one_usize,6963 .rhs = .one_usize,
6958 });6964 });
6959 _ = try loop_scope.addBin(.store, index_ptr, index_plus_one);6965 _ = try loop_scope.addPlNode(.store_node, node, Zir.Inst.Bin{
6966 .lhs = index_ptr,
6967 .rhs = index_plus_one,
6968 });
6960 const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;6969 const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;
6961 _ = try loop_scope.addNode(repeat_tag, node);6970 _ = try loop_scope.addNode(repeat_tag, node);
69626971
...@@ -7124,7 +7133,7 @@ fn switchExprErrUnion(...@@ -7124,7 +7133,7 @@ fn switchExprErrUnion(
7124 block_scope.setBreakResultInfo(block_ri);7133 block_scope.setBreakResultInfo(block_ri);
71257134
7126 // Sema expects a dbg_stmt immediately before switch_block_err_union7135 // Sema expects a dbg_stmt immediately before switch_block_err_union
7127 try emitDbgStmt(parent_gz, operand_lc);7136 try emitDbgStmtForceCurrentIndex(parent_gz, operand_lc);
7128 // This gets added to the parent block later, after the item expressions.7137 // This gets added to the parent block later, after the item expressions.
7129 const switch_block = try parent_gz.makeBlockInst(.switch_block_err_union, switch_node);7138 const switch_block = try parent_gz.makeBlockInst(.switch_block_err_union, switch_node);
71307139
...@@ -7704,7 +7713,7 @@ fn switchExpr(...@@ -7704,7 +7713,7 @@ fn switchExpr(
7704 block_scope.setBreakResultInfo(block_ri);7713 block_scope.setBreakResultInfo(block_ri);
77057714
7706 // Sema expects a dbg_stmt immediately before switch_block(_ref)7715 // Sema expects a dbg_stmt immediately before switch_block(_ref)
7707 try emitDbgStmt(parent_gz, operand_lc);7716 try emitDbgStmtForceCurrentIndex(parent_gz, operand_lc);
7708 // This gets added to the parent block later, after the item expressions.7717 // This gets added to the parent block later, after the item expressions.
7709 const switch_tag: Zir.Inst.Tag = if (any_payload_is_ref) .switch_block_ref else .switch_block;7718 const switch_tag: Zir.Inst.Tag = if (any_payload_is_ref) .switch_block_ref else .switch_block;
7710 const switch_block = try parent_gz.makeBlockInst(switch_tag, switch_node);7719 const switch_block = try parent_gz.makeBlockInst(switch_tag, switch_node);
...@@ -8009,7 +8018,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -8009,7 +8018,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
8009 try genDefers(gz, defer_outer, scope, .normal_only);8018 try genDefers(gz, defer_outer, scope, .normal_only);
80108019
8011 // As our last action before the return, "pop" the error trace if needed8020 // As our last action before the return, "pop" the error trace if needed
8012 _ = try gz.addRestoreErrRetIndex(.ret, .always);8021 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80138022
8014 _ = try gz.addUnNode(.ret_node, .void_value, node);8023 _ = try gz.addUnNode(.ret_node, .void_value, node);
8015 return Zir.Inst.Ref.unreachable_value;8024 return Zir.Inst.Ref.unreachable_value;
...@@ -8038,7 +8047,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -8038,7 +8047,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
8038 .rl = .{ .ptr = .{ .inst = try gz.addNode(.ret_ptr, node) } },8047 .rl = .{ .ptr = .{ .inst = try gz.addNode(.ret_ptr, node) } },
8039 .ctx = .@"return",8048 .ctx = .@"return",
8040 } else .{8049 } else .{
8041 .rl = .{ .ty = try gz.addNode(.ret_type, node) },8050 .rl = .{ .coerced_ty = astgen.fn_ret_ty },
8042 .ctx = .@"return",8051 .ctx = .@"return",
8043 };8052 };
8044 const prev_anon_name_strategy = gz.anon_name_strategy;8053 const prev_anon_name_strategy = gz.anon_name_strategy;
...@@ -8052,7 +8061,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -8052,7 +8061,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
8052 try genDefers(gz, defer_outer, scope, .normal_only);8061 try genDefers(gz, defer_outer, scope, .normal_only);
80538062
8054 // As our last action before the return, "pop" the error trace if needed8063 // As our last action before the return, "pop" the error trace if needed
8055 _ = try gz.addRestoreErrRetIndex(.ret, .always);8064 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80568065
8057 try emitDbgStmt(gz, ret_lc);8066 try emitDbgStmt(gz, ret_lc);
8058 try gz.addRet(ri, operand, node);8067 try gz.addRet(ri, operand, node);
...@@ -8075,7 +8084,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -8075,7 +8084,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80758084
8076 // As our last action before the return, "pop" the error trace if needed8085 // As our last action before the return, "pop" the error trace if needed
8077 const result = if (ri.rl == .ptr) try gz.addUnNode(.load, ri.rl.ptr.inst, node) else operand;8086 const result = if (ri.rl == .ptr) try gz.addUnNode(.load, ri.rl.ptr.inst, node) else operand;
8078 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result });8087 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result }, node);
80798088
8080 try gz.addRet(ri, operand, node);8089 try gz.addRet(ri, operand, node);
8081 return Zir.Inst.Ref.unreachable_value;8090 return Zir.Inst.Ref.unreachable_value;
...@@ -8092,7 +8101,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -8092,7 +8101,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
8092 try genDefers(&then_scope, defer_outer, scope, .normal_only);8101 try genDefers(&then_scope, defer_outer, scope, .normal_only);
80938102
8094 // As our last action before the return, "pop" the error trace if needed8103 // As our last action before the return, "pop" the error trace if needed
8095 _ = try then_scope.addRestoreErrRetIndex(.ret, .always);8104 _ = try then_scope.addRestoreErrRetIndex(.ret, .always, node);
80968105
8097 try emitDbgStmt(&then_scope, ret_lc);8106 try emitDbgStmt(&then_scope, ret_lc);
8098 try then_scope.addRet(ri, operand, node);8107 try then_scope.addRet(ri, operand, node);
...@@ -8674,7 +8683,7 @@ fn unionInit(...@@ -8674,7 +8683,7 @@ fn unionInit(
8674 params: []const Ast.Node.Index,8683 params: []const Ast.Node.Index,
8675) InnerError!Zir.Inst.Ref {8684) InnerError!Zir.Inst.Ref {
8676 const union_type = try typeExpr(gz, scope, params[0]);8685 const union_type = try typeExpr(gz, scope, params[0]);
8677 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]);8686 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
8678 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{8687 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
8679 .container_type = union_type,8688 .container_type = union_type,
8680 .field_name = field_name,8689 .field_name = field_name,
...@@ -8987,12 +8996,12 @@ fn builtinCall(...@@ -8987,12 +8996,12 @@ fn builtinCall(
8987 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {8996 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
8988 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{8997 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
8989 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),8998 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
8990 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]),8999 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
8991 });9000 });
8992 }9001 }
8993 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{9002 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
8994 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),9003 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
8995 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]),9004 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
8996 });9005 });
8997 return rvalue(gz, ri, result, node);9006 return rvalue(gz, ri, result, node);
8998 },9007 },
...@@ -9119,7 +9128,7 @@ fn builtinCall(...@@ -9119,7 +9128,7 @@ fn builtinCall(
9119 return rvalue(gz, ri, .void_value, node);9128 return rvalue(gz, ri, .void_value, node);
9120 },9129 },
9121 .set_align_stack => {9130 .set_align_stack => {
9122 const order = try expr(gz, scope, align_ri, params[0]);9131 const order = try expr(gz, scope, coerced_align_ri, params[0]);
9123 _ = try gz.addExtendedPayload(.set_align_stack, Zir.Inst.UnNode{9132 _ = try gz.addExtendedPayload(.set_align_stack, Zir.Inst.UnNode{
9124 .node = gz.nodeIndexToRelative(node),9133 .node = gz.nodeIndexToRelative(node),
9125 .operand = order,9134 .operand = order,
...@@ -9161,32 +9170,32 @@ fn builtinCall(...@@ -9161,32 +9170,32 @@ fn builtinCall(
9161 .bit_size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .bit_size_of),9170 .bit_size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .bit_size_of),
9162 .align_of => return simpleUnOpType(gz, scope, ri, node, params[0], .align_of),9171 .align_of => return simpleUnOpType(gz, scope, ri, node, params[0], .align_of),
91639172
9164 .int_from_ptr => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_ptr),9173 .int_from_ptr => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_ptr),
9165 .compile_error => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .compile_error),9174 .compile_error => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .compile_error),
9166 .set_eval_branch_quota => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .set_eval_branch_quota),9175 .set_eval_branch_quota => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .set_eval_branch_quota),
9167 .int_from_enum => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_enum),9176 .int_from_enum => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_enum),
9168 .int_from_bool => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_bool),9177 .int_from_bool => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_bool),
9169 .embed_file => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .embed_file),9178 .embed_file => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .embed_file),
9170 .error_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .anyerror_type } }, params[0], .error_name),9179 .error_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .anyerror_type } }, params[0], .error_name),
9171 .set_runtime_safety => return simpleUnOp(gz, scope, ri, node, bool_ri, params[0], .set_runtime_safety),9180 .set_runtime_safety => return simpleUnOp(gz, scope, ri, node, coerced_bool_ri, params[0], .set_runtime_safety),
9172 .sqrt => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sqrt),9181 .sqrt => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sqrt),
9173 .sin => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sin),9182 .sin => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sin),
9174 .cos => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .cos),9183 .cos => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .cos),
9175 .tan => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tan),9184 .tan => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tan),
9176 .exp => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp),9185 .exp => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp),
9177 .exp2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp2),9186 .exp2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp2),
9178 .log => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log),9187 .log => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log),
9179 .log2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log2),9188 .log2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log2),
9180 .log10 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log10),9189 .log10 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log10),
9181 .abs => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .abs),9190 .abs => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .abs),
9182 .floor => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .floor),9191 .floor => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .floor),
9183 .ceil => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .ceil),9192 .ceil => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .ceil),
9184 .trunc => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .trunc),9193 .trunc => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .trunc),
9185 .round => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .round),9194 .round => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .round),
9186 .tag_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tag_name),9195 .tag_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tag_name),
9187 .type_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .type_name),9196 .type_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .type_name),
9188 .Frame => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_type),9197 .Frame => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_type),
9189 .frame_size => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_size),9198 .frame_size => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_size),
91909199
9191 .int_from_float => return typeCast(gz, scope, ri, node, params[0], .int_from_float, builtin_name),9200 .int_from_float => return typeCast(gz, scope, ri, node, params[0], .int_from_float, builtin_name),
9192 .float_from_int => return typeCast(gz, scope, ri, node, params[0], .float_from_int, builtin_name),9201 .float_from_int => return typeCast(gz, scope, ri, node, params[0], .float_from_int, builtin_name),
...@@ -9224,7 +9233,7 @@ fn builtinCall(...@@ -9224,7 +9233,7 @@ fn builtinCall(
9224 },9233 },
9225 .panic => {9234 .panic => {
9226 try emitDbgNode(gz, node);9235 try emitDbgNode(gz, node);
9227 return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .panic);9236 return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .panic);
9228 },9237 },
9229 .trap => {9238 .trap => {
9230 try emitDbgNode(gz, node);9239 try emitDbgNode(gz, node);
...@@ -9313,7 +9322,7 @@ fn builtinCall(...@@ -9313,7 +9322,7 @@ fn builtinCall(
9313 },9322 },
9314 .c_define => {9323 .c_define => {
9315 if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});9324 if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});
9316 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0]);9325 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0]);
9317 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);9326 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
9318 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{9327 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
9319 .node = gz.nodeIndexToRelative(node),9328 .node = gz.nodeIndexToRelative(node),
...@@ -9334,7 +9343,7 @@ fn builtinCall(...@@ -9334,7 +9343,7 @@ fn builtinCall(
9334 return rvalue(gz, ri, result, node);9343 return rvalue(gz, ri, result, node);
9335 },9344 },
9336 .reduce => {9345 .reduce => {
9337 const op = try expr(gz, scope, .{ .rl = .{ .ty = .reduce_op_type } }, params[0]);9346 const op = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .reduce_op_type } }, params[0]);
9338 const scalar = try expr(gz, scope, .{ .rl = .none }, params[1]);9347 const scalar = try expr(gz, scope, .{ .rl = .none }, params[1]);
9339 const result = try gz.addPlNode(.reduce, node, Zir.Inst.Bin{9348 const result = try gz.addPlNode(.reduce, node, Zir.Inst.Bin{
9340 .lhs = op,9349 .lhs = op,
...@@ -9410,7 +9419,7 @@ fn builtinCall(...@@ -9410,7 +9419,7 @@ fn builtinCall(
9410 },9419 },
9411 .field_parent_ptr => {9420 .field_parent_ptr => {
9412 const parent_type = try typeExpr(gz, scope, params[0]);9421 const parent_type = try typeExpr(gz, scope, params[0]);
9413 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]);9422 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
9414 const result = try gz.addPlNode(.field_parent_ptr, node, Zir.Inst.FieldParentPtr{9423 const result = try gz.addPlNode(.field_parent_ptr, node, Zir.Inst.FieldParentPtr{
9415 .parent_type = parent_type,9424 .parent_type = parent_type,
9416 .field_name = field_name,9425 .field_name = field_name,
...@@ -9547,7 +9556,7 @@ fn hasDeclOrField(...@@ -9547,7 +9556,7 @@ fn hasDeclOrField(
9547 tag: Zir.Inst.Tag,9556 tag: Zir.Inst.Tag,
9548) InnerError!Zir.Inst.Ref {9557) InnerError!Zir.Inst.Ref {
9549 const container_type = try typeExpr(gz, scope, lhs_node);9558 const container_type = try typeExpr(gz, scope, lhs_node);
9550 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, rhs_node);9559 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
9551 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{9560 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
9552 .lhs = container_type,9561 .lhs = container_type,
9553 .rhs = name,9562 .rhs = name,
...@@ -9697,7 +9706,7 @@ fn simpleCBuiltin(...@@ -9697,7 +9706,7 @@ fn simpleCBuiltin(
9697) InnerError!Zir.Inst.Ref {9706) InnerError!Zir.Inst.Ref {
9698 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";9707 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
9699 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});9708 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});
9700 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, operand_node);9709 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, operand_node);
9701 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{9710 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
9702 .node = gz.nodeIndexToRelative(node),9711 .node = gz.nodeIndexToRelative(node),
9703 .operand = operand,9712 .operand = operand,
...@@ -9715,7 +9724,7 @@ fn offsetOf(...@@ -9715,7 +9724,7 @@ fn offsetOf(
9715 tag: Zir.Inst.Tag,9724 tag: Zir.Inst.Tag,
9716) InnerError!Zir.Inst.Ref {9725) InnerError!Zir.Inst.Ref {
9717 const type_inst = try typeExpr(gz, scope, lhs_node);9726 const type_inst = try typeExpr(gz, scope, lhs_node);
9718 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, rhs_node);9727 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
9719 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{9728 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
9720 .lhs = type_inst,9729 .lhs = type_inst,
9721 .rhs = field_name,9730 .rhs = field_name,
...@@ -9828,13 +9837,8 @@ fn callExpr(...@@ -9828,13 +9837,8 @@ fn callExpr(
9828 astgen.advanceSourceCursor(astgen.tree.tokens.items(.start)[call.ast.lparen]);9837 astgen.advanceSourceCursor(astgen.tree.tokens.items(.start)[call.ast.lparen]);
9829 const line = astgen.source_line - gz.decl_line;9838 const line = astgen.source_line - gz.decl_line;
9830 const column = astgen.source_column;9839 const column = astgen.source_column;
98319840 // Sema expects a dbg_stmt immediately before call,
9832 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{9841 try emitDbgStmtForceCurrentIndex(gz, .{ line, column });
9833 .dbg_stmt = .{
9834 .line = line,
9835 .column = column,
9836 },
9837 } });
9838 }9842 }
98399843
9840 switch (callee) {9844 switch (callee) {
...@@ -10985,7 +10989,10 @@ fn rvalueInner(...@@ -10985,7 +10989,10 @@ fn rvalueInner(
10985 return .void_value;10989 return .void_value;
10986 },10990 },
10987 .inferred_ptr => |alloc| {10991 .inferred_ptr => |alloc| {
10988 _ = try gz.addBin(.store_to_inferred_ptr, alloc, result);10992 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
10993 .lhs = alloc,
10994 .rhs = result,
10995 });
10989 return .void_value;10996 return .void_value;
10990 },10997 },
10991 .destructure => |destructure| {10998 .destructure => |destructure| {
...@@ -11012,7 +11019,10 @@ fn rvalueInner(...@@ -11012,7 +11019,10 @@ fn rvalueInner(
11012 });11019 });
11013 },11020 },
11014 .inferred_ptr => |ptr_inst| {11021 .inferred_ptr => |ptr_inst| {
11015 _ = try gz.addBin(.store_to_inferred_ptr, ptr_inst, elem_val);11022 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
11023 .lhs = ptr_inst,
11024 .rhs = elem_val,
11025 });
11016 },11026 },
11017 .discard => unreachable,11027 .discard => unreachable,
11018 }11028 }
...@@ -11834,19 +11844,20 @@ const GenZir = struct {...@@ -11834,19 +11844,20 @@ const GenZir = struct {
11834 }11844 }
1183511845
11836 /// Assumes nothing stacked on `gz`. Unstacks `gz`.11846 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
11837 fn setBoolBrBody(gz: *GenZir, inst: Zir.Inst.Index) !void {11847 fn setBoolBrBody(gz: *GenZir, bool_br: Zir.Inst.Index, bool_br_lhs: Zir.Inst.Ref) !void {
11838 const astgen = gz.astgen;11848 const astgen = gz.astgen;
11839 const gpa = astgen.gpa;11849 const gpa = astgen.gpa;
11840 const body = gz.instructionsSlice();11850 const body = gz.instructionsSlice();
11841 const body_len = astgen.countBodyLenAfterFixups(body);11851 const body_len = astgen.countBodyLenAfterFixups(body);
11842 try astgen.extra.ensureUnusedCapacity(11852 try astgen.extra.ensureUnusedCapacity(
11843 gpa,11853 gpa,
11844 @typeInfo(Zir.Inst.Block).Struct.fields.len + body_len,11854 @typeInfo(Zir.Inst.BoolBr).Struct.fields.len + body_len,
11845 );11855 );
11846 const zir_datas = astgen.instructions.items(.data);11856 const zir_datas = astgen.instructions.items(.data);
11847 zir_datas[@intFromEnum(inst)].bool_br.payload_index = astgen.addExtraAssumeCapacity(11857 zir_datas[@intFromEnum(bool_br)].pl_node.payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.BoolBr{
11848 Zir.Inst.Block{ .body_len = body_len },11858 .lhs = bool_br_lhs,
11849 );11859 .body_len = body_len,
11860 });
11850 astgen.appendBodyWithFixups(body);11861 astgen.appendBodyWithFixups(body);
11851 gz.unstack();11862 gz.unstack();
11852 }11863 }
...@@ -12231,30 +12242,6 @@ const GenZir = struct {...@@ -12231,30 +12242,6 @@ const GenZir = struct {
12231 return new_index.toRef();12242 return new_index.toRef();
12232 }12243 }
1223312244
12234 /// Note that this returns a `Zir.Inst.Index` not a ref.
12235 /// Leaves the `payload_index` field undefined.
12236 fn addBoolBr(
12237 gz: *GenZir,
12238 tag: Zir.Inst.Tag,
12239 lhs: Zir.Inst.Ref,
12240 ) !Zir.Inst.Index {
12241 assert(lhs != .none);
12242 const gpa = gz.astgen.gpa;
12243 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12244 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
12245
12246 const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
12247 gz.astgen.instructions.appendAssumeCapacity(.{
12248 .tag = tag,
12249 .data = .{ .bool_br = .{
12250 .lhs = lhs,
12251 .payload_index = undefined,
12252 } },
12253 });
12254 gz.instructions.appendAssumeCapacity(new_index);
12255 return new_index;
12256 }
12257
12258 fn addInt(gz: *GenZir, integer: u64) !Zir.Inst.Ref {12245 fn addInt(gz: *GenZir, integer: u64) !Zir.Inst.Ref {
12259 return gz.add(.{12246 return gz.add(.{
12260 .tag = .int,12247 .tag = .int,
...@@ -12575,17 +12562,37 @@ const GenZir = struct {...@@ -12575,17 +12562,37 @@ const GenZir = struct {
12575 always: void,12562 always: void,
12576 if_non_error: Zir.Inst.Ref,12563 if_non_error: Zir.Inst.Ref,
12577 },12564 },
12565 src_node: Ast.Node.Index,
12578 ) !Zir.Inst.Index {12566 ) !Zir.Inst.Index {
12579 return gz.addAsIndex(.{12567 switch (cond) {
12580 .tag = .restore_err_ret_index,12568 .always => return gz.addAsIndex(.{
12581 .data = .{ .restore_err_ret_index = .{12569 .tag = .restore_err_ret_index_unconditional,
12582 .block = switch (bt) {12570 .data = .{ .un_node = .{
12583 .ret => .none,12571 .operand = switch (bt) {
12584 .block => |b| b.toRef(),12572 .ret => .none,
12585 },12573 .block => |b| b.toRef(),
12586 .operand = if (cond == .if_non_error) cond.if_non_error else .none,12574 },
12587 } },12575 .src_node = gz.nodeIndexToRelative(src_node),
12588 });12576 } },
12577 }),
12578 .if_non_error => |operand| switch (bt) {
12579 .ret => return gz.addAsIndex(.{
12580 .tag = .restore_err_ret_index_fn_entry,
12581 .data = .{ .un_node = .{
12582 .operand = operand,
12583 .src_node = gz.nodeIndexToRelative(src_node),
12584 } },
12585 }),
12586 .block => |block| return (try gz.addExtendedPayload(
12587 .restore_err_ret_index,
12588 Zir.Inst.RestoreErrRetIndex{
12589 .src_node = gz.nodeIndexToRelative(src_node),
12590 .block = block.toRef(),
12591 .operand = operand,
12592 },
12593 )).toIndex().?,
12594 },
12595 }
12589 }12596 }
1259012597
12591 fn addBreak(12598 fn addBreak(
...@@ -12911,20 +12918,20 @@ const GenZir = struct {...@@ -12911,20 +12918,20 @@ const GenZir = struct {
12911 const astgen = gz.astgen;12918 const astgen = gz.astgen;
12912 const gpa = astgen.gpa;12919 const gpa = astgen.gpa;
1291312920
12921 // Node 0 is valid for the root `struct_decl` of a file!
12922 assert(args.src_node != 0 or gz.parent.tag == .top);
12923
12914 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);12924 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1291512925
12916 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 6);12926 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 4);
12917 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.StructDecl{12927 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.StructDecl{
12918 .fields_hash_0 = fields_hash_arr[0],12928 .fields_hash_0 = fields_hash_arr[0],
12919 .fields_hash_1 = fields_hash_arr[1],12929 .fields_hash_1 = fields_hash_arr[1],
12920 .fields_hash_2 = fields_hash_arr[2],12930 .fields_hash_2 = fields_hash_arr[2],
12921 .fields_hash_3 = fields_hash_arr[3],12931 .fields_hash_3 = fields_hash_arr[3],
12932 .src_node = gz.nodeIndexToRelative(args.src_node),
12922 });12933 });
1292312934
12924 if (args.src_node != 0) {
12925 const node_offset = gz.nodeIndexToRelative(args.src_node);
12926 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12927 }
12928 if (args.fields_len != 0) {12935 if (args.fields_len != 0) {
12929 astgen.extra.appendAssumeCapacity(args.fields_len);12936 astgen.extra.appendAssumeCapacity(args.fields_len);
12930 }12937 }
...@@ -12942,7 +12949,6 @@ const GenZir = struct {...@@ -12942,7 +12949,6 @@ const GenZir = struct {
12942 .data = .{ .extended = .{12949 .data = .{ .extended = .{
12943 .opcode = .struct_decl,12950 .opcode = .struct_decl,
12944 .small = @bitCast(Zir.Inst.StructDecl.Small{12951 .small = @bitCast(Zir.Inst.StructDecl.Small{
12945 .has_src_node = args.src_node != 0,
12946 .has_fields_len = args.fields_len != 0,12952 .has_fields_len = args.fields_len != 0,
12947 .has_decls_len = args.decls_len != 0,12953 .has_decls_len = args.decls_len != 0,
12948 .has_backing_int = args.backing_int_ref != .none,12954 .has_backing_int = args.backing_int_ref != .none,
...@@ -12974,20 +12980,19 @@ const GenZir = struct {...@@ -12974,20 +12980,19 @@ const GenZir = struct {
12974 const astgen = gz.astgen;12980 const astgen = gz.astgen;
12975 const gpa = astgen.gpa;12981 const gpa = astgen.gpa;
1297612982
12983 assert(args.src_node != 0);
12984
12977 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);12985 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1297812986
12979 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 5);12987 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 4);
12980 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.UnionDecl{12988 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.UnionDecl{
12981 .fields_hash_0 = fields_hash_arr[0],12989 .fields_hash_0 = fields_hash_arr[0],
12982 .fields_hash_1 = fields_hash_arr[1],12990 .fields_hash_1 = fields_hash_arr[1],
12983 .fields_hash_2 = fields_hash_arr[2],12991 .fields_hash_2 = fields_hash_arr[2],
12984 .fields_hash_3 = fields_hash_arr[3],12992 .fields_hash_3 = fields_hash_arr[3],
12993 .src_node = gz.nodeIndexToRelative(args.src_node),
12985 });12994 });
1298612995
12987 if (args.src_node != 0) {
12988 const node_offset = gz.nodeIndexToRelative(args.src_node);
12989 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12990 }
12991 if (args.tag_type != .none) {12996 if (args.tag_type != .none) {
12992 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));12997 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
12993 }12998 }
...@@ -13005,7 +13010,6 @@ const GenZir = struct {...@@ -13005,7 +13010,6 @@ const GenZir = struct {
13005 .data = .{ .extended = .{13010 .data = .{ .extended = .{
13006 .opcode = .union_decl,13011 .opcode = .union_decl,
13007 .small = @bitCast(Zir.Inst.UnionDecl.Small{13012 .small = @bitCast(Zir.Inst.UnionDecl.Small{
13008 .has_src_node = args.src_node != 0,
13009 .has_tag_type = args.tag_type != .none,13013 .has_tag_type = args.tag_type != .none,
13010 .has_body_len = args.body_len != 0,13014 .has_body_len = args.body_len != 0,
13011 .has_fields_len = args.fields_len != 0,13015 .has_fields_len = args.fields_len != 0,
...@@ -13032,20 +13036,19 @@ const GenZir = struct {...@@ -13032,20 +13036,19 @@ const GenZir = struct {
13032 const astgen = gz.astgen;13036 const astgen = gz.astgen;
13033 const gpa = astgen.gpa;13037 const gpa = astgen.gpa;
1303413038
13039 assert(args.src_node != 0);
13040
13035 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);13041 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1303613042
13037 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 5);13043 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 4);
13038 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.EnumDecl{13044 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.EnumDecl{
13039 .fields_hash_0 = fields_hash_arr[0],13045 .fields_hash_0 = fields_hash_arr[0],
13040 .fields_hash_1 = fields_hash_arr[1],13046 .fields_hash_1 = fields_hash_arr[1],
13041 .fields_hash_2 = fields_hash_arr[2],13047 .fields_hash_2 = fields_hash_arr[2],
13042 .fields_hash_3 = fields_hash_arr[3],13048 .fields_hash_3 = fields_hash_arr[3],
13049 .src_node = gz.nodeIndexToRelative(args.src_node),
13043 });13050 });
1304413051
13045 if (args.src_node != 0) {
13046 const node_offset = gz.nodeIndexToRelative(args.src_node);
13047 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13048 }
13049 if (args.tag_type != .none) {13052 if (args.tag_type != .none) {
13050 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));13053 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
13051 }13054 }
...@@ -13063,7 +13066,6 @@ const GenZir = struct {...@@ -13063,7 +13066,6 @@ const GenZir = struct {
13063 .data = .{ .extended = .{13066 .data = .{ .extended = .{
13064 .opcode = .enum_decl,13067 .opcode = .enum_decl,
13065 .small = @bitCast(Zir.Inst.EnumDecl.Small{13068 .small = @bitCast(Zir.Inst.EnumDecl.Small{
13066 .has_src_node = args.src_node != 0,
13067 .has_tag_type = args.tag_type != .none,13069 .has_tag_type = args.tag_type != .none,
13068 .has_body_len = args.body_len != 0,13070 .has_body_len = args.body_len != 0,
13069 .has_fields_len = args.fields_len != 0,13071 .has_fields_len = args.fields_len != 0,
...@@ -13083,13 +13085,13 @@ const GenZir = struct {...@@ -13083,13 +13085,13 @@ const GenZir = struct {
13083 const astgen = gz.astgen;13085 const astgen = gz.astgen;
13084 const gpa = astgen.gpa;13086 const gpa = astgen.gpa;
1308513087
13086 try astgen.extra.ensureUnusedCapacity(gpa, 2);13088 assert(args.src_node != 0);
13087 const payload_index: u32 = @intCast(astgen.extra.items.len);13089
13090 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.OpaqueDecl).Struct.fields.len + 1);
13091 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{
13092 .src_node = gz.nodeIndexToRelative(args.src_node),
13093 });
1308813094
13089 if (args.src_node != 0) {
13090 const node_offset = gz.nodeIndexToRelative(args.src_node);
13091 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13092 }
13093 if (args.decls_len != 0) {13095 if (args.decls_len != 0) {
13094 astgen.extra.appendAssumeCapacity(args.decls_len);13096 astgen.extra.appendAssumeCapacity(args.decls_len);
13095 }13097 }
...@@ -13098,7 +13100,6 @@ const GenZir = struct {...@@ -13098,7 +13100,6 @@ const GenZir = struct {
13098 .data = .{ .extended = .{13100 .data = .{ .extended = .{
13099 .opcode = .opaque_decl,13101 .opcode = .opaque_decl,
13100 .small = @bitCast(Zir.Inst.OpaqueDecl.Small{13102 .small = @bitCast(Zir.Inst.OpaqueDecl.Small{
13101 .has_src_node = args.src_node != 0,
13102 .has_decls_len = args.decls_len != 0,13103 .has_decls_len = args.decls_len != 0,
13103 .name_strategy = gz.anon_name_strategy,13104 .name_strategy = gz.anon_name_strategy,
13104 }),13105 }),
...@@ -13136,7 +13137,7 @@ const GenZir = struct {...@@ -13136,7 +13137,7 @@ const GenZir = struct {
13136 fn addRet(gz: *GenZir, ri: ResultInfo, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {13137 fn addRet(gz: *GenZir, ri: ResultInfo, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {
13137 switch (ri.rl) {13138 switch (ri.rl) {
13138 .ptr => |ptr_res| _ = try gz.addUnNode(.ret_load, ptr_res.inst, node),13139 .ptr => |ptr_res| _ = try gz.addUnNode(.ret_load, ptr_res.inst, node),
13139 .ty => _ = try gz.addUnNode(.ret_node, operand, node),13140 .coerced_ty => _ = try gz.addUnNode(.ret_node, operand, node),
13140 else => unreachable,13141 else => unreachable,
13141 }13142 }
13142 }13143 }
...@@ -13517,6 +13518,44 @@ fn countBodyLenAfterFixups(astgen: *AstGen, body: []const Zir.Inst.Index) u32 {...@@ -13517,6 +13518,44 @@ fn countBodyLenAfterFixups(astgen: *AstGen, body: []const Zir.Inst.Index) u32 {
1351713518
13518fn emitDbgStmt(gz: *GenZir, lc: LineColumn) !void {13519fn emitDbgStmt(gz: *GenZir, lc: LineColumn) !void {
13519 if (gz.is_comptime) return;13520 if (gz.is_comptime) return;
13521 if (gz.instructions.items.len > 0) {
13522 const astgen = gz.astgen;
13523 const last = gz.instructions.items[gz.instructions.items.len - 1];
13524 if (astgen.instructions.items(.tag)[@intFromEnum(last)] == .dbg_stmt) {
13525 astgen.instructions.items(.data)[@intFromEnum(last)].dbg_stmt = .{
13526 .line = lc[0],
13527 .column = lc[1],
13528 };
13529 return;
13530 }
13531 }
13532
13533 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
13534 .dbg_stmt = .{
13535 .line = lc[0],
13536 .column = lc[1],
13537 },
13538 } });
13539}
13540
13541/// In some cases, Sema expects us to generate a `dbg_stmt` at the instruction
13542/// *index* directly preceding the next instruction (e.g. if a call is %10, it
13543/// expects a dbg_stmt at %9). TODO: this logic may allow redundant dbg_stmt
13544/// instructions; fix up Sema so we don't need it!
13545fn emitDbgStmtForceCurrentIndex(gz: *GenZir, lc: LineColumn) !void {
13546 const astgen = gz.astgen;
13547 if (gz.instructions.items.len > 0 and
13548 @intFromEnum(gz.instructions.items[gz.instructions.items.len - 1]) == astgen.instructions.len - 1)
13549 {
13550 const last = astgen.instructions.len - 1;
13551 if (astgen.instructions.items(.tag)[last] == .dbg_stmt) {
13552 astgen.instructions.items(.data)[last].dbg_stmt = .{
13553 .line = lc[0],
13554 .column = lc[1],
13555 };
13556 return;
13557 }
13558 }
1352013559
13521 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{13560 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
13522 .dbg_stmt = .{13561 .dbg_stmt = .{
src/Autodoc.zig+15-48
...@@ -1799,7 +1799,8 @@ fn walkInstruction(...@@ -1799,7 +1799,8 @@ fn walkInstruction(
1799 };1799 };
1800 },1800 },
1801 .bool_br_and, .bool_br_or => {1801 .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
1804 const bin_index = self.exprs.items.len;1805 const bin_index = self.exprs.items.len;
1805 try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });1806 try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
...@@ -1808,14 +1809,13 @@ fn walkInstruction(...@@ -1808,14 +1809,13 @@ fn walkInstruction(
1808 file,1809 file,
1809 parent_scope,1810 parent_scope,
1810 parent_src,1811 parent_src,
1811 bool_br.lhs,1812 extra.data.lhs,
1812 false,1813 false,
1813 call_ctx,1814 call_ctx,
1814 );1815 );
1815 const lhs_index = self.exprs.items.len;1816 const lhs_index = self.exprs.items.len;
1816 try self.exprs.append(self.arena, lhs.expr);1817 try self.exprs.append(self.arena, lhs.expr);
18171818
1818 const extra = file.zir.extraData(Zir.Inst.Block, bool_br.payload_index);
1819 const rhs = try self.walkInstruction(1819 const rhs = try self.walkInstruction(
1820 file,1820 file,
1821 parent_scope,1821 parent_scope,
...@@ -3395,19 +3395,10 @@ fn walkInstruction(...@@ -3395,19 +3395,10 @@ fn walkInstruction(
3395 .enclosing_type = type_slot_index,3395 .enclosing_type = type_slot_index,
3396 };3396 };
33973397
3398 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));3398 const extra = file.zir.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3399 var extra_index: usize = extended.operand;3399 var extra_index: usize = extra.end;
3400
3401 const src_node: ?i32 = if (small.has_src_node) blk: {
3402 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3403 extra_index += 1;
3404 break :blk src_node;
3405 } else null;
34063400
3407 const src_info = if (src_node) |sn|3401 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
3408 try self.srcLocInfo(file, sn, parent_src)
3409 else
3410 parent_src;
34113402
3412 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};3403 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
3413 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};3404 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};
...@@ -3498,18 +3489,10 @@ fn walkInstruction(...@@ -3498,18 +3489,10 @@ fn walkInstruction(
3498 };3489 };
34993490
3500 const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));3491 const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));
3501 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;3492 const extra = file.zir.extraData(Zir.Inst.UnionDecl, extended.operand);
35023493 var extra_index: usize = extra.end;
3503 const src_node: ?i32 = if (small.has_src_node) blk: {
3504 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3505 extra_index += 1;
3506 break :blk src_node;
3507 } else null;
35083494
3509 const src_info = if (src_node) |sn|3495 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
3510 try self.srcLocInfo(file, sn, parent_src)
3511 else
3512 parent_src;
35133496
3514 // We delay analysis because union tags can refer to3497 // We delay analysis because union tags can refer to
3515 // decls defined inside the union itself.3498 // decls defined inside the union itself.
...@@ -3628,18 +3611,10 @@ fn walkInstruction(...@@ -3628,18 +3611,10 @@ fn walkInstruction(
3628 };3611 };
36293612
3630 const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));3613 const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));
3631 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;3614 const extra = file.zir.extraData(Zir.Inst.EnumDecl, extended.operand);
36323615 var extra_index: usize = extra.end;
3633 const src_node: ?i32 = if (small.has_src_node) blk: {
3634 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3635 extra_index += 1;
3636 break :blk src_node;
3637 } else null;
36383616
3639 const src_info = if (src_node) |sn|3617 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
3640 try self.srcLocInfo(file, sn, parent_src)
3641 else
3642 parent_src;
36433618
3644 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {3619 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {
3645 const tag_type = file.zir.extra[extra_index];3620 const tag_type = file.zir.extra[extra_index];
...@@ -3779,18 +3754,10 @@ fn walkInstruction(...@@ -3779,18 +3754,10 @@ fn walkInstruction(
3779 };3754 };
37803755
3781 const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));3756 const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));
3782 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;3757 const extra = file.zir.extraData(Zir.Inst.StructDecl, extended.operand);
37833758 var extra_index: usize = extra.end;
3784 const src_node: ?i32 = if (small.has_src_node) blk: {
3785 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3786 extra_index += 1;
3787 break :blk src_node;
3788 } else null;
37893759
3790 const src_info = if (src_node) |sn|3760 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
3791 try self.srcLocInfo(file, sn, parent_src)
3792 else
3793 parent_src;
37943761
3795 const fields_len = if (small.has_fields_len) blk: {3762 const fields_len = if (small.has_fields_len) blk: {
3796 const fields_len = file.zir.extra[extra_index];3763 const fields_len = file.zir.extra[extra_index];
src/Module.zig+15
...@@ -1867,6 +1867,16 @@ pub const SrcLoc = struct {...@@ -1867,6 +1867,16 @@ pub const SrcLoc = struct {
1867 else => return nodeToSpan(tree, node),1867 else => return nodeToSpan(tree, node),
1868 }1868 }
1869 },1869 },
1870 .node_offset_return_operand => |node_off| {
1871 const tree = try src_loc.file_scope.getTree(gpa);
1872 const node = src_loc.declRelativeToNodeIndex(node_off);
1873 const node_tags = tree.nodes.items(.tag);
1874 const node_datas = tree.nodes.items(.data);
1875 if (node_tags[node] == .@"return" and node_datas[node].lhs != 0) {
1876 return nodeToSpan(tree, node_datas[node].lhs);
1877 }
1878 return nodeToSpan(tree, node);
1879 },
1870 }1880 }
1871 }1881 }
18721882
...@@ -2221,6 +2231,10 @@ pub const LazySrcLoc = union(enum) {...@@ -2221,6 +2231,10 @@ pub const LazySrcLoc = union(enum) {
2221 /// The source location points to the RHS of an assignment.2231 /// The source location points to the RHS of an assignment.
2222 /// The Decl is determined contextually.2232 /// The Decl is determined contextually.
2223 node_offset_store_operand: i32,2233 node_offset_store_operand: i32,
2234 /// The source location points to the operand of a `return` statement, or
2235 /// the `return` itself if there is no explicit operand.
2236 /// The Decl is determined contextually.
2237 node_offset_return_operand: i32,
2224 /// The source location points to a for loop input.2238 /// The source location points to a for loop input.
2225 /// The Decl is determined contextually.2239 /// The Decl is determined contextually.
2226 for_input: struct {2240 for_input: struct {
...@@ -2347,6 +2361,7 @@ pub const LazySrcLoc = union(enum) {...@@ -2347,6 +2361,7 @@ pub const LazySrcLoc = union(enum) {
2347 .node_offset_init_ty,2361 .node_offset_init_ty,
2348 .node_offset_store_ptr,2362 .node_offset_store_ptr,
2349 .node_offset_store_operand,2363 .node_offset_store_operand,
2364 .node_offset_return_operand,
2350 .for_input,2365 .for_input,
2351 .for_capture_from_input,2366 .for_capture_from_input,
2352 .array_cat_lhs,2367 .array_cat_lhs,
src/Sema.zig+146-141
...@@ -50,11 +50,6 @@ branch_count: u32 = 0,...@@ -50,11 +50,6 @@ branch_count: u32 = 0,
50/// Populated when returning `error.ComptimeBreak`. Used to communicate the50/// Populated when returning `error.ComptimeBreak`. Used to communicate the
51/// break instruction up the stack to find the corresponding Block.51/// break instruction up the stack to find the corresponding Block.
52comptime_break_inst: Zir.Inst.Index = undefined,52comptime_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 },
58decl_val_table: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Air.Inst.Ref) = .{},53decl_val_table: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Air.Inst.Ref) = .{},
59/// When doing a generic function instantiation, this array collects a value54/// When doing a generic function instantiation, this array collects a value
60/// for each parameter of the generic owner. `none` for non-comptime parameters.55/// for each parameter of the generic owner. `none` for non-comptime parameters.
...@@ -1006,10 +1001,10 @@ fn analyzeBodyInner(...@@ -1006,10 +1001,10 @@ fn analyzeBodyInner(
1006 const air_inst: Air.Inst.Ref = switch (tags[@intFromEnum(inst)]) {1001 const air_inst: Air.Inst.Ref = switch (tags[@intFromEnum(inst)]) {
1007 // zig fmt: off1002 // zig fmt: off
1008 .alloc => try sema.zirAlloc(block, inst),1003 .alloc => try sema.zirAlloc(block, inst),
1009 .alloc_inferred => try sema.zirAllocInferred(block, inst, true),1004 .alloc_inferred => try sema.zirAllocInferred(block, true),
1010 .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, false),1005 .alloc_inferred_mut => try sema.zirAllocInferred(block, false),
1011 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(inst, true),1006 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(true),
1012 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, false),1007 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(false),
1013 .alloc_mut => try sema.zirAllocMut(block, inst),1008 .alloc_mut => try sema.zirAllocMut(block, inst),
1014 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),1009 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
1015 .make_ptr_const => try sema.zirMakePtrConst(block, inst),1010 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
...@@ -1308,6 +1303,11 @@ fn analyzeBodyInner(...@@ -1308,6 +1303,11 @@ fn analyzeBodyInner(
1308 i += 1;1303 i += 1;
1309 continue;1304 continue;
1310 },1305 },
1306 .restore_err_ret_index => {
1307 try sema.zirRestoreErrRetIndex(block, extended);
1308 i += 1;
1309 continue;
1310 },
1311 .value_placeholder => unreachable, // never appears in a body1311 .value_placeholder => unreachable, // never appears in a body
1312 };1312 };
1313 },1313 },
...@@ -1369,11 +1369,6 @@ fn analyzeBodyInner(...@@ -1369,11 +1369,6 @@ fn analyzeBodyInner(
1369 i += 1;1369 i += 1;
1370 continue;1370 continue;
1371 },1371 },
1372 .store => {
1373 try sema.zirStore(block, inst);
1374 i += 1;
1375 continue;
1376 },
1377 .store_node => {1372 .store_node => {
1378 try sema.zirStoreNode(block, inst);1373 try sema.zirStoreNode(block, inst);
1379 i += 1;1374 i += 1;
...@@ -1518,8 +1513,15 @@ fn analyzeBodyInner(...@@ -1518,8 +1513,15 @@ fn analyzeBodyInner(
1518 i += 1;1513 i += 1;
1519 continue;1514 continue;
1520 },1515 },
1521 .restore_err_ret_index => {1516 .restore_err_ret_index_unconditional => {
1522 try sema.zirRestoreErrRetIndex(block, inst);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);
1523 i += 1;1525 i += 1;
1524 continue;1526 continue;
1525 },1527 },
...@@ -2723,7 +2725,6 @@ pub fn getStructType(...@@ -2723,7 +2725,6 @@ pub fn getStructType(
2723 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);2725 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
27242726
2725 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;2727 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
2726 extra_index += @intFromBool(small.has_src_node);
2727 const fields_len = if (small.has_fields_len) blk: {2728 const fields_len = if (small.has_fields_len) blk: {
2728 const fields_len = sema.code.extra[extra_index];2729 const fields_len = sema.code.extra[extra_index];
2729 extra_index += 1;2730 extra_index += 1;
...@@ -2776,10 +2777,7 @@ fn zirStructDecl(...@@ -2776,10 +2777,7 @@ fn zirStructDecl(
2776 const mod = sema.mod;2777 const mod = sema.mod;
2777 const ip = &mod.intern_pool;2778 const ip = &mod.intern_pool;
2778 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);2779 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
2779 const src: LazySrcLoc = if (small.has_src_node) blk: {2780 const src = sema.code.extraData(Zir.Inst.StructDecl, extended.operand).data.src();
2780 const node_offset: i32 = @bitCast(sema.code.extra[extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len]);
2781 break :blk LazySrcLoc.nodeOffset(node_offset);
2782 } else sema.src;
27832781
2784 // Because these three things each reference each other, `undefined`2782 // Because these three things each reference each other, `undefined`
2785 // placeholders are used before being set after the struct type gains an2783 // placeholders are used before being set after the struct type gains an
...@@ -2939,13 +2937,10 @@ fn zirEnumDecl(...@@ -2939,13 +2937,10 @@ fn zirEnumDecl(
2939 const mod = sema.mod;2937 const mod = sema.mod;
2940 const gpa = sema.gpa;2938 const gpa = sema.gpa;
2941 const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);2939 const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);
2942 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;2940 const extra = sema.code.extraData(Zir.Inst.EnumDecl, extended.operand);
2941 var extra_index: usize = extra.end;
29432942
2944 const src: LazySrcLoc = if (small.has_src_node) blk: {2943 const src = extra.data.src();
2945 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
2946 extra_index += 1;
2947 break :blk LazySrcLoc.nodeOffset(node_offset);
2948 } else sema.src;
2949 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };2944 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
29502945
2951 const tag_type_ref = if (small.has_tag_type) blk: {2946 const tag_type_ref = if (small.has_tag_type) blk: {
...@@ -3212,13 +3207,10 @@ fn zirUnionDecl(...@@ -3212,13 +3207,10 @@ fn zirUnionDecl(
3212 const mod = sema.mod;3207 const mod = sema.mod;
3213 const gpa = sema.gpa;3208 const gpa = sema.gpa;
3214 const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);3209 const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);
3215 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;3210 const extra = sema.code.extraData(Zir.Inst.UnionDecl, extended.operand);
3211 var extra_index: usize = extra.end;
32163212
3217 const src: LazySrcLoc = if (small.has_src_node) blk: {3213 const src = extra.data.src();
3218 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3219 extra_index += 1;
3220 break :blk LazySrcLoc.nodeOffset(node_offset);
3221 } else sema.src;
32223214
3223 extra_index += @intFromBool(small.has_tag_type);3215 extra_index += @intFromBool(small.has_tag_type);
3224 extra_index += @intFromBool(small.has_body_len);3216 extra_index += @intFromBool(small.has_body_len);
...@@ -3321,13 +3313,10 @@ fn zirOpaqueDecl(...@@ -3321,13 +3313,10 @@ fn zirOpaqueDecl(
33213313
3322 const mod = sema.mod;3314 const mod = sema.mod;
3323 const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small);3315 const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small);
3324 var extra_index: usize = extended.operand;3316 const extra = sema.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3317 var extra_index: usize = extra.end;
33253318
3326 const src: LazySrcLoc = if (small.has_src_node) blk: {3319 const src = extra.data.src();
3327 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3328 extra_index += 1;
3329 break :blk LazySrcLoc.nodeOffset(node_offset);
3330 } else sema.src;
33313320
3332 const decls_len = if (small.has_decls_len) blk: {3321 const decls_len = if (small.has_decls_len) blk: {
3333 const decls_len = sema.code.extra[extra_index];3322 const decls_len = sema.code.extra[extra_index];
...@@ -3977,13 +3966,9 @@ fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Ai...@@ -3977,13 +3966,9 @@ fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Ai
39773966
3978fn zirAllocInferredComptime(3967fn zirAllocInferredComptime(
3979 sema: *Sema,3968 sema: *Sema,
3980 inst: Zir.Inst.Index,
3981 is_const: bool,3969 is_const: bool,
3982) CompileError!Air.Inst.Ref {3970) CompileError!Air.Inst.Ref {
3983 const gpa = sema.gpa;3971 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;
39873972
3988 try sema.air_instructions.append(gpa, .{3973 try sema.air_instructions.append(gpa, .{
3989 .tag = .inferred_alloc_comptime,3974 .tag = .inferred_alloc_comptime,
...@@ -4042,16 +4027,12 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -4042,16 +4027,12 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
4042fn zirAllocInferred(4027fn zirAllocInferred(
4043 sema: *Sema,4028 sema: *Sema,
4044 block: *Block,4029 block: *Block,
4045 inst: Zir.Inst.Index,
4046 is_const: bool,4030 is_const: bool,
4047) CompileError!Air.Inst.Ref {4031) CompileError!Air.Inst.Ref {
4048 const tracy = trace(@src());4032 const tracy = trace(@src());
4049 defer tracy.end();4033 defer tracy.end();
40504034
4051 const gpa = sema.gpa;4035 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;
40554036
4056 if (block.is_comptime) {4037 if (block.is_comptime) {
4057 try sema.air_instructions.append(gpa, .{4038 try sema.air_instructions.append(gpa, .{
...@@ -5428,10 +5409,11 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi...@@ -5428,10 +5409,11 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
5428 const tracy = trace(@src());5409 const tracy = trace(@src());
5429 defer tracy.end();5410 defer tracy.end();
54305411
5431 const src: LazySrcLoc = sema.src;5412 const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
5432 const bin_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;5413 const src = pl_node.src();
5433 const ptr = try sema.resolveInst(bin_inst.lhs);5414 const bin = sema.code.extraData(Zir.Inst.Bin, pl_node.payload_index).data;
5434 const operand = try sema.resolveInst(bin_inst.rhs);5415 const ptr = try sema.resolveInst(bin.lhs);
5416 const operand = try sema.resolveInst(bin.rhs);
5435 const ptr_inst = ptr.toIndex().?;5417 const ptr_inst = ptr.toIndex().?;
5436 const air_datas = sema.air_instructions.items(.data);5418 const air_datas = sema.air_instructions.items(.data);
54375419
...@@ -5496,16 +5478,6 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi...@@ -5496,16 +5478,6 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
5496 sema.branch_quota = @max(sema.branch_quota, quota);5478 sema.branch_quota = @max(sema.branch_quota, quota);
5497}5479}
54985480
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
5509fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {5481fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
5510 const tracy = trace(@src());5482 const tracy = trace(@src());
5511 defer tracy.end();5483 defer tracy.end();
...@@ -5699,17 +5671,20 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.I...@@ -5699,17 +5671,20 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.I
5699 const src = inst_data.src();5671 const src = inst_data.src();
5700 const msg_inst = try sema.resolveInst(inst_data.operand);5672 const msg_inst = try sema.resolveInst(inst_data.operand);
57015673
5674 // `panicWithMsg` would perform this coercion for us, but we can get a better
5675 // source location if we do it here.
5676 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, .{ .node_offset_builtin_call_arg0 = inst_data.src_node });
5677
5702 if (block.is_comptime) {5678 if (block.is_comptime) {
5703 return sema.fail(block, src, "encountered @panic at comptime", .{});5679 return sema.fail(block, src, "encountered @panic at comptime", .{});
5704 }5680 }
5705 try sema.panicWithMsg(block, src, msg_inst, .@"@panic");5681 try sema.panicWithMsg(block, src, coerced_msg, .@"@panic");
5706 return always_noreturn;5682 return always_noreturn;
5707}5683}
57085684
5709fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {5685fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
5710 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;5686 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
5711 const src = LazySrcLoc.nodeOffset(src_node);5687 const src = LazySrcLoc.nodeOffset(src_node);
5712 sema.src = src;
5713 if (block.is_comptime)5688 if (block.is_comptime)
5714 return sema.fail(block, src, "encountered @trap at comptime", .{});5689 return sema.fail(block, src, "encountered @trap at comptime", .{});
5715 _ = try block.addNoOp(.trap);5690 _ = try block.addNoOp(.trap);
...@@ -6384,10 +6359,6 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError...@@ -6384,10 +6359,6 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
6384}6359}
63856360
6386fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {6361fn 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.
6391 if (block.is_comptime or block.ownerModule().strip) return;6362 if (block.is_comptime or block.ownerModule().strip) return;
63926363
6393 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;6364 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
...@@ -6632,7 +6603,6 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {...@@ -6632,7 +6603,6 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
6632pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref {6603pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref {
6633 const mod = sema.mod;6604 const mod = sema.mod;
6634 const gpa = sema.gpa;6605 const gpa = sema.gpa;
6635 const src = sema.src;
66366606
6637 if (block.is_comptime or block.is_typeof) {6607 if (block.is_comptime or block.is_typeof) {
6638 const index_val = try mod.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);6608 const index_val = try mod.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
...@@ -6650,9 +6620,10 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref...@@ -6650,9 +6620,10 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
6650 else => |e| return e,6620 else => |e| return e,
6651 };6621 };
6652 const field_name = try mod.intern_pool.getOrPutString(gpa, "index");6622 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) {6623 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, .unneeded) catch |err| switch (err) {
6654 error.NeededSourceLocation, error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,6624 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.StackTrace is corrupt"),
6655 else => |e| return e,6625 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
6626 error.OutOfMemory => |e| return e,
6656 };6627 };
66576628
6658 return try block.addInst(.{6629 return try block.addInst(.{
...@@ -9900,7 +9871,6 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -9900,7 +9871,6 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
9900 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;9871 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
9901 const src = inst_data.src();9872 const src = inst_data.src();
9902 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;9873 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
9903 sema.src = src;
9904 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);9874 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);
9905}9875}
99069876
...@@ -10508,7 +10478,8 @@ fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -10508,7 +10478,8 @@ fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
10508 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };10478 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };
10509 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;10479 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
10510 const array = try sema.resolveInst(extra.lhs);10480 const array = try sema.resolveInst(extra.lhs);
10511 const elem_index = try sema.resolveInst(extra.rhs);10481 const uncoerced_elem_index = try sema.resolveInst(extra.rhs);
10482 const elem_index = try sema.coerce(block, Type.usize, uncoerced_elem_index, elem_index_src);
10512 return sema.elemVal(block, src, array, elem_index, elem_index_src, true);10483 return sema.elemVal(block, src, array, elem_index, elem_index_src, true);
10513}10484}
1051410485
...@@ -10560,7 +10531,8 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -10560,7 +10531,8 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
10560 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };10531 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };
10561 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;10532 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
10562 const array_ptr = try sema.resolveInst(extra.lhs);10533 const array_ptr = try sema.resolveInst(extra.lhs);
10563 const elem_index = try sema.resolveInst(extra.rhs);10534 const uncoerced_elem_index = try sema.resolveInst(extra.rhs);
10535 const elem_index = try sema.coerce(block, Type.usize, uncoerced_elem_index, elem_index_src);
10564 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false, true);10536 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false, true);
10565}10537}
1056610538
...@@ -10855,7 +10827,7 @@ const SwitchProngAnalysis = struct {...@@ -10855,7 +10827,7 @@ const SwitchProngAnalysis = struct {
10855 .address_space = operand_ptr_ty.ptrAddressSpace(mod),10827 .address_space = operand_ptr_ty.ptrAddressSpace(mod),
10856 },10828 },
10857 });10829 });
10858 if (try sema.resolveDefinedValue(block, sema.src, spa.operand_ptr)) |union_ptr| {10830 if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |union_ptr| {
10859 return Air.internedToRef((try mod.intern(.{ .ptr = .{10831 return Air.internedToRef((try mod.intern(.{ .ptr = .{
10860 .ty = ptr_field_ty.toIntern(),10832 .ty = ptr_field_ty.toIntern(),
10861 .addr = .{ .field = .{10833 .addr = .{ .field = .{
...@@ -10866,7 +10838,7 @@ const SwitchProngAnalysis = struct {...@@ -10866,7 +10838,7 @@ const SwitchProngAnalysis = struct {
10866 }10838 }
10867 return block.addStructFieldPtr(spa.operand_ptr, field_index, ptr_field_ty);10839 return block.addStructFieldPtr(spa.operand_ptr, field_index, ptr_field_ty);
10868 } else {10840 } else {
10869 if (try sema.resolveDefinedValue(block, sema.src, spa.operand)) |union_val| {10841 if (try sema.resolveDefinedValue(block, operand_src, spa.operand)) |union_val| {
10870 const tag_and_val = ip.indexToKey(union_val.toIntern()).un;10842 const tag_and_val = ip.indexToKey(union_val.toIntern()).un;
10871 return Air.internedToRef(tag_and_val.val);10843 return Air.internedToRef(tag_and_val.val);
10872 }10844 }
...@@ -13191,6 +13163,7 @@ fn validateErrSetSwitch(...@@ -13191,6 +13163,7 @@ fn validateErrSetSwitch(
13191 // else => |e| return e,13163 // else => |e| return e,
13192 // even if all the possible errors were already handled.13164 // even if all the possible errors were already handled.
13193 const tags = sema.code.instructions.items(.tag);13165 const tags = sema.code.instructions.items(.tag);
13166 const datas = sema.code.instructions.items(.data);
13194 for (else_case.body) |else_inst| switch (tags[@intFromEnum(else_inst)]) {13167 for (else_case.body) |else_inst| switch (tags[@intFromEnum(else_inst)]) {
13195 .dbg_block_begin,13168 .dbg_block_begin,
13196 .dbg_block_end,13169 .dbg_block_end,
...@@ -13205,11 +13178,16 @@ fn validateErrSetSwitch(...@@ -13205,11 +13178,16 @@ fn validateErrSetSwitch(
13205 .err_union_code,13178 .err_union_code,
13206 .ret_err_value_code,13179 .ret_err_value_code,
13207 .save_err_ret_index,13180 .save_err_ret_index,
13208 .restore_err_ret_index,13181 .restore_err_ret_index_unconditional,
13182 .restore_err_ret_index_fn_entry,
13209 .is_non_err,13183 .is_non_err,
13210 .ret_is_non_err,13184 .ret_is_non_err,
13211 .condbr,13185 .condbr,
13212 => {},13186 => {},
13187 .extended => switch (datas[@intFromEnum(else_inst)].extended.opcode) {
13188 .restore_err_ret_index => {},
13189 else => break,
13190 },
13213 else => break,13191 else => break,
13214 } else break :else_validation;13192 } else break :else_validation;
1321513193
...@@ -13707,7 +13685,6 @@ fn zirShl(...@@ -13707,7 +13685,6 @@ fn zirShl(
13707 const mod = sema.mod;13685 const mod = sema.mod;
13708 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;13686 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
13709 const src = inst_data.src();13687 const src = inst_data.src();
13710 sema.src = src;
13711 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };13688 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
13712 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };13689 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
13713 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;13690 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -13878,7 +13855,6 @@ fn zirShr(...@@ -13878,7 +13855,6 @@ fn zirShr(
13878 const mod = sema.mod;13855 const mod = sema.mod;
13879 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;13856 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
13880 const src = inst_data.src();13857 const src = inst_data.src();
13881 sema.src = src;
13882 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };13858 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
13883 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };13859 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
13884 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;13860 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -14014,7 +13990,6 @@ fn zirBitwise(...@@ -14014,7 +13990,6 @@ fn zirBitwise(
14014 const mod = sema.mod;13990 const mod = sema.mod;
14015 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;13991 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14016 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };13992 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14017 sema.src = src;
14018 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };13993 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
14019 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };13994 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
14020 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;13995 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -14795,21 +14770,20 @@ fn zirArithmetic(...@@ -14795,21 +14770,20 @@ fn zirArithmetic(
14795 defer tracy.end();14770 defer tracy.end();
1479614771
14797 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;14772 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14798 sema.src = .{ .node_offset_bin_op = inst_data.src_node };14773 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14799 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };14774 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
14800 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };14775 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
14801 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;14776 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
14802 const lhs = try sema.resolveInst(extra.lhs);14777 const lhs = try sema.resolveInst(extra.lhs);
14803 const rhs = try sema.resolveInst(extra.rhs);14778 const rhs = try sema.resolveInst(extra.rhs);
1480414779
14805 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, sema.src, lhs_src, rhs_src, safety);14780 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, src, lhs_src, rhs_src, safety);
14806}14781}
1480714782
14808fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {14783fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
14809 const mod = sema.mod;14784 const mod = sema.mod;
14810 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;14785 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14811 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };14786 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14812 sema.src = src;
14813 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };14787 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
14814 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };14788 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
14815 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;14789 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -14975,7 +14949,6 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -14975,7 +14949,6 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
14975 const mod = sema.mod;14949 const mod = sema.mod;
14976 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;14950 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14977 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };14951 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14978 sema.src = src;
14979 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };14952 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
14980 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };14953 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
14981 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;14954 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -15141,7 +15114,6 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15141,7 +15114,6 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15141 const mod = sema.mod;15114 const mod = sema.mod;
15142 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;15115 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
15143 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };15116 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15144 sema.src = src;
15145 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };15117 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
15146 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };15118 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
15147 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;15119 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -15252,7 +15224,6 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15252,7 +15224,6 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15252 const mod = sema.mod;15224 const mod = sema.mod;
15253 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;15225 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
15254 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };15226 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15255 sema.src = src;
15256 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };15227 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
15257 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };15228 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
15258 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;15229 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -15494,7 +15465,6 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -15494,7 +15465,6 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
15494 const mod = sema.mod;15465 const mod = sema.mod;
15495 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;15466 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
15496 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };15467 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15497 sema.src = src;
15498 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };15468 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
15499 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };15469 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
15500 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;15470 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -15679,7 +15649,6 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins...@@ -15679,7 +15649,6 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
15679 const mod = sema.mod;15649 const mod = sema.mod;
15680 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;15650 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
15681 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };15651 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15682 sema.src = src;
15683 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };15652 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
15684 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };15653 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
15685 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;15654 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -15775,7 +15744,6 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins...@@ -15775,7 +15744,6 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
15775 const mod = sema.mod;15744 const mod = sema.mod;
15776 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;15745 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
15777 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };15746 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15778 sema.src = src;
15779 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };15747 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
15780 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };15748 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
15781 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;15749 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -18741,13 +18709,18 @@ fn zirBoolBr(...@@ -18741,13 +18709,18 @@ fn zirBoolBr(
18741 defer tracy.end();18709 defer tracy.end();
1874218710
18743 const mod = sema.mod;18711 const mod = sema.mod;
18712 const gpa = sema.gpa;
18713
18744 const datas = sema.code.instructions.items(.data);18714 const datas = sema.code.instructions.items(.data);
18745 const inst_data = datas[@intFromEnum(inst)].bool_br;18715 const inst_data = datas[@intFromEnum(inst)].pl_node;
18746 const lhs = try sema.resolveInst(inst_data.lhs);18716 const extra = sema.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
18747 const lhs_src = sema.src;18717
18748 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);18718 const uncoerced_lhs = try sema.resolveInst(extra.data.lhs);
18749 const body = sema.code.bodySlice(extra.end, extra.data.body_len);18719 const body = sema.code.bodySlice(extra.end, extra.data.body_len);
18750 const gpa = sema.gpa;18720 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
18721 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
18722
18723 const lhs = try sema.coerce(parent_block, Type.bool, uncoerced_lhs, lhs_src);
1875118724
18752 if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| {18725 if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| {
18753 if (is_bool_or and lhs_val.toBool()) {18726 if (is_bool_or and lhs_val.toBool()) {
...@@ -18758,7 +18731,11 @@ fn zirBoolBr(...@@ -18758,7 +18731,11 @@ fn zirBoolBr(
18758 // comptime-known left-hand side. No need for a block here; the result18731 // comptime-known left-hand side. No need for a block here; the result
18759 // is simply the rhs expression. Here we rely on there only being 118732 // is simply the rhs expression. Here we rely on there only being 1
18760 // break instruction (`break_inline`).18733 // break instruction (`break_inline`).
18761 return sema.resolveBody(parent_block, body, inst);18734 const rhs_result = try sema.resolveBody(parent_block, body, inst);
18735 if (sema.typeOf(rhs_result).isNoReturn(mod)) {
18736 return rhs_result;
18737 }
18738 return sema.coerce(parent_block, Type.bool, rhs_result, rhs_src);
18762 }18739 }
1876318740
18764 const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);18741 const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
...@@ -18789,13 +18766,16 @@ fn zirBoolBr(...@@ -18789,13 +18766,16 @@ fn zirBoolBr(
18789 _ = try lhs_block.addBr(block_inst, lhs_result);18766 _ = try lhs_block.addBr(block_inst, lhs_result);
1879018767
18791 const rhs_result = try sema.resolveBody(rhs_block, body, inst);18768 const rhs_result = try sema.resolveBody(rhs_block, body, inst);
18792 if (!sema.typeOf(rhs_result).isNoReturn(mod)) {18769 const rhs_noret = sema.typeOf(rhs_result).isNoReturn(mod);
18793 _ = try rhs_block.addBr(block_inst, rhs_result);18770 const coerced_rhs_result = if (!rhs_noret) rhs: {
18794 }18771 const coerced_result = try sema.coerce(rhs_block, Type.bool, rhs_result, rhs_src);
18772 _ = try rhs_block.addBr(block_inst, coerced_result);
18773 break :rhs coerced_result;
18774 } else rhs_result;
1879518775
18796 const result = sema.finishCondBr(parent_block, &child_block, &then_block, &else_block, lhs, block_inst);18776 const result = sema.finishCondBr(parent_block, &child_block, &then_block, &else_block, lhs, block_inst);
18797 if (!sema.typeOf(rhs_result).isNoReturn(mod)) {18777 if (!rhs_noret) {
18798 if (try sema.resolveDefinedValue(rhs_block, sema.src, rhs_result)) |rhs_val| {18778 if (try sema.resolveDefinedValue(rhs_block, rhs_src, coerced_rhs_result)) |rhs_val| {
18799 if (is_bool_or and rhs_val.toBool()) {18779 if (is_bool_or and rhs_val.toBool()) {
18800 return .bool_true;18780 return .bool_true;
18801 } else if (!is_bool_or and !rhs_val.toBool()) {18781 } else if (!is_bool_or and !rhs_val.toBool()) {
...@@ -19206,7 +19186,7 @@ fn zirRetErrValue(...@@ -19206,7 +19186,7 @@ fn zirRetErrValue(
19206 .ty = error_set_type.toIntern(),19186 .ty = error_set_type.toIntern(),
19207 .name = err_name,19187 .name = err_name,
19208 } })));19188 } })));
19209 return sema.analyzeRet(block, result_inst, src);19189 return sema.analyzeRet(block, result_inst, src, src);
19210}19190}
1921119191
19212fn zirRetImplicit(19192fn zirRetImplicit(
...@@ -19256,7 +19236,7 @@ fn zirRetImplicit(...@@ -19256,7 +19236,7 @@ fn zirRetImplicit(
19256 return sema.failWithOwnedErrorMsg(block, msg);19236 return sema.failWithOwnedErrorMsg(block, msg);
19257 }19237 }
1925819238
19259 return sema.analyzeRet(block, operand, r_brace_src);19239 return sema.analyzeRet(block, operand, r_brace_src, r_brace_src);
19260}19240}
1926119241
19262fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {19242fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
...@@ -19267,7 +19247,7 @@ fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir...@@ -19267,7 +19247,7 @@ fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
19267 const operand = try sema.resolveInst(inst_data.operand);19247 const operand = try sema.resolveInst(inst_data.operand);
19268 const src = inst_data.src();19248 const src = inst_data.src();
1926919249
19270 return sema.analyzeRet(block, operand, src);19250 return sema.analyzeRet(block, operand, src, .{ .node_offset_return_operand = inst_data.src_node });
19271}19251}
1927219252
19273fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {19253fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
...@@ -19280,7 +19260,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir...@@ -19280,7 +19260,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
1928019260
19281 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {19261 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {
19282 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);19262 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
19283 return sema.analyzeRet(block, operand, src);19263 return sema.analyzeRet(block, operand, src, .{ .node_offset_return_operand = inst_data.src_node });
19284 }19264 }
1928519265
19286 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {19266 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
...@@ -19375,17 +19355,21 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE...@@ -19375,17 +19355,21 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
19375 block.error_return_trace_index = try sema.analyzeSaveErrRetIndex(block);19355 block.error_return_trace_index = try sema.analyzeSaveErrRetIndex(block);
19376}19356}
1937719357
19378fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {19358fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
19379 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;19359 const extra = sema.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
19380 const src = sema.src; // TODO19360 return sema.restoreErrRetIndex(start_block, extra.src(), extra.block, extra.operand);
1938119361}
19382 const mod = sema.mod;
19383 const ip = &mod.intern_pool;
1938419362
19363/// If `operand` is non-error (or is `none`), restores the error return trace to
19364/// its state at the point `block` was reached (or, if `block` is `none`, the
19365/// point this function began execution).
19366fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_block: Zir.Inst.Ref, operand_zir: Zir.Inst.Ref) CompileError!void {
19385 const tracy = trace(@src());19367 const tracy = trace(@src());
19386 defer tracy.end();19368 defer tracy.end();
1938719369
19388 const saved_index = if (inst_data.block.toIndexAllowNone()) |zir_block| b: {19370 const mod = sema.mod;
19371
19372 const saved_index = if (target_block.toIndexAllowNone()) |zir_block| b: {
19389 var block = start_block;19373 var block = start_block;
19390 while (true) {19374 while (true) {
19391 if (block.label) |label| {19375 if (block.label) |label| {
...@@ -19409,7 +19393,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)...@@ -19409,7 +19393,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
19409 return; // No need to restore19393 return; // No need to restore
19410 };19394 };
1941119395
19412 const operand = try sema.resolveInstAllowNone(inst_data.operand);19396 const operand = try sema.resolveInstAllowNone(operand_zir);
1941319397
19414 if (start_block.is_comptime or start_block.is_typeof) {19398 if (start_block.is_comptime or start_block.is_typeof) {
19415 const is_non_error = if (operand != .none) blk: {19399 const is_non_error = if (operand != .none) blk: {
...@@ -19427,7 +19411,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)...@@ -19427,7 +19411,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
19427 return;19411 return;
19428 }19412 }
1942919413
19430 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;19414 if (!mod.intern_pool.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
19431 if (!start_block.ownerModule().error_tracing) return;19415 if (!start_block.ownerModule().error_tracing) return;
1943219416
19433 assert(saved_index != .none); // The .error_return_trace_index field was dropped somewhere19417 assert(saved_index != .none); // The .error_return_trace_index field was dropped somewhere
...@@ -19470,6 +19454,7 @@ fn analyzeRet(...@@ -19470,6 +19454,7 @@ fn analyzeRet(
19470 block: *Block,19454 block: *Block,
19471 uncasted_operand: Air.Inst.Ref,19455 uncasted_operand: Air.Inst.Ref,
19472 src: LazySrcLoc,19456 src: LazySrcLoc,
19457 operand_src: LazySrcLoc,
19473) CompileError!Zir.Inst.Index {19458) CompileError!Zir.Inst.Index {
19474 // Special case for returning an error to an inferred error set; we need to19459 // Special case for returning an error to an inferred error set; we need to
19475 // add the error tag to the inferred error set of the in-scope function, so19460 // add the error tag to the inferred error set of the in-scope function, so
...@@ -19478,14 +19463,14 @@ fn analyzeRet(...@@ -19478,14 +19463,14 @@ fn analyzeRet(
19478 if (sema.fn_ret_ty_ies != null and sema.fn_ret_ty.zigTypeTag(mod) == .ErrorUnion) {19463 if (sema.fn_ret_ty_ies != null and sema.fn_ret_ty.zigTypeTag(mod) == .ErrorUnion) {
19479 try sema.addToInferredErrorSet(uncasted_operand);19464 try sema.addToInferredErrorSet(uncasted_operand);
19480 }19465 }
19481 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, .{ .is_ret = true }) catch |err| switch (err) {19466 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, operand_src, .{ .is_ret = true }) catch |err| switch (err) {
19482 error.NotCoercible => unreachable,19467 error.NotCoercible => unreachable,
19483 else => |e| return e,19468 else => |e| return e,
19484 };19469 };
1948519470
19486 if (block.inlining) |inlining| {19471 if (block.inlining) |inlining| {
19487 if (block.is_comptime) {19472 if (block.is_comptime) {
19488 const ret_val = try sema.resolveConstValue(block, src, operand, .{19473 const ret_val = try sema.resolveConstValue(block, operand_src, operand, .{
19489 .needed_comptime_reason = "value being returned at comptime must be comptime-known",19474 .needed_comptime_reason = "value being returned at comptime must be comptime-known",
19490 });19475 });
19491 inlining.comptime_result = operand;19476 inlining.comptime_result = operand;
...@@ -19520,7 +19505,7 @@ fn analyzeRet(...@@ -19520,7 +19505,7 @@ fn analyzeRet(
19520 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {19505 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
19521 // Avoid adding a frame to the error return trace in case the value is comptime-known19506 // Avoid adding a frame to the error return trace in case the value is comptime-known
19522 // to be not an error.19507 // to be not an error.
19523 const is_non_err = try sema.analyzeIsNonErr(block, src, operand);19508 const is_non_err = try sema.analyzeIsNonErr(block, operand_src, operand);
19524 return sema.retWithErrTracing(block, src, is_non_err, air_tag, operand);19509 return sema.retWithErrTracing(block, src, is_non_err, air_tag, operand);
19525 }19510 }
1952619511
...@@ -20768,8 +20753,9 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -20768,8 +20753,9 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2076820753
20769fn zirErrorName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {20754fn zirErrorName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
20770 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;20755 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
20771 const operand = try sema.resolveInst(inst_data.operand);
20772 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };20756 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
20757 const uncoerced_operand = try sema.resolveInst(inst_data.operand);
20758 const operand = try sema.coerce(block, Type.anyerror, uncoerced_operand, operand_src);
2077320759
20774 if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| {20760 if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| {
20775 const err_name = sema.mod.intern_pool.indexToKey(val.toIntern()).err.name;20761 const err_name = sema.mod.intern_pool.indexToKey(val.toIntern()).err.name;
...@@ -23161,7 +23147,6 @@ fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -23161,7 +23147,6 @@ fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
23161fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 {23147fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 {
23162 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;23148 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
23163 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };23149 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
23164 sema.src = src;
23165 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };23150 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
23166 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };23151 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
23167 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;23152 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
...@@ -25394,15 +25379,21 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25394,15 +25379,21 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25394 } else if (extra.data.bits.has_align_ref) blk: {25379 } else if (extra.data.bits.has_align_ref) blk: {
25395 const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);25380 const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
25396 extra_index += 1;25381 extra_index += 1;
25397 const align_tv = sema.resolveInstConst(block, align_src, align_ref, .{25382 const uncoerced_align = sema.resolveInst(align_ref) catch |err| switch (err) {
25383 error.GenericPoison => break :blk null,
25384 else => |e| return e,
25385 };
25386 const coerced_align = sema.coerce(block, Type.u29, uncoerced_align, align_src) catch |err| switch (err) {
25387 error.GenericPoison => break :blk null,
25388 else => |e| return e,
25389 };
25390 const align_val = sema.resolveConstDefinedValue(block, align_src, coerced_align, .{
25398 .needed_comptime_reason = "alignment must be comptime-known",25391 .needed_comptime_reason = "alignment must be comptime-known",
25399 }) catch |err| switch (err) {25392 }) catch |err| switch (err) {
25400 error.GenericPoison => {25393 error.GenericPoison => break :blk null,
25401 break :blk null;
25402 },
25403 else => |e| return e,25394 else => |e| return e,
25404 };25395 };
25405 const alignment = try sema.validateAlignAllowZero(block, align_src, try align_tv.val.toUnsignedIntAdvanced(sema));25396 const alignment = try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntAdvanced(sema));
25406 const default = target_util.defaultFunctionAlignment(target);25397 const default = target_util.defaultFunctionAlignment(target);
25407 break :blk if (alignment == default) .none else alignment;25398 break :blk if (alignment == default) .none else alignment;
25408 } else .none;25399 } else .none;
...@@ -25413,7 +25404,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25413,7 +25404,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25413 const body = sema.code.bodySlice(extra_index, body_len);25404 const body = sema.code.bodySlice(extra_index, body_len);
25414 extra_index += body.len;25405 extra_index += body.len;
2541525406
25416 const addrspace_ty = try sema.getBuiltinType("AddressSpace");25407 const addrspace_ty = Type.fromInterned(.address_space_type);
25417 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, .{25408 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, .{
25418 .needed_comptime_reason = "addrspace must be comptime-known",25409 .needed_comptime_reason = "addrspace must be comptime-known",
25419 });25410 });
...@@ -25424,15 +25415,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25424,15 +25415,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25424 } else if (extra.data.bits.has_addrspace_ref) blk: {25415 } else if (extra.data.bits.has_addrspace_ref) blk: {
25425 const addrspace_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);25416 const addrspace_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
25426 extra_index += 1;25417 extra_index += 1;
25427 const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref, .{25418 const addrspace_ty = Type.fromInterned(.address_space_type);
25419 const uncoerced_addrspace = sema.resolveInst(addrspace_ref) catch |err| switch (err) {
25420 error.GenericPoison => break :blk null,
25421 else => |e| return e,
25422 };
25423 const coerced_addrspace = sema.coerce(block, addrspace_ty, uncoerced_addrspace, addrspace_src) catch |err| switch (err) {
25424 error.GenericPoison => break :blk null,
25425 else => |e| return e,
25426 };
25427 const addrspace_val = sema.resolveConstDefinedValue(block, addrspace_src, coerced_addrspace, .{
25428 .needed_comptime_reason = "addrspace must be comptime-known",25428 .needed_comptime_reason = "addrspace must be comptime-known",
25429 }) catch |err| switch (err) {25429 }) catch |err| switch (err) {
25430 error.GenericPoison => {25430 error.GenericPoison => break :blk null,
25431 break :blk null;
25432 },
25433 else => |e| return e,25431 else => |e| return e,
25434 };25432 };
25435 break :blk mod.toEnum(std.builtin.AddressSpace, addrspace_tv.val);25433 break :blk mod.toEnum(std.builtin.AddressSpace, addrspace_val);
25436 } else target_util.defaultAddressSpace(target, .function);25434 } else target_util.defaultAddressSpace(target, .function);
2543725435
25438 const section: Section = if (extra.data.bits.has_section_body) blk: {25436 const section: Section = if (extra.data.bits.has_section_body) blk: {
...@@ -25480,15 +25478,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25480,15 +25478,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25480 } else if (extra.data.bits.has_cc_ref) blk: {25478 } else if (extra.data.bits.has_cc_ref) blk: {
25481 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);25479 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
25482 extra_index += 1;25480 extra_index += 1;
25483 const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref, .{25481 const cc_ty = Type.fromInterned(.calling_convention_type);
25482 const uncoerced_cc = sema.resolveInst(cc_ref) catch |err| switch (err) {
25483 error.GenericPoison => break :blk null,
25484 else => |e| return e,
25485 };
25486 const coerced_cc = sema.coerce(block, cc_ty, uncoerced_cc, cc_src) catch |err| switch (err) {
25487 error.GenericPoison => break :blk null,
25488 else => |e| return e,
25489 };
25490 const cc_val = sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{
25484 .needed_comptime_reason = "calling convention must be comptime-known",25491 .needed_comptime_reason = "calling convention must be comptime-known",
25485 }) catch |err| switch (err) {25492 }) catch |err| switch (err) {
25486 error.GenericPoison => {25493 error.GenericPoison => break :blk null,
25487 break :blk null;
25488 },
25489 else => |e| return e,25494 else => |e| return e,
25490 };25495 };
25491 break :blk mod.toEnum(std.builtin.CallingConvention, cc_tv.val);25496 break :blk mod.toEnum(std.builtin.CallingConvention, cc_val);
25492 } else if (sema.owner_decl.is_exported and has_body)25497 } else if (sema.owner_decl.is_exported and has_body)
25493 .C25498 .C
25494 else25499 else
...@@ -26416,12 +26421,16 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP...@@ -26416,12 +26421,16 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
26416 try sema.prepareSimplePanic(block);26421 try sema.prepareSimplePanic(block);
2641726422
26418 const panic_messages_ty = try sema.getBuiltinType("panic_messages");26423 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
26419 const msg_decl_index = (try sema.namespaceLookup(26424 const msg_decl_index = (sema.namespaceLookup(
26420 block,26425 block,
26421 sema.src,26426 .unneeded,
26422 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,26427 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
26423 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),26428 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
26424 )).?;26429 ) catch |err| switch (err) {
26430 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
26431 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
26432 error.OutOfMemory => |e| return e,
26433 }).?;
26425 try sema.ensureDeclAnalyzed(msg_decl_index);26434 try sema.ensureDeclAnalyzed(msg_decl_index);
26426 mod.panic_messages[@intFromEnum(panic_id)] = msg_decl_index.toOptional();26435 mod.panic_messages[@intFromEnum(panic_id)] = msg_decl_index.toOptional();
26427 return msg_decl_index;26436 return msg_decl_index;
...@@ -35645,7 +35654,6 @@ fn semaBackingIntType(mod: *Module, struct_type: InternPool.Key.StructType) Comp...@@ -35645,7 +35654,6 @@ fn semaBackingIntType(mod: *Module, struct_type: InternPool.Key.StructType) Comp
3564535654
35646 if (small.has_backing_int) {35655 if (small.has_backing_int) {
35647 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;35656 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
35648 extra_index += @intFromBool(small.has_src_node);
35649 extra_index += @intFromBool(small.has_fields_len);35657 extra_index += @intFromBool(small.has_fields_len);
35650 extra_index += @intFromBool(small.has_decls_len);35658 extra_index += @intFromBool(small.has_decls_len);
3565135659
...@@ -36357,8 +36365,6 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {...@@ -36357,8 +36365,6 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
36357 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);36365 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
36358 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;36366 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
3635936367
36360 extra_index += @intFromBool(small.has_src_node);
36361
36362 const fields_len = if (small.has_fields_len) blk: {36368 const fields_len = if (small.has_fields_len) blk: {
36363 const fields_len = zir.extra[extra_index];36369 const fields_len = zir.extra[extra_index];
36364 extra_index += 1;36370 extra_index += 1;
...@@ -36826,7 +36832,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un...@@ -36826,7 +36832,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
36826 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;36832 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3682736833
36828 const src = LazySrcLoc.nodeOffset(0);36834 const src = LazySrcLoc.nodeOffset(0);
36829 extra_index += @intFromBool(small.has_src_node);
3683036835
36831 const tag_type_ref: Zir.Inst.Ref = if (small.has_tag_type) blk: {36836 const tag_type_ref: Zir.Inst.Ref = if (small.has_tag_type) blk: {
36832 const ty_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);36837 const ty_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
src/Zir.zig+108-77
...@@ -303,11 +303,11 @@ pub const Inst = struct {...@@ -303,11 +303,11 @@ pub const Inst = struct {
303 bool_not,303 bool_not,
304 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand304 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
305 /// is a block, which is evaluated if `lhs` is `true`.305 /// 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`.
307 bool_br_and,307 bool_br_and,
308 /// Short-circuiting boolean `or`. `lhs` is a boolean `Ref` and the other operand308 /// Short-circuiting boolean `or`. `lhs` is a boolean `Ref` and the other operand
309 /// is a block, which is evaluated if `lhs` is `false`.309 /// 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`.
311 bool_br_or,311 bool_br_or,
312 /// Return a value from a block.312 /// Return a value from a block.
313 /// Uses the `break` union field.313 /// Uses the `break` union field.
...@@ -592,16 +592,12 @@ pub const Inst = struct {...@@ -592,16 +592,12 @@ pub const Inst = struct {
592 /// Returns a pointer to the subslice.592 /// Returns a pointer to the subslice.
593 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`.593 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`.
594 slice_length,594 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,
599 /// Same as `store` except provides a source location.595 /// Same as `store` except provides a source location.
600 /// Uses the `pl_node` union field. Payload is `Bin`.596 /// Uses the `pl_node` union field. Payload is `Bin`.
601 store_node,597 store_node,
602 /// Same as `store` but the type of the value being stored will be used to infer598 /// Same as `store_node` but the type of the value being stored will be
603 /// the pointer type.599 /// used to infer the pointer type of an `alloc_inferred`.
604 /// Uses the `bin` union field.600 /// Uses the `pl_node` union field. Payload is `Bin`.
605 store_to_inferred_ptr,601 store_to_inferred_ptr,
606 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.602 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
607 /// Uses the `str` union field.603 /// Uses the `str` union field.
...@@ -1036,10 +1032,18 @@ pub const Inst = struct {...@@ -1036,10 +1032,18 @@ pub const Inst = struct {
1036 /// block, if the operand is .none or of an error/error-union type.1032 /// block, if the operand is .none or of an error/error-union type.
1037 /// Uses the `save_err_ret_index` field.1033 /// Uses the `save_err_ret_index` field.
1038 save_err_ret_index,1034 save_err_ret_index,
1039 /// Sets error return trace to zero if no operand is given,1035 /// Specialized form of `Extended.restore_err_ret_index`.
1040 /// otherwise sets the value to the given amount.1036 /// Unconditionally restores the error return index to its last saved state
1041 /// Uses the `restore_err_ret_index` union field.1037 /// in the block referred to by `operand`. If `operand` is `none`, restores
1042 restore_err_ret_index,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
1044 /// The ZIR instruction tag is one of the `Extended` ones.1048 /// The ZIR instruction tag is one of the `Extended` ones.
1045 /// Uses the `extended` union field.1049 /// Uses the `extended` union field.
...@@ -1145,7 +1149,6 @@ pub const Inst = struct {...@@ -1145,7 +1149,6 @@ pub const Inst = struct {
1145 .shl,1149 .shl,
1146 .shl_sat,1150 .shl_sat,
1147 .shr,1151 .shr,
1148 .store,
1149 .store_node,1152 .store_node,
1150 .store_to_inferred_ptr,1153 .store_to_inferred_ptr,
1151 .str,1154 .str,
...@@ -1265,7 +1268,6 @@ pub const Inst = struct {...@@ -1265,7 +1268,6 @@ pub const Inst = struct {
1265 .@"defer",1268 .@"defer",
1266 .defer_err_code,1269 .defer_err_code,
1267 .save_err_ret_index,1270 .save_err_ret_index,
1268 .restore_err_ret_index,
1269 .for_len,1271 .for_len,
1270 .opt_eu_base_ptr_init,1272 .opt_eu_base_ptr_init,
1271 .coerce_ptr_elem_ty,1273 .coerce_ptr_elem_ty,
...@@ -1290,6 +1292,8 @@ pub const Inst = struct {...@@ -1290,6 +1292,8 @@ pub const Inst = struct {
1290 .array_init_elem_type,1292 .array_init_elem_type,
1291 .array_init_elem_ptr,1293 .array_init_elem_ptr,
1292 .validate_ref_ty,1294 .validate_ref_ty,
1295 .restore_err_ret_index_unconditional,
1296 .restore_err_ret_index_fn_entry,
1293 => false,1297 => false,
12941298
1295 .@"break",1299 .@"break",
...@@ -1338,7 +1342,6 @@ pub const Inst = struct {...@@ -1338,7 +1342,6 @@ pub const Inst = struct {
1338 .ensure_err_union_payload_void,1342 .ensure_err_union_payload_void,
1339 .set_eval_branch_quota,1343 .set_eval_branch_quota,
1340 .atomic_store,1344 .atomic_store,
1341 .store,
1342 .store_node,1345 .store_node,
1343 .store_to_inferred_ptr,1346 .store_to_inferred_ptr,
1344 .resolve_inferred_alloc,1347 .resolve_inferred_alloc,
...@@ -1352,8 +1355,9 @@ pub const Inst = struct {...@@ -1352,8 +1355,9 @@ pub const Inst = struct {
1352 .check_comptime_control_flow,1355 .check_comptime_control_flow,
1353 .@"defer",1356 .@"defer",
1354 .defer_err_code,1357 .defer_err_code,
1355 .restore_err_ret_index,
1356 .save_err_ret_index,1358 .save_err_ret_index,
1359 .restore_err_ret_index_unconditional,
1360 .restore_err_ret_index_fn_entry,
1357 .validate_struct_init_ty,1361 .validate_struct_init_ty,
1358 .validate_struct_init_result_ty,1362 .validate_struct_init_result_ty,
1359 .validate_ptr_struct_init,1363 .validate_ptr_struct_init,
...@@ -1635,8 +1639,8 @@ pub const Inst = struct {...@@ -1635,8 +1639,8 @@ pub const Inst = struct {
1635 .declaration = .pl_node,1639 .declaration = .pl_node,
1636 .suspend_block = .pl_node,1640 .suspend_block = .pl_node,
1637 .bool_not = .un_node,1641 .bool_not = .un_node,
1638 .bool_br_and = .bool_br,1642 .bool_br_and = .pl_node,
1639 .bool_br_or = .bool_br,1643 .bool_br_or = .pl_node,
1640 .@"break" = .@"break",1644 .@"break" = .@"break",
1641 .break_inline = .@"break",1645 .break_inline = .@"break",
1642 .check_comptime_control_flow = .un_node,1646 .check_comptime_control_flow = .un_node,
...@@ -1713,9 +1717,8 @@ pub const Inst = struct {...@@ -1713,9 +1717,8 @@ pub const Inst = struct {
1713 .slice_end = .pl_node,1717 .slice_end = .pl_node,
1714 .slice_sentinel = .pl_node,1718 .slice_sentinel = .pl_node,
1715 .slice_length = .pl_node,1719 .slice_length = .pl_node,
1716 .store = .bin,
1717 .store_node = .pl_node,1720 .store_node = .pl_node,
1718 .store_to_inferred_ptr = .bin,1721 .store_to_inferred_ptr = .pl_node,
1719 .str = .str,1722 .str = .str,
1720 .negate = .un_node,1723 .negate = .un_node,
1721 .negate_wrap = .un_node,1724 .negate_wrap = .un_node,
...@@ -1845,7 +1848,8 @@ pub const Inst = struct {...@@ -1845,7 +1848,8 @@ pub const Inst = struct {
1845 .defer_err_code = .defer_err_code,1848 .defer_err_code = .defer_err_code,
18461849
1847 .save_err_ret_index = .save_err_ret_index,1850 .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
1850 .struct_init_empty = .un_node,1854 .struct_init_empty = .un_node,
1851 .struct_init_empty_result = .un_node,1855 .struct_init_empty_result = .un_node,
...@@ -2075,6 +2079,13 @@ pub const Inst = struct {...@@ -2075,6 +2079,13 @@ pub const Inst = struct {
2075 /// Implements the `@inComptime` builtin.2079 /// Implements the `@inComptime` builtin.
2076 /// `operand` is `src_node: i32`.2080 /// `operand` is `src_node: i32`.
2077 in_comptime,2081 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,
2078 /// Used as a placeholder instruction which is just a dummy index for Sema to replace2089 /// Used as a placeholder instruction which is just a dummy index for Sema to replace
2079 /// with a specific value. For instance, this is used for the capture of an `errdefer`.2090 /// with a specific value. For instance, this is used for the capture of an `errdefer`.
2080 /// This should never appear in a body.2091 /// This should never appear in a body.
...@@ -2345,11 +2356,6 @@ pub const Inst = struct {...@@ -2345,11 +2356,6 @@ pub const Inst = struct {
2345 return LazySrcLoc.nodeOffset(self.src_node);2356 return LazySrcLoc.nodeOffset(self.src_node);
2346 }2357 }
2347 },2358 },
2348 bool_br: struct {
2349 lhs: Ref,
2350 /// Points to a `Block`.
2351 payload_index: u32,
2352 },
2353 @"unreachable": struct {2359 @"unreachable": struct {
2354 /// Offset from Decl AST node index.2360 /// Offset from Decl AST node index.
2355 /// `Tag` determines which kind of AST node this points to.2361 /// `Tag` determines which kind of AST node this points to.
...@@ -2396,10 +2402,6 @@ pub const Inst = struct {...@@ -2396,10 +2402,6 @@ pub const Inst = struct {
2396 save_err_ret_index: struct {2402 save_err_ret_index: struct {
2397 operand: Ref, // If error type (or .none), save new trace index2403 operand: Ref, // If error type (or .none), save new trace index
2398 },2404 },
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 },
2403 elem_val_imm: struct {2405 elem_val_imm: struct {
2404 /// The indexable value being accessed.2406 /// The indexable value being accessed.
2405 operand: Ref,2407 operand: Ref,
...@@ -2435,7 +2437,6 @@ pub const Inst = struct {...@@ -2435,7 +2437,6 @@ pub const Inst = struct {
2435 float,2437 float,
2436 ptr_type,2438 ptr_type,
2437 int_type,2439 int_type,
2438 bool_br,
2439 @"unreachable",2440 @"unreachable",
2440 @"break",2441 @"break",
2441 dbg_stmt,2442 dbg_stmt,
...@@ -2444,7 +2445,6 @@ pub const Inst = struct {...@@ -2444,7 +2445,6 @@ pub const Inst = struct {
2444 @"defer",2445 @"defer",
2445 defer_err_code,2446 defer_err_code,
2446 save_err_ret_index,2447 save_err_ret_index,
2447 restore_err_ret_index,
2448 elem_val_imm,2448 elem_val_imm,
2449 };2449 };
2450 };2450 };
...@@ -2630,6 +2630,13 @@ pub const Inst = struct {...@@ -2630,6 +2630,13 @@ pub const Inst = struct {
2630 body_len: u32,2630 body_len: u32,
2631 };2631 };
26322632
2633 /// Trailing:
2634 /// * inst: Index // for each `body_len`
2635 pub const BoolBr = struct {
2636 lhs: Ref,
2637 body_len: u32,
2638 };
2639
2633 /// Trailing:2640 /// Trailing:
2634 /// 0. doc_comment: u32 // if `has_doc_comment`; null-terminated string index2641 /// 0. doc_comment: u32 // if `has_doc_comment`; null-terminated string index
2635 /// 1. align_body_len: u32 // if `has_align_linksection_addrspace`; 0 means no `align`2642 /// 1. align_body_len: u32 // if `has_align_linksection_addrspace`; 0 means no `align`
...@@ -3015,20 +3022,19 @@ pub const Inst = struct {...@@ -3015,20 +3022,19 @@ pub const Inst = struct {
3015 };3022 };
30163023
3017 /// Trailing:3024 /// Trailing:
3018 /// 0. src_node: i32, // if has_src_node3025 /// 0. fields_len: u32, // if has_fields_len
3019 /// 1. fields_len: u32, // if has_fields_len3026 /// 1. decls_len: u32, // if has_decls_len
3020 /// 2. decls_len: u32, // if has_decls_len3027 /// 2. backing_int_body_len: u32, // if has_backing_int
3021 /// 3. backing_int_body_len: u32, // if has_backing_int3028 /// 3. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 0
3022 /// 4. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 03029 /// 4. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 0
3023 /// 5. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 03030 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3024 /// 6. decl: Index, // for every decls_len; points to a `declaration` instruction3031 /// 6. flags: u32 // for every 8 fields
3025 /// 7. flags: u32 // for every 8 fields
3026 /// - sets of 4 bits:3032 /// - sets of 4 bits:
3027 /// 0b000X: whether corresponding field has an align expression3033 /// 0b000X: whether corresponding field has an align expression
3028 /// 0b00X0: whether corresponding field has a default expression3034 /// 0b00X0: whether corresponding field has a default expression
3029 /// 0b0X00: whether corresponding field is comptime3035 /// 0b0X00: whether corresponding field is comptime
3030 /// 0bX000: whether corresponding field has a type expression3036 /// 0bX000: whether corresponding field has a type expression
3031 /// 8. fields: { // for every fields_len3037 /// 7. fields: { // for every fields_len
3032 /// field_name: u32, // if !is_tuple3038 /// field_name: u32, // if !is_tuple
3033 /// doc_comment: NullTerminatedString, // .empty if no doc comment3039 /// doc_comment: NullTerminatedString, // .empty if no doc comment
3034 /// field_type: Ref, // if corresponding bit is not set. none means anytype.3040 /// field_type: Ref, // if corresponding bit is not set. none means anytype.
...@@ -3036,7 +3042,7 @@ pub const Inst = struct {...@@ -3036,7 +3042,7 @@ pub const Inst = struct {
3036 /// align_body_len: u32, // if corresponding bit is set3042 /// align_body_len: u32, // if corresponding bit is set
3037 /// init_body_len: u32, // if corresponding bit is set3043 /// init_body_len: u32, // if corresponding bit is set
3038 /// }3044 /// }
3039 /// 10. bodies: { // for every fields_len3045 /// 8. bodies: { // for every fields_len
3040 /// field_type_body_inst: Inst, // for each field_type_body_len3046 /// field_type_body_inst: Inst, // for each field_type_body_len
3041 /// align_body_inst: Inst, // for each align_body_len3047 /// align_body_inst: Inst, // for each align_body_len
3042 /// init_body_inst: Inst, // for each init_body_len3048 /// init_body_inst: Inst, // for each init_body_len
...@@ -3048,8 +3054,13 @@ pub const Inst = struct {...@@ -3048,8 +3054,13 @@ pub const Inst = struct {
3048 fields_hash_1: u32,3054 fields_hash_1: u32,
3049 fields_hash_2: u32,3055 fields_hash_2: u32,
3050 fields_hash_3: u32,3056 fields_hash_3: u32,
3057 src_node: i32,
3058
3059 pub fn src(self: StructDecl) LazySrcLoc {
3060 return LazySrcLoc.nodeOffset(self.src_node);
3061 }
3062
3051 pub const Small = packed struct {3063 pub const Small = packed struct {
3052 has_src_node: bool,
3053 has_fields_len: bool,3064 has_fields_len: bool,
3054 has_decls_len: bool,3065 has_decls_len: bool,
3055 has_backing_int: bool,3066 has_backing_int: bool,
...@@ -3061,7 +3072,7 @@ pub const Inst = struct {...@@ -3061,7 +3072,7 @@ pub const Inst = struct {
3061 any_default_inits: bool,3072 any_default_inits: bool,
3062 any_comptime_fields: bool,3073 any_comptime_fields: bool,
3063 any_aligned_fields: bool,3074 any_aligned_fields: bool,
3064 _: u2 = undefined,3075 _: u3 = undefined,
3065 };3076 };
3066 };3077 };
30673078
...@@ -3095,16 +3106,15 @@ pub const Inst = struct {...@@ -3095,16 +3106,15 @@ pub const Inst = struct {
3095 };3106 };
30963107
3097 /// Trailing:3108 /// Trailing:
3098 /// 0. src_node: i32, // if has_src_node3109 /// 0. tag_type: Ref, // if has_tag_type
3099 /// 1. tag_type: Ref, // if has_tag_type3110 /// 1. body_len: u32, // if has_body_len
3100 /// 2. body_len: u32, // if has_body_len3111 /// 2. fields_len: u32, // if has_fields_len
3101 /// 3. fields_len: u32, // if has_fields_len3112 /// 3. decls_len: u32, // if has_decls_len
3102 /// 4. decls_len: u32, // if has_decls_len3113 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3103 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction3114 /// 5. inst: Index // for every body_len
3104 /// 6. inst: Index // for every body_len3115 /// 6. has_bits: u32 // for every 32 fields
3105 /// 7. has_bits: u32 // for every 32 fields
3106 /// - the bit is whether corresponding field has an value expression3116 /// - the bit is whether corresponding field has an value expression
3107 /// 8. fields: { // for every fields_len3117 /// 7. fields: { // for every fields_len
3108 /// field_name: u32,3118 /// field_name: u32,
3109 /// doc_comment: u32, // .empty if no doc_comment3119 /// doc_comment: u32, // .empty if no doc_comment
3110 /// value: Ref, // if corresponding bit is set3120 /// value: Ref, // if corresponding bit is set
...@@ -3116,33 +3126,37 @@ pub const Inst = struct {...@@ -3116,33 +3126,37 @@ pub const Inst = struct {
3116 fields_hash_1: u32,3126 fields_hash_1: u32,
3117 fields_hash_2: u32,3127 fields_hash_2: u32,
3118 fields_hash_3: u32,3128 fields_hash_3: u32,
3129 src_node: i32,
3130
3131 pub fn src(self: EnumDecl) LazySrcLoc {
3132 return LazySrcLoc.nodeOffset(self.src_node);
3133 }
3134
3119 pub const Small = packed struct {3135 pub const Small = packed struct {
3120 has_src_node: bool,
3121 has_tag_type: bool,3136 has_tag_type: bool,
3122 has_body_len: bool,3137 has_body_len: bool,
3123 has_fields_len: bool,3138 has_fields_len: bool,
3124 has_decls_len: bool,3139 has_decls_len: bool,
3125 name_strategy: NameStrategy,3140 name_strategy: NameStrategy,
3126 nonexhaustive: bool,3141 nonexhaustive: bool,
3127 _: u8 = undefined,3142 _: u9 = undefined,
3128 };3143 };
3129 };3144 };
31303145
3131 /// Trailing:3146 /// Trailing:
3132 /// 0. src_node: i32, // if has_src_node3147 /// 0. tag_type: Ref, // if has_tag_type
3133 /// 1. tag_type: Ref, // if has_tag_type3148 /// 1. body_len: u32, // if has_body_len
3134 /// 2. body_len: u32, // if has_body_len3149 /// 2. fields_len: u32, // if has_fields_len
3135 /// 3. fields_len: u32, // if has_fields_len3150 /// 3. decls_len: u32, // if has_decls_len
3136 /// 4. decls_len: u32, // if has_decls_len3151 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3137 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction3152 /// 5. inst: Index // for every body_len
3138 /// 6. inst: Index // for every body_len3153 /// 6. has_bits: u32 // for every 8 fields
3139 /// 7. has_bits: u32 // for every 8 fields
3140 /// - sets of 4 bits:3154 /// - sets of 4 bits:
3141 /// 0b000X: whether corresponding field has a type expression3155 /// 0b000X: whether corresponding field has a type expression
3142 /// 0b00X0: whether corresponding field has a align expression3156 /// 0b00X0: whether corresponding field has a align expression
3143 /// 0b0X00: whether corresponding field has a tag value expression3157 /// 0b0X00: whether corresponding field has a tag value expression
3144 /// 0bX000: unused3158 /// 0bX000: unused
3145 /// 8. fields: { // for every fields_len3159 /// 7. fields: { // for every fields_len
3146 /// field_name: NullTerminatedString, // null terminated string index3160 /// field_name: NullTerminatedString, // null terminated string index
3147 /// doc_comment: NullTerminatedString, // .empty if no doc comment3161 /// doc_comment: NullTerminatedString, // .empty if no doc comment
3148 /// field_type: Ref, // if corresponding bit is set3162 /// field_type: Ref, // if corresponding bit is set
...@@ -3157,8 +3171,13 @@ pub const Inst = struct {...@@ -3157,8 +3171,13 @@ pub const Inst = struct {
3157 fields_hash_1: u32,3171 fields_hash_1: u32,
3158 fields_hash_2: u32,3172 fields_hash_2: u32,
3159 fields_hash_3: u32,3173 fields_hash_3: u32,
3174 src_node: i32,
3175
3176 pub fn src(self: UnionDecl) LazySrcLoc {
3177 return LazySrcLoc.nodeOffset(self.src_node);
3178 }
3179
3160 pub const Small = packed struct {3180 pub const Small = packed struct {
3161 has_src_node: bool,
3162 has_tag_type: bool,3181 has_tag_type: bool,
3163 has_body_len: bool,3182 has_body_len: bool,
3164 has_fields_len: bool,3183 has_fields_len: bool,
...@@ -3173,20 +3192,24 @@ pub const Inst = struct {...@@ -3173,20 +3192,24 @@ pub const Inst = struct {
3173 /// true | false | union(T) { }3192 /// true | false | union(T) { }
3174 auto_enum_tag: bool,3193 auto_enum_tag: bool,
3175 any_aligned_fields: bool,3194 any_aligned_fields: bool,
3176 _: u5 = undefined,3195 _: u6 = undefined,
3177 };3196 };
3178 };3197 };
31793198
3180 /// Trailing:3199 /// Trailing:
3181 /// 0. src_node: i32, // if has_src_node3200 /// 0. decls_len: u32, // if has_decls_len
3182 /// 1. decls_len: u32, // if has_decls_len3201 /// 1. decl: Index, // for every decls_len; points to a `declaration` instruction
3183 /// 2. decl: Index, // for every decls_len; points to a `declaration` instruction
3184 pub const OpaqueDecl = struct {3202 pub const OpaqueDecl = struct {
3203 src_node: i32,
3204
3205 pub fn src(self: OpaqueDecl) LazySrcLoc {
3206 return LazySrcLoc.nodeOffset(self.src_node);
3207 }
3208
3185 pub const Small = packed struct {3209 pub const Small = packed struct {
3186 has_src_node: bool,
3187 has_decls_len: bool,3210 has_decls_len: bool,
3188 name_strategy: NameStrategy,3211 name_strategy: NameStrategy,
3189 _: u12 = undefined,3212 _: u13 = undefined,
3190 };3213 };
3191 };3214 };
31923215
...@@ -3439,6 +3462,18 @@ pub const Inst = struct {...@@ -3439,6 +3462,18 @@ pub const Inst = struct {
3439 /// The RHS of the array multiplication.3462 /// The RHS of the array multiplication.
3440 rhs: Ref,3463 rhs: Ref,
3441 };3464 };
3465
3466 pub const RestoreErrRetIndex = struct {
3467 src_node: i32,
3468 /// If `.none`, restore the trace to its state upon function entry.
3469 block: Ref,
3470 /// If `.none`, restore unconditionally.
3471 operand: Ref,
3472
3473 pub fn src(self: RestoreErrRetIndex) LazySrcLoc {
3474 return LazySrcLoc.nodeOffset(self.src_node);
3475 }
3476 };
3442};3477};
34433478
3444pub const SpecialProng = enum { none, @"else", under };3479pub const SpecialProng = enum { none, @"else", under };
...@@ -3476,7 +3511,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {...@@ -3476,7 +3511,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
3476 .struct_decl => {3511 .struct_decl => {
3477 const small: Inst.StructDecl.Small = @bitCast(extended.small);3512 const small: Inst.StructDecl.Small = @bitCast(extended.small);
3478 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).Struct.fields.len);3513 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).Struct.fields.len);
3479 extra_index += @intFromBool(small.has_src_node);
3480 extra_index += @intFromBool(small.has_fields_len);3514 extra_index += @intFromBool(small.has_fields_len);
3481 const decls_len = if (small.has_decls_len) decls_len: {3515 const decls_len = if (small.has_decls_len) decls_len: {
3482 const decls_len = zir.extra[extra_index];3516 const decls_len = zir.extra[extra_index];
...@@ -3503,7 +3537,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {...@@ -3503,7 +3537,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
3503 .enum_decl => {3537 .enum_decl => {
3504 const small: Inst.EnumDecl.Small = @bitCast(extended.small);3538 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
3505 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).Struct.fields.len);3539 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).Struct.fields.len);
3506 extra_index += @intFromBool(small.has_src_node);
3507 extra_index += @intFromBool(small.has_tag_type);3540 extra_index += @intFromBool(small.has_tag_type);
3508 extra_index += @intFromBool(small.has_body_len);3541 extra_index += @intFromBool(small.has_body_len);
3509 extra_index += @intFromBool(small.has_fields_len);3542 extra_index += @intFromBool(small.has_fields_len);
...@@ -3522,7 +3555,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {...@@ -3522,7 +3555,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
3522 .union_decl => {3555 .union_decl => {
3523 const small: Inst.UnionDecl.Small = @bitCast(extended.small);3556 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
3524 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).Struct.fields.len);3557 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).Struct.fields.len);
3525 extra_index += @intFromBool(small.has_src_node);
3526 extra_index += @intFromBool(small.has_tag_type);3558 extra_index += @intFromBool(small.has_tag_type);
3527 extra_index += @intFromBool(small.has_body_len);3559 extra_index += @intFromBool(small.has_body_len);
3528 extra_index += @intFromBool(small.has_fields_len);3560 extra_index += @intFromBool(small.has_fields_len);
...@@ -3540,8 +3572,7 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {...@@ -3540,8 +3572,7 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
3540 },3572 },
3541 .opaque_decl => {3573 .opaque_decl => {
3542 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);3574 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
3543 var extra_index: u32 = extended.operand;3575 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.OpaqueDecl).Struct.fields.len);
3544 extra_index += @intFromBool(small.has_src_node);
3545 const decls_len = if (small.has_decls_len) decls_len: {3576 const decls_len = if (small.has_decls_len) decls_len: {
3546 const decls_len = zir.extra[extra_index];3577 const decls_len = zir.extra[extra_index];
3547 extra_index += 1;3578 extra_index += 1;
src/print_zir.zig+30-58
...@@ -199,10 +199,6 @@ const Writer = struct {...@@ -199,10 +199,6 @@ const Writer = struct {
199 const tag = tags[@intFromEnum(inst)];199 const tag = tags[@intFromEnum(inst)];
200 try stream.print("= {s}(", .{@tagName(tags[@intFromEnum(inst)])});200 try stream.print("= {s}(", .{@tagName(tags[@intFromEnum(inst)])});
201 switch (tag) {201 switch (tag) {
202 .store,
203 .store_to_inferred_ptr,
204 => try self.writeBin(stream, inst),
205
206 .alloc,202 .alloc,
207 .alloc_mut,203 .alloc_mut,
208 .alloc_comptime_mut,204 .alloc_comptime_mut,
...@@ -280,6 +276,8 @@ const Writer = struct {...@@ -280,6 +276,8 @@ const Writer = struct {
280 .validate_deref,276 .validate_deref,
281 .check_comptime_control_flow,277 .check_comptime_control_flow,
282 .opt_eu_base_ptr_init,278 .opt_eu_base_ptr_init,
279 .restore_err_ret_index_unconditional,
280 .restore_err_ret_index_fn_entry,
283 => try self.writeUnNode(stream, inst),281 => try self.writeUnNode(stream, inst),
284282
285 .ref,283 .ref,
...@@ -303,7 +301,6 @@ const Writer = struct {...@@ -303,7 +301,6 @@ const Writer = struct {
303 .int_type => try self.writeIntType(stream, inst),301 .int_type => try self.writeIntType(stream, inst),
304302
305 .save_err_ret_index => try self.writeSaveErrRetIndex(stream, inst),303 .save_err_ret_index => try self.writeSaveErrRetIndex(stream, inst),
306 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, inst),
307304
308 .@"break",305 .@"break",
309 .break_inline,306 .break_inline,
...@@ -392,6 +389,7 @@ const Writer = struct {...@@ -392,6 +389,7 @@ const Writer = struct {
392 .shr_exact,389 .shr_exact,
393 .xor,390 .xor,
394 .store_node,391 .store_node,
392 .store_to_inferred_ptr,
395 .error_union_type,393 .error_union_type,
396 .merge_error_sets,394 .merge_error_sets,
397 .bit_and,395 .bit_and,
...@@ -615,6 +613,8 @@ const Writer = struct {...@@ -615,6 +613,8 @@ const Writer = struct {
615 .cmpxchg => try self.writeCmpxchg(stream, extended),613 .cmpxchg => try self.writeCmpxchg(stream, extended),
616 .ptr_cast_full => try self.writePtrCastFull(stream, extended),614 .ptr_cast_full => try self.writePtrCastFull(stream, extended),
617 .ptr_cast_no_dest => try self.writePtrCastNoDest(stream, extended),615 .ptr_cast_no_dest => try self.writePtrCastNoDest(stream, extended),
616
617 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, extended),
618 }618 }
619 }619 }
620620
...@@ -624,14 +624,6 @@ const Writer = struct {...@@ -624,14 +624,6 @@ const Writer = struct {
624 try self.writeSrc(stream, src);624 try self.writeSrc(stream, src);
625 }625 }
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
635 fn writeArrayInitElemType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {627 fn writeArrayInitElemType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
636 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;628 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;
637 try self.writeInstRef(stream, inst_data.lhs);629 try self.writeInstRef(stream, inst_data.lhs);
...@@ -1413,12 +1405,6 @@ const Writer = struct {...@@ -1413,12 +1405,6 @@ const Writer = struct {
14131405
1414 var extra_index: usize = extra.end;1406 var extra_index: usize = extra.end;
14151407
1416 const src_node: ?i32 = if (small.has_src_node) blk: {
1417 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1418 extra_index += 1;
1419 break :blk src_node;
1420 } else null;
1421
1422 const fields_len = if (small.has_fields_len) blk: {1408 const fields_len = if (small.has_fields_len) blk: {
1423 const fields_len = self.code.extra[extra_index];1409 const fields_len = self.code.extra[extra_index];
1424 extra_index += 1;1410 extra_index += 1;
...@@ -1461,7 +1447,7 @@ const Writer = struct {...@@ -1461,7 +1447,7 @@ const Writer = struct {
1461 try stream.writeAll("{}, ");1447 try stream.writeAll("{}, ");
1462 } else {1448 } else {
1463 const prev_parent_decl_node = self.parent_decl_node;1449 const prev_parent_decl_node = self.parent_decl_node;
1464 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1450 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1465 defer self.parent_decl_node = prev_parent_decl_node;1451 defer self.parent_decl_node = prev_parent_decl_node;
14661452
1467 try stream.writeAll("{\n");1453 try stream.writeAll("{\n");
...@@ -1542,7 +1528,7 @@ const Writer = struct {...@@ -1542,7 +1528,7 @@ const Writer = struct {
1542 }1528 }
15431529
1544 const prev_parent_decl_node = self.parent_decl_node;1530 const prev_parent_decl_node = self.parent_decl_node;
1545 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1531 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1546 try stream.writeAll("{\n");1532 try stream.writeAll("{\n");
1547 self.indent += 2;1533 self.indent += 2;
15481534
...@@ -1595,7 +1581,7 @@ const Writer = struct {...@@ -1595,7 +1581,7 @@ const Writer = struct {
1595 try stream.writeByteNTimes(' ', self.indent);1581 try stream.writeByteNTimes(' ', self.indent);
1596 try stream.writeAll("})");1582 try stream.writeAll("})");
1597 }1583 }
1598 try self.writeSrcNode(stream, src_node);1584 try self.writeSrcNode(stream, extra.data.src_node);
1599 }1585 }
16001586
1601 fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {1587 fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
...@@ -1613,12 +1599,6 @@ const Writer = struct {...@@ -1613,12 +1599,6 @@ const Writer = struct {
16131599
1614 var extra_index: usize = extra.end;1600 var extra_index: usize = extra.end;
16151601
1616 const src_node: ?i32 = if (small.has_src_node) blk: {
1617 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1618 extra_index += 1;
1619 break :blk src_node;
1620 } else null;
1621
1622 const tag_type_ref = if (small.has_tag_type) blk: {1602 const tag_type_ref = if (small.has_tag_type) blk: {
1623 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));1603 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
1624 extra_index += 1;1604 extra_index += 1;
...@@ -1652,7 +1632,7 @@ const Writer = struct {...@@ -1652,7 +1632,7 @@ const Writer = struct {
1652 try stream.writeAll("{}");1632 try stream.writeAll("{}");
1653 } else {1633 } else {
1654 const prev_parent_decl_node = self.parent_decl_node;1634 const prev_parent_decl_node = self.parent_decl_node;
1655 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1635 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1656 defer self.parent_decl_node = prev_parent_decl_node;1636 defer self.parent_decl_node = prev_parent_decl_node;
16571637
1658 try stream.writeAll("{\n");1638 try stream.writeAll("{\n");
...@@ -1671,7 +1651,7 @@ const Writer = struct {...@@ -1671,7 +1651,7 @@ const Writer = struct {
16711651
1672 if (fields_len == 0) {1652 if (fields_len == 0) {
1673 try stream.writeAll("})");1653 try stream.writeAll("})");
1674 try self.writeSrcNode(stream, src_node);1654 try self.writeSrcNode(stream, extra.data.src_node);
1675 return;1655 return;
1676 }1656 }
1677 try stream.writeAll(", ");1657 try stream.writeAll(", ");
...@@ -1680,7 +1660,7 @@ const Writer = struct {...@@ -1680,7 +1660,7 @@ const Writer = struct {
1680 extra_index += body.len;1660 extra_index += body.len;
16811661
1682 const prev_parent_decl_node = self.parent_decl_node;1662 const prev_parent_decl_node = self.parent_decl_node;
1683 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1663 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1684 try self.writeBracedDecl(stream, body);1664 try self.writeBracedDecl(stream, body);
1685 try stream.writeAll(", {\n");1665 try stream.writeAll(", {\n");
16861666
...@@ -1748,7 +1728,7 @@ const Writer = struct {...@@ -1748,7 +1728,7 @@ const Writer = struct {
1748 self.indent -= 2;1728 self.indent -= 2;
1749 try stream.writeByteNTimes(' ', self.indent);1729 try stream.writeByteNTimes(' ', self.indent);
1750 try stream.writeAll("})");1730 try stream.writeAll("})");
1751 try self.writeSrcNode(stream, src_node);1731 try self.writeSrcNode(stream, extra.data.src_node);
1752 }1732 }
17531733
1754 fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {1734 fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
...@@ -1766,12 +1746,6 @@ const Writer = struct {...@@ -1766,12 +1746,6 @@ const Writer = struct {
17661746
1767 var extra_index: usize = extra.end;1747 var extra_index: usize = extra.end;
17681748
1769 const src_node: ?i32 = if (small.has_src_node) blk: {
1770 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1771 extra_index += 1;
1772 break :blk src_node;
1773 } else null;
1774
1775 const tag_type_ref = if (small.has_tag_type) blk: {1749 const tag_type_ref = if (small.has_tag_type) blk: {
1776 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));1750 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
1777 extra_index += 1;1751 extra_index += 1;
...@@ -1803,7 +1777,7 @@ const Writer = struct {...@@ -1803,7 +1777,7 @@ const Writer = struct {
1803 try stream.writeAll("{}, ");1777 try stream.writeAll("{}, ");
1804 } else {1778 } else {
1805 const prev_parent_decl_node = self.parent_decl_node;1779 const prev_parent_decl_node = self.parent_decl_node;
1806 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1780 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1807 defer self.parent_decl_node = prev_parent_decl_node;1781 defer self.parent_decl_node = prev_parent_decl_node;
18081782
1809 try stream.writeAll("{\n");1783 try stream.writeAll("{\n");
...@@ -1824,7 +1798,7 @@ const Writer = struct {...@@ -1824,7 +1798,7 @@ const Writer = struct {
1824 extra_index += body.len;1798 extra_index += body.len;
18251799
1826 const prev_parent_decl_node = self.parent_decl_node;1800 const prev_parent_decl_node = self.parent_decl_node;
1827 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1801 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1828 try self.writeBracedDecl(stream, body);1802 try self.writeBracedDecl(stream, body);
1829 if (fields_len == 0) {1803 if (fields_len == 0) {
1830 try stream.writeAll(", {})");1804 try stream.writeAll(", {})");
...@@ -1872,7 +1846,7 @@ const Writer = struct {...@@ -1872,7 +1846,7 @@ const Writer = struct {
1872 try stream.writeByteNTimes(' ', self.indent);1846 try stream.writeByteNTimes(' ', self.indent);
1873 try stream.writeAll("})");1847 try stream.writeAll("})");
1874 }1848 }
1875 try self.writeSrcNode(stream, src_node);1849 try self.writeSrcNode(stream, extra.data.src_node);
1876 }1850 }
18771851
1878 fn writeOpaqueDecl(1852 fn writeOpaqueDecl(
...@@ -1881,13 +1855,8 @@ const Writer = struct {...@@ -1881,13 +1855,8 @@ const Writer = struct {
1881 extended: Zir.Inst.Extended.InstData,1855 extended: Zir.Inst.Extended.InstData,
1882 ) !void {1856 ) !void {
1883 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));1857 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
1884 var extra_index: usize = extended.operand;1858 const extra = self.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
18851859 var extra_index: usize = extra.end;
1886 const src_node: ?i32 = if (small.has_src_node) blk: {
1887 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1888 extra_index += 1;
1889 break :blk src_node;
1890 } else null;
18911860
1892 const decls_len = if (small.has_decls_len) blk: {1861 const decls_len = if (small.has_decls_len) blk: {
1893 const decls_len = self.code.extra[extra_index];1862 const decls_len = self.code.extra[extra_index];
...@@ -1901,7 +1870,7 @@ const Writer = struct {...@@ -1901,7 +1870,7 @@ const Writer = struct {
1901 try stream.writeAll("{})");1870 try stream.writeAll("{})");
1902 } else {1871 } else {
1903 const prev_parent_decl_node = self.parent_decl_node;1872 const prev_parent_decl_node = self.parent_decl_node;
1904 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);1873 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1905 defer self.parent_decl_node = prev_parent_decl_node;1874 defer self.parent_decl_node = prev_parent_decl_node;
19061875
1907 try stream.writeAll("{\n");1876 try stream.writeAll("{\n");
...@@ -1911,7 +1880,7 @@ const Writer = struct {...@@ -1911,7 +1880,7 @@ const Writer = struct {
1911 try stream.writeByteNTimes(' ', self.indent);1880 try stream.writeByteNTimes(' ', self.indent);
1912 try stream.writeAll("})");1881 try stream.writeAll("})");
1913 }1882 }
1914 try self.writeSrcNode(stream, src_node);1883 try self.writeSrcNode(stream, extra.data.src_node);
1915 }1884 }
19161885
1917 fn writeErrorSetDecl(1886 fn writeErrorSetDecl(
...@@ -2505,12 +2474,14 @@ const Writer = struct {...@@ -2505,12 +2474,14 @@ const Writer = struct {
2505 }2474 }
25062475
2507 fn writeBoolBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2476 fn writeBoolBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2508 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bool_br;2477 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2509 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);2478 const extra = self.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
2510 const body = self.code.bodySlice(extra.end, extra.data.body_len);2479 const body = self.code.bodySlice(extra.end, extra.data.body_len);
2511 try self.writeInstRef(stream, inst_data.lhs);2480 try self.writeInstRef(stream, extra.data.lhs);
2512 try stream.writeAll(", ");2481 try stream.writeAll(", ");
2513 try self.writeBracedBody(stream, body);2482 try self.writeBracedBody(stream, body);
2483 try stream.writeAll(") ");
2484 try self.writeSrc(stream, inst_data.src());
2514 }2485 }
25152486
2516 fn writeIntType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2487 fn writeIntType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
...@@ -2531,13 +2502,14 @@ const Writer = struct {...@@ -2531,13 +2502,14 @@ const Writer = struct {
2531 try stream.writeAll(")");2502 try stream.writeAll(")");
2532 }2503 }
25332504
2534 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2505 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
2535 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;2506 const extra = self.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
25362507
2537 try self.writeInstRef(stream, inst_data.block);2508 try self.writeInstRef(stream, extra.block);
2538 try self.writeInstRef(stream, inst_data.operand);2509 try self.writeInstRef(stream, extra.operand);
25392510
2540 try stream.writeAll(")");2511 try stream.writeAll(") ");
2512 try self.writeSrc(stream, extra.src());
2541 }2513 }
25422514
2543 fn writeBreak(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2515 fn writeBreak(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
test/cases/compile_errors/non_constant_expression_in_array_size.zig+2-2
...@@ -14,6 +14,6 @@ export fn entry() usize {...@@ -14,6 +14,6 @@ export fn entry() usize {
14// backend=stage214// backend=stage2
15// target=native15// target=native
16//16//
17// :6:5: error: unable to resolve comptime value17// :6:12: error: unable to resolve comptime value
18// :6:5: note: value being returned at comptime must be comptime-known18// :6:12: note: value being returned at comptime must be comptime-known
19// :2:12: note: called from here19// :2:12: note: called from here