@samitouri / QOS-React / commits / 75871d7de7

[ssa] Add support for other kinds of Instructions

Sathya Gunasekaran committed Oct 24, 2022 at 16:04 UTC 75871d7de751d0fed77981c20159f491465d05ca
7 files changed +208 -1
compiler/forget/src/HIR/SSAify.ts
+42 -1
@@ -272,7 +272,6 @@ function rewriteUsesAndCollectOutputs(
272 function rewriteUses(instr: Instruction, builder: SSABuilder) {
273 const instrValue = instr.value;
274
275 - // TODO(gsn): Handle more kinds of Instructions
275 switch (instrValue.kind) {
276 case "BinaryExpression": {
277 instrValue.left = builder.getPlace(instrValue.left);
@@ -283,5 +282,47 @@ function rewriteUses(instr: Instruction, builder: SSABuilder) {
282 instr.value = builder.getPlace(instrValue);
283 break;
284 }
285 + case "NewExpression":
286 + case "CallExpression": {
287 + instrValue.callee = builder.getPlace(instrValue.callee);
288 + instrValue.args = instrValue.args.map((arg) => builder.getPlace(arg));
289 + break;
290 + }
291 + case "UnaryExpression": {
292 + instrValue.value = builder.getPlace(instrValue.value);
293 + break;
294 + }
295 + case "JsxExpression": {
296 + instrValue.tag = builder.getPlace(instrValue.tag);
297 + for (const [prop, place] of instrValue.props.entries()) {
298 + instrValue.props.set(prop, builder.getPlace(place));
299 + }
300 + if (instrValue.children) {
301 + instrValue.children = instrValue.children.map((p) =>
302 + builder.getPlace(p)
303 + );
304 + }
305 + break;
306 + }
307 + case "ObjectExpression": {
308 + if (instrValue.properties !== null) {
309 + for (const [prop, place] of instrValue.properties) {
310 + instrValue.properties?.set(prop, builder.getPlace(place));
311 + }
312 + }
313 + break;
314 + }
315 + case "ArrayExpression": {
316 + instrValue.elements = instrValue.elements.map((e) => builder.getPlace(e));
317 + break;
318 + }
319 + case "OtherStatement":
320 + case "Primitive":
321 + case "JSXText": {
322 + break;
323 + }
324 + default: {
325 + assertExhaustive(instrValue, "Unexpected instruction kind");
326 + }
327 }
328 }
compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md new
+35
@@ -0,0 +1,35 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const a = 1;
7 + const b = 2;
8 + const x = [a, b];
9 + return x;
10 +}
11 +
12 +```
13 +
14 +## HIR
15 +
16 +```
17 +bb0:
18 + Const mutate a$5 = 1
19 + Const mutate b$6 = 2
20 + Const mutate x$7 = Array [mutate a$5, mutate b$6]
21 + Return mutate x$7
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +function Component$0(props$1) {
28 + const a$5 = 1;
29 + const b$6 = 2;
30 + const x$7 = [a$5, b$6];
31 + return x$7;
32 +}
33 +
34 +```
35 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.js new
+6
@@ -0,0 +1,6 @@
1 +function Component(props) {
2 + const a = 1;
3 + const b = 2;
4 + const x = [a, b];
5 + return x;
6 +}
compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md new
+68
@@ -0,0 +1,68 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function foo() {
6 + let x = 1;
7 + let y = 2;
8 +
9 + if (x > 1) {
10 + x = 2;
11 + } else {
12 + y = 3;
13 + }
14 +
15 + let t = { x: x, y: y };
16 + return t;
17 +}
18 +
19 +```
20 +
21 +## HIR
22 +
23 +```
24 +bb0:
25 + Let mutate x$6 = 1
26 + Let mutate y$7 = 2
27 + Const mutate $8 = 1
28 + Const mutate $9 = Binary mutate x$6 > mutate $8
29 + If (mutate $9) then:bb2 else:bb3
30 +bb2:
31 + predecessor blocks: bb0
32 + Reassign mutate x$14 = 2
33 + Goto bb1
34 +bb3:
35 + predecessor blocks: bb0
36 + Reassign mutate y$10 = 3
37 + Goto bb1
38 +bb1:
39 + predecessor blocks: bb3 bb2
40 + Const mutate x$11: phi(bb3: mutate x$6, bb2: mutate x$14)
41 + Const mutate y$12: phi(bb3: mutate y$10, bb2: mutate y$7)
42 + Let mutate t$13 = Object { x: mutate x$11, y: mutate y$12 }
43 + Return mutate t$13
44 +```
45 +
46 +## Code
47 +
48 +```javascript
49 +function foo$0() {
50 + let x$6 = 1;
51 + let y$7 = 2;
52 + if (x$6 > 1) {
53 + x$14 = 2;
54 + ("<<TODO: handle complex control flow in codegen>>");
55 + } else {
56 + y$10 = 3;
57 + ("<<TODO: handle complex control flow in codegen>>");
58 + }
59 +
60 + let t$13 = {
61 + x: x$11,
62 + y: y$12,
63 + };
64 + return t$13;
65 +}
66 +
67 +```
68 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.js new
+13
@@ -0,0 +1,13 @@
1 +function foo() {
2 + let x = 1;
3 + let y = 2;
4 +
5 + if (x > 1) {
6 + x = 2;
7 + } else {
8 + y = 3;
9 + }
10 +
11 + let t = { x: x, y: y };
12 + return t;
13 +}
compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md new
+38
@@ -0,0 +1,38 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const a = 1;
7 + const b = 2;
8 + const x = { a: a, b: b };
9 + return x;
10 +}
11 +
12 +```
13 +
14 +## HIR
15 +
16 +```
17 +bb0:
18 + Const mutate a$5 = 1
19 + Const mutate b$6 = 2
20 + Const mutate x$7 = Object { a: mutate a$5, b: mutate b$6 }
21 + Return mutate x$7
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +function Component$0(props$1) {
28 + const a$5 = 1;
29 + const b$6 = 2;
30 + const x$7 = {
31 + a: a$5,
32 + b: b$6,
33 + };
34 + return x$7;
35 +}
36 +
37 +```
38 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.js new
+6
@@ -0,0 +1,6 @@
1 +function Component(props) {
2 + const a = 1;
3 + const b = 2;
4 + const x = { a: a, b: b };
5 + return x;
6 +}