authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-13 11:03:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-13 20:32:32-07:00
logfc402bdbbbb5be2a538f0469f3f213264e7fcc59
tree4f3b85c10d1eef73deacfbe961c2c086ca1a65ed
parenta9590f3bf863493ed9b15ad8ab0d4fa7991805a5

stage2: zir_sema for loops

Also remove the "repeat" instruction and make it implied to be at the end of a Loop body.

3 files changed, 38 insertions(+), 27 deletions(-)

src-self-hosted/astgen.zig+5-6
......@@ -531,13 +531,12 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
531531 const cond_block = try addZIRInstBlock(mod, &loop_scope.base, while_src, .{
532532 .instructions = try loop_scope.arena.dupe(*zir.Inst, continue_scope.instructions.items),
533533 });
534 // TODO avoid emitting the continue expr when there
535 // are no jumps to it. This happens when the last statement of a while body is noreturn
536 // and there are no `continue` statements.
537 // The "repeat" at the end of a loop body is implied.
534538 if (while_node.continue_expr) |cont_expr| {
535 const cont_expr_result = try expr(mod, &loop_scope.base, .{ .ty = void_type }, cont_expr);
536 if (!cont_expr_result.tag.isNoReturn()) {
537 _ = try addZIRNoOp(mod, &loop_scope.base, while_src, .repeat);
538 }
539 } else {
540 _ = try addZIRNoOp(mod, &loop_scope.base, while_src, .repeat);
539 _ = try expr(mod, &loop_scope.base, .{ .ty = void_type }, cont_expr);
541540 }
542541 const loop = try addZIRInstLoop(mod, &expr_scope.base, while_src, .{
543542 .instructions = try expr_scope.arena.dupe(*zir.Inst, loop_scope.instructions.items),
src-self-hosted/zir.zig+2-15
......@@ -151,7 +151,8 @@ pub const Inst = struct {
151151 isnonnull,
152152 /// Return a boolean true if an optional is null. `x == null`
153153 isnull,
154 /// A labeled block of code that loops forever.
154 /// A labeled block of code that loops forever. At the end of the body it is implied
155 /// to repeat; no explicit "repeat" instruction terminates loop bodies.
155156 loop,
156157 /// Ambiguously remainder division or modulus. If the computation would possibly have
157158 /// a different value depending on whether the operation is remainder division or modulus,
......@@ -175,8 +176,6 @@ pub const Inst = struct {
175176 /// the memory location is in the stack frame, local to the scope containing the
176177 /// instruction.
177178 ref,
178 /// Sends control flow back to the loop block operand.
179 repeat,
180179 /// Obtains a pointer to the return value.
181180 ret_ptr,
182181 /// Obtains the return type of the in-scope function.
......@@ -294,7 +293,6 @@ pub const Inst = struct {
294293 .compileerror => CompileError,
295294 .loop => Loop,
296295 .@"const" => Const,
297 .repeat => Repeat,
298296 .str => Str,
299297 .int => Int,
300298 .inttype => IntType,
......@@ -390,7 +388,6 @@ pub const Inst = struct {
390388 .breakvoid,
391389 .condbr,
392390 .compileerror,
393 .repeat,
394391 .@"return",
395392 .returnvoid,
396393 .unreach_nocheck,
......@@ -587,16 +584,6 @@ pub const Inst = struct {
587584 kw_args: struct {},
588585 };
589586
590 pub const Repeat = struct {
591 pub const base_tag = Tag.repeat;
592 base: Inst,
593
594 positionals: struct {
595 loop: *Loop,
596 },
597 kw_args: struct {},
598 };
599
600587 pub const Str = struct {
601588 pub const base_tag = Tag.str;
602589 base: Inst,
src-self-hosted/zir_sema.zig+31-6
......@@ -61,7 +61,6 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
6161 },
6262 .inttype => return analyzeInstIntType(mod, scope, old_inst.castTag(.inttype).?),
6363 .loop => return analyzeInstLoop(mod, scope, old_inst.castTag(.loop).?),
64 .repeat => return analyzeInstRepeat(mod, scope, old_inst.castTag(.repeat).?),
6564 .param_type => return analyzeInstParamType(mod, scope, old_inst.castTag(.param_type).?),
6665 .ptrtoint => return analyzeInstPtrToInt(mod, scope, old_inst.castTag(.ptrtoint).?),
6766 .fieldptr => return analyzeInstFieldPtr(mod, scope, old_inst.castTag(.fieldptr).?),
......@@ -440,12 +439,38 @@ fn analyzeInstArg(mod: *Module, scope: *Scope, inst: *zir.Inst.Arg) InnerError!*
440439 return mod.addArg(b, inst.base.src, param_type, name);
441440}
442441
443fn analyzeInstRepeat(mod: *Module, scope: *Scope, inst: *zir.Inst.Repeat) InnerError!*Inst {
444 return mod.fail(scope, inst.base.src, "TODO analyze .repeat ZIR", .{});
445}
446
447442fn analyzeInstLoop(mod: *Module, scope: *Scope, inst: *zir.Inst.Loop) InnerError!*Inst {
448 return mod.fail(scope, inst.base.src, "TODO analyze .loop ZIR", .{});
443 const parent_block = scope.cast(Scope.Block).?;
444
445 // Reserve space for a Loop instruction so that generated Break instructions can
446 // point to it, even if it doesn't end up getting used because the code ends up being
447 // comptime evaluated.
448 const loop_inst = try parent_block.arena.create(Inst.Loop);
449 loop_inst.* = .{
450 .base = .{
451 .tag = Inst.Loop.base_tag,
452 .ty = Type.initTag(.noreturn),
453 .src = inst.base.src,
454 },
455 .body = undefined,
456 };
457
458 var child_block: Scope.Block = .{
459 .parent = parent_block,
460 .func = parent_block.func,
461 .decl = parent_block.decl,
462 .instructions = .{},
463 .arena = parent_block.arena,
464 };
465 defer child_block.instructions.deinit(mod.gpa);
466
467 try analyzeBody(mod, &child_block.base, inst.positionals.body);
468
469 // Loop repetition is implied so the last instruction may or may not be a noreturn instruction.
470
471 try parent_block.instructions.append(mod.gpa, &loop_inst.base);
472 loop_inst.body = .{ .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items) };
473 return &loop_inst.base;
449474}
450475
451476fn analyzeInstBlock(mod: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerError!*Inst {