From dc7ac6fea4e42ad35a79832b79691ab0afba43e8 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 31 Jan 2023 13:39:34 -0800 Subject: [PATCH] [valueblocks] Scaffolding for ternary terminals Core representation for terminals, without the lowering and conversion logic. --- compiler/forget/src/HIR/HIR.ts | 11 ++++++++++- compiler/forget/src/HIR/HIRBuilder.ts | 1 + compiler/forget/src/HIR/PrintHIR.ts | 5 +++++ compiler/forget/src/HIR/visitors.ts | 14 ++++++++++++++ .../src/ReactiveScopes/BuildReactiveFunction.ts | 1 + compiler/forget/src/Utils/VisualizeHIRMermaid.ts | 9 +++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 5d9cde3984..6dbe997d1a 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -238,7 +238,8 @@ export type Terminal = | SwitchTerminal | ForTerminal | WhileTerminal - | LogicalTerminal; + | LogicalTerminal + | TernaryTerminal; /** * Terminal nodes allowed for a value block @@ -321,6 +322,14 @@ export type LogicalTerminal = { loc: SourceLocation; }; +export type TernaryTerminal = { + kind: "ternary"; + test: BlockId; + fallthrough: BlockId; + id: InstructionId; + loc: SourceLocation; +}; + /** * Instructions generally represent expressions but with all nesting flattened away, * such that all operands to each instruction are either primitive values OR are diff --git a/compiler/forget/src/HIR/HIRBuilder.ts b/compiler/forget/src/HIR/HIRBuilder.ts index e5a6c0ce9b..4b97fa27fb 100644 --- a/compiler/forget/src/HIR/HIRBuilder.ts +++ b/compiler/forget/src/HIR/HIRBuilder.ts @@ -562,6 +562,7 @@ export function reversePostorderBlocks(func: HIR): void { } break; } + case "ternary": case "logical": { visit(terminal.test); break; diff --git a/compiler/forget/src/HIR/PrintHIR.ts b/compiler/forget/src/HIR/PrintHIR.ts index a9a43918a4..e3fcf44be7 100644 --- a/compiler/forget/src/HIR/PrintHIR.ts +++ b/compiler/forget/src/HIR/PrintHIR.ts @@ -79,6 +79,7 @@ export function printMixedHIR( case "branch": case "if": case "logical": + case "ternary": case "return": case "switch": case "throw": @@ -146,6 +147,10 @@ export function printTerminal(terminal: Terminal): Array | string { value = `[${terminal.id}] Logical ${terminal.operator} test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`; break; } + case "ternary": { + value = `[${terminal.id}] Ternary test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`; + break; + } case "throw": { value = `[${terminal.id}] Throw ${printPlace(terminal.value)}`; break; diff --git a/compiler/forget/src/HIR/visitors.ts b/compiler/forget/src/HIR/visitors.ts index aa690410da..26e3c52340 100644 --- a/compiler/forget/src/HIR/visitors.ts +++ b/compiler/forget/src/HIR/visitors.ts @@ -294,6 +294,17 @@ export function mapTerminalSuccessors( loc: terminal.loc, }; } + case "ternary": { + const test = fn(terminal.test); + const fallthrough = fn(terminal.fallthrough); + return { + kind: "ternary", + test, + fallthrough, + id: makeInstructionId(0), + loc: terminal.loc, + }; + } case "return": { return { kind: "return", @@ -373,6 +384,7 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable { } break; } + case "ternary": case "logical": { yield terminal.test; break; @@ -432,6 +444,7 @@ export function mapTerminalOperands( } break; } + case "ternary": case "logical": case "while": case "for": @@ -476,6 +489,7 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable { } break; } + case "ternary": case "logical": case "while": case "for": diff --git a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts index 7e252fa85a..2560b2a64e 100644 --- a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts @@ -406,6 +406,7 @@ class Driver { break; } + case "ternary": case "logical": { const fallthroughId = terminal.fallthrough; invariant( diff --git a/compiler/forget/src/Utils/VisualizeHIRMermaid.ts b/compiler/forget/src/Utils/VisualizeHIRMermaid.ts index 6d55dccfe9..c6ded36f72 100644 --- a/compiler/forget/src/Utils/VisualizeHIRMermaid.ts +++ b/compiler/forget/src/Utils/VisualizeHIRMermaid.ts @@ -103,6 +103,10 @@ function printTerminalLabel(terminal: Terminal): string { buffer.push(`Logical ${terminal.operator}`); break; } + case "ternary": { + buffer.push("Ternary"); + break; + } case "while": buffer.push("While"); break; @@ -144,6 +148,11 @@ function printTerminalArrows(blockId: BlockId, terminal: Terminal): string { buffer.push(printJumpArrow(blockId, terminal.fallthrough, "fallthrough")); break; } + case "ternary": { + 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;