diff --git a/compiler/forget/src/HIR/Pipeline.ts b/compiler/forget/src/HIR/Pipeline.ts new file mode 100644 index 0000000000..4b73f4413c --- /dev/null +++ b/compiler/forget/src/HIR/Pipeline.ts @@ -0,0 +1,64 @@ +/** + * 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 { 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 { inferMutableRanges } from "../HIR/InferMutableLifetimes"; +import inferReferenceEffects from "../HIR/InferReferenceEffects"; +import leaveSSA from "../HIR/LeaveSSA"; +import codegen from "./Codegen"; +import { HIRFunction } from "./HIR"; +import { inferReactiveScopeVariables } from "./InferReactiveScopeVariables"; + +export type CompilerFlags = { + eliminateRedundantPhi: boolean; + inferReferenceEffects: boolean; + inferMutableRanges: boolean; + inferReactiveScopeVariables: boolean; + leaveSSA: boolean; + codegen: boolean; +}; + +export type CompilerResult = { + ir: HIRFunction; + ast: t.Function | null; +}; + +export default function ( + func: NodePath, + flags: CompilerFlags +): CompilerResult { + const env = new Environment(); + const ir = lower(func, env); + enterSSA(ir, env); + if (flags.eliminateRedundantPhi) { + eliminateRedundantPhi(ir); + } + if (flags.inferReferenceEffects) { + inferReferenceEffects(ir); + } + if (flags.inferMutableRanges) { + inferMutableRanges(ir); + } + if (flags.inferReactiveScopeVariables) { + inferReactiveScopeVariables(ir); + } + if (flags.leaveSSA) { + leaveSSA(ir); + } + if (flags.codegen) { + return { + ast: codegen(ir), + ir: ir, + }; + } + + return { ast: null, ir: ir }; +} diff --git a/compiler/forget/src/__tests__/hir-test.ts b/compiler/forget/src/__tests__/hir-test.ts index dccc49c0ea..bb2e38c947 100644 --- a/compiler/forget/src/__tests__/hir-test.ts +++ b/compiler/forget/src/__tests__/hir-test.ts @@ -14,18 +14,9 @@ import { wasmFolder } from "@hpcc-js/wasm"; import invariant from "invariant"; import path from "path"; import prettier from "prettier"; -import { lower } from "../HIR/BuildHIR"; -import codegen from "../HIR/Codegen"; -import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi"; -import enterSSA from "../HIR/EnterSSA"; -import { HIRFunction } from "../HIR/HIR"; -import { Environment } from "../HIR/HIRBuilder"; -import { inferMutableRanges } from "../HIR/InferMutableLifetimes"; -import { inferReactiveScopeVariables } from "../HIR/InferReactiveScopeVariables"; -import inferReferenceEffects from "../HIR/InferReferenceEffects"; -import leaveSSA from "../HIR/LeaveSSA"; import printHIR from "../HIR/PrintHIR"; import visualizeHIRMermaid from "../HIR/VisualizeHIRMermaid"; +import run from "./../HIR/Pipeline"; import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures"; function wrapWithTripleBackticks(s: string, ext?: string) { @@ -65,18 +56,23 @@ describe("React Forget (HIR version)", () => { traverse(ast, { FunctionDeclaration: { enter(nodePath) { - const env: Environment = new Environment(); - const ir: HIRFunction = lower(nodePath, env); - enterSSA(ir, env); - eliminateRedundantPhi(ir); - inferReferenceEffects(ir); - inferMutableRanges(ir); - inferReactiveScopeVariables(ir); - leaveSSA(ir); + const compilerFlags = { + eliminateRedundantPhi: true, + inferReferenceEffects: true, + inferMutableRanges: true, + leaveSSA: true, + inferReactiveScopeVariables: true, + codegen: true, + }; + + const { ast, ir } = run(nodePath, compilerFlags); + invariant( + ast !== null, + "ast is null when codegen option is enabled" + ); + const textHIR = printHIR(ir.body); const visualization = visualizeHIRMermaid(ir); - - const ast = codegen(ir); const text = prettier.format( generate(ast).code.replace("\n\n", "\n"), {