From 05d1fe94f91fbe03fcdfbe661f677331f9283a14 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 16 Dec 2022 17:25:11 -0800 Subject: [PATCH] [be] Split files into directories Moves _some_ files from HIR into new top-level directories. To not make @gsathya's life a pain I left the files he's touching alone, but I moved some others. My intent is to have something like this: * Babel/ - code for the Babel plugin, though ideally this actually gets split into a separate package and the compiler itself is AST in, AST out w no Babel dep. * HIR/ - the core HIRFunction, HIR and related data types, plus the HIR construction and printing, HIR visitors. * Inference/ - the core inference passes that operate on the HIR, including type inference, reference effects, alias analysis, mutable range analysis, etc. I'll let @gsathya move these files when at a good stopping point. * ReactiveScopes/ - inference relating to reactive scopes, constructing/printing/codegenning ReactiveFunction * SSA/ - enter/leave SSA and eliminate redundant phis * Utils/ - every project needs a place to put stuff that doesn't fit into the other categories, this is ours. This leaves just index.ts at the top level, and overall feels pretty tidy. Not too tedious to figure out where anything goes, hopefully. --- .../forget/src/{ => Babel}/BabelPlugin.ts | 4 +- .../src/{ => Babel}/plugin-syntax-jsx.d.ts | 0 compiler/forget/src/Common/Dumper.ts | 113 ------------ compiler/forget/src/Common/PathUnion.ts | 39 ---- compiler/forget/src/HIR/BuildHIR.ts | 6 +- compiler/forget/src/HIR/Codegen.ts | 6 +- compiler/forget/src/HIR/HIR.ts | 2 +- compiler/forget/src/HIR/HIRBuilder.ts | 6 +- compiler/forget/src/HIR/HIRTreeVisitor.ts | 2 +- compiler/forget/src/HIR/InferAlias.ts | 4 +- .../forget/src/HIR/InferAliasForFields.ts | 2 +- .../forget/src/HIR/InferMutableLifetimes.ts | 2 +- .../src/HIR/InferMutableRangesForAlias.ts | 2 +- .../forget/src/HIR/InferReferenceEffects.ts | 5 +- compiler/forget/src/HIR/Pipeline.ts | 24 +-- compiler/forget/src/HIR/PrintHIR.ts | 4 +- compiler/forget/src/HIR/PrintHIRTree.ts | 173 ------------------ compiler/forget/src/HIR/visitors.ts | 2 +- .../BuildReactiveFunction.ts | 8 +- .../CodegenReactiveFunction.ts | 8 +- .../FlattenReactiveLoops.ts | 8 +- .../InferReactiveScopeVariables.ts | 8 +- .../InferReactiveScopes.ts | 14 +- .../PrintReactiveFunction.ts | 6 +- .../PropagateScopeDependencies.ts | 6 +- .../PruneUnusedLabels.ts | 4 +- .../src/{HIR => SSA}/EliminateRedundantPhi.ts | 4 +- compiler/forget/src/{HIR => SSA}/EnterSSA.ts | 10 +- compiler/forget/src/{HIR => SSA}/LeaveSSA.ts | 7 +- .../forget/src/{ => Utils}/CompilerError.ts | 0 .../forget/src/{HIR => Utils}/DisjointSet.ts | 2 +- .../src/{HIR => Utils}/VisualizeHIRMermaid.ts | 6 +- compiler/forget/src/{HIR => Utils}/logger.ts | 4 +- compiler/forget/src/{HIR => Utils}/todo.ts | 0 .../forget/src/{Common => Utils}/utils.ts | 37 ---- compiler/forget/src/__tests__/hir-test.ts | 2 +- compiler/forget/src/index.ts | 24 +-- 37 files changed, 98 insertions(+), 456 deletions(-) rename compiler/forget/src/{ => Babel}/BabelPlugin.ts (92%) rename compiler/forget/src/{ => Babel}/plugin-syntax-jsx.d.ts (100%) delete mode 100644 compiler/forget/src/Common/Dumper.ts delete mode 100644 compiler/forget/src/Common/PathUnion.ts delete mode 100644 compiler/forget/src/HIR/PrintHIRTree.ts rename compiler/forget/src/{HIR => ReactiveScopes}/BuildReactiveFunction.ts (97%) rename compiler/forget/src/{HIR => ReactiveScopes}/CodegenReactiveFunction.ts (98%) rename compiler/forget/src/{HIR => ReactiveScopes}/FlattenReactiveLoops.ts (96%) rename compiler/forget/src/{HIR => ReactiveScopes}/InferReactiveScopeVariables.ts (96%) rename compiler/forget/src/{HIR => ReactiveScopes}/InferReactiveScopes.ts (97%) rename compiler/forget/src/{HIR => ReactiveScopes}/PrintReactiveFunction.ts (98%) rename compiler/forget/src/{HIR => ReactiveScopes}/PropagateScopeDependencies.ts (98%) rename compiler/forget/src/{HIR => ReactiveScopes}/PruneUnusedLabels.ts (96%) rename compiler/forget/src/{HIR => SSA}/EliminateRedundantPhi.ts (96%) rename compiler/forget/src/{HIR => SSA}/EnterSSA.ts (96%) rename compiler/forget/src/{HIR => SSA}/LeaveSSA.ts (98%) rename compiler/forget/src/{ => Utils}/CompilerError.ts (100%) rename compiler/forget/src/{HIR => Utils}/DisjointSet.ts (98%) rename compiler/forget/src/{HIR => Utils}/VisualizeHIRMermaid.ts (96%) rename compiler/forget/src/{HIR => Utils}/logger.ts (86%) rename compiler/forget/src/{HIR => Utils}/todo.ts (100%) rename compiler/forget/src/{Common => Utils}/utils.ts (52%) diff --git a/compiler/forget/src/BabelPlugin.ts b/compiler/forget/src/Babel/BabelPlugin.ts similarity index 92% rename from compiler/forget/src/BabelPlugin.ts rename to compiler/forget/src/Babel/BabelPlugin.ts index 516be22189..51df340d8a 100644 --- a/compiler/forget/src/BabelPlugin.ts +++ b/compiler/forget/src/Babel/BabelPlugin.ts @@ -10,8 +10,8 @@ import type * as BabelCore from "@babel/core"; import type { PluginObj } from "@babel/core"; import jsx from "@babel/plugin-syntax-jsx"; -import { invariant } from "./CompilerError"; -import Pipeline from "./HIR/Pipeline"; +import Pipeline from "../HIR/Pipeline"; +import { invariant } from "../Utils/CompilerError"; /** * The React Forget Babel Plugin diff --git a/compiler/forget/src/plugin-syntax-jsx.d.ts b/compiler/forget/src/Babel/plugin-syntax-jsx.d.ts similarity index 100% rename from compiler/forget/src/plugin-syntax-jsx.d.ts rename to compiler/forget/src/Babel/plugin-syntax-jsx.d.ts diff --git a/compiler/forget/src/Common/Dumper.ts b/compiler/forget/src/Common/Dumper.ts deleted file mode 100644 index b584fea15f..0000000000 --- a/compiler/forget/src/Common/Dumper.ts +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import * as t from "@babel/types"; -import generate from "@babel/generator"; - -/** - * Dumper. - */ - -const defaultDumpNodeOption = { - // dump with location. - loc: false, - - // dump with the node type. - type: false, - - // dump as the entire source code instead of a shorten name. - source: false, - - // instead of , fallback to source. - fallbackToSource: true, -}; - -type DumpNodeOption = typeof defaultDumpNodeOption; - -export function dumpNodeLoc(node: t.Node): string { - return `${node.loc?.start.line ?? "?"}:${node.loc?.start.column ?? "?"}`; -} - -/** - * Dump a string form of the @param node to help with debugging. - */ -export function dumpNode( - node: t.Node | null | undefined, - options: Partial = defaultDumpNodeOption -): string { - let str = ""; - let opt = { ...defaultDumpNodeOption, ...options }; - - if (!node) return ""; - - if (opt.loc) { - str += `${dumpNodeLoc(node)} `; - } - - if (opt.source) { - str += generate(node).code; - } else { - switch (node.type) { - case "Identifier": - str += node.name; - break; - - case "JSXIdentifier": - str += `${node.name}`; - break; - - case "VariableDeclarator": - str += `${dumpNode(node.id, { source: true })}`; - break; - - case "JSXMemberExpression": - str += `${dumpNode(node.object)}.${dumpNode(node.property)}`; - break; - - case "JSXNamespacedName": - str += `${dumpNode(node.namespace)}:${dumpNode(node.name)}`; - break; - - case "JSXOpeningElement": - str += dumpNode(node.name); - break; - - case "JSXAttribute": - str += dumpNode(node.name); - break; - - case "FunctionDeclaration": - str += `function ${dumpNode(node.id)}`; - break; - - case "JSXFragment": - str += `<>`; - break; - - case "JSXElement": - if (node.selfClosing) { - str += `<${dumpNode(node.openingElement)} />`; - } else { - str += `<${dumpNode(node.openingElement)}>`; - } - break; - - default: - if (opt.fallbackToSource) { - str += "`" + generate(node, { compact: true }).code + "`"; - } else { - str += ``; - } - } - } - - if (opt.type) { - str += ` : ${node.type}`; - } - - return str; -} diff --git a/compiler/forget/src/Common/PathUnion.ts b/compiler/forget/src/Common/PathUnion.ts deleted file mode 100644 index 46755c0820..0000000000 --- a/compiler/forget/src/Common/PathUnion.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import { Node, NodePath } from "@babel/traverse"; - -/** - * A utility type to generate NodePath as a disjointed union type for better - * type refinement and exhaustiveness check. - * - * @see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types - */ -export type PathUnion = N extends Node - ? NodePath - : N extends undefined - ? NodePath - : N extends null - ? NodePath - : never; - -/** - * Cast @param path of type {@link NodePath} to {@link PathUnion}. - * - * This should only be used right before a type refinement (aka. narrowing): - * - * const path = pathUnion(rawPath) - * switch(path.kind) { - * case "A": - * // path is now refined to NodePath - * } - */ -export function pathUnion( - path: NodePath -): PathUnion { - return path as PathUnion; -} diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index d0c62bed95..2ff8d420c4 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -7,8 +7,9 @@ import { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; -import { assertExhaustive } from "../Common/utils"; -import { invariant } from "../CompilerError"; +import { invariant } from "../Utils/CompilerError"; +import todo, { todoInvariant } from "../Utils/todo"; +import { assertExhaustive } from "../Utils/utils"; import { Effect, GeneratedSource, @@ -24,7 +25,6 @@ import { ThrowTerminal, } from "./HIR"; import HIRBuilder, { Environment } from "./HIRBuilder"; -import todo, { todoInvariant } from "./todo"; // ******************************************************************************************* // ******************************************************************************************* diff --git a/compiler/forget/src/HIR/Codegen.ts b/compiler/forget/src/HIR/Codegen.ts index 648e516d8a..ba0cc9ec54 100644 --- a/compiler/forget/src/HIR/Codegen.ts +++ b/compiler/forget/src/HIR/Codegen.ts @@ -6,8 +6,9 @@ */ import * as t from "@babel/types"; -import { assertExhaustive } from "../Common/utils"; -import { invariant } from "../CompilerError"; +import { invariant } from "../Utils/CompilerError"; +import { todoInvariant } from "../Utils/todo"; +import { assertExhaustive } from "../Utils/utils"; import { BlockId, GeneratedSource, @@ -23,7 +24,6 @@ import { SourceLocation, } from "./HIR"; import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor"; -import { todoInvariant } from "./todo"; function withLoc TNode>( fn: T diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 6c1c9bef72..1ad7607ec8 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -6,7 +6,7 @@ */ import * as t from "@babel/types"; -import { invariant } from "../CompilerError"; +import { invariant } from "../Utils/CompilerError"; // ******************************************************************************************* // ******************************************************************************************* diff --git a/compiler/forget/src/HIR/HIRBuilder.ts b/compiler/forget/src/HIR/HIRBuilder.ts index 9c6dac2dcd..5a80f54650 100644 --- a/compiler/forget/src/HIR/HIRBuilder.ts +++ b/compiler/forget/src/HIR/HIRBuilder.ts @@ -6,8 +6,9 @@ */ import * as t from "@babel/types"; -import { assertExhaustive } from "../Common/utils"; -import { invariant } from "../CompilerError"; +import { invariant } from "../Utils/CompilerError"; +import { logHIR } from "../Utils/logger"; +import { assertExhaustive } from "../Utils/utils"; import { BasicBlock, BlockId, @@ -23,7 +24,6 @@ import { makeType, Terminal, } from "./HIR"; -import { logHIR } from "./logger"; import { printInstruction } from "./PrintHIR"; import { eachTerminalSuccessor, mapTerminalSuccessors } from "./visitors"; diff --git a/compiler/forget/src/HIR/HIRTreeVisitor.ts b/compiler/forget/src/HIR/HIRTreeVisitor.ts index b67933a189..5ed4a04fb0 100644 --- a/compiler/forget/src/HIR/HIRTreeVisitor.ts +++ b/compiler/forget/src/HIR/HIRTreeVisitor.ts @@ -6,7 +6,7 @@ */ import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; +import { assertExhaustive } from "../Utils/utils"; import { BasicBlock, BlockId, diff --git a/compiler/forget/src/HIR/InferAlias.ts b/compiler/forget/src/HIR/InferAlias.ts index 5511f7a532..001169f737 100644 --- a/compiler/forget/src/HIR/InferAlias.ts +++ b/compiler/forget/src/HIR/InferAlias.ts @@ -1,7 +1,5 @@ -import invariant from "invariant"; -import DisjointSet from "./DisjointSet"; +import DisjointSet from "../Utils/DisjointSet"; import { HIRFunction, Identifier, Instruction, LValue, Place } from "./HIR"; -import { printInstructionValue } from "./PrintHIR"; export type AliasSet = Set; diff --git a/compiler/forget/src/HIR/InferAliasForFields.ts b/compiler/forget/src/HIR/InferAliasForFields.ts index dce04df16f..5e19588217 100644 --- a/compiler/forget/src/HIR/InferAliasForFields.ts +++ b/compiler/forget/src/HIR/InferAliasForFields.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -import DisjointSet from "./DisjointSet"; +import DisjointSet from "../Utils/DisjointSet"; import { HIRFunction, Identifier } from "./HIR"; export function inferAliasForFields( diff --git a/compiler/forget/src/HIR/InferMutableLifetimes.ts b/compiler/forget/src/HIR/InferMutableLifetimes.ts index 8d8d8d5201..881ad9ea4a 100644 --- a/compiler/forget/src/HIR/InferMutableLifetimes.ts +++ b/compiler/forget/src/HIR/InferMutableLifetimes.ts @@ -6,7 +6,7 @@ */ import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; +import { assertExhaustive } from "../Utils/utils"; import { Effect, HIRFunction, diff --git a/compiler/forget/src/HIR/InferMutableRangesForAlias.ts b/compiler/forget/src/HIR/InferMutableRangesForAlias.ts index 023ea8cd42..576eac7e26 100644 --- a/compiler/forget/src/HIR/InferMutableRangesForAlias.ts +++ b/compiler/forget/src/HIR/InferMutableRangesForAlias.ts @@ -1,4 +1,4 @@ -import DisjointSet from "./DisjointSet"; +import DisjointSet from "../Utils/DisjointSet"; import { Identifier, InstructionId } from "./HIR"; export function inferMutableRangesForAlias(aliases: DisjointSet) { diff --git a/compiler/forget/src/HIR/InferReferenceEffects.ts b/compiler/forget/src/HIR/InferReferenceEffects.ts index 6cc80c7194..d3481aaba9 100644 --- a/compiler/forget/src/HIR/InferReferenceEffects.ts +++ b/compiler/forget/src/HIR/InferReferenceEffects.ts @@ -5,8 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import { assertExhaustive } from "../Common/utils"; -import { invariant } from "../CompilerError"; +import { invariant } from "../Utils/CompilerError"; +import { assertExhaustive } from "../Utils/utils"; import { BasicBlock, BlockId, @@ -14,7 +14,6 @@ import { HIRFunction, IdentifierId, InstructionValue, - makeType, Phi, Place, ValueKind, diff --git a/compiler/forget/src/HIR/Pipeline.ts b/compiler/forget/src/HIR/Pipeline.ts index 6a32076958..d9a46d9922 100644 --- a/compiler/forget/src/HIR/Pipeline.ts +++ b/compiler/forget/src/HIR/Pipeline.ts @@ -7,23 +7,23 @@ import { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import { lower } from "../HIR/BuildHIR"; -import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi"; -import enterSSA from "../HIR/EnterSSA"; import { Environment } from "../HIR/HIRBuilder"; import inferReferenceEffects from "../HIR/InferReferenceEffects"; -import { leaveSSA } from "../HIR/LeaveSSA"; -import { buildReactiveFunction } from "./BuildReactiveFunction"; -import { codegenReactiveFunction } from "./CodegenReactiveFunction"; -import { flattenReactiveLoops } from "./FlattenReactiveLoops"; +import { buildReactiveFunction } from "../ReactiveScopes/BuildReactiveFunction"; +import { codegenReactiveFunction } from "../ReactiveScopes/CodegenReactiveFunction"; +import { flattenReactiveLoops } from "../ReactiveScopes/FlattenReactiveLoops"; +import { inferReactiveScopes } from "../ReactiveScopes/InferReactiveScopes"; +import { inferReactiveScopeVariables } from "../ReactiveScopes/InferReactiveScopeVariables"; +import { printReactiveFunction } from "../ReactiveScopes/PrintReactiveFunction"; +import { propagateScopeDependencies } from "../ReactiveScopes/PropagateScopeDependencies"; +import { pruneUnusedLabels } from "../ReactiveScopes/PruneUnusedLabels"; +import { eliminateRedundantPhi } from "../SSA/EliminateRedundantPhi"; +import enterSSA from "../SSA/EnterSSA"; +import { leaveSSA } from "../SSA/LeaveSSA"; +import { logHIRFunction } from "../Utils/logger"; import { HIRFunction } from "./HIR"; import { inferMutableRanges } from "./InferMutableRanges"; -import { inferReactiveScopes } from "./InferReactiveScopes"; -import { inferReactiveScopeVariables } from "./InferReactiveScopeVariables"; import { inferTypes } from "./InferTypes"; -import { logHIRFunction } from "./logger"; -import { printReactiveFunction } from "./PrintReactiveFunction"; -import { propagateScopeDependencies } from "./PropagateScopeDependencies"; -import { pruneUnusedLabels } from "./PruneUnusedLabels"; export type CompilerFlags = { eliminateRedundantPhi: boolean; diff --git a/compiler/forget/src/HIR/PrintHIR.ts b/compiler/forget/src/HIR/PrintHIR.ts index e6f8ec1f5c..b4d19d73e4 100644 --- a/compiler/forget/src/HIR/PrintHIR.ts +++ b/compiler/forget/src/HIR/PrintHIR.ts @@ -6,8 +6,8 @@ */ import generate from "@babel/generator"; -import { assertExhaustive } from "../Common/utils"; -import DisjointSet from "./DisjointSet"; +import DisjointSet from "../Utils/DisjointSet"; +import { assertExhaustive } from "../Utils/utils"; import { GotoVariant, HIR, diff --git a/compiler/forget/src/HIR/PrintHIRTree.ts b/compiler/forget/src/HIR/PrintHIRTree.ts deleted file mode 100644 index 1460090520..0000000000 --- a/compiler/forget/src/HIR/PrintHIRTree.ts +++ /dev/null @@ -1,173 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import { assertExhaustive } from "../Common/utils"; -import { - BlockId, - HIRFunction, - Instruction, - InstructionId, - InstructionValue, -} from "./HIR"; -import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor"; -import { printLValue, printMixedHIR } from "./PrintHIR"; - -/** - * Returns a text description of the HIR that has the overall tree shape - * of the original AST, but with the contents of each block printed - * similarly to printHIR's instruction formatting. - */ -export function printHIRTree(fn: HIRFunction): string { - return visitTree(fn, new PrintVisitor()); -} - -class PrintVisitor - implements - Visitor< - Array, - string, - Array, - Array, - string, - string, - string - > -{ - depth: number = 0; // for indentation - - enterBlock(): string[] { - this.depth++; - return []; - } - enterValueBlock(): string[] { - return this.enterBlock(); - } - leaveValueBlock(block: string[], value: string): string { - return this.leaveBlock(block); - } - enterInitBlock(block: string[]): string[] { - return this.enterBlock(); - } - leaveInitBlock(block: string[]): string[] { - return block; - } - visitValue(value: InstructionValue): string { - return printMixedHIR(value); - } - visitInstruction(instr: Instruction, value: string): string { - if (instr.lvalue !== null) { - return `[${instr.id}] ${printLValue(instr.lvalue)} = ${value}`; - } else { - return `[${instr.id}] ${value}`; - } - } - visitTerminalId(id: InstructionId): void {} - visitImplicitTerminal(): string | null { - return null; - } - visitTerminal( - terminal: BlockTerminal - ): string { - let value: string; - switch (terminal.kind) { - case "break": { - if (terminal.label !== null) { - value = `Break ${terminal.label}`; - } else { - value = "Break"; - } - break; - } - case "continue": { - if (terminal.label !== null) { - value = `Continue ${terminal.label}`; - } else { - value = "Continue"; - } - break; - } - case "if": { - if (terminal.alternate !== null) { - value = `If (${ - terminal.test - }) ${terminal.consequent.trimStart()} else ${terminal.alternate}`; - } else { - value = `If (${terminal.test}) ${terminal.consequent.trimStart()}`; - } - break; - } - case "switch": { - const prefix = " ".repeat(this.depth); - value = `Switch (${terminal.test}) {\n${terminal.cases - .flatMap((case_) => case_.split("\n").map((line) => ` ${line}`)) - .join("\n")}\n${prefix}}`; - break; - } - case "while": { - value = `While (${terminal.test}) ${terminal.loop.trimStart()}`; - break; - } - case "for": { - value = `For (TODO) (${ - terminal.test - }) (TODO) ${terminal.loop.trimStart()}`; - break; - } - case "return": { - if (terminal.value !== null) { - value = `Return ${terminal.value}`; - } else { - value = "Return"; - } - break; - } - case "throw": { - value = `Throw ${terminal.value}`; - break; - } - default: { - assertExhaustive( - terminal, - `Unexpected terminal kind '${(terminal as any).kind}'` - ); - } - } - return value; - } - visitCase(test: string | null, block: string): string { - const prefix = " ".repeat(this.depth); - if (test === null) { - return `${prefix}default: ${block.trimStart()}`; - } else { - return `${prefix}case ${test}: ${block.trimStart()}`; - } - } - appendBlock( - block: string[], - item: string, - label?: BlockId | undefined - ): void { - const prefix = " ".repeat(this.depth); - if (item !== "") { - block.push(`${prefix}${item.trimStart()}`); - } - if (label !== undefined) { - block.push(`${prefix}bb${label}:`); - } - } - appendValueBlock(block: string[], item: string): void { - this.appendBlock(block, item); - } - appendInitBlock(block: string[], item: string): void { - this.appendBlock(block, item); - } - leaveBlock(block: string[]): string { - this.depth--; - const prefix = " ".repeat(this.depth); - return `${prefix}{\n${block.join("\n")}\n${prefix}}`; - } -} diff --git a/compiler/forget/src/HIR/visitors.ts b/compiler/forget/src/HIR/visitors.ts index 9df714df84..6bd04c1e2c 100644 --- a/compiler/forget/src/HIR/visitors.ts +++ b/compiler/forget/src/HIR/visitors.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import { assertExhaustive } from "../Common/utils"; +import { assertExhaustive } from "../Utils/utils"; import { BasicBlock, BlockId, diff --git a/compiler/forget/src/HIR/BuildReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts similarity index 97% rename from compiler/forget/src/HIR/BuildReactiveFunction.ts rename to compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts index f38b160566..322a550c8a 100644 --- a/compiler/forget/src/HIR/BuildReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts @@ -6,7 +6,6 @@ */ import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; import { BlockId, HIRFunction, @@ -22,9 +21,10 @@ import { ReactiveTerminal, ReactiveValueBlock, ScopeId, -} from "./HIR"; -import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor"; -import { eachInstructionOperand } from "./visitors"; +} from "../HIR/HIR"; +import { BlockTerminal, Visitor, visitTree } from "../HIR/HIRTreeVisitor"; +import { eachInstructionOperand } from "../HIR/visitors"; +import { assertExhaustive } from "../Utils/utils"; export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction { const builder = new ReactiveFunctionBuilder(); diff --git a/compiler/forget/src/HIR/CodegenReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts similarity index 98% rename from compiler/forget/src/HIR/CodegenReactiveFunction.ts rename to compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 90cb06616f..d444d9f7bb 100644 --- a/compiler/forget/src/HIR/CodegenReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -7,7 +7,6 @@ import * as t from "@babel/types"; import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; import { codegenInstruction, codegenInstructionValue, @@ -16,7 +15,7 @@ import { convertIdentifier, createFunctionDeclaration, Temporaries, -} from "./Codegen"; +} from "../HIR/Codegen"; import { Identifier, Instruction, @@ -26,8 +25,9 @@ import { ReactiveScope, ReactiveTerminal, ReactiveValueBlock, -} from "./HIR"; -import { todoInvariant } from "./todo"; +} from "../HIR/HIR"; +import { todoInvariant } from "../Utils/todo"; +import { assertExhaustive } from "../Utils/utils"; export function codegenReactiveFunction(fn: ReactiveFunction): t.Function { const cx = new Context(); diff --git a/compiler/forget/src/HIR/FlattenReactiveLoops.ts b/compiler/forget/src/ReactiveScopes/FlattenReactiveLoops.ts similarity index 96% rename from compiler/forget/src/HIR/FlattenReactiveLoops.ts rename to compiler/forget/src/ReactiveScopes/FlattenReactiveLoops.ts index 0d8546b655..741ce7b9c9 100644 --- a/compiler/forget/src/HIR/FlattenReactiveLoops.ts +++ b/compiler/forget/src/ReactiveScopes/FlattenReactiveLoops.ts @@ -5,8 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -import { assertExhaustive } from "../Common/utils"; -import { ReactiveBlock, ReactiveFunction, ReactiveScopeBlock } from "./HIR"; +import { + ReactiveBlock, + ReactiveFunction, + ReactiveScopeBlock, +} from "../HIR/HIR"; +import { assertExhaustive } from "../Utils/utils"; /** * Given a reactive function, flattens any scopes contained within a loop construct. diff --git a/compiler/forget/src/HIR/InferReactiveScopeVariables.ts b/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts similarity index 96% rename from compiler/forget/src/HIR/InferReactiveScopeVariables.ts rename to compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts index d71dafa780..60040610b1 100644 --- a/compiler/forget/src/HIR/InferReactiveScopeVariables.ts +++ b/compiler/forget/src/ReactiveScopes/InferReactiveScopeVariables.ts @@ -6,8 +6,6 @@ */ import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; -import DisjointSet from "./DisjointSet"; import { HIRFunction, Identifier, @@ -18,8 +16,10 @@ import { Place, ReactiveScope, ScopeId, -} from "./HIR"; -import { eachInstructionOperand } from "./visitors"; +} from "../HIR/HIR"; +import { eachInstructionOperand } from "../HIR/visitors"; +import DisjointSet from "../Utils/DisjointSet"; +import { assertExhaustive } from "../Utils/utils"; /** * For each mutable variable, infers a reactive scope which will construct that diff --git a/compiler/forget/src/HIR/InferReactiveScopes.ts b/compiler/forget/src/ReactiveScopes/InferReactiveScopes.ts similarity index 97% rename from compiler/forget/src/HIR/InferReactiveScopes.ts rename to compiler/forget/src/ReactiveScopes/InferReactiveScopes.ts index 83a54be271..11c9f7ee5c 100644 --- a/compiler/forget/src/HIR/InferReactiveScopes.ts +++ b/compiler/forget/src/ReactiveScopes/InferReactiveScopes.ts @@ -6,8 +6,6 @@ */ import invariant from "invariant"; -import { retainWhere } from "../Common/utils"; -import DisjointSet from "./DisjointSet"; import { BlockId, HIRFunction, @@ -18,14 +16,16 @@ import { MutableRange, ReactiveScope, ScopeId, -} from "./HIR"; -import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor"; -import { log } from "./logger"; -import { printFunction } from "./PrintHIR"; +} from "../HIR/HIR"; +import { BlockTerminal, Visitor, visitTree } from "../HIR/HIRTreeVisitor"; +import { printFunction } from "../HIR/PrintHIR"; import { eachInstructionOperand, eachInstructionValueOperand, -} from "./visitors"; +} from "../HIR/visitors"; +import DisjointSet from "../Utils/DisjointSet"; +import { log } from "../Utils/logger"; +import { retainWhere } from "../Utils/utils"; /** * This is a second (final) stage of constructing reactive scopes. Prior to this pass, diff --git a/compiler/forget/src/HIR/PrintReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts similarity index 98% rename from compiler/forget/src/HIR/PrintReactiveFunction.ts rename to compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts index cd334cfd4a..79dc510a22 100644 --- a/compiler/forget/src/HIR/PrintReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts @@ -6,20 +6,20 @@ */ import invariant from "invariant"; -import { assertExhaustive } from "../Common/utils"; import { ReactiveFunction, ReactiveScopeBlock, ReactiveStatement, ReactiveTerminal, ReactiveValueBlock, -} from "./HIR"; +} from "../HIR/HIR"; import { printIdentifier, printInstruction, printInstructionValue, printPlace, -} from "./PrintHIR"; +} from "../HIR/PrintHIR"; +import { assertExhaustive } from "../Utils/utils"; export function printReactiveFunction(fn: ReactiveFunction): string { const writer = new Writer(); diff --git a/compiler/forget/src/HIR/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts similarity index 98% rename from compiler/forget/src/HIR/PropagateScopeDependencies.ts rename to compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index 2afc52ad95..fd8e0ad5fb 100644 --- a/compiler/forget/src/HIR/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import { assertExhaustive } from "../Common/utils"; import { Identifier, Instruction, @@ -18,8 +17,9 @@ import { ReactiveBlock, ReactiveFunction, ReactiveValueBlock, -} from "./HIR"; -import { eachInstructionValueOperand } from "./visitors"; +} from "../HIR/HIR"; +import { eachInstructionValueOperand } from "../HIR/visitors"; +import { assertExhaustive } from "../Utils/utils"; /** * Infers the dependencies of each scope to include variables whose values diff --git a/compiler/forget/src/HIR/PruneUnusedLabels.ts b/compiler/forget/src/ReactiveScopes/PruneUnusedLabels.ts similarity index 96% rename from compiler/forget/src/HIR/PruneUnusedLabels.ts rename to compiler/forget/src/ReactiveScopes/PruneUnusedLabels.ts index a4c4f7732a..940201e98f 100644 --- a/compiler/forget/src/HIR/PruneUnusedLabels.ts +++ b/compiler/forget/src/ReactiveScopes/PruneUnusedLabels.ts @@ -5,13 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -import { assertExhaustive } from "../Common/utils"; import { BlockId, ReactiveBlock, ReactiveFunction, ReactiveTerminal, -} from "./HIR"; +} from "../HIR/HIR"; +import { assertExhaustive } from "../Utils/utils"; /** * Prunes terminal labels that are never explicitly jumped to. diff --git a/compiler/forget/src/HIR/EliminateRedundantPhi.ts b/compiler/forget/src/SSA/EliminateRedundantPhi.ts similarity index 96% rename from compiler/forget/src/HIR/EliminateRedundantPhi.ts rename to compiler/forget/src/SSA/EliminateRedundantPhi.ts index 7039117e20..21c4fc81f9 100644 --- a/compiler/forget/src/HIR/EliminateRedundantPhi.ts +++ b/compiler/forget/src/SSA/EliminateRedundantPhi.ts @@ -6,8 +6,8 @@ */ import invariant from "invariant"; -import { BlockId, HIRFunction, Identifier, Place } from "./HIR"; -import { eachInstructionOperand, eachTerminalOperand } from "./visitors"; +import { BlockId, HIRFunction, Identifier, Place } from "../HIR/HIR"; +import { eachInstructionOperand, eachTerminalOperand } from "../HIR/visitors"; /** * Pass to eliminate redundant phi nodes: diff --git a/compiler/forget/src/HIR/EnterSSA.ts b/compiler/forget/src/SSA/EnterSSA.ts similarity index 96% rename from compiler/forget/src/HIR/EnterSSA.ts rename to compiler/forget/src/SSA/EnterSSA.ts index 4e2fc1733e..4c710bc282 100644 --- a/compiler/forget/src/HIR/EnterSSA.ts +++ b/compiler/forget/src/SSA/EnterSSA.ts @@ -1,4 +1,3 @@ -import { invariant } from "../CompilerError"; import { BasicBlock, HIRFunction, @@ -8,14 +7,15 @@ import { makeType, Phi, Place, -} from "./HIR"; -import { Environment } from "./HIRBuilder"; -import { printIdentifier } from "./PrintHIR"; +} from "../HIR/HIR"; +import { Environment } from "../HIR/HIRBuilder"; +import { printIdentifier } from "../HIR/PrintHIR"; import { eachTerminalSuccessor, mapInstructionOperands, mapTerminalOperands, -} from "./visitors"; +} from "../HIR/visitors"; +import { invariant } from "../Utils/CompilerError"; type IncompletePhi = { oldId: Identifier; diff --git a/compiler/forget/src/HIR/LeaveSSA.ts b/compiler/forget/src/SSA/LeaveSSA.ts similarity index 98% rename from compiler/forget/src/HIR/LeaveSSA.ts rename to compiler/forget/src/SSA/LeaveSSA.ts index 8579a63a47..06041dc60f 100644 --- a/compiler/forget/src/HIR/LeaveSSA.ts +++ b/compiler/forget/src/SSA/LeaveSSA.ts @@ -17,8 +17,11 @@ import { makeInstructionId, Phi, Place, -} from "./HIR"; -import { eachInstructionValueOperand, eachTerminalOperand } from "./visitors"; +} from "../HIR/HIR"; +import { + eachInstructionValueOperand, + eachTerminalOperand, +} from "../HIR/visitors"; /** * Removes SSA form by creating unique variable declarations for the versions of each variables. diff --git a/compiler/forget/src/CompilerError.ts b/compiler/forget/src/Utils/CompilerError.ts similarity index 100% rename from compiler/forget/src/CompilerError.ts rename to compiler/forget/src/Utils/CompilerError.ts diff --git a/compiler/forget/src/HIR/DisjointSet.ts b/compiler/forget/src/Utils/DisjointSet.ts similarity index 98% rename from compiler/forget/src/HIR/DisjointSet.ts rename to compiler/forget/src/Utils/DisjointSet.ts index 6b554dace7..c9c4bc56f3 100644 --- a/compiler/forget/src/HIR/DisjointSet.ts +++ b/compiler/forget/src/Utils/DisjointSet.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import { invariant } from "../CompilerError"; +import { invariant } from "./CompilerError"; /** * Represents items which form disjoint sets. diff --git a/compiler/forget/src/HIR/VisualizeHIRMermaid.ts b/compiler/forget/src/Utils/VisualizeHIRMermaid.ts similarity index 96% rename from compiler/forget/src/HIR/VisualizeHIRMermaid.ts rename to compiler/forget/src/Utils/VisualizeHIRMermaid.ts index dea5d6339b..397efad179 100644 --- a/compiler/forget/src/HIR/VisualizeHIRMermaid.ts +++ b/compiler/forget/src/Utils/VisualizeHIRMermaid.ts @@ -1,6 +1,6 @@ -import { assertExhaustive } from "../Common/utils"; -import { BasicBlock, BlockId, HIRFunction, Terminal } from "./HIR"; -import { printInstruction, printPlace } from "./PrintHIR"; +import { BasicBlock, BlockId, HIRFunction, Terminal } from "../HIR/HIR"; +import { printInstruction, printPlace } from "../HIR/PrintHIR"; +import { assertExhaustive } from "./utils"; const INSTRUCTIONS_NODE_NAME = "instrs"; const TERMINAL_NODE_NAME = "terminal"; diff --git a/compiler/forget/src/HIR/logger.ts b/compiler/forget/src/Utils/logger.ts similarity index 86% rename from compiler/forget/src/HIR/logger.ts rename to compiler/forget/src/Utils/logger.ts index 368dd729ea..61c79beb45 100644 --- a/compiler/forget/src/HIR/logger.ts +++ b/compiler/forget/src/Utils/logger.ts @@ -5,8 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import { HIR, HIRFunction } from "./HIR"; -import printHIR, { printFunction } from "./PrintHIR"; +import { HIR, HIRFunction } from "../HIR/HIR"; +import printHIR, { printFunction } from "../HIR/PrintHIR"; let ENABLED: boolean = false; diff --git a/compiler/forget/src/HIR/todo.ts b/compiler/forget/src/Utils/todo.ts similarity index 100% rename from compiler/forget/src/HIR/todo.ts rename to compiler/forget/src/Utils/todo.ts diff --git a/compiler/forget/src/Common/utils.ts b/compiler/forget/src/Utils/utils.ts similarity index 52% rename from compiler/forget/src/Common/utils.ts rename to compiler/forget/src/Utils/utils.ts index 264d157222..b042d9b86d 100644 --- a/compiler/forget/src/Common/utils.ts +++ b/compiler/forget/src/Utils/utils.ts @@ -5,43 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/** - * Individual util functions. - */ - -export function setEq(a: Set, b: Set): boolean { - return a.size === b.size && [...a].every((v) => b.has(v)); -} - -export function nullableSetEq( - a: Set | undefined, - b: Set | undefined -): boolean { - if (a === undefined && b === undefined) return true; - if (a === undefined || b === undefined) return false; - return a.size === b.size && [...a].every((v) => b.has(v)); -} - -export function setSubset(a: Set, b: Set): boolean { - return a.size <= b.size && [...a].every((v) => b.has(v)); -} - -export function setIntersect(a: Set, b: Set): boolean { - return [...a].some((v) => b.has(v)); -} - -export function setFirst(s: Set): T { - return [...s][0]; -} - -export function setEmpty(s: Set): boolean { - return s.size === 0; -} - -export function hasOwnProperty(obj: T, key: PropertyKey): key is keyof T { - return Object.prototype.hasOwnProperty.call(obj, key); -} - /** * Trigger an exhaustivess check in TypeScript and throw at runtime. * diff --git a/compiler/forget/src/__tests__/hir-test.ts b/compiler/forget/src/__tests__/hir-test.ts index 5dbf5341c1..57522d7e15 100644 --- a/compiler/forget/src/__tests__/hir-test.ts +++ b/compiler/forget/src/__tests__/hir-test.ts @@ -14,9 +14,9 @@ import { wasmFolder } from "@hpcc-js/wasm"; import invariant from "invariant"; import path from "path"; import prettier from "prettier"; -import { toggleLogging } from "../HIR/logger"; import run from "../HIR/Pipeline"; import { printFunction } from "../HIR/PrintHIR"; +import { toggleLogging } from "../Utils/logger"; import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures"; function wrapWithTripleBackticks(s: string, ext?: string) { diff --git a/compiler/forget/src/index.ts b/compiler/forget/src/index.ts index 3229e02553..11599dc923 100644 --- a/compiler/forget/src/index.ts +++ b/compiler/forget/src/index.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import BabelPlugin from "./BabelPlugin"; +import BabelPlugin from "./Babel/BabelPlugin"; declare global { var __DEV__: boolean | null | undefined; @@ -15,22 +15,22 @@ import { parse } from "@babel/parser"; import traverse, { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import { lower } from "./HIR/BuildHIR"; -import { buildReactiveFunction } from "./HIR/BuildReactiveFunction"; import codegen from "./HIR/Codegen"; -import { codegenReactiveFunction } from "./HIR/CodegenReactiveFunction"; -import { eliminateRedundantPhi } from "./HIR/EliminateRedundantPhi"; -import enterSSA from "./HIR/EnterSSA"; -import { flattenReactiveLoops } from "./HIR/FlattenReactiveLoops"; import { Environment } from "./HIR/HIRBuilder"; import { inferMutableRanges } from "./HIR/InferMutableRanges"; -import { inferReactiveScopes } from "./HIR/InferReactiveScopes"; -import { inferReactiveScopeVariables } from "./HIR/InferReactiveScopeVariables"; import inferReferenceEffects from "./HIR/InferReferenceEffects"; -import { leaveSSA } from "./HIR/LeaveSSA"; import printHIR, { printFunction } from "./HIR/PrintHIR"; -import { printReactiveFunction } from "./HIR/PrintReactiveFunction"; -import { propagateScopeDependencies } from "./HIR/PropagateScopeDependencies"; -import { pruneUnusedLabels } from "./HIR/PruneUnusedLabels"; +import { buildReactiveFunction } from "./ReactiveScopes/BuildReactiveFunction"; +import { codegenReactiveFunction } from "./ReactiveScopes/CodegenReactiveFunction"; +import { flattenReactiveLoops } from "./ReactiveScopes/FlattenReactiveLoops"; +import { inferReactiveScopes } from "./ReactiveScopes/InferReactiveScopes"; +import { inferReactiveScopeVariables } from "./ReactiveScopes/InferReactiveScopeVariables"; +import { printReactiveFunction } from "./ReactiveScopes/PrintReactiveFunction"; +import { propagateScopeDependencies } from "./ReactiveScopes/PropagateScopeDependencies"; +import { pruneUnusedLabels } from "./ReactiveScopes/PruneUnusedLabels"; +import { eliminateRedundantPhi } from "./SSA/EliminateRedundantPhi"; +import enterSSA from "./SSA/EnterSSA"; +import { leaveSSA } from "./SSA/LeaveSSA"; function parseFunctions( source: string