@samitouri / QOS-React / commits / dc7ac6fea4

[valueblocks] Scaffolding for ternary terminals

Core representation for terminals, without the lowering and conversion logic.

Joe Savona committed Jan 31, 2023 at 13:39 UTC dc7ac6fea4e42ad35a79832b79691ab0afba43e8
6 files changed +40 -1
compiler/forget/src/HIR/HIR.ts
+10 -1
@@ -238,7 +238,8 @@ export type Terminal =
238 | SwitchTerminal
239 | ForTerminal
240 | WhileTerminal
241 - | LogicalTerminal;
241 + | LogicalTerminal
242 + | TernaryTerminal;
243
244 /**
245 * Terminal nodes allowed for a value block
@@ -321,6 +322,14 @@ export type LogicalTerminal = {
322 loc: SourceLocation;
323 };
324
325 +export type TernaryTerminal = {
326 + kind: "ternary";
327 + test: BlockId;
328 + fallthrough: BlockId;
329 + id: InstructionId;
330 + loc: SourceLocation;
331 +};
332 +
333 /**
334 * Instructions generally represent expressions but with all nesting flattened away,
335 * such that all operands to each instruction are either primitive values OR are
compiler/forget/src/HIR/HIRBuilder.ts
+1
@@ -562,6 +562,7 @@ export function reversePostorderBlocks(func: HIR): void {
562 }
563 break;
564 }
565 + case "ternary":
566 case "logical": {
567 visit(terminal.test);
568 break;
compiler/forget/src/HIR/PrintHIR.ts
+5
@@ -79,6 +79,7 @@ export function printMixedHIR(
79 case "branch":
80 case "if":
81 case "logical":
82 + case "ternary":
83 case "return":
84 case "switch":
85 case "throw":
@@ -146,6 +147,10 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
147 value = `[${terminal.id}] Logical ${terminal.operator} test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`;
148 break;
149 }
150 + case "ternary": {
151 + value = `[${terminal.id}] Ternary test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`;
152 + break;
153 + }
154 case "throw": {
155 value = `[${terminal.id}] Throw ${printPlace(terminal.value)}`;
156 break;
compiler/forget/src/HIR/visitors.ts
+14
@@ -294,6 +294,17 @@ export function mapTerminalSuccessors(
294 loc: terminal.loc,
295 };
296 }
297 + case "ternary": {
298 + const test = fn(terminal.test);
299 + const fallthrough = fn(terminal.fallthrough);
300 + return {
301 + kind: "ternary",
302 + test,
303 + fallthrough,
304 + id: makeInstructionId(0),
305 + loc: terminal.loc,
306 + };
307 + }
308 case "return": {
309 return {
310 kind: "return",
@@ -373,6 +384,7 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
384 }
385 break;
386 }
387 + case "ternary":
388 case "logical": {
389 yield terminal.test;
390 break;
@@ -432,6 +444,7 @@ export function mapTerminalOperands(
444 }
445 break;
446 }
447 + case "ternary":
448 case "logical":
449 case "while":
450 case "for":
@@ -476,6 +489,7 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
489 }
490 break;
491 }
492 + case "ternary":
493 case "logical":
494 case "while":
495 case "for":
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
+1
@@ -406,6 +406,7 @@ class Driver {
406
407 break;
408 }
409 + case "ternary":
410 case "logical": {
411 const fallthroughId = terminal.fallthrough;
412 invariant(
compiler/forget/src/Utils/VisualizeHIRMermaid.ts
+9
@@ -103,6 +103,10 @@ function printTerminalLabel(terminal: Terminal): string {
103 buffer.push(`Logical ${terminal.operator}`);
104 break;
105 }
106 + case "ternary": {
107 + buffer.push("Ternary");
108 + break;
109 + }
110 case "while":
111 buffer.push("While");
112 break;
@@ -144,6 +148,11 @@ function printTerminalArrows(blockId: BlockId, terminal: Terminal): string {
148 buffer.push(printJumpArrow(blockId, terminal.fallthrough, "fallthrough"));
149 break;
150 }
151 + case "ternary": {
152 + buffer.push(printJumpArrow(blockId, terminal.test, "test"));
153 + buffer.push(printJumpArrow(blockId, terminal.fallthrough, "fallthrough"));
154 + break;
155 + }
156 case "goto": {
157 buffer.push(printJumpArrow(blockId, terminal.block, null));
158 break;