[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).
This commit is contained in:
Joe Savona
2023-01-25 17:05:23 -05:00
parent f88fa006e7
commit 4ab8cc6ae9
2 changed files with 18 additions and 27 deletions
+16 -25
View File
@@ -13,6 +13,7 @@ import { Err, Ok, Result } from "../lib/Result";
import { assertExhaustive } from "../Utils/utils";
import {
BlockId,
BranchTerminal,
Case,
Effect,
GeneratedSource,
@@ -411,14 +412,13 @@ function lowerStatement(
} else {
builder.terminateWithContinuation(
{
kind: "if",
kind: "branch",
test: lowerExpressionToPlace(
builder,
test as NodePath<t.Expression>
),
consequent: bodyBlock,
alternate: continuationBlock.id,
fallthrough: continuationBlock.id,
id: makeInstructionId(0),
},
continuationBlock
@@ -489,37 +489,28 @@ function lowerStatement(
* The code leading up to the loop must jump to the conditional block,
* to evaluate whether to enter the loop or bypass to the continuation.
*/
const loc = stmt.node.loc;
if (loc == null) {
builder.pushError({
reason: `(BuildHIR::lowerStatement) Expected WhileStatement to have a location, got ${loc}`,
severity: ErrorSeverity.InvalidInput,
nodePath: stmt,
});
} else {
builder.terminateWithContinuation(
{
kind: "while",
loc,
test: conditionalBlock.id,
loop: loopBlock,
fallthrough: continuationBlock.id,
id: makeInstructionId(0),
},
conditionalBlock
);
}
const loc = stmt.node.loc ?? GeneratedSource;
builder.terminateWithContinuation(
{
kind: "while",
loc,
test: conditionalBlock.id,
loop: loopBlock,
fallthrough: continuationBlock.id,
id: makeInstructionId(0),
},
conditionalBlock
);
/**
* The conditional block is empty and exists solely as conditional for
* (re)entering or exiting the loop
*/
const test = lowerExpressionToPlace(builder, stmt.get("test"));
const terminal: IfTerminal = {
kind: "if",
const terminal: BranchTerminal = {
kind: "branch",
test,
consequent: loopBlock,
alternate: continuationBlock.id,
fallthrough: continuationBlock.id,
id: makeInstructionId(0),
};
// Complete the conditional and continue with code after the loop
@@ -247,7 +247,7 @@ class Driver {
const testBlock = this.cx.ir.blocks.get(terminal.test)!;
const testTerminal = testBlock.terminal;
invariant(
testTerminal.kind === "if",
testTerminal.kind === "branch",
"Expected while loop test block to end in an if"
);
const testValue = this.visitValueBlock(blockValue, testBlock, {
@@ -314,7 +314,7 @@ class Driver {
const testBlock = this.cx.ir.blocks.get(terminal.test)!;
const testTerminal = testBlock.terminal;
invariant(
testTerminal.kind === "if",
testTerminal.kind === "branch",
"Expected for loop test block to end in an if"
);
const testValue = this.visitValueBlock(blockValue, testBlock, {