mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[ssa] Add support for other kinds of Instructions
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
function Component(props) {
|
||||
const a = 1;
|
||||
const b = 2;
|
||||
const x = [a, b];
|
||||
return x;
|
||||
}
|
||||
@@ -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;
|
||||
("<<TODO: handle complex control flow in codegen>>");
|
||||
} else {
|
||||
y$10 = 3;
|
||||
("<<TODO: handle complex control flow in codegen>>");
|
||||
}
|
||||
|
||||
let t$13 = {
|
||||
x: x$11,
|
||||
y: y$12,
|
||||
};
|
||||
return t$13;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
function Component(props) {
|
||||
const a = 1;
|
||||
const b = 2;
|
||||
const x = { a: a, b: b };
|
||||
return x;
|
||||
}
|
||||
Reference in New Issue
Block a user