diff --git a/compiler/forget/packages/playground/components/Editor/Output.tsx b/compiler/forget/packages/playground/components/Editor/Output.tsx index 0c9e7536d2..e91abd7498 100644 --- a/compiler/forget/packages/playground/components/Editor/Output.tsx +++ b/compiler/forget/packages/playground/components/Editor/Output.tsx @@ -20,6 +20,8 @@ import type { Store } from "../../lib/stores"; import TabbedWindow, { TabTypes } from "../TabbedWindow"; import { monacoOptions } from "./monacoOptions"; const { + buildReactiveFunction, + printReactiveFunction, parseFunctions, Environment, enterSSA, @@ -54,6 +56,7 @@ type CompilerOutput = { inferReactiveScopeVariablesOutput: string; inferReactiveScopesOutput: string; inferReactiveScopeDependenciesOutput: string; + reactiveFunctionOutput: string; leaveSSAOutput: string; codegenOutput: string; sourceMapUrl: string | null; @@ -99,6 +102,9 @@ function compile(source: string): CompilerOutput | CompilerError { inferReactiveScopeDependencies(ir); const inferReactiveScopeDependenciesOutput = printHIR(ir.body); + const reactiveFunction = buildReactiveFunction(ir); + const reactiveFunctionOutput = printReactiveFunction(reactiveFunction); + codegen(ir); const ast = codegen(ir); const generated = generate( @@ -128,6 +134,7 @@ function compile(source: string): CompilerOutput | CompilerError { inferReactiveScopeDependenciesOutput, inferReactiveScopeVariablesOutput, inferReactiveScopesOutput, + reactiveFunctionOutput, leaveSSAOutput, codegenOutput, sourceMapUrl, @@ -203,6 +210,11 @@ function Output({ store, setTabsOpen, tabsOpen }: Props) { output={compilerOutput.inferReactiveScopeDependenciesOutput} > ), + ReactiveFunctions: ( + + ), JS: , SourceMap: ( <> diff --git a/compiler/forget/packages/playground/components/TabbedWindow.tsx b/compiler/forget/packages/playground/components/TabbedWindow.tsx index 116e3548fc..545249da36 100644 --- a/compiler/forget/packages/playground/components/TabbedWindow.tsx +++ b/compiler/forget/packages/playground/components/TabbedWindow.tsx @@ -16,6 +16,7 @@ export type TabTypes = | "InferReactiveScopeVariables" | "InferReactiveScopes" | "InferReactiveScopeDependencies" + | "ReactiveFunctions" | "LeaveSSA" | "JS" | "SourceMap" diff --git a/compiler/forget/src/HIR/BuildReactiveFunction.ts b/compiler/forget/src/HIR/BuildReactiveFunction.ts new file mode 100644 index 0000000000..ee97927297 --- /dev/null +++ b/compiler/forget/src/HIR/BuildReactiveFunction.ts @@ -0,0 +1,394 @@ +/** + * 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 invariant from "invariant"; +import { assertExhaustive } from "../Common/utils"; +import { + BlockId, + HIRFunction, + Instruction, + InstructionId, + InstructionValue, + Place, + ReactiveBasicBlock, + ReactiveBlock, + ReactiveFunction, + ReactiveInstruction, + ReactiveScope, + ReactiveTerminal, + ReactiveValueBlock, + ScopeId, +} from "./HIR"; +import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor"; +import { eachInstructionOperand } from "./visitors"; + +export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction { + const builder = new ReactiveFunctionBuilder(); + const body = visitTree(fn, builder); + invariant(body != null, "Expected a root block"); + return { + loc: fn.loc, + id: fn.id, + params: fn.params, + generator: fn.generator, + async: fn.async, + body, + }; +} + +type BlockKind = + | { kind: "block"; block: ReactiveBasicBlock } + | { kind: "scope"; block: ReactiveBasicBlock; scope: ReactiveScope }; + +class Builder { + #instructions: ReactiveBasicBlock; + #stack: Array< + | { kind: "scope"; block: ReactiveBlock } + | { kind: "block"; block: ReactiveBasicBlock } + >; + + constructor() { + const block: ReactiveBasicBlock = []; + this.#instructions = block; + this.#stack = [{ kind: "block", block }]; + } + + append(item: ReactiveInstruction, label: BlockId | undefined): void { + if (label !== undefined) { + invariant(item.kind === "terminal", "Only terminals may have a label"); + item.label = label; + } + this.#instructions.push(item); + } + + startScope(scope: ReactiveScope): void { + const block: ReactiveBlock = { + kind: "block", + id: scope.id, + range: scope.range, + dependencies: scope.dependencies, + instructions: [], + }; + this.append(block, undefined); + this.#instructions = block.instructions; + this.#stack.push({ kind: "scope", block }); + } + + visitId(id: InstructionId): void { + for (let i = 0; i < this.#stack.length; i++) { + const scope = this.#stack[i]!; + if (scope.kind === "scope" && id >= scope.block.range.end) { + this.#stack.length = i; + break; + } + } + const last = this.#stack[this.#stack.length - 1]!; + if (last.kind === "block") { + this.#instructions = last.block; + } else { + this.#instructions = last.block.instructions; + } + } + + complete(): ReactiveBasicBlock { + // TODO @josephsavona: debug two failures of this + // invariant( + // this.#stack.length === 1, + // "Expected all scopes to be closed when exiting a block" + // ); + const first = this.#stack[0]!; + invariant( + first.kind === "block", + "Expected first stack item to be a basic block" + ); + return first.block; + } +} + +class ReactiveFunctionBuilder + implements + Visitor< + Builder, + ReactiveBasicBlock, + ReactiveValueBlock, + ReactiveValueBlock, + InstructionValue | ReactiveValueBlock, + ReactiveInstruction, + { test: InstructionValue | null; block: ReactiveBasicBlock } + > +{ + #builders: Array = []; + #scopes: Set = new Set(); + + visitId(id: InstructionId): void { + const builder = this.#builders[this.#builders.length - 1]!; + builder.visitId(id); + } + + enterBlock(): Builder { + const builder = new Builder(); + this.#builders.push(builder); + return builder; + } + appendBlock( + block: Builder, + item: ReactiveInstruction, + label?: BlockId | undefined + ): void { + block.append(item, label); + } + leaveBlock(block: Builder): ReactiveBasicBlock { + const builder = this.#builders.pop(); + invariant( + builder === block, + "Expected enterBlock/leaveBlock to be called 1:1" + ); + return block.complete(); + } + + enterValueBlock(block: Builder): ReactiveValueBlock { + return { + kind: "value-block", + instructions: [], + value: null, + }; + } + appendValueBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void { + block.instructions.push(item); + } + leaveValueBlock( + block: ReactiveValueBlock, + value: InstructionValue | ReactiveValueBlock | null + ): InstructionValue | ReactiveValueBlock { + if (value !== null) { + invariant( + value.kind !== "value-block", + "Expected value block to end in a value" + ); + block.value = value; + } + return block; + } + + enterInitBlock(block: Builder): ReactiveValueBlock { + return this.enterValueBlock(block); + } + appendInitBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void { + this.appendValueBlock(block, item); + } + leaveInitBlock(block: ReactiveValueBlock): ReactiveValueBlock { + return block; + } + + visitValue( + value: InstructionValue, + id: InstructionId + ): InstructionValue | ReactiveValueBlock { + return value; + } + visitInstruction( + instruction: Instruction, + value: InstructionValue | ReactiveValueBlock + ): ReactiveInstruction { + this.visitId(instruction.id); + const scope = getInstructionScope(instruction); + if (scope !== null && !this.#scopes.has(scope.id)) { + this.#scopes.add(scope.id); + const builder = this.#builders[this.#builders.length - 1]!; + builder.startScope(scope); + } + return { kind: "instruction", instruction }; + } + visitTerminalId(id: InstructionId): void { + this.visitId(id); + } + visitImplicitTerminal(): ReactiveInstruction | null { + return null; + } + visitTerminal( + terminal: BlockTerminal< + ReactiveValueBlock, + InstructionValue | ReactiveValueBlock, + ReactiveBasicBlock, + { test: InstructionValue | null; block: ReactiveBasicBlock } + > + ): ReactiveInstruction { + let result: ReactiveTerminal; + switch (terminal.kind) { + case "break": { + result = { kind: "break", label: terminal.label }; + break; + } + case "continue": { + result = { kind: "continue", label: terminal.label }; + break; + } + case "for": { + const { test, update } = terminal; + result = { + kind: "for", + init: terminal.init, + test: terminal.test as ReactiveValueBlock, + update: terminal.update as ReactiveValueBlock, + loop: terminal.loop, + }; + break; + } + case "if": { + result = { + kind: "if", + test: terminal.test as Place, + consequent: terminal.consequent, + alternate: terminal.alternate, + }; + break; + } + case "return": { + const value = terminal.value; + if (value !== null && value.kind !== "Identifier") { + invariant(false, "Expected return to be a Place"); + } + result = { kind: "return", value }; + break; + } + case "switch": { + result = { + kind: "switch", + test: terminal.test as Place, + cases: terminal.cases as Array<{ + test: Place | null; + block: ReactiveBasicBlock | void; + }>, + }; + break; + } + case "throw": { + result = { kind: "throw", value: terminal.value as Place }; + break; + } + case "while": { + result = { + kind: "while", + test: terminal.test as ReactiveValueBlock, + loop: terminal.loop, + }; + break; + } + default: { + assertExhaustive( + terminal, + `Unexpected terminal kind '${(terminal as any).kind}'` + ); + } + } + return { + kind: "terminal", + terminal: result, + label: null, + }; + } + visitCase( + test: InstructionValue | ReactiveValueBlock | null, + block: ReactiveBasicBlock + ): { test: InstructionValue | null; block: ReactiveBasicBlock } { + if (test !== null && test.kind !== "Identifier") { + invariant(false, "Expected a Place"); + } + return { test, block }; + } +} + +function getInstructionScope(instr: Instruction): ReactiveScope | null { + let scope: ReactiveScope | null = null; + if ( + instr.lvalue !== null && + instr.lvalue.place.identifier.scope !== null && + isScopeActive(instr.lvalue.place.identifier.scope, instr.id) + ) { + scope = instr.lvalue.place.identifier.scope; + } else { + for (const operand of eachInstructionOperand(instr)) { + if ( + operand.identifier.scope !== null && + isScopeActive(operand.identifier.scope, instr.id) + ) { + scope = operand.identifier.scope; + break; + } + } + } + if ( + scope !== null && + (scope.range.end > scope.range.start + 1 || mayAllocate(instr.value)) + ) { + return scope; + } + return null; +} + +function mayAllocate(value: InstructionValue): boolean { + switch (value.kind) { + case "BinaryExpression": + case "Identifier": + case "JSXText": + case "Primitive": { + return false; + } + case "ArrayExpression": + case "CallExpression": + case "JsxExpression": + case "JsxFragment": + case "NewExpression": + case "ObjectExpression": + case "OtherStatement": + case "UnaryExpression": { + return true; + } + default: { + assertExhaustive(value, `Unexpected value kind '${(value as any).kind}'`); + } + } +} + +function isScopeActive(scope: ReactiveScope, id: InstructionId): boolean { + return id >= scope.range.start && id < scope.range.end; +} + +class ArraySet { + items: Array = []; + set: Set = new Set(); + + add(item: T) { + if (!this.set.has(item)) { + this.set.add(item); + this.items.push(item); + } + } + + delete(item: T) { + if (this.set.has(item)) { + this.set.delete(item); + this.items.splice( + this.items.findIndex((x) => x === item), + 1 + ); + } + } + + has(item: T): boolean { + return this.set.has(item); + } + + last(): T { + invariant(this.items.length > 0, "Called ArraySet::last on empty set"); + return this.items[this.items.length - 1]; + } + + *[Symbol.iterator]() { + yield* this.items; + } +} diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 95d5e7f6ae..ecc95d53b3 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -14,47 +14,93 @@ import { invariant } from "../CompilerError"; // ******************************************************************************************* // ******************************************************************************************* -// option 1: control dependencies using CFG (done in Joe's PR) -// option 2: reachability (did execution reach this point last time, if not calculate) -// option 3: push change variable calculation to point of use (more allocations) -// option 4: compute intermediates - -// AST -> (lowering) -> HIR -> (dep analysis) -> Reactive Scopes -> (scheduling?) -> HIR -> (codegen) -> AST +// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (scheduling?) -> HIR -> (codegen) -> AST /** * A location in a source file, intended to be used for providing diagnostic information and * transforming code while preserving source information (ie to emit source maps). * * `GeneratedSource` indicates that there is no single source location from which the code derives. - * */ export const GeneratedSource = Symbol(); export type SourceLocation = t.SourceLocation | typeof GeneratedSource; /** - * A React function defines a computation that takes some set of reactive - * inputs (eg props, hook arguments) and returns a result (JSX, hook return - * value). It is composed of one or more Reactive Scopes, where each - * Reactive Scope takes a set of reactive inputs and produces one *or more* - * result values. The idea is that each Reactive Scope is responsible for - * producing its result values whenever its inputs change: scopes contain - * instructions to produce the result values, deriving from the inputs - * as well as from the results of other scopes. + * A React function defines a computation that takes some set of reactive inputs + * (props, hook arguments) and return a result (JSX, hook return value). Unlike + * HIR, the data model is tree-shaped: * - * A React function comprises a Reactive Scope responsible for producing the - * return value of the function, plus additional scopes that the return scopes - * may depend upon (transitively). + * ReactFunction + * ReactiveBlock + * ReactiveBlockScope* + * Place* (dependencies) + * (ReactiveInstruction | ReactiveTerminal)* + * + * Where ReactiveTerminal may recursively contain zero or more ReactiveBlocks. + * + * Each ReactiveBlockScope describes a set of dependencies as well as the instructions (and terminals) + * within that scope. */ -export type ReactFunction = { +export type ReactiveFunction = { loc: SourceLocation; id: Identifier | null; params: Array; - returnScope: ScopeId; - scopes: Map; + generator: boolean; + async: boolean; + body: ReactiveBasicBlock; }; +export type ReactiveBlock = { + kind: "block"; + id: ScopeId; + range: MutableRange; + dependencies: Set; + instructions: ReactiveBasicBlock; +}; + +export type ReactiveBasicBlock = Array; + +export type ReactiveValueBlock = { + kind: "value-block"; + instructions: ReactiveBasicBlock; + value: InstructionValue | null; +}; + +export type ReactiveInstruction = + | { kind: "instruction"; instruction: Instruction } + | { kind: "terminal"; terminal: ReactiveTerminal; label: BlockId | null } + | ReactiveBlock; + +export type ReactiveTerminal = + | { kind: "break"; label: BlockId | null } + | { kind: "continue"; label: BlockId | null } + | { kind: "return"; value: Place | null } + | { kind: "throw"; value: Place } + | { + kind: "switch"; + test: Place; + cases: Array<{ + test: Place | null; + block: ReactiveBasicBlock | void; + }>; + } + | { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBasicBlock } + | { + kind: "for"; + init: ReactiveValueBlock; + test: ReactiveValueBlock; + update: ReactiveValueBlock; + loop: ReactiveBasicBlock; + } + | { + kind: "if"; + test: Place; + consequent: ReactiveBasicBlock; + alternate: ReactiveBasicBlock | null; + }; + /** - * A function declaration including its path + * A function lowered to HIR form, ie where its body is lowered to an HIR control-flow graph */ export type HIRFunction = { loc: SourceLocation; diff --git a/compiler/forget/src/HIR/InferReactiveScopeVariables.ts b/compiler/forget/src/HIR/InferReactiveScopeVariables.ts index ddb5ff1917..1739af085d 100644 --- a/compiler/forget/src/HIR/InferReactiveScopeVariables.ts +++ b/compiler/forget/src/HIR/InferReactiveScopeVariables.ts @@ -77,15 +77,15 @@ export function inferReactiveScopeVariables(fn: HIRFunction) { for (const instr of block.instructions) { const operands: Array = []; if (instr.lvalue !== null) { - // invariant( - // isMutable(instr, instr.lvalue!.place), - // "Assignment always means the value is mutable:\n" + - // printMixedHIR(instr) - // ); operands.push(instr.lvalue!.place.identifier); } for (const operand of eachInstructionOperand(instr)) { - if (isMutable(instr, operand)) { + if ( + isMutable(instr, operand) && + // exclude global variables from being added to scopes, we can't recreate them! + // TODO: improve handling of module-scoped variables and globals + operand.identifier.mutableRange.start > 0 + ) { operands.push(operand.identifier); } } diff --git a/compiler/forget/src/HIR/PrintReactiveFunction.ts b/compiler/forget/src/HIR/PrintReactiveFunction.ts new file mode 100644 index 0000000000..80bfdd1f49 --- /dev/null +++ b/compiler/forget/src/HIR/PrintReactiveFunction.ts @@ -0,0 +1,204 @@ +/** + * 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 invariant from "invariant"; +import { assertExhaustive } from "../Common/utils"; +import { + ReactiveBlock, + ReactiveFunction, + ReactiveInstruction, + ReactiveTerminal, + ReactiveValueBlock, +} from "./HIR"; +import { + printInstruction, + printInstructionValue, + printPlace, +} from "./PrintHIR"; + +export function printReactiveFunction(fn: ReactiveFunction): string { + const writer = new Writer(); + writer.writeLine(`function ${fn.id?.name ?? ""}(`); + writer.indented(() => { + for (const param of fn.params) { + writer.writeLine(`${param.identifier.name ?? ""},`); + } + }); + writer.writeLine(") {"); + printReactiveInstructions(writer, fn.body); + writer.writeLine("}"); + return writer.complete(); +} + +export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void { + writer.writeLine( + `scope @${block.id} [${block.range.start}:${ + block.range.end + }] deps=[${Array.from(block.dependencies) + .map((dep) => printPlace(dep)) + .join(", ")}] {` + ); + printReactiveInstructions(writer, block.instructions); + writer.writeLine("}"); +} + +export function printReactiveInstructions( + writer: Writer, + instructions: Array +): void { + writer.indented(() => { + for (const instr of instructions) { + printReactiveInstruction(writer, instr); + } + }); +} + +function printReactiveInstruction( + writer: Writer, + instr: ReactiveInstruction +): void { + switch (instr.kind) { + case "instruction": { + writer.writeLine(printInstruction(instr.instruction)); + break; + } + case "block": { + printReactiveBlock(writer, instr); + break; + } + case "terminal": { + printTerminal(writer, instr.terminal); + break; + } + default: { + assertExhaustive( + instr, + `Unexpected terminal kind '${(instr as any).kind}'` + ); + } + } +} + +function printValueBlock(writer: Writer, block: ReactiveValueBlock): void { + writer.indented(() => { + for (const instr of block.instructions) { + printReactiveInstruction(writer, instr); + } + if (block.value !== null) { + writer.writeLine(printInstructionValue(block.value)); + } + }); +} + +function printTerminal(writer: Writer, terminal: ReactiveTerminal): void { + switch (terminal.kind) { + case "break": { + if (terminal.label !== null) { + writer.writeLine(`break bb${terminal.label}`); + } else { + writer.writeLine(`break`); + } + break; + } + case "continue": { + if (terminal.label !== null) { + writer.writeLine(`continue bb${terminal.label}`); + } else { + writer.writeLine(`continue`); + } + break; + } + case "while": { + writer.writeLine(`while (`); + printValueBlock(writer, terminal.test); + writer.writeLine(") {"); + printReactiveInstructions(writer, terminal.loop); + writer.writeLine("}"); + break; + } + case "if": { + const { test, consequent, alternate } = terminal; + writer.writeLine(`if (${printPlace(test)}) {`); + printReactiveInstructions(writer, consequent); + if (alternate !== null) { + writer.writeLine("} else {"); + printReactiveInstructions(writer, alternate); + } + writer.writeLine("}"); + break; + } + case "switch": { + writer.writeLine(`switch (${printPlace(terminal.test)}) {`); + writer.indented(() => { + for (const case_ of terminal.cases) { + let prefix = + case_.test !== null ? `case ${printPlace(case_.test)}` : "default"; + writer.writeLine(`${prefix}: {`); + writer.indented(() => { + const block = case_.block; + invariant(block != null, "Expected case to have a block"); + printReactiveInstructions(writer, block); + }); + writer.writeLine("}"); + } + }); + writer.writeLine("}"); + break; + } + case "for": { + writer.writeLine("for ("); + printValueBlock(writer, terminal.init); + writer.writeLine(";"); + printValueBlock(writer, terminal.test); + writer.writeLine(";"); + printValueBlock(writer, terminal.update); + writer.writeLine(") {"); + printReactiveInstructions(writer, terminal.loop); + writer.writeLine("}"); + break; + } + case "throw": { + writer.writeLine(`throw ${printPlace(terminal.value)}`); + break; + } + case "return": { + if (terminal.value !== null) { + writer.writeLine(`return ${printPlace(terminal.value)}`); + } else { + writer.writeLine("return"); + } + break; + } + } +} + +export class Writer { + #out: Array = []; + #depth: number; + + constructor({ depth }: { depth: number } = { depth: 0 }) { + this.#depth = depth; + } + + complete(): string { + return this.#out.join(""); + } + + write(s: string): void { + this.#out.push(" ".repeat(this.#depth) + s); + } + + writeLine(s: string): void { + this.#out.push(" ".repeat(this.#depth) + s + "\n"); + } + + indented(f: () => void): void { + this.#depth++; + f(); + this.#depth--; + } +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.expect.md b/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.expect.md new file mode 100644 index 0000000000..d12e73b42f --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.expect.md @@ -0,0 +1,110 @@ + +## Input + +```javascript +/** + * props.b *does* influence `a` + */ +function Component(props) { + const a = []; + a.push(props.a); + label: { + if (props.b) { + break label; + } + a.push(props.c); + } + a.push(props.d); + return a; +} + +``` + +## HIR + +``` +bb0: + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + [3] If (read props$3.b) then:bb1 else:bb2 fallthrough=bb2 +bb2: + predecessor blocks: bb0 + [4] Call mutate a$4_@0.push(read props$3.c) + [5] Goto bb1 +bb1: + predecessor blocks: bb0 bb2 + [6] Call mutate a$4_@0.push(read props$3.d) + [7] Return freeze a$4_@0 +scope0 [1:7]: + - dependency: read props$3.a + - dependency: read props$3.d + - dependency: read props$3.c +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$3.a, read props$3.d, read props$3.c] { + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + if (read props$3.b) { + [6] Call mutate a$4_@0.push(read props$3.d) + return freeze a$4_@0 + } + [4] Call mutate a$4_@0.push(read props$3.c) + } +} + +``` + +### CFG + +```mermaid +flowchart TB + %% Basic Blocks + subgraph bb0 + bb0_instrs[" + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + "] + bb0_instrs --> bb0_terminal(["If (read props$3.b)"]) + end + subgraph bb2 + bb2_instrs[" + [4] Call mutate a$4_@0.push(read props$3.c) + "] + bb2_instrs --> bb2_terminal(["Goto"]) + end + subgraph bb1 + bb1_instrs[" + [6] Call mutate a$4_@0.push(read props$3.d) + "] + bb1_instrs --> bb1_terminal(["Return freeze a$4_@0"]) + end + + %% Jumps + bb0_terminal -- "then" --> bb1 + bb0_terminal -- "else" --> bb2 + bb2_terminal --> bb1 + +``` + +## Code + +```javascript +function Component$0(props$3) { + const a$4 = []; + a$4.push(props$3.a); + bb2: if (props$3.b) { + a$4.push(props$3.d); + return a$4; + } + + a$4.push(props$3.c); +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.js b/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.js new file mode 100644 index 0000000000..0028ebbc9a --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/_bug_conditional-break-labeled.js @@ -0,0 +1,15 @@ +/** + * props.b *does* influence `a` + */ +function Component(props) { + const a = []; + a.push(props.a); + label: { + if (props.b) { + break label; + } + a.push(props.c); + } + a.push(props.d); + return a; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md index 75414ab485..ae9bd9de3f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md @@ -28,6 +28,20 @@ scope3 [4:5]: - dependency: read x$5_@1 ``` +## Reactive Scopes + +``` +function f( +) { + [1] Const mutate x$4_@0 = 1 + [2] Const mutate x$5_@1 = 2 + [3] Const mutate $6_@2 = Binary read x$5_@1 + read x$5_@1 + [4] Const mutate $7_@3 = Binary read $6_@2 + read x$5_@1 + return read $7_@3 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_inverted-if-else.expect.md b/compiler/forget/src/__tests__/fixtures/hir/_bug_inverted-if-else.expect.md index 5bc24db254..7659b17c09 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug_inverted-if-else.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/_bug_inverted-if-else.expect.md @@ -39,6 +39,23 @@ scope2 [5:6]: - dependency: read c$7 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + [1] Const mutate x$8_@0 = null + if (read a$5) { + [3] Const mutate x$9_@1 = read b$6 + } + [5] Const mutate x$10_@2 = read c$7 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md index 4dacc57104..39c269cd07 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md @@ -17,16 +17,34 @@ function component() { ``` bb0: - [1] Const mutate z$5_@0[0:7] = Array [] - [2] Const mutate y$6_@0[0:7] = Object { } - [3] Reassign mutate y$6_@0.z[0:7] = read z$5_@0 - [4] Const mutate x$7_@0[0:7] = Object { } - [5] Reassign mutate x$7_@0.y[0:7] = read y$6_@0 - [6] Call mutate mutate$4_@0(mutate x$7_@0.y.z) + [1] Const mutate z$5_@0[1:7] = Array [] + [2] Const mutate y$6_@0[1:7] = Object { } + [3] Reassign mutate y$6_@0.z[1:7] = read z$5_@0 + [4] Const mutate x$7_@0[1:7] = Object { } + [5] Reassign mutate x$7_@0.y[1:7] = read y$6_@0 + [6] Call mutate mutate$4(mutate x$7_@0.y.z) [7] Return ``` +## Reactive Scopes + +``` +function component( +) { + scope @0 [1:7] deps=[] { + [1] Const mutate z$5_@0[1:7] = Array [] + [2] Const mutate y$6_@0[1:7] = Object { } + [3] Reassign mutate y$6_@0.z[1:7] = read z$5_@0 + [4] Const mutate x$7_@0[1:7] = Object { } + [5] Reassign mutate x$7_@0.y[1:7] = read y$6_@0 + [6] Call mutate mutate$4(mutate x$7_@0.y.z) + } + return +} + +``` + ### CFG ```mermaid @@ -34,12 +52,12 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate z$5_@0[0:7] = Array [] - [2] Const mutate y$6_@0[0:7] = Object { } - [3] Reassign mutate y$6_@0.z[0:7] = read z$5_@0 - [4] Const mutate x$7_@0[0:7] = Object { } - [5] Reassign mutate x$7_@0.y[0:7] = read y$6_@0 - [6] Call mutate mutate$4_@0(mutate x$7_@0.y.z) + [1] Const mutate z$5_@0[1:7] = Array [] + [2] Const mutate y$6_@0[1:7] = Object { } + [3] Reassign mutate y$6_@0.z[1:7] = read z$5_@0 + [4] Const mutate x$7_@0[1:7] = Object { } + [5] Reassign mutate x$7_@0.y[1:7] = read y$6_@0 + [6] Call mutate mutate$4(mutate x$7_@0.y.z) "] bb0_instrs --> bb0_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md index 0059fc662e..0f80b22002 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path.expect.md @@ -29,6 +29,27 @@ scope2 [4:6]: - dependency: read y$5_@1 ``` +## Reactive Scopes + +``` +function component( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate z$4_@0 = Array [] + } + scope @1 [2:4] deps=[read z$4_@0] { + [2] Const mutate y$5_@1[2:4] = Object { } + [3] Reassign mutate y$5_@1.z[2:4] = read z$4_@0 + } + scope @2 [4:6] deps=[read y$5_@1] { + [4] Const mutate x$6_@2[4:6] = Object { } + [5] Reassign mutate x$6_@2.y[4:6] = read y$5_@1 + } + return freeze x$6_@2 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md index 70ddeda17b..bae0db20ab 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md @@ -27,20 +27,20 @@ function mutate(x, y) {} ``` bb0: - [1] Let mutate a$8_@0[0:12] = Object { } - [2] Let mutate b$9_@0[0:12] = Object { } - [3] Let mutate c$10_@0[0:12] = Object { } + [1] Let mutate a$8_@0[1:12] = Object { } + [2] Let mutate b$9_@0[1:12] = Object { } + [3] Let mutate c$10_@0[1:12] = Object { } [4] While test=bb1 loop=bb3 fallthrough=bb2 bb1: predecessor blocks: bb0 bb3 [5] If (read cond$7) then:bb3 else:bb2 fallthrough=bb2 bb3: predecessor blocks: bb1 - [6] Const mutate z$13_@0[0:12] = read a$8_@0 - [7] Reassign mutate a$8_@0[0:12] = read b$9_@0 - [8] Reassign mutate b$9_@0[0:12] = read c$10_@0 - [9] Reassign mutate c$10_@0[0:12] = read z$13_@0 - [10] Call mutate mutate$6_@0(mutate a$8_@0, mutate b$9_@0) + [6] Const mutate z$13_@0[1:12] = read a$8_@0 + [7] Reassign mutate a$8_@0[1:12] = read b$9_@0 + [8] Reassign mutate b$9_@0[1:12] = read c$10_@0 + [9] Reassign mutate c$10_@0[1:12] = read z$13_@0 + [10] Call mutate mutate$6(mutate a$8_@0, mutate b$9_@0) [11] Goto(Continue) bb1 bb2: predecessor blocks: bb1 @@ -48,6 +48,35 @@ bb2: [13] read b$9_@0 [14] read c$10_@0 [15] Return freeze a$8_@0 +scope0 [1:12]: + - dependency: read cond$7 +``` + +## Reactive Scopes + +``` +function foo( + cond, +) { + scope @0 [1:12] deps=[read cond$7] { + [1] Let mutate a$8_@0[1:12] = Object { } + [2] Let mutate b$9_@0[1:12] = Object { } + [3] Let mutate c$10_@0[1:12] = Object { } + while ( + read cond$7 + ) { + [6] Const mutate z$13_@0[1:12] = read a$8_@0 + [7] Reassign mutate a$8_@0[1:12] = read b$9_@0 + [8] Reassign mutate b$9_@0[1:12] = read c$10_@0 + [9] Reassign mutate c$10_@0[1:12] = read z$13_@0 + [10] Call mutate mutate$6(mutate a$8_@0, mutate b$9_@0) + } + } + [12] read a$8_@0 + [13] read b$9_@0 + [14] read c$10_@0 + return freeze a$8_@0 +} ``` @@ -58,9 +87,9 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Let mutate a$8_@0[0:12] = Object { } - [2] Let mutate b$9_@0[0:12] = Object { } - [3] Let mutate c$10_@0[0:12] = Object { } + [1] Let mutate a$8_@0[1:12] = Object { } + [2] Let mutate b$9_@0[1:12] = Object { } + [3] Let mutate c$10_@0[1:12] = Object { } "] bb0_instrs --> bb0_terminal(["While"]) end @@ -69,11 +98,11 @@ flowchart TB end subgraph bb3 bb3_instrs[" - [6] Const mutate z$13_@0[0:12] = read a$8_@0 - [7] Reassign mutate a$8_@0[0:12] = read b$9_@0 - [8] Reassign mutate b$9_@0[0:12] = read c$10_@0 - [9] Reassign mutate c$10_@0[0:12] = read z$13_@0 - [10] Call mutate mutate$6_@0(mutate a$8_@0, mutate b$9_@0) + [6] Const mutate z$13_@0[1:12] = read a$8_@0 + [7] Reassign mutate a$8_@0[1:12] = read b$9_@0 + [8] Reassign mutate b$9_@0[1:12] = read c$10_@0 + [9] Reassign mutate c$10_@0[1:12] = read z$13_@0 + [10] Call mutate mutate$6(mutate a$8_@0, mutate b$9_@0) "] bb3_instrs --> bb3_terminal(["Goto"]) end @@ -126,6 +155,18 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( + x, + y, +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md index d19420aa34..5b9c9a691b 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md @@ -39,6 +39,23 @@ scope6 [7:8]: - dependency: read $10_@5 ``` +## Reactive Scopes + +``` +function f( +) { + [1] Const mutate x$5_@0 = 1 + [2] Const mutate $6_@1 = 1 + [3] Const mutate x$7_@2 = Binary read x$5_@0 + read $6_@1 + [4] Const mutate $8_@3 = 1 + [5] Const mutate x$9_@4 = Binary read x$7_@2 + read $8_@3 + [6] Const mutate $10_@5 = 1 + [7] Const mutate x$11_@6 = Binary read x$9_@4 >>> read $10_@5 + return +} + +``` + ### CFG ```mermaid @@ -85,6 +102,23 @@ scope2 [3:4]: - dependency: mutate a$4_@1.b.c ``` +## Reactive Scopes + +``` +function g( + a, +) { + [1] Const mutate $5_@0 = 1 + scope @1 [0:5] deps=[] { + [2] Reassign mutate a$4_@1.b.c[0:5] = Binary read a$4_@1.b.c + read $5_@0 + [3] Const mutate $6_@2 = 2 + [4] Reassign mutate a$4_@1.b.c[0:5] = Binary read a$4_@1.b.c * read $6_@2 + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/call.expect.md index 5cabd7f171..54c89ed590 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/call.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/call.expect.md @@ -23,6 +23,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -46,12 +56,12 @@ function foo$0() {} ``` bb0: - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] Call mutate foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = "div" [5] Const mutate _$13_@2 = JSX - [6] Call mutate foo$4_@0(mutate b$11_@0) + [6] Call mutate foo$4(mutate b$11_@0) [7] Const mutate $14_@3 = "div" [8] Const mutate $15_@4 = JSX [9] Return read $15_@4 @@ -63,6 +73,31 @@ scope4 [8:9]: - dependency: freeze b$11_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[] { + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) + [4] Const mutate $12_@1 = "div" + scope @2 [5:6] deps=[read $12_@1] { + [5] Const mutate _$13_@2 = JSX + } + [6] Call mutate foo$4(mutate b$11_@0) + } + [7] Const mutate $14_@3 = "div" + scope @4 [8:9] deps=[read $14_@3, read a$10_@0, freeze b$11_@0] { + [8] Const mutate $15_@4 = JSX + } + return read $15_@4 +} + +``` + ### CFG ```mermaid @@ -70,12 +105,12 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] Call mutate foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = 'div' [5] Const mutate _$13_@2 = JSX - [6] Call mutate foo$4_@0(mutate b$11_@0) + [6] Call mutate foo$4(mutate b$11_@0) [7] Const mutate $14_@3 = 'div' [8] Const mutate $15_@4 = JSX "] diff --git a/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md index b37c15cfbb..d2230a7a17 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md @@ -37,6 +37,28 @@ bb1: ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + if (read a$4) { + while ( + read b$5 + ) { + if (read c$6) { + break + } + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/component.expect.md b/compiler/forget/src/__tests__/fixtures/hir/component.expect.md index ef4299261b..91f78b4a08 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/component.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/component.expect.md @@ -2,7 +2,6 @@ ## Input ```javascript -// @Out DefUseGraph function Component(props) { const items = props.items; const maxItems = props.maxItems; @@ -10,7 +9,8 @@ function Component(props) { const renderedItems = []; const seen = new Set(); const max = Math.max(0, maxItems); - for (const item of items) { + for (let i = 0; i < items.length; i += 1) { + const item = items.at(i); if (item == null || seen.has(item)) { continue; } @@ -35,63 +35,162 @@ function Component(props) { ``` bb0: - [1] Const mutate items$27_@0 = read props$26.items - [2] Const mutate maxItems$28_@1 = read props$26.maxItems - [3] Const mutate renderedItems$29_@2[3:22] = Array [] - [4] Const mutate seen$30_@3[0:19] = New mutate Set$6_@3() - [5] Const mutate $31_@4 = 0 - [6] Const mutate max$32_@5[0:7] = Call mutate Math$8_@5.max(read $31_@4, read maxItems$28_@1) - [7] Goto bb1 -bb1: - predecessor blocks: bb0 bb5 bb10 - [8] If (read items$27_@0) then:bb3 else:bb2 fallthrough=bb2 + [1] Const mutate items$30_@0 = read props$29.items + [2] Const mutate maxItems$31_@1 = read props$29.maxItems + [3] Const mutate renderedItems$32_@2[3:33] = Array [] + [4] Const mutate seen$33_@2[3:33] = New mutate Set$6() + [5] Const mutate $34_@4 = 0 + [6] Const mutate max$35_@5 = Call mutate Math$8.max(read $34_@4, read maxItems$31_@1) + [7] For init=bb3 test=bb1 loop=bb5 update=bb4 fallthrough=bb2 bb3: - predecessor blocks: bb1 - [9] Const mutate $34_@6 = null - [10] Const mutate $36_@3[0:19] = Binary read item$10_@3 == read $34_@6 - [11] Let mutate $37_@3[0:19] = undefined - [11] If (read $36_@3) then:bb8 else:bb9 fallthrough=bb7 -bb8: - predecessor blocks: bb3 - [12] Const mutate $37_@3[0:19] = read $36_@3 - [13] Goto bb7 -bb9: - predecessor blocks: bb3 - [14] Const mutate $37_@3[0:19] = Call mutate seen$30_@3.has(mutate item$10_@3) - [15] Goto bb7 -bb7: - predecessor blocks: bb8 bb9 - [16] If (read $37_@3) then:bb5 else:bb4 fallthrough=bb4 + predecessor blocks: bb0 + [8] Let mutate i$36_@2[3:33] = 0 + [9] Goto bb1 +bb1: + predecessor blocks: bb3 bb4 + [10] Const mutate $39_@2[3:33] = Binary read i$36_@2 < read items$30_@0.length + [11] If (read $39_@2) then:bb5 else:bb2 fallthrough=bb2 bb5: - predecessor blocks: bb7 - [17] Goto(Continue) bb1 -bb4: - predecessor blocks: bb7 - [18] Call mutate seen$30_@3.add(mutate item$10_@3) - [19] Const mutate $43_@7 = "div" - [20] Const mutate $44_@8 = JSX {read item$10_@3} - [21] Call mutate renderedItems$29_@2.push(read $44_@8) - [22] Const mutate $49_@9 = Binary read renderedItems$29_@2.length >= read max$32_@5 - [23] If (read $49_@9) then:bb2 else:bb10 fallthrough=bb10 + predecessor blocks: bb1 + [12] Const mutate item$40_@2[3:33] = Call read items$30_@0.at(read i$36_@2) + [13] Const mutate $41_@6 = null + [14] Const mutate $42_@2[3:33] = Binary read item$40_@2 == read $41_@6 + [15] Let mutate $43_@2[3:33] = undefined + [15] If (read $42_@2) then:bb10 else:bb11 fallthrough=bb9 bb10: - predecessor blocks: bb4 - [24] Goto(Continue) bb1 + predecessor blocks: bb5 + [16] Const mutate $43_@2[3:33] = read $42_@2 + [17] Goto bb9 +bb11: + predecessor blocks: bb5 + [18] Const mutate $43_@2[3:33] = Call mutate seen$33_@2.has(mutate item$40_@2) + [19] Goto bb9 +bb9: + predecessor blocks: bb10 bb11 + [20] If (read $43_@2) then:bb7 else:bb6 fallthrough=bb6 +bb7: + predecessor blocks: bb9 + [21] Goto(Continue) bb4 +bb6: + predecessor blocks: bb9 + [22] Call mutate seen$33_@2.add(mutate item$40_@2) + [23] Const mutate $49_@7 = "div" + [24] Const mutate $50_@8 = JSX {freeze item$40_@2} + [25] Call mutate renderedItems$32_@2.push(read $50_@8) + [26] Const mutate $55_@9 = Binary read renderedItems$32_@2.length >= read max$35_@5 + [27] If (read $55_@9) then:bb2 else:bb12 fallthrough=bb12 +bb12: + predecessor blocks: bb6 + [28] Goto(Continue) bb4 +bb4: + predecessor blocks: bb7 bb12 + [29] Const mutate $56_@2[3:33] = 1 + [30] Reassign mutate i$36_@2[3:33] = Binary read i$36_@2 + read $56_@2 + [31] read i$36_@2 + [32] Goto bb1 bb2: - predecessor blocks: bb4 bb1 - [25] Const mutate count$52_@10 = read renderedItems$29_@2.length - [26] Const mutate $53_@11 = "div" - [27] Const mutate $54_@12 = "\n " - [28] Const mutate $55_@13 = "h1" - [29] Const mutate $56_@14 = " Items" - [30] Const mutate $57_@15 = JSX {freeze count$52_@10}{read $56_@14} - [31] Const mutate $58_@16 = "\n " - [32] Const mutate $59_@17 = "\n " - [33] Const mutate $60_@18 = JSX {read $54_@12}{read $57_@15}{read $58_@16}{freeze renderedItems$29_@2}{read $59_@17} - [34] Return read $60_@18 + predecessor blocks: bb6 bb1 + [33] Const mutate count$66_@11 = read renderedItems$32_@2.length + [34] Const mutate $67_@12 = "div" + [35] Const mutate $68_@13 = "\n " + [36] Const mutate $69_@14 = "h1" + [37] Const mutate $70_@15 = " Items" + [38] Const mutate $71_@16 = JSX {freeze count$66_@11}{read $70_@15} + [39] Const mutate $72_@17 = "\n " + [40] Const mutate $73_@18 = "\n " + [41] Const mutate $74_@19 = JSX {read $68_@13}{read $71_@16}{read $72_@17}{freeze renderedItems$32_@2}{read $73_@18} + [42] Return read $74_@19 scope0 [1:2]: - - dependency: read props$26.items + - dependency: read props$29.items scope1 [2:3]: - - dependency: read props$26.maxItems + - dependency: read props$29.maxItems +scope5 [6:7]: + - dependency: read $34_@4 + - dependency: read maxItems$31_@1 +scope8 [24:25]: + - dependency: read $49_@7 +scope9 [26:27]: + - dependency: read max$35_@5 +scope11 [33:34]: + - dependency: read renderedItems$32_@2.length +scope16 [38:39]: + - dependency: read $69_@14 + - dependency: freeze count$66_@11 + - dependency: read $70_@15 +scope19 [41:42]: + - dependency: read $67_@12 + - dependency: read $68_@13 + - dependency: read $71_@16 + - dependency: read $72_@17 + - dependency: freeze renderedItems$32_@2 + - dependency: read $73_@18 +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + [1] Const mutate items$30_@0 = read props$29.items + [2] Const mutate maxItems$31_@1 = read props$29.maxItems + scope @2 [3:33] deps=[] { + [3] Const mutate renderedItems$32_@2[3:33] = Array [] + [4] Const mutate seen$33_@2[3:33] = New mutate Set$6() + [5] Const mutate $34_@4 = 0 + scope @5 [6:7] deps=[read $34_@4, read maxItems$31_@1] { + [6] Const mutate max$35_@5 = Call mutate Math$8.max(read $34_@4, read maxItems$31_@1) + } + for ( + [8] Let mutate i$36_@2[3:33] = 0 + ; + [10] Const mutate $39_@2[3:33] = Binary read i$36_@2 < read items$30_@0.length + read $39_@2 + ; + [29] Const mutate $56_@2[3:33] = 1 + [30] Reassign mutate i$36_@2[3:33] = Binary read i$36_@2 + read $56_@2 + read i$36_@2 + ) { + [12] Const mutate item$40_@2[3:33] = Call read items$30_@0.at(read i$36_@2) + [13] Const mutate $41_@6 = null + [14] Const mutate $42_@2[3:33] = Binary read item$40_@2 == read $41_@6 + [15] Let mutate $43_@2[3:33] = undefined + if (read $42_@2) { + [16] Const mutate $43_@2[3:33] = read $42_@2 + } else { + [18] Const mutate $43_@2[3:33] = Call mutate seen$33_@2.has(mutate item$40_@2) + } + if (read $43_@2) { + continue + } + [22] Call mutate seen$33_@2.add(mutate item$40_@2) + [23] Const mutate $49_@7 = "div" + scope @8 [24:25] deps=[read $49_@7] { + [24] Const mutate $50_@8 = JSX {freeze item$40_@2} + } + [25] Call mutate renderedItems$32_@2.push(read $50_@8) + [26] Const mutate $55_@9 = Binary read renderedItems$32_@2.length >= read max$35_@5 + if (read $55_@9) { + break + } + } + } + [33] Const mutate count$66_@11 = read renderedItems$32_@2.length + [34] Const mutate $67_@12 = "div" + [35] Const mutate $68_@13 = "\n " + [36] Const mutate $69_@14 = "h1" + [37] Const mutate $70_@15 = " Items" + scope @16 [38:39] deps=[read $69_@14, freeze count$66_@11, read $70_@15] { + [38] Const mutate $71_@16 = JSX {freeze count$66_@11}{read $70_@15} + } + [39] Const mutate $72_@17 = "\n " + [40] Const mutate $73_@18 = "\n " + scope @19 [41:42] deps=[read $67_@12, read $68_@13, read $71_@16, read $72_@17, freeze renderedItems$32_@2, read $73_@18] { + [41] Const mutate $74_@19 = JSX {read $68_@13}{read $71_@16}{read $72_@17}{freeze renderedItems$32_@2}{read $73_@18} + } + return read $74_@19 +} + ``` ### CFG @@ -101,99 +200,149 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate items$27_@0 = read props$26.items - [2] Const mutate maxItems$28_@1 = read props$26.maxItems - [3] Const mutate renderedItems$29_@2[3:22] = Array [] - [4] Const mutate seen$30_@3[0:19] = New mutate Set$6_@3() - [5] Const mutate $31_@4 = 0 - [6] Const mutate max$32_@5[0:7] = Call mutate Math$8_@5.max(read $31_@4, read maxItems$28_@1) + [1] Const mutate items$30_@0 = read props$29.items + [2] Const mutate maxItems$31_@1 = read props$29.maxItems + [3] Const mutate renderedItems$32_@2[3:33] = Array [] + [4] Const mutate seen$33_@2[3:33] = New mutate Set$6() + [5] Const mutate $34_@4 = 0 + [6] Const mutate max$35_@5 = Call mutate Math$8.max(read $34_@4, read maxItems$31_@1) "] - bb0_instrs --> bb0_terminal(["Goto"]) - end - subgraph bb1 - bb1_terminal(["If (read items$27_@0)"]) + bb0_instrs --> bb0_terminal(["For"]) end subgraph bb3 bb3_instrs[" - [9] Const mutate $34_@6 = null - [10] Const mutate $36_@3[0:19] = Binary read item$10_@3 == read $34_@6 - [11] Let mutate $37_@3[0:19] = undefined + [8] Let mutate i$36_@2[3:33] = 0 "] - bb3_instrs --> bb3_terminal(["If (read $36_@3)"]) + bb3_instrs --> bb3_terminal(["Goto"]) end - subgraph bb8 - bb8_instrs[" - [12] Const mutate $37_@3[0:19] = read $36_@3 + subgraph bb1 + bb1_instrs[" + [10] Const mutate $39_@2[3:33] = Binary read i$36_@2 < read items$30_@0.length "] - bb8_instrs --> bb8_terminal(["Goto"]) - end - subgraph bb9 - bb9_instrs[" - [14] Const mutate $37_@3[0:19] = Call mutate seen$30_@3.has(mutate item$10_@3) - "] - bb9_instrs --> bb9_terminal(["Goto"]) - end - subgraph bb7 - bb7_terminal(["If (read $37_@3)"]) + bb1_instrs --> bb1_terminal(["If (read $39_@2)"]) end subgraph bb5 - bb5_terminal(["Goto"]) + bb5_instrs[" + [12] Const mutate item$40_@2[3:33] = Call read items$30_@0.at(read i$36_@2) + [13] Const mutate $41_@6 = null + [14] Const mutate $42_@2[3:33] = Binary read item$40_@2 == read $41_@6 + [15] Let mutate $43_@2[3:33] = undefined + "] + bb5_instrs --> bb5_terminal(["If (read $42_@2)"]) + end + subgraph bb10 + bb10_instrs[" + [16] Const mutate $43_@2[3:33] = read $42_@2 + "] + bb10_instrs --> bb10_terminal(["Goto"]) + end + subgraph bb11 + bb11_instrs[" + [18] Const mutate $43_@2[3:33] = Call mutate seen$33_@2.has(mutate item$40_@2) + "] + bb11_instrs --> bb11_terminal(["Goto"]) + end + subgraph bb9 + bb9_terminal(["If (read $43_@2)"]) + end + subgraph bb7 + bb7_terminal(["Goto"]) + end + subgraph bb6 + bb6_instrs[" + [22] Call mutate seen$33_@2.add(mutate item$40_@2) + [23] Const mutate $49_@7 = 'div' + [24] Const mutate $50_@8 = JSX {freeze item$40_@2} + [25] Call mutate renderedItems$32_@2.push(read $50_@8) + [26] Const mutate $55_@9 = Binary read renderedItems$32_@2.length >= read max$35_@5 + "] + bb6_instrs --> bb6_terminal(["If (read $55_@9)"]) + end + subgraph bb12 + bb12_terminal(["Goto"]) end subgraph bb4 bb4_instrs[" - [18] Call mutate seen$30_@3.add(mutate item$10_@3) - [19] Const mutate $43_@7 = 'div' - [20] Const mutate $44_@8 = JSX {read item$10_@3} - [21] Call mutate renderedItems$29_@2.push(read $44_@8) - [22] Const mutate $49_@9 = Binary read renderedItems$29_@2.length >= read max$32_@5 + [29] Const mutate $56_@2[3:33] = 1 + [30] Reassign mutate i$36_@2[3:33] = Binary read i$36_@2 + read $56_@2 + [31] read i$36_@2 "] - bb4_instrs --> bb4_terminal(["If (read $49_@9)"]) - end - subgraph bb10 - bb10_terminal(["Goto"]) + bb4_instrs --> bb4_terminal(["Goto"]) end subgraph bb2 bb2_instrs[" - [25] Const mutate count$52_@10 = read renderedItems$29_@2.length - [26] Const mutate $53_@11 = 'div' - [27] Const mutate $54_@12 = '\n ' - [28] Const mutate $55_@13 = 'h1' - [29] Const mutate $56_@14 = ' Items' - [30] Const mutate $57_@15 = JSX {freeze count$52_@10}{read $56_@14} - [31] Const mutate $58_@16 = '\n ' - [32] Const mutate $59_@17 = '\n ' - [33] Const mutate $60_@18 = JSX {read $54_@12}{read $57_@15}{read $58_@16}{freeze renderedItems$29_@2}{read $59_@17} + [33] Const mutate count$66_@11 = read renderedItems$32_@2.length + [34] Const mutate $67_@12 = 'div' + [35] Const mutate $68_@13 = '\n ' + [36] Const mutate $69_@14 = 'h1' + [37] Const mutate $70_@15 = ' Items' + [38] Const mutate $71_@16 = JSX {freeze count$66_@11}{read $70_@15} + [39] Const mutate $72_@17 = '\n ' + [40] Const mutate $73_@18 = '\n ' + [41] Const mutate $74_@19 = JSX {read $68_@13}{read $71_@16}{read $72_@17}{freeze renderedItems$32_@2}{read $73_@18} "] - bb2_instrs --> bb2_terminal(["Return read $60_@18"]) + bb2_instrs --> bb2_terminal(["Return read $74_@19"]) end %% Jumps - bb0_terminal --> bb1 - bb1_terminal -- "then" --> bb3 + bb0_terminal -- "init" --> bb3 + bb0_terminal -- "test" --> bb1 + bb0_terminal -- "update" --> bb4 + bb0_terminal -- "loop" --> bb5 + bb0_terminal -- "fallthrough" --> bb2 + bb3_terminal --> bb1 + bb1_terminal -- "then" --> bb5 bb1_terminal -- "else" --> bb2 - bb3_terminal -- "then" --> bb8 - bb3_terminal -- "else" --> bb9 - bb3_terminal -- "fallthrough" --> bb7 - bb8_terminal --> bb7 - bb9_terminal --> bb7 - bb7_terminal -- "then" --> bb5 - bb7_terminal -- "else" --> bb4 - bb5_terminal --> bb1 - bb4_terminal -- "then" --> bb2 - bb4_terminal -- "else" --> bb10 - bb10_terminal --> bb1 + bb5_terminal -- "then" --> bb10 + bb5_terminal -- "else" --> bb11 + bb5_terminal -- "fallthrough" --> bb9 + bb10_terminal --> bb9 + bb11_terminal --> bb9 + bb9_terminal -- "then" --> bb7 + bb9_terminal -- "else" --> bb6 + bb7_terminal --> bb4 + bb6_terminal -- "then" --> bb2 + bb6_terminal -- "else" --> bb12 + bb12_terminal --> bb4 + bb4_terminal --> bb1 ``` ## Code ```javascript -function Component$0(props$26) { - const items$27 = props$26.items; - const maxItems$28 = props$26.maxItems; - const renderedItems$29 = []; - const seen$30 = new Set$6(); - const max$32 = Math$8.max(0, maxItems$28); +function Component$0(props$29) { + const items$30 = props$29.items; + const maxItems$31 = props$29.maxItems; + const renderedItems$32 = []; + const seen$33 = new Set$6(); + const max$35 = Math$8.max(0, maxItems$31); + bb2: for (let i$36 = 0; i$36 < items$30.length; i$36 = i$36 + 1, i$36) { + const item$40 = items$30.at(i$36); + + bb9: if (item$40 == null) { + } else { + } + + bb6: if (seen$33.has(item$40)) { + continue; + } + + seen$33.add(item$40); + renderedItems$32.push(
{item$40}
); + + bb12: if (renderedItems$32.length >= max$35) { + break; + } + } + + const count$66 = renderedItems$32.length; + return ( +
+ {

{count$66} Items

} + {renderedItems$32} +
+ ); } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/component.js b/compiler/forget/src/__tests__/fixtures/hir/component.js index d81c842552..86336ff14f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/component.js +++ b/compiler/forget/src/__tests__/fixtures/hir/component.js @@ -1,4 +1,3 @@ -// @Out DefUseGraph function Component(props) { const items = props.items; const maxItems = props.maxItems; @@ -6,7 +5,8 @@ function Component(props) { const renderedItems = []; const seen = new Set(); const max = Math.max(0, maxItems); - for (const item of items) { + for (let i = 0; i < items.length; i += 1) { + const item = items.at(i); if (item == null || seen.has(item)) { continue; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md index 7efd9c60bf..9bc79d873d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md @@ -95,6 +95,26 @@ scope0 [1:7]: - dependency: read props$4.d ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$4.a, read props$4.b, read props$4.d] { + [1] Const mutate a_DEBUG$5_@0[1:7] = Array [] + [2] Call mutate a_DEBUG$5_@0.push(read props$4.a) + if (read props$4.b) { + [4] Const mutate $6_@1 = null + return read $6_@1 + } + [6] Call mutate a_DEBUG$5_@0.push(read props$4.d) + } + return freeze a_DEBUG$5_@0 +} + +``` + ### CFG ```mermaid @@ -163,6 +183,25 @@ scope0 [1:7]: - dependency: read props$3.d ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$3.a, read props$3.c, read props$3.b, read props$3.d] { + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + if (read props$3.b) { + [4] Call mutate a$4_@0.push(read props$3.c) + } + [6] Call mutate a$4_@0.push(read props$3.d) + } + return freeze a$4_@0 +} + +``` + ### CFG ```mermaid @@ -233,6 +272,27 @@ scope0 [1:8]: - dependency: read props$4.d ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:8] deps=[read props$4.a, read props$4.c, read props$4.b, read props$4.d] { + [1] Const mutate a$5_@0[1:8] = Array [] + [2] Call mutate a$5_@0.push(read props$4.a) + if (read props$4.b) { + [4] Call mutate a$5_@0.push(read props$4.c) + [5] Const mutate $6_@1 = null + return read $6_@1 + } + [7] Call mutate a$5_@0.push(read props$4.d) + } + return freeze a$5_@0 +} + +``` + ### CFG ```mermaid @@ -303,6 +363,26 @@ scope0 [1:7]: - dependency: read props$3.d ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$3.a, read props$3.c, read props$3.b, read props$3.d] { + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + if (read props$3.b) { + [4] Call mutate a$4_@0.push(read props$3.c) + return freeze a$4_@0 + } + [6] Call mutate a$4_@0.push(read props$3.d) + } + return freeze a$4_@0 +} + +``` + ### CFG ```mermaid @@ -371,6 +451,25 @@ scope0 [1:7]: - dependency: read props$3.c ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$3.a, read props$3.d, read props$3.c] { + [1] Const mutate a$4_@0[1:7] = Array [] + [2] Call mutate a$4_@0.push(read props$3.a) + if (read props$3.b) { + [6] Call mutate a$4_@0.push(read props$3.d) + return freeze a$4_@0 + } + [4] Call mutate a$4_@0.push(read props$3.c) + } +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md index f7ec1e0374..33495a9637 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md @@ -58,6 +58,30 @@ scope2 [9:10]: - dependency: freeze b$8_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:9] deps=[] { + [1] Const mutate a$7_@0[1:9] = Array [] + [2] Const mutate b$8_@0[1:9] = Array [] + if (read b$8_@0) { + [4] Call mutate a$7_@0.push(read props$6.p0) + } + if (read props$6.p1) { + [7] Call mutate b$8_@0.push(read props$6.p2) + } + } + scope @2 [9:10] deps=[freeze a$7_@0, freeze b$8_@0] { + [9] Const mutate $16_@2 = JSX + } + return read $16_@2 +} + +``` + ### CFG ```mermaid @@ -124,9 +148,9 @@ function Component$0(props$6) { ``` bb0: - [1] Const mutate a$9_@0[0:10] = Array [] - [2] Const mutate b$10_@0[0:10] = Array [] - [3] Const mutate $11_@0[0:10] = Call mutate mayMutate$4_@0(mutate b$10_@0) + [1] Const mutate a$9_@0[1:10] = Array [] + [2] Const mutate b$10_@0[1:10] = Array [] + [3] Const mutate $11_@0[1:10] = Call mutate mayMutate$4(mutate b$10_@0) [4] If (read $11_@0) then:bb2 else:bb1 fallthrough=bb1 bb2: predecessor blocks: bb0 @@ -148,6 +172,31 @@ scope2 [10:11]: - dependency: freeze b$10_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:10] deps=[] { + [1] Const mutate a$9_@0[1:10] = Array [] + [2] Const mutate b$10_@0[1:10] = Array [] + [3] Const mutate $11_@0[1:10] = Call mutate mayMutate$4(mutate b$10_@0) + if (read $11_@0) { + [5] Call mutate a$9_@0.push(read props$8.p0) + } + if (read props$8.p1) { + [8] Call mutate b$10_@0.push(read props$8.p2) + } + } + scope @2 [10:11] deps=[freeze a$9_@0, freeze b$10_@0] { + [10] Const mutate $19_@2 = JSX + } + return read $19_@2 +} + +``` + ### CFG ```mermaid @@ -155,9 +204,9 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$9_@0[0:10] = Array [] - [2] Const mutate b$10_@0[0:10] = Array [] - [3] Const mutate $11_@0[0:10] = Call mutate mayMutate$4_@0(mutate b$10_@0) + [1] Const mutate a$9_@0[1:10] = Array [] + [2] Const mutate b$10_@0[1:10] = Array [] + [3] Const mutate $11_@0[1:10] = Call mutate mayMutate$4(mutate b$10_@0) "] bb0_instrs --> bb0_terminal(["If (read $11_@0)"]) end @@ -219,6 +268,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -246,6 +305,16 @@ bb0: ``` +## Reactive Scopes + +``` +function mayMutate( +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md b/compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md index cc460a0f84..f9aaf352ef 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md @@ -23,6 +23,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -46,12 +56,12 @@ function Foo$0() {} ``` bb0: - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] New mutate Foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] New mutate Foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = "div" [5] Const mutate _$13_@2 = JSX - [6] New mutate Foo$4_@0(mutate b$11_@0) + [6] New mutate Foo$4(mutate b$11_@0) [7] Const mutate $14_@3 = "div" [8] Const mutate $15_@4 = JSX [9] Return read $15_@4 @@ -63,6 +73,31 @@ scope4 [8:9]: - dependency: freeze b$11_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[] { + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] New mutate Foo$4(mutate a$10_@0, mutate b$11_@0) + [4] Const mutate $12_@1 = "div" + scope @2 [5:6] deps=[read $12_@1] { + [5] Const mutate _$13_@2 = JSX + } + [6] New mutate Foo$4(mutate b$11_@0) + } + [7] Const mutate $14_@3 = "div" + scope @4 [8:9] deps=[read $14_@3, read a$10_@0, freeze b$11_@0] { + [8] Const mutate $15_@4 = JSX + } + return read $15_@4 +} + +``` + ### CFG ```mermaid @@ -70,12 +105,12 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] New mutate Foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] New mutate Foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = 'div' [5] Const mutate _$13_@2 = JSX - [6] New mutate Foo$4_@0(mutate b$11_@0) + [6] New mutate Foo$4(mutate b$11_@0) [7] Const mutate $14_@3 = 'div' [8] Const mutate $15_@4 = JSX "] diff --git a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md index 335172be9e..ffb0e2309e 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md @@ -53,6 +53,33 @@ scope2 [5:12]: - dependency: read b$7 ``` +## Reactive Scopes + +``` +function foo( + a, + b, +) { + scope @0 [1:3] deps=[read a$6] { + [1] Const mutate x$8_@0[1:3] = Array [] + [2] Call mutate x$8_@0.push(read a$6) + } + [3] Const mutate $9_@1 = "div" + [4] JSX {freeze x$8_@0} + scope @2 [5:12] deps=[read x$8_@0, read x$8_@0.length, read b$7, read b$7] { + [5] Const mutate y$10_@2[5:12] = Array [] + if (read x$8_@0.length) { + [7] Call mutate y$10_@2.push(read x$8_@0) + } + if (read b$7) { + [10] Call mutate y$10_@2.push(read b$7) + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/dependencies.expect.md b/compiler/forget/src/__tests__/fixtures/hir/dependencies.expect.md index b71339dc8e..965fd2dd02 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dependencies.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/dependencies.expect.md @@ -52,6 +52,32 @@ scope1 [3:7]: - dependency: read x$6 ``` +## Reactive Scopes + +``` +function foo( + x, + y, + z, +) { + scope @0 [1:10] deps=[read z$8, read x$6, read x$6, read y$7] { + [1] Const mutate items$9_@0[1:10] = Array [read z$8] + [2] Call mutate items$9_@0.push(read x$6) + scope @1 [3:7] deps=[read y$7, read x$6] { + [3] Const mutate items2$10_@1[3:7] = Array [] + if (read x$6) { + [5] Call mutate items2$10_@1.push(read y$7) + } + } + if (read y$7) { + [8] Call mutate items$9_@0.push(read x$6) + } + } + return freeze items2$10_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md index eaa18210ec..bc9629631c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md @@ -52,6 +52,34 @@ scope0 [1:8]: - dependency: read a$7 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:8] deps=[read c$9, read b$8, read a$7] { + [1] Const mutate x$10_@0[1:8] = Array [] + if (read a$7) { + if (read b$8) { + if (read c$9) { + [5] Const mutate $11_@1 = 0 + [6] Call mutate x$10_@0.push(read $11_@1) + } + } + } + } + if (read a$7.length) { + return read a$7 + } + [10] Const mutate $13_@2 = null + return read $13_@2 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md index 3c577ec1e5..3b0ce9b4f1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md @@ -21,13 +21,29 @@ bb0: [1] Const mutate a$5_@0 = Array [] [2] Const mutate b$6_@1 = read a$5_@0 [3] Call read useFreeze$3(freeze a$5_@0) - [4] Call mutate foo$4_@2(read b$6_@1) + [4] Call mutate foo$4(read b$6_@1) [5] Return scope1 [2:3]: - dependency: read a$5_@0 - dependency: freeze a$5_@0 ``` +## Reactive Scopes + +``` +function Component( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate a$5_@0 = Array [] + } + [2] Const mutate b$6_@1 = read a$5_@0 + [3] Call read useFreeze$3(freeze a$5_@0) + [4] Call mutate foo$4(read b$6_@1) + return +} + +``` + ### CFG ```mermaid @@ -38,7 +54,7 @@ flowchart TB [1] Const mutate a$5_@0 = Array [] [2] Const mutate b$6_@1 = read a$5_@0 [3] Call read useFreeze$3(freeze a$5_@0) - [4] Call mutate foo$4_@2(read b$6_@1) + [4] Call mutate foo$4(read b$6_@1) "] bb0_instrs --> bb0_terminal(["Return"]) end @@ -66,6 +82,16 @@ bb0: ``` +## Reactive Scopes + +``` +function useFreeze( +) { + return +} + +``` + ### CFG ```mermaid @@ -93,6 +119,17 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( + x, +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md index d3a174c0f3..e3a6016516 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md @@ -27,6 +27,16 @@ bb0: ``` +## Reactive Scopes + +``` +function useFreeze( +) { + return +} + +``` + ### CFG ```mermaid @@ -54,6 +64,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -79,21 +99,46 @@ function foo$0() {} bb0: [1] Const mutate x$11_@0 = Array [] [2] Const mutate y$12_@1 = Call read useFreeze$4(freeze x$11_@0) - [3] Call mutate foo$5_@2(read y$12_@1, read x$11_@0) - [4] Const mutate $13_@3 = "\n " - [5] Const mutate $14_@4 = "\n " - [6] Const mutate $15_@5 = "\n " - [7] Const mutate $16_@6 = JSX {read $13_@3}{read x$11_@0}{read $14_@4}{read y$12_@1}{read $15_@5} - [8] Return read $16_@6 + [3] Call mutate foo$5(read y$12_@1, read x$11_@0) + [4] Const mutate $13_@2 = "\n " + [5] Const mutate $14_@3 = "\n " + [6] Const mutate $15_@4 = "\n " + [7] Const mutate $16_@5 = JSX {read $13_@2}{read x$11_@0}{read $14_@3}{read y$12_@1}{read $15_@4} + [8] Return read $16_@5 scope1 [2:3]: - dependency: freeze x$11_@0 -scope6 [7:8]: - - dependency: read Component$0 - - dependency: read $13_@3 - dependency: read x$11_@0 - - dependency: read $14_@4 +scope5 [7:8]: + - dependency: read Component$0 + - dependency: read $13_@2 + - dependency: read x$11_@0 + - dependency: read $14_@3 - dependency: read y$12_@1 - - dependency: read $15_@5 + - dependency: read $15_@4 +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:2] deps=[] { + [1] Const mutate x$11_@0 = Array [] + } + scope @1 [2:3] deps=[freeze x$11_@0, read x$11_@0] { + [2] Const mutate y$12_@1 = Call read useFreeze$4(freeze x$11_@0) + } + [3] Call mutate foo$5(read y$12_@1, read x$11_@0) + [4] Const mutate $13_@2 = "\n " + [5] Const mutate $14_@3 = "\n " + [6] Const mutate $15_@4 = "\n " + scope @5 [7:8] deps=[read Component$0, read $13_@2, read x$11_@0, read $14_@3, read y$12_@1, read $15_@4] { + [7] Const mutate $16_@5 = JSX {read $13_@2}{read x$11_@0}{read $14_@3}{read y$12_@1}{read $15_@4} + } + return read $16_@5 +} + ``` ### CFG @@ -105,13 +150,13 @@ flowchart TB bb0_instrs[" [1] Const mutate x$11_@0 = Array [] [2] Const mutate y$12_@1 = Call read useFreeze$4(freeze x$11_@0) - [3] Call mutate foo$5_@2(read y$12_@1, read x$11_@0) - [4] Const mutate $13_@3 = '\n ' - [5] Const mutate $14_@4 = '\n ' - [6] Const mutate $15_@5 = '\n ' - [7] Const mutate $16_@6 = JSX {read $13_@3}{read x$11_@0}{read $14_@4}{read y$12_@1}{read $15_@5} + [3] Call mutate foo$5(read y$12_@1, read x$11_@0) + [4] Const mutate $13_@2 = '\n ' + [5] Const mutate $14_@3 = '\n ' + [6] Const mutate $15_@4 = '\n ' + [7] Const mutate $16_@5 = JSX {read $13_@2}{read x$11_@0}{read $14_@3}{read y$12_@1}{read $15_@4} "] - bb0_instrs --> bb0_terminal(["Return read $16_@6"]) + bb0_instrs --> bb0_terminal(["Return read $16_@5"]) end %% Jumps diff --git a/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md b/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md index ae4e7defa9..a60b33c1f7 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md @@ -22,11 +22,27 @@ bb0: [1] Const mutate a$4_@0 = Array [] [2] Call read useFreeze$2(freeze a$4_@0) [3] Call read useFreeze$2(read a$4_@0) - [4] Call mutate call$3_@1(read a$4_@0) + [4] Call mutate call$3(read a$4_@0) [5] Return read a$4_@0 ``` +## Reactive Scopes + +``` +function Component( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate a$4_@0 = Array [] + } + [2] Call read useFreeze$2(freeze a$4_@0) + [3] Call read useFreeze$2(read a$4_@0) + [4] Call mutate call$3(read a$4_@0) + return read a$4_@0 +} + +``` + ### CFG ```mermaid @@ -37,7 +53,7 @@ flowchart TB [1] Const mutate a$4_@0 = Array [] [2] Call read useFreeze$2(freeze a$4_@0) [3] Call read useFreeze$2(read a$4_@0) - [4] Call mutate call$3_@1(read a$4_@0) + [4] Call mutate call$3(read a$4_@0) "] bb0_instrs --> bb0_terminal(["Return read a$4_@0"]) end @@ -66,6 +82,17 @@ bb0: ``` +## Reactive Scopes + +``` +function useFreeze( + x, +) { + return +} + +``` + ### CFG ```mermaid @@ -93,6 +120,17 @@ bb0: ``` +## Reactive Scopes + +``` +function call( + x, +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md b/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md index c96da2c1eb..24d92fa229 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md @@ -43,7 +43,7 @@ bb1: predecessor blocks: bb2 bb3 [9] Call read useFreeze$5(freeze a$11_@3) [10] Call read useFreeze$5(read a$11_@3) - [11] Call mutate call$6_@4(read a$11_@3) + [11] Call mutate call$6(read a$11_@3) [12] Return read a$11_@3 scope0 [1:2]: - dependency: read props$7.cond @@ -54,6 +54,31 @@ scope3 [4:9]: - dependency: read cond$8_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + [1] Const mutate cond$8_@0 = read props$7.cond + [2] Const mutate x$9_@1 = read props$7.x + [3] Const mutate a$10_@2 = undefined + scope @3 [4:9] deps=[read x$9_@1, read cond$8_@0] { + [4] Let mutate a$11_@3[4:9] = undefined + if (read cond$8_@0) { + [5] Reassign mutate a$11_@3[4:9] = read x$9_@1 + } else { + [7] Reassign mutate a$11_@3[4:9] = Array [] + } + } + [9] Call read useFreeze$5(freeze a$11_@3) + [10] Call read useFreeze$5(read a$11_@3) + [11] Call mutate call$6(read a$11_@3) + return read a$11_@3 +} + +``` + ### CFG ```mermaid @@ -84,7 +109,7 @@ flowchart TB bb1_instrs[" [9] Call read useFreeze$5(freeze a$11_@3) [10] Call read useFreeze$5(read a$11_@3) - [11] Call mutate call$6_@4(read a$11_@3) + [11] Call mutate call$6(read a$11_@3) "] bb1_instrs --> bb1_terminal(["Return read a$11_@3"]) end @@ -127,6 +152,17 @@ bb0: ``` +## Reactive Scopes + +``` +function useFreeze( + x, +) { + return +} + +``` + ### CFG ```mermaid @@ -154,6 +190,17 @@ bb0: ``` +## Reactive Scopes + +``` +function call( + x, +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md index 429c39885d..03b38b1b70 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md @@ -41,6 +41,16 @@ bb0: ``` +## Reactive Scopes + +``` +function compute( +) { + return +} + +``` + ### CFG ```mermaid @@ -68,6 +78,16 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( +) { + return +} + +``` + ### CFG ```mermaid @@ -95,6 +115,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -122,6 +152,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -145,21 +185,45 @@ function Foo$0() {} ``` bb0: - [1] Const mutate a$9_@0[0:7] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:7] = Call mutate compute$3_@0(read props$8.b) + [1] Const mutate a$9_@1[1:7] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@1[1:7] = Call mutate compute$3(read props$8.b) [3] If (read props$8.c) then:bb2 else:bb1 fallthrough=bb1 bb2: predecessor blocks: bb0 - [4] Call mutate mutate$5_@0(mutate a$9_@0) - [5] Call mutate mutate$5_@0(mutate b$10_@0) + [4] Call mutate mutate$5(mutate a$9_@1) + [5] Call mutate mutate$5(mutate b$10_@1) [6] Goto bb1 bb1: predecessor blocks: bb2 bb0 - [7] Const mutate $14_@1 = JSX - [8] Return read $14_@1 -scope1 [7:8]: - - dependency: freeze a$9_@0 - - dependency: freeze b$10_@0 + [7] Const mutate $14_@2 = JSX + [8] Return read $14_@2 +scope1 [1:7]: + - dependency: read props$8.a +scope2 [7:8]: + - dependency: freeze a$9_@1 + - dependency: freeze b$10_@1 +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + scope @1 [1:7] deps=[read props$8.a] { + [1] Const mutate a$9_@1[1:7] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@1[1:7] = Call mutate compute$3(read props$8.b) + if (read props$8.c) { + [4] Call mutate mutate$5(mutate a$9_@1) + [5] Call mutate mutate$5(mutate b$10_@1) + } + } + scope @2 [7:8] deps=[freeze a$9_@1, freeze b$10_@1] { + [7] Const mutate $14_@2 = JSX + } + return read $14_@2 +} + ``` ### CFG @@ -169,23 +233,23 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$9_@0[0:7] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:7] = Call mutate compute$3_@0(read props$8.b) + [1] Const mutate a$9_@1[1:7] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@1[1:7] = Call mutate compute$3(read props$8.b) "] bb0_instrs --> bb0_terminal(["If (read props$8.c)"]) end subgraph bb2 bb2_instrs[" - [4] Call mutate mutate$5_@0(mutate a$9_@0) - [5] Call mutate mutate$5_@0(mutate b$10_@0) + [4] Call mutate mutate$5(mutate a$9_@1) + [5] Call mutate mutate$5(mutate b$10_@1) "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb1 bb1_instrs[" - [7] Const mutate $14_@1 = JSX + [7] Const mutate $14_@2 = JSX "] - bb1_instrs --> bb1_terminal(["Return read $14_@1"]) + bb1_instrs --> bb1_terminal(["Return read $14_@2"]) end %% Jumps diff --git a/compiler/forget/src/__tests__/fixtures/hir/independent.expect.md b/compiler/forget/src/__tests__/fixtures/hir/independent.expect.md index dea0007418..177843fb68 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/independent.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/independent.expect.md @@ -28,13 +28,37 @@ function Foo() {} ``` bb0: - [1] Const mutate a$8_@0[0:3] = Call mutate compute$3_@0(read props$7.a) - [2] Const mutate b$9_@0[0:3] = Call mutate compute$3_@0(read props$7.b) - [3] Const mutate $10_@1 = JSX - [4] Return read $10_@1 -scope1 [3:4]: + [1] Const mutate a$8_@0 = Call mutate compute$3(read props$7.a) + [2] Const mutate b$9_@1 = Call mutate compute$3(read props$7.b) + [3] Const mutate $10_@2 = JSX + [4] Return read $10_@2 +scope0 [1:2]: + - dependency: read props$7.a +scope1 [2:3]: + - dependency: read props$7.b +scope2 [3:4]: - dependency: freeze a$8_@0 - - dependency: freeze b$9_@0 + - dependency: freeze b$9_@1 +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:2] deps=[read props$7.a] { + [1] Const mutate a$8_@0 = Call mutate compute$3(read props$7.a) + } + scope @1 [2:3] deps=[read props$7.b] { + [2] Const mutate b$9_@1 = Call mutate compute$3(read props$7.b) + } + scope @2 [3:4] deps=[freeze a$8_@0, freeze b$9_@1] { + [3] Const mutate $10_@2 = JSX + } + return read $10_@2 +} + ``` ### CFG @@ -44,11 +68,11 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$8_@0[0:3] = Call mutate compute$3_@0(read props$7.a) - [2] Const mutate b$9_@0[0:3] = Call mutate compute$3_@0(read props$7.b) - [3] Const mutate $10_@1 = JSX + [1] Const mutate a$8_@0 = Call mutate compute$3(read props$7.a) + [2] Const mutate b$9_@1 = Call mutate compute$3(read props$7.b) + [3] Const mutate $10_@2 = JSX "] - bb0_instrs --> bb0_terminal(["Return read $10_@1"]) + bb0_instrs --> bb0_terminal(["Return read $10_@2"]) end %% Jumps @@ -73,6 +97,16 @@ bb0: ``` +## Reactive Scopes + +``` +function compute( +) { + return +} + +``` + ### CFG ```mermaid @@ -100,6 +134,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -127,6 +171,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md index 2c48298942..b26057c4ee 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md @@ -35,6 +35,16 @@ bb0: ``` +## Reactive Scopes + +``` +function compute( +) { + return +} + +``` + ### CFG ```mermaid @@ -62,6 +72,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -89,6 +109,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -112,22 +142,47 @@ function Foo$0() {} ``` bb0: - [1] Const mutate a$9_@0[0:6] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:6] = Call mutate compute$3_@0(read props$8.b) + [1] Const mutate a$9_@0[1:6] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:6] = Call mutate compute$3(read props$8.b) [3] If (read props$8.c) then:bb2 else:bb1 fallthrough=bb1 bb2: predecessor blocks: bb0 - [4] Call mutate foo$5_@0(mutate a$9_@0, mutate b$10_@0) + [4] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) [5] Goto bb1 bb1: predecessor blocks: bb2 bb0 [6] Const mutate $14_@1 = JSX [7] Return read $14_@1 +scope0 [1:6]: + - dependency: read props$8.a + - dependency: read props$8.b + - dependency: read props$8.c scope1 [6:7]: - dependency: freeze a$9_@0 - dependency: freeze b$10_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:6] deps=[read props$8.a, read props$8.b, read props$8.c] { + [1] Const mutate a$9_@0[1:6] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:6] = Call mutate compute$3(read props$8.b) + if (read props$8.c) { + [4] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) + } + } + scope @1 [6:7] deps=[freeze a$9_@0, freeze b$10_@0] { + [6] Const mutate $14_@1 = JSX + } + return read $14_@1 +} + +``` + ### CFG ```mermaid @@ -135,14 +190,14 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$9_@0[0:6] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:6] = Call mutate compute$3_@0(read props$8.b) + [1] Const mutate a$9_@0[1:6] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:6] = Call mutate compute$3(read props$8.b) "] bb0_instrs --> bb0_terminal(["If (read props$8.c)"]) end subgraph bb2 bb2_instrs[" - [4] Call mutate foo$5_@0(mutate a$9_@0, mutate b$10_@0) + [4] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) "] bb2_instrs --> bb2_terminal(["Goto"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md b/compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md index f1c7a9b8f0..5480b9d2c6 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md @@ -28,16 +28,38 @@ function Foo() {} ``` bb0: - [1] Const mutate a$9_@0[0:4] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:4] = Call mutate compute$3_@0(read props$8.b) - [3] Call mutate foo$5_@0(mutate a$9_@0, mutate b$10_@0) + [1] Const mutate a$9_@0[1:4] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:4] = Call mutate compute$3(read props$8.b) + [3] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) [4] Const mutate $11_@1 = JSX [5] Return read $11_@1 +scope0 [1:4]: + - dependency: read props$8.a + - dependency: read props$8.b scope1 [4:5]: - dependency: freeze a$9_@0 - dependency: freeze b$10_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:4] deps=[read props$8.a, read props$8.b] { + [1] Const mutate a$9_@0[1:4] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:4] = Call mutate compute$3(read props$8.b) + [3] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) + } + scope @1 [4:5] deps=[freeze a$9_@0, freeze b$10_@0] { + [4] Const mutate $11_@1 = JSX + } + return read $11_@1 +} + +``` + ### CFG ```mermaid @@ -45,9 +67,9 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$9_@0[0:4] = Call mutate compute$3_@0(read props$8.a) - [2] Const mutate b$10_@0[0:4] = Call mutate compute$3_@0(read props$8.b) - [3] Call mutate foo$5_@0(mutate a$9_@0, mutate b$10_@0) + [1] Const mutate a$9_@0[1:4] = Call mutate compute$3(read props$8.a) + [2] Const mutate b$10_@0[1:4] = Call mutate compute$3(read props$8.b) + [3] Call mutate foo$5(mutate a$9_@0, mutate b$10_@0) [4] Const mutate $11_@1 = JSX "] bb0_instrs --> bb0_terminal(["Return read $11_@1"]) @@ -76,6 +98,16 @@ bb0: ``` +## Reactive Scopes + +``` +function compute( +) { + return +} + +``` + ### CFG ```mermaid @@ -103,6 +135,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -130,6 +172,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md index f3f702bb89..208608c0ce 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md @@ -36,6 +36,27 @@ scope0 [1:6]: - dependency: read a$5 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:6] deps=[read c$7, read b$6, read a$5] { + [1] Const mutate y$8_@0[1:6] = Array [] + if (read a$5) { + if (read b$6) { + [4] Call mutate y$8_@0.push(read c$7) + } + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/issue852.expect.md b/compiler/forget/src/__tests__/fixtures/hir/issue852.expect.md index 5cdb48396d..d4cce83be8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/issue852.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/issue852.expect.md @@ -15,17 +15,36 @@ function Component(c) { ``` bb0: - [1] Const mutate x$7_@0[0:3] = Object { c: read c$6 } - [2] Call mutate mutate$3_@0(mutate x$7_@0) + [1] Const mutate x$7_@0[1:3] = Object { c: read c$6 } + [2] Call mutate mutate$3(mutate x$7_@0) [3] Const mutate a$8_@1 = read x$7_@0 [4] Const mutate b$9_@2 = read a$8_@1 [5] Return +scope0 [1:3]: + - dependency: read c$6 scope1 [3:4]: - dependency: read x$7_@0 scope2 [4:5]: - dependency: read a$8_@1 ``` +## Reactive Scopes + +``` +function Component( + c, +) { + scope @0 [1:3] deps=[read c$6] { + [1] Const mutate x$7_@0[1:3] = Object { c: read c$6 } + [2] Call mutate mutate$3(mutate x$7_@0) + } + [3] Const mutate a$8_@1 = read x$7_@0 + [4] Const mutate b$9_@2 = read a$8_@1 + return +} + +``` + ### CFG ```mermaid @@ -33,8 +52,8 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$7_@0[0:3] = Object { c: read c$6 } - [2] Call mutate mutate$3_@0(mutate x$7_@0) + [1] Const mutate x$7_@0[1:3] = Object { c: read c$6 } + [2] Call mutate mutate$3(mutate x$7_@0) [3] Const mutate a$8_@1 = read x$7_@0 [4] Const mutate b$9_@2 = read a$8_@1 "] diff --git a/compiler/forget/src/__tests__/fixtures/hir/jsx-fragment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/jsx-fragment.expect.md index e41629f5ec..8c2ce46742 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/jsx-fragment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/jsx-fragment.expect.md @@ -47,6 +47,34 @@ scope10 [11:12]: - dependency: read $23_@9 ``` +## Reactive Scopes + +``` +function Foo( + props, +) { + [1] Const mutate $14_@0 = "\n Hello " + [2] Const mutate $15_@1 = " " + [3] Const mutate $16_@2 = "\n " + [4] Const mutate $17_@3 = "div" + [5] Const mutate $18_@4 = "\n " + [6] Const mutate $19_@5 = "Text" + scope @6 [7:8] deps=[read $19_@5] { + [7] Const mutate $20_@6 = JsxFragment [read $19_@5] + } + [8] Const mutate $21_@7 = "\n " + scope @8 [9:10] deps=[read $17_@3, read $18_@4, read $20_@6, read $21_@7] { + [9] Const mutate $22_@8 = JSX {read $18_@4}{read $20_@6}{read $21_@7} + } + [10] Const mutate $23_@9 = "\n " + scope @10 [11:12] deps=[read $14_@0, read props$13.greeting, read $15_@1, read $16_@2, read $22_@8, read $23_@9] { + [11] Const mutate $24_@10 = JsxFragment [read $14_@0, read props$13.greeting, read $15_@1, read $16_@2, read $22_@8, read $23_@9] + } + return read $24_@10 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md index 1b90ba1c89..f6cd755a54 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md @@ -23,20 +23,43 @@ function g() {} ``` bb0: - [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0() - [2] Let mutate $6_@1[0:7] = undefined + [1] Const mutate $5_@0 = Call mutate f$1() + [2] Let mutate $6_@1[2:7] = undefined [2] If (read $5_@0) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [3] Const mutate $6_@1[0:7] = Call mutate g$4_@1() + [3] Const mutate $6_@1[2:7] = Call mutate g$4() [4] Goto bb1 bb3: predecessor blocks: bb0 - [5] Const mutate $6_@1[0:7] = read $5_@0 + [5] Const mutate $6_@1[2:7] = read $5_@0 [6] Goto bb1 bb1: predecessor blocks: bb2 bb3 [7] Return freeze $6_@1 +scope1 [2:7]: + - dependency: read $5_@0 + - dependency: read $5_@0 +``` + +## Reactive Scopes + +``` +function And( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate $5_@0 = Call mutate f$1() + } + scope @1 [2:7] deps=[read $5_@0, read $5_@0] { + [2] Let mutate $6_@1[2:7] = undefined + if (read $5_@0) { + [3] Const mutate $6_@1[2:7] = Call mutate g$4() + } else { + [5] Const mutate $6_@1[2:7] = read $5_@0 + } + } + return freeze $6_@1 +} ``` @@ -47,20 +70,20 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0() - [2] Let mutate $6_@1[0:7] = undefined + [1] Const mutate $5_@0 = Call mutate f$1() + [2] Let mutate $6_@1[2:7] = undefined "] bb0_instrs --> bb0_terminal(["If (read $5_@0)"]) end subgraph bb2 bb2_instrs[" - [3] Const mutate $6_@1[0:7] = Call mutate g$4_@1() + [3] Const mutate $6_@1[2:7] = Call mutate g$4() "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" - [5] Const mutate $6_@1[0:7] = read $5_@0 + [5] Const mutate $6_@1[2:7] = read $5_@0 "] bb3_instrs --> bb3_terminal(["Goto"]) end @@ -92,20 +115,43 @@ function And$0() { ``` bb0: - [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0() - [2] Let mutate $6_@1[0:7] = undefined + [1] Const mutate $5_@0 = Call mutate f$1() + [2] Let mutate $6_@1[2:7] = undefined [2] If (read $5_@0) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [3] Const mutate $6_@1[0:7] = read $5_@0 + [3] Const mutate $6_@1[2:7] = read $5_@0 [4] Goto bb1 bb3: predecessor blocks: bb0 - [5] Const mutate $6_@1[0:7] = Call mutate g$4_@1() + [5] Const mutate $6_@1[2:7] = Call mutate g$4() [6] Goto bb1 bb1: predecessor blocks: bb2 bb3 [7] Return freeze $6_@1 +scope1 [2:7]: + - dependency: read $5_@0 + - dependency: read $5_@0 +``` + +## Reactive Scopes + +``` +function Or( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate $5_@0 = Call mutate f$1() + } + scope @1 [2:7] deps=[read $5_@0, read $5_@0] { + [2] Let mutate $6_@1[2:7] = undefined + if (read $5_@0) { + [3] Const mutate $6_@1[2:7] = read $5_@0 + } else { + [5] Const mutate $6_@1[2:7] = Call mutate g$4() + } + } + return freeze $6_@1 +} ``` @@ -116,20 +162,20 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0() - [2] Let mutate $6_@1[0:7] = undefined + [1] Const mutate $5_@0 = Call mutate f$1() + [2] Let mutate $6_@1[2:7] = undefined "] bb0_instrs --> bb0_terminal(["If (read $5_@0)"]) end subgraph bb2 bb2_instrs[" - [3] Const mutate $6_@1[0:7] = read $5_@0 + [3] Const mutate $6_@1[2:7] = read $5_@0 "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" - [5] Const mutate $6_@1[0:7] = Call mutate g$4_@1() + [5] Const mutate $6_@1[2:7] = Call mutate g$4() "] bb3_instrs --> bb3_terminal(["Goto"]) end @@ -161,18 +207,18 @@ function Or$0() { ``` bb0: - [1] Const mutate $9_@0[0:2] = Call mutate f$2_@0() + [1] Const mutate $9_@0 = Call mutate f$2() [2] Const mutate $10_@1 = null [3] Const mutate $11_@2 = Binary read $9_@0 != read $10_@1 - [4] Let mutate $12_@3[0:9] = undefined + [4] Let mutate $12_@3[4:9] = undefined [4] If (read $11_@2) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [5] Const mutate $12_@3[0:9] = read $9_@0 + [5] Const mutate $12_@3[4:9] = read $9_@0 [6] Goto bb1 bb3: predecessor blocks: bb0 - [7] Const mutate $12_@3[0:9] = Call mutate g$7_@3() + [7] Const mutate $12_@3[4:9] = Call mutate g$7() [8] Goto bb1 bb1: predecessor blocks: bb2 bb3 @@ -180,6 +226,33 @@ bb1: scope2 [3:4]: - dependency: read $9_@0 - dependency: read $10_@1 +scope3 [4:9]: + - dependency: read $9_@0 + - dependency: read $11_@2 +``` + +## Reactive Scopes + +``` +function QuestionQuestion( + props, +) { + scope @0 [1:2] deps=[] { + [1] Const mutate $9_@0 = Call mutate f$2() + } + [2] Const mutate $10_@1 = null + [3] Const mutate $11_@2 = Binary read $9_@0 != read $10_@1 + scope @3 [4:9] deps=[read $9_@0, read $11_@2] { + [4] Let mutate $12_@3[4:9] = undefined + if (read $11_@2) { + [5] Const mutate $12_@3[4:9] = read $9_@0 + } else { + [7] Const mutate $12_@3[4:9] = Call mutate g$7() + } + } + return freeze $12_@3 +} + ``` ### CFG @@ -189,22 +262,22 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate $9_@0[0:2] = Call mutate f$2_@0() + [1] Const mutate $9_@0 = Call mutate f$2() [2] Const mutate $10_@1 = null [3] Const mutate $11_@2 = Binary read $9_@0 != read $10_@1 - [4] Let mutate $12_@3[0:9] = undefined + [4] Let mutate $12_@3[4:9] = undefined "] bb0_instrs --> bb0_terminal(["If (read $11_@2)"]) end subgraph bb2 bb2_instrs[" - [5] Const mutate $12_@3[0:9] = read $9_@0 + [5] Const mutate $12_@3[4:9] = read $9_@0 "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" - [7] Const mutate $12_@3[0:9] = Call mutate g$7_@3() + [7] Const mutate $12_@3[4:9] = Call mutate g$7() "] bb3_instrs --> bb3_terminal(["Goto"]) end @@ -240,6 +313,16 @@ bb0: ``` +## Reactive Scopes + +``` +function f( +) { + return +} + +``` + ### CFG ```mermaid @@ -267,6 +350,16 @@ bb0: ``` +## Reactive Scopes + +``` +function g( +) { + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md index 05ed23f016..fee469ce89 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md @@ -47,6 +47,18 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( + x, + y, +) { + return +} + +``` + ### CFG ```mermaid @@ -74,6 +86,17 @@ bb0: ``` +## Reactive Scopes + +``` +function cond( + x, +) { + return +} + +``` + ### CFG ```mermaid @@ -97,10 +120,10 @@ function cond$0(x$2) {} ``` bb0: - [1] Let mutate a$18_@0[0:23] = Object { } - [2] Let mutate b$20_@0[0:23] = Object { } - [3] Let mutate c$22_@0[0:23] = Object { } - [4] Let mutate d$24_@0[0:23] = Object { } + [1] Let mutate a$18_@0[1:23] = Object { } + [2] Let mutate b$20_@0[1:23] = Object { } + [3] Let mutate c$22_@0[1:23] = Object { } + [4] Let mutate d$24_@0[1:23] = Object { } [5] While test=bb1 loop=bb3 fallthrough=bb2 bb1: predecessor blocks: bb0 bb4 @@ -108,13 +131,13 @@ bb1: [7] If (read $17_@1) then:bb3 else:bb2 fallthrough=bb2 bb3: predecessor blocks: bb1 - [8] Const mutate z$19_@0[0:23] = read a$18_@0 - [9] Reassign mutate a$18_@0[0:23] = read b$20_@0 - [10] Reassign mutate b$20_@0[0:23] = read c$22_@0 - [11] Reassign mutate c$22_@0[0:23] = read d$24_@0 - [12] Reassign mutate d$24_@0[0:23] = read z$19_@0 - [13] Call mutate mutate$7_@0(mutate a$18_@0, mutate b$20_@0) - [14] Const mutate $29_@0[0:23] = Call mutate cond$8_@0(mutate a$18_@0) + [8] Const mutate z$19_@0[1:23] = read a$18_@0 + [9] Reassign mutate a$18_@0[1:23] = read b$20_@0 + [10] Reassign mutate b$20_@0[1:23] = read c$22_@0 + [11] Reassign mutate c$22_@0[1:23] = read d$24_@0 + [12] Reassign mutate d$24_@0[1:23] = read z$19_@0 + [13] Call mutate mutate$7(mutate a$18_@0, mutate b$20_@0) + [14] Const mutate $29_@0[1:23] = Call mutate cond$8(mutate a$18_@0) [15] If (read $29_@0) then:bb2 else:bb4 fallthrough=bb4 bb4: predecessor blocks: bb3 @@ -134,11 +157,55 @@ bb11: bb13: predecessor blocks: bb11 [21] Const mutate $34_@2 = null - [22] Call mutate mutate$7_@0(mutate d$24_@0, read $34_@2) + [22] Call mutate mutate$7(mutate d$24_@0, read $34_@2) [23] Return ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:23] deps=[] { + [1] Let mutate a$18_@0[1:23] = Object { } + [2] Let mutate b$20_@0[1:23] = Object { } + [3] Let mutate c$22_@0[1:23] = Object { } + [4] Let mutate d$24_@0[1:23] = Object { } + scope @1 [6:8] deps=[] { + while ( + [6] Const mutate $17_@1[6:8] = true + read $17_@1 + ) { + [8] Const mutate z$19_@0[1:23] = read a$18_@0 + [9] Reassign mutate a$18_@0[1:23] = read b$20_@0 + [10] Reassign mutate b$20_@0[1:23] = read c$22_@0 + [11] Reassign mutate c$22_@0[1:23] = read d$24_@0 + [12] Reassign mutate d$24_@0[1:23] = read z$19_@0 + [13] Call mutate mutate$7(mutate a$18_@0, mutate b$20_@0) + [14] Const mutate $29_@0[1:23] = Call mutate cond$8(mutate a$18_@0) + if (read $29_@0) { + break + } + } + } + if (read a$18_@0) { + } + if (read b$20_@0) { + } + if (read c$22_@0) { + } + if (read d$24_@0) { + } + [21] Const mutate $34_@2 = null + [22] Call mutate mutate$7(mutate d$24_@0, read $34_@2) + } + return +} + +``` + ### CFG ```mermaid @@ -146,10 +213,10 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Let mutate a$18_@0[0:23] = Object { } - [2] Let mutate b$20_@0[0:23] = Object { } - [3] Let mutate c$22_@0[0:23] = Object { } - [4] Let mutate d$24_@0[0:23] = Object { } + [1] Let mutate a$18_@0[1:23] = Object { } + [2] Let mutate b$20_@0[1:23] = Object { } + [3] Let mutate c$22_@0[1:23] = Object { } + [4] Let mutate d$24_@0[1:23] = Object { } "] bb0_instrs --> bb0_terminal(["While"]) end @@ -161,13 +228,13 @@ flowchart TB end subgraph bb3 bb3_instrs[" - [8] Const mutate z$19_@0[0:23] = read a$18_@0 - [9] Reassign mutate a$18_@0[0:23] = read b$20_@0 - [10] Reassign mutate b$20_@0[0:23] = read c$22_@0 - [11] Reassign mutate c$22_@0[0:23] = read d$24_@0 - [12] Reassign mutate d$24_@0[0:23] = read z$19_@0 - [13] Call mutate mutate$7_@0(mutate a$18_@0, mutate b$20_@0) - [14] Const mutate $29_@0[0:23] = Call mutate cond$8_@0(mutate a$18_@0) + [8] Const mutate z$19_@0[1:23] = read a$18_@0 + [9] Reassign mutate a$18_@0[1:23] = read b$20_@0 + [10] Reassign mutate b$20_@0[1:23] = read c$22_@0 + [11] Reassign mutate c$22_@0[1:23] = read d$24_@0 + [12] Reassign mutate d$24_@0[1:23] = read z$19_@0 + [13] Call mutate mutate$7(mutate a$18_@0, mutate b$20_@0) + [14] Const mutate $29_@0[1:23] = Call mutate cond$8(mutate a$18_@0) "] bb3_instrs --> bb3_terminal(["If (read $29_@0)"]) end @@ -189,7 +256,7 @@ flowchart TB subgraph bb13 bb13_instrs[" [21] Const mutate $34_@2 = null - [22] Call mutate mutate$7_@0(mutate d$24_@0, read $34_@2) + [22] Call mutate mutate$7(mutate d$24_@0, read $34_@2) "] bb13_instrs --> bb13_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md index eb555749f2..7aa8caf489 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md @@ -43,6 +43,18 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( + x, + y, +) { + return +} + +``` + ### CFG ```mermaid @@ -67,12 +79,12 @@ function mutate$0(x$3, y$4) {} ``` bb0: [1] Const mutate a$11_@0 = Object { } - [2] Const mutate b$12_@1[0:15] = Array [read a$11_@0] + [2] Const mutate b$12_@1[2:15] = Array [read a$11_@0] [3] Const mutate c$13_@2 = Object { } - [4] Const mutate d$14_@1[0:15] = Object { c: read c$13_@2 } - [5] Const mutate x$15_@1[0:15] = Object { } - [6] Reassign mutate x$15_@1.b[0:15] = read b$12_@1 - [7] Const mutate y$16_@1[0:15] = Call mutate mutate$8_@1(mutate x$15_@1, mutate d$14_@1) + [4] Const mutate d$14_@1[2:15] = Object { c: read c$13_@2 } + [5] Const mutate x$15_@1[2:15] = Object { } + [6] Reassign mutate x$15_@1.b[2:15] = read b$12_@1 + [7] Const mutate y$16_@1[2:15] = Call mutate mutate$8(mutate x$15_@1, mutate d$14_@1) [8] If (read a$11_@0) then:bb1 else:bb1 fallthrough=bb1 bb1: predecessor blocks: bb0 @@ -89,8 +101,46 @@ bb7: bb9: predecessor blocks: bb7 [13] Const mutate $17_@3 = null - [14] Call mutate mutate$8_@1(mutate x$15_@1, read $17_@3) + [14] Call mutate mutate$8(mutate x$15_@1, read $17_@3) [15] Return +scope1 [2:15]: + - dependency: read a$11_@0 + - dependency: read a$11_@0 +``` + +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:2] deps=[] { + [1] Const mutate a$11_@0 = Object { } + } + scope @1 [2:15] deps=[read a$11_@0, read a$11_@0] { + [2] Const mutate b$12_@1[2:15] = Array [read a$11_@0] + scope @2 [3:4] deps=[] { + [3] Const mutate c$13_@2 = Object { } + } + [4] Const mutate d$14_@1[2:15] = Object { c: read c$13_@2 } + [5] Const mutate x$15_@1[2:15] = Object { } + [6] Reassign mutate x$15_@1.b[2:15] = read b$12_@1 + [7] Const mutate y$16_@1[2:15] = Call mutate mutate$8(mutate x$15_@1, mutate d$14_@1) + if (read a$11_@0) { + } + if (read b$12_@1) { + } + if (read c$13_@2) { + } + if (read d$14_@1) { + } + if (read y$16_@1) { + } + [13] Const mutate $17_@3 = null + [14] Call mutate mutate$8(mutate x$15_@1, read $17_@3) + } + return +} ``` @@ -102,12 +152,12 @@ flowchart TB subgraph bb0 bb0_instrs[" [1] Const mutate a$11_@0 = Object { } - [2] Const mutate b$12_@1[0:15] = Array [read a$11_@0] + [2] Const mutate b$12_@1[2:15] = Array [read a$11_@0] [3] Const mutate c$13_@2 = Object { } - [4] Const mutate d$14_@1[0:15] = Object { c: read c$13_@2 } - [5] Const mutate x$15_@1[0:15] = Object { } - [6] Reassign mutate x$15_@1.b[0:15] = read b$12_@1 - [7] Const mutate y$16_@1[0:15] = Call mutate mutate$8_@1(mutate x$15_@1, mutate d$14_@1) + [4] Const mutate d$14_@1[2:15] = Object { c: read c$13_@2 } + [5] Const mutate x$15_@1[2:15] = Object { } + [6] Reassign mutate x$15_@1.b[2:15] = read b$12_@1 + [7] Const mutate y$16_@1[2:15] = Call mutate mutate$8(mutate x$15_@1, mutate d$14_@1) "] bb0_instrs --> bb0_terminal(["If (read a$11_@0)"]) end @@ -126,7 +176,7 @@ flowchart TB subgraph bb9 bb9_instrs[" [13] Const mutate $17_@3 = null - [14] Call mutate mutate$8_@1(mutate x$15_@1, read $17_@3) + [14] Call mutate mutate$8(mutate x$15_@1, read $17_@3) "] bb9_instrs --> bb9_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md b/compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md index 9c8623264a..c2afa6e4b1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md @@ -42,6 +42,16 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( +) { + return +} + +``` + ### CFG ```mermaid @@ -69,6 +79,16 @@ bb0: ``` +## Reactive Scopes + +``` +function cond( +) { + return +} + +``` + ### CFG ```mermaid @@ -92,19 +112,19 @@ function cond$0() {} ``` bb0: - [1] Const mutate a$12_@0[0:18] = Object { } - [2] Const mutate b$13_@0[0:18] = Object { } + [1] Const mutate a$12_@0[1:18] = Object { } + [2] Const mutate b$13_@0[1:18] = Object { } [3] Const mutate c$14_@1 = Object { } - [4] Const mutate d$15_@0[0:18] = Object { } + [4] Const mutate d$15_@0[1:18] = Object { } [5] While test=bb1 loop=bb3 fallthrough=bb2 bb1: predecessor blocks: bb0 bb4 - [6] Const mutate $16_@2[6:8] = true - [7] If (read $16_@2) then:bb3 else:bb2 fallthrough=bb2 + [6] Const mutate $16_@3[6:8] = true + [7] If (read $16_@3) then:bb3 else:bb2 fallthrough=bb2 bb3: predecessor blocks: bb1 - [8] Call mutate mutate$6_@0(mutate a$12_@0, mutate b$13_@0) - [9] Const mutate $21_@0[0:18] = Call mutate cond$7_@0(mutate a$12_@0) + [8] Call mutate mutate$6(mutate a$12_@0, mutate b$13_@0) + [9] Const mutate $21_@0[1:18] = Call mutate cond$7(mutate a$12_@0) [10] If (read $21_@0) then:bb2 else:bb4 fallthrough=bb4 bb4: predecessor blocks: bb3 @@ -123,12 +143,53 @@ bb11: [15] If (read d$15_@0) then:bb13 else:bb13 fallthrough=bb13 bb13: predecessor blocks: bb11 - [16] Const mutate $28_@3 = null - [17] Call mutate mutate$6_@0(mutate d$15_@0, read $28_@3) + [16] Const mutate $28_@4 = null + [17] Call mutate mutate$6(mutate d$15_@0, read $28_@4) [18] Return ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:18] deps=[] { + [1] Const mutate a$12_@0[1:18] = Object { } + [2] Const mutate b$13_@0[1:18] = Object { } + scope @1 [3:4] deps=[] { + [3] Const mutate c$14_@1 = Object { } + } + [4] Const mutate d$15_@0[1:18] = Object { } + scope @3 [6:8] deps=[] { + while ( + [6] Const mutate $16_@3[6:8] = true + read $16_@3 + ) { + [8] Call mutate mutate$6(mutate a$12_@0, mutate b$13_@0) + [9] Const mutate $21_@0[1:18] = Call mutate cond$7(mutate a$12_@0) + if (read $21_@0) { + break + } + } + } + if (read a$12_@0) { + } + if (read b$13_@0) { + } + if (read c$14_@1) { + } + if (read d$15_@0) { + } + [16] Const mutate $28_@4 = null + [17] Call mutate mutate$6(mutate d$15_@0, read $28_@4) + } + return +} + +``` + ### CFG ```mermaid @@ -136,23 +197,23 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$12_@0[0:18] = Object { } - [2] Const mutate b$13_@0[0:18] = Object { } + [1] Const mutate a$12_@0[1:18] = Object { } + [2] Const mutate b$13_@0[1:18] = Object { } [3] Const mutate c$14_@1 = Object { } - [4] Const mutate d$15_@0[0:18] = Object { } + [4] Const mutate d$15_@0[1:18] = Object { } "] bb0_instrs --> bb0_terminal(["While"]) end subgraph bb1 bb1_instrs[" - [6] Const mutate $16_@2[6:8] = true + [6] Const mutate $16_@3[6:8] = true "] - bb1_instrs --> bb1_terminal(["If (read $16_@2)"]) + bb1_instrs --> bb1_terminal(["If (read $16_@3)"]) end subgraph bb3 bb3_instrs[" - [8] Call mutate mutate$6_@0(mutate a$12_@0, mutate b$13_@0) - [9] Const mutate $21_@0[0:18] = Call mutate cond$7_@0(mutate a$12_@0) + [8] Call mutate mutate$6(mutate a$12_@0, mutate b$13_@0) + [9] Const mutate $21_@0[1:18] = Call mutate cond$7(mutate a$12_@0) "] bb3_instrs --> bb3_terminal(["If (read $21_@0)"]) end @@ -173,8 +234,8 @@ flowchart TB end subgraph bb13 bb13_instrs[" - [16] Const mutate $28_@3 = null - [17] Call mutate mutate$6_@0(mutate d$15_@0, read $28_@3) + [16] Const mutate $28_@4 = null + [17] Call mutate mutate$6(mutate d$15_@0, read $28_@4) "] bb13_instrs --> bb13_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved-by-terminal.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved-by-terminal.expect.md index 5136162579..ae0bcc0ec4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved-by-terminal.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved-by-terminal.expect.md @@ -30,6 +30,27 @@ bb1: ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:6] deps=[] { + [1] Const mutate x$9_@0[1:6] = Array [] + [2] Const mutate y$10_@0[1:6] = Array [] + if (read x$9_@0) { + } + [4] Call mutate y$10_@0.push(read a$6) + [5] Call mutate x$9_@0.push(read b$7) + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved.expect.md index bbf1a8064b..b330531555 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-interleaved.expect.md @@ -23,6 +23,24 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( + a, + b, +) { + scope @0 [1:5] deps=[] { + [1] Const mutate x$7_@0[1:5] = Array [] + [2] Const mutate y$8_@0[1:5] = Array [] + [3] Call mutate x$7_@0.push(read a$5) + [4] Call mutate y$8_@0.push(read b$6) + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowed.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowed.expect.md index ccce815fc6..045885e97f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowed.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowed.expect.md @@ -25,6 +25,26 @@ scope1 [2:4]: - dependency: read a$5 ``` +## Reactive Scopes + +``` +function foo( + a, + b, +) { + scope @0 [1:5] deps=[] { + [1] Const mutate x$7_@0[1:5] = Array [] + scope @1 [2:4] deps=[read b$6, read a$5] { + [2] Const mutate y$8_@1[2:4] = Array [] + [3] Call mutate y$8_@1.push(read b$6) + } + [4] Call mutate x$7_@0.push(read a$5) + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowing-within-block.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowing-within-block.expect.md index f032fdf3b6..42837f1df8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowing-within-block.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowing-within-block.expect.md @@ -50,6 +50,35 @@ scope3 [8:9]: - dependency: freeze y$12_@1 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:11] deps=[read a$8] { + [1] Const mutate x$11_@0[1:11] = Array [] + if (read a$8) { + scope @1 [3:7] deps=[read c$10, read b$9] { + [3] Const mutate y$12_@1[3:7] = Array [] + if (read b$9) { + [5] Call mutate y$12_@1.push(read c$10) + } + } + [7] Const mutate $13_@2 = "div" + scope @3 [8:9] deps=[read $13_@2, freeze y$12_@1] { + [8] Const mutate $15_@3 = JSX {freeze y$12_@1} + } + [9] Call mutate x$11_@0.push(read $15_@3) + } + } + return freeze x$11_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-while.expect.md index c74a032cca..454acfaf77 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-while.expect.md @@ -34,6 +34,29 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:8] deps=[] { + [1] Const mutate x$9_@0[1:8] = Array [] + [2] Const mutate y$10_@0[1:8] = Array [] + while ( + read c$8 + ) { + [5] Call mutate y$10_@0.push(read b$7) + [6] Call mutate x$9_@0.push(read a$6) + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-within-block.expect.md b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-within-block.expect.md index 23198cd527..4e7e690600 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-within-block.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-within-block.expect.md @@ -44,6 +44,29 @@ scope0 [1:9]: - dependency: read a$6 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:9] deps=[read c$8, read b$7, read a$6] { + [1] Const mutate x$9_@0[1:9] = Array [] + if (read a$6) { + [3] Const mutate y$10_@0[1:9] = Array [] + if (read b$7) { + [5] Call mutate y$10_@0.push(read c$8) + } + [7] Call mutate x$9_@0.push(mutate y$10_@0) + } + } + return freeze x$9_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md index 2fc05fdcf0..ca4a1f88fd 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md @@ -33,6 +33,27 @@ scope1 [6:7]: - dependency: read child$9_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:6] deps=[read Component$0, read props$6.p0] { + [1] Const mutate x$7_@0[1:6] = Object { } + [2] Const mutate y$8_@0[1:6] = Array [] + [3] Reassign mutate x$7_@0.y[1:6] = read y$8_@0 + [4] Const mutate child$9_@0[1:6] = JSX + [5] Call mutate x$7_@0.y.push(read props$6.p0) + } + scope @1 [6:7] deps=[read Component$0, freeze x$7_@0, read child$9_@0] { + [6] Const mutate $10_@1 = JSX {read child$9_@0} + } + return read $10_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reactive-scope-grouping.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reactive-scope-grouping.expect.md index acd1cf952b..fb550e9214 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reactive-scope-grouping.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reactive-scope-grouping.expect.md @@ -28,6 +28,25 @@ scope1 [2:5]: - dependency: mutate x$4_@0.y ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:6] deps=[] { + [1] Const mutate x$4_@0[1:6] = Object { } + scope @1 [2:5] deps=[mutate x$4_@0.y] { + [2] Const mutate y$5_@1[2:5] = Array [] + [3] Const mutate z$6_@1[2:5] = Object { } + [4] Call mutate y$5_@1.push(mutate z$6_@1) + } + [5] Reassign mutate x$4_@0.y[1:6] = read y$5_@1 + } + return freeze x$4_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.expect.md new file mode 100644 index 0000000000..e2d052255d --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.expect.md @@ -0,0 +1,136 @@ + +## Input + +```javascript +function foo(a, b, c) { + const x = []; + if (a) { + const y = []; + y.push(b); + x.push(
{y}
); + } else { + x.push(c); + } + return x; +} + +``` + +## HIR + +``` +bb0: + [1] Const mutate x$11_@0[1:11] = Array [] + [2] If (read a$8) then:bb2 else:bb3 fallthrough=bb1 +bb2: + predecessor blocks: bb0 + [3] Const mutate y$12_@1[3:5] = Array [] + [4] Call mutate y$12_@1.push(read b$9) + [5] Const mutate $13_@2 = "div" + [6] Const mutate $14_@3 = JSX {freeze y$12_@1} + [7] Call mutate x$11_@0.push(read $14_@3) + [8] Goto bb1 +bb3: + predecessor blocks: bb0 + [9] Call mutate x$11_@0.push(read c$10) + [10] Goto bb1 +bb1: + predecessor blocks: bb2 bb3 + [11] Return freeze x$11_@0 +scope0 [1:11]: + - dependency: read c$10 + - dependency: read a$8 +scope1 [3:5]: + - dependency: read b$9 +scope3 [6:7]: + - dependency: read $13_@2 + - dependency: freeze y$12_@1 +``` + +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:11] deps=[read c$10, read a$8] { + [1] Const mutate x$11_@0[1:11] = Array [] + if (read a$8) { + scope @1 [3:5] deps=[read b$9] { + [3] Const mutate y$12_@1[3:5] = Array [] + [4] Call mutate y$12_@1.push(read b$9) + } + [5] Const mutate $13_@2 = "div" + scope @3 [6:7] deps=[read $13_@2, freeze y$12_@1] { + [6] Const mutate $14_@3 = JSX {freeze y$12_@1} + } + [7] Call mutate x$11_@0.push(read $14_@3) + } else { + [9] Call mutate x$11_@0.push(read c$10) + } + } + return freeze x$11_@0 +} + +``` + +### CFG + +```mermaid +flowchart TB + %% Basic Blocks + subgraph bb0 + bb0_instrs[" + [1] Const mutate x$11_@0[1:11] = Array [] + "] + bb0_instrs --> bb0_terminal(["If (read a$8)"]) + end + subgraph bb2 + bb2_instrs[" + [3] Const mutate y$12_@1[3:5] = Array [] + [4] Call mutate y$12_@1.push(read b$9) + [5] Const mutate $13_@2 = 'div' + [6] Const mutate $14_@3 = JSX {freeze y$12_@1} + [7] Call mutate x$11_@0.push(read $14_@3) + "] + bb2_instrs --> bb2_terminal(["Goto"]) + end + subgraph bb3 + bb3_instrs[" + [9] Call mutate x$11_@0.push(read c$10) + "] + bb3_instrs --> bb3_terminal(["Goto"]) + end + subgraph bb1 + bb1_terminal(["Return freeze x$11_@0"]) + end + + %% Jumps + bb0_terminal -- "then" --> bb2 + bb0_terminal -- "else" --> bb3 + bb0_terminal -- "fallthrough" --> bb1 + bb2_terminal --> bb1 + bb3_terminal --> bb1 + +``` + +## Code + +```javascript +function foo$0(a$8, b$9, c$10) { + const x$11 = []; + bb1: if (a$8) { + const y$12 = []; + y$12.push(b$9); + x$11.push(
{y$12}
); + } else { + x$11.push(c$10); + } + + return x$11; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.js b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.js new file mode 100644 index 0000000000..22aee02f38 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes-if.js @@ -0,0 +1,11 @@ +function foo(a, b, c) { + const x = []; + if (a) { + const y = []; + y.push(b); + x.push(
{y}
); + } else { + x.push(c); + } + return x; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes.expect.md index 5b43a6ee47..c6744ced64 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reactive-scopes.expect.md @@ -46,6 +46,32 @@ scope4 [9:10]: - dependency: freeze x$10_@0 ``` +## Reactive Scopes + +``` +function f( + a, + b, +) { + scope @0 [1:8] deps=[read b$9, read b$9] { + [1] Const mutate x$10_@0[1:8] = Array [] + [2] Const mutate $11_@1 = 1 + [3] Const mutate $12_@2 = Binary read a$8.length === read $11_@1 + if (read $12_@2) { + if (read b$9) { + [6] Call mutate x$10_@0.push(read b$9) + } + } + } + [8] Const mutate $13_@3 = "div" + scope @4 [9:10] deps=[read $13_@3, freeze x$10_@0] { + [9] Const mutate $15_@4 = JSX {freeze x$10_@0} + } + return read $15_@4 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md index 0f8e1e816c..d1a74b564d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md @@ -51,6 +51,32 @@ scope2 [9:10]: - dependency: read y$8_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$6.p0, read props$6.p1] { + [1] Let mutate x$7_@0[1:7] = Array [] + [2] Call mutate x$7_@0.push(read props$6.p0) + [3] Const mutate y$8_@0[1:7] = read x$7_@0 + if (read props$6.p1) { + [5] Reassign mutate x$7_@0[1:7] = Array [] + } + } + scope @1 [7:8] deps=[read Component$0, freeze x$7_@0, read y$8_@0.push, read props$6.p2] { + [7] Const mutate _$12_@1 = JSX + } + [8] Call read y$8_@0.push(read props$6.p2) + scope @2 [9:10] deps=[read Component$0, read x$7_@0, read y$8_@0] { + [9] Const mutate $15_@2 = JSX + } + return read $15_@2 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reassignment-separate-scopes.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reassignment-separate-scopes.expect.md index 0960f0c3bb..903fd8be3c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reassignment-separate-scopes.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reassignment-separate-scopes.expect.md @@ -87,6 +87,51 @@ scope9 [19:20]: - dependency: read $28_@8 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:5] deps=[read a$13, read a$13] { + [1] Const mutate x$16_@0[1:5] = Array [] + if (read a$13) { + [3] Call mutate x$16_@0.push(read a$13) + } + } + [5] Const mutate $17_@1 = "div" + scope @2 [6:7] deps=[read $17_@1, freeze x$16_@0] { + [6] Const mutate y$19_@2 = JSX {freeze x$16_@0} + } + [7] Const mutate $20_@3 = 0 + scope @4 [8:15] deps=[read c$15, read b$14, read b$14] { + [8] Let mutate x$22_@4[8:15] = undefined + switch (read b$14) { + case read $20_@3: { + [9] Reassign mutate x$22_@4[8:15] = Array [] + [10] Call mutate x$22_@4.push(read b$14) + break bb3 + } + default: { + [12] Reassign mutate x$22_@4[8:15] = Array [] + [13] Call mutate x$22_@4.push(read c$15) + } + } + } + [15] Const mutate $25_@5 = "div" + [16] Const mutate $26_@6 = "\n " + [17] Const mutate $27_@7 = "\n " + [18] Const mutate $28_@8 = "\n " + scope @9 [19:20] deps=[read $25_@5, read $26_@6, read y$19_@2, read $27_@7, freeze x$22_@4, read $28_@8] { + [19] Const mutate $31_@9 = JSX {read $26_@6}{read y$19_@2}{read $27_@7}{freeze x$22_@4}{read $28_@8} + } + return read $31_@9 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md index b0db57ff2f..78abaa27e8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md @@ -41,6 +41,32 @@ scope3 [7:8]: - dependency: freeze y$8_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[read props$6.p0] { + [1] Const mutate x$7_@0[1:7] = Array [] + [2] Call mutate x$7_@0.push(read props$6.p0) + [3] Const mutate y$8_@0[1:7] = read x$7_@0 + scope @1 [4:5] deps=[] { + [4] Const mutate x$9_@1 = Array [] + } + scope @2 [5:6] deps=[read Component$0, freeze x$9_@1, read props$6.p1] { + [5] Const mutate _$10_@2 = JSX + } + [6] Call mutate y$8_@0.push(read props$6.p1) + } + scope @3 [7:8] deps=[read Component$0, read x$9_@1, freeze y$8_@0] { + [7] Const mutate $11_@3 = JSX + } + return read $11_@3 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md b/compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md index 7ab74156a9..e56b35d2c2 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md @@ -88,6 +88,48 @@ scope1 [2:18]: - dependency: read props$6.cond ``` +## Reactive Scopes + +``` +function Component( + props, +) { + [1] Const mutate x$7_@0 = undefined + scope @1 [2:18] deps=[read props$6.v2, read props$6.v1, read props$6.v0, read props$6.test, read props$6.b, read props$6.c, read props$6.cond2, read props$6.cond] { + [2] Let mutate x$11_@1[2:18] = undefined + if (read props$6.cond) { + [3] Const mutate $8_@2 = 2 + [4] Const mutate $9_@3 = 1 + [5] Const mutate $10_@4 = 0 + switch (read props$6.test) { + case read $10_@4: { + [7] Reassign mutate x$11_@1[2:18] = read props$6.v0 + break bb1 + } + case read $9_@3: { + [9] Reassign mutate x$11_@1[2:18] = read props$6.v1 + break bb1 + } + case read $8_@2: { + } + default: { + [11] Reassign mutate x$11_@1[2:18] = read props$6.v2 + } + } + } else { + if (read props$6.cond2) { + [14] Reassign mutate x$11_@1[2:18] = read props$6.b + } else { + [16] Reassign mutate x$11_@1[2:18] = read props$6.c + } + } + } + [18] read x$11_@1 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/simple-alias.expect.md b/compiler/forget/src/__tests__/fixtures/hir/simple-alias.expect.md index 9292e01da1..fd8ef9e6f0 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/simple-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/simple-alias.expect.md @@ -24,6 +24,16 @@ bb0: ``` +## Reactive Scopes + +``` +function mutate( +) { + return +} + +``` + ### CFG ```mermaid @@ -48,16 +58,37 @@ function mutate$0() {} ``` bb0: [1] Const mutate a$5_@0 = Object { } - [2] Const mutate b$6_@1[0:8] = Object { } - [3] Const mutate c$7_@1[0:8] = Object { } - [4] Const mutate a$8_@1[0:8] = read b$6_@1 - [5] Const mutate b$9_@1[0:8] = read c$7_@1 - [6] Const mutate c$10_@1[0:8] = read a$8_@1 - [7] Call mutate mutate$4_@1(mutate a$8_@1, mutate b$9_@1) + [2] Const mutate b$6_@1[2:8] = Object { } + [3] Const mutate c$7_@1[2:8] = Object { } + [4] Const mutate a$8_@1[2:8] = read b$6_@1 + [5] Const mutate b$9_@1[2:8] = read c$7_@1 + [6] Const mutate c$10_@1[2:8] = read a$8_@1 + [7] Call mutate mutate$4(mutate a$8_@1, mutate b$9_@1) [8] Return freeze c$10_@1 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate a$5_@0 = Object { } + } + scope @1 [2:8] deps=[] { + [2] Const mutate b$6_@1[2:8] = Object { } + [3] Const mutate c$7_@1[2:8] = Object { } + [4] Const mutate a$8_@1[2:8] = read b$6_@1 + [5] Const mutate b$9_@1[2:8] = read c$7_@1 + [6] Const mutate c$10_@1[2:8] = read a$8_@1 + [7] Call mutate mutate$4(mutate a$8_@1, mutate b$9_@1) + } + return freeze c$10_@1 +} + +``` + ### CFG ```mermaid @@ -66,12 +97,12 @@ flowchart TB subgraph bb0 bb0_instrs[" [1] Const mutate a$5_@0 = Object { } - [2] Const mutate b$6_@1[0:8] = Object { } - [3] Const mutate c$7_@1[0:8] = Object { } - [4] Const mutate a$8_@1[0:8] = read b$6_@1 - [5] Const mutate b$9_@1[0:8] = read c$7_@1 - [6] Const mutate c$10_@1[0:8] = read a$8_@1 - [7] Call mutate mutate$4_@1(mutate a$8_@1, mutate b$9_@1) + [2] Const mutate b$6_@1[2:8] = Object { } + [3] Const mutate c$7_@1[2:8] = Object { } + [4] Const mutate a$8_@1[2:8] = read b$6_@1 + [5] Const mutate b$9_@1[2:8] = read c$7_@1 + [6] Const mutate c$10_@1[2:8] = read a$8_@1 + [7] Call mutate mutate$4(mutate a$8_@1, mutate b$9_@1) "] bb0_instrs --> bb0_terminal(["Return freeze c$10_@1"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md b/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md index 98fc77e99a..8f7928b325 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md @@ -38,6 +38,30 @@ scope4 [7:8]: - dependency: read $13_@3 ``` +## Reactive Scopes + +``` +function foo( + x, + y, +) { + if (read x$8) { + [2] Const mutate $10_@0 = false + scope @1 [3:4] deps=[read foo$0, read $10_@0, read y$9] { + [3] Const mutate $11_@1 = Call read foo$0(read $10_@0, read y$9) + } + return freeze $11_@1 + } + [5] Const mutate $12_@2 = 10 + [6] Const mutate $13_@3 = Binary read y$9 * read $12_@2 + scope @4 [7:8] deps=[read $13_@3] { + [7] Const mutate $14_@4 = Array [read $13_@3] + } + return freeze $14_@4 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md index 500ad90a38..30e32e5c78 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md @@ -24,6 +24,22 @@ scope2 [3:4]: - dependency: read b$7_@1 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + [1] Const mutate a$6_@0 = 1 + [2] Const mutate b$7_@1 = 2 + scope @2 [3:4] deps=[read a$6_@0, read b$7_@1] { + [3] Const mutate x$8_@2 = Array [read a$6_@0, read b$7_@1] + } + return freeze x$8_@2 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md index ab34c709f7..511bbea405 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md @@ -26,6 +26,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -49,30 +59,60 @@ function foo$0() {} ``` bb0: - [1] Const mutate a$11_@0[0:10] = Array [] - [2] Const mutate b$12_@0[0:10] = Object { } - [3] Call mutate foo$4_@0(mutate a$11_@0, mutate b$12_@0) - [4] Const mutate $13_@0[0:10] = Call mutate foo$4_@0() - [5] If (read $13_@0) then:bb2 else:bb1 fallthrough=bb1 + [1] Const mutate a$11_@0[1:10] = Array [] + [2] Const mutate b$12_@0[1:10] = Object { } + [3] Call mutate foo$4(mutate a$11_@0, mutate b$12_@0) + [4] Const mutate $13_@1 = Call mutate foo$4() + [5] If (read $13_@1) then:bb2 else:bb1 fallthrough=bb1 bb2: predecessor blocks: bb0 - [6] Const mutate $14_@1 = "div" - [7] Const mutate _$15_@2 = JSX + [6] Const mutate $14_@2 = "div" + [7] Const mutate _$15_@3 = JSX [8] Goto bb1 bb1: predecessor blocks: bb2 bb0 - [9] Call mutate foo$4_@0(read a$11_@0, mutate b$12_@0) - [10] Const mutate $19_@3 = "div" - [11] Const mutate $20_@4 = JSX - [12] Return read $20_@4 -scope2 [7:8]: - - dependency: read $14_@1 -scope4 [11:12]: - - dependency: read $19_@3 + [9] Call mutate foo$4(read a$11_@0, mutate b$12_@0) + [10] Const mutate $19_@4 = "div" + [11] Const mutate $20_@5 = JSX + [12] Return read $20_@5 +scope3 [7:8]: + - dependency: read $14_@2 +scope5 [11:12]: + - dependency: read $19_@4 - dependency: freeze a$11_@0 - dependency: freeze b$12_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:10] deps=[] { + [1] Const mutate a$11_@0[1:10] = Array [] + [2] Const mutate b$12_@0[1:10] = Object { } + [3] Call mutate foo$4(mutate a$11_@0, mutate b$12_@0) + scope @1 [4:5] deps=[] { + [4] Const mutate $13_@1 = Call mutate foo$4() + } + if (read $13_@1) { + [6] Const mutate $14_@2 = "div" + scope @3 [7:8] deps=[read $14_@2] { + [7] Const mutate _$15_@3 = JSX + } + } + [9] Call mutate foo$4(read a$11_@0, mutate b$12_@0) + } + [10] Const mutate $19_@4 = "div" + scope @5 [11:12] deps=[read $19_@4, freeze a$11_@0, freeze b$12_@0] { + [11] Const mutate $20_@5 = JSX + } + return read $20_@5 +} + +``` + ### CFG ```mermaid @@ -80,27 +120,27 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$11_@0[0:10] = Array [] - [2] Const mutate b$12_@0[0:10] = Object { } - [3] Call mutate foo$4_@0(mutate a$11_@0, mutate b$12_@0) - [4] Const mutate $13_@0[0:10] = Call mutate foo$4_@0() + [1] Const mutate a$11_@0[1:10] = Array [] + [2] Const mutate b$12_@0[1:10] = Object { } + [3] Call mutate foo$4(mutate a$11_@0, mutate b$12_@0) + [4] Const mutate $13_@1 = Call mutate foo$4() "] - bb0_instrs --> bb0_terminal(["If (read $13_@0)"]) + bb0_instrs --> bb0_terminal(["If (read $13_@1)"]) end subgraph bb2 bb2_instrs[" - [6] Const mutate $14_@1 = 'div' - [7] Const mutate _$15_@2 = JSX + [6] Const mutate $14_@2 = 'div' + [7] Const mutate _$15_@3 = JSX "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb1 bb1_instrs[" - [9] Call mutate foo$4_@0(read a$11_@0, mutate b$12_@0) - [10] Const mutate $19_@3 = 'div' - [11] Const mutate $20_@4 = JSX + [9] Call mutate foo$4(read a$11_@0, mutate b$12_@0) + [10] Const mutate $19_@4 = 'div' + [11] Const mutate $20_@5 = JSX "] - bb1_instrs --> bb1_terminal(["Return read $20_@4"]) + bb1_instrs --> bb1_terminal(["Return read $20_@5"]) end %% Jumps diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md index 2b0fc4a432..4a1a28681b 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md @@ -23,6 +23,16 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -46,12 +56,12 @@ function foo$0() {} ``` bb0: - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] Call mutate foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = "div" [5] Const mutate _$13_@2 = JSX - [6] Call mutate foo$4_@0(read a$10_@0, mutate b$11_@0) + [6] Call mutate foo$4(read a$10_@0, mutate b$11_@0) [7] Const mutate $14_@3 = "div" [8] Const mutate $15_@4 = JSX [9] Return read $15_@4 @@ -63,6 +73,31 @@ scope4 [8:9]: - dependency: freeze b$11_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:7] deps=[] { + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) + [4] Const mutate $12_@1 = "div" + scope @2 [5:6] deps=[read $12_@1] { + [5] Const mutate _$13_@2 = JSX + } + [6] Call mutate foo$4(read a$10_@0, mutate b$11_@0) + } + [7] Const mutate $14_@3 = "div" + scope @4 [8:9] deps=[read $14_@3, read a$10_@0, freeze b$11_@0] { + [8] Const mutate $15_@4 = JSX + } + return read $15_@4 +} + +``` + ### CFG ```mermaid @@ -70,12 +105,12 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$10_@0[0:7] = Array [] - [2] Const mutate b$11_@0[0:7] = Object { } - [3] Call mutate foo$4_@0(mutate a$10_@0, mutate b$11_@0) + [1] Const mutate a$10_@0[1:7] = Array [] + [2] Const mutate b$11_@0[1:7] = Object { } + [3] Call mutate foo$4(mutate a$10_@0, mutate b$11_@0) [4] Const mutate $12_@1 = 'div' [5] Const mutate _$13_@2 = JSX - [6] Call mutate foo$4_@0(read a$10_@0, mutate b$11_@0) + [6] Call mutate foo$4(read a$10_@0, mutate b$11_@0) [7] Const mutate $14_@3 = 'div' [8] Const mutate $15_@4 = JSX "] diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md index c1cd70c44d..e2a9c4ac9f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md @@ -53,6 +53,31 @@ scope6 [13:14]: - dependency: read x$7_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:13] deps=[] { + [1] Let mutate x$7_@0[1:13] = 1 + [2] Const mutate y$8_@1 = 2 + [3] Const mutate $9_@2 = 2 + [4] Const mutate $10_@3 = Binary read y$8_@1 === read $9_@2 + if (read $10_@3) { + [6] Reassign mutate x$7_@0[1:13] = 3 + } + [8] Const mutate $12_@4 = 3 + [9] Const mutate $14_@5 = Binary read y$8_@1 === read $12_@4 + if (read $14_@5) { + [11] Reassign mutate x$7_@0[1:13] = 5 + } + } + [13] Const mutate y$18_@6 = read x$7_@0 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md index 2b906a0992..12bf726045 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md @@ -38,6 +38,26 @@ scope4 [8:9]: - dependency: read x$5_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:8] deps=[] { + [1] Let mutate x$5_@0[1:8] = 1 + [2] Const mutate y$6_@1 = 2 + [3] Const mutate $7_@2 = 2 + [4] Const mutate $8_@3 = Binary read y$6_@1 === read $7_@2 + if (read $8_@3) { + [6] Reassign mutate x$5_@0[1:8] = 3 + } + } + [8] Const mutate y$11_@4 = read x$5_@0 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-for-of.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-for-of.expect.md index 3b6f55073b..9f507073ee 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-for-of.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-for-of.expect.md @@ -41,6 +41,19 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( + cond, +) { + scope @0 [1:2] deps=[] { + [1] Const mutate items$5_@0 = Array [] + } +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-for-trivial-update.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-for-trivial-update.expect.md index 446ebf66ad..7b81f506f2 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-for-trivial-update.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-for-trivial-update.expect.md @@ -42,6 +42,33 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( +) { + scope @1 [1:13] deps=[] { + [1] Let mutate x$6_@1[1:13] = 1 + scope @3 [6:8] deps=[] { + for ( + [3] Const mutate i$7_@1[1:13] = 0 + ; + [5] Const mutate $8_@1[1:13] = 10 + [6] Const mutate $10_@3[6:8] = Binary read i$7_@1 < read $8_@1 + read $10_@3 + ; + read i$7_@1 + ) { + [8] Const mutate $11_@4 = 1 + [9] Reassign mutate x$6_@1[1:13] = Binary read x$6_@1 + read $11_@4 + } + } + } + return read x$6_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md index 2cbce2ee3b..e080b9975d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md @@ -44,6 +44,33 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( +) { + scope @1 [1:15] deps=[] { + [1] Let mutate x$7_@1[1:15] = 1 + for ( + [3] Let mutate i$8_@1[1:15] = 0 + ; + [5] Const mutate $9_@1[1:15] = 10 + [6] Const mutate $11_@1[1:15] = Binary read i$8_@1 < read $9_@1 + read $11_@1 + ; + [11] Const mutate $15_@1[1:15] = 1 + [12] Reassign mutate i$8_@1[1:15] = Binary read i$8_@1 + read $15_@1 + read i$8_@1 + ) { + [8] Const mutate $12_@3 = 1 + [9] Reassign mutate x$7_@1[1:15] = Binary read x$7_@1 + read $12_@3 + } + } + return read x$7_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md index 45ed0f01a9..eebed5a80f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md @@ -40,6 +40,23 @@ scope3 [6:7]: - dependency: read x$5_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate x$5_@0 = 1 + [2] Const mutate y$6_@1 = 2 + if (read y$6_@1) { + [4] Const mutate z$7_@2 = Binary read x$5_@0 + read y$6_@1 + } else { + [6] Const mutate z$8_@3 = read x$5_@0 + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-multiple-phis.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-multiple-phis.expect.md index c0bbfa338b..26b0f10ef8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-multiple-phis.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-multiple-phis.expect.md @@ -76,6 +76,43 @@ scope2 [3:20]: - dependency: read $14_@1 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, + d, +) { + [1] Const mutate x$13_@0 = 0 + [2] Const mutate $14_@1 = true + scope @2 [3:20] deps=[read a$9, read b$10, read c$11, read d$12, read $14_@1] { + [3] Let mutate x$18_@2[3:20] = undefined + if (read $14_@1) { + [4] Const mutate $15_@3 = true + if (read $15_@3) { + [6] Reassign mutate x$18_@2[3:20] = read a$9 + } else { + [8] Reassign mutate x$18_@2[3:20] = read b$10 + } + [10] read x$18_@2 + } else { + [12] Const mutate $19_@4 = true + if (read $19_@4) { + [14] Reassign mutate x$18_@2[3:20] = read c$11 + } else { + [16] Reassign mutate x$18_@2[3:20] = read d$12 + } + [18] read x$18_@2 + } + } + [20] read x$18_@2 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md index f63447d11e..829bb6c7db 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md @@ -56,6 +56,34 @@ scope1 [8:9]: - dependency: read x$9_@0 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + [1] Const mutate x$9_@0 = 0 + while ( + read a$6 + ) { + while ( + read b$7 + ) { + while ( + read c$8 + ) { + [8] Const mutate $13_@1 = 1 + [9] Binary read x$9_@0 + read $13_@1 + } + } + } + return read x$9_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md index 6ad92a1b1e..6189826912 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md @@ -41,6 +41,28 @@ scope0 [1:8]: - dependency: read b$6 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + scope @0 [1:8] deps=[read a$5, read c$7, read c$7, read b$6] { + [1] Const mutate x$8_@0[1:8] = read a$5 + if (read b$6) { + if (read c$7) { + [4] Reassign mutate x$8_@0[1:8] = read c$7 + } + [6] read x$8_@0 + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-reassignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-reassignment.expect.md index 23fd9620f9..35b5005307 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-reassignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-reassignment.expect.md @@ -43,6 +43,31 @@ scope0 [1:8]: - dependency: read a$7 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, + d, + e, +) { + scope @0 [1:8] deps=[read b$8, read d$10, read c$9, read a$7] { + [1] Let mutate x$12_@0[1:8] = null + if (read a$7) { + [3] Reassign mutate x$12_@0[1:8] = read b$8 + } else { + if (read c$9) { + [6] Reassign mutate x$12_@0[1:8] = read d$10 + } + } + } + return read x$12_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md index 6a87423160..959a0eba9d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md @@ -21,6 +21,16 @@ bb0: ``` +## Reactive Scopes + +``` +function Foo( +) { + return +} + +``` + ### CFG ```mermaid @@ -44,13 +54,29 @@ function Foo$0() {} ``` bb0: - [1] Const mutate a$7_@0[0:4] = Array [] - [2] Const mutate b$8_@0[0:4] = Object { } - [3] Const mutate c$9_@0[0:4] = New mutate Foo$5_@0(mutate a$7_@0, mutate b$8_@0) + [1] Const mutate a$7_@0[1:4] = Array [] + [2] Const mutate b$8_@0[1:4] = Object { } + [3] Const mutate c$9_@0[1:4] = New mutate Foo$5(mutate a$7_@0, mutate b$8_@0) [4] Return freeze c$9_@0 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @0 [1:4] deps=[] { + [1] Const mutate a$7_@0[1:4] = Array [] + [2] Const mutate b$8_@0[1:4] = Object { } + [3] Const mutate c$9_@0[1:4] = New mutate Foo$5(mutate a$7_@0, mutate b$8_@0) + } + return freeze c$9_@0 +} + +``` + ### CFG ```mermaid @@ -58,9 +84,9 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$7_@0[0:4] = Array [] - [2] Const mutate b$8_@0[0:4] = Object { } - [3] Const mutate c$9_@0[0:4] = New mutate Foo$5_@0(mutate a$7_@0, mutate b$8_@0) + [1] Const mutate a$7_@0[1:4] = Array [] + [2] Const mutate b$8_@0[1:4] = Object { } + [3] Const mutate c$9_@0[1:4] = New mutate Foo$5(mutate a$7_@0, mutate b$8_@0) "] bb0_instrs --> bb0_terminal(["Return freeze c$9_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md index a608db38bc..b725068b4a 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md @@ -44,6 +44,30 @@ scope3 [10:11]: - dependency: read y$7_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:10] deps=[] { + [1] Let mutate x$6_@0[1:10] = 1 + [2] Let mutate y$7_@0[1:10] = 2 + [3] Const mutate $8_@2 = 1 + [4] Const mutate $9_@0[1:10] = Binary read x$6_@0 > read $8_@2 + if (read $9_@0) { + [6] Reassign mutate x$6_@0[1:10] = 2 + } else { + [8] Reassign mutate y$7_@0[1:10] = 3 + } + } + scope @3 [10:11] deps=[read x$6_@0, read y$7_@0] { + [10] Const mutate t$14_@3 = Object { x: read x$6_@0, y: read y$7_@0 } + } + return freeze t$14_@3 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md index dc0a3999aa..f4ee57d3d8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md @@ -24,6 +24,22 @@ scope2 [3:4]: - dependency: read b$7_@1 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + [1] Const mutate a$6_@0 = 1 + [2] Const mutate b$7_@1 = 2 + scope @2 [3:4] deps=[read a$6_@0, read b$7_@1] { + [3] Const mutate x$8_@2 = Object { a: read a$6_@0, b: read b$7_@1 } + } + return freeze x$8_@2 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-alias-mutate-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-alias-mutate-if.expect.md index 97ab761b76..9acc1fc729 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-alias-mutate-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-alias-mutate-if.expect.md @@ -22,23 +22,47 @@ function foo(a) { ``` bb0: - [1] Const mutate b$8_@0[0:11] = Object { } - [2] Const mutate x$9_@0[0:11] = read b$8_@0 + [1] Const mutate b$8_@0[1:11] = Object { } + [2] Const mutate x$9_@0[1:11] = read b$8_@0 [3] If (read a$7) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [4] Const mutate y$10_@0[0:11] = Object { } - [5] Reassign mutate x$9_@0.y[0:11] = read y$10_@0 + [4] Const mutate y$10_@0[1:11] = Object { } + [5] Reassign mutate x$9_@0.y[1:11] = read y$10_@0 [6] Goto bb1 bb3: predecessor blocks: bb0 - [7] Const mutate z$11_@0[0:11] = Object { } - [8] Reassign mutate x$9_@0.z[0:11] = read z$11_@0 + [7] Const mutate z$11_@0[1:11] = Object { } + [8] Reassign mutate x$9_@0.z[1:11] = read z$11_@0 [9] Goto bb1 bb1: predecessor blocks: bb2 bb3 - [10] Call mutate mutate$6_@0(mutate b$8_@0) + [10] Call mutate mutate$6(mutate b$8_@0) [11] Return freeze x$9_@0 +scope0 [1:11]: + - dependency: read a$7 +``` + +## Reactive Scopes + +``` +function foo( + a, +) { + scope @0 [1:11] deps=[read a$7] { + [1] Const mutate b$8_@0[1:11] = Object { } + [2] Const mutate x$9_@0[1:11] = read b$8_@0 + if (read a$7) { + [4] Const mutate y$10_@0[1:11] = Object { } + [5] Reassign mutate x$9_@0.y[1:11] = read y$10_@0 + } else { + [7] Const mutate z$11_@0[1:11] = Object { } + [8] Reassign mutate x$9_@0.z[1:11] = read z$11_@0 + } + [10] Call mutate mutate$6(mutate b$8_@0) + } + return freeze x$9_@0 +} ``` @@ -49,28 +73,28 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate b$8_@0[0:11] = Object { } - [2] Const mutate x$9_@0[0:11] = read b$8_@0 + [1] Const mutate b$8_@0[1:11] = Object { } + [2] Const mutate x$9_@0[1:11] = read b$8_@0 "] bb0_instrs --> bb0_terminal(["If (read a$7)"]) end subgraph bb2 bb2_instrs[" - [4] Const mutate y$10_@0[0:11] = Object { } - [5] Reassign mutate x$9_@0.y[0:11] = read y$10_@0 + [4] Const mutate y$10_@0[1:11] = Object { } + [5] Reassign mutate x$9_@0.y[1:11] = read y$10_@0 "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" - [7] Const mutate z$11_@0[0:11] = Object { } - [8] Reassign mutate x$9_@0.z[0:11] = read z$11_@0 + [7] Const mutate z$11_@0[1:11] = Object { } + [8] Reassign mutate x$9_@0.z[1:11] = read z$11_@0 "] bb3_instrs --> bb3_terminal(["Goto"]) end subgraph bb1 bb1_instrs[" - [10] Call mutate mutate$6_@0(mutate b$8_@0) + [10] Call mutate mutate$6(mutate b$8_@0) "] bb1_instrs --> bb1_terminal(["Return freeze x$9_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-if.expect.md index 34e6e0bd2c..b4d3bf4c66 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-if.expect.md @@ -43,6 +43,31 @@ scope2 [6:7]: - dependency: mutate x$6_@0.z ``` +## Reactive Scopes + +``` +function foo( + a, +) { + scope @0 [1:9] deps=[read a$5] { + [1] Const mutate x$6_@0[1:9] = Object { } + if (read a$5) { + scope @1 [3:4] deps=[mutate x$6_@0.y] { + [3] Const mutate y$7_@1 = Object { } + } + [4] Reassign mutate x$6_@0.y[1:9] = read y$7_@1 + } else { + scope @2 [6:7] deps=[mutate x$6_@0.z] { + [6] Const mutate z$8_@2 = Object { } + } + [7] Reassign mutate x$6_@0.z[1:9] = read z$8_@2 + } + } + return freeze x$6_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-if.expect.md index 60db051fe8..6665a0c2b8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-if.expect.md @@ -21,22 +21,45 @@ function foo(a) { ``` bb0: - [1] Const mutate x$7_@0[0:10] = Object { } + [1] Const mutate x$7_@0[1:10] = Object { } [2] If (read a$6) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [3] Const mutate y$8_@0[0:10] = Object { } - [4] Reassign mutate x$7_@0.y[0:10] = read y$8_@0 + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 [5] Goto bb1 bb3: predecessor blocks: bb0 - [6] Const mutate z$9_@0[0:10] = Object { } - [7] Reassign mutate x$7_@0.z[0:10] = read z$9_@0 + [6] Const mutate z$9_@0[1:10] = Object { } + [7] Reassign mutate x$7_@0.z[1:10] = read z$9_@0 [8] Goto bb1 bb1: predecessor blocks: bb2 bb3 - [9] Call mutate mutate$5_@0(mutate x$7_@0) + [9] Call mutate mutate$5(mutate x$7_@0) [10] Return freeze x$7_@0 +scope0 [1:10]: + - dependency: read a$6 +``` + +## Reactive Scopes + +``` +function foo( + a, +) { + scope @0 [1:10] deps=[read a$6] { + [1] Const mutate x$7_@0[1:10] = Object { } + if (read a$6) { + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 + } else { + [6] Const mutate z$9_@0[1:10] = Object { } + [7] Reassign mutate x$7_@0.z[1:10] = read z$9_@0 + } + [9] Call mutate mutate$5(mutate x$7_@0) + } + return freeze x$7_@0 +} ``` @@ -47,27 +70,27 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$7_@0[0:10] = Object { } + [1] Const mutate x$7_@0[1:10] = Object { } "] bb0_instrs --> bb0_terminal(["If (read a$6)"]) end subgraph bb2 bb2_instrs[" - [3] Const mutate y$8_@0[0:10] = Object { } - [4] Reassign mutate x$7_@0.y[0:10] = read y$8_@0 + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" - [6] Const mutate z$9_@0[0:10] = Object { } - [7] Reassign mutate x$7_@0.z[0:10] = read z$9_@0 + [6] Const mutate z$9_@0[1:10] = Object { } + [7] Reassign mutate x$7_@0.z[1:10] = read z$9_@0 "] bb3_instrs --> bb3_terminal(["Goto"]) end subgraph bb1 bb1_instrs[" - [9] Call mutate mutate$5_@0(mutate x$7_@0) + [9] Call mutate mutate$5(mutate x$7_@0) "] bb1_instrs --> bb1_terminal(["Return freeze x$7_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-inside-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-inside-if.expect.md index 90b79932c8..1ee9f5e723 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-inside-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-inside-if.expect.md @@ -21,26 +21,52 @@ function foo(a) { ``` bb0: - [1] Const mutate x$7_@0[0:10] = Object { } + [1] Const mutate x$7_@0[1:10] = Object { } [2] If (read a$6) then:bb2 else:bb3 fallthrough=bb1 bb2: predecessor blocks: bb0 - [3] Const mutate y$8_@0[0:10] = Object { } - [4] Reassign mutate x$7_@0.y[0:10] = read y$8_@0 - [5] Call mutate mutate$4_@0(mutate y$8_@0) + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 + [5] Call mutate mutate$4(mutate y$8_@0) [6] Goto bb1 bb3: predecessor blocks: bb0 [7] Const mutate z$9_@1 = Object { } - [8] Reassign mutate x$7_@0.z[0:10] = read z$9_@1 + [8] Reassign mutate x$7_@0.z[1:10] = read z$9_@1 [9] Goto bb1 bb1: predecessor blocks: bb2 bb3 [10] Return freeze x$7_@0 +scope0 [1:10]: + - dependency: read a$6 scope1 [7:8]: - dependency: mutate x$7_@0.z ``` +## Reactive Scopes + +``` +function foo( + a, +) { + scope @0 [1:10] deps=[read a$6] { + [1] Const mutate x$7_@0[1:10] = Object { } + if (read a$6) { + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 + [5] Call mutate mutate$4(mutate y$8_@0) + } else { + scope @1 [7:8] deps=[mutate x$7_@0.z] { + [7] Const mutate z$9_@1 = Object { } + } + [8] Reassign mutate x$7_@0.z[1:10] = read z$9_@1 + } + } + return freeze x$7_@0 +} + +``` + ### CFG ```mermaid @@ -48,22 +74,22 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$7_@0[0:10] = Object { } + [1] Const mutate x$7_@0[1:10] = Object { } "] bb0_instrs --> bb0_terminal(["If (read a$6)"]) end subgraph bb2 bb2_instrs[" - [3] Const mutate y$8_@0[0:10] = Object { } - [4] Reassign mutate x$7_@0.y[0:10] = read y$8_@0 - [5] Call mutate mutate$4_@0(mutate y$8_@0) + [3] Const mutate y$8_@0[1:10] = Object { } + [4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0 + [5] Call mutate mutate$4(mutate y$8_@0) "] bb2_instrs --> bb2_terminal(["Goto"]) end subgraph bb3 bb3_instrs[" [7] Const mutate z$9_@1 = Object { } - [8] Reassign mutate x$7_@0.z[0:10] = read z$9_@1 + [8] Reassign mutate x$7_@0.z[1:10] = read z$9_@1 "] bb3_instrs --> bb3_terminal(["Goto"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate.expect.md index b605a460c5..3ff4b6ce3c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate.expect.md @@ -19,15 +19,32 @@ function foo() { ``` bb0: - [1] Const mutate a$5_@0[0:6] = Object { } - [2] Const mutate x$6_@0[0:6] = read a$5_@0 - [3] Const mutate y$7_@0[0:6] = Object { } - [4] Reassign mutate y$7_@0.x[0:6] = read x$6_@0 - [5] Call mutate mutate$4_@0(mutate a$5_@0) + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate x$6_@0[1:6] = read a$5_@0 + [3] Const mutate y$7_@0[1:6] = Object { } + [4] Reassign mutate y$7_@0.x[1:6] = read x$6_@0 + [5] Call mutate mutate$4(mutate a$5_@0) [6] Return freeze y$7_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:6] deps=[] { + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate x$6_@0[1:6] = read a$5_@0 + [3] Const mutate y$7_@0[1:6] = Object { } + [4] Reassign mutate y$7_@0.x[1:6] = read x$6_@0 + [5] Call mutate mutate$4(mutate a$5_@0) + } + return freeze y$7_@0 +} + +``` + ### CFG ```mermaid @@ -35,11 +52,11 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$5_@0[0:6] = Object { } - [2] Const mutate x$6_@0[0:6] = read a$5_@0 - [3] Const mutate y$7_@0[0:6] = Object { } - [4] Reassign mutate y$7_@0.x[0:6] = read x$6_@0 - [5] Call mutate mutate$4_@0(mutate a$5_@0) + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate x$6_@0[1:6] = read a$5_@0 + [3] Const mutate y$7_@0[1:6] = Object { } + [4] Reassign mutate y$7_@0.x[1:6] = read x$6_@0 + [5] Call mutate mutate$4(mutate a$5_@0) "] bb0_instrs --> bb0_terminal(["Return freeze y$7_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-call.expect.md index 49adb48798..527ca43175 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-call.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-call.expect.md @@ -24,6 +24,24 @@ scope1 [2:5]: - dependency: read x$4_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate x$4_@0 = Array [] + } + scope @1 [2:5] deps=[read x$4_@0] { + [2] Const mutate y$5_@1[2:5] = Object { x: read x$4_@0 } + [3] Const mutate $6_@1[2:5] = Array [] + [4] Call mutate y$5_@1.x.push(mutate $6_@1) + } + return freeze y$5_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-2.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-2.expect.md index 6530ca5d04..f1a977e859 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-2.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-2.expect.md @@ -16,14 +16,30 @@ function foo() { ``` bb0: - [1] Const mutate x$4_@0[0:5] = Array [] - [2] Const mutate y$5_@0[0:5] = Object { } - [3] Reassign mutate y$5_@0.x[0:5] = read x$4_@0 - [4] Call mutate mutate$3_@0(mutate x$4_@0) + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate x$4_@0) [5] Return freeze y$5_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:5] deps=[] { + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate x$4_@0) + } + return freeze y$5_@0 +} + +``` + ### CFG ```mermaid @@ -31,10 +47,10 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$4_@0[0:5] = Array [] - [2] Const mutate y$5_@0[0:5] = Object { } - [3] Reassign mutate y$5_@0.x[0:5] = read x$4_@0 - [4] Call mutate mutate$3_@0(mutate x$4_@0) + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate x$4_@0) "] bb0_instrs --> bb0_terminal(["Return freeze y$5_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-alias.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-alias.expect.md index a6ea974af6..dca62a4430 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate-alias.expect.md @@ -19,15 +19,32 @@ function foo() { ``` bb0: - [1] Const mutate a$5_@0[0:6] = Object { } - [2] Const mutate y$6_@0[0:6] = read a$5_@0 - [3] Const mutate x$7_@0[0:6] = Array [] - [4] Reassign mutate y$6_@0.x[0:6] = read x$7_@0 - [5] Call mutate mutate$4_@0(mutate a$5_@0) + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate y$6_@0[1:6] = read a$5_@0 + [3] Const mutate x$7_@0[1:6] = Array [] + [4] Reassign mutate y$6_@0.x[1:6] = read x$7_@0 + [5] Call mutate mutate$4(mutate a$5_@0) [6] Return freeze y$6_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:6] deps=[] { + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate y$6_@0[1:6] = read a$5_@0 + [3] Const mutate x$7_@0[1:6] = Array [] + [4] Reassign mutate y$6_@0.x[1:6] = read x$7_@0 + [5] Call mutate mutate$4(mutate a$5_@0) + } + return freeze y$6_@0 +} + +``` + ### CFG ```mermaid @@ -35,11 +52,11 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate a$5_@0[0:6] = Object { } - [2] Const mutate y$6_@0[0:6] = read a$5_@0 - [3] Const mutate x$7_@0[0:6] = Array [] - [4] Reassign mutate y$6_@0.x[0:6] = read x$7_@0 - [5] Call mutate mutate$4_@0(mutate a$5_@0) + [1] Const mutate a$5_@0[1:6] = Object { } + [2] Const mutate y$6_@0[1:6] = read a$5_@0 + [3] Const mutate x$7_@0[1:6] = Array [] + [4] Reassign mutate y$6_@0.x[1:6] = read x$7_@0 + [5] Call mutate mutate$4(mutate a$5_@0) "] bb0_instrs --> bb0_terminal(["Return freeze y$6_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate.expect.md index 6e29dc3e93..23ef8e40b4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property-mutate.expect.md @@ -16,14 +16,30 @@ function foo() { ``` bb0: - [1] Const mutate x$4_@0[0:5] = Array [] - [2] Const mutate y$5_@0[0:5] = Object { } - [3] Reassign mutate y$5_@0.x[0:5] = read x$4_@0 - [4] Call mutate mutate$3_@0(mutate y$5_@0) + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate y$5_@0) [5] Return freeze y$5_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:5] deps=[] { + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate y$5_@0) + } + return freeze y$5_@0 +} + +``` + ### CFG ```mermaid @@ -31,10 +47,10 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$4_@0[0:5] = Array [] - [2] Const mutate y$5_@0[0:5] = Object { } - [3] Reassign mutate y$5_@0.x[0:5] = read x$4_@0 - [4] Call mutate mutate$3_@0(mutate y$5_@0) + [1] Const mutate x$4_@0[1:5] = Array [] + [2] Const mutate y$5_@0[1:5] = Object { } + [3] Reassign mutate y$5_@0.x[1:5] = read x$4_@0 + [4] Call mutate mutate$3(mutate y$5_@0) "] bb0_instrs --> bb0_terminal(["Return freeze y$5_@0"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md index d517e2a897..7fea6cce69 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md @@ -23,6 +23,23 @@ scope1 [2:4]: - dependency: read x$3_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:2] deps=[] { + [1] Const mutate x$3_@0 = Array [] + } + scope @1 [2:4] deps=[read x$3_@0] { + [2] Const mutate y$4_@1[2:4] = Object { } + [3] Reassign mutate y$4_@1.x[2:4] = read x$3_@0 + } + return freeze y$4_@1 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-reassign.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-reassign.expect.md index 997c7f3e6d..d97ca11318 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-reassign.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-reassign.expect.md @@ -29,6 +29,23 @@ scope3 [4:5]: - dependency: read c$7 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, +) { + [1] Const mutate x$8_@0 = 0 + [2] Const mutate x$9_@1 = read a$5 + [3] Const mutate x$10_@2 = read b$6 + [4] Const mutate x$11_@3 = read c$7 + return read x$11_@3 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md index 0d055cbf5d..a08b13fc20 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md @@ -31,6 +31,24 @@ bb1: ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:7] deps=[] { + [1] Let mutate x$4_@0[1:7] = 1 + [2] Const mutate $5_@1 = 1 + [3] Const mutate $6_@0[1:7] = Binary read x$4_@0 === read $5_@1 + if (read $6_@0) { + [5] Reassign mutate x$4_@0[1:7] = 2 + } + } + return read x$4_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md index db3125e60a..32ecef51c1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md @@ -25,6 +25,16 @@ bb0: ``` +## Reactive Scopes + +``` +function log( +) { + return +} + +``` + ### CFG ```mermaid @@ -53,7 +63,7 @@ bb0: bb2: predecessor blocks: bb0 [3] Const mutate str$7_@1 = "other test" - [4] Call mutate log$4_@2(read str$7_@1) + [4] Call mutate log$4(read str$7_@1) [5] Goto bb1 bb3: predecessor blocks: bb0 @@ -61,8 +71,30 @@ bb3: [7] Goto bb1 bb1: predecessor blocks: bb2 bb3 - [8] Call mutate log$4_@2(read str$6_@0) + [8] Call mutate log$4(read str$6_@0) [9] Return +scope0 [1:8]: + - dependency: read cond$5 +``` + +## Reactive Scopes + +``` +function Foo( + cond, +) { + scope @0 [1:8] deps=[read cond$5] { + [1] Let mutate str$6_@0[1:8] = "" + if (read cond$5) { + [3] Const mutate str$7_@1 = "other test" + [4] Call mutate log$4(read str$7_@1) + } else { + [6] Reassign mutate str$6_@0[1:8] = "fallthrough test" + } + } + [8] Call mutate log$4(read str$6_@0) + return +} ``` @@ -80,7 +112,7 @@ flowchart TB subgraph bb2 bb2_instrs[" [3] Const mutate str$7_@1 = 'other test' - [4] Call mutate log$4_@2(read str$7_@1) + [4] Call mutate log$4(read str$7_@1) "] bb2_instrs --> bb2_terminal(["Goto"]) end @@ -92,7 +124,7 @@ flowchart TB end subgraph bb1 bb1_instrs[" - [8] Call mutate log$4_@2(read str$6_@0) + [8] Call mutate log$4(read str$6_@0) "] bb1_instrs --> bb1_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-sibling-phis.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-sibling-phis.expect.md index ef66c17ae3..f53e233660 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-sibling-phis.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-sibling-phis.expect.md @@ -78,6 +78,45 @@ scope5 [13:18]: - dependency: read $19_@4 ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, + d, +) { + [1] Const mutate x$13_@0 = 0 + [2] Const mutate $14_@1 = true + if (read $14_@1) { + [4] Const mutate $15_@2 = true + scope @3 [5:10] deps=[read a$9, read b$10, read $15_@2] { + [5] Let mutate x$16_@3[5:10] = undefined + if (read $15_@2) { + [6] Reassign mutate x$16_@3[5:10] = read a$9 + } else { + [8] Reassign mutate x$16_@3[5:10] = read b$10 + } + } + [10] read x$16_@3 + } else { + [12] Const mutate $19_@4 = true + scope @5 [13:18] deps=[read c$11, read d$12, read $19_@4] { + [13] Let mutate x$20_@5[13:18] = undefined + if (read $19_@4) { + [14] Reassign mutate x$20_@5[13:18] = read c$11 + } else { + [16] Reassign mutate x$20_@5[13:18] = read d$12 + } + } + [18] read x$20_@5 + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md index 7ec607f293..5039fdda75 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md @@ -46,6 +46,28 @@ scope4 [9:10]: - dependency: read y$8_@3 ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate y$5_@0 = 2 + [2] Const mutate $6_@1 = 1 + [3] Const mutate $7_@2 = Binary read y$5_@0 > read $6_@1 + scope @3 [4:9] deps=[read $7_@2] { + [4] Let mutate y$8_@3[4:9] = undefined + if (read $7_@2) { + [5] Reassign mutate y$8_@3[4:9] = 1 + } else { + [7] Reassign mutate y$8_@3[4:9] = 2 + } + } + [9] Const mutate x$11_@4 = read y$8_@3 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md index e935b42ec6..60ed0f7eb3 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md @@ -19,6 +19,18 @@ bb0: ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate x$3_@0 = 1 + [2] Const mutate y$4_@1 = 2 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md index 00e490f435..aa84ac55f9 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md @@ -32,6 +32,21 @@ scope2 [4:5]: - dependency: read y$5_@1 ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate x$4_@0 = 1 + [2] Const mutate y$5_@1 = 2 + if (read y$5_@1) { + [4] Const mutate z$6_@2 = Binary read x$4_@0 + read y$5_@1 + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md index 0477776811..9a4368ccc5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md @@ -76,6 +76,41 @@ scope9 [16:17]: - dependency: read x$16_@5 ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate x$10_@0 = 1 + [2] Const mutate $11_@1 = 2 + [3] Const mutate $12_@2 = Binary read x$10_@0 === read $11_@1 + [4] Const mutate $13_@3 = 1 + [5] Const mutate $14_@4 = Binary read x$10_@0 === read $13_@3 + scope @5 [6:16] deps=[read x$10_@0] { + [6] Let mutate x$16_@5[6:16] = undefined + switch (read x$10_@0) { + case read $14_@4: { + [7] Const mutate $15_@6 = 1 + [8] Reassign mutate x$16_@5[6:16] = Binary read x$10_@0 + read $15_@6 + break bb1 + } + case read $12_@2: { + [10] Const mutate $17_@7 = 2 + [11] Reassign mutate x$16_@5[6:16] = Binary read x$10_@0 + read $17_@7 + break bb1 + } + default: { + [13] Const mutate $19_@8 = 3 + [14] Reassign mutate x$16_@5[6:16] = Binary read x$10_@0 + read $19_@8 + } + } + } + [16] Const mutate y$22_@9 = read x$16_@5 + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md index cad5be1b3b..78eee9adf8 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md @@ -30,6 +30,24 @@ bb1: ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:7] deps=[] { + [1] Let mutate x$4_@0[1:7] = 1 + [2] Const mutate $5_@1 = 1 + [3] Const mutate $6_@0[1:7] = Binary read x$4_@0 === read $5_@1 + if (read $6_@0) { + [5] Reassign mutate x$4_@0[1:7] = 2 + } + } + throw read x$4_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-while-no-reassign.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-while-no-reassign.expect.md index 2307b9a5b3..1dd13a8803 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-while-no-reassign.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-while-no-reassign.expect.md @@ -38,6 +38,29 @@ scope3 [6:7]: - dependency: read x$5_@0 ``` +## Reactive Scopes + +``` +function foo( +) { + [1] Const mutate x$5_@0 = 1 + scope @1 [3:6] deps=[] { + scope @2 [4:6] deps=[read x$5_@0] { + while ( + [3] Const mutate $6_@1[3:6] = 10 + [4] Const mutate $8_@2[4:6] = Binary read x$5_@0 < read $6_@1 + read $8_@2 + ) { + [6] Const mutate $9_@3 = 1 + [7] Binary read x$5_@0 + read $9_@3 + } + } + } + return read x$5_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md index 32e88277ad..63d41bf541 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md @@ -35,6 +35,27 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( +) { + scope @0 [1:9] deps=[] { + [1] Let mutate x$5_@0[1:9] = 1 + while ( + [3] Const mutate $6_@0[1:9] = 10 + [4] Const mutate $8_@0[1:9] = Binary read x$5_@0 < read $6_@0 + read $8_@0 + ) { + [6] Const mutate $9_@2 = 1 + [7] Reassign mutate x$5_@0[1:9] = Binary read x$5_@0 + read $9_@2 + } + } + return read x$5_@0 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md b/compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md index 5c98e702b0..144dfe7b37 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md @@ -69,6 +69,47 @@ scope6 [14:15]: - dependency: read child$19_@5 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @1 [1:12] deps=[] { + [1] Const mutate x$10_@1[1:12] = Array [] + [2] Let mutate y$11_@1[1:12] = undefined + [3] Const mutate $12_@2 = false + [4] Const mutate $13_@3 = true + [5] Const mutate $14_@4 = 1 + switch (read props$9.p0) { + case read $14_@4: { + break bb1 + } + case read $13_@3: { + [7] Call mutate x$10_@1.push(read props$9.p2) + [8] Reassign mutate y$11_@1[1:12] = Array [] + break bb1 + } + default: { + break bb1 + } + case read $12_@2: { + [10] Reassign mutate y$11_@1[1:12] = read x$10_@1 + } + } + } + scope @5 [12:13] deps=[read Component$0, freeze x$10_@1, read y$11_@1.push, read props$9.p4] { + [12] Const mutate child$19_@5 = JSX + } + [13] Call read y$11_@1.push(read props$9.p4) + scope @6 [14:15] deps=[read Component$0, freeze y$11_@1, read child$19_@5] { + [14] Const mutate $22_@6 = JSX {read child$19_@5} + } + return read $22_@6 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md b/compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md index 4c1fc55365..aaaed0699d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md @@ -82,6 +82,49 @@ bb1: ``` +## Reactive Scopes + +``` +function foo( + x, +) { + [1] Const mutate y$10_@0 = undefined + [2] Const mutate $11_@1 = 5 + [3] Const mutate $12_@2 = 4 + [4] Const mutate $13_@3 = 3 + [5] Const mutate $14_@4 = 2 + [6] Const mutate $15_@5 = 1 + [7] Const mutate $16_@6 = 0 + switch (read x$9) { + case read $16_@6: { + [9] Const mutate y$17_@7 = 0 + } + case read $15_@5: { + [11] Const mutate y$18_@8 = 1 + break bb1 + } + case read $14_@4: { + break bb1 + } + case read $13_@3: { + [13] Const mutate y$19_@9 = 3 + break bb1 + } + case read $12_@2: { + [15] Const mutate y$20_@10 = 4 + } + case read $11_@1: { + [17] Const mutate y$21_@11 = 5 + } + default: { + [19] Const mutate y$22_@12 = 0 + } + } + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/switch.expect.md b/compiler/forget/src/__tests__/fixtures/hir/switch.expect.md index ef207bf344..66c692e540 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/switch.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/switch.expect.md @@ -64,6 +64,42 @@ scope6 [14:15]: - dependency: read child$19_@5 ``` +## Reactive Scopes + +``` +function Component( + props, +) { + scope @1 [1:12] deps=[] { + [1] Const mutate x$9_@1[1:12] = Array [] + [2] Let mutate y$10_@1[1:12] = undefined + [3] Const mutate $11_@2 = false + [4] Const mutate $12_@3 = true + switch (read props$8.p0) { + case read $12_@3: { + [6] Call mutate x$9_@1.push(read props$8.p2) + [7] Call mutate x$9_@1.push(read props$8.p3) + scope @4 [8:9] deps=[] { + [8] Const mutate y$13_@4 = Array [] + } + } + case read $11_@2: { + [10] Reassign mutate y$10_@1[1:12] = read x$9_@1 + } + } + } + scope @5 [12:13] deps=[read Component$0, freeze x$9_@1, read y$10_@1.push, read props$8.p4] { + [12] Const mutate child$19_@5 = JSX + } + [13] Call read y$10_@1.push(read props$8.p4) + scope @6 [14:15] deps=[read Component$0, read y$10_@1, read child$19_@5] { + [14] Const mutate $23_@6 = JSX {read child$19_@5} + } + return read $23_@6 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/transitive-alias-fields.expect.md b/compiler/forget/src/__tests__/fixtures/hir/transitive-alias-fields.expect.md index 4766d851df..cd74924400 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/transitive-alias-fields.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/transitive-alias-fields.expect.md @@ -21,18 +21,38 @@ function component() { ``` bb0: - [1] Const mutate x$6_@0[0:9] = Object { } - [2] Const mutate p$7_@0[0:9] = Object { } - [3] Const mutate q$8_@0[0:9] = Object { } - [4] Const mutate y$9_@0[0:9] = Object { } - [5] Reassign mutate x$6_@0.y[0:9] = read y$9_@0 - [6] Reassign mutate p$7_@0.y[0:9] = read x$6_@0.y - [7] Reassign mutate q$8_@0.y[0:9] = read p$7_@0.y - [8] Call mutate mutate$5_@0(mutate q$8_@0) + [1] Const mutate x$6_@0[1:9] = Object { } + [2] Const mutate p$7_@0[1:9] = Object { } + [3] Const mutate q$8_@0[1:9] = Object { } + [4] Const mutate y$9_@0[1:9] = Object { } + [5] Reassign mutate x$6_@0.y[1:9] = read y$9_@0 + [6] Reassign mutate p$7_@0.y[1:9] = read x$6_@0.y + [7] Reassign mutate q$8_@0.y[1:9] = read p$7_@0.y + [8] Call mutate mutate$5(mutate q$8_@0) [9] Return ``` +## Reactive Scopes + +``` +function component( +) { + scope @0 [1:9] deps=[] { + [1] Const mutate x$6_@0[1:9] = Object { } + [2] Const mutate p$7_@0[1:9] = Object { } + [3] Const mutate q$8_@0[1:9] = Object { } + [4] Const mutate y$9_@0[1:9] = Object { } + [5] Reassign mutate x$6_@0.y[1:9] = read y$9_@0 + [6] Reassign mutate p$7_@0.y[1:9] = read x$6_@0.y + [7] Reassign mutate q$8_@0.y[1:9] = read p$7_@0.y + [8] Call mutate mutate$5(mutate q$8_@0) + } + return +} + +``` + ### CFG ```mermaid @@ -40,14 +60,14 @@ flowchart TB %% Basic Blocks subgraph bb0 bb0_instrs[" - [1] Const mutate x$6_@0[0:9] = Object { } - [2] Const mutate p$7_@0[0:9] = Object { } - [3] Const mutate q$8_@0[0:9] = Object { } - [4] Const mutate y$9_@0[0:9] = Object { } - [5] Reassign mutate x$6_@0.y[0:9] = read y$9_@0 - [6] Reassign mutate p$7_@0.y[0:9] = read x$6_@0.y - [7] Reassign mutate q$8_@0.y[0:9] = read p$7_@0.y - [8] Call mutate mutate$5_@0(mutate q$8_@0) + [1] Const mutate x$6_@0[1:9] = Object { } + [2] Const mutate p$7_@0[1:9] = Object { } + [3] Const mutate q$8_@0[1:9] = Object { } + [4] Const mutate y$9_@0[1:9] = Object { } + [5] Reassign mutate x$6_@0.y[1:9] = read y$9_@0 + [6] Reassign mutate p$7_@0.y[1:9] = read x$6_@0.y + [7] Reassign mutate q$8_@0.y[1:9] = read p$7_@0.y + [8] Call mutate mutate$5(mutate q$8_@0) "] bb0_instrs --> bb0_terminal(["Return"]) end diff --git a/compiler/forget/src/__tests__/fixtures/hir/while-break.expect.md b/compiler/forget/src/__tests__/fixtures/hir/while-break.expect.md index bfcbbd99d8..69a9788e72 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/while-break.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/while-break.expect.md @@ -25,6 +25,23 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( + a, + b, +) { + while ( + read a$3 + ) { + break + } + return read b$4 +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md b/compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md index 8beb1687f4..0793867115 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md @@ -40,6 +40,29 @@ bb2: ``` +## Reactive Scopes + +``` +function foo( + a, + b, + c, + d, +) { + while ( + read a$5 + ) { + if (read b$6) { + continue + } + [5] Call read c$7() + } + [7] Call read d$8() + return +} + +``` + ### CFG ```mermaid diff --git a/compiler/forget/src/__tests__/hir-test.ts b/compiler/forget/src/__tests__/hir-test.ts index 7699373b46..97e004a192 100644 --- a/compiler/forget/src/__tests__/hir-test.ts +++ b/compiler/forget/src/__tests__/hir-test.ts @@ -14,9 +14,11 @@ import { wasmFolder } from "@hpcc-js/wasm"; import invariant from "invariant"; import path from "path"; import prettier from "prettier"; +import { buildReactiveFunction } from "../HIR/BuildReactiveFunction"; import { toggleLogging } from "../HIR/logger"; import run from "../HIR/Pipeline"; import { printFunction } from "../HIR/PrintHIR"; +import { printReactiveFunction } from "../HIR/PrintReactiveFunction"; import visualizeHIRMermaid from "../HIR/VisualizeHIRMermaid"; import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures"; @@ -49,7 +51,7 @@ describe("React Forget (HIR version)", () => { } } - let items: Array<[string, string, string]> | null = null; + let items: Array | null = null; let error: Error | null = null; if (options.debug) { toggleLogging(options.debug); @@ -101,29 +103,37 @@ ${wrapWithTripleBackticks(error.message)} `; } -function formatOutput(items: Array<[string, string, string]>): Array { - return items.map(([hir, text, visualization]) => { +function formatOutput(items: Array): Array { + return items.map(({ ir, js, mermaid, scopes }) => { return ` ## HIR -${wrapWithTripleBackticks(hir)} +${wrapWithTripleBackticks(ir)} + +## Reactive Scopes + +${wrapWithTripleBackticks(scopes)} ### CFG -${wrapWithTripleBackticks(visualization, "mermaid")} +${wrapWithTripleBackticks(mermaid, "mermaid")} ## Code -${wrapWithTripleBackticks(text, "javascript")} +${wrapWithTripleBackticks(js, "javascript")} `.trim(); }); } -function transform( - text: string, - file: string -): Array<[string, string, string]> { - const items: Array<[string, string, string]> = []; +type TestOutput = { + ir: string; + js: string; + mermaid: string; + scopes: string; +}; + +function transform(text: string, file: string): Array { + const items: Array = []; const ast = parser.parse(text, { sourceFilename: file, plugins: ["typescript", "jsx"], @@ -142,15 +152,18 @@ function transform( codegen: true, }); + const reactiveFunction = buildReactiveFunction(ir); + const scopes = printReactiveFunction(reactiveFunction); + const textHIR = printFunction(ir); - const visualization = visualizeHIRMermaid(ir); + const mermaid = visualizeHIRMermaid(ir); invariant(ast !== null, "ast is null when codegen option is enabled"); const text = prettier.format(generate(ast).code.replace("\n\n", "\n"), { semi: true, parser: "babel-ts", }); - items.push([textHIR, text, visualization]); + items.push({ ir: textHIR, js: text, scopes, mermaid }); }, }, }); diff --git a/compiler/forget/src/index.ts b/compiler/forget/src/index.ts index 5dc5256782..febd404e7c 100644 --- a/compiler/forget/src/index.ts +++ b/compiler/forget/src/index.ts @@ -25,6 +25,7 @@ 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 { eliminateRedundantPhi } from "./HIR/EliminateRedundantPhi"; import enterSSA from "./HIR/EnterSSA"; @@ -35,7 +36,8 @@ import { inferReactiveScopes } from "./HIR/InferReactiveScopes"; import { inferReactiveScopeVariables } from "./HIR/InferReactiveScopeVariables"; import inferReferenceEffects from "./HIR/InferReferenceEffects"; import { leaveSSA } from "./HIR/LeaveSSA"; -import printHIR from "./HIR/PrintHIR"; +import printHIR, { printFunction } from "./HIR/PrintHIR"; +import { printReactiveFunction } from "./HIR/PrintReactiveFunction"; function parseFunctions( source: string @@ -68,6 +70,9 @@ export const HIR = { inferReactiveScopeDependencies, inferReactiveScopeVariables, inferReactiveScopes, + buildReactiveFunction, + printReactiveFunction, + printFunction, printHIR, Environment, leaveSSA,