Fixture tests for 2 cases of expressions that produce incorrect output
They're fairly related, but I figured it's worth keeping more examples. - For the `while` example we need to codegen into a single expression. - For the expression with contained assignment we need to either keep the SSA ids around or re-create a similar expression during codegen.
Jan Kassens committed
Nov 29, 2022 at 18:33 UTC
aa2b152ea8d04896e36702b5065836af39b40bd6
4 files changed
+183
compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md
new
+60
@@ -0,0 +1,60 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function f() {
6
+ let x = 1;
7
+ // BUG: `x` has different values within this expression. Currently, the
8
+ // assignment is evaluated too early.
9
+ return x + (x = 2) + x;
10
+}
11
+
12
+```
13
+
14
+## HIR
15
+
16
+```
17
+bb0:
18
+ [1] Let mutate x$4_@0 = 1
19
+ [2] Reassign mutate x$5_@1 = 2
20
+ [3] Const mutate $6_@2 = Binary read x$5_@1 + read x$5_@1
21
+ [4] Const mutate $7_@3 = Binary read $6_@2 + read x$5_@1
22
+ [5] Return read $7_@3
23
+scope2 [3:4]:
24
+ - read x$5_@1
25
+ - read x$5_@1
26
+scope3 [4:5]:
27
+ - read $6_@2
28
+ - read x$5_@1
29
+```
30
+
31
+### CFG
32
+
33
+```mermaid
34
+flowchart TB
35
+ %% Basic Blocks
36
+ subgraph bb0
37
+ bb0_instrs["
38
+ [1] Let mutate x$4_@0 = 1
39
+ [2] Reassign mutate x$5_@1 = 2
40
+ [3] Const mutate $6_@2 = Binary read x$5_@1 + read x$5_@1
41
+ [4] Const mutate $7_@3 = Binary read $6_@2 + read x$5_@1
42
+ "]
43
+ bb0_instrs --> bb0_terminal(["Return read $7_@3"])
44
+ end
45
+
46
+ %% Jumps
47
+ %% empty
48
+```
49
+
50
+## Code
51
+
52
+```javascript
53
+function f$0() {
54
+ let x$1 = 1;
55
+ x$1 = 2;
56
+ return x$1 + x$1 + x$1;
57
+}
58
+
59
+```
60
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.js
new
+6
@@ -0,0 +1,6 @@
1
+function f() {
2
+ let x = 1;
3
+ // BUG: `x` has different values within this expression. Currently, the
4
+ // assignment is evaluated too early.
5
+ return x + (x = 2) + x;
6
+}
compiler/forget/src/__tests__/fixtures/hir/_bug_while-with-assignment.expect.md
new
+107
@@ -0,0 +1,107 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function f(reader) {
6
+ const queue = [1, 2, 3];
7
+ let value = 0;
8
+ let sum = 0;
9
+ // BUG: we need to codegen the complex test expression
10
+ while ((value = queue.pop()) != null) {
11
+ sum += value;
12
+ }
13
+ return sum;
14
+}
15
+
16
+```
17
+
18
+## HIR
19
+
20
+```
21
+bb0:
22
+ [1] Const mutate $11_@0 = 1
23
+ [2] Const mutate $12_@1 = 2
24
+ [3] Const mutate $13_@2 = 3
25
+ [4] Const mutate queue$14_@3[0:14] = Array [read $11_@0, read $12_@1, read $13_@2]
26
+ [5] Let mutate value$15_@4 = 0
27
+ [6] Let mutate sum$16_@3[0:14] = 0
28
+ [7] While test=bb1 loop=bb3 fallthrough=bb2
29
+bb1:
30
+ predecessor blocks: bb0 bb3
31
+ sum$21_@3[0:14]: phi(bb0: sum$16_@3, bb3: sum$22_@3)
32
+ [8] Reassign mutate value$18_@3[0:14] = Call mutate queue$14_@3.pop()
33
+ [9] Const mutate $19_@6 = null
34
+ [10] Const mutate $20_@7[10:14] = Binary read value$18_@3 != read $19_@6
35
+ [11] If (read $20_@7) then:bb3 else:bb2
36
+bb3:
37
+ predecessor blocks: bb1
38
+ [12] Reassign mutate sum$22_@3[0:14] = Binary read sum$21_@3 + read value$18_@3
39
+ [13] Goto(Continue) bb1
40
+bb2:
41
+ predecessor blocks: bb1
42
+ [14] Return read sum$21_@3
43
+scope7 [10:14]:
44
+ - read $19_@6
45
+```
46
+
47
+### CFG
48
+
49
+```mermaid
50
+flowchart TB
51
+ %% Basic Blocks
52
+ subgraph bb0
53
+ bb0_instrs["
54
+ [1] Const mutate $11_@0 = 1
55
+ [2] Const mutate $12_@1 = 2
56
+ [3] Const mutate $13_@2 = 3
57
+ [4] Const mutate queue$14_@3[0:14] = Array [read $11_@0, read $12_@1, read $13_@2]
58
+ [5] Let mutate value$15_@4 = 0
59
+ [6] Let mutate sum$16_@3[0:14] = 0
60
+ "]
61
+ bb0_instrs --> bb0_terminal(["While"])
62
+ end
63
+ subgraph bb1
64
+ bb1_instrs["
65
+ [8] Reassign mutate value$18_@3[0:14] = Call mutate queue$14_@3.pop()
66
+ [9] Const mutate $19_@6 = null
67
+ [10] Const mutate $20_@7[10:14] = Binary read value$18_@3 != read $19_@6
68
+ "]
69
+ bb1_instrs --> bb1_terminal(["If (read $20_@7)"])
70
+ end
71
+ subgraph bb3
72
+ bb3_instrs["
73
+ [12] Reassign mutate sum$22_@3[0:14] = Binary read sum$21_@3 + read value$18_@3
74
+ "]
75
+ bb3_instrs --> bb3_terminal(["Goto"])
76
+ end
77
+ subgraph bb2
78
+ bb2_terminal(["Return read sum$21_@3"])
79
+ end
80
+
81
+ %% Jumps
82
+ bb0_terminal -- "test" --> bb1
83
+ bb0_terminal -- "loop" --> bb3
84
+ bb0_terminal -- "fallthrough" --> bb2
85
+ bb1_terminal -- "then" --> bb3
86
+ bb1_terminal -- "else" --> bb2
87
+ bb3_terminal --> bb1
88
+
89
+```
90
+
91
+## Code
92
+
93
+```javascript
94
+function f$0(reader$1) {
95
+ const queue$2 = [1, 2, 3];
96
+ let value$6 = 0;
97
+ let sum$7 = 0;
98
+ value$6 = queue$2.pop();
99
+ bb2: while (value$6 != null) {
100
+ sum$7 = sum$7 + value$6;
101
+ }
102
+
103
+ return sum$7;
104
+}
105
+
106
+```
107
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/_bug_while-with-assignment.js
new
+10
@@ -0,0 +1,10 @@
1
+function f(reader) {
2
+ const queue = [1, 2, 3];
3
+ let value = 0;
4
+ let sum = 0;
5
+ // BUG: we need to codegen the complex test expression
6
+ while ((value = queue.pop()) != null) {
7
+ sum += value;
8
+ }
9
+ return sum;
10
+}