Skip to content

Commit

Permalink
Scaffolding for LogicalTerminal
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Jan 10, 2023
1 parent f9ecc96 commit a4fc975
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/forget/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export type Terminal =
| IfTerminal
| SwitchTerminal
| ForTerminal
| WhileTerminal;
| WhileTerminal
| LogicalTerminal;

/**
* Terminal nodes allowed for a value block
Expand Down Expand Up @@ -226,6 +227,14 @@ export type ForTerminal = {
id: InstructionId;
};

export type LogicalTerminal = {
kind: "logical";
operator: t.LogicalExpression["operator"];
test: BlockId;
fallthrough: BlockId;
id: InstructionId;
};

/**
* Instructions generally represent expressions but with all nesting flattened away,
* such that all operands to each instruction are either primitive values OR are
Expand Down
4 changes: 4 additions & 0 deletions compiler/forget/src/HIR/HIRBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ export function reversePostorderBlocks(func: HIR): void {
}
break;
}
case "logical": {
visit(terminal.test);
break;
}
case "while": {
visit(terminal.test);
break;
Expand Down
4 changes: 4 additions & 0 deletions compiler/forget/src/HIR/HIRTreeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import invariant from "invariant";
import todo from "../Utils/todo";
import { assertExhaustive } from "../Utils/utils";
import {
BasicBlock,
Expand Down Expand Up @@ -445,6 +446,9 @@ class Driver<
}
break;
}
case "logical": {
todo("Implement tree visitor for logical terminal");
}
case "goto": {
this.visitor.visitTerminalId(terminal.id);
switch (terminal.variant) {
Expand Down
5 changes: 5 additions & 0 deletions compiler/forget/src/HIR/PrintHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function printMixedHIR(
}
switch (value.kind) {
case "if":
case "logical":
case "return":
case "switch":
case "throw":
Expand Down Expand Up @@ -135,6 +136,10 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
}`;
break;
}
case "logical": {
value = `[${terminal.id}] Logical ${terminal.operator} test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`;
break;
}
case "throw": {
value = `[${terminal.id}] Throw ${printPlace(terminal.value)}`;
break;
Expand Down
17 changes: 17 additions & 0 deletions compiler/forget/src/HIR/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ export function mapTerminalSuccessors(
id: makeInstructionId(0),
};
}
case "logical": {
const test = fn(terminal.test);
const fallthrough = fn(terminal.fallthrough);
return {
kind: "logical",
test,
fallthrough,
operator: terminal.operator,
id: makeInstructionId(0),
};
}
case "return": {
return {
kind: "return",
Expand Down Expand Up @@ -312,6 +323,10 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
}
break;
}
case "logical": {
yield terminal.test;
break;
}
case "return": {
break;
}
Expand Down Expand Up @@ -361,6 +376,7 @@ export function mapTerminalOperands(
}
break;
}
case "logical":
case "while":
case "for":
case "goto": {
Expand Down Expand Up @@ -399,6 +415,7 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
}
break;
}
case "logical":
case "while":
case "for":
case "goto": {
Expand Down
9 changes: 9 additions & 0 deletions compiler/forget/src/Utils/VisualizeHIRMermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ function printTerminalLabel(terminal: Terminal): string {
case "throw":
buffer.push(`Throw ${printPlace(terminal.value)}`);
break;
case "logical": {
buffer.push(`Logical ${terminal.operator}`);
break;
}
case "while":
buffer.push("While");
break;
Expand Down Expand Up @@ -123,6 +127,11 @@ function printTerminalArrows(blockId: BlockId, terminal: Terminal): string {
}
break;
}
case "logical": {
buffer.push(printJumpArrow(blockId, terminal.test, "test"));
buffer.push(printJumpArrow(blockId, terminal.fallthrough, "fallthrough"));
break;
}
case "goto": {
buffer.push(printJumpArrow(blockId, terminal.block, null));
break;
Expand Down

0 comments on commit a4fc975

Please sign in to comment.