[valueblocks] Use branch terminal for if/for test blocks
Updates `while` and `for` to use the new branch terminal as their test block terminal (IfTerminal is now only meant for if statements).
Joe Savona committed
Jan 25, 2023 at 17:05 UTC
4ab8cc6ae90c04e1eba6d12f427b26c8b8952fe6
2 files changed
+18
-27
compiler/forget/src/HIR/BuildHIR.ts
+16
-25
@@ -13,6 +13,7 @@ import { Err, Ok, Result } from "../lib/Result";
13
import { assertExhaustive } from "../Utils/utils";
14
import {
15
BlockId,
16
+ BranchTerminal,
17
Case,
18
Effect,
19
GeneratedSource,
@@ -411,14 +412,13 @@ function lowerStatement(
412
} else {
413
builder.terminateWithContinuation(
414
{
414
- kind: "if",
415
+ kind: "branch",
416
test: lowerExpressionToPlace(
417
builder,
418
test as NodePath<t.Expression>
419
),
420
consequent: bodyBlock,
421
alternate: continuationBlock.id,
421
- fallthrough: continuationBlock.id,
422
id: makeInstructionId(0),
423
},
424
continuationBlock
@@ -489,37 +489,28 @@ function lowerStatement(
489
* The code leading up to the loop must jump to the conditional block,
490
* to evaluate whether to enter the loop or bypass to the continuation.
491
*/
492
- const loc = stmt.node.loc;
493
- if (loc == null) {
494
- builder.pushError({
495
- reason: `(BuildHIR::lowerStatement) Expected WhileStatement to have a location, got ${loc}`,
496
- severity: ErrorSeverity.InvalidInput,
497
- nodePath: stmt,
498
- });
499
- } else {
500
- builder.terminateWithContinuation(
501
- {
502
- kind: "while",
503
- loc,
504
- test: conditionalBlock.id,
505
- loop: loopBlock,
506
- fallthrough: continuationBlock.id,
507
- id: makeInstructionId(0),
508
- },
509
- conditionalBlock
510
- );
511
- }
492
+ const loc = stmt.node.loc ?? GeneratedSource;
493
+ builder.terminateWithContinuation(
494
+ {
495
+ kind: "while",
496
+ loc,
497
+ test: conditionalBlock.id,
498
+ loop: loopBlock,
499
+ fallthrough: continuationBlock.id,
500
+ id: makeInstructionId(0),
501
+ },
502
+ conditionalBlock
503
+ );
504
/**
505
* The conditional block is empty and exists solely as conditional for
506
* (re)entering or exiting the loop
507
*/
508
const test = lowerExpressionToPlace(builder, stmt.get("test"));
517
- const terminal: IfTerminal = {
518
- kind: "if",
509
+ const terminal: BranchTerminal = {
510
+ kind: "branch",
511
test,
512
consequent: loopBlock,
513
alternate: continuationBlock.id,
522
- fallthrough: continuationBlock.id,
514
id: makeInstructionId(0),
515
};
516
// Complete the conditional and continue with code after the loop
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
+2
-2
@@ -247,7 +247,7 @@ class Driver {
247
const testBlock = this.cx.ir.blocks.get(terminal.test)!;
248
const testTerminal = testBlock.terminal;
249
invariant(
250
- testTerminal.kind === "if",
250
+ testTerminal.kind === "branch",
251
"Expected while loop test block to end in an if"
252
);
253
const testValue = this.visitValueBlock(blockValue, testBlock, {
@@ -314,7 +314,7 @@ class Driver {
314
const testBlock = this.cx.ir.blocks.get(terminal.test)!;
315
const testTerminal = testBlock.terminal;
316
invariant(
317
- testTerminal.kind === "if",
317
+ testTerminal.kind === "branch",
318
"Expected for loop test block to end in an if"
319
);
320
const testValue = this.visitValueBlock(blockValue, testBlock, {