authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-31 20:15:08-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-01-31 20:15:08-08:00
logbf76501b5d46277d3706a1f0b92ba52f2a47d894
tree7a40c904b9246092009c02f5b09a2a8732b69175
parentfdc875ed0080cd2542a854a8cd6c627b25e9b7a4
parent0f5eda973e0c17b3f792cdb06674bf8d2863c8fb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7847 from ziglang/astgen-rl-rework

stage2: rework astgen result locations

7 files changed, 1280 insertions(+), 1398 deletions(-)

src/Module.zig+62-11
......@@ -375,6 +375,10 @@ pub const Scope = struct {
375375 }
376376 }
377377
378 pub fn isComptime(self: *Scope) bool {
379 return self.getGenZIR().force_comptime;
380 }
381
378382 pub fn ownerDecl(self: *Scope) ?*Decl {
379383 return switch (self.tag) {
380384 .block => self.cast(Block).?.owner_decl,
......@@ -671,14 +675,36 @@ pub const Scope = struct {
671675 };
672676
673677 pub const Merges = struct {
674 results: ArrayListUnmanaged(*Inst),
675678 block_inst: *Inst.Block,
679 /// Separate array list from break_inst_list so that it can be passed directly
680 /// to resolvePeerTypes.
681 results: ArrayListUnmanaged(*Inst),
682 /// Keeps track of the break instructions so that the operand can be replaced
683 /// if we need to add type coercion at the end of block analysis.
684 /// Same indexes, capacity, length as `results`.
685 br_list: ArrayListUnmanaged(*Inst.Br),
676686 };
677687
678688 /// For debugging purposes.
679689 pub fn dump(self: *Block, mod: Module) void {
680690 zir.dumpBlock(mod, self);
681691 }
692
693 pub fn makeSubBlock(parent: *Block) Block {
694 return .{
695 .parent = parent,
696 .inst_table = parent.inst_table,
697 .func = parent.func,
698 .owner_decl = parent.owner_decl,
699 .src_decl = parent.src_decl,
700 .instructions = .{},
701 .arena = parent.arena,
702 .label = null,
703 .inlining = parent.inlining,
704 .is_comptime = parent.is_comptime,
705 .branch_quota = parent.branch_quota,
706 };
707 }
682708 };
683709
684710 /// This is a temporary structure, references to it are valid only
......@@ -690,13 +716,32 @@ pub const Scope = struct {
690716 parent: *Scope,
691717 decl: *Decl,
692718 arena: *Allocator,
719 force_comptime: bool,
693720 /// The first N instructions in a function body ZIR are arg instructions.
694721 instructions: std.ArrayListUnmanaged(*zir.Inst) = .{},
695722 label: ?Label = null,
696723 break_block: ?*zir.Inst.Block = null,
697724 continue_block: ?*zir.Inst.Block = null,
698 /// only valid if label != null or (continue_block and break_block) != null
725 /// Only valid when setBlockResultLoc is called.
699726 break_result_loc: astgen.ResultLoc = undefined,
727 /// When a block has a pointer result location, here it is.
728 rl_ptr: ?*zir.Inst = null,
729 /// Keeps track of how many branches of a block did not actually
730 /// consume the result location. astgen uses this to figure out
731 /// whether to rely on break instructions or writing to the result
732 /// pointer for the result instruction.
733 rvalue_rl_count: usize = 0,
734 /// Keeps track of how many break instructions there are. When astgen is finished
735 /// with a block, it can check this against rvalue_rl_count to find out whether
736 /// the break instructions should be downgraded to break_void.
737 break_count: usize = 0,
738 /// Tracks `break :foo bar` instructions so they can possibly be elided later if
739 /// the labeled block ends up not needing a result location pointer.
740 labeled_breaks: std.ArrayListUnmanaged(*zir.Inst.Break) = .{},
741 /// Tracks `store_to_block_ptr` instructions that correspond to break instructions
742 /// so they can possibly be elided later if the labeled block ends up not needing
743 /// a result location pointer.
744 labeled_store_to_block_ptr_list: std.ArrayListUnmanaged(*zir.Inst.BinOp) = .{},
700745
701746 pub const Label = struct {
702747 token: ast.TokenIndex,
......@@ -968,6 +1013,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
9681013 .decl = decl,
9691014 .arena = &fn_type_scope_arena.allocator,
9701015 .parent = &decl.container.base,
1016 .force_comptime = true,
9711017 };
9721018 defer fn_type_scope.instructions.deinit(self.gpa);
9731019
......@@ -1131,6 +1177,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11311177 .decl = decl,
11321178 .arena = &decl_arena.allocator,
11331179 .parent = &decl.container.base,
1180 .force_comptime = false,
11341181 };
11351182 defer gen_scope.instructions.deinit(self.gpa);
11361183
......@@ -1171,7 +1218,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11711218 !gen_scope.instructions.items[gen_scope.instructions.items.len - 1].tag.isNoReturn())
11721219 {
11731220 const src = tree.token_locs[body_block.rbrace].start;
1174 _ = try astgen.addZIRNoOp(self, &gen_scope.base, src, .returnvoid);
1221 _ = try astgen.addZIRNoOp(self, &gen_scope.base, src, .return_void);
11751222 }
11761223
11771224 if (std.builtin.mode == .Debug and self.comp.verbose_ir) {
......@@ -1329,6 +1376,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13291376 .decl = decl,
13301377 .arena = &gen_scope_arena.allocator,
13311378 .parent = &decl.container.base,
1379 .force_comptime = false,
13321380 };
13331381 defer gen_scope.instructions.deinit(self.gpa);
13341382
......@@ -1388,6 +1436,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13881436 .decl = decl,
13891437 .arena = &type_scope_arena.allocator,
13901438 .parent = &decl.container.base,
1439 .force_comptime = true,
13911440 };
13921441 defer type_scope.instructions.deinit(self.gpa);
13931442
......@@ -1457,13 +1506,15 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
14571506
14581507 decl.analysis = .in_progress;
14591508
1460 // A comptime decl does not store any value so we can just deinit this arena after analysis is done.
1509 // A comptime decl does not store any value so we can just deinit
1510 // this arena after analysis is done.
14611511 var analysis_arena = std.heap.ArenaAllocator.init(self.gpa);
14621512 defer analysis_arena.deinit();
14631513 var gen_scope: Scope.GenZIR = .{
14641514 .decl = decl,
14651515 .arena = &analysis_arena.allocator,
14661516 .parent = &decl.container.base,
1517 .force_comptime = true,
14671518 };
14681519 defer gen_scope.instructions.deinit(self.gpa);
14691520
......@@ -2100,7 +2151,7 @@ pub fn addBr(
21002151 src: usize,
21012152 target_block: *Inst.Block,
21022153 operand: *Inst,
2103) !*Inst {
2154) !*Inst.Br {
21042155 const inst = try scope_block.arena.create(Inst.Br);
21052156 inst.* = .{
21062157 .base = .{
......@@ -2112,7 +2163,7 @@ pub fn addBr(
21122163 .block = target_block,
21132164 };
21142165 try scope_block.instructions.append(self.gpa, &inst.base);
2115 return &inst.base;
2166 return inst;
21162167}
21172168
21182169pub fn addCondBr(
......@@ -3466,18 +3517,18 @@ pub fn addSafetyCheck(mod: *Module, parent_block: *Scope.Block, ok: *Inst, panic
34663517 };
34673518
34683519 const ok_body: ir.Body = .{
3469 .instructions = try parent_block.arena.alloc(*Inst, 1), // Only need space for the brvoid.
3520 .instructions = try parent_block.arena.alloc(*Inst, 1), // Only need space for the br_void.
34703521 };
3471 const brvoid = try parent_block.arena.create(Inst.BrVoid);
3472 brvoid.* = .{
3522 const br_void = try parent_block.arena.create(Inst.BrVoid);
3523 br_void.* = .{
34733524 .base = .{
3474 .tag = .brvoid,
3525 .tag = .br_void,
34753526 .ty = Type.initTag(.noreturn),
34763527 .src = ok.src,
34773528 },
34783529 .block = block_inst,
34793530 };
3480 ok_body.instructions[0] = &brvoid.base;
3531 ok_body.instructions[0] = &br_void.base;
34813532
34823533 var fail_block: Scope.Block = .{
34833534 .parent = parent_block,
src/astgen.zig+674-611
......@@ -14,25 +14,45 @@ const InnerError = Module.InnerError;
1414
1515pub const ResultLoc = union(enum) {
1616 /// The expression is the right-hand side of assignment to `_`. Only the side-effects of the
17 /// expression should be generated.
17 /// expression should be generated. The result instruction from the expression must
18 /// be ignored.
1819 discard,
1920 /// The expression has an inferred type, and it will be evaluated as an rvalue.
2021 none,
2122 /// The expression must generate a pointer rather than a value. For example, the left hand side
2223 /// of an assignment uses this kind of result location.
2324 ref,
24 /// The expression will be type coerced into this type, but it will be evaluated as an rvalue.
25 /// The expression will be coerced into this type, but it will be evaluated as an rvalue.
2526 ty: *zir.Inst,
26 /// The expression must store its result into this typed pointer.
27 /// The expression must store its result into this typed pointer. The result instruction
28 /// from the expression must be ignored.
2729 ptr: *zir.Inst,
2830 /// The expression must store its result into this allocation, which has an inferred type.
31 /// The result instruction from the expression must be ignored.
2932 inferred_ptr: *zir.Inst.Tag.alloc_inferred.Type(),
3033 /// The expression must store its result into this pointer, which is a typed pointer that
3134 /// has been bitcasted to whatever the expression's type is.
35 /// The result instruction from the expression must be ignored.
3236 bitcasted_ptr: *zir.Inst.UnOp,
3337 /// There is a pointer for the expression to store its result into, however, its type
3438 /// is inferred based on peer type resolution for a `zir.Inst.Block`.
35 block_ptr: *zir.Inst.Block,
39 /// The result instruction from the expression must be ignored.
40 block_ptr: *Module.Scope.GenZIR,
41
42 pub const Strategy = struct {
43 elide_store_to_block_ptr_instructions: bool,
44 tag: Tag,
45
46 pub const Tag = enum {
47 /// Both branches will use break_void; result location is used to communicate the
48 /// result instruction.
49 break_void,
50 /// Use break statements to pass the block result value, and call rvalue() at
51 /// the end depending on rl. Also elide the store_to_block_ptr instructions
52 /// depending on rl.
53 break_operand,
54 };
55 };
3656};
3757
3858pub fn typeExpr(mod: *Module, scope: *Scope, type_node: *ast.Node) InnerError!*zir.Inst {
......@@ -179,6 +199,9 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst {
179199}
180200
181201/// Turn Zig AST into untyped ZIR istructions.
202/// When `rl` is discard, ptr, inferred_ptr, bitcasted_ptr, or inferred_ptr, the
203/// result instruction can be used to inspect whether it is isNoReturn() but that is it,
204/// it must otherwise not be used.
182205pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerError!*zir.Inst {
183206 switch (node.tag) {
184207 .Root => unreachable, // Top-level declaration.
......@@ -197,20 +220,20 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
197220 .FieldInitializer => unreachable, // Handled explicitly.
198221 .ContainerField => unreachable, // Handled explicitly.
199222
200 .Assign => return rlWrapVoid(mod, scope, rl, node, try assign(mod, scope, node.castTag(.Assign).?)),
201 .AssignBitAnd => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitAnd).?, .bitand)),
202 .AssignBitOr => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitOr).?, .bitor)),
203 .AssignBitShiftLeft => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitShiftLeft).?, .shl)),
204 .AssignBitShiftRight => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitShiftRight).?, .shr)),
205 .AssignBitXor => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitXor).?, .xor)),
206 .AssignDiv => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignDiv).?, .div)),
207 .AssignSub => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignSub).?, .sub)),
208 .AssignSubWrap => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignSubWrap).?, .subwrap)),
209 .AssignMod => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMod).?, .mod_rem)),
210 .AssignAdd => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignAdd).?, .add)),
211 .AssignAddWrap => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignAddWrap).?, .addwrap)),
212 .AssignMul => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMul).?, .mul)),
213 .AssignMulWrap => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMulWrap).?, .mulwrap)),
223 .Assign => return rvalueVoid(mod, scope, rl, node, try assign(mod, scope, node.castTag(.Assign).?)),
224 .AssignBitAnd => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitAnd).?, .bit_and)),
225 .AssignBitOr => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitOr).?, .bit_or)),
226 .AssignBitShiftLeft => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitShiftLeft).?, .shl)),
227 .AssignBitShiftRight => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitShiftRight).?, .shr)),
228 .AssignBitXor => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitXor).?, .xor)),
229 .AssignDiv => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignDiv).?, .div)),
230 .AssignSub => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignSub).?, .sub)),
231 .AssignSubWrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignSubWrap).?, .subwrap)),
232 .AssignMod => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMod).?, .mod_rem)),
233 .AssignAdd => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignAdd).?, .add)),
234 .AssignAddWrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignAddWrap).?, .addwrap)),
235 .AssignMul => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMul).?, .mul)),
236 .AssignMulWrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignMulWrap).?, .mulwrap)),
214237
215238 .Add => return simpleBinOp(mod, scope, rl, node.castTag(.Add).?, .add),
216239 .AddWrap => return simpleBinOp(mod, scope, rl, node.castTag(.AddWrap).?, .addwrap),
......@@ -220,8 +243,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
220243 .MulWrap => return simpleBinOp(mod, scope, rl, node.castTag(.MulWrap).?, .mulwrap),
221244 .Div => return simpleBinOp(mod, scope, rl, node.castTag(.Div).?, .div),
222245 .Mod => return simpleBinOp(mod, scope, rl, node.castTag(.Mod).?, .mod_rem),
223 .BitAnd => return simpleBinOp(mod, scope, rl, node.castTag(.BitAnd).?, .bitand),
224 .BitOr => return simpleBinOp(mod, scope, rl, node.castTag(.BitOr).?, .bitor),
246 .BitAnd => return simpleBinOp(mod, scope, rl, node.castTag(.BitAnd).?, .bit_and),
247 .BitOr => return simpleBinOp(mod, scope, rl, node.castTag(.BitOr).?, .bit_or),
225248 .BitShiftLeft => return simpleBinOp(mod, scope, rl, node.castTag(.BitShiftLeft).?, .shl),
226249 .BitShiftRight => return simpleBinOp(mod, scope, rl, node.castTag(.BitShiftRight).?, .shr),
227250 .BitXor => return simpleBinOp(mod, scope, rl, node.castTag(.BitXor).?, .xor),
......@@ -239,15 +262,15 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
239262 .BoolAnd => return boolBinOp(mod, scope, rl, node.castTag(.BoolAnd).?),
240263 .BoolOr => return boolBinOp(mod, scope, rl, node.castTag(.BoolOr).?),
241264
242 .BoolNot => return rlWrap(mod, scope, rl, try boolNot(mod, scope, node.castTag(.BoolNot).?)),
243 .BitNot => return rlWrap(mod, scope, rl, try bitNot(mod, scope, node.castTag(.BitNot).?)),
244 .Negation => return rlWrap(mod, scope, rl, try negation(mod, scope, node.castTag(.Negation).?, .sub)),
245 .NegationWrap => return rlWrap(mod, scope, rl, try negation(mod, scope, node.castTag(.NegationWrap).?, .subwrap)),
265 .BoolNot => return rvalue(mod, scope, rl, try boolNot(mod, scope, node.castTag(.BoolNot).?)),
266 .BitNot => return rvalue(mod, scope, rl, try bitNot(mod, scope, node.castTag(.BitNot).?)),
267 .Negation => return rvalue(mod, scope, rl, try negation(mod, scope, node.castTag(.Negation).?, .sub)),
268 .NegationWrap => return rvalue(mod, scope, rl, try negation(mod, scope, node.castTag(.NegationWrap).?, .subwrap)),
246269
247270 .Identifier => return try identifier(mod, scope, rl, node.castTag(.Identifier).?),
248 .Asm => return rlWrap(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)),
249 .StringLiteral => return rlWrap(mod, scope, rl, try stringLiteral(mod, scope, node.castTag(.StringLiteral).?)),
250 .IntegerLiteral => return rlWrap(mod, scope, rl, try integerLiteral(mod, scope, node.castTag(.IntegerLiteral).?)),
271 .Asm => return rvalue(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)),
272 .StringLiteral => return rvalue(mod, scope, rl, try stringLiteral(mod, scope, node.castTag(.StringLiteral).?)),
273 .IntegerLiteral => return rvalue(mod, scope, rl, try integerLiteral(mod, scope, node.castTag(.IntegerLiteral).?)),
251274 .BuiltinCall => return builtinCall(mod, scope, rl, node.castTag(.BuiltinCall).?),
252275 .Call => return callExpr(mod, scope, rl, node.castTag(.Call).?),
253276 .Unreachable => return unreach(mod, scope, node.castTag(.Unreachable).?),
......@@ -255,38 +278,38 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
255278 .If => return ifExpr(mod, scope, rl, node.castTag(.If).?),
256279 .While => return whileExpr(mod, scope, rl, node.castTag(.While).?),
257280 .Period => return field(mod, scope, rl, node.castTag(.Period).?),
258 .Deref => return rlWrap(mod, scope, rl, try deref(mod, scope, node.castTag(.Deref).?)),
259 .AddressOf => return rlWrap(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),
260 .FloatLiteral => return rlWrap(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),
261 .UndefinedLiteral => return rlWrap(mod, scope, rl, try undefLiteral(mod, scope, node.castTag(.UndefinedLiteral).?)),
262 .BoolLiteral => return rlWrap(mod, scope, rl, try boolLiteral(mod, scope, node.castTag(.BoolLiteral).?)),
263 .NullLiteral => return rlWrap(mod, scope, rl, try nullLiteral(mod, scope, node.castTag(.NullLiteral).?)),
264 .OptionalType => return rlWrap(mod, scope, rl, try optionalType(mod, scope, node.castTag(.OptionalType).?)),
281 .Deref => return rvalue(mod, scope, rl, try deref(mod, scope, node.castTag(.Deref).?)),
282 .AddressOf => return rvalue(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),
283 .FloatLiteral => return rvalue(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),
284 .UndefinedLiteral => return rvalue(mod, scope, rl, try undefLiteral(mod, scope, node.castTag(.UndefinedLiteral).?)),
285 .BoolLiteral => return rvalue(mod, scope, rl, try boolLiteral(mod, scope, node.castTag(.BoolLiteral).?)),
286 .NullLiteral => return rvalue(mod, scope, rl, try nullLiteral(mod, scope, node.castTag(.NullLiteral).?)),
287 .OptionalType => return rvalue(mod, scope, rl, try optionalType(mod, scope, node.castTag(.OptionalType).?)),
265288 .UnwrapOptional => return unwrapOptional(mod, scope, rl, node.castTag(.UnwrapOptional).?),
266 .Block => return rlWrapVoid(mod, scope, rl, node, try blockExpr(mod, scope, node.castTag(.Block).?)),
289 .Block => return rvalueVoid(mod, scope, rl, node, try blockExpr(mod, scope, node.castTag(.Block).?)),
267290 .LabeledBlock => return labeledBlockExpr(mod, scope, rl, node.castTag(.LabeledBlock).?, .block),
268 .Break => return rlWrap(mod, scope, rl, try breakExpr(mod, scope, node.castTag(.Break).?)),
269 .Continue => return rlWrap(mod, scope, rl, try continueExpr(mod, scope, node.castTag(.Continue).?)),
270 .PtrType => return rlWrap(mod, scope, rl, try ptrType(mod, scope, node.castTag(.PtrType).?)),
291 .Break => return rvalue(mod, scope, rl, try breakExpr(mod, scope, node.castTag(.Break).?)),
292 .Continue => return rvalue(mod, scope, rl, try continueExpr(mod, scope, node.castTag(.Continue).?)),
293 .PtrType => return rvalue(mod, scope, rl, try ptrType(mod, scope, node.castTag(.PtrType).?)),
271294 .GroupedExpression => return expr(mod, scope, rl, node.castTag(.GroupedExpression).?.expr),
272 .ArrayType => return rlWrap(mod, scope, rl, try arrayType(mod, scope, node.castTag(.ArrayType).?)),
273 .ArrayTypeSentinel => return rlWrap(mod, scope, rl, try arrayTypeSentinel(mod, scope, node.castTag(.ArrayTypeSentinel).?)),
274 .EnumLiteral => return rlWrap(mod, scope, rl, try enumLiteral(mod, scope, node.castTag(.EnumLiteral).?)),
275 .MultilineStringLiteral => return rlWrap(mod, scope, rl, try multilineStrLiteral(mod, scope, node.castTag(.MultilineStringLiteral).?)),
276 .CharLiteral => return rlWrap(mod, scope, rl, try charLiteral(mod, scope, node.castTag(.CharLiteral).?)),
277 .SliceType => return rlWrap(mod, scope, rl, try sliceType(mod, scope, node.castTag(.SliceType).?)),
278 .ErrorUnion => return rlWrap(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.ErrorUnion).?, .error_union_type)),
279 .MergeErrorSets => return rlWrap(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.MergeErrorSets).?, .merge_error_sets)),
280 .AnyFrameType => return rlWrap(mod, scope, rl, try anyFrameType(mod, scope, node.castTag(.AnyFrameType).?)),
281 .ErrorSetDecl => return rlWrap(mod, scope, rl, try errorSetDecl(mod, scope, node.castTag(.ErrorSetDecl).?)),
282 .ErrorType => return rlWrap(mod, scope, rl, try errorType(mod, scope, node.castTag(.ErrorType).?)),
295 .ArrayType => return rvalue(mod, scope, rl, try arrayType(mod, scope, node.castTag(.ArrayType).?)),
296 .ArrayTypeSentinel => return rvalue(mod, scope, rl, try arrayTypeSentinel(mod, scope, node.castTag(.ArrayTypeSentinel).?)),
297 .EnumLiteral => return rvalue(mod, scope, rl, try enumLiteral(mod, scope, node.castTag(.EnumLiteral).?)),
298 .MultilineStringLiteral => return rvalue(mod, scope, rl, try multilineStrLiteral(mod, scope, node.castTag(.MultilineStringLiteral).?)),
299 .CharLiteral => return rvalue(mod, scope, rl, try charLiteral(mod, scope, node.castTag(.CharLiteral).?)),
300 .SliceType => return rvalue(mod, scope, rl, try sliceType(mod, scope, node.castTag(.SliceType).?)),
301 .ErrorUnion => return rvalue(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.ErrorUnion).?, .error_union_type)),
302 .MergeErrorSets => return rvalue(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.MergeErrorSets).?, .merge_error_sets)),
303 .AnyFrameType => return rvalue(mod, scope, rl, try anyFrameType(mod, scope, node.castTag(.AnyFrameType).?)),
304 .ErrorSetDecl => return rvalue(mod, scope, rl, try errorSetDecl(mod, scope, node.castTag(.ErrorSetDecl).?)),
305 .ErrorType => return rvalue(mod, scope, rl, try errorType(mod, scope, node.castTag(.ErrorType).?)),
283306 .For => return forExpr(mod, scope, rl, node.castTag(.For).?),
284307 .ArrayAccess => return arrayAccess(mod, scope, rl, node.castTag(.ArrayAccess).?),
285 .Slice => return rlWrap(mod, scope, rl, try sliceExpr(mod, scope, node.castTag(.Slice).?)),
308 .Slice => return rvalue(mod, scope, rl, try sliceExpr(mod, scope, node.castTag(.Slice).?)),
286309 .Catch => return catchExpr(mod, scope, rl, node.castTag(.Catch).?),
287310 .Comptime => return comptimeKeyword(mod, scope, rl, node.castTag(.Comptime).?),
288311 .OrElse => return orelseExpr(mod, scope, rl, node.castTag(.OrElse).?),
289 .Switch => return switchExpr(mod, scope, rl, node.castTag(.Switch).?),
312 .Switch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Switch", .{}),
290313 .ContainerDecl => return containerDecl(mod, scope, rl, node.castTag(.ContainerDecl).?),
291314
292315 .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}),
......@@ -311,11 +334,19 @@ fn comptimeKeyword(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.C
311334 return comptimeExpr(mod, scope, rl, node.expr);
312335}
313336
314pub fn comptimeExpr(mod: *Module, parent_scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerError!*zir.Inst {
315 const tree = parent_scope.tree();
316 const src = tree.token_locs[node.firstToken()].start;
337pub fn comptimeExpr(
338 mod: *Module,
339 parent_scope: *Scope,
340 rl: ResultLoc,
341 node: *ast.Node,
342) InnerError!*zir.Inst {
343 // If we are already in a comptime scope, no need to make another one.
344 if (parent_scope.isComptime()) {
345 return expr(mod, parent_scope, rl, node);
346 }
317347
318 // Optimization for labeled blocks: don't need to have 2 layers of blocks, we can reuse the existing one.
348 // Optimization for labeled blocks: don't need to have 2 layers of blocks,
349 // we can reuse the existing one.
319350 if (node.castTag(.LabeledBlock)) |block_node| {
320351 return labeledBlockExpr(mod, parent_scope, rl, block_node, .block_comptime);
321352 }
......@@ -325,6 +356,7 @@ pub fn comptimeExpr(mod: *Module, parent_scope: *Scope, rl: ResultLoc, node: *as
325356 .parent = parent_scope,
326357 .decl = parent_scope.ownerDecl().?,
327358 .arena = parent_scope.arena(),
359 .force_comptime = true,
328360 .instructions = .{},
329361 };
330362 defer block_scope.instructions.deinit(mod.gpa);
......@@ -333,6 +365,9 @@ pub fn comptimeExpr(mod: *Module, parent_scope: *Scope, rl: ResultLoc, node: *as
333365 // instruction is the block's result value.
334366 _ = try expr(mod, &block_scope.base, rl, node);
335367
368 const tree = parent_scope.tree();
369 const src = tree.token_locs[node.firstToken()].start;
370
336371 const block = try addZIRInstBlock(mod, parent_scope, src, .block_comptime_flat, .{
337372 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),
338373 });
......@@ -340,7 +375,11 @@ pub fn comptimeExpr(mod: *Module, parent_scope: *Scope, rl: ResultLoc, node: *as
340375 return &block.base;
341376}
342377
343fn breakExpr(mod: *Module, parent_scope: *Scope, node: *ast.Node.ControlFlowExpression) InnerError!*zir.Inst {
378fn breakExpr(
379 mod: *Module,
380 parent_scope: *Scope,
381 node: *ast.Node.ControlFlowExpression,
382) InnerError!*zir.Inst {
344383 const tree = parent_scope.tree();
345384 const src = tree.token_locs[node.ltoken].start;
346385
......@@ -366,25 +405,31 @@ fn breakExpr(mod: *Module, parent_scope: *Scope, node: *ast.Node.ControlFlowExpr
366405 continue;
367406 };
368407
369 if (node.getRHS()) |rhs| {
370 // Most result location types can be forwarded directly; however
371 // if we need to write to a pointer which has an inferred type,
372 // proper type inference requires peer type resolution on the block's
373 // break operand expressions.
374 const branch_rl: ResultLoc = switch (gen_zir.break_result_loc) {
375 .discard, .none, .ty, .ptr, .ref => gen_zir.break_result_loc,
376 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block_inst },
377 };
378 const operand = try expr(mod, parent_scope, branch_rl, rhs);
379 return try addZIRInst(mod, parent_scope, src, zir.Inst.Break, .{
408 const rhs = node.getRHS() orelse {
409 return addZirInstTag(mod, parent_scope, src, .break_void, .{
380410 .block = block_inst,
381 .operand = operand,
382 }, .{});
383 } else {
384 return try addZIRInst(mod, parent_scope, src, zir.Inst.BreakVoid, .{
385 .block = block_inst,
386 }, .{});
411 });
412 };
413 gen_zir.break_count += 1;
414 const prev_rvalue_rl_count = gen_zir.rvalue_rl_count;
415 const operand = try expr(mod, parent_scope, gen_zir.break_result_loc, rhs);
416 const have_store_to_block = gen_zir.rvalue_rl_count != prev_rvalue_rl_count;
417 const br = try addZirInstTag(mod, parent_scope, src, .@"break", .{
418 .block = block_inst,
419 .operand = operand,
420 });
421 if (gen_zir.break_result_loc == .block_ptr) {
422 try gen_zir.labeled_breaks.append(mod.gpa, br.castTag(.@"break").?);
423
424 if (have_store_to_block) {
425 const inst_list = parent_scope.getGenZIR().instructions.items;
426 const last_inst = inst_list[inst_list.len - 2];
427 const store_inst = last_inst.castTag(.store_to_block_ptr).?;
428 assert(store_inst.positionals.lhs == gen_zir.rl_ptr.?);
429 try gen_zir.labeled_store_to_block_ptr_list.append(mod.gpa, store_inst);
430 }
387431 }
432 return br;
388433 },
389434 .local_val => scope = scope.cast(Scope.LocalVal).?.parent,
390435 .local_ptr => scope = scope.cast(Scope.LocalPtr).?.parent,
......@@ -424,9 +469,9 @@ fn continueExpr(mod: *Module, parent_scope: *Scope, node: *ast.Node.ControlFlowE
424469 continue;
425470 }
426471
427 return addZIRInst(mod, parent_scope, src, zir.Inst.BreakVoid, .{
472 return addZirInstTag(mod, parent_scope, src, .break_void, .{
428473 .block = continue_block,
429 }, .{});
474 });
430475 },
431476 .local_val => scope = scope.cast(Scope.LocalVal).?.parent,
432477 .local_ptr => scope = scope.cast(Scope.LocalPtr).?.parent,
......@@ -526,28 +571,65 @@ fn labeledBlockExpr(
526571 .parent = parent_scope,
527572 .decl = parent_scope.ownerDecl().?,
528573 .arena = gen_zir.arena,
574 .force_comptime = parent_scope.isComptime(),
529575 .instructions = .{},
530 .break_result_loc = rl,
531576 // TODO @as here is working around a stage1 miscompilation bug :(
532577 .label = @as(?Scope.GenZIR.Label, Scope.GenZIR.Label{
533578 .token = block_node.label,
534579 .block_inst = block_inst,
535580 }),
536581 };
582 setBlockResultLoc(&block_scope, rl);
537583 defer block_scope.instructions.deinit(mod.gpa);
584 defer block_scope.labeled_breaks.deinit(mod.gpa);
585 defer block_scope.labeled_store_to_block_ptr_list.deinit(mod.gpa);
538586
539587 try blockExprStmts(mod, &block_scope.base, &block_node.base, block_node.statements());
588
540589 if (!block_scope.label.?.used) {
541590 return mod.fail(parent_scope, tree.token_locs[block_node.label].start, "unused block label", .{});
542591 }
543592
544 block_inst.positionals.body.instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items);
545593 try gen_zir.instructions.append(mod.gpa, &block_inst.base);
546594
547 return &block_inst.base;
595 const strat = rlStrategy(rl, &block_scope);
596 switch (strat.tag) {
597 .break_void => {
598 // The code took advantage of the result location as a pointer.
599 // Turn the break instructions into break_void instructions.
600 for (block_scope.labeled_breaks.items) |br| {
601 br.base.tag = .break_void;
602 }
603 // TODO technically not needed since we changed the tag to break_void but
604 // would be better still to elide the ones that are in this list.
605 try copyBodyNoEliding(&block_inst.positionals.body, block_scope);
606
607 return &block_inst.base;
608 },
609 .break_operand => {
610 // All break operands are values that did not use the result location pointer.
611 if (strat.elide_store_to_block_ptr_instructions) {
612 for (block_scope.labeled_store_to_block_ptr_list.items) |inst| {
613 inst.base.tag = .void_value;
614 }
615 // TODO technically not needed since we changed the tag to void_value but
616 // would be better still to elide the ones that are in this list.
617 }
618 try copyBodyNoEliding(&block_inst.positionals.body, block_scope);
619 switch (rl) {
620 .ref => return &block_inst.base,
621 else => return rvalue(mod, parent_scope, rl, &block_inst.base),
622 }
623 },
624 }
548625}
549626
550fn blockExprStmts(mod: *Module, parent_scope: *Scope, node: *ast.Node, statements: []*ast.Node) !void {
627fn blockExprStmts(
628 mod: *Module,
629 parent_scope: *Scope,
630 node: *ast.Node,
631 statements: []*ast.Node,
632) !void {
551633 const tree = parent_scope.tree();
552634
553635 var block_arena = std.heap.ArenaAllocator.init(mod.gpa);
......@@ -563,8 +645,8 @@ fn blockExprStmts(mod: *Module, parent_scope: *Scope, node: *ast.Node, statement
563645 scope = try varDecl(mod, scope, var_decl_node, &block_arena.allocator);
564646 },
565647 .Assign => try assign(mod, scope, statement.castTag(.Assign).?),
566 .AssignBitAnd => try assignOp(mod, scope, statement.castTag(.AssignBitAnd).?, .bitand),
567 .AssignBitOr => try assignOp(mod, scope, statement.castTag(.AssignBitOr).?, .bitor),
648 .AssignBitAnd => try assignOp(mod, scope, statement.castTag(.AssignBitAnd).?, .bit_and),
649 .AssignBitOr => try assignOp(mod, scope, statement.castTag(.AssignBitOr).?, .bit_or),
568650 .AssignBitShiftLeft => try assignOp(mod, scope, statement.castTag(.AssignBitShiftLeft).?, .shl),
569651 .AssignBitShiftRight => try assignOp(mod, scope, statement.castTag(.AssignBitShiftRight).?, .shr),
570652 .AssignBitXor => try assignOp(mod, scope, statement.castTag(.AssignBitXor).?, .xor),
......@@ -644,6 +726,7 @@ fn varDecl(
644726
645727 // Namespace vars shadowing detection
646728 if (mod.lookupDeclName(scope, ident_name)) |_| {
729 // TODO add note for other definition
647730 return mod.fail(scope, name_src, "redefinition of '{s}'", .{ident_name});
648731 }
649732 const init_node = node.getInitNode() orelse
......@@ -651,36 +734,103 @@ fn varDecl(
651734
652735 switch (tree.token_ids[node.mut_token]) {
653736 .Keyword_const => {
654 var resolve_inferred_alloc: ?*zir.Inst = null;
655737 // Depending on the type of AST the initialization expression is, we may need an lvalue
656738 // or an rvalue as a result location. If it is an rvalue, we can use the instruction as
657739 // the variable, no memory location needed.
658 const result_loc = if (nodeMayNeedMemoryLocation(init_node, scope)) r: {
659 if (node.getTypeNode()) |type_node| {
660 const type_inst = try typeExpr(mod, scope, type_node);
661 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);
662 break :r ResultLoc{ .ptr = alloc };
663 } else {
664 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred);
665 resolve_inferred_alloc = &alloc.base;
666 break :r ResultLoc{ .inferred_ptr = alloc };
667 }
668 } else r: {
669 if (node.getTypeNode()) |type_node|
670 break :r ResultLoc{ .ty = try typeExpr(mod, scope, type_node) }
740 if (!nodeMayNeedMemoryLocation(init_node, scope)) {
741 const result_loc: ResultLoc = if (node.getTypeNode()) |type_node|
742 .{ .ty = try typeExpr(mod, scope, type_node) }
671743 else
672 break :r .none;
744 .none;
745 const init_inst = try expr(mod, scope, result_loc, init_node);
746 const sub_scope = try block_arena.create(Scope.LocalVal);
747 sub_scope.* = .{
748 .parent = scope,
749 .gen_zir = scope.getGenZIR(),
750 .name = ident_name,
751 .inst = init_inst,
752 };
753 return &sub_scope.base;
754 }
755
756 // Detect whether the initialization expression actually uses the
757 // result location pointer.
758 var init_scope: Scope.GenZIR = .{
759 .parent = scope,
760 .decl = scope.ownerDecl().?,
761 .arena = scope.arena(),
762 .force_comptime = scope.isComptime(),
763 .instructions = .{},
673764 };
674 const init_inst = try expr(mod, scope, result_loc, init_node);
765 defer init_scope.instructions.deinit(mod.gpa);
766
767 var resolve_inferred_alloc: ?*zir.Inst = null;
768 var opt_type_inst: ?*zir.Inst = null;
769 if (node.getTypeNode()) |type_node| {
770 const type_inst = try typeExpr(mod, &init_scope.base, type_node);
771 opt_type_inst = type_inst;
772 init_scope.rl_ptr = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
773 } else {
774 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);
775 resolve_inferred_alloc = &alloc.base;
776 init_scope.rl_ptr = &alloc.base;
777 }
778 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
779 const init_inst = try expr(mod, &init_scope.base, init_result_loc, init_node);
780 const parent_zir = &scope.getGenZIR().instructions;
781 if (init_scope.rvalue_rl_count == 1) {
782 // Result location pointer not used. We don't need an alloc for this
783 // const local, and type inference becomes trivial.
784 // Move the init_scope instructions into the parent scope, eliding
785 // the alloc instruction and the store_to_block_ptr instruction.
786 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
787 try parent_zir.ensureCapacity(mod.gpa, expected_len);
788 for (init_scope.instructions.items) |src_inst| {
789 if (src_inst == init_scope.rl_ptr.?) continue;
790 if (src_inst.castTag(.store_to_block_ptr)) |store| {
791 if (store.positionals.lhs == init_scope.rl_ptr.?) continue;
792 }
793 parent_zir.appendAssumeCapacity(src_inst);
794 }
795 assert(parent_zir.items.len == expected_len);
796 const casted_init = if (opt_type_inst) |type_inst|
797 try addZIRBinOp(mod, scope, type_inst.src, .as, type_inst, init_inst)
798 else
799 init_inst;
800
801 const sub_scope = try block_arena.create(Scope.LocalVal);
802 sub_scope.* = .{
803 .parent = scope,
804 .gen_zir = scope.getGenZIR(),
805 .name = ident_name,
806 .inst = casted_init,
807 };
808 return &sub_scope.base;
809 }
810 // The initialization expression took advantage of the result location
811 // of the const local. In this case we will create an alloc and a LocalPtr for it.
812 // Move the init_scope instructions into the parent scope, swapping
813 // store_to_block_ptr for store_to_inferred_ptr.
814 const expected_len = parent_zir.items.len + init_scope.instructions.items.len;
815 try parent_zir.ensureCapacity(mod.gpa, expected_len);
816 for (init_scope.instructions.items) |src_inst| {
817 if (src_inst.castTag(.store_to_block_ptr)) |store| {
818 if (store.positionals.lhs == init_scope.rl_ptr.?) {
819 src_inst.tag = .store_to_inferred_ptr;
820 }
821 }
822 parent_zir.appendAssumeCapacity(src_inst);
823 }
824 assert(parent_zir.items.len == expected_len);
675825 if (resolve_inferred_alloc) |inst| {
676826 _ = try addZIRUnOp(mod, scope, name_src, .resolve_inferred_alloc, inst);
677827 }
678 const sub_scope = try block_arena.create(Scope.LocalVal);
828 const sub_scope = try block_arena.create(Scope.LocalPtr);
679829 sub_scope.* = .{
680830 .parent = scope,
681831 .gen_zir = scope.getGenZIR(),
682832 .name = ident_name,
683 .inst = init_inst,
833 .ptr = init_scope.rl_ptr.?,
684834 };
685835 return &sub_scope.base;
686836 },
......@@ -751,14 +901,14 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr
751901 .val = Value.initTag(.bool_type),
752902 });
753903 const operand = try expr(mod, scope, .{ .ty = bool_type }, node.rhs);
754 return addZIRUnOp(mod, scope, src, .boolnot, operand);
904 return addZIRUnOp(mod, scope, src, .bool_not, operand);
755905}
756906
757907fn bitNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
758908 const tree = scope.tree();
759909 const src = tree.token_locs[node.op_token].start;
760910 const operand = try expr(mod, scope, .none, node.rhs);
761 return addZIRUnOp(mod, scope, src, .bitnot, operand);
911 return addZIRUnOp(mod, scope, src, .bit_not, operand);
762912}
763913
764914fn negation(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp, op_inst_tag: zir.Inst.Tag) InnerError!*zir.Inst {
......@@ -971,6 +1121,7 @@ fn containerDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Con
9711121 .parent = scope,
9721122 .decl = scope.ownerDecl().?,
9731123 .arena = scope.arena(),
1124 .force_comptime = scope.isComptime(),
9741125 .instructions = .{},
9751126 };
9761127 defer gen_scope.instructions.deinit(mod.gpa);
......@@ -1101,7 +1252,7 @@ fn containerDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Con
11011252 if (rl == .ref) {
11021253 return addZIRInst(mod, scope, src, zir.Inst.DeclRef, .{ .decl = decl }, .{});
11031254 } else {
1104 return rlWrap(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{
1255 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{
11051256 .decl = decl,
11061257 }, .{}));
11071258 }
......@@ -1207,24 +1358,15 @@ fn orelseCatchExpr(
12071358 .parent = scope,
12081359 .decl = scope.ownerDecl().?,
12091360 .arena = scope.arena(),
1361 .force_comptime = scope.isComptime(),
12101362 .instructions = .{},
12111363 };
1364 setBlockResultLoc(&block_scope, rl);
12121365 defer block_scope.instructions.deinit(mod.gpa);
12131366
1214 const block = try addZIRInstBlock(mod, scope, src, .block, .{
1215 .instructions = undefined, // populated below
1216 });
1217
1218 // Most result location types can be forwarded directly; however
1219 // if we need to write to a pointer which has an inferred type,
1220 // proper type inference requires peer type resolution on the if's
1221 // branches.
1222 const branch_rl: ResultLoc = switch (rl) {
1223 .discard, .none, .ty, .ptr, .ref => rl,
1224 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block },
1225 };
12261367 // This could be a pointer or value depending on the `rl` parameter.
1227 const operand = try expr(mod, &block_scope.base, branch_rl, lhs);
1368 block_scope.break_count += 1;
1369 const operand = try expr(mod, &block_scope.base, block_scope.break_result_loc, lhs);
12281370 const cond = try addZIRUnOp(mod, &block_scope.base, src, cond_op, operand);
12291371
12301372 const condbr = try addZIRInstSpecial(mod, &block_scope.base, src, zir.Inst.CondBr, .{
......@@ -1233,18 +1375,22 @@ fn orelseCatchExpr(
12331375 .else_body = undefined, // populated below
12341376 }, .{});
12351377
1378 const block = try addZIRInstBlock(mod, scope, src, .block, .{
1379 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),
1380 });
1381
12361382 var then_scope: Scope.GenZIR = .{
12371383 .parent = &block_scope.base,
12381384 .decl = block_scope.decl,
12391385 .arena = block_scope.arena,
1386 .force_comptime = block_scope.force_comptime,
12401387 .instructions = .{},
12411388 };
12421389 defer then_scope.instructions.deinit(mod.gpa);
12431390
12441391 var err_val_scope: Scope.LocalVal = undefined;
12451392 const then_sub_scope = blk: {
1246 const payload = payload_node orelse
1247 break :blk &then_scope.base;
1393 const payload = payload_node orelse break :blk &then_scope.base;
12481394
12491395 const err_name = tree.tokenSlice(payload.castTag(.Payload).?.error_symbol.firstToken());
12501396 if (mem.eql(u8, err_name, "_"))
......@@ -1259,32 +1405,113 @@ fn orelseCatchExpr(
12591405 break :blk &err_val_scope.base;
12601406 };
12611407
1262 _ = try addZIRInst(mod, &then_scope.base, src, zir.Inst.Break, .{
1263 .block = block,
1264 .operand = try expr(mod, then_sub_scope, branch_rl, rhs),
1265 }, .{});
1408 block_scope.break_count += 1;
1409 const then_result = try expr(mod, then_sub_scope, block_scope.break_result_loc, rhs);
12661410
12671411 var else_scope: Scope.GenZIR = .{
12681412 .parent = &block_scope.base,
12691413 .decl = block_scope.decl,
12701414 .arena = block_scope.arena,
1415 .force_comptime = block_scope.force_comptime,
12711416 .instructions = .{},
12721417 };
12731418 defer else_scope.instructions.deinit(mod.gpa);
12741419
12751420 // This could be a pointer or value depending on `unwrap_op`.
12761421 const unwrapped_payload = try addZIRUnOp(mod, &else_scope.base, src, unwrap_op, operand);
1277 _ = try addZIRInst(mod, &else_scope.base, src, zir.Inst.Break, .{
1278 .block = block,
1279 .operand = unwrapped_payload,
1280 }, .{});
12811422
1282 // All branches have been generated, add the instructions to the block.
1283 block.positionals.body.instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items);
1423 return finishThenElseBlock(
1424 mod,
1425 scope,
1426 rl,
1427 &block_scope,
1428 &then_scope,
1429 &else_scope,
1430 &condbr.positionals.then_body,
1431 &condbr.positionals.else_body,
1432 src,
1433 src,
1434 then_result,
1435 unwrapped_payload,
1436 block,
1437 block,
1438 );
1439}
12841440
1285 condbr.positionals.then_body = .{ .instructions = try then_scope.arena.dupe(*zir.Inst, then_scope.instructions.items) };
1286 condbr.positionals.else_body = .{ .instructions = try else_scope.arena.dupe(*zir.Inst, else_scope.instructions.items) };
1287 return &block.base;
1441fn finishThenElseBlock(
1442 mod: *Module,
1443 parent_scope: *Scope,
1444 rl: ResultLoc,
1445 block_scope: *Scope.GenZIR,
1446 then_scope: *Scope.GenZIR,
1447 else_scope: *Scope.GenZIR,
1448 then_body: *zir.Body,
1449 else_body: *zir.Body,
1450 then_src: usize,
1451 else_src: usize,
1452 then_result: *zir.Inst,
1453 else_result: ?*zir.Inst,
1454 main_block: *zir.Inst.Block,
1455 then_break_block: *zir.Inst.Block,
1456) InnerError!*zir.Inst {
1457 // We now have enough information to decide whether the result instruction should
1458 // be communicated via result location pointer or break instructions.
1459 const strat = rlStrategy(rl, block_scope);
1460 switch (strat.tag) {
1461 .break_void => {
1462 if (!then_result.tag.isNoReturn()) {
1463 _ = try addZirInstTag(mod, &then_scope.base, then_src, .break_void, .{
1464 .block = then_break_block,
1465 });
1466 }
1467 if (else_result) |inst| {
1468 if (!inst.tag.isNoReturn()) {
1469 _ = try addZirInstTag(mod, &else_scope.base, else_src, .break_void, .{
1470 .block = main_block,
1471 });
1472 }
1473 } else {
1474 _ = try addZirInstTag(mod, &else_scope.base, else_src, .break_void, .{
1475 .block = main_block,
1476 });
1477 }
1478 assert(!strat.elide_store_to_block_ptr_instructions);
1479 try copyBodyNoEliding(then_body, then_scope.*);
1480 try copyBodyNoEliding(else_body, else_scope.*);
1481 return &main_block.base;
1482 },
1483 .break_operand => {
1484 if (!then_result.tag.isNoReturn()) {
1485 _ = try addZirInstTag(mod, &then_scope.base, then_src, .@"break", .{
1486 .block = then_break_block,
1487 .operand = then_result,
1488 });
1489 }
1490 if (else_result) |inst| {
1491 if (!inst.tag.isNoReturn()) {
1492 _ = try addZirInstTag(mod, &else_scope.base, else_src, .@"break", .{
1493 .block = main_block,
1494 .operand = inst,
1495 });
1496 }
1497 } else {
1498 _ = try addZirInstTag(mod, &else_scope.base, else_src, .break_void, .{
1499 .block = main_block,
1500 });
1501 }
1502 if (strat.elide_store_to_block_ptr_instructions) {
1503 try copyBodyWithElidedStoreBlockPtr(then_body, then_scope.*);
1504 try copyBodyWithElidedStoreBlockPtr(else_body, else_scope.*);
1505 } else {
1506 try copyBodyNoEliding(then_body, then_scope.*);
1507 try copyBodyNoEliding(else_body, else_scope.*);
1508 }
1509 switch (rl) {
1510 .ref => return &main_block.base,
1511 else => return rvalue(mod, parent_scope, rl, &main_block.base),
1512 }
1513 },
1514 }
12881515}
12891516
12901517/// Return whether the identifier names of two tokens are equal. Resolves @""
......@@ -1308,7 +1535,7 @@ pub fn field(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.SimpleI
13081535 .field_name = field_name,
13091536 });
13101537 }
1311 return rlWrap(mod, scope, rl, try addZirInstTag(mod, scope, src, .field_val, .{
1538 return rvalue(mod, scope, rl, try addZirInstTag(mod, scope, src, .field_val, .{
13121539 .object = try expr(mod, scope, .none, node.lhs),
13131540 .field_name = field_name,
13141541 }));
......@@ -1338,7 +1565,7 @@ fn namedField(
13381565 .field_name = try comptimeExpr(mod, scope, string_rl, params[1]),
13391566 });
13401567 }
1341 return rlWrap(mod, scope, rl, try addZirInstTag(mod, scope, src, .field_val_named, .{
1568 return rvalue(mod, scope, rl, try addZirInstTag(mod, scope, src, .field_val_named, .{
13421569 .object = try expr(mod, scope, .none, params[0]),
13431570 .field_name = try comptimeExpr(mod, scope, string_rl, params[1]),
13441571 }));
......@@ -1359,7 +1586,7 @@ fn arrayAccess(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Array
13591586 .index = try expr(mod, scope, index_rl, node.index_expr),
13601587 });
13611588 }
1362 return rlWrap(mod, scope, rl, try addZirInstTag(mod, scope, src, .elem_val, .{
1589 return rvalue(mod, scope, rl, try addZirInstTag(mod, scope, src, .elem_val, .{
13631590 .array = try expr(mod, scope, .none, node.lhs),
13641591 .index = try expr(mod, scope, index_rl, node.index_expr),
13651592 }));
......@@ -1416,7 +1643,7 @@ fn simpleBinOp(
14161643 const rhs = try expr(mod, scope, .none, infix_node.rhs);
14171644
14181645 const result = try addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
1419 return rlWrap(mod, scope, rl, result);
1646 return rvalue(mod, scope, rl, result);
14201647}
14211648
14221649fn boolBinOp(
......@@ -1436,6 +1663,7 @@ fn boolBinOp(
14361663 .parent = scope,
14371664 .decl = scope.ownerDecl().?,
14381665 .arena = scope.arena(),
1666 .force_comptime = scope.isComptime(),
14391667 .instructions = .{},
14401668 };
14411669 defer block_scope.instructions.deinit(mod.gpa);
......@@ -1455,6 +1683,7 @@ fn boolBinOp(
14551683 .parent = scope,
14561684 .decl = block_scope.decl,
14571685 .arena = block_scope.arena,
1686 .force_comptime = block_scope.force_comptime,
14581687 .instructions = .{},
14591688 };
14601689 defer rhs_scope.instructions.deinit(mod.gpa);
......@@ -1469,6 +1698,7 @@ fn boolBinOp(
14691698 .parent = scope,
14701699 .decl = block_scope.decl,
14711700 .arena = block_scope.arena,
1701 .force_comptime = block_scope.force_comptime,
14721702 .instructions = .{},
14731703 };
14741704 defer const_scope.instructions.deinit(mod.gpa);
......@@ -1498,7 +1728,7 @@ fn boolBinOp(
14981728 condbr.positionals.else_body = .{ .instructions = try rhs_scope.arena.dupe(*zir.Inst, rhs_scope.instructions.items) };
14991729 }
15001730
1501 return rlWrap(mod, scope, rl, &block.base);
1731 return rvalue(mod, scope, rl, &block.base);
15021732}
15031733
15041734const CondKind = union(enum) {
......@@ -1582,8 +1812,10 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn
15821812 .parent = scope,
15831813 .decl = scope.ownerDecl().?,
15841814 .arena = scope.arena(),
1815 .force_comptime = scope.isComptime(),
15851816 .instructions = .{},
15861817 };
1818 setBlockResultLoc(&block_scope, rl);
15871819 defer block_scope.instructions.deinit(mod.gpa);
15881820
15891821 const tree = scope.tree();
......@@ -1605,6 +1837,7 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn
16051837 .parent = scope,
16061838 .decl = block_scope.decl,
16071839 .arena = block_scope.arena,
1840 .force_comptime = block_scope.force_comptime,
16081841 .instructions = .{},
16091842 };
16101843 defer then_scope.instructions.deinit(mod.gpa);
......@@ -1612,62 +1845,81 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn
16121845 // declare payload to the then_scope
16131846 const then_sub_scope = try cond_kind.thenSubScope(mod, &then_scope, then_src, if_node.payload);
16141847
1615 // Most result location types can be forwarded directly; however
1616 // if we need to write to a pointer which has an inferred type,
1617 // proper type inference requires peer type resolution on the if's
1618 // branches.
1619 const branch_rl: ResultLoc = switch (rl) {
1620 .discard, .none, .ty, .ptr, .ref => rl,
1621 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block },
1622 };
1623
1624 const then_result = try expr(mod, then_sub_scope, branch_rl, if_node.body);
1625 if (!then_result.tag.isNoReturn()) {
1626 _ = try addZIRInst(mod, then_sub_scope, then_src, zir.Inst.Break, .{
1627 .block = block,
1628 .operand = then_result,
1629 }, .{});
1630 }
1631 condbr.positionals.then_body = .{
1632 .instructions = try then_scope.arena.dupe(*zir.Inst, then_scope.instructions.items),
1633 };
1848 block_scope.break_count += 1;
1849 const then_result = try expr(mod, then_sub_scope, block_scope.break_result_loc, if_node.body);
1850 // We hold off on the break instructions as well as copying the then/else
1851 // instructions into place until we know whether to keep store_to_block_ptr
1852 // instructions or not.
16341853
16351854 var else_scope: Scope.GenZIR = .{
16361855 .parent = scope,
16371856 .decl = block_scope.decl,
16381857 .arena = block_scope.arena,
1858 .force_comptime = block_scope.force_comptime,
16391859 .instructions = .{},
16401860 };
16411861 defer else_scope.instructions.deinit(mod.gpa);
16421862
1643 if (if_node.@"else") |else_node| {
1644 const else_src = tree.token_locs[else_node.body.lastToken()].start;
1863 var else_src: usize = undefined;
1864 var else_sub_scope: *Module.Scope = undefined;
1865 const else_result: ?*zir.Inst = if (if_node.@"else") |else_node| blk: {
1866 else_src = tree.token_locs[else_node.body.lastToken()].start;
16451867 // declare payload to the then_scope
1646 const else_sub_scope = try cond_kind.elseSubScope(mod, &else_scope, else_src, else_node.payload);
1868 else_sub_scope = try cond_kind.elseSubScope(mod, &else_scope, else_src, else_node.payload);
1869
1870 block_scope.break_count += 1;
1871 break :blk try expr(mod, else_sub_scope, block_scope.break_result_loc, else_node.body);
1872 } else blk: {
1873 else_src = tree.token_locs[if_node.lastToken()].start;
1874 else_sub_scope = &else_scope.base;
1875 break :blk null;
1876 };
16471877
1648 const else_result = try expr(mod, else_sub_scope, branch_rl, else_node.body);
1649 if (!else_result.tag.isNoReturn()) {
1650 _ = try addZIRInst(mod, else_sub_scope, else_src, zir.Inst.Break, .{
1651 .block = block,
1652 .operand = else_result,
1653 }, .{});
1878 return finishThenElseBlock(
1879 mod,
1880 scope,
1881 rl,
1882 &block_scope,
1883 &then_scope,
1884 &else_scope,
1885 &condbr.positionals.then_body,
1886 &condbr.positionals.else_body,
1887 then_src,
1888 else_src,
1889 then_result,
1890 else_result,
1891 block,
1892 block,
1893 );
1894}
1895
1896/// Expects to find exactly 1 .store_to_block_ptr instruction.
1897fn copyBodyWithElidedStoreBlockPtr(body: *zir.Body, scope: Module.Scope.GenZIR) !void {
1898 body.* = .{
1899 .instructions = try scope.arena.alloc(*zir.Inst, scope.instructions.items.len - 1),
1900 };
1901 var dst_index: usize = 0;
1902 for (scope.instructions.items) |src_inst| {
1903 if (src_inst.tag != .store_to_block_ptr) {
1904 body.instructions[dst_index] = src_inst;
1905 dst_index += 1;
16541906 }
1655 } else {
1656 // TODO Optimization opportunity: we can avoid an allocation and a memcpy here
1657 // by directly allocating the body for this one instruction.
1658 const else_src = tree.token_locs[if_node.lastToken()].start;
1659 _ = try addZIRInst(mod, &else_scope.base, else_src, zir.Inst.BreakVoid, .{
1660 .block = block,
1661 }, .{});
16621907 }
1663 condbr.positionals.else_body = .{
1664 .instructions = try else_scope.arena.dupe(*zir.Inst, else_scope.instructions.items),
1665 };
1908 assert(dst_index == body.instructions.len);
1909}
16661910
1667 return &block.base;
1911fn copyBodyNoEliding(body: *zir.Body, scope: Module.Scope.GenZIR) !void {
1912 body.* = .{
1913 .instructions = try scope.arena.dupe(*zir.Inst, scope.instructions.items),
1914 };
16681915}
16691916
1670fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.While) InnerError!*zir.Inst {
1917fn whileExpr(
1918 mod: *Module,
1919 scope: *Scope,
1920 rl: ResultLoc,
1921 while_node: *ast.Node.While,
1922) InnerError!*zir.Inst {
16711923 var cond_kind: CondKind = .bool;
16721924 if (while_node.payload) |_| cond_kind = .{ .optional = null };
16731925 if (while_node.@"else") |else_node| {
......@@ -1683,27 +1935,21 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
16831935 if (while_node.inline_token) |tok|
16841936 return mod.failTok(scope, tok, "TODO inline while", .{});
16851937
1686 var expr_scope: Scope.GenZIR = .{
1938 var loop_scope: Scope.GenZIR = .{
16871939 .parent = scope,
16881940 .decl = scope.ownerDecl().?,
16891941 .arena = scope.arena(),
1942 .force_comptime = scope.isComptime(),
16901943 .instructions = .{},
16911944 };
1692 defer expr_scope.instructions.deinit(mod.gpa);
1693
1694 var loop_scope: Scope.GenZIR = .{
1695 .parent = &expr_scope.base,
1696 .decl = expr_scope.decl,
1697 .arena = expr_scope.arena,
1698 .instructions = .{},
1699 .break_result_loc = rl,
1700 };
1945 setBlockResultLoc(&loop_scope, rl);
17011946 defer loop_scope.instructions.deinit(mod.gpa);
17021947
17031948 var continue_scope: Scope.GenZIR = .{
17041949 .parent = &loop_scope.base,
17051950 .decl = loop_scope.decl,
17061951 .arena = loop_scope.arena,
1952 .force_comptime = loop_scope.force_comptime,
17071953 .instructions = .{},
17081954 };
17091955 defer continue_scope.instructions.deinit(mod.gpa);
......@@ -1731,11 +1977,21 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
17311977 if (while_node.continue_expr) |cont_expr| {
17321978 _ = try expr(mod, &loop_scope.base, .{ .ty = void_type }, cont_expr);
17331979 }
1734 const loop = try addZIRInstLoop(mod, &expr_scope.base, while_src, .{
1735 .instructions = try expr_scope.arena.dupe(*zir.Inst, loop_scope.instructions.items),
1736 });
1980 const loop = try scope.arena().create(zir.Inst.Loop);
1981 loop.* = .{
1982 .base = .{
1983 .tag = .loop,
1984 .src = while_src,
1985 },
1986 .positionals = .{
1987 .body = .{
1988 .instructions = try scope.arena().dupe(*zir.Inst, loop_scope.instructions.items),
1989 },
1990 },
1991 .kw_args = .{},
1992 };
17371993 const while_block = try addZIRInstBlock(mod, scope, while_src, .block, .{
1738 .instructions = try expr_scope.arena.dupe(*zir.Inst, expr_scope.instructions.items),
1994 .instructions = try scope.arena().dupe(*zir.Inst, &[1]*zir.Inst{&loop.base}),
17391995 });
17401996 loop_scope.break_block = while_block;
17411997 loop_scope.continue_block = cond_block;
......@@ -1751,6 +2007,7 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
17512007 .parent = &continue_scope.base,
17522008 .decl = continue_scope.decl,
17532009 .arena = continue_scope.arena,
2010 .force_comptime = continue_scope.force_comptime,
17542011 .instructions = .{},
17552012 };
17562013 defer then_scope.instructions.deinit(mod.gpa);
......@@ -1758,61 +2015,51 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
17582015 // declare payload to the then_scope
17592016 const then_sub_scope = try cond_kind.thenSubScope(mod, &then_scope, then_src, while_node.payload);
17602017
1761 // Most result location types can be forwarded directly; however
1762 // if we need to write to a pointer which has an inferred type,
1763 // proper type inference requires peer type resolution on the while's
1764 // branches.
1765 const branch_rl: ResultLoc = switch (rl) {
1766 .discard, .none, .ty, .ptr, .ref => rl,
1767 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = while_block },
1768 };
1769
1770 const then_result = try expr(mod, then_sub_scope, branch_rl, while_node.body);
1771 if (!then_result.tag.isNoReturn()) {
1772 _ = try addZIRInst(mod, then_sub_scope, then_src, zir.Inst.Break, .{
1773 .block = cond_block,
1774 .operand = then_result,
1775 }, .{});
1776 }
1777 condbr.positionals.then_body = .{
1778 .instructions = try then_scope.arena.dupe(*zir.Inst, then_scope.instructions.items),
1779 };
2018 loop_scope.break_count += 1;
2019 const then_result = try expr(mod, then_sub_scope, loop_scope.break_result_loc, while_node.body);
17802020
17812021 var else_scope: Scope.GenZIR = .{
17822022 .parent = &continue_scope.base,
17832023 .decl = continue_scope.decl,
17842024 .arena = continue_scope.arena,
2025 .force_comptime = continue_scope.force_comptime,
17852026 .instructions = .{},
17862027 };
17872028 defer else_scope.instructions.deinit(mod.gpa);
17882029
1789 if (while_node.@"else") |else_node| {
1790 const else_src = tree.token_locs[else_node.body.lastToken()].start;
2030 var else_src: usize = undefined;
2031 const else_result: ?*zir.Inst = if (while_node.@"else") |else_node| blk: {
2032 else_src = tree.token_locs[else_node.body.lastToken()].start;
17912033 // declare payload to the then_scope
17922034 const else_sub_scope = try cond_kind.elseSubScope(mod, &else_scope, else_src, else_node.payload);
17932035
1794 const else_result = try expr(mod, else_sub_scope, branch_rl, else_node.body);
1795 if (!else_result.tag.isNoReturn()) {
1796 _ = try addZIRInst(mod, else_sub_scope, else_src, zir.Inst.Break, .{
1797 .block = while_block,
1798 .operand = else_result,
1799 }, .{});
1800 }
1801 } else {
1802 const else_src = tree.token_locs[while_node.lastToken()].start;
1803 _ = try addZIRInst(mod, &else_scope.base, else_src, zir.Inst.BreakVoid, .{
1804 .block = while_block,
1805 }, .{});
1806 }
1807 condbr.positionals.else_body = .{
1808 .instructions = try else_scope.arena.dupe(*zir.Inst, else_scope.instructions.items),
2036 loop_scope.break_count += 1;
2037 break :blk try expr(mod, else_sub_scope, loop_scope.break_result_loc, else_node.body);
2038 } else blk: {
2039 else_src = tree.token_locs[while_node.lastToken()].start;
2040 break :blk null;
18092041 };
18102042 if (loop_scope.label) |some| {
18112043 if (!some.used) {
18122044 return mod.fail(scope, tree.token_locs[some.token].start, "unused while label", .{});
18132045 }
18142046 }
1815 return &while_block.base;
2047 return finishThenElseBlock(
2048 mod,
2049 scope,
2050 rl,
2051 &loop_scope,
2052 &then_scope,
2053 &else_scope,
2054 &condbr.positionals.then_body,
2055 &condbr.positionals.else_body,
2056 then_src,
2057 else_src,
2058 then_result,
2059 else_result,
2060 while_block,
2061 cond_block,
2062 );
18162063}
18172064
18182065fn forExpr(
......@@ -1828,48 +2075,42 @@ fn forExpr(
18282075 if (for_node.inline_token) |tok|
18292076 return mod.failTok(scope, tok, "TODO inline for", .{});
18302077
1831 var for_scope: Scope.GenZIR = .{
1832 .parent = scope,
1833 .decl = scope.ownerDecl().?,
1834 .arena = scope.arena(),
1835 .instructions = .{},
1836 };
1837 defer for_scope.instructions.deinit(mod.gpa);
1838
18392078 // setup variables and constants
18402079 const tree = scope.tree();
18412080 const for_src = tree.token_locs[for_node.for_token].start;
18422081 const index_ptr = blk: {
1843 const usize_type = try addZIRInstConst(mod, &for_scope.base, for_src, .{
2082 const usize_type = try addZIRInstConst(mod, scope, for_src, .{
18442083 .ty = Type.initTag(.type),
18452084 .val = Value.initTag(.usize_type),
18462085 });
1847 const index_ptr = try addZIRUnOp(mod, &for_scope.base, for_src, .alloc, usize_type);
2086 const index_ptr = try addZIRUnOp(mod, scope, for_src, .alloc, usize_type);
18482087 // initialize to zero
1849 const zero = try addZIRInstConst(mod, &for_scope.base, for_src, .{
2088 const zero = try addZIRInstConst(mod, scope, for_src, .{
18502089 .ty = Type.initTag(.usize),
18512090 .val = Value.initTag(.zero),
18522091 });
1853 _ = try addZIRBinOp(mod, &for_scope.base, for_src, .store, index_ptr, zero);
2092 _ = try addZIRBinOp(mod, scope, for_src, .store, index_ptr, zero);
18542093 break :blk index_ptr;
18552094 };
1856 const array_ptr = try expr(mod, &for_scope.base, .ref, for_node.array_expr);
2095 const array_ptr = try expr(mod, scope, .ref, for_node.array_expr);
18572096 const cond_src = tree.token_locs[for_node.array_expr.firstToken()].start;
1858 const len = try addZIRUnOp(mod, &for_scope.base, cond_src, .indexable_ptr_len, array_ptr);
2097 const len = try addZIRUnOp(mod, scope, cond_src, .indexable_ptr_len, array_ptr);
18592098
18602099 var loop_scope: Scope.GenZIR = .{
1861 .parent = &for_scope.base,
1862 .decl = for_scope.decl,
1863 .arena = for_scope.arena,
2100 .parent = scope,
2101 .decl = scope.ownerDecl().?,
2102 .arena = scope.arena(),
2103 .force_comptime = scope.isComptime(),
18642104 .instructions = .{},
1865 .break_result_loc = rl,
18662105 };
2106 setBlockResultLoc(&loop_scope, rl);
18672107 defer loop_scope.instructions.deinit(mod.gpa);
18682108
18692109 var cond_scope: Scope.GenZIR = .{
18702110 .parent = &loop_scope.base,
18712111 .decl = loop_scope.decl,
18722112 .arena = loop_scope.arena,
2113 .force_comptime = loop_scope.force_comptime,
18732114 .instructions = .{},
18742115 };
18752116 defer cond_scope.instructions.deinit(mod.gpa);
......@@ -1896,12 +2137,21 @@ fn forExpr(
18962137 const index_plus_one = try addZIRBinOp(mod, &loop_scope.base, for_src, .add, index_2, one);
18972138 _ = try addZIRBinOp(mod, &loop_scope.base, for_src, .store, index_ptr, index_plus_one);
18982139
1899 // looping stuff
1900 const loop = try addZIRInstLoop(mod, &for_scope.base, for_src, .{
1901 .instructions = try for_scope.arena.dupe(*zir.Inst, loop_scope.instructions.items),
1902 });
2140 const loop = try scope.arena().create(zir.Inst.Loop);
2141 loop.* = .{
2142 .base = .{
2143 .tag = .loop,
2144 .src = for_src,
2145 },
2146 .positionals = .{
2147 .body = .{
2148 .instructions = try scope.arena().dupe(*zir.Inst, loop_scope.instructions.items),
2149 },
2150 },
2151 .kw_args = .{},
2152 };
19032153 const for_block = try addZIRInstBlock(mod, scope, for_src, .block, .{
1904 .instructions = try for_scope.arena.dupe(*zir.Inst, for_scope.instructions.items),
2154 .instructions = try scope.arena().dupe(*zir.Inst, &[1]*zir.Inst{&loop.base}),
19052155 });
19062156 loop_scope.break_block = for_block;
19072157 loop_scope.continue_block = cond_block;
......@@ -1918,19 +2168,11 @@ fn forExpr(
19182168 .parent = &cond_scope.base,
19192169 .decl = cond_scope.decl,
19202170 .arena = cond_scope.arena,
2171 .force_comptime = cond_scope.force_comptime,
19212172 .instructions = .{},
19222173 };
19232174 defer then_scope.instructions.deinit(mod.gpa);
19242175
1925 // Most result location types can be forwarded directly; however
1926 // if we need to write to a pointer which has an inferred type,
1927 // proper type inference requires peer type resolution on the while's
1928 // branches.
1929 const branch_rl: ResultLoc = switch (rl) {
1930 .discard, .none, .ty, .ptr, .ref => rl,
1931 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = for_block },
1932 };
1933
19342176 var index_scope: Scope.LocalPtr = undefined;
19352177 const then_sub_scope = blk: {
19362178 const payload = for_node.payload.castTag(.PointerIndexPayload).?;
......@@ -1959,319 +2201,49 @@ fn forExpr(
19592201 break :blk &index_scope.base;
19602202 };
19612203
1962 const then_result = try expr(mod, then_sub_scope, branch_rl, for_node.body);
1963 if (!then_result.tag.isNoReturn()) {
1964 _ = try addZIRInst(mod, then_sub_scope, then_src, zir.Inst.Break, .{
1965 .block = cond_block,
1966 .operand = then_result,
1967 }, .{});
1968 }
1969 condbr.positionals.then_body = .{
1970 .instructions = try then_scope.arena.dupe(*zir.Inst, then_scope.instructions.items),
1971 };
2204 loop_scope.break_count += 1;
2205 const then_result = try expr(mod, then_sub_scope, loop_scope.break_result_loc, for_node.body);
19722206
19732207 // else branch
19742208 var else_scope: Scope.GenZIR = .{
19752209 .parent = &cond_scope.base,
19762210 .decl = cond_scope.decl,
19772211 .arena = cond_scope.arena,
2212 .force_comptime = cond_scope.force_comptime,
19782213 .instructions = .{},
19792214 };
19802215 defer else_scope.instructions.deinit(mod.gpa);
19812216
1982 if (for_node.@"else") |else_node| {
1983 const else_src = tree.token_locs[else_node.body.lastToken()].start;
1984 const else_result = try expr(mod, &else_scope.base, branch_rl, else_node.body);
1985 if (!else_result.tag.isNoReturn()) {
1986 _ = try addZIRInst(mod, &else_scope.base, else_src, zir.Inst.Break, .{
1987 .block = for_block,
1988 .operand = else_result,
1989 }, .{});
1990 }
1991 } else {
1992 const else_src = tree.token_locs[for_node.lastToken()].start;
1993 _ = try addZIRInst(mod, &else_scope.base, else_src, zir.Inst.BreakVoid, .{
1994 .block = for_block,
1995 }, .{});
1996 }
1997 condbr.positionals.else_body = .{
1998 .instructions = try else_scope.arena.dupe(*zir.Inst, else_scope.instructions.items),
2217 var else_src: usize = undefined;
2218 const else_result: ?*zir.Inst = if (for_node.@"else") |else_node| blk: {
2219 else_src = tree.token_locs[else_node.body.lastToken()].start;
2220 loop_scope.break_count += 1;
2221 break :blk try expr(mod, &else_scope.base, loop_scope.break_result_loc, else_node.body);
2222 } else blk: {
2223 else_src = tree.token_locs[for_node.lastToken()].start;
2224 break :blk null;
19992225 };
20002226 if (loop_scope.label) |some| {
20012227 if (!some.used) {
20022228 return mod.fail(scope, tree.token_locs[some.token].start, "unused for label", .{});
20032229 }
20042230 }
2005 return &for_block.base;
2006}
2007
2008fn getRangeNode(node: *ast.Node) ?*ast.Node.SimpleInfixOp {
2009 var cur = node;
2010 while (true) {
2011 switch (cur.tag) {
2012 .Range => return @fieldParentPtr(ast.Node.SimpleInfixOp, "base", cur),
2013 .GroupedExpression => cur = @fieldParentPtr(ast.Node.GroupedExpression, "base", cur).expr,
2014 else => return null,
2015 }
2016 }
2017}
2018
2019fn switchExpr(mod: *Module, scope: *Scope, rl: ResultLoc, switch_node: *ast.Node.Switch) InnerError!*zir.Inst {
2020 var block_scope: Scope.GenZIR = .{
2021 .parent = scope,
2022 .decl = scope.ownerDecl().?,
2023 .arena = scope.arena(),
2024 .instructions = .{},
2025 };
2026 defer block_scope.instructions.deinit(mod.gpa);
2027
2028 const tree = scope.tree();
2029 const switch_src = tree.token_locs[switch_node.switch_token].start;
2030 const target_ptr = try expr(mod, &block_scope.base, .ref, switch_node.expr);
2031 const target = try addZIRUnOp(mod, &block_scope.base, target_ptr.src, .deref, target_ptr);
2032 // Add the switch instruction here so that it comes before any range checks.
2033 const switch_inst = (try addZIRInst(mod, &block_scope.base, switch_src, zir.Inst.SwitchBr, .{
2034 .target_ptr = target_ptr,
2035 .cases = undefined, // populated below
2036 .items = &[_]*zir.Inst{}, // populated below
2037 .else_body = undefined, // populated below
2038 }, .{})).castTag(.switchbr).?;
2039
2040 var items = std.ArrayList(*zir.Inst).init(mod.gpa);
2041 defer items.deinit();
2042 var cases = std.ArrayList(zir.Inst.SwitchBr.Case).init(mod.gpa);
2043 defer cases.deinit();
2044
2045 // Add comptime block containing all prong items first,
2046 const item_block = try addZIRInstBlock(mod, scope, switch_src, .block_comptime_flat, .{
2047 .instructions = undefined, // populated below
2048 });
2049 // then add block containing the switch.
2050 const block = try addZIRInstBlock(mod, scope, switch_src, .block, .{
2051 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),
2052 });
2053
2054 // Most result location types can be forwarded directly; however
2055 // if we need to write to a pointer which has an inferred type,
2056 // proper type inference requires peer type resolution on the switch case.
2057 const case_rl: ResultLoc = switch (rl) {
2058 .discard, .none, .ty, .ptr, .ref => rl,
2059 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block },
2060 };
2061
2062 var item_scope: Scope.GenZIR = .{
2063 .parent = scope,
2064 .decl = scope.ownerDecl().?,
2065 .arena = scope.arena(),
2066 .instructions = .{},
2067 };
2068 defer item_scope.instructions.deinit(mod.gpa);
2069
2070 var case_scope: Scope.GenZIR = .{
2071 .parent = scope,
2072 .decl = block_scope.decl,
2073 .arena = block_scope.arena,
2074 .instructions = .{},
2075 };
2076 defer case_scope.instructions.deinit(mod.gpa);
2077
2078 var else_scope: Scope.GenZIR = .{
2079 .parent = scope,
2080 .decl = block_scope.decl,
2081 .arena = block_scope.arena,
2082 .instructions = .{},
2083 };
2084 defer else_scope.instructions.deinit(mod.gpa);
2085
2086 // first we gather all the switch items and check else/'_' prongs
2087 var else_src: ?usize = null;
2088 var underscore_src: ?usize = null;
2089 var first_range: ?*zir.Inst = null;
2090 var special_case: ?*ast.Node.SwitchCase = null;
2091 for (switch_node.cases()) |uncasted_case| {
2092 const case = uncasted_case.castTag(.SwitchCase).?;
2093 const case_src = tree.token_locs[case.firstToken()].start;
2094 // reset without freeing to reduce allocations.
2095 case_scope.instructions.items.len = 0;
2096 assert(case.items_len != 0);
2097
2098 // Check for else/_ prong, those are handled last.
2099 if (case.items_len == 1 and case.items()[0].tag == .SwitchElse) {
2100 if (else_src) |src| {
2101 const msg = msg: {
2102 const msg = try mod.errMsg(
2103 scope,
2104 case_src,
2105 "multiple else prongs in switch expression",
2106 .{},
2107 );
2108 errdefer msg.destroy(mod.gpa);
2109 try mod.errNote(scope, src, msg, "previous else prong is here", .{});
2110 break :msg msg;
2111 };
2112 return mod.failWithOwnedErrorMsg(scope, msg);
2113 }
2114 else_src = case_src;
2115 special_case = case;
2116 continue;
2117 } else if (case.items_len == 1 and case.items()[0].tag == .Identifier and
2118 mem.eql(u8, tree.tokenSlice(case.items()[0].firstToken()), "_"))
2119 {
2120 if (underscore_src) |src| {
2121 const msg = msg: {
2122 const msg = try mod.errMsg(
2123 scope,
2124 case_src,
2125 "multiple '_' prongs in switch expression",
2126 .{},
2127 );
2128 errdefer msg.destroy(mod.gpa);
2129 try mod.errNote(scope, src, msg, "previous '_' prong is here", .{});
2130 break :msg msg;
2131 };
2132 return mod.failWithOwnedErrorMsg(scope, msg);
2133 }
2134 underscore_src = case_src;
2135 special_case = case;
2136 continue;
2137 }
2138
2139 if (else_src) |some_else| {
2140 if (underscore_src) |some_underscore| {
2141 const msg = msg: {
2142 const msg = try mod.errMsg(
2143 scope,
2144 switch_src,
2145 "else and '_' prong in switch expression",
2146 .{},
2147 );
2148 errdefer msg.destroy(mod.gpa);
2149 try mod.errNote(scope, some_else, msg, "else prong is here", .{});
2150 try mod.errNote(scope, some_underscore, msg, "'_' prong is here", .{});
2151 break :msg msg;
2152 };
2153 return mod.failWithOwnedErrorMsg(scope, msg);
2154 }
2155 }
2156
2157 // If this is a simple one item prong then it is handled by the switchbr.
2158 if (case.items_len == 1 and getRangeNode(case.items()[0]) == null) {
2159 const item = try expr(mod, &item_scope.base, .none, case.items()[0]);
2160 try items.append(item);
2161 try switchCaseExpr(mod, &case_scope.base, case_rl, block, case);
2162
2163 try cases.append(.{
2164 .item = item,
2165 .body = .{ .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items) },
2166 });
2167 continue;
2168 }
2169
2170 // TODO if the case has few items and no ranges it might be better
2171 // to just handle them as switch prongs.
2172
2173 // Check if the target matches any of the items.
2174 // 1, 2, 3..6 will result in
2175 // target == 1 or target == 2 or (target >= 3 and target <= 6)
2176 var any_ok: ?*zir.Inst = null;
2177 for (case.items()) |item| {
2178 if (getRangeNode(item)) |range| {
2179 const start = try expr(mod, &item_scope.base, .none, range.lhs);
2180 const end = try expr(mod, &item_scope.base, .none, range.rhs);
2181 const range_src = tree.token_locs[range.op_token].start;
2182 const range_inst = try addZIRBinOp(mod, &item_scope.base, range_src, .switch_range, start, end);
2183 try items.append(range_inst);
2184 if (first_range == null) first_range = range_inst;
2185
2186 // target >= start and target <= end
2187 const range_start_ok = try addZIRBinOp(mod, &else_scope.base, range_src, .cmp_gte, target, start);
2188 const range_end_ok = try addZIRBinOp(mod, &else_scope.base, range_src, .cmp_lte, target, end);
2189 const range_ok = try addZIRBinOp(mod, &else_scope.base, range_src, .booland, range_start_ok, range_end_ok);
2190
2191 if (any_ok) |some| {
2192 any_ok = try addZIRBinOp(mod, &else_scope.base, range_src, .boolor, some, range_ok);
2193 } else {
2194 any_ok = range_ok;
2195 }
2196 continue;
2197 }
2198
2199 const item_inst = try expr(mod, &item_scope.base, .none, item);
2200 try items.append(item_inst);
2201 const cpm_ok = try addZIRBinOp(mod, &else_scope.base, item_inst.src, .cmp_eq, target, item_inst);
2202
2203 if (any_ok) |some| {
2204 any_ok = try addZIRBinOp(mod, &else_scope.base, item_inst.src, .boolor, some, cpm_ok);
2205 } else {
2206 any_ok = cpm_ok;
2207 }
2208 }
2209
2210 const condbr = try addZIRInstSpecial(mod, &case_scope.base, case_src, zir.Inst.CondBr, .{
2211 .condition = any_ok.?,
2212 .then_body = undefined, // populated below
2213 .else_body = undefined, // populated below
2214 }, .{});
2215 const cond_block = try addZIRInstBlock(mod, &else_scope.base, case_src, .block, .{
2216 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),
2217 });
2218
2219 // reset cond_scope for then_body
2220 case_scope.instructions.items.len = 0;
2221 try switchCaseExpr(mod, &case_scope.base, case_rl, block, case);
2222 condbr.positionals.then_body = .{
2223 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),
2224 };
2225
2226 // reset cond_scope for else_body
2227 case_scope.instructions.items.len = 0;
2228 _ = try addZIRInst(mod, &case_scope.base, case_src, zir.Inst.BreakVoid, .{
2229 .block = cond_block,
2230 }, .{});
2231 condbr.positionals.else_body = .{
2232 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),
2233 };
2234 }
2235
2236 // Generate else block or a break last to finish the block.
2237 if (special_case) |case| {
2238 try switchCaseExpr(mod, &else_scope.base, case_rl, block, case);
2239 } else {
2240 // Not handling all possible cases is a compile error.
2241 _ = try addZIRNoOp(mod, &else_scope.base, switch_src, .unreach_nocheck);
2242 }
2243
2244 // All items have been generated, add the instructions to the comptime block.
2245 item_block.positionals.body = .{
2246 .instructions = try block_scope.arena.dupe(*zir.Inst, item_scope.instructions.items),
2247 };
2248
2249 // Actually populate switch instruction values.
2250 if (else_src != null) switch_inst.kw_args.special_prong = .@"else";
2251 if (underscore_src != null) switch_inst.kw_args.special_prong = .underscore;
2252 switch_inst.positionals.cases = try block_scope.arena.dupe(zir.Inst.SwitchBr.Case, cases.items);
2253 switch_inst.positionals.items = try block_scope.arena.dupe(*zir.Inst, items.items);
2254 switch_inst.kw_args.range = first_range;
2255 switch_inst.positionals.else_body = .{
2256 .instructions = try block_scope.arena.dupe(*zir.Inst, else_scope.instructions.items),
2257 };
2258 return &block.base;
2259}
2260
2261fn switchCaseExpr(mod: *Module, scope: *Scope, rl: ResultLoc, block: *zir.Inst.Block, case: *ast.Node.SwitchCase) !void {
2262 const tree = scope.tree();
2263 const case_src = tree.token_locs[case.firstToken()].start;
2264 if (case.payload != null) {
2265 return mod.fail(scope, case_src, "TODO switch case payload capture", .{});
2266 }
2267
2268 const case_body = try expr(mod, scope, rl, case.expr);
2269 if (!case_body.tag.isNoReturn()) {
2270 _ = try addZIRInst(mod, scope, case_src, zir.Inst.Break, .{
2271 .block = block,
2272 .operand = case_body,
2273 }, .{});
2274 }
2231 return finishThenElseBlock(
2232 mod,
2233 scope,
2234 rl,
2235 &loop_scope,
2236 &then_scope,
2237 &else_scope,
2238 &condbr.positionals.then_body,
2239 &condbr.positionals.else_body,
2240 then_src,
2241 else_src,
2242 then_result,
2243 else_result,
2244 for_block,
2245 cond_block,
2246 );
22752247}
22762248
22772249fn ret(mod: *Module, scope: *Scope, cfe: *ast.Node.ControlFlowExpression) InnerError!*zir.Inst {
......@@ -2288,7 +2260,7 @@ fn ret(mod: *Module, scope: *Scope, cfe: *ast.Node.ControlFlowExpression) InnerE
22882260 return addZIRUnOp(mod, scope, src, .@"return", operand);
22892261 }
22902262 } else {
2291 return addZIRNoOp(mod, scope, src, .returnvoid);
2263 return addZIRNoOp(mod, scope, src, .return_void);
22922264 }
22932265}
22942266
......@@ -2305,7 +2277,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23052277
23062278 if (getSimplePrimitiveValue(ident_name)) |typed_value| {
23072279 const result = try addZIRInstConst(mod, scope, src, typed_value);
2308 return rlWrap(mod, scope, rl, result);
2280 return rvalue(mod, scope, rl, result);
23092281 }
23102282
23112283 if (ident_name.len >= 2) integer: {
......@@ -2327,7 +2299,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23272299 32 => if (is_signed) Value.initTag(.i32_type) else Value.initTag(.u32_type),
23282300 64 => if (is_signed) Value.initTag(.i64_type) else Value.initTag(.u64_type),
23292301 else => {
2330 return rlWrap(mod, scope, rl, try addZIRInstConst(mod, scope, src, .{
2302 return rvalue(mod, scope, rl, try addZIRInstConst(mod, scope, src, .{
23312303 .ty = Type.initTag(.type),
23322304 .val = try Value.Tag.int_type.create(scope.arena(), .{
23332305 .signed = is_signed,
......@@ -2340,7 +2312,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23402312 .ty = Type.initTag(.type),
23412313 .val = val,
23422314 });
2343 return rlWrap(mod, scope, rl, result);
2315 return rvalue(mod, scope, rl, result);
23442316 }
23452317 }
23462318
......@@ -2351,7 +2323,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23512323 .local_val => {
23522324 const local_val = s.cast(Scope.LocalVal).?;
23532325 if (mem.eql(u8, local_val.name, ident_name)) {
2354 return rlWrap(mod, scope, rl, local_val.inst);
2326 return rvalue(mod, scope, rl, local_val.inst);
23552327 }
23562328 s = local_val.parent;
23572329 },
......@@ -2360,7 +2332,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23602332 if (mem.eql(u8, local_ptr.name, ident_name)) {
23612333 if (rl == .ref) return local_ptr.ptr;
23622334 const loaded = try addZIRUnOp(mod, scope, src, .deref, local_ptr.ptr);
2363 return rlWrap(mod, scope, rl, loaded);
2335 return rvalue(mod, scope, rl, loaded);
23642336 }
23652337 s = local_ptr.parent;
23662338 },
......@@ -2373,7 +2345,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
23732345 if (rl == .ref) {
23742346 return addZIRInst(mod, scope, src, zir.Inst.DeclRef, .{ .decl = decl }, .{});
23752347 } else {
2376 return rlWrap(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{
2348 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclVal, .{
23772349 .decl = decl,
23782350 }, .{}));
23792351 }
......@@ -2590,7 +2562,7 @@ fn simpleCast(
25902562 const dest_type = try typeExpr(mod, scope, params[0]);
25912563 const rhs = try expr(mod, scope, .none, params[1]);
25922564 const result = try addZIRBinOp(mod, scope, src, inst_tag, dest_type, rhs);
2593 return rlWrap(mod, scope, rl, result);
2565 return rvalue(mod, scope, rl, result);
25942566}
25952567
25962568fn ptrToInt(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
......@@ -2601,31 +2573,30 @@ fn ptrToInt(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError
26012573 return addZIRUnOp(mod, scope, src, .ptrtoint, operand);
26022574}
26032575
2604fn as(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
2576fn as(
2577 mod: *Module,
2578 scope: *Scope,
2579 rl: ResultLoc,
2580 call: *ast.Node.BuiltinCall,
2581) InnerError!*zir.Inst {
26052582 try ensureBuiltinParamCount(mod, scope, call, 2);
26062583 const tree = scope.tree();
26072584 const src = tree.token_locs[call.builtin_token].start;
26082585 const params = call.params();
26092586 const dest_type = try typeExpr(mod, scope, params[0]);
26102587 switch (rl) {
2611 .none => return try expr(mod, scope, .{ .ty = dest_type }, params[1]),
2612 .discard => {
2613 const result = try expr(mod, scope, .{ .ty = dest_type }, params[1]);
2614 _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result);
2615 return result;
2616 },
2617 .ref => {
2618 const result = try expr(mod, scope, .{ .ty = dest_type }, params[1]);
2619 return addZIRUnOp(mod, scope, result.src, .ref, result);
2620 },
2621 .ty => |result_ty| {
2588 .none, .discard, .ref, .ty => {
26222589 const result = try expr(mod, scope, .{ .ty = dest_type }, params[1]);
2623 return addZIRBinOp(mod, scope, src, .as, result_ty, result);
2590 return rvalue(mod, scope, rl, result);
26242591 },
2592
26252593 .ptr => |result_ptr| {
2626 const casted_result_ptr = try addZIRBinOp(mod, scope, src, .coerce_result_ptr, dest_type, result_ptr);
2627 return expr(mod, scope, .{ .ptr = casted_result_ptr }, params[1]);
2594 return asRlPtr(mod, scope, rl, src, result_ptr, params[1], dest_type);
2595 },
2596 .block_ptr => |block_scope| {
2597 return asRlPtr(mod, scope, rl, src, block_scope.rl_ptr.?, params[1], dest_type);
26282598 },
2599
26292600 .bitcasted_ptr => |bitcasted_ptr| {
26302601 // TODO here we should be able to resolve the inference; we now have a type for the result.
26312602 return mod.failTok(scope, call.builtin_token, "TODO implement @as with result location @bitCast", .{});
......@@ -2634,13 +2605,50 @@ fn as(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) I
26342605 // TODO here we should be able to resolve the inference; we now have a type for the result.
26352606 return mod.failTok(scope, call.builtin_token, "TODO implement @as with inferred-type result location pointer", .{});
26362607 },
2637 .block_ptr => |block_ptr| {
2638 const casted_block_ptr = try addZIRInst(mod, scope, src, zir.Inst.CoerceResultBlockPtr, .{
2639 .dest_type = dest_type,
2640 .block = block_ptr,
2641 }, .{});
2642 return expr(mod, scope, .{ .ptr = casted_block_ptr }, params[1]);
2643 },
2608 }
2609}
2610
2611fn asRlPtr(
2612 mod: *Module,
2613 scope: *Scope,
2614 rl: ResultLoc,
2615 src: usize,
2616 result_ptr: *zir.Inst,
2617 operand_node: *ast.Node,
2618 dest_type: *zir.Inst,
2619) InnerError!*zir.Inst {
2620 // Detect whether this expr() call goes into rvalue() to store the result into the
2621 // result location. If it does, elide the coerce_result_ptr instruction
2622 // as well as the store instruction, instead passing the result as an rvalue.
2623 var as_scope: Scope.GenZIR = .{
2624 .parent = scope,
2625 .decl = scope.ownerDecl().?,
2626 .arena = scope.arena(),
2627 .force_comptime = scope.isComptime(),
2628 .instructions = .{},
2629 };
2630 defer as_scope.instructions.deinit(mod.gpa);
2631
2632 as_scope.rl_ptr = try addZIRBinOp(mod, &as_scope.base, src, .coerce_result_ptr, dest_type, result_ptr);
2633 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
2634 const parent_zir = &scope.getGenZIR().instructions;
2635 if (as_scope.rvalue_rl_count == 1) {
2636 // Busted! This expression didn't actually need a pointer.
2637 const expected_len = parent_zir.items.len + as_scope.instructions.items.len - 2;
2638 try parent_zir.ensureCapacity(mod.gpa, expected_len);
2639 for (as_scope.instructions.items) |src_inst| {
2640 if (src_inst == as_scope.rl_ptr.?) continue;
2641 if (src_inst.castTag(.store_to_block_ptr)) |store| {
2642 if (store.positionals.lhs == as_scope.rl_ptr.?) continue;
2643 }
2644 parent_zir.appendAssumeCapacity(src_inst);
2645 }
2646 assert(parent_zir.items.len == expected_len);
2647 const casted_result = try addZIRBinOp(mod, scope, dest_type.src, .as, dest_type, result);
2648 return rvalue(mod, scope, rl, casted_result);
2649 } else {
2650 try parent_zir.appendSlice(mod.gpa, as_scope.instructions.items);
2651 return result;
26442652 }
26452653}
26462654
......@@ -2703,7 +2711,7 @@ fn compileError(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerE
27032711 const src = tree.token_locs[call.builtin_token].start;
27042712 const params = call.params();
27052713 const target = try expr(mod, scope, .none, params[0]);
2706 return addZIRUnOp(mod, scope, src, .compileerror, target);
2714 return addZIRUnOp(mod, scope, src, .compile_error, target);
27072715}
27082716
27092717fn setEvalBranchQuota(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
......@@ -2728,12 +2736,12 @@ fn typeOf(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCal
27282736 return mod.failTok(scope, call.builtin_token, "expected at least 1 argument, found 0", .{});
27292737 }
27302738 if (params.len == 1) {
2731 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .typeof, try expr(mod, scope, .none, params[0])));
2739 return rvalue(mod, scope, rl, try addZIRUnOp(mod, scope, src, .typeof, try expr(mod, scope, .none, params[0])));
27322740 }
27332741 var items = try arena.alloc(*zir.Inst, params.len);
27342742 for (params) |param, param_i|
27352743 items[param_i] = try expr(mod, scope, .none, param);
2736 return rlWrap(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.TypeOfPeer, .{ .items = items }, .{}));
2744 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.TypeOfPeer, .{ .items = items }, .{}));
27372745}
27382746fn compileLog(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
27392747 const tree = scope.tree();
......@@ -2756,7 +2764,7 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
27562764 // Also, some builtins have a variable number of parameters.
27572765
27582766 if (mem.eql(u8, builtin_name, "@ptrToInt")) {
2759 return rlWrap(mod, scope, rl, try ptrToInt(mod, scope, call));
2767 return rvalue(mod, scope, rl, try ptrToInt(mod, scope, call));
27602768 } else if (mem.eql(u8, builtin_name, "@as")) {
27612769 return as(mod, scope, rl, call);
27622770 } else if (mem.eql(u8, builtin_name, "@floatCast")) {
......@@ -2769,9 +2777,9 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
27692777 return typeOf(mod, scope, rl, call);
27702778 } else if (mem.eql(u8, builtin_name, "@breakpoint")) {
27712779 const src = tree.token_locs[call.builtin_token].start;
2772 return rlWrap(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));
2780 return rvalue(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));
27732781 } else if (mem.eql(u8, builtin_name, "@import")) {
2774 return rlWrap(mod, scope, rl, try import(mod, scope, call));
2782 return rvalue(mod, scope, rl, try import(mod, scope, call));
27752783 } else if (mem.eql(u8, builtin_name, "@compileError")) {
27762784 return compileError(mod, scope, call);
27772785 } else if (mem.eql(u8, builtin_name, "@setEvalBranchQuota")) {
......@@ -2806,13 +2814,13 @@ fn callExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Call) In
28062814 .args = args,
28072815 }, .{});
28082816 // TODO function call with result location
2809 return rlWrap(mod, scope, rl, result);
2817 return rvalue(mod, scope, rl, result);
28102818}
28112819
28122820fn unreach(mod: *Module, scope: *Scope, unreach_node: *ast.Node.OneToken) InnerError!*zir.Inst {
28132821 const tree = scope.tree();
28142822 const src = tree.token_locs[unreach_node.token].start;
2815 return addZIRNoOp(mod, scope, src, .@"unreachable");
2823 return addZIRNoOp(mod, scope, src, .unreachable_safe);
28162824}
28172825
28182826fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {
......@@ -3099,7 +3107,7 @@ fn nodeMayNeedMemoryLocation(start_node: *ast.Node, scope: *Scope) bool {
30993107/// result locations must call this function on their result.
31003108/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.
31013109/// If the `ResultLoc` is `ty`, it will coerce the result to the type.
3102fn rlWrap(mod: *Module, scope: *Scope, rl: ResultLoc, result: *zir.Inst) InnerError!*zir.Inst {
3110fn rvalue(mod: *Module, scope: *Scope, rl: ResultLoc, result: *zir.Inst) InnerError!*zir.Inst {
31033111 switch (rl) {
31043112 .none => return result,
31053113 .discard => {
......@@ -3113,42 +3121,97 @@ fn rlWrap(mod: *Module, scope: *Scope, rl: ResultLoc, result: *zir.Inst) InnerEr
31133121 },
31143122 .ty => |ty_inst| return addZIRBinOp(mod, scope, result.src, .as, ty_inst, result),
31153123 .ptr => |ptr_inst| {
3116 const casted_result = try addZIRInst(mod, scope, result.src, zir.Inst.CoerceToPtrElem, .{
3117 .ptr = ptr_inst,
3118 .value = result,
3119 }, .{});
3120 _ = try addZIRBinOp(mod, scope, result.src, .store, ptr_inst, casted_result);
3121 return casted_result;
3124 _ = try addZIRBinOp(mod, scope, result.src, .store, ptr_inst, result);
3125 return result;
31223126 },
31233127 .bitcasted_ptr => |bitcasted_ptr| {
3124 return mod.fail(scope, result.src, "TODO implement rlWrap .bitcasted_ptr", .{});
3128 return mod.fail(scope, result.src, "TODO implement rvalue .bitcasted_ptr", .{});
31253129 },
31263130 .inferred_ptr => |alloc| {
31273131 _ = try addZIRBinOp(mod, scope, result.src, .store_to_inferred_ptr, &alloc.base, result);
31283132 return result;
31293133 },
3130 .block_ptr => |block_ptr| {
3131 return mod.fail(scope, result.src, "TODO implement rlWrap .block_ptr", .{});
3134 .block_ptr => |block_scope| {
3135 block_scope.rvalue_rl_count += 1;
3136 _ = try addZIRBinOp(mod, scope, result.src, .store_to_block_ptr, block_scope.rl_ptr.?, result);
3137 return result;
31323138 },
31333139 }
31343140}
31353141
3136fn rlWrapVoid(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node, result: void) InnerError!*zir.Inst {
3142fn rvalueVoid(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node, result: void) InnerError!*zir.Inst {
31373143 const src = scope.tree().token_locs[node.firstToken()].start;
31383144 const void_inst = try addZIRInstConst(mod, scope, src, .{
31393145 .ty = Type.initTag(.void),
31403146 .val = Value.initTag(.void_value),
31413147 });
3142 return rlWrap(mod, scope, rl, void_inst);
3148 return rvalue(mod, scope, rl, void_inst);
3149}
3150
3151fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZIR) ResultLoc.Strategy {
3152 var elide_store_to_block_ptr_instructions = false;
3153 switch (rl) {
3154 // In this branch there will not be any store_to_block_ptr instructions.
3155 .discard, .none, .ty, .ref => return .{
3156 .tag = .break_operand,
3157 .elide_store_to_block_ptr_instructions = false,
3158 },
3159 // The pointer got passed through to the sub-expressions, so we will use
3160 // break_void here.
3161 // In this branch there will not be any store_to_block_ptr instructions.
3162 .ptr => return .{
3163 .tag = .break_void,
3164 .elide_store_to_block_ptr_instructions = false,
3165 },
3166 .inferred_ptr, .bitcasted_ptr, .block_ptr => {
3167 if (block_scope.rvalue_rl_count == block_scope.break_count) {
3168 // Neither prong of the if consumed the result location, so we can
3169 // use break instructions to create an rvalue.
3170 return .{
3171 .tag = .break_operand,
3172 .elide_store_to_block_ptr_instructions = true,
3173 };
3174 } else {
3175 // Allow the store_to_block_ptr instructions to remain so that
3176 // semantic analysis can turn them into bitcasts.
3177 return .{
3178 .tag = .break_void,
3179 .elide_store_to_block_ptr_instructions = false,
3180 };
3181 }
3182 },
3183 }
31433184}
31443185
3145/// TODO go over all the callsites and see where we can introduce "by-value" ZIR instructions
3146/// to save ZIR memory. For example, see DeclVal vs DeclRef.
3147/// Do not add additional callsites to this function.
3148fn rlWrapPtr(mod: *Module, scope: *Scope, rl: ResultLoc, ptr: *zir.Inst) InnerError!*zir.Inst {
3149 if (rl == .ref) return ptr;
3186fn setBlockResultLoc(block_scope: *Scope.GenZIR, parent_rl: ResultLoc) void {
3187 // Depending on whether the result location is a pointer or value, different
3188 // ZIR needs to be generated. In the former case we rely on storing to the
3189 // pointer to communicate the result, and use breakvoid; in the latter case
3190 // the block break instructions will have the result values.
3191 // One more complication: when the result location is a pointer, we detect
3192 // the scenario where the result location is not consumed. In this case
3193 // we emit ZIR for the block break instructions to have the result values,
3194 // and then rvalue() on that to pass the value to the result location.
3195 switch (parent_rl) {
3196 .discard, .none, .ty, .ptr, .ref => {
3197 block_scope.break_result_loc = parent_rl;
3198 },
3199
3200 .inferred_ptr => |ptr| {
3201 block_scope.rl_ptr = &ptr.base;
3202 block_scope.break_result_loc = .{ .block_ptr = block_scope };
3203 },
3204
3205 .bitcasted_ptr => |ptr| {
3206 block_scope.rl_ptr = &ptr.base;
3207 block_scope.break_result_loc = .{ .block_ptr = block_scope };
3208 },
31503209
3151 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, ptr.src, .deref, ptr));
3210 .block_ptr => |parent_block_scope| {
3211 block_scope.rl_ptr = parent_block_scope.rl_ptr.?;
3212 block_scope.break_result_loc = .{ .block_ptr = block_scope };
3213 },
3214 }
31523215}
31533216
31543217pub fn addZirInstTag(
src/codegen.zig+34-23
......@@ -840,14 +840,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
840840 .arg => return self.genArg(inst.castTag(.arg).?),
841841 .assembly => return self.genAsm(inst.castTag(.assembly).?),
842842 .bitcast => return self.genBitCast(inst.castTag(.bitcast).?),
843 .bitand => return self.genBitAnd(inst.castTag(.bitand).?),
844 .bitor => return self.genBitOr(inst.castTag(.bitor).?),
843 .bit_and => return self.genBitAnd(inst.castTag(.bit_and).?),
844 .bit_or => return self.genBitOr(inst.castTag(.bit_or).?),
845845 .block => return self.genBlock(inst.castTag(.block).?),
846846 .br => return self.genBr(inst.castTag(.br).?),
847 .br_block_flat => return self.genBrBlockFlat(inst.castTag(.br_block_flat).?),
847848 .breakpoint => return self.genBreakpoint(inst.src),
848 .brvoid => return self.genBrVoid(inst.castTag(.brvoid).?),
849 .booland => return self.genBoolOp(inst.castTag(.booland).?),
850 .boolor => return self.genBoolOp(inst.castTag(.boolor).?),
849 .br_void => return self.genBrVoid(inst.castTag(.br_void).?),
850 .bool_and => return self.genBoolOp(inst.castTag(.bool_and).?),
851 .bool_or => return self.genBoolOp(inst.castTag(.bool_or).?),
851852 .call => return self.genCall(inst.castTag(.call).?),
852853 .cmp_lt => return self.genCmp(inst.castTag(.cmp_lt).?, .lt),
853854 .cmp_lte => return self.genCmp(inst.castTag(.cmp_lte).?, .lte),
......@@ -1097,7 +1098,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
10971098 if (inst.base.isUnused())
10981099 return MCValue.dead;
10991100 switch (arch) {
1100 .arm, .armeb => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bitand),
1101 .arm, .armeb => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bit_and),
11011102 else => return self.fail(inst.base.src, "TODO implement bitwise and for {}", .{self.target.cpu.arch}),
11021103 }
11031104 }
......@@ -1107,7 +1108,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
11071108 if (inst.base.isUnused())
11081109 return MCValue.dead;
11091110 switch (arch) {
1110 .arm, .armeb => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bitor),
1111 .arm, .armeb => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bit_or),
11111112 else => return self.fail(inst.base.src, "TODO implement bitwise or for {}", .{self.target.cpu.arch}),
11121113 }
11131114 }
......@@ -1371,10 +1372,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13711372 writeInt(u32, try self.code.addManyAsArray(4), Instruction.rsb(.al, dst_reg, dst_reg, operand).toU32());
13721373 }
13731374 },
1374 .booland, .bitand => {
1375 .bool_and, .bit_and => {
13751376 writeInt(u32, try self.code.addManyAsArray(4), Instruction.@"and"(.al, dst_reg, dst_reg, operand).toU32());
13761377 },
1377 .boolor, .bitor => {
1378 .bool_or, .bit_or => {
13781379 writeInt(u32, try self.code.addManyAsArray(4), Instruction.orr(.al, dst_reg, dst_reg, operand).toU32());
13791380 },
13801381 .not, .xor => {
......@@ -2441,17 +2442,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
24412442 }
24422443 }
24432444
2445 fn genBrBlockFlat(self: *Self, inst: *ir.Inst.BrBlockFlat) !MCValue {
2446 try self.genBody(inst.body);
2447 const last = inst.body.instructions[inst.body.instructions.len - 1];
2448 return self.br(inst.base.src, inst.block, last);
2449 }
2450
24442451 fn genBr(self: *Self, inst: *ir.Inst.Br) !MCValue {
2445 if (inst.operand.ty.hasCodeGenBits()) {
2446 const operand = try self.resolveInst(inst.operand);
2447 const block_mcv = @bitCast(MCValue, inst.block.codegen.mcv);
2448 if (block_mcv == .none) {
2449 inst.block.codegen.mcv = @bitCast(AnyMCValue, operand);
2450 } else {
2451 try self.setRegOrMem(inst.base.src, inst.block.base.ty, block_mcv, operand);
2452 }
2453 }
2454 return self.brVoid(inst.base.src, inst.block);
2452 return self.br(inst.base.src, inst.block, inst.operand);
24552453 }
24562454
24572455 fn genBrVoid(self: *Self, inst: *ir.Inst.BrVoid) !MCValue {
......@@ -2464,20 +2462,33 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
24642462 switch (arch) {
24652463 .x86_64 => switch (inst.base.tag) {
24662464 // lhs AND rhs
2467 .booland => return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 4, 0x20),
2465 .bool_and => return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 4, 0x20),
24682466 // lhs OR rhs
2469 .boolor => return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 1, 0x08),
2467 .bool_or => return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 1, 0x08),
24702468 else => unreachable, // Not a boolean operation
24712469 },
24722470 .arm, .armeb => switch (inst.base.tag) {
2473 .booland => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .booland),
2474 .boolor => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .boolor),
2471 .bool_and => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bool_and),
2472 .bool_or => return try self.genArmBinOp(&inst.base, inst.lhs, inst.rhs, .bool_or),
24752473 else => unreachable, // Not a boolean operation
24762474 },
24772475 else => return self.fail(inst.base.src, "TODO implement boolean operations for {}", .{self.target.cpu.arch}),
24782476 }
24792477 }
24802478
2479 fn br(self: *Self, src: usize, block: *ir.Inst.Block, operand: *ir.Inst) !MCValue {
2480 if (operand.ty.hasCodeGenBits()) {
2481 const operand_mcv = try self.resolveInst(operand);
2482 const block_mcv = @bitCast(MCValue, block.codegen.mcv);
2483 if (block_mcv == .none) {
2484 block.codegen.mcv = @bitCast(AnyMCValue, operand_mcv);
2485 } else {
2486 try self.setRegOrMem(src, block.base.ty, block_mcv, operand_mcv);
2487 }
2488 }
2489 return self.brVoid(src, block);
2490 }
2491
24812492 fn brVoid(self: *Self, src: usize, block: *ir.Inst.Block) !MCValue {
24822493 // Emit a jump with a relocation. It will be patched up after the block ends.
24832494 try block.codegen.relocs.ensureCapacity(self.gpa, block.codegen.relocs.items.len + 1);
src/ir.zig+43-13
......@@ -56,13 +56,20 @@ pub const Inst = struct {
5656 alloc,
5757 arg,
5858 assembly,
59 bitand,
59 bit_and,
6060 bitcast,
61 bitor,
61 bit_or,
6262 block,
6363 br,
64 /// Same as `br` except the operand is a list of instructions to be treated as
65 /// a flat block; that is there is only 1 break instruction from the block, and
66 /// it is implied to be after the last instruction, and the last instruction is
67 /// the break operand.
68 /// This instruction exists for late-stage semantic analysis patch ups, to
69 /// replace one br operand with multiple instructions, without moving anything else around.
70 br_block_flat,
6471 breakpoint,
65 brvoid,
72 br_void,
6673 call,
6774 cmp_lt,
6875 cmp_lte,
......@@ -85,8 +92,8 @@ pub const Inst = struct {
8592 is_err,
8693 // *E!T => bool
8794 is_err_ptr,
88 booland,
89 boolor,
95 bool_and,
96 bool_or,
9097 /// Read a value from a pointer.
9198 load,
9299 loop,
......@@ -147,10 +154,10 @@ pub const Inst = struct {
147154 .cmp_gt,
148155 .cmp_neq,
149156 .store,
150 .booland,
151 .boolor,
152 .bitand,
153 .bitor,
157 .bool_and,
158 .bool_or,
159 .bit_and,
160 .bit_or,
154161 .xor,
155162 => BinOp,
156163
......@@ -158,7 +165,8 @@ pub const Inst = struct {
158165 .assembly => Assembly,
159166 .block => Block,
160167 .br => Br,
161 .brvoid => BrVoid,
168 .br_block_flat => BrBlockFlat,
169 .br_void => BrVoid,
162170 .call => Call,
163171 .condbr => CondBr,
164172 .constant => Constant,
......@@ -251,7 +259,8 @@ pub const Inst = struct {
251259 pub fn breakBlock(base: *Inst) ?*Block {
252260 return switch (base.tag) {
253261 .br => base.castTag(.br).?.block,
254 .brvoid => base.castTag(.brvoid).?.block,
262 .br_void => base.castTag(.br_void).?.block,
263 .br_block_flat => base.castTag(.br_block_flat).?.block,
255264 else => null,
256265 };
257266 }
......@@ -355,6 +364,27 @@ pub const Inst = struct {
355364 }
356365 };
357366
367 pub const convertable_br_size = std.math.max(@sizeOf(BrBlockFlat), @sizeOf(Br));
368 pub const convertable_br_align = std.math.max(@alignOf(BrBlockFlat), @alignOf(Br));
369 comptime {
370 assert(@byteOffsetOf(BrBlockFlat, "base") == @byteOffsetOf(Br, "base"));
371 }
372
373 pub const BrBlockFlat = struct {
374 pub const base_tag = Tag.br_block_flat;
375
376 base: Inst,
377 block: *Block,
378 body: Body,
379
380 pub fn operandCount(self: *const BrBlockFlat) usize {
381 return 0;
382 }
383 pub fn getOperand(self: *const BrBlockFlat, index: usize) ?*Inst {
384 return null;
385 }
386 };
387
358388 pub const Br = struct {
359389 pub const base_tag = Tag.br;
360390
......@@ -363,7 +393,7 @@ pub const Inst = struct {
363393 operand: *Inst,
364394
365395 pub fn operandCount(self: *const Br) usize {
366 return 0;
396 return 1;
367397 }
368398 pub fn getOperand(self: *const Br, index: usize) ?*Inst {
369399 if (index == 0)
......@@ -373,7 +403,7 @@ pub const Inst = struct {
373403 };
374404
375405 pub const BrVoid = struct {
376 pub const base_tag = Tag.brvoid;
406 pub const base_tag = Tag.br_void;
377407
378408 base: Inst,
379409 block: *Block,
src/zir.zig+110-165
......@@ -59,7 +59,7 @@ pub const Inst = struct {
5959 /// Inline assembly.
6060 @"asm",
6161 /// Bitwise AND. `&`
62 bitand,
62 bit_and,
6363 /// TODO delete this instruction, it has no purpose.
6464 bitcast,
6565 /// An arbitrary typed pointer is pointer-casted to a new Pointer.
......@@ -71,9 +71,9 @@ pub const Inst = struct {
7171 /// The new result location pointer has an inferred type.
7272 bitcast_result_ptr,
7373 /// Bitwise NOT. `~`
74 bitnot,
74 bit_not,
7575 /// Bitwise OR. `|`
76 bitor,
76 bit_or,
7777 /// A labeled block of code, which can return a value.
7878 block,
7979 /// A block of code, which can return a value. There are no instructions that break out of
......@@ -83,17 +83,17 @@ pub const Inst = struct {
8383 block_comptime,
8484 /// Same as `block_flat` but additionally makes the inner instructions execute at comptime.
8585 block_comptime_flat,
86 /// Boolean AND. See also `bitand`.
87 booland,
88 /// Boolean NOT. See also `bitnot`.
89 boolnot,
90 /// Boolean OR. See also `bitor`.
91 boolor,
86 /// Boolean AND. See also `bit_and`.
87 bool_and,
88 /// Boolean NOT. See also `bit_not`.
89 bool_not,
90 /// Boolean OR. See also `bit_or`.
91 bool_or,
9292 /// Return a value from a `Block`.
9393 @"break",
9494 breakpoint,
9595 /// Same as `break` but without an operand; the operand is assumed to be the void value.
96 breakvoid,
96 break_void,
9797 /// Function call.
9898 call,
9999 /// `<`
......@@ -112,16 +112,10 @@ pub const Inst = struct {
112112 /// as type coercion from the new element type to the old element type.
113113 /// LHS is destination element type, RHS is result pointer.
114114 coerce_result_ptr,
115 /// This instruction does a `coerce_result_ptr` operation on a `Block`'s
116 /// result location pointer, whose type is inferred by peer type resolution on the
117 /// `Block`'s corresponding `break` instructions.
118 coerce_result_block_ptr,
119 /// Equivalent to `as(ptr_child_type(typeof(ptr)), value)`.
120 coerce_to_ptr_elem,
121115 /// Emit an error message and fail compilation.
122 compileerror,
116 compile_error,
123117 /// Log compile time variables and emit an error message.
124 compilelog,
118 compile_log,
125119 /// Conditional branch. Splits control flow based on a boolean condition value.
126120 condbr,
127121 /// Special case, has no textual representation.
......@@ -135,11 +129,11 @@ pub const Inst = struct {
135129 /// Declares the beginning of a statement. Used for debug info.
136130 dbg_stmt,
137131 /// Represents a pointer to a global decl.
138 declref,
132 decl_ref,
139133 /// Represents a pointer to a global decl by string name.
140 declref_str,
141 /// Equivalent to a declref followed by deref.
142 declval,
134 decl_ref_str,
135 /// Equivalent to a decl_ref followed by deref.
136 decl_val,
143137 /// Load the value from a pointer.
144138 deref,
145139 /// Arithmetic division. Asserts no integer overflow.
......@@ -185,7 +179,7 @@ pub const Inst = struct {
185179 /// can hold the same mathematical value.
186180 intcast,
187181 /// Make an integer type out of signedness and bit count.
188 inttype,
182 int_type,
189183 /// Return a boolean false if an optional is null. `x != null`
190184 is_non_null,
191185 /// Return a boolean true if an optional is null. `x == null`
......@@ -232,7 +226,7 @@ pub const Inst = struct {
232226 /// Sends control flow back to the function's callee. Takes an operand as the return value.
233227 @"return",
234228 /// Same as `return` but there is no operand; the operand is implicitly the void value.
235 returnvoid,
229 return_void,
236230 /// Changes the maximum number of backwards branches that compile-time
237231 /// code execution can use before giving up and making a compile error.
238232 set_eval_branch_quota,
......@@ -270,6 +264,9 @@ pub const Inst = struct {
270264 /// Write a value to a pointer. For loading, see `deref`.
271265 store,
272266 /// Same as `store` but the type of the value being stored will be used to infer
267 /// the block type. The LHS is the pointer to store to.
268 store_to_block_ptr,
269 /// Same as `store` but the type of the value being stored will be used to infer
273270 /// the pointer type.
274271 store_to_inferred_ptr,
275272 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
......@@ -286,11 +283,11 @@ pub const Inst = struct {
286283 typeof_peer,
287284 /// Asserts control-flow will not reach this instruction. Not safety checked - the compiler
288285 /// will assume the correctness of this instruction.
289 unreach_nocheck,
286 unreachable_unsafe,
290287 /// Asserts control-flow will not reach this instruction. In safety-checked modes,
291288 /// this will generate a call to the panic function unless it can be proven unreachable
292289 /// by the compiler.
293 @"unreachable",
290 unreachable_safe,
294291 /// Bitwise XOR. `^`
295292 xor,
296293 /// Create an optional type '?T'
......@@ -339,12 +336,8 @@ pub const Inst = struct {
339336 enum_literal,
340337 /// Create an enum type.
341338 enum_type,
342 /// A switch expression.
343 switchbr,
344 /// A range in a switch case, `lhs...rhs`.
345 /// Only checks that `lhs >= rhs` if they are ints, everything else is
346 /// validated by the .switch instruction.
347 switch_range,
339 /// Does nothing; returns a void value.
340 void_value,
348341
349342 pub fn Type(tag: Tag) type {
350343 return switch (tag) {
......@@ -352,17 +345,18 @@ pub const Inst = struct {
352345 .alloc_inferred_mut,
353346 .breakpoint,
354347 .dbg_stmt,
355 .returnvoid,
348 .return_void,
356349 .ret_ptr,
357350 .ret_type,
358 .unreach_nocheck,
359 .@"unreachable",
351 .unreachable_unsafe,
352 .unreachable_safe,
353 .void_value,
360354 => NoOp,
361355
362356 .alloc,
363357 .alloc_mut,
364 .boolnot,
365 .compileerror,
358 .bool_not,
359 .compile_error,
366360 .deref,
367361 .@"return",
368362 .is_null,
......@@ -400,7 +394,7 @@ pub const Inst = struct {
400394 .err_union_code_ptr,
401395 .ensure_err_payload_void,
402396 .anyframe_type,
403 .bitnot,
397 .bit_not,
404398 .import,
405399 .set_eval_branch_quota,
406400 .indexable_ptr_len,
......@@ -411,10 +405,10 @@ pub const Inst = struct {
411405 .array_cat,
412406 .array_mul,
413407 .array_type,
414 .bitand,
415 .bitor,
416 .booland,
417 .boolor,
408 .bit_and,
409 .bit_or,
410 .bool_and,
411 .bool_or,
418412 .div,
419413 .mod_rem,
420414 .mul,
......@@ -422,6 +416,7 @@ pub const Inst = struct {
422416 .shl,
423417 .shr,
424418 .store,
419 .store_to_block_ptr,
425420 .store_to_inferred_ptr,
426421 .sub,
427422 .subwrap,
......@@ -440,7 +435,6 @@ pub const Inst = struct {
440435 .error_union_type,
441436 .merge_error_sets,
442437 .slice_start,
443 .switch_range,
444438 => BinOp,
445439
446440 .block,
......@@ -452,19 +446,17 @@ pub const Inst = struct {
452446 .arg => Arg,
453447 .array_type_sentinel => ArrayTypeSentinel,
454448 .@"break" => Break,
455 .breakvoid => BreakVoid,
449 .break_void => BreakVoid,
456450 .call => Call,
457 .coerce_to_ptr_elem => CoerceToPtrElem,
458 .declref => DeclRef,
459 .declref_str => DeclRefStr,
460 .declval => DeclVal,
461 .coerce_result_block_ptr => CoerceResultBlockPtr,
462 .compilelog => CompileLog,
451 .decl_ref => DeclRef,
452 .decl_ref_str => DeclRefStr,
453 .decl_val => DeclVal,
454 .compile_log => CompileLog,
463455 .loop => Loop,
464456 .@"const" => Const,
465457 .str => Str,
466458 .int => Int,
467 .inttype => IntType,
459 .int_type => IntType,
468460 .field_ptr, .field_val => Field,
469461 .field_ptr_named, .field_val_named => FieldNamed,
470462 .@"asm" => Asm,
......@@ -479,7 +471,6 @@ pub const Inst = struct {
479471 .enum_literal => EnumLiteral,
480472 .error_set => ErrorSet,
481473 .slice => Slice,
482 .switchbr => SwitchBr,
483474 .typeof_peer => TypeOfPeer,
484475 .container_field_named => ContainerFieldNamed,
485476 .container_field_typed => ContainerFieldTyped,
......@@ -508,18 +499,18 @@ pub const Inst = struct {
508499 .arg,
509500 .as,
510501 .@"asm",
511 .bitand,
502 .bit_and,
512503 .bitcast,
513504 .bitcast_ref,
514505 .bitcast_result_ptr,
515 .bitor,
506 .bit_or,
516507 .block,
517508 .block_flat,
518509 .block_comptime,
519510 .block_comptime_flat,
520 .boolnot,
521 .booland,
522 .boolor,
511 .bool_not,
512 .bool_and,
513 .bool_or,
523514 .breakpoint,
524515 .call,
525516 .cmp_lt,
......@@ -529,13 +520,11 @@ pub const Inst = struct {
529520 .cmp_gt,
530521 .cmp_neq,
531522 .coerce_result_ptr,
532 .coerce_result_block_ptr,
533 .coerce_to_ptr_elem,
534523 .@"const",
535524 .dbg_stmt,
536 .declref,
537 .declref_str,
538 .declval,
525 .decl_ref,
526 .decl_ref_str,
527 .decl_val,
539528 .deref,
540529 .div,
541530 .elem_ptr,
......@@ -552,7 +541,7 @@ pub const Inst = struct {
552541 .fntype,
553542 .int,
554543 .intcast,
555 .inttype,
544 .int_type,
556545 .is_non_null,
557546 .is_null,
558547 .is_non_null_ptr,
......@@ -579,6 +568,7 @@ pub const Inst = struct {
579568 .mut_slice_type,
580569 .const_slice_type,
581570 .store,
571 .store_to_block_ptr,
582572 .store_to_inferred_ptr,
583573 .str,
584574 .sub,
......@@ -602,31 +592,30 @@ pub const Inst = struct {
602592 .merge_error_sets,
603593 .anyframe_type,
604594 .error_union_type,
605 .bitnot,
595 .bit_not,
606596 .error_set,
607597 .slice,
608598 .slice_start,
609599 .import,
610 .switch_range,
611600 .typeof_peer,
612601 .resolve_inferred_alloc,
613602 .set_eval_branch_quota,
614 .compilelog,
603 .compile_log,
615604 .enum_type,
616605 .union_type,
617606 .struct_type,
607 .void_value,
618608 => false,
619609
620610 .@"break",
621 .breakvoid,
611 .break_void,
622612 .condbr,
623 .compileerror,
613 .compile_error,
624614 .@"return",
625 .returnvoid,
626 .unreach_nocheck,
627 .@"unreachable",
615 .return_void,
616 .unreachable_unsafe,
617 .unreachable_safe,
628618 .loop,
629 .switchbr,
630619 .container_field_named,
631620 .container_field_typed,
632621 .container_field,
......@@ -717,7 +706,7 @@ pub const Inst = struct {
717706 };
718707
719708 pub const BreakVoid = struct {
720 pub const base_tag = Tag.breakvoid;
709 pub const base_tag = Tag.break_void;
721710 base: Inst,
722711
723712 positionals: struct {
......@@ -739,19 +728,8 @@ pub const Inst = struct {
739728 },
740729 };
741730
742 pub const CoerceToPtrElem = struct {
743 pub const base_tag = Tag.coerce_to_ptr_elem;
744 base: Inst,
745
746 positionals: struct {
747 ptr: *Inst,
748 value: *Inst,
749 },
750 kw_args: struct {},
751 };
752
753731 pub const DeclRef = struct {
754 pub const base_tag = Tag.declref;
732 pub const base_tag = Tag.decl_ref;
755733 base: Inst,
756734
757735 positionals: struct {
......@@ -761,7 +739,7 @@ pub const Inst = struct {
761739 };
762740
763741 pub const DeclRefStr = struct {
764 pub const base_tag = Tag.declref_str;
742 pub const base_tag = Tag.decl_ref_str;
765743 base: Inst,
766744
767745 positionals: struct {
......@@ -771,7 +749,7 @@ pub const Inst = struct {
771749 };
772750
773751 pub const DeclVal = struct {
774 pub const base_tag = Tag.declval;
752 pub const base_tag = Tag.decl_val;
775753 base: Inst,
776754
777755 positionals: struct {
......@@ -780,19 +758,8 @@ pub const Inst = struct {
780758 kw_args: struct {},
781759 };
782760
783 pub const CoerceResultBlockPtr = struct {
784 pub const base_tag = Tag.coerce_result_block_ptr;
785 base: Inst,
786
787 positionals: struct {
788 dest_type: *Inst,
789 block: *Block,
790 },
791 kw_args: struct {},
792 };
793
794761 pub const CompileLog = struct {
795 pub const base_tag = Tag.compilelog;
762 pub const base_tag = Tag.compile_log;
796763 base: Inst,
797764
798765 positionals: struct {
......@@ -905,7 +872,7 @@ pub const Inst = struct {
905872 };
906873
907874 pub const IntType = struct {
908 pub const base_tag = Tag.inttype;
875 pub const base_tag = Tag.int_type;
909876 base: Inst,
910877
911878 positionals: struct {
......@@ -1114,32 +1081,6 @@ pub const Inst = struct {
11141081 },
11151082 };
11161083
1117 pub const SwitchBr = struct {
1118 pub const base_tag = Tag.switchbr;
1119 base: Inst,
1120
1121 positionals: struct {
1122 target_ptr: *Inst,
1123 /// List of all individual items and ranges
1124 items: []*Inst,
1125 cases: []Case,
1126 else_body: Body,
1127 },
1128 kw_args: struct {
1129 /// Pointer to first range if such exists.
1130 range: ?*Inst = null,
1131 special_prong: enum {
1132 none,
1133 @"else",
1134 underscore,
1135 } = .none,
1136 },
1137
1138 pub const Case = struct {
1139 item: *Inst,
1140 body: Body,
1141 };
1142 };
11431084 pub const TypeOfPeer = struct {
11441085 pub const base_tag = .typeof_peer;
11451086 base: Inst,
......@@ -1473,7 +1414,7 @@ const Writer = struct {
14731414 TypedValue => return stream.print("TypedValue{{ .ty = {}, .val = {}}}", .{ param.ty, param.val }),
14741415 *IrModule.Decl => return stream.print("Decl({s})", .{param.name}),
14751416 *Inst.Block => {
1476 const name = self.block_table.get(param).?;
1417 const name = self.block_table.get(param) orelse "!BADREF!";
14771418 return stream.print("\"{}\"", .{std.zig.fmtEscapes(name)});
14781419 },
14791420 *Inst.Loop => {
......@@ -1490,26 +1431,6 @@ const Writer = struct {
14901431 }
14911432 try stream.writeByte(']');
14921433 },
1493 []Inst.SwitchBr.Case => {
1494 if (param.len == 0) {
1495 return stream.writeAll("{}");
1496 }
1497 try stream.writeAll("{\n");
1498 for (param) |*case, i| {
1499 if (i != 0) {
1500 try stream.writeAll(",\n");
1501 }
1502 try stream.writeByteNTimes(' ', self.indent);
1503 self.indent += 2;
1504 try self.writeParamToStream(stream, &case.item);
1505 try stream.writeAll(" => ");
1506 try self.writeParamToStream(stream, &case.body);
1507 self.indent -= 2;
1508 }
1509 try stream.writeByte('\n');
1510 try stream.writeByteNTimes(' ', self.indent - 2);
1511 try stream.writeByte('}');
1512 },
15131434 else => |T| @compileError("unimplemented: rendering parameter of type " ++ @typeName(T)),
15141435 }
15151436 }
......@@ -1641,10 +1562,10 @@ const DumpTzir = struct {
16411562 .cmp_gt,
16421563 .cmp_neq,
16431564 .store,
1644 .booland,
1645 .boolor,
1646 .bitand,
1647 .bitor,
1565 .bool_and,
1566 .bool_or,
1567 .bit_and,
1568 .bit_or,
16481569 .xor,
16491570 => {
16501571 const bin_op = inst.cast(ir.Inst.BinOp).?;
......@@ -1660,9 +1581,15 @@ const DumpTzir = struct {
16601581 try dtz.findConst(br.operand);
16611582 },
16621583
1663 .brvoid => {
1664 const brvoid = inst.castTag(.brvoid).?;
1665 try dtz.findConst(&brvoid.block.base);
1584 .br_block_flat => {
1585 const br_block_flat = inst.castTag(.br_block_flat).?;
1586 try dtz.findConst(&br_block_flat.block.base);
1587 try dtz.fetchInstsAndResolveConsts(br_block_flat.body);
1588 },
1589
1590 .br_void => {
1591 const br_void = inst.castTag(.br_void).?;
1592 try dtz.findConst(&br_void.block.base);
16661593 },
16671594
16681595 .block => {
......@@ -1753,10 +1680,10 @@ const DumpTzir = struct {
17531680 .cmp_gt,
17541681 .cmp_neq,
17551682 .store,
1756 .booland,
1757 .boolor,
1758 .bitand,
1759 .bitor,
1683 .bool_and,
1684 .bool_or,
1685 .bit_and,
1686 .bit_or,
17601687 .xor,
17611688 => {
17621689 const bin_op = inst.cast(ir.Inst.BinOp).?;
......@@ -1805,9 +1732,27 @@ const DumpTzir = struct {
18051732 }
18061733 },
18071734
1808 .brvoid => {
1809 const brvoid = inst.castTag(.brvoid).?;
1810 const kinky = try dtz.writeInst(writer, &brvoid.block.base);
1735 .br_block_flat => {
1736 const br_block_flat = inst.castTag(.br_block_flat).?;
1737 const block_kinky = try dtz.writeInst(writer, &br_block_flat.block.base);
1738 if (block_kinky != null) {
1739 try writer.writeAll(", { // Instruction does not dominate all uses!\n");
1740 } else {
1741 try writer.writeAll(", {\n");
1742 }
1743
1744 const old_indent = dtz.indent;
1745 dtz.indent += 2;
1746 try dtz.dumpBody(br_block_flat.body, writer);
1747 dtz.indent = old_indent;
1748
1749 try writer.writeByteNTimes(' ', dtz.indent);
1750 try writer.writeAll("})\n");
1751 },
1752
1753 .br_void => {
1754 const br_void = inst.castTag(.br_void).?;
1755 const kinky = try dtz.writeInst(writer, &br_void.block.base);
18111756 if (kinky) |_| {
18121757 try writer.writeAll(") // Instruction does not dominate all uses!\n");
18131758 } else {
......@@ -1818,7 +1763,7 @@ const DumpTzir = struct {
18181763 .block => {
18191764 const block = inst.castTag(.block).?;
18201765
1821 try writer.writeAll("\n");
1766 try writer.writeAll("{\n");
18221767
18231768 const old_indent = dtz.indent;
18241769 dtz.indent += 2;
......@@ -1826,7 +1771,7 @@ const DumpTzir = struct {
18261771 dtz.indent = old_indent;
18271772
18281773 try writer.writeByteNTimes(' ', dtz.indent);
1829 try writer.writeAll(")\n");
1774 try writer.writeAll("})\n");
18301775 },
18311776
18321777 .condbr => {
......@@ -1856,7 +1801,7 @@ const DumpTzir = struct {
18561801 .loop => {
18571802 const loop = inst.castTag(.loop).?;
18581803
1859 try writer.writeAll("\n");
1804 try writer.writeAll("{\n");
18601805
18611806 const old_indent = dtz.indent;
18621807 dtz.indent += 2;
......@@ -1864,7 +1809,7 @@ const DumpTzir = struct {
18641809 dtz.indent = old_indent;
18651810
18661811 try writer.writeByteNTimes(' ', dtz.indent);
1867 try writer.writeAll(")\n");
1812 try writer.writeAll("})\n");
18681813 },
18691814
18701815 .call => {
src/zir_sema.zig+357-538
......@@ -28,144 +28,132 @@ const Decl = Module.Decl;
2828
2929pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {
3030 switch (old_inst.tag) {
31 .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?),
32 .alloc_mut => return analyzeInstAllocMut(mod, scope, old_inst.castTag(.alloc_mut).?),
33 .alloc_inferred => return analyzeInstAllocInferred(
34 mod,
35 scope,
36 old_inst.castTag(.alloc_inferred).?,
37 .inferred_alloc_const,
38 ),
39 .alloc_inferred_mut => return analyzeInstAllocInferred(
40 mod,
41 scope,
42 old_inst.castTag(.alloc_inferred_mut).?,
43 .inferred_alloc_mut,
44 ),
45 .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?),
46 .bitcast_ref => return bitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?),
47 .bitcast_result_ptr => return bitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?),
48 .block => return analyzeInstBlock(mod, scope, old_inst.castTag(.block).?, false),
49 .block_comptime => return analyzeInstBlock(mod, scope, old_inst.castTag(.block_comptime).?, true),
50 .block_flat => return analyzeInstBlockFlat(mod, scope, old_inst.castTag(.block_flat).?, false),
51 .block_comptime_flat => return analyzeInstBlockFlat(mod, scope, old_inst.castTag(.block_comptime_flat).?, true),
52 .@"break" => return analyzeInstBreak(mod, scope, old_inst.castTag(.@"break").?),
53 .breakpoint => return analyzeInstBreakpoint(mod, scope, old_inst.castTag(.breakpoint).?),
54 .breakvoid => return analyzeInstBreakVoid(mod, scope, old_inst.castTag(.breakvoid).?),
55 .call => return call(mod, scope, old_inst.castTag(.call).?),
56 .coerce_result_block_ptr => return analyzeInstCoerceResultBlockPtr(mod, scope, old_inst.castTag(.coerce_result_block_ptr).?),
57 .coerce_result_ptr => return analyzeInstCoerceResultPtr(mod, scope, old_inst.castTag(.coerce_result_ptr).?),
58 .coerce_to_ptr_elem => return analyzeInstCoerceToPtrElem(mod, scope, old_inst.castTag(.coerce_to_ptr_elem).?),
59 .compileerror => return analyzeInstCompileError(mod, scope, old_inst.castTag(.compileerror).?),
60 .compilelog => return analyzeInstCompileLog(mod, scope, old_inst.castTag(.compilelog).?),
61 .@"const" => return analyzeInstConst(mod, scope, old_inst.castTag(.@"const").?),
62 .dbg_stmt => return analyzeInstDbgStmt(mod, scope, old_inst.castTag(.dbg_stmt).?),
63 .declref => return declRef(mod, scope, old_inst.castTag(.declref).?),
64 .declref_str => return analyzeInstDeclRefStr(mod, scope, old_inst.castTag(.declref_str).?),
65 .declval => return declVal(mod, scope, old_inst.castTag(.declval).?),
66 .ensure_result_used => return analyzeInstEnsureResultUsed(mod, scope, old_inst.castTag(.ensure_result_used).?),
67 .ensure_result_non_error => return analyzeInstEnsureResultNonError(mod, scope, old_inst.castTag(.ensure_result_non_error).?),
68 .indexable_ptr_len => return indexablePtrLen(mod, scope, old_inst.castTag(.indexable_ptr_len).?),
69 .ref => return ref(mod, scope, old_inst.castTag(.ref).?),
70 .resolve_inferred_alloc => return analyzeInstResolveInferredAlloc(mod, scope, old_inst.castTag(.resolve_inferred_alloc).?),
71 .ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),
72 .ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?),
73 .store_to_inferred_ptr => return analyzeInstStoreToInferredPtr(mod, scope, old_inst.castTag(.store_to_inferred_ptr).?),
74 .single_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?, false, .One),
75 .single_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?, true, .One),
76 .many_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_const_ptr_type).?, false, .Many),
77 .many_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_mut_ptr_type).?, true, .Many),
78 .c_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_const_ptr_type).?, false, .C),
79 .c_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_mut_ptr_type).?, true, .C),
80 .const_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.const_slice_type).?, false, .Slice),
81 .mut_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice),
82 .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
83 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
84 .set_eval_branch_quota => return analyzeInstSetEvalBranchQuota(mod, scope, old_inst.castTag(.set_eval_branch_quota).?),
85 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
86 .int => return analyzeInstInt(mod, scope, old_inst.castTag(.int).?),
87 .inttype => return analyzeInstIntType(mod, scope, old_inst.castTag(.inttype).?),
88 .loop => return analyzeInstLoop(mod, scope, old_inst.castTag(.loop).?),
89 .param_type => return analyzeInstParamType(mod, scope, old_inst.castTag(.param_type).?),
90 .ptrtoint => return analyzeInstPtrToInt(mod, scope, old_inst.castTag(.ptrtoint).?),
91 .field_ptr => return fieldPtr(mod, scope, old_inst.castTag(.field_ptr).?),
92 .field_val => return fieldVal(mod, scope, old_inst.castTag(.field_val).?),
93 .field_ptr_named => return fieldPtrNamed(mod, scope, old_inst.castTag(.field_ptr_named).?),
94 .field_val_named => return fieldValNamed(mod, scope, old_inst.castTag(.field_val_named).?),
95 .deref => return analyzeInstDeref(mod, scope, old_inst.castTag(.deref).?),
96 .as => return analyzeInstAs(mod, scope, old_inst.castTag(.as).?),
97 .@"asm" => return analyzeInstAsm(mod, scope, old_inst.castTag(.@"asm").?),
98 .@"unreachable" => return analyzeInstUnreachable(mod, scope, old_inst.castTag(.@"unreachable").?, true),
99 .unreach_nocheck => return analyzeInstUnreachable(mod, scope, old_inst.castTag(.unreach_nocheck).?, false),
100 .@"return" => return analyzeInstRet(mod, scope, old_inst.castTag(.@"return").?),
101 .returnvoid => return analyzeInstRetVoid(mod, scope, old_inst.castTag(.returnvoid).?),
102 .@"fn" => return analyzeInstFn(mod, scope, old_inst.castTag(.@"fn").?),
103 .@"export" => return analyzeInstExport(mod, scope, old_inst.castTag(.@"export").?),
104 .primitive => return analyzeInstPrimitive(mod, scope, old_inst.castTag(.primitive).?),
105 .fntype => return analyzeInstFnType(mod, scope, old_inst.castTag(.fntype).?),
106 .intcast => return analyzeInstIntCast(mod, scope, old_inst.castTag(.intcast).?),
107 .bitcast => return analyzeInstBitCast(mod, scope, old_inst.castTag(.bitcast).?),
108 .floatcast => return analyzeInstFloatCast(mod, scope, old_inst.castTag(.floatcast).?),
109 .elem_ptr => return elemPtr(mod, scope, old_inst.castTag(.elem_ptr).?),
110 .elem_val => return elemVal(mod, scope, old_inst.castTag(.elem_val).?),
111 .add => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.add).?),
112 .addwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.addwrap).?),
113 .sub => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.sub).?),
114 .subwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.subwrap).?),
115 .mul => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mul).?),
116 .mulwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mulwrap).?),
117 .div => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.div).?),
118 .mod_rem => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mod_rem).?),
119 .array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?),
120 .array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?),
121 .bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?),
122 .bitnot => return analyzeInstBitNot(mod, scope, old_inst.castTag(.bitnot).?),
123 .bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?),
124 .xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?),
125 .shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?),
126 .shr => return analyzeInstShr(mod, scope, old_inst.castTag(.shr).?),
127 .cmp_lt => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_lt).?, .lt),
128 .cmp_lte => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_lte).?, .lte),
129 .cmp_eq => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_eq).?, .eq),
130 .cmp_gte => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_gte).?, .gte),
131 .cmp_gt => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_gt).?, .gt),
132 .cmp_neq => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_neq).?, .neq),
133 .condbr => return analyzeInstCondBr(mod, scope, old_inst.castTag(.condbr).?),
134 .is_null => return isNull(mod, scope, old_inst.castTag(.is_null).?, false),
135 .is_non_null => return isNull(mod, scope, old_inst.castTag(.is_non_null).?, true),
136 .is_null_ptr => return isNullPtr(mod, scope, old_inst.castTag(.is_null_ptr).?, false),
137 .is_non_null_ptr => return isNullPtr(mod, scope, old_inst.castTag(.is_non_null_ptr).?, true),
138 .is_err => return isErr(mod, scope, old_inst.castTag(.is_err).?),
139 .is_err_ptr => return isErrPtr(mod, scope, old_inst.castTag(.is_err_ptr).?),
140 .boolnot => return analyzeInstBoolNot(mod, scope, old_inst.castTag(.boolnot).?),
141 .typeof => return analyzeInstTypeOf(mod, scope, old_inst.castTag(.typeof).?),
142 .typeof_peer => return analyzeInstTypeOfPeer(mod, scope, old_inst.castTag(.typeof_peer).?),
143 .optional_type => return analyzeInstOptionalType(mod, scope, old_inst.castTag(.optional_type).?),
144 .optional_payload_safe => return optionalPayload(mod, scope, old_inst.castTag(.optional_payload_safe).?, true),
145 .optional_payload_unsafe => return optionalPayload(mod, scope, old_inst.castTag(.optional_payload_unsafe).?, false),
146 .optional_payload_safe_ptr => return optionalPayloadPtr(mod, scope, old_inst.castTag(.optional_payload_safe_ptr).?, true),
147 .optional_payload_unsafe_ptr => return optionalPayloadPtr(mod, scope, old_inst.castTag(.optional_payload_unsafe_ptr).?, false),
148 .err_union_payload_safe => return errorUnionPayload(mod, scope, old_inst.castTag(.err_union_payload_safe).?, true),
149 .err_union_payload_unsafe => return errorUnionPayload(mod, scope, old_inst.castTag(.err_union_payload_unsafe).?, false),
150 .err_union_payload_safe_ptr => return errorUnionPayloadPtr(mod, scope, old_inst.castTag(.err_union_payload_safe_ptr).?, true),
151 .err_union_payload_unsafe_ptr => return errorUnionPayloadPtr(mod, scope, old_inst.castTag(.err_union_payload_unsafe_ptr).?, false),
152 .err_union_code => return errorUnionCode(mod, scope, old_inst.castTag(.err_union_code).?),
153 .err_union_code_ptr => return errorUnionCodePtr(mod, scope, old_inst.castTag(.err_union_code_ptr).?),
154 .ensure_err_payload_void => return analyzeInstEnsureErrPayloadVoid(mod, scope, old_inst.castTag(.ensure_err_payload_void).?),
155 .array_type => return analyzeInstArrayType(mod, scope, old_inst.castTag(.array_type).?),
156 .array_type_sentinel => return analyzeInstArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?),
157 .enum_literal => return analyzeInstEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?),
158 .merge_error_sets => return analyzeInstMergeErrorSets(mod, scope, old_inst.castTag(.merge_error_sets).?),
159 .error_union_type => return analyzeInstErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),
160 .anyframe_type => return analyzeInstAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),
161 .error_set => return analyzeInstErrorSet(mod, scope, old_inst.castTag(.error_set).?),
162 .slice => return analyzeInstSlice(mod, scope, old_inst.castTag(.slice).?),
163 .slice_start => return analyzeInstSliceStart(mod, scope, old_inst.castTag(.slice_start).?),
164 .import => return analyzeInstImport(mod, scope, old_inst.castTag(.import).?),
165 .switchbr => return analyzeInstSwitchBr(mod, scope, old_inst.castTag(.switchbr).?),
166 .switch_range => return analyzeInstSwitchRange(mod, scope, old_inst.castTag(.switch_range).?),
167 .booland => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.booland).?),
168 .boolor => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.boolor).?),
31 .alloc => return zirAlloc(mod, scope, old_inst.castTag(.alloc).?),
32 .alloc_mut => return zirAllocMut(mod, scope, old_inst.castTag(.alloc_mut).?),
33 .alloc_inferred => return zirAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?, .inferred_alloc_const),
34 .alloc_inferred_mut => return zirAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred_mut).?, .inferred_alloc_mut),
35 .arg => return zirArg(mod, scope, old_inst.castTag(.arg).?),
36 .bitcast_ref => return zirBitcastRef(mod, scope, old_inst.castTag(.bitcast_ref).?),
37 .bitcast_result_ptr => return zirBitcastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?),
38 .block => return zirBlock(mod, scope, old_inst.castTag(.block).?, false),
39 .block_comptime => return zirBlock(mod, scope, old_inst.castTag(.block_comptime).?, true),
40 .block_flat => return zirBlockFlat(mod, scope, old_inst.castTag(.block_flat).?, false),
41 .block_comptime_flat => return zirBlockFlat(mod, scope, old_inst.castTag(.block_comptime_flat).?, true),
42 .@"break" => return zirBreak(mod, scope, old_inst.castTag(.@"break").?),
43 .breakpoint => return zirBreakpoint(mod, scope, old_inst.castTag(.breakpoint).?),
44 .break_void => return zirBreakVoid(mod, scope, old_inst.castTag(.break_void).?),
45 .call => return zirCall(mod, scope, old_inst.castTag(.call).?),
46 .coerce_result_ptr => return zirCoerceResultPtr(mod, scope, old_inst.castTag(.coerce_result_ptr).?),
47 .compile_error => return zirCompileError(mod, scope, old_inst.castTag(.compile_error).?),
48 .compile_log => return zirCompileLog(mod, scope, old_inst.castTag(.compile_log).?),
49 .@"const" => return zirConst(mod, scope, old_inst.castTag(.@"const").?),
50 .dbg_stmt => return zirDbgStmt(mod, scope, old_inst.castTag(.dbg_stmt).?),
51 .decl_ref => return zirDeclRef(mod, scope, old_inst.castTag(.decl_ref).?),
52 .decl_ref_str => return zirDeclRefStr(mod, scope, old_inst.castTag(.decl_ref_str).?),
53 .decl_val => return zirDeclVal(mod, scope, old_inst.castTag(.decl_val).?),
54 .ensure_result_used => return zirEnsureResultUsed(mod, scope, old_inst.castTag(.ensure_result_used).?),
55 .ensure_result_non_error => return zirEnsureResultNonError(mod, scope, old_inst.castTag(.ensure_result_non_error).?),
56 .indexable_ptr_len => return zirIndexablePtrLen(mod, scope, old_inst.castTag(.indexable_ptr_len).?),
57 .ref => return zirRef(mod, scope, old_inst.castTag(.ref).?),
58 .resolve_inferred_alloc => return zirResolveInferredAlloc(mod, scope, old_inst.castTag(.resolve_inferred_alloc).?),
59 .ret_ptr => return zirRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),
60 .ret_type => return zirRetType(mod, scope, old_inst.castTag(.ret_type).?),
61 .store_to_block_ptr => return zirStoreToBlockPtr(mod, scope, old_inst.castTag(.store_to_block_ptr).?),
62 .store_to_inferred_ptr => return zirStoreToInferredPtr(mod, scope, old_inst.castTag(.store_to_inferred_ptr).?),
63 .single_const_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?, false, .One),
64 .single_mut_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?, true, .One),
65 .many_const_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.many_const_ptr_type).?, false, .Many),
66 .many_mut_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.many_mut_ptr_type).?, true, .Many),
67 .c_const_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.c_const_ptr_type).?, false, .C),
68 .c_mut_ptr_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.c_mut_ptr_type).?, true, .C),
69 .const_slice_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.const_slice_type).?, false, .Slice),
70 .mut_slice_type => return zirSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice),
71 .ptr_type => return zirPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
72 .store => return zirStore(mod, scope, old_inst.castTag(.store).?),
73 .set_eval_branch_quota => return zirSetEvalBranchQuota(mod, scope, old_inst.castTag(.set_eval_branch_quota).?),
74 .str => return zirStr(mod, scope, old_inst.castTag(.str).?),
75 .int => return zirInt(mod, scope, old_inst.castTag(.int).?),
76 .int_type => return zirIntType(mod, scope, old_inst.castTag(.int_type).?),
77 .loop => return zirLoop(mod, scope, old_inst.castTag(.loop).?),
78 .param_type => return zirParamType(mod, scope, old_inst.castTag(.param_type).?),
79 .ptrtoint => return zirPtrtoint(mod, scope, old_inst.castTag(.ptrtoint).?),
80 .field_ptr => return zirFieldPtr(mod, scope, old_inst.castTag(.field_ptr).?),
81 .field_val => return zirFieldVal(mod, scope, old_inst.castTag(.field_val).?),
82 .field_ptr_named => return zirFieldPtrNamed(mod, scope, old_inst.castTag(.field_ptr_named).?),
83 .field_val_named => return zirFieldValNamed(mod, scope, old_inst.castTag(.field_val_named).?),
84 .deref => return zirDeref(mod, scope, old_inst.castTag(.deref).?),
85 .as => return zirAs(mod, scope, old_inst.castTag(.as).?),
86 .@"asm" => return zirAsm(mod, scope, old_inst.castTag(.@"asm").?),
87 .unreachable_safe => return zirUnreachable(mod, scope, old_inst.castTag(.unreachable_safe).?, true),
88 .unreachable_unsafe => return zirUnreachable(mod, scope, old_inst.castTag(.unreachable_unsafe).?, false),
89 .@"return" => return zirReturn(mod, scope, old_inst.castTag(.@"return").?),
90 .return_void => return zirReturnVoid(mod, scope, old_inst.castTag(.return_void).?),
91 .@"fn" => return zirFn(mod, scope, old_inst.castTag(.@"fn").?),
92 .@"export" => return zirExport(mod, scope, old_inst.castTag(.@"export").?),
93 .primitive => return zirPrimitive(mod, scope, old_inst.castTag(.primitive).?),
94 .fntype => return zirFnType(mod, scope, old_inst.castTag(.fntype).?),
95 .intcast => return zirIntcast(mod, scope, old_inst.castTag(.intcast).?),
96 .bitcast => return zirBitcast(mod, scope, old_inst.castTag(.bitcast).?),
97 .floatcast => return zirFloatcast(mod, scope, old_inst.castTag(.floatcast).?),
98 .elem_ptr => return zirElemPtr(mod, scope, old_inst.castTag(.elem_ptr).?),
99 .elem_val => return zirElemVal(mod, scope, old_inst.castTag(.elem_val).?),
100 .add => return zirArithmetic(mod, scope, old_inst.castTag(.add).?),
101 .addwrap => return zirArithmetic(mod, scope, old_inst.castTag(.addwrap).?),
102 .sub => return zirArithmetic(mod, scope, old_inst.castTag(.sub).?),
103 .subwrap => return zirArithmetic(mod, scope, old_inst.castTag(.subwrap).?),
104 .mul => return zirArithmetic(mod, scope, old_inst.castTag(.mul).?),
105 .mulwrap => return zirArithmetic(mod, scope, old_inst.castTag(.mulwrap).?),
106 .div => return zirArithmetic(mod, scope, old_inst.castTag(.div).?),
107 .mod_rem => return zirArithmetic(mod, scope, old_inst.castTag(.mod_rem).?),
108 .array_cat => return zirArrayCat(mod, scope, old_inst.castTag(.array_cat).?),
109 .array_mul => return zirArrayMul(mod, scope, old_inst.castTag(.array_mul).?),
110 .bit_and => return zirBitwise(mod, scope, old_inst.castTag(.bit_and).?),
111 .bit_not => return zirBitNot(mod, scope, old_inst.castTag(.bit_not).?),
112 .bit_or => return zirBitwise(mod, scope, old_inst.castTag(.bit_or).?),
113 .xor => return zirBitwise(mod, scope, old_inst.castTag(.xor).?),
114 .shl => return zirShl(mod, scope, old_inst.castTag(.shl).?),
115 .shr => return zirShr(mod, scope, old_inst.castTag(.shr).?),
116 .cmp_lt => return zirCmp(mod, scope, old_inst.castTag(.cmp_lt).?, .lt),
117 .cmp_lte => return zirCmp(mod, scope, old_inst.castTag(.cmp_lte).?, .lte),
118 .cmp_eq => return zirCmp(mod, scope, old_inst.castTag(.cmp_eq).?, .eq),
119 .cmp_gte => return zirCmp(mod, scope, old_inst.castTag(.cmp_gte).?, .gte),
120 .cmp_gt => return zirCmp(mod, scope, old_inst.castTag(.cmp_gt).?, .gt),
121 .cmp_neq => return zirCmp(mod, scope, old_inst.castTag(.cmp_neq).?, .neq),
122 .condbr => return zirCondbr(mod, scope, old_inst.castTag(.condbr).?),
123 .is_null => return zirIsNull(mod, scope, old_inst.castTag(.is_null).?, false),
124 .is_non_null => return zirIsNull(mod, scope, old_inst.castTag(.is_non_null).?, true),
125 .is_null_ptr => return zirIsNullPtr(mod, scope, old_inst.castTag(.is_null_ptr).?, false),
126 .is_non_null_ptr => return zirIsNullPtr(mod, scope, old_inst.castTag(.is_non_null_ptr).?, true),
127 .is_err => return zirIsErr(mod, scope, old_inst.castTag(.is_err).?),
128 .is_err_ptr => return zirIsErrPtr(mod, scope, old_inst.castTag(.is_err_ptr).?),
129 .bool_not => return zirBoolNot(mod, scope, old_inst.castTag(.bool_not).?),
130 .typeof => return zirTypeof(mod, scope, old_inst.castTag(.typeof).?),
131 .typeof_peer => return zirTypeofPeer(mod, scope, old_inst.castTag(.typeof_peer).?),
132 .optional_type => return zirOptionalType(mod, scope, old_inst.castTag(.optional_type).?),
133 .optional_payload_safe => return zirOptionalPayload(mod, scope, old_inst.castTag(.optional_payload_safe).?, true),
134 .optional_payload_unsafe => return zirOptionalPayload(mod, scope, old_inst.castTag(.optional_payload_unsafe).?, false),
135 .optional_payload_safe_ptr => return zirOptionalPayloadPtr(mod, scope, old_inst.castTag(.optional_payload_safe_ptr).?, true),
136 .optional_payload_unsafe_ptr => return zirOptionalPayloadPtr(mod, scope, old_inst.castTag(.optional_payload_unsafe_ptr).?, false),
137 .err_union_payload_safe => return zirErrUnionPayload(mod, scope, old_inst.castTag(.err_union_payload_safe).?, true),
138 .err_union_payload_unsafe => return zirErrUnionPayload(mod, scope, old_inst.castTag(.err_union_payload_unsafe).?, false),
139 .err_union_payload_safe_ptr => return zirErrUnionPayloadPtr(mod, scope, old_inst.castTag(.err_union_payload_safe_ptr).?, true),
140 .err_union_payload_unsafe_ptr => return zirErrUnionPayloadPtr(mod, scope, old_inst.castTag(.err_union_payload_unsafe_ptr).?, false),
141 .err_union_code => return zirErrUnionCode(mod, scope, old_inst.castTag(.err_union_code).?),
142 .err_union_code_ptr => return zirErrUnionCodePtr(mod, scope, old_inst.castTag(.err_union_code_ptr).?),
143 .ensure_err_payload_void => return zirEnsureErrPayloadVoid(mod, scope, old_inst.castTag(.ensure_err_payload_void).?),
144 .array_type => return zirArrayType(mod, scope, old_inst.castTag(.array_type).?),
145 .array_type_sentinel => return zirArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?),
146 .enum_literal => return zirEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?),
147 .merge_error_sets => return zirMergeErrorSets(mod, scope, old_inst.castTag(.merge_error_sets).?),
148 .error_union_type => return zirErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),
149 .anyframe_type => return zirAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),
150 .error_set => return zirErrorSet(mod, scope, old_inst.castTag(.error_set).?),
151 .slice => return zirSlice(mod, scope, old_inst.castTag(.slice).?),
152 .slice_start => return zirSliceStart(mod, scope, old_inst.castTag(.slice_start).?),
153 .import => return zirImport(mod, scope, old_inst.castTag(.import).?),
154 .bool_and => return zirBoolOp(mod, scope, old_inst.castTag(.bool_and).?),
155 .bool_or => return zirBoolOp(mod, scope, old_inst.castTag(.bool_or).?),
156 .void_value => return mod.constVoid(scope, old_inst.src),
169157
170158 .container_field_named,
171159 .container_field_typed,
......@@ -258,7 +246,7 @@ pub fn resolveInstConst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerE
258246 };
259247}
260248
261fn analyzeInstConst(mod: *Module, scope: *Scope, const_inst: *zir.Inst.Const) InnerError!*Inst {
249fn zirConst(mod: *Module, scope: *Scope, const_inst: *zir.Inst.Const) InnerError!*Inst {
262250 const tracy = trace(@src());
263251 defer tracy.end();
264252 // Move the TypedValue from old memory to new memory. This allows freeing the ZIR instructions
......@@ -275,44 +263,25 @@ fn analyzeConstInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError
275263 };
276264}
277265
278fn analyzeInstCoerceResultBlockPtr(
279 mod: *Module,
280 scope: *Scope,
281 inst: *zir.Inst.CoerceResultBlockPtr,
282) InnerError!*Inst {
283 const tracy = trace(@src());
284 defer tracy.end();
285 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstCoerceResultBlockPtr", .{});
286}
287
288fn bitCastRef(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
266fn zirBitcastRef(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
289267 const tracy = trace(@src());
290268 defer tracy.end();
291 return mod.fail(scope, inst.base.src, "TODO implement zir_sema.bitCastRef", .{});
269 return mod.fail(scope, inst.base.src, "TODO implement zir_sema.zirBitcastRef", .{});
292270}
293271
294fn bitCastResultPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
272fn zirBitcastResultPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
295273 const tracy = trace(@src());
296274 defer tracy.end();
297 return mod.fail(scope, inst.base.src, "TODO implement zir_sema.bitCastResultPtr", .{});
275 return mod.fail(scope, inst.base.src, "TODO implement zir_sema.zirBitcastResultPtr", .{});
298276}
299277
300fn analyzeInstCoerceResultPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
278fn zirCoerceResultPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
301279 const tracy = trace(@src());
302280 defer tracy.end();
303 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstCoerceResultPtr", .{});
281 return mod.fail(scope, inst.base.src, "TODO implement zirCoerceResultPtr", .{});
304282}
305283
306/// Equivalent to `as(ptr_child_type(typeof(ptr)), value)`.
307fn analyzeInstCoerceToPtrElem(mod: *Module, scope: *Scope, inst: *zir.Inst.CoerceToPtrElem) InnerError!*Inst {
308 const tracy = trace(@src());
309 defer tracy.end();
310 const ptr = try resolveInst(mod, scope, inst.positionals.ptr);
311 const operand = try resolveInst(mod, scope, inst.positionals.value);
312 return mod.coerce(scope, ptr.ty.elemType(), operand);
313}
314
315fn analyzeInstRetPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
284fn zirRetPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
316285 const tracy = trace(@src());
317286 defer tracy.end();
318287 const b = try mod.requireFunctionBlock(scope, inst.base.src);
......@@ -322,7 +291,7 @@ fn analyzeInstRetPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerErr
322291 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
323292}
324293
325fn ref(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
294fn zirRef(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
326295 const tracy = trace(@src());
327296 defer tracy.end();
328297
......@@ -330,7 +299,7 @@ fn ref(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
330299 return mod.analyzeRef(scope, inst.base.src, operand);
331300}
332301
333fn analyzeInstRetType(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
302fn zirRetType(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
334303 const tracy = trace(@src());
335304 defer tracy.end();
336305 const b = try mod.requireFunctionBlock(scope, inst.base.src);
......@@ -339,7 +308,7 @@ fn analyzeInstRetType(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerEr
339308 return mod.constType(scope, inst.base.src, ret_type);
340309}
341310
342fn analyzeInstEnsureResultUsed(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
311fn zirEnsureResultUsed(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
343312 const tracy = trace(@src());
344313 defer tracy.end();
345314 const operand = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -349,7 +318,7 @@ fn analyzeInstEnsureResultUsed(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp
349318 }
350319}
351320
352fn analyzeInstEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
321fn zirEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
353322 const tracy = trace(@src());
354323 defer tracy.end();
355324 const operand = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -359,7 +328,7 @@ fn analyzeInstEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst.
359328 }
360329}
361330
362fn indexablePtrLen(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
331fn zirIndexablePtrLen(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
363332 const tracy = trace(@src());
364333 defer tracy.end();
365334
......@@ -389,7 +358,7 @@ fn indexablePtrLen(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError
389358 return mod.analyzeDeref(scope, inst.base.src, result_ptr, result_ptr.src);
390359}
391360
392fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
361fn zirAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
393362 const tracy = trace(@src());
394363 defer tracy.end();
395364 const var_type = try resolveType(mod, scope, inst.positionals.operand);
......@@ -398,7 +367,7 @@ fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErro
398367 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
399368}
400369
401fn analyzeInstAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
370fn zirAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
402371 const tracy = trace(@src());
403372 defer tracy.end();
404373 const var_type = try resolveType(mod, scope, inst.positionals.operand);
......@@ -408,7 +377,7 @@ fn analyzeInstAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerE
408377 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
409378}
410379
411fn analyzeInstAllocInferred(
380fn zirAllocInferred(
412381 mod: *Module,
413382 scope: *Scope,
414383 inst: *zir.Inst.NoOp,
......@@ -437,7 +406,7 @@ fn analyzeInstAllocInferred(
437406 return result;
438407}
439408
440fn analyzeInstResolveInferredAlloc(
409fn zirResolveInferredAlloc(
441410 mod: *Module,
442411 scope: *Scope,
443412 inst: *zir.Inst.UnOp,
......@@ -466,28 +435,46 @@ fn analyzeInstResolveInferredAlloc(
466435 return mod.constVoid(scope, inst.base.src);
467436}
468437
469fn analyzeInstStoreToInferredPtr(
438fn zirStoreToBlockPtr(
470439 mod: *Module,
471440 scope: *Scope,
472441 inst: *zir.Inst.BinOp,
473442) InnerError!*Inst {
474443 const tracy = trace(@src());
475444 defer tracy.end();
445
446 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
447 const value = try resolveInst(mod, scope, inst.positionals.rhs);
448 const ptr_ty = try mod.simplePtrType(scope, inst.base.src, value.ty, true, .One);
449 // TODO detect when this store should be done at compile-time. For example,
450 // if expressions should force it when the condition is compile-time known.
451 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
452 const bitcasted_ptr = try mod.addUnOp(b, inst.base.src, ptr_ty, .bitcast, ptr);
453 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);
454}
455
456fn zirStoreToInferredPtr(
457 mod: *Module,
458 scope: *Scope,
459 inst: *zir.Inst.BinOp,
460) InnerError!*Inst {
461 const tracy = trace(@src());
462 defer tracy.end();
463
476464 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
477465 const value = try resolveInst(mod, scope, inst.positionals.rhs);
478466 const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?;
479467 // Add the stored instruction to the set we will use to resolve peer types
480468 // for the inferred allocation.
481469 try inferred_alloc.data.stored_inst_list.append(scope.arena(), value);
482 // Create a new alloc with exactly the type the pointer wants.
483 // Later it gets cleaned up by aliasing the alloc we are supposed to be storing to.
470 // Create a runtime bitcast instruction with exactly the type the pointer wants.
484471 const ptr_ty = try mod.simplePtrType(scope, inst.base.src, value.ty, true, .One);
485472 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
486473 const bitcasted_ptr = try mod.addUnOp(b, inst.base.src, ptr_ty, .bitcast, ptr);
487474 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);
488475}
489476
490fn analyzeInstSetEvalBranchQuota(
477fn zirSetEvalBranchQuota(
491478 mod: *Module,
492479 scope: *Scope,
493480 inst: *zir.Inst.UnOp,
......@@ -499,15 +486,16 @@ fn analyzeInstSetEvalBranchQuota(
499486 return mod.constVoid(scope, inst.base.src);
500487}
501488
502fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
489fn zirStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
503490 const tracy = trace(@src());
504491 defer tracy.end();
492
505493 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
506494 const value = try resolveInst(mod, scope, inst.positionals.rhs);
507495 return mod.storePtr(scope, inst.base.src, ptr, value);
508496}
509497
510fn analyzeInstParamType(mod: *Module, scope: *Scope, inst: *zir.Inst.ParamType) InnerError!*Inst {
498fn zirParamType(mod: *Module, scope: *Scope, inst: *zir.Inst.ParamType) InnerError!*Inst {
511499 const tracy = trace(@src());
512500 defer tracy.end();
513501 const fn_inst = try resolveInst(mod, scope, inst.positionals.func);
......@@ -516,7 +504,7 @@ fn analyzeInstParamType(mod: *Module, scope: *Scope, inst: *zir.Inst.ParamType)
516504 const fn_ty: Type = switch (fn_inst.ty.zigTypeTag()) {
517505 .Fn => fn_inst.ty,
518506 .BoundFn => {
519 return mod.fail(scope, fn_inst.src, "TODO implement analyzeInstParamType for method call syntax", .{});
507 return mod.fail(scope, fn_inst.src, "TODO implement zirParamType for method call syntax", .{});
520508 },
521509 else => {
522510 return mod.fail(scope, fn_inst.src, "expected function, found '{}'", .{fn_inst.ty});
......@@ -538,7 +526,7 @@ fn analyzeInstParamType(mod: *Module, scope: *Scope, inst: *zir.Inst.ParamType)
538526 return mod.constType(scope, inst.base.src, param_type);
539527}
540528
541fn analyzeInstStr(mod: *Module, scope: *Scope, str_inst: *zir.Inst.Str) InnerError!*Inst {
529fn zirStr(mod: *Module, scope: *Scope, str_inst: *zir.Inst.Str) InnerError!*Inst {
542530 const tracy = trace(@src());
543531 defer tracy.end();
544532 // The bytes references memory inside the ZIR module, which can get deallocated
......@@ -557,14 +545,14 @@ fn analyzeInstStr(mod: *Module, scope: *Scope, str_inst: *zir.Inst.Str) InnerErr
557545 return mod.analyzeDeclRef(scope, str_inst.base.src, new_decl);
558546}
559547
560fn analyzeInstInt(mod: *Module, scope: *Scope, inst: *zir.Inst.Int) InnerError!*Inst {
548fn zirInt(mod: *Module, scope: *Scope, inst: *zir.Inst.Int) InnerError!*Inst {
561549 const tracy = trace(@src());
562550 defer tracy.end();
563551
564552 return mod.constIntBig(scope, inst.base.src, Type.initTag(.comptime_int), inst.positionals.int);
565553}
566554
567fn analyzeInstExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export) InnerError!*Inst {
555fn zirExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export) InnerError!*Inst {
568556 const tracy = trace(@src());
569557 defer tracy.end();
570558 const symbol_name = try resolveConstString(mod, scope, export_inst.positionals.symbol_name);
......@@ -574,14 +562,14 @@ fn analyzeInstExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export)
574562 return mod.constVoid(scope, export_inst.base.src);
575563}
576564
577fn analyzeInstCompileError(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
565fn zirCompileError(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
578566 const tracy = trace(@src());
579567 defer tracy.end();
580568 const msg = try resolveConstString(mod, scope, inst.positionals.operand);
581569 return mod.fail(scope, inst.base.src, "{s}", .{msg});
582570}
583571
584fn analyzeInstCompileLog(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileLog) InnerError!*Inst {
572fn zirCompileLog(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileLog) InnerError!*Inst {
585573 var managed = mod.compile_log_text.toManaged(mod.gpa);
586574 defer mod.compile_log_text = managed.moveToUnmanaged();
587575 const writer = managed.writer();
......@@ -608,7 +596,7 @@ fn analyzeInstCompileLog(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileLog
608596 return mod.constVoid(scope, inst.base.src);
609597}
610598
611fn analyzeInstArg(mod: *Module, scope: *Scope, inst: *zir.Inst.Arg) InnerError!*Inst {
599fn zirArg(mod: *Module, scope: *Scope, inst: *zir.Inst.Arg) InnerError!*Inst {
612600 const tracy = trace(@src());
613601 defer tracy.end();
614602 const b = try mod.requireFunctionBlock(scope, inst.base.src);
......@@ -631,7 +619,7 @@ fn analyzeInstArg(mod: *Module, scope: *Scope, inst: *zir.Inst.Arg) InnerError!*
631619 return mod.addArg(b, inst.base.src, param_type, name);
632620}
633621
634fn analyzeInstLoop(mod: *Module, scope: *Scope, inst: *zir.Inst.Loop) InnerError!*Inst {
622fn zirLoop(mod: *Module, scope: *Scope, inst: *zir.Inst.Loop) InnerError!*Inst {
635623 const tracy = trace(@src());
636624 defer tracy.end();
637625 const parent_block = scope.cast(Scope.Block).?;
......@@ -672,25 +660,14 @@ fn analyzeInstLoop(mod: *Module, scope: *Scope, inst: *zir.Inst.Loop) InnerError
672660 return &loop_inst.base;
673661}
674662
675fn analyzeInstBlockFlat(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_comptime: bool) InnerError!*Inst {
663fn zirBlockFlat(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_comptime: bool) InnerError!*Inst {
676664 const tracy = trace(@src());
677665 defer tracy.end();
678666 const parent_block = scope.cast(Scope.Block).?;
679667
680 var child_block: Scope.Block = .{
681 .parent = parent_block,
682 .inst_table = parent_block.inst_table,
683 .func = parent_block.func,
684 .owner_decl = parent_block.owner_decl,
685 .src_decl = parent_block.src_decl,
686 .instructions = .{},
687 .arena = parent_block.arena,
688 .label = null,
689 .inlining = parent_block.inlining,
690 .is_comptime = parent_block.is_comptime or is_comptime,
691 .branch_quota = parent_block.branch_quota,
692 };
668 var child_block = parent_block.makeSubBlock();
693669 defer child_block.instructions.deinit(mod.gpa);
670 child_block.is_comptime = child_block.is_comptime or is_comptime;
694671
695672 try analyzeBody(mod, &child_block, inst.positionals.body);
696673
......@@ -704,9 +681,15 @@ fn analyzeInstBlockFlat(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_c
704681 return resolveInst(mod, scope, last_zir_inst);
705682}
706683
707fn analyzeInstBlock(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_comptime: bool) InnerError!*Inst {
684fn zirBlock(
685 mod: *Module,
686 scope: *Scope,
687 inst: *zir.Inst.Block,
688 is_comptime: bool,
689) InnerError!*Inst {
708690 const tracy = trace(@src());
709691 defer tracy.end();
692
710693 const parent_block = scope.cast(Scope.Block).?;
711694
712695 // Reserve space for a Block instruction so that generated Break instructions can
......@@ -735,6 +718,7 @@ fn analyzeInstBlock(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_compt
735718 .zir_block = inst,
736719 .merges = .{
737720 .results = .{},
721 .br_list = .{},
738722 .block_inst = block_inst,
739723 },
740724 }),
......@@ -746,6 +730,7 @@ fn analyzeInstBlock(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_compt
746730
747731 defer child_block.instructions.deinit(mod.gpa);
748732 defer merges.results.deinit(mod.gpa);
733 defer merges.br_list.deinit(mod.gpa);
749734
750735 try analyzeBody(mod, &child_block, inst.positionals.body);
751736
......@@ -779,49 +764,127 @@ fn analyzeBlockBody(
779764 const last_inst = child_block.instructions.items[last_inst_index];
780765 if (last_inst.breakBlock()) |br_block| {
781766 if (br_block == merges.block_inst) {
782 // No need for a block instruction. We can put the new instructions directly into the parent block.
783 // Here we omit the break instruction.
767 // No need for a block instruction. We can put the new instructions directly
768 // into the parent block. Here we omit the break instruction.
784769 const copied_instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items[0..last_inst_index]);
785770 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
786771 return merges.results.items[0];
787772 }
788773 }
789774 }
790 // It should be impossible to have the number of results be > 1 in a comptime scope.
791 assert(!child_block.is_comptime); // We should have already got a compile error in the condbr condition.
775 // It is impossible to have the number of results be > 1 in a comptime scope.
776 assert(!child_block.is_comptime); // Should already got a compile error in the condbr condition.
792777
793778 // Need to set the type and emit the Block instruction. This allows machine code generation
794779 // to emit a jump instruction to after the block when it encounters the break.
795780 try parent_block.instructions.append(mod.gpa, &merges.block_inst.base);
796 merges.block_inst.base.ty = try mod.resolvePeerTypes(scope, merges.results.items);
797 merges.block_inst.body = .{ .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items) };
781 const resolved_ty = try mod.resolvePeerTypes(scope, merges.results.items);
782 merges.block_inst.base.ty = resolved_ty;
783 merges.block_inst.body = .{
784 .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items),
785 };
786 // Now that the block has its type resolved, we need to go back into all the break
787 // instructions, and insert type coercion on the operands.
788 for (merges.br_list.items) |br| {
789 if (br.operand.ty.eql(resolved_ty)) {
790 // No type coercion needed.
791 continue;
792 }
793 var coerce_block = parent_block.makeSubBlock();
794 defer coerce_block.instructions.deinit(mod.gpa);
795 const coerced_operand = try mod.coerce(&coerce_block.base, resolved_ty, br.operand);
796 // If no instructions were produced, such as in the case of a coercion of a
797 // constant value to a new type, we can simply point the br operand to it.
798 if (coerce_block.instructions.items.len == 0) {
799 br.operand = coerced_operand;
800 continue;
801 }
802 assert(coerce_block.instructions.items[coerce_block.instructions.items.len - 1] == coerced_operand);
803 // Here we depend on the br instruction having been over-allocated (if necessary)
804 // inide analyzeBreak so that it can be converted into a br_block_flat instruction.
805 const br_src = br.base.src;
806 const br_ty = br.base.ty;
807 const br_block_flat = @ptrCast(*Inst.BrBlockFlat, br);
808 br_block_flat.* = .{
809 .base = .{
810 .src = br_src,
811 .ty = br_ty,
812 .tag = .br_block_flat,
813 },
814 .block = merges.block_inst,
815 .body = .{
816 .instructions = try parent_block.arena.dupe(*Inst, coerce_block.instructions.items),
817 },
818 };
819 }
798820 return &merges.block_inst.base;
799821}
800822
801fn analyzeInstBreakpoint(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
823fn zirBreakpoint(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
802824 const tracy = trace(@src());
803825 defer tracy.end();
804826 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
805827 return mod.addNoOp(b, inst.base.src, Type.initTag(.void), .breakpoint);
806828}
807829
808fn analyzeInstBreak(mod: *Module, scope: *Scope, inst: *zir.Inst.Break) InnerError!*Inst {
830fn zirBreak(mod: *Module, scope: *Scope, inst: *zir.Inst.Break) InnerError!*Inst {
809831 const tracy = trace(@src());
810832 defer tracy.end();
833
811834 const operand = try resolveInst(mod, scope, inst.positionals.operand);
812835 const block = inst.positionals.block;
813836 return analyzeBreak(mod, scope, inst.base.src, block, operand);
814837}
815838
816fn analyzeInstBreakVoid(mod: *Module, scope: *Scope, inst: *zir.Inst.BreakVoid) InnerError!*Inst {
839fn zirBreakVoid(mod: *Module, scope: *Scope, inst: *zir.Inst.BreakVoid) InnerError!*Inst {
817840 const tracy = trace(@src());
818841 defer tracy.end();
842
819843 const block = inst.positionals.block;
820844 const void_inst = try mod.constVoid(scope, inst.base.src);
821845 return analyzeBreak(mod, scope, inst.base.src, block, void_inst);
822846}
823847
824fn analyzeInstDbgStmt(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
848fn analyzeBreak(
849 mod: *Module,
850 scope: *Scope,
851 src: usize,
852 zir_block: *zir.Inst.Block,
853 operand: *Inst,
854) InnerError!*Inst {
855 var opt_block = scope.cast(Scope.Block);
856 while (opt_block) |block| {
857 if (block.label) |*label| {
858 if (label.zir_block == zir_block) {
859 const b = try mod.requireFunctionBlock(scope, src);
860 // Here we add a br instruction, but we over-allocate a little bit
861 // (if necessary) to make it possible to convert the instruction into
862 // a br_block_flat instruction later.
863 const br = @ptrCast(*Inst.Br, try b.arena.alignedAlloc(
864 u8,
865 Inst.convertable_br_align,
866 Inst.convertable_br_size,
867 ));
868 br.* = .{
869 .base = .{
870 .tag = .br,
871 .ty = Type.initTag(.noreturn),
872 .src = src,
873 },
874 .operand = operand,
875 .block = label.merges.block_inst,
876 };
877 try b.instructions.append(mod.gpa, &br.base);
878 try label.merges.results.append(mod.gpa, operand);
879 try label.merges.br_list.append(mod.gpa, br);
880 return &br.base;
881 }
882 }
883 opt_block = block.parent;
884 } else unreachable;
885}
886
887fn zirDbgStmt(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
825888 const tracy = trace(@src());
826889 defer tracy.end();
827890 if (scope.cast(Scope.Block)) |b| {
......@@ -832,26 +895,26 @@ fn analyzeInstDbgStmt(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerEr
832895 return mod.constVoid(scope, inst.base.src);
833896}
834897
835fn analyzeInstDeclRefStr(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclRefStr) InnerError!*Inst {
898fn zirDeclRefStr(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclRefStr) InnerError!*Inst {
836899 const tracy = trace(@src());
837900 defer tracy.end();
838901 const decl_name = try resolveConstString(mod, scope, inst.positionals.name);
839902 return mod.analyzeDeclRefByName(scope, inst.base.src, decl_name);
840903}
841904
842fn declRef(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclRef) InnerError!*Inst {
905fn zirDeclRef(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclRef) InnerError!*Inst {
843906 const tracy = trace(@src());
844907 defer tracy.end();
845908 return mod.analyzeDeclRef(scope, inst.base.src, inst.positionals.decl);
846909}
847910
848fn declVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerError!*Inst {
911fn zirDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerError!*Inst {
849912 const tracy = trace(@src());
850913 defer tracy.end();
851914 return mod.analyzeDeclVal(scope, inst.base.src, inst.positionals.decl);
852915}
853916
854fn call(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
917fn zirCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
855918 const tracy = trace(@src());
856919 defer tracy.end();
857920
......@@ -965,6 +1028,7 @@ fn call(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
9651028 .casted_args = casted_args,
9661029 .merges = .{
9671030 .results = .{},
1031 .br_list = .{},
9681032 .block_inst = block_inst,
9691033 },
9701034 };
......@@ -989,6 +1053,7 @@ fn call(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
9891053
9901054 defer child_block.instructions.deinit(mod.gpa);
9911055 defer merges.results.deinit(mod.gpa);
1056 defer merges.br_list.deinit(mod.gpa);
9921057
9931058 try mod.emitBackwardBranch(&child_block, inst.base.src);
9941059
......@@ -1002,7 +1067,7 @@ fn call(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
10021067 return mod.addCall(b, inst.base.src, ret_type, func, casted_args);
10031068}
10041069
1005fn analyzeInstFn(mod: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {
1070fn zirFn(mod: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {
10061071 const tracy = trace(@src());
10071072 defer tracy.end();
10081073 const fn_type = try resolveType(mod, scope, fn_inst.positionals.fn_type);
......@@ -1019,13 +1084,13 @@ fn analyzeInstFn(mod: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!
10191084 });
10201085}
10211086
1022fn analyzeInstIntType(mod: *Module, scope: *Scope, inttype: *zir.Inst.IntType) InnerError!*Inst {
1087fn zirIntType(mod: *Module, scope: *Scope, inttype: *zir.Inst.IntType) InnerError!*Inst {
10231088 const tracy = trace(@src());
10241089 defer tracy.end();
10251090 return mod.fail(scope, inttype.base.src, "TODO implement inttype", .{});
10261091}
10271092
1028fn analyzeInstOptionalType(mod: *Module, scope: *Scope, optional: *zir.Inst.UnOp) InnerError!*Inst {
1093fn zirOptionalType(mod: *Module, scope: *Scope, optional: *zir.Inst.UnOp) InnerError!*Inst {
10291094 const tracy = trace(@src());
10301095 defer tracy.end();
10311096 const child_type = try resolveType(mod, scope, optional.positionals.operand);
......@@ -1033,7 +1098,7 @@ fn analyzeInstOptionalType(mod: *Module, scope: *Scope, optional: *zir.Inst.UnOp
10331098 return mod.constType(scope, optional.base.src, try mod.optionalType(scope, child_type));
10341099}
10351100
1036fn analyzeInstArrayType(mod: *Module, scope: *Scope, array: *zir.Inst.BinOp) InnerError!*Inst {
1101fn zirArrayType(mod: *Module, scope: *Scope, array: *zir.Inst.BinOp) InnerError!*Inst {
10371102 const tracy = trace(@src());
10381103 defer tracy.end();
10391104 // TODO these should be lazily evaluated
......@@ -1043,7 +1108,7 @@ fn analyzeInstArrayType(mod: *Module, scope: *Scope, array: *zir.Inst.BinOp) Inn
10431108 return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), null, elem_type));
10441109}
10451110
1046fn analyzeInstArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.ArrayTypeSentinel) InnerError!*Inst {
1111fn zirArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.ArrayTypeSentinel) InnerError!*Inst {
10471112 const tracy = trace(@src());
10481113 defer tracy.end();
10491114 // TODO these should be lazily evaluated
......@@ -1054,7 +1119,7 @@ fn analyzeInstArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.Ar
10541119 return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type));
10551120}
10561121
1057fn analyzeInstErrorUnionType(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1122fn zirErrorUnionType(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
10581123 const tracy = trace(@src());
10591124 defer tracy.end();
10601125 const error_union = try resolveType(mod, scope, inst.positionals.lhs);
......@@ -1067,7 +1132,7 @@ fn analyzeInstErrorUnionType(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp)
10671132 return mod.constType(scope, inst.base.src, try mod.errorUnionType(scope, error_union, payload));
10681133}
10691134
1070fn analyzeInstAnyframeType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1135fn zirAnyframeType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
10711136 const tracy = trace(@src());
10721137 defer tracy.end();
10731138 const return_type = try resolveType(mod, scope, inst.positionals.operand);
......@@ -1075,7 +1140,7 @@ fn analyzeInstAnyframeType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) In
10751140 return mod.constType(scope, inst.base.src, try mod.anyframeType(scope, return_type));
10761141}
10771142
1078fn analyzeInstErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) InnerError!*Inst {
1143fn zirErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) InnerError!*Inst {
10791144 const tracy = trace(@src());
10801145 defer tracy.end();
10811146 // The declarations arena will store the hashmap.
......@@ -1107,13 +1172,13 @@ fn analyzeInstErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) In
11071172 return mod.analyzeDeclVal(scope, inst.base.src, new_decl);
11081173}
11091174
1110fn analyzeInstMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1175fn zirMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
11111176 const tracy = trace(@src());
11121177 defer tracy.end();
11131178 return mod.fail(scope, inst.base.src, "TODO implement merge_error_sets", .{});
11141179}
11151180
1116fn analyzeInstEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiteral) InnerError!*Inst {
1181fn zirEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiteral) InnerError!*Inst {
11171182 const tracy = trace(@src());
11181183 defer tracy.end();
11191184 const duped_name = try scope.arena().dupe(u8, inst.positionals.name);
......@@ -1124,7 +1189,7 @@ fn analyzeInstEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiter
11241189}
11251190
11261191/// Pointer in, pointer out.
1127fn optionalPayloadPtr(
1192fn zirOptionalPayloadPtr(
11281193 mod: *Module,
11291194 scope: *Scope,
11301195 unwrap: *zir.Inst.UnOp,
......@@ -1165,7 +1230,7 @@ fn optionalPayloadPtr(
11651230}
11661231
11671232/// Value in, value out.
1168fn optionalPayload(
1233fn zirOptionalPayload(
11691234 mod: *Module,
11701235 scope: *Scope,
11711236 unwrap: *zir.Inst.UnOp,
......@@ -1201,40 +1266,40 @@ fn optionalPayload(
12011266}
12021267
12031268/// Value in, value out
1204fn errorUnionPayload(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst {
1269fn zirErrUnionPayload(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst {
12051270 const tracy = trace(@src());
12061271 defer tracy.end();
1207 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.errorUnionPayload", .{});
1272 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.zirErrUnionPayload", .{});
12081273}
12091274
12101275/// Pointer in, pointer out
1211fn errorUnionPayloadPtr(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst {
1276fn zirErrUnionPayloadPtr(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst {
12121277 const tracy = trace(@src());
12131278 defer tracy.end();
1214 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.errorUnionPayloadPtr", .{});
1279 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.zirErrUnionPayloadPtr", .{});
12151280}
12161281
12171282/// Value in, value out
1218fn errorUnionCode(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
1283fn zirErrUnionCode(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
12191284 const tracy = trace(@src());
12201285 defer tracy.end();
1221 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.errorUnionCode", .{});
1286 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.zirErrUnionCode", .{});
12221287}
12231288
12241289/// Pointer in, value out
1225fn errorUnionCodePtr(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
1290fn zirErrUnionCodePtr(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
12261291 const tracy = trace(@src());
12271292 defer tracy.end();
1228 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.errorUnionCodePtr", .{});
1293 return mod.fail(scope, unwrap.base.src, "TODO implement zir_sema.zirErrUnionCodePtr", .{});
12291294}
12301295
1231fn analyzeInstEnsureErrPayloadVoid(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
1296fn zirEnsureErrPayloadVoid(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst {
12321297 const tracy = trace(@src());
12331298 defer tracy.end();
1234 return mod.fail(scope, unwrap.base.src, "TODO implement analyzeInstEnsureErrPayloadVoid", .{});
1299 return mod.fail(scope, unwrap.base.src, "TODO implement zirEnsureErrPayloadVoid", .{});
12351300}
12361301
1237fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*Inst {
1302fn zirFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*Inst {
12381303 const tracy = trace(@src());
12391304 defer tracy.end();
12401305 const return_type = try resolveType(mod, scope, fntype.positionals.return_type);
......@@ -1277,13 +1342,13 @@ fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) Inne
12771342 return mod.constType(scope, fntype.base.src, fn_ty);
12781343}
12791344
1280fn analyzeInstPrimitive(mod: *Module, scope: *Scope, primitive: *zir.Inst.Primitive) InnerError!*Inst {
1345fn zirPrimitive(mod: *Module, scope: *Scope, primitive: *zir.Inst.Primitive) InnerError!*Inst {
12811346 const tracy = trace(@src());
12821347 defer tracy.end();
12831348 return mod.constInst(scope, primitive.base.src, primitive.positionals.tag.toTypedValue());
12841349}
12851350
1286fn analyzeInstAs(mod: *Module, scope: *Scope, as: *zir.Inst.BinOp) InnerError!*Inst {
1351fn zirAs(mod: *Module, scope: *Scope, as: *zir.Inst.BinOp) InnerError!*Inst {
12871352 const tracy = trace(@src());
12881353 defer tracy.end();
12891354 const dest_type = try resolveType(mod, scope, as.positionals.lhs);
......@@ -1291,7 +1356,7 @@ fn analyzeInstAs(mod: *Module, scope: *Scope, as: *zir.Inst.BinOp) InnerError!*I
12911356 return mod.coerce(scope, dest_type, new_inst);
12921357}
12931358
1294fn analyzeInstPtrToInt(mod: *Module, scope: *Scope, ptrtoint: *zir.Inst.UnOp) InnerError!*Inst {
1359fn zirPtrtoint(mod: *Module, scope: *Scope, ptrtoint: *zir.Inst.UnOp) InnerError!*Inst {
12951360 const tracy = trace(@src());
12961361 defer tracy.end();
12971362 const ptr = try resolveInst(mod, scope, ptrtoint.positionals.operand);
......@@ -1304,7 +1369,7 @@ fn analyzeInstPtrToInt(mod: *Module, scope: *Scope, ptrtoint: *zir.Inst.UnOp) In
13041369 return mod.addUnOp(b, ptrtoint.base.src, ty, .ptrtoint, ptr);
13051370}
13061371
1307fn fieldVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst {
1372fn zirFieldVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst {
13081373 const tracy = trace(@src());
13091374 defer tracy.end();
13101375
......@@ -1315,7 +1380,7 @@ fn fieldVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst
13151380 return mod.analyzeDeref(scope, inst.base.src, result_ptr, result_ptr.src);
13161381}
13171382
1318fn fieldPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst {
1383fn zirFieldPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst {
13191384 const tracy = trace(@src());
13201385 defer tracy.end();
13211386
......@@ -1324,7 +1389,7 @@ fn fieldPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Field) InnerError!*Inst
13241389 return mod.namedFieldPtr(scope, inst.base.src, object_ptr, field_name, inst.base.src);
13251390}
13261391
1327fn fieldValNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerError!*Inst {
1392fn zirFieldValNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerError!*Inst {
13281393 const tracy = trace(@src());
13291394 defer tracy.end();
13301395
......@@ -1336,7 +1401,7 @@ fn fieldValNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerE
13361401 return mod.analyzeDeref(scope, inst.base.src, result_ptr, result_ptr.src);
13371402}
13381403
1339fn fieldPtrNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerError!*Inst {
1404fn zirFieldPtrNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerError!*Inst {
13401405 const tracy = trace(@src());
13411406 defer tracy.end();
13421407
......@@ -1346,7 +1411,7 @@ fn fieldPtrNamed(mod: *Module, scope: *Scope, inst: *zir.Inst.FieldNamed) InnerE
13461411 return mod.namedFieldPtr(scope, inst.base.src, object_ptr, field_name, fsrc);
13471412}
13481413
1349fn analyzeInstIntCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1414fn zirIntcast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
13501415 const tracy = trace(@src());
13511416 defer tracy.end();
13521417 const dest_type = try resolveType(mod, scope, inst.positionals.lhs);
......@@ -1384,7 +1449,7 @@ fn analyzeInstIntCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerE
13841449 return mod.fail(scope, inst.base.src, "TODO implement analyze widen or shorten int", .{});
13851450}
13861451
1387fn analyzeInstBitCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1452fn zirBitcast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
13881453 const tracy = trace(@src());
13891454 defer tracy.end();
13901455 const dest_type = try resolveType(mod, scope, inst.positionals.lhs);
......@@ -1392,7 +1457,7 @@ fn analyzeInstBitCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerE
13921457 return mod.bitcast(scope, dest_type, operand);
13931458}
13941459
1395fn analyzeInstFloatCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1460fn zirFloatcast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
13961461 const tracy = trace(@src());
13971462 defer tracy.end();
13981463 const dest_type = try resolveType(mod, scope, inst.positionals.lhs);
......@@ -1430,7 +1495,7 @@ fn analyzeInstFloatCast(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) Inne
14301495 return mod.fail(scope, inst.base.src, "TODO implement analyze widen or shorten float", .{});
14311496}
14321497
1433fn elemVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
1498fn zirElemVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
14341499 const tracy = trace(@src());
14351500 defer tracy.end();
14361501
......@@ -1441,7 +1506,7 @@ fn elemVal(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
14411506 return mod.analyzeDeref(scope, inst.base.src, result_ptr, result_ptr.src);
14421507}
14431508
1444fn elemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
1509fn zirElemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
14451510 const tracy = trace(@src());
14461511 defer tracy.end();
14471512
......@@ -1450,7 +1515,7 @@ fn elemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.Elem) InnerError!*Inst {
14501515 return mod.elemPtr(scope, inst.base.src, array_ptr, elem_index);
14511516}
14521517
1453fn analyzeInstSlice(mod: *Module, scope: *Scope, inst: *zir.Inst.Slice) InnerError!*Inst {
1518fn zirSlice(mod: *Module, scope: *Scope, inst: *zir.Inst.Slice) InnerError!*Inst {
14541519 const tracy = trace(@src());
14551520 defer tracy.end();
14561521 const array_ptr = try resolveInst(mod, scope, inst.positionals.array_ptr);
......@@ -1461,7 +1526,7 @@ fn analyzeInstSlice(mod: *Module, scope: *Scope, inst: *zir.Inst.Slice) InnerErr
14611526 return mod.analyzeSlice(scope, inst.base.src, array_ptr, start, end, sentinel);
14621527}
14631528
1464fn analyzeInstSliceStart(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1529fn zirSliceStart(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
14651530 const tracy = trace(@src());
14661531 defer tracy.end();
14671532 const array_ptr = try resolveInst(mod, scope, inst.positionals.lhs);
......@@ -1470,235 +1535,7 @@ fn analyzeInstSliceStart(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) Inn
14701535 return mod.analyzeSlice(scope, inst.base.src, array_ptr, start, null, null);
14711536}
14721537
1473fn analyzeInstSwitchRange(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1474 const tracy = trace(@src());
1475 defer tracy.end();
1476 const start = try resolveInst(mod, scope, inst.positionals.lhs);
1477 const end = try resolveInst(mod, scope, inst.positionals.rhs);
1478
1479 switch (start.ty.zigTypeTag()) {
1480 .Int, .ComptimeInt => {},
1481 else => return mod.constVoid(scope, inst.base.src),
1482 }
1483 switch (end.ty.zigTypeTag()) {
1484 .Int, .ComptimeInt => {},
1485 else => return mod.constVoid(scope, inst.base.src),
1486 }
1487 if (start.value()) |start_val| {
1488 if (end.value()) |end_val| {
1489 if (start_val.compare(.gte, end_val)) {
1490 return mod.fail(scope, inst.base.src, "range start value must be smaller than the end value", .{});
1491 }
1492 }
1493 }
1494 return mod.constVoid(scope, inst.base.src);
1495}
1496
1497fn analyzeInstSwitchBr(mod: *Module, scope: *Scope, inst: *zir.Inst.SwitchBr) InnerError!*Inst {
1498 const tracy = trace(@src());
1499 defer tracy.end();
1500 const target_ptr = try resolveInst(mod, scope, inst.positionals.target_ptr);
1501 const target = try mod.analyzeDeref(scope, inst.base.src, target_ptr, inst.positionals.target_ptr.src);
1502 try validateSwitch(mod, scope, target, inst);
1503
1504 if (try mod.resolveDefinedValue(scope, target)) |target_val| {
1505 for (inst.positionals.cases) |case| {
1506 const resolved = try resolveInst(mod, scope, case.item);
1507 const casted = try mod.coerce(scope, target.ty, resolved);
1508 const item = try mod.resolveConstValue(scope, casted);
1509
1510 if (target_val.eql(item)) {
1511 try analyzeBody(mod, scope.cast(Scope.Block).?, case.body);
1512 return mod.constNoReturn(scope, inst.base.src);
1513 }
1514 }
1515 try analyzeBody(mod, scope.cast(Scope.Block).?, inst.positionals.else_body);
1516 return mod.constNoReturn(scope, inst.base.src);
1517 }
1518
1519 if (inst.positionals.cases.len == 0) {
1520 // no cases just analyze else_branch
1521 try analyzeBody(mod, scope.cast(Scope.Block).?, inst.positionals.else_body);
1522 return mod.constNoReturn(scope, inst.base.src);
1523 }
1524
1525 const parent_block = try mod.requireRuntimeBlock(scope, inst.base.src);
1526 const cases = try parent_block.arena.alloc(Inst.SwitchBr.Case, inst.positionals.cases.len);
1527
1528 var case_block: Scope.Block = .{
1529 .parent = parent_block,
1530 .inst_table = parent_block.inst_table,
1531 .func = parent_block.func,
1532 .owner_decl = parent_block.owner_decl,
1533 .src_decl = parent_block.src_decl,
1534 .instructions = .{},
1535 .arena = parent_block.arena,
1536 .inlining = parent_block.inlining,
1537 .is_comptime = parent_block.is_comptime,
1538 .branch_quota = parent_block.branch_quota,
1539 };
1540 defer case_block.instructions.deinit(mod.gpa);
1541
1542 for (inst.positionals.cases) |case, i| {
1543 // Reset without freeing.
1544 case_block.instructions.items.len = 0;
1545
1546 const resolved = try resolveInst(mod, scope, case.item);
1547 const casted = try mod.coerce(scope, target.ty, resolved);
1548 const item = try mod.resolveConstValue(scope, casted);
1549
1550 try analyzeBody(mod, &case_block, case.body);
1551
1552 cases[i] = .{
1553 .item = item,
1554 .body = .{ .instructions = try parent_block.arena.dupe(*Inst, case_block.instructions.items) },
1555 };
1556 }
1557
1558 case_block.instructions.items.len = 0;
1559 try analyzeBody(mod, &case_block, inst.positionals.else_body);
1560
1561 const else_body: ir.Body = .{
1562 .instructions = try parent_block.arena.dupe(*Inst, case_block.instructions.items),
1563 };
1564
1565 return mod.addSwitchBr(parent_block, inst.base.src, target_ptr, cases, else_body);
1566}
1567
1568fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.SwitchBr) InnerError!void {
1569 // validate usage of '_' prongs
1570 if (inst.kw_args.special_prong == .underscore and target.ty.zigTypeTag() != .Enum) {
1571 return mod.fail(scope, inst.base.src, "'_' prong only allowed when switching on non-exhaustive enums", .{});
1572 // TODO notes "'_' prong here" inst.positionals.cases[last].src
1573 }
1574
1575 // check that target type supports ranges
1576 if (inst.kw_args.range) |range_inst| {
1577 switch (target.ty.zigTypeTag()) {
1578 .Int, .ComptimeInt => {},
1579 else => {
1580 return mod.fail(scope, target.src, "ranges not allowed when switching on type {}", .{target.ty});
1581 // TODO notes "range used here" range_inst.src
1582 },
1583 }
1584 }
1585
1586 // validate for duplicate items/missing else prong
1587 switch (target.ty.zigTypeTag()) {
1588 .Enum => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Enum", .{}),
1589 .ErrorSet => return mod.fail(scope, inst.base.src, "TODO validateSwitch .ErrorSet", .{}),
1590 .Union => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Union", .{}),
1591 .Int, .ComptimeInt => {
1592 var range_set = @import("RangeSet.zig").init(mod.gpa);
1593 defer range_set.deinit();
1594
1595 for (inst.positionals.items) |item| {
1596 const maybe_src = if (item.castTag(.switch_range)) |range| blk: {
1597 const start_resolved = try resolveInst(mod, scope, range.positionals.lhs);
1598 const start_casted = try mod.coerce(scope, target.ty, start_resolved);
1599 const end_resolved = try resolveInst(mod, scope, range.positionals.rhs);
1600 const end_casted = try mod.coerce(scope, target.ty, end_resolved);
1601
1602 break :blk try range_set.add(
1603 try mod.resolveConstValue(scope, start_casted),
1604 try mod.resolveConstValue(scope, end_casted),
1605 item.src,
1606 );
1607 } else blk: {
1608 const resolved = try resolveInst(mod, scope, item);
1609 const casted = try mod.coerce(scope, target.ty, resolved);
1610 const value = try mod.resolveConstValue(scope, casted);
1611 break :blk try range_set.add(value, value, item.src);
1612 };
1613
1614 if (maybe_src) |previous_src| {
1615 return mod.fail(scope, item.src, "duplicate switch value", .{});
1616 // TODO notes "previous value is here" previous_src
1617 }
1618 }
1619
1620 if (target.ty.zigTypeTag() == .Int) {
1621 var arena = std.heap.ArenaAllocator.init(mod.gpa);
1622 defer arena.deinit();
1623
1624 const start = try target.ty.minInt(&arena, mod.getTarget());
1625 const end = try target.ty.maxInt(&arena, mod.getTarget());
1626 if (try range_set.spans(start, end)) {
1627 if (inst.kw_args.special_prong == .@"else") {
1628 return mod.fail(scope, inst.base.src, "unreachable else prong, all cases already handled", .{});
1629 }
1630 return;
1631 }
1632 }
1633
1634 if (inst.kw_args.special_prong != .@"else") {
1635 return mod.fail(scope, inst.base.src, "switch must handle all possibilities", .{});
1636 }
1637 },
1638 .Bool => {
1639 var true_count: u8 = 0;
1640 var false_count: u8 = 0;
1641 for (inst.positionals.items) |item| {
1642 const resolved = try resolveInst(mod, scope, item);
1643 const casted = try mod.coerce(scope, Type.initTag(.bool), resolved);
1644 if ((try mod.resolveConstValue(scope, casted)).toBool()) {
1645 true_count += 1;
1646 } else {
1647 false_count += 1;
1648 }
1649
1650 if (true_count + false_count > 2) {
1651 return mod.fail(scope, item.src, "duplicate switch value", .{});
1652 }
1653 }
1654 if ((true_count + false_count < 2) and inst.kw_args.special_prong != .@"else") {
1655 return mod.fail(scope, inst.base.src, "switch must handle all possibilities", .{});
1656 }
1657 if ((true_count + false_count == 2) and inst.kw_args.special_prong == .@"else") {
1658 return mod.fail(scope, inst.base.src, "unreachable else prong, all cases already handled", .{});
1659 }
1660 },
1661 .EnumLiteral, .Void, .Fn, .Pointer, .Type => {
1662 if (inst.kw_args.special_prong != .@"else") {
1663 return mod.fail(scope, inst.base.src, "else prong required when switching on type '{}'", .{target.ty});
1664 }
1665
1666 var seen_values = std.HashMap(Value, usize, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage).init(mod.gpa);
1667 defer seen_values.deinit();
1668
1669 for (inst.positionals.items) |item| {
1670 const resolved = try resolveInst(mod, scope, item);
1671 const casted = try mod.coerce(scope, target.ty, resolved);
1672 const val = try mod.resolveConstValue(scope, casted);
1673
1674 if (try seen_values.fetchPut(val, item.src)) |prev| {
1675 return mod.fail(scope, item.src, "duplicate switch value", .{});
1676 // TODO notes "previous value here" prev.value
1677 }
1678 }
1679 },
1680
1681 .ErrorUnion,
1682 .NoReturn,
1683 .Array,
1684 .Struct,
1685 .Undefined,
1686 .Null,
1687 .Optional,
1688 .BoundFn,
1689 .Opaque,
1690 .Vector,
1691 .Frame,
1692 .AnyFrame,
1693 .ComptimeFloat,
1694 .Float,
1695 => {
1696 return mod.fail(scope, target.src, "invalid switch target type '{}'", .{target.ty});
1697 },
1698 }
1699}
1700
1701fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1538fn zirImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
17021539 const tracy = trace(@src());
17031540 defer tracy.end();
17041541 const operand = try resolveConstString(mod, scope, inst.positionals.operand);
......@@ -1718,19 +1555,19 @@ fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErr
17181555 return mod.constType(scope, inst.base.src, file_scope.root_container.ty);
17191556}
17201557
1721fn analyzeInstShl(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1558fn zirShl(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
17221559 const tracy = trace(@src());
17231560 defer tracy.end();
1724 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstShl", .{});
1561 return mod.fail(scope, inst.base.src, "TODO implement zirShl", .{});
17251562}
17261563
1727fn analyzeInstShr(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1564fn zirShr(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
17281565 const tracy = trace(@src());
17291566 defer tracy.end();
1730 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstShr", .{});
1567 return mod.fail(scope, inst.base.src, "TODO implement zirShr", .{});
17311568}
17321569
1733fn analyzeInstBitwise(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1570fn zirBitwise(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
17341571 const tracy = trace(@src());
17351572 defer tracy.end();
17361573
......@@ -1784,8 +1621,8 @@ fn analyzeInstBitwise(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerE
17841621
17851622 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
17861623 const ir_tag = switch (inst.base.tag) {
1787 .bitand => Inst.Tag.bitand,
1788 .bitor => Inst.Tag.bitor,
1624 .bit_and => Inst.Tag.bit_and,
1625 .bit_or => Inst.Tag.bit_or,
17891626 .xor => Inst.Tag.xor,
17901627 else => unreachable,
17911628 };
......@@ -1793,25 +1630,25 @@ fn analyzeInstBitwise(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerE
17931630 return mod.addBinOp(b, inst.base.src, scalar_type, ir_tag, casted_lhs, casted_rhs);
17941631}
17951632
1796fn analyzeInstBitNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1633fn zirBitNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
17971634 const tracy = trace(@src());
17981635 defer tracy.end();
1799 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstBitNot", .{});
1636 return mod.fail(scope, inst.base.src, "TODO implement zirBitNot", .{});
18001637}
18011638
1802fn analyzeInstArrayCat(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1639fn zirArrayCat(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
18031640 const tracy = trace(@src());
18041641 defer tracy.end();
1805 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstArrayCat", .{});
1642 return mod.fail(scope, inst.base.src, "TODO implement zirArrayCat", .{});
18061643}
18071644
1808fn analyzeInstArrayMul(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1645fn zirArrayMul(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
18091646 const tracy = trace(@src());
18101647 defer tracy.end();
1811 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstArrayMul", .{});
1648 return mod.fail(scope, inst.base.src, "TODO implement zirArrayMul", .{});
18121649}
18131650
1814fn analyzeInstArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1651fn zirArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
18151652 const tracy = trace(@src());
18161653 defer tracy.end();
18171654
......@@ -1912,14 +1749,14 @@ fn analyzeInstComptimeOp(mod: *Module, scope: *Scope, res_type: Type, inst: *zir
19121749 });
19131750}
19141751
1915fn analyzeInstDeref(mod: *Module, scope: *Scope, deref: *zir.Inst.UnOp) InnerError!*Inst {
1752fn zirDeref(mod: *Module, scope: *Scope, deref: *zir.Inst.UnOp) InnerError!*Inst {
19161753 const tracy = trace(@src());
19171754 defer tracy.end();
19181755 const ptr = try resolveInst(mod, scope, deref.positionals.operand);
19191756 return mod.analyzeDeref(scope, deref.base.src, ptr, deref.positionals.operand.src);
19201757}
19211758
1922fn analyzeInstAsm(mod: *Module, scope: *Scope, assembly: *zir.Inst.Asm) InnerError!*Inst {
1759fn zirAsm(mod: *Module, scope: *Scope, assembly: *zir.Inst.Asm) InnerError!*Inst {
19231760 const tracy = trace(@src());
19241761 defer tracy.end();
19251762 const return_type = try resolveType(mod, scope, assembly.positionals.return_type);
......@@ -1960,7 +1797,7 @@ fn analyzeInstAsm(mod: *Module, scope: *Scope, assembly: *zir.Inst.Asm) InnerErr
19601797 return &inst.base;
19611798}
19621799
1963fn analyzeInstCmp(
1800fn zirCmp(
19641801 mod: *Module,
19651802 scope: *Scope,
19661803 inst: *zir.Inst.BinOp,
......@@ -2018,14 +1855,14 @@ fn analyzeInstCmp(
20181855 return mod.fail(scope, inst.base.src, "TODO implement more cmp analysis", .{});
20191856}
20201857
2021fn analyzeInstTypeOf(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1858fn zirTypeof(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
20221859 const tracy = trace(@src());
20231860 defer tracy.end();
20241861 const operand = try resolveInst(mod, scope, inst.positionals.operand);
20251862 return mod.constType(scope, inst.base.src, operand.ty);
20261863}
20271864
2028fn analyzeInstTypeOfPeer(mod: *Module, scope: *Scope, inst: *zir.Inst.TypeOfPeer) InnerError!*Inst {
1865fn zirTypeofPeer(mod: *Module, scope: *Scope, inst: *zir.Inst.TypeOfPeer) InnerError!*Inst {
20291866 const tracy = trace(@src());
20301867 defer tracy.end();
20311868 var insts_to_res = try mod.gpa.alloc(*ir.Inst, inst.positionals.items.len);
......@@ -2037,7 +1874,7 @@ fn analyzeInstTypeOfPeer(mod: *Module, scope: *Scope, inst: *zir.Inst.TypeOfPeer
20371874 return mod.constType(scope, inst.base.src, pt_res);
20381875}
20391876
2040fn analyzeInstBoolNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1877fn zirBoolNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
20411878 const tracy = trace(@src());
20421879 defer tracy.end();
20431880 const uncasted_operand = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -2050,7 +1887,7 @@ fn analyzeInstBoolNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerEr
20501887 return mod.addUnOp(b, inst.base.src, bool_type, .not, operand);
20511888}
20521889
2053fn analyzeInstBoolOp(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1890fn zirBoolOp(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
20541891 const tracy = trace(@src());
20551892 defer tracy.end();
20561893 const bool_type = Type.initTag(.bool);
......@@ -2059,7 +1896,7 @@ fn analyzeInstBoolOp(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerEr
20591896 const uncasted_rhs = try resolveInst(mod, scope, inst.positionals.rhs);
20601897 const rhs = try mod.coerce(scope, bool_type, uncasted_rhs);
20611898
2062 const is_bool_or = inst.base.tag == .boolor;
1899 const is_bool_or = inst.base.tag == .bool_or;
20631900
20641901 if (lhs.value()) |lhs_val| {
20651902 if (rhs.value()) |rhs_val| {
......@@ -2071,17 +1908,17 @@ fn analyzeInstBoolOp(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerEr
20711908 }
20721909 }
20731910 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
2074 return mod.addBinOp(b, inst.base.src, bool_type, if (is_bool_or) .boolor else .booland, lhs, rhs);
1911 return mod.addBinOp(b, inst.base.src, bool_type, if (is_bool_or) .bool_or else .bool_and, lhs, rhs);
20751912}
20761913
2077fn isNull(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, invert_logic: bool) InnerError!*Inst {
1914fn zirIsNull(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, invert_logic: bool) InnerError!*Inst {
20781915 const tracy = trace(@src());
20791916 defer tracy.end();
20801917 const operand = try resolveInst(mod, scope, inst.positionals.operand);
20811918 return mod.analyzeIsNull(scope, inst.base.src, operand, invert_logic);
20821919}
20831920
2084fn isNullPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, invert_logic: bool) InnerError!*Inst {
1921fn zirIsNullPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, invert_logic: bool) InnerError!*Inst {
20851922 const tracy = trace(@src());
20861923 defer tracy.end();
20871924 const ptr = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -2089,14 +1926,14 @@ fn isNullPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, invert_logic: bo
20891926 return mod.analyzeIsNull(scope, inst.base.src, loaded, invert_logic);
20901927}
20911928
2092fn isErr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1929fn zirIsErr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
20931930 const tracy = trace(@src());
20941931 defer tracy.end();
20951932 const operand = try resolveInst(mod, scope, inst.positionals.operand);
20961933 return mod.analyzeIsErr(scope, inst.base.src, operand);
20971934}
20981935
2099fn isErrPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1936fn zirIsErrPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
21001937 const tracy = trace(@src());
21011938 defer tracy.end();
21021939 const ptr = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -2104,7 +1941,7 @@ fn isErrPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst
21041941 return mod.analyzeIsErr(scope, inst.base.src, loaded);
21051942}
21061943
2107fn analyzeInstCondBr(mod: *Module, scope: *Scope, inst: *zir.Inst.CondBr) InnerError!*Inst {
1944fn zirCondbr(mod: *Module, scope: *Scope, inst: *zir.Inst.CondBr) InnerError!*Inst {
21081945 const tracy = trace(@src());
21091946 defer tracy.end();
21101947 const uncasted_cond = try resolveInst(mod, scope, inst.positionals.condition);
......@@ -2153,7 +1990,7 @@ fn analyzeInstCondBr(mod: *Module, scope: *Scope, inst: *zir.Inst.CondBr) InnerE
21531990 return mod.addCondBr(parent_block, inst.base.src, cond, then_body, else_body);
21541991}
21551992
2156fn analyzeInstUnreachable(
1993fn zirUnreachable(
21571994 mod: *Module,
21581995 scope: *Scope,
21591996 unreach: *zir.Inst.NoOp,
......@@ -2170,7 +2007,7 @@ fn analyzeInstUnreachable(
21702007 }
21712008}
21722009
2173fn analyzeInstRet(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
2010fn zirReturn(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
21742011 const tracy = trace(@src());
21752012 defer tracy.end();
21762013 const operand = try resolveInst(mod, scope, inst.positionals.operand);
......@@ -2179,13 +2016,14 @@ fn analyzeInstRet(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!
21792016 if (b.inlining) |inlining| {
21802017 // We are inlining a function call; rewrite the `ret` as a `break`.
21812018 try inlining.merges.results.append(mod.gpa, operand);
2182 return mod.addBr(b, inst.base.src, inlining.merges.block_inst, operand);
2019 const br = try mod.addBr(b, inst.base.src, inlining.merges.block_inst, operand);
2020 return &br.base;
21832021 }
21842022
21852023 return mod.addUnOp(b, inst.base.src, Type.initTag(.noreturn), .ret, operand);
21862024}
21872025
2188fn analyzeInstRetVoid(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
2026fn zirReturnVoid(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
21892027 const tracy = trace(@src());
21902028 defer tracy.end();
21912029 const b = try mod.requireFunctionBlock(scope, inst.base.src);
......@@ -2193,7 +2031,8 @@ fn analyzeInstRetVoid(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerEr
21932031 // We are inlining a function call; rewrite the `retvoid` as a `breakvoid`.
21942032 const void_inst = try mod.constVoid(scope, inst.base.src);
21952033 try inlining.merges.results.append(mod.gpa, void_inst);
2196 return mod.addBr(b, inst.base.src, inlining.merges.block_inst, void_inst);
2034 const br = try mod.addBr(b, inst.base.src, inlining.merges.block_inst, void_inst);
2035 return &br.base;
21972036 }
21982037
21992038 if (b.func) |func| {
......@@ -2216,27 +2055,7 @@ fn floatOpAllowed(tag: zir.Inst.Tag) bool {
22162055 };
22172056}
22182057
2219fn analyzeBreak(
2220 mod: *Module,
2221 scope: *Scope,
2222 src: usize,
2223 zir_block: *zir.Inst.Block,
2224 operand: *Inst,
2225) InnerError!*Inst {
2226 var opt_block = scope.cast(Scope.Block);
2227 while (opt_block) |block| {
2228 if (block.label) |*label| {
2229 if (label.zir_block == zir_block) {
2230 try label.merges.results.append(mod.gpa, operand);
2231 const b = try mod.requireFunctionBlock(scope, src);
2232 return mod.addBr(b, src, label.merges.block_inst, operand);
2233 }
2234 }
2235 opt_block = block.parent;
2236 } else unreachable;
2237}
2238
2239fn analyzeInstSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, mutable: bool, size: std.builtin.TypeInfo.Pointer.Size) InnerError!*Inst {
2058fn zirSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, mutable: bool, size: std.builtin.TypeInfo.Pointer.Size) InnerError!*Inst {
22402059 const tracy = trace(@src());
22412060 defer tracy.end();
22422061 const elem_type = try resolveType(mod, scope, inst.positionals.operand);
......@@ -2244,7 +2063,7 @@ fn analyzeInstSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, m
22442063 return mod.constType(scope, inst.base.src, ty);
22452064}
22462065
2247fn analyzeInstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.PtrType) InnerError!*Inst {
2066fn zirPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.PtrType) InnerError!*Inst {
22482067 const tracy = trace(@src());
22492068 defer tracy.end();
22502069 // TODO lazy values
test/stage2/test.zig-37
......@@ -962,43 +962,6 @@ pub fn addCases(ctx: *TestContext) !void {
962962 ,
963963 "hello\nhello\nhello\nhello\nhello\n",
964964 );
965
966 // comptime switch
967
968 // Basic for loop
969 case.addCompareOutput(
970 \\pub export fn _start() noreturn {
971 \\ assert(foo() == 1);
972 \\ exit();
973 \\}
974 \\
975 \\fn foo() u32 {
976 \\ const a: comptime_int = 1;
977 \\ var b: u32 = 0;
978 \\ switch (a) {
979 \\ 1 => b = 1,
980 \\ 2 => b = 2,
981 \\ else => unreachable,
982 \\ }
983 \\ return b;
984 \\}
985 \\
986 \\pub fn assert(ok: bool) void {
987 \\ if (!ok) unreachable; // assertion failure
988 \\}
989 \\
990 \\fn exit() noreturn {
991 \\ asm volatile ("syscall"
992 \\ :
993 \\ : [number] "{rax}" (231),
994 \\ [arg1] "{rdi}" (0)
995 \\ : "rcx", "r11", "memory"
996 \\ );
997 \\ unreachable;
998 \\}
999 ,
1000 "",
1001 );
1002965 }
1003966
1004967 {