diff --git a/compiler/forget/src/HIR/BuildReactiveFunction.ts b/compiler/forget/src/HIR/BuildReactiveFunction.ts index 7d30ba3d98..f38b160566 100644 --- a/compiler/forget/src/HIR/BuildReactiveFunction.ts +++ b/compiler/forget/src/HIR/BuildReactiveFunction.ts @@ -14,11 +14,11 @@ import { InstructionId, InstructionValue, Place, - ReactiveBasicBlock, ReactiveBlock, ReactiveFunction, - ReactiveInstruction, ReactiveScope, + ReactiveScopeBlock, + ReactiveStatement, ReactiveTerminal, ReactiveValueBlock, ScopeId, @@ -41,23 +41,23 @@ export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction { } type BlockKind = - | { kind: "block"; block: ReactiveBasicBlock } - | { kind: "scope"; block: ReactiveBasicBlock; scope: ReactiveScope }; + | { kind: "block"; block: ReactiveBlock } + | { kind: "scope"; block: ReactiveBlock; scope: ReactiveScope }; class Builder { - #instructions: ReactiveBasicBlock; + #instructions: ReactiveBlock; #stack: Array< - | { kind: "scope"; block: ReactiveBlock } - | { kind: "block"; block: ReactiveBasicBlock } + | { kind: "scope"; block: ReactiveScopeBlock } + | { kind: "block"; block: ReactiveBlock } >; constructor() { - const block: ReactiveBasicBlock = []; + const block: ReactiveBlock = []; this.#instructions = block; this.#stack = [{ kind: "block", block }]; } - append(item: ReactiveInstruction, label: BlockId | undefined): void { + append(item: ReactiveStatement, label: BlockId | undefined): void { if (label !== undefined) { invariant(item.kind === "terminal", "Only terminals may have a label"); item.label = label; @@ -66,8 +66,8 @@ class Builder { } startScope(scope: ReactiveScope): void { - const block: ReactiveBlock = { - kind: "block", + const block: ReactiveScopeBlock = { + kind: "scope", scope, instructions: [], }; @@ -92,7 +92,7 @@ class Builder { } } - complete(): ReactiveBasicBlock { + complete(): ReactiveBlock { // TODO @josephsavona: debug two failures of this // invariant( // this.#stack.length === 1, @@ -111,12 +111,12 @@ class ReactiveFunctionBuilder implements Visitor< Builder, - ReactiveBasicBlock, + ReactiveBlock, ReactiveValueBlock, ReactiveValueBlock, InstructionValue | ReactiveValueBlock, - ReactiveInstruction, - { test: InstructionValue | null; block: ReactiveBasicBlock } + ReactiveStatement, + { test: InstructionValue | null; block: ReactiveBlock } > { #builders: Array = []; @@ -134,12 +134,12 @@ class ReactiveFunctionBuilder } appendBlock( block: Builder, - item: ReactiveInstruction, + item: ReactiveStatement, label?: BlockId | undefined ): void { block.append(item, label); } - leaveBlock(block: Builder): ReactiveBasicBlock { + leaveBlock(block: Builder): ReactiveBlock { const builder = this.#builders.pop(); invariant( builder === block, @@ -155,7 +155,7 @@ class ReactiveFunctionBuilder value: null, }; } - appendValueBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void { + appendValueBlock(block: ReactiveValueBlock, item: ReactiveStatement): void { block.instructions.push(item); } leaveValueBlock( @@ -175,7 +175,7 @@ class ReactiveFunctionBuilder enterInitBlock(block: Builder): ReactiveValueBlock { return this.enterValueBlock(block); } - appendInitBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void { + appendInitBlock(block: ReactiveValueBlock, item: ReactiveStatement): void { this.appendValueBlock(block, item); } leaveInitBlock(block: ReactiveValueBlock): ReactiveValueBlock { @@ -191,7 +191,7 @@ class ReactiveFunctionBuilder visitInstruction( instruction: Instruction, value: InstructionValue | ReactiveValueBlock - ): ReactiveInstruction { + ): ReactiveStatement { this.visitId(instruction.id); const scope = getInstructionScope(instruction); if (scope !== null && !this.#scopes.has(scope.id)) { @@ -204,17 +204,17 @@ class ReactiveFunctionBuilder visitTerminalId(id: InstructionId): void { this.visitId(id); } - visitImplicitTerminal(): ReactiveInstruction | null { + visitImplicitTerminal(): ReactiveStatement | null { return null; } visitTerminal( terminal: BlockTerminal< ReactiveValueBlock, InstructionValue | ReactiveValueBlock, - ReactiveBasicBlock, - { test: InstructionValue | null; block: ReactiveBasicBlock } + ReactiveBlock, + { test: InstructionValue | null; block: ReactiveBlock } > - ): ReactiveInstruction { + ): ReactiveStatement { let result: ReactiveTerminal; switch (terminal.kind) { case "break": { @@ -259,7 +259,7 @@ class ReactiveFunctionBuilder test: terminal.test as Place, cases: terminal.cases as Array<{ test: Place | null; - block: ReactiveBasicBlock | void; + block: ReactiveBlock | void; }>, }; break; @@ -291,8 +291,8 @@ class ReactiveFunctionBuilder } visitCase( test: InstructionValue | ReactiveValueBlock | null, - block: ReactiveBasicBlock - ): { test: InstructionValue | null; block: ReactiveBasicBlock } { + block: ReactiveBlock + ): { test: InstructionValue | null; block: ReactiveBlock } { if (test !== null && test.kind !== "Identifier") { invariant(false, "Expected a Place"); } diff --git a/compiler/forget/src/HIR/CodegenReactiveFunction.ts b/compiler/forget/src/HIR/CodegenReactiveFunction.ts index 31465729e2..90cb06616f 100644 --- a/compiler/forget/src/HIR/CodegenReactiveFunction.ts +++ b/compiler/forget/src/HIR/CodegenReactiveFunction.ts @@ -21,7 +21,7 @@ import { Identifier, Instruction, InstructionKind, - ReactiveBasicBlock, + ReactiveBlock, ReactiveFunction, ReactiveScope, ReactiveTerminal, @@ -84,10 +84,7 @@ class Context { } } -function codegenBlock( - cx: Context, - block: ReactiveBasicBlock -): t.BlockStatement { +function codegenBlock(cx: Context, block: ReactiveBlock): t.BlockStatement { const statements: Array = []; for (const item of block) { switch (item.kind) { @@ -102,7 +99,7 @@ function codegenBlock( } break; } - case "block": { + case "scope": { codegenReactiveScope(cx, statements, item.scope, item.instructions); break; } @@ -132,7 +129,7 @@ function codegenReactiveScope( cx: Context, statements: Array, scope: ReactiveScope, - block: ReactiveBasicBlock + block: ReactiveBlock ): void { const cacheStoreStatements: Array = []; const cacheLoadStatements: Array = []; diff --git a/compiler/forget/src/HIR/FlattenReactiveLoops.ts b/compiler/forget/src/HIR/FlattenReactiveLoops.ts index d07cb8a168..0d8546b655 100644 --- a/compiler/forget/src/HIR/FlattenReactiveLoops.ts +++ b/compiler/forget/src/HIR/FlattenReactiveLoops.ts @@ -6,7 +6,7 @@ */ import { assertExhaustive } from "../Common/utils"; -import { ReactiveBasicBlock, ReactiveBlock, ReactiveFunction } from "./HIR"; +import { ReactiveBlock, ReactiveFunction, ReactiveScopeBlock } from "./HIR"; /** * Given a reactive function, flattens any scopes contained within a loop construct. @@ -16,12 +16,12 @@ export function flattenReactiveLoops(fn: ReactiveFunction): void { visit(fn.body, false); } -function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void { +function visit(block: ReactiveBlock, shouldFlatten: boolean): void { let i = 0; while (i < block.length) { const item = block[i]!; switch (item.kind) { - case "block": { + case "scope": { if (shouldFlatten) { const successors = block.splice(i + 1); block.pop(); // remove the current element @@ -87,10 +87,10 @@ function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void { } } -function flatten(scope: ReactiveBlock, block: ReactiveBasicBlock): void { +function flatten(scope: ReactiveScopeBlock, block: ReactiveBlock): void { for (const item of scope.instructions) { switch (item.kind) { - case "block": { + case "scope": { flatten(item, block); break; } diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 2178fc138b..6c1c9bef72 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -14,7 +14,7 @@ import { invariant } from "../CompilerError"; // ******************************************************************************************* // ******************************************************************************************* -// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (scheduling?) -> HIR -> (codegen) -> AST +// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (codegen) -> AST /** * A location in a source file, intended to be used for providing diagnostic information and @@ -47,27 +47,27 @@ export type ReactiveFunction = { params: Array; generator: boolean; async: boolean; - body: ReactiveBasicBlock; + body: ReactiveBlock; }; -export type ReactiveBlock = { - kind: "block"; +export type ReactiveScopeBlock = { + kind: "scope"; scope: ReactiveScope; - instructions: ReactiveBasicBlock; + instructions: ReactiveBlock; }; -export type ReactiveBasicBlock = Array; +export type ReactiveBlock = Array; export type ReactiveValueBlock = { kind: "value-block"; - instructions: ReactiveBasicBlock; + instructions: ReactiveBlock; value: InstructionValue | null; }; -export type ReactiveInstruction = +export type ReactiveStatement = | { kind: "instruction"; instruction: Instruction } | { kind: "terminal"; terminal: ReactiveTerminal; label: BlockId | null } - | ReactiveBlock; + | ReactiveScopeBlock; export type ReactiveTerminal = | { kind: "break"; label: BlockId | null } @@ -79,22 +79,22 @@ export type ReactiveTerminal = test: Place; cases: Array<{ test: Place | null; - block: ReactiveBasicBlock | void; + block: ReactiveBlock | void; }>; } - | { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBasicBlock } + | { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBlock } | { kind: "for"; init: ReactiveValueBlock; test: ReactiveValueBlock; update: ReactiveValueBlock; - loop: ReactiveBasicBlock; + loop: ReactiveBlock; } | { kind: "if"; test: Place; - consequent: ReactiveBasicBlock; - alternate: ReactiveBasicBlock | null; + consequent: ReactiveBlock; + alternate: ReactiveBlock | null; }; /** diff --git a/compiler/forget/src/HIR/PrintReactiveFunction.ts b/compiler/forget/src/HIR/PrintReactiveFunction.ts index 9f300d3a54..cd334cfd4a 100644 --- a/compiler/forget/src/HIR/PrintReactiveFunction.ts +++ b/compiler/forget/src/HIR/PrintReactiveFunction.ts @@ -8,9 +8,9 @@ import invariant from "invariant"; import { assertExhaustive } from "../Common/utils"; import { - ReactiveBlock, ReactiveFunction, - ReactiveInstruction, + ReactiveScopeBlock, + ReactiveStatement, ReactiveTerminal, ReactiveValueBlock, } from "./HIR"; @@ -35,7 +35,10 @@ export function printReactiveFunction(fn: ReactiveFunction): string { return writer.complete(); } -export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void { +export function printReactiveBlock( + writer: Writer, + block: ReactiveScopeBlock +): void { writer.writeLine( `scope @${block.scope.id} [${block.scope.range.start}:${ block.scope.range.end @@ -51,7 +54,7 @@ export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void { export function printReactiveInstructions( writer: Writer, - instructions: Array + instructions: Array ): void { writer.indented(() => { for (const instr of instructions) { @@ -62,14 +65,14 @@ export function printReactiveInstructions( function printReactiveInstruction( writer: Writer, - instr: ReactiveInstruction + instr: ReactiveStatement ): void { switch (instr.kind) { case "instruction": { writer.writeLine(printInstruction(instr.instruction)); break; } - case "block": { + case "scope": { printReactiveBlock(writer, instr); break; } diff --git a/compiler/forget/src/HIR/PropagateScopeDependencies.ts b/compiler/forget/src/HIR/PropagateScopeDependencies.ts index 4a77905437..2afc52ad95 100644 --- a/compiler/forget/src/HIR/PropagateScopeDependencies.ts +++ b/compiler/forget/src/HIR/PropagateScopeDependencies.ts @@ -15,7 +15,7 @@ import { makeInstructionId, MutableRange, Place, - ReactiveBasicBlock, + ReactiveBlock, ReactiveFunction, ReactiveValueBlock, } from "./HIR"; @@ -50,14 +50,14 @@ enum DeclKind { type DeclMap = Map; function visit( - block: ReactiveBasicBlock, + block: ReactiveBlock, dependencies: Set, declarations: DeclMap, scopeRange: MutableRange | null ): void { for (const item of block) { switch (item.kind) { - case "block": { + case "scope": { const scopeDependencies: Set = new Set(); // TODO: it would be sufficient to use a single mapping of declarations const scopeDeclarations: DeclMap = new Map(declarations); diff --git a/compiler/forget/src/HIR/PruneUnusedLabels.ts b/compiler/forget/src/HIR/PruneUnusedLabels.ts index 666f04a1f9..a4c4f7732a 100644 --- a/compiler/forget/src/HIR/PruneUnusedLabels.ts +++ b/compiler/forget/src/HIR/PruneUnusedLabels.ts @@ -8,7 +8,7 @@ import { assertExhaustive } from "../Common/utils"; import { BlockId, - ReactiveBasicBlock, + ReactiveBlock, ReactiveFunction, ReactiveTerminal, } from "./HIR"; @@ -23,7 +23,7 @@ export function pruneUnusedLabels(fn: ReactiveFunction): void { type Labels = Set; -function visitBlock(labels: Labels, block: ReactiveBasicBlock): void { +function visitBlock(labels: Labels, block: ReactiveBlock): void { for (const item of block) { if (item.kind === "terminal") { // first visit the terminal's contents, which is the only place that can @@ -33,7 +33,7 @@ function visitBlock(labels: Labels, block: ReactiveBasicBlock): void { if (item.label !== null && !labels.has(item.label)) { item.label = null; } - } else if (item.kind === "block") { + } else if (item.kind === "scope") { visitBlock(labels, item.instructions); } }