diff --git a/compiler/forget/src/HIR/SSAify.ts b/compiler/forget/src/HIR/SSAify.ts index 8c5a05ddea..2eab9c35d5 100644 --- a/compiler/forget/src/HIR/SSAify.ts +++ b/compiler/forget/src/HIR/SSAify.ts @@ -272,7 +272,6 @@ function rewriteUsesAndCollectOutputs( function rewriteUses(instr: Instruction, builder: SSABuilder) { const instrValue = instr.value; - // TODO(gsn): Handle more kinds of Instructions switch (instrValue.kind) { case "BinaryExpression": { instrValue.left = builder.getPlace(instrValue.left); @@ -283,5 +282,47 @@ function rewriteUses(instr: Instruction, builder: SSABuilder) { instr.value = builder.getPlace(instrValue); break; } + case "NewExpression": + case "CallExpression": { + instrValue.callee = builder.getPlace(instrValue.callee); + instrValue.args = instrValue.args.map((arg) => builder.getPlace(arg)); + break; + } + case "UnaryExpression": { + instrValue.value = builder.getPlace(instrValue.value); + break; + } + case "JsxExpression": { + instrValue.tag = builder.getPlace(instrValue.tag); + for (const [prop, place] of instrValue.props.entries()) { + instrValue.props.set(prop, builder.getPlace(place)); + } + if (instrValue.children) { + instrValue.children = instrValue.children.map((p) => + builder.getPlace(p) + ); + } + break; + } + case "ObjectExpression": { + if (instrValue.properties !== null) { + for (const [prop, place] of instrValue.properties) { + instrValue.properties?.set(prop, builder.getPlace(place)); + } + } + break; + } + case "ArrayExpression": { + instrValue.elements = instrValue.elements.map((e) => builder.getPlace(e)); + break; + } + case "OtherStatement": + case "Primitive": + case "JSXText": { + break; + } + default: { + assertExhaustive(instrValue, "Unexpected instruction kind"); + } } } diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md new file mode 100644 index 0000000000..ed5f9bd1f7 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + const a = 1; + const b = 2; + const x = [a, b]; + return x; +} + +``` + +## HIR + +``` +bb0: + Const mutate a$5 = 1 + Const mutate b$6 = 2 + Const mutate x$7 = Array [mutate a$5, mutate b$6] + Return mutate x$7 +``` + +## Code + +```javascript +function Component$0(props$1) { + const a$5 = 1; + const b$6 = 2; + const x$7 = [a$5, b$6]; + return x$7; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.js b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.js new file mode 100644 index 0000000000..8acfd9aa7d --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.js @@ -0,0 +1,6 @@ +function Component(props) { + const a = 1; + const b = 2; + const x = [a, b]; + return x; +} 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 new file mode 100644 index 0000000000..df19f8759e --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md @@ -0,0 +1,68 @@ + +## Input + +```javascript +function foo() { + let x = 1; + let y = 2; + + if (x > 1) { + x = 2; + } else { + y = 3; + } + + let t = { x: x, y: y }; + return t; +} + +``` + +## HIR + +``` +bb0: + Let mutate x$6 = 1 + Let mutate y$7 = 2 + Const mutate $8 = 1 + Const mutate $9 = Binary mutate x$6 > mutate $8 + If (mutate $9) then:bb2 else:bb3 +bb2: + predecessor blocks: bb0 + Reassign mutate x$14 = 2 + Goto bb1 +bb3: + predecessor blocks: bb0 + Reassign mutate y$10 = 3 + Goto bb1 +bb1: + predecessor blocks: bb3 bb2 + Const mutate x$11: phi(bb3: mutate x$6, bb2: mutate x$14) + Const mutate y$12: phi(bb3: mutate y$10, bb2: mutate y$7) + Let mutate t$13 = Object { x: mutate x$11, y: mutate y$12 } + Return mutate t$13 +``` + +## Code + +```javascript +function foo$0() { + let x$6 = 1; + let y$7 = 2; + if (x$6 > 1) { + x$14 = 2; + ("<>"); + } else { + y$10 = 3; + ("<>"); + } + + let t$13 = { + x: x$11, + y: y$12, + }; + return t$13; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.js b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.js new file mode 100644 index 0000000000..a0a481fabd --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.js @@ -0,0 +1,13 @@ +function foo() { + let x = 1; + let y = 2; + + if (x > 1) { + x = 2; + } else { + y = 3; + } + + let t = { x: x, y: y }; + return t; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md new file mode 100644 index 0000000000..d777532d6c --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md @@ -0,0 +1,38 @@ + +## Input + +```javascript +function Component(props) { + const a = 1; + const b = 2; + const x = { a: a, b: b }; + return x; +} + +``` + +## HIR + +``` +bb0: + Const mutate a$5 = 1 + Const mutate b$6 = 2 + Const mutate x$7 = Object { a: mutate a$5, b: mutate b$6 } + Return mutate x$7 +``` + +## Code + +```javascript +function Component$0(props$1) { + const a$5 = 1; + const b$6 = 2; + const x$7 = { + a: a$5, + b: b$6, + }; + return x$7; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.js b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.js new file mode 100644 index 0000000000..c4d01b71ae --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.js @@ -0,0 +1,6 @@ +function Component(props) { + const a = 1; + const b = 2; + const x = { a: a, b: b }; + return x; +}