Skip to content

Commit 1389ff6

Browse files
committed
[Tolk] Change order of assignment evaluation, lhs first
In FunC (and in Tolk before), the assignment > lhs = rhs evaluation order (at IR level) was "rhs first, lhs second". In practice, this did not matter, because lhs could only be a primitive: > (v1, v2) = getValue() Left side of assignment actually has no "evaluation". Since Tolk implemented indexed access, there could be > getTensor().0 = getValue() or (in the future) > getObject().field = getValue() where evaluation order becomes significant. Now evaluation order will be to "lhs first, rhs second" (more expected from user's point of view), which will become significant when building control flow graph.
1 parent 2a68c86 commit 1389ff6

20 files changed

+596
-450
lines changed

tolk-tester/tests/a10.tolk

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,16 @@ fun test95() {
144144
"""
145145
test95 PROC:<{
146146
...
147-
next GETGLOB // '10
148-
3 PUSHINT // '10 '12=3
149-
4 PUSHINT // '10 '12=3 '13=4
150-
5 PUSHINT // '10 '12=3 '13=4 '14=5
151-
TRIPLE // '15 '16
152-
next SETGLOB
147+
next GETGLOB // g_next
148+
3 PUSHINT // g_next '14=3
149+
4 PUSHINT // g_next '14=3 '15=4
150+
5 PUSHINT // g_next '14=3 '15=4 '16=5
151+
TRIPLE // '10 '11
152+
SWAP
153153
cur SETGLOB
154-
cur GETGLOB // '17
155-
next GETGLOB // '17 '18
154+
next SETGLOB
155+
cur GETGLOB // g_cur
156+
next GETGLOB // g_cur g_next
156157
}>
157158
"""
158159
*/

tolk-tester/tests/allow_post_modification.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ fun main() {
147147
// x.0 x.1
148148
"""
149149

150-
@code_hash 7627024945492125068389905298530400936797031708759561372406088054030801992712
150+
@code_hash 61280273714870328160131559159866470128402169974050439159015534193532598351244
151151
*/

tolk-tester/tests/assignment-tests.tolk

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,185 @@ fun typesAsIdentifiers(builder: builder) {
2626
return int;
2727
}
2828

29+
global callOrder: tuple;
30+
31+
fun getTensor_12() {
32+
callOrder.tuplePush(100);
33+
return (1, 2);
34+
}
35+
fun getTensor_1X(x: int) {
36+
callOrder.tuplePush(101);
37+
return (1, x);
38+
}
39+
fun getTuple_12() {
40+
callOrder.tuplePush(110);
41+
return [1, 2];
42+
}
43+
fun getTuple_1X(x: int) {
44+
callOrder.tuplePush(111);
45+
return [1, x];
46+
}
47+
fun getUntypedTuple_12() {
48+
callOrder.tuplePush(120);
49+
var t = createEmptyTuple(); t.tuplePush(1); t.tuplePush(2);
50+
return t;
51+
}
52+
fun getUntypedTuple_1X(x: int) {
53+
callOrder.tuplePush(121);
54+
var t = createEmptyTuple(); t.tuplePush(1); t.tuplePush(x);
55+
return t;
56+
}
57+
fun getIntValue5() {
58+
callOrder.tuplePush(10);
59+
return 5;
60+
}
61+
fun getIntValueX(x: int) {
62+
callOrder.tuplePush(11);
63+
return x;
64+
}
65+
66+
@method_id(102)
67+
fun test102() {
68+
callOrder = createEmptyTuple();
69+
var x = 0;
70+
getTensor_12().0 = getIntValue5();
71+
getTensor_1X(5).1 = getIntValue5();
72+
getTensor_1X(x = 10).0 = getIntValueX(x);
73+
return (callOrder, x);
74+
}
75+
76+
@method_id(103)
77+
fun test103() {
78+
callOrder = createEmptyTuple();
79+
var x = 0;
80+
getTuple_12().0 = getIntValue5();
81+
getTuple_1X(5).1 = getIntValue5();
82+
getTuple_1X(x = 10).0 = getIntValueX(x);
83+
return (callOrder, x);
84+
}
85+
86+
@method_id(104)
87+
fun test104() {
88+
callOrder = createEmptyTuple();
89+
var x = 0;
90+
getUntypedTuple_12().0 = getIntValue5();
91+
getUntypedTuple_1X(5).1 = getIntValue5();
92+
getUntypedTuple_1X(x = 10).0 = getIntValueX(x);
93+
return (callOrder, x);
94+
}
95+
96+
@method_id(105)
97+
fun test105() {
98+
callOrder = createEmptyTuple();
99+
getTensor_12().0 = getTensor_1X(getIntValue5()).1 = getIntValueX(getTensor_12().1);
100+
return callOrder;
101+
}
102+
103+
@method_id(106)
104+
fun test106() {
105+
callOrder = createEmptyTuple();
106+
getTuple_12().0 = getTuple_1X(getIntValue5()).1 = getIntValueX(getTuple_12().1);
107+
return callOrder;
108+
}
109+
110+
global t107: (int, int);
111+
112+
@method_id(107)
113+
fun test107() {
114+
((t107 = (1, 2)).0, (t107 = (3, 4)).1) = (5, 6);
115+
return t107;
116+
}
117+
118+
global g108: int;
119+
fun assertEq(a: int, b: int) {
120+
assert(a == b, 10);
121+
return b;
122+
}
123+
124+
@method_id(108)
125+
fun test108() {
126+
callOrder = createEmptyTuple();
127+
g108 = 0;
128+
getTensor_1X(g108 = 8).1 = assertEq(g108, 8);
129+
return (callOrder, g108);
130+
}
131+
132+
@method_id(109)
133+
fun test109() {
134+
callOrder = createEmptyTuple();
135+
var x = 0;
136+
[getTuple_12().0, getTuple_1X(x = getIntValue5()).1, getTuple_1X(x += 10).0] = [getIntValue5(), getIntValue5(), getIntValueX(x)];
137+
return (callOrder, x);
138+
}
139+
140+
global g110: int;
141+
global t110: (int, int);
142+
143+
@method_id(110)
144+
fun test110() {
145+
callOrder = createEmptyTuple();
146+
var xy = [0, 0];
147+
[xy.0, getTuple_1X(g110 = 8).0] = [g110 += 5, getIntValueX(g110 += 10)];
148+
[xy.1, getTuple_1X((t110 = (8, 9)).0).1] = [t110.0 += 5, getIntValueX(t110.1 += 10)];
149+
return (xy, callOrder, g110, t110);
150+
}
151+
152+
@method_id(111)
153+
fun test111() {
154+
callOrder = createEmptyTuple();
155+
var z = -1;
156+
var xy = [0, z = 0];
157+
var rhs = [getIntValueX(xy.1 += 10), xy.1, xy.0, z += 50];
158+
[xy.0, getTuple_1X(g110 = 8 + getIntValueX(xy.1)).0, xy.1, z] = rhs;
159+
return (xy, g110, callOrder, z);
160+
}
161+
162+
@method_id(112)
163+
fun test112() {
164+
var xy = [1, 2];
165+
((((xy))).0, ((xy.1))) = ((xy).1, ((xy.0)));
166+
return xy;
167+
}
168+
169+
@method_id(113)
170+
fun test113() {
171+
var (a, t, z) = (1, [2,3], (-1,-1));
172+
(a, t, a, z, t.1, z.1) = (10, [a,12], 13, (a, t.1), 14, t.1);
173+
return (a, t, z);
174+
}
175+
176+
global g114: int;
177+
global t114: [int, int];
178+
global z114: (int, int);
179+
180+
@method_id(114)
181+
fun test114() {
182+
g114 = 1;
183+
t114 = [2, 3];
184+
(g114, t114, g114, z114, t114.1, z114.1) = (10, [g114,12], 13, (g114, t114.1), 14, t114.1);
185+
return (g114, t114, z114);
186+
}
187+
188+
@method_id(115)
189+
fun test115() {
190+
callOrder = createEmptyTuple();
191+
var x = 0;
192+
var y = 0;
193+
[getTensor_1X(x = 5).0, y] = getTuple_1X(x = 9);
194+
return (callOrder, x, y);
195+
}
196+
197+
@method_id(116)
198+
fun test116() {
199+
var (a,b,c,d) = (0,0,0,0);
200+
var rhs = [1, 2, 3, 4];
201+
var rhs2 = ([a,b,c,d] = rhs);
202+
__expect_type(rhs2, "[int, int, int, int]");
203+
return (a, b, c, d, rhs2);
204+
}
205+
206+
207+
29208
fun main(value: int) {
30209
var (x: int, y) = (autoInferIntNull(value), autoInferIntNull(value * 2));
31210
if (x == null && y == null) { return null; }
@@ -37,4 +216,35 @@ fun main(value: int) {
37216
@testcase | 0 | 6 | -1
38217
@testcase | 0 | 11 | (null)
39218
@testcase | 101 | 78 | 88
219+
@testcase | 102 | | [ 100 10 101 10 101 11 ] 10
220+
@testcase | 103 | | [ 110 10 111 10 111 11 ] 10
221+
@testcase | 104 | | [ 120 10 121 10 121 11 ] 10
222+
@testcase | 105 | | [ 100 10 101 100 11 ]
223+
@testcase | 106 | | [ 110 10 111 110 11 ]
224+
@testcase | 107 | | 3 4
225+
@testcase | 108 | | [ 101 ] 8
226+
@testcase | 109 | | [ 110 10 111 111 10 10 11 ] 15
227+
@testcase | 110 | | [ 13 13 ] [ 111 11 111 11 ] 23 13 19
228+
@testcase | 111 | | [ 10 0 ] 18 [ 11 11 111 ] 50
229+
@testcase | 112 | | [ 2 1 ]
230+
@testcase | 113 | | 13 [ 1 14 ] 1 3
231+
@testcase | 114 | | 13 [ 1 14 ] 1 3
232+
@testcase | 115 | | [ 101 111 ] 9 9
233+
@testcase | 116 | | 1 2 3 4 [ 1 2 3 4 ]
234+
235+
236+
@fif_codegen
237+
"""
238+
test116 PROC:<{
239+
//
240+
1 PUSHINT // '10=1
241+
2 PUSHINT // '10=1 '11=2
242+
3 PUSHINT // '10=1 '11=2 '12=3
243+
4 PUSHINT // '10=1 '11=2 '12=3 '13=4
244+
4 TUPLE // rhs
245+
DUP // rhs rhs
246+
4 UNTUPLE // rhs2 a b c d
247+
4 ROLL // a b c d rhs2
248+
}>
249+
"""
40250
*/

tolk-tester/tests/codegen_check_demo.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Below, I just give examples of @fif_codegen tag:
3535
"""
3636
main PROC:<{
3737
// s
38-
17 PUSHINT // s '1=17
38+
17 PUSHINT // s '3=17
3939
OVER // s z=17 t
4040
WHILE:<{
4141
...

tolk-tester/tests/indexed-access.tolk

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ fun plus(mutate self: int, y: int): int {
2121

2222
fun eq<X>(v: X): X { return v; }
2323

24+
global gTup: [int];
25+
global gTens: (int, int);
26+
27+
@method_id(100)
28+
fun testCodegenSimple() {
29+
var t1 = [1];
30+
t1.0 = 2;
31+
debugPrintString("");
32+
var t2 = [[1]];
33+
t2.0.0 = 2;
34+
debugPrintString("");
35+
gTup = [1];
36+
gTup.0 = 2;
37+
debugPrintString("");
38+
gTens = (1,2);
39+
gTens.1 = 4;
40+
debugPrintString("");
41+
return (t1, t2, gTup, gTens);
42+
}
43+
2444
@method_id(101)
2545
fun test101() {
2646
var t = (1, (2, 3), [4, 5, [6, 7]], 8);
@@ -241,30 +261,60 @@ fun main(){}
241261

242262
@fif_codegen
243263
"""
244-
testCodegenNoPureIndexedAccess PROC:<{
264+
testCodegenSimple PROC:<{
245265
//
246-
0 PUSHINT // '8=0
266+
1 PUSHINT // '2=1
267+
SINGLE // t1
268+
2 PUSHINT // t1 '3=2
269+
0 SETINDEX // t1
270+
x{} PUSHSLICE // t1 '6
271+
STRDUMP DROP
272+
1 PUSHINT // t1 '10=1
273+
SINGLE // t1 '9
274+
SINGLE // t1 t2
275+
2 PUSHINT // t1 t2 '11=2
276+
OVER // t1 t2 '11=2 t2
277+
0 INDEX // t1 t2 '11=2 '14
278+
SWAP // t1 t2 '14 '11=2
279+
0 SETINDEX // t1 t2 '14
280+
0 SETINDEX // t1 t2
281+
x{} PUSHSLICE // t1 t2 '17
282+
STRDUMP DROP
283+
1 PUSHINT // t1 t2 '20=1
284+
SINGLE // t1 t2 '18
285+
gTup SETGLOB
286+
2 PUSHINT // t1 t2 '21=2
287+
gTup GETGLOB // t1 t2 '21=2 g_gTup
288+
SWAP // t1 t2 g_gTup '21=2
289+
0 SETINDEX // t1 t2 g_gTup
290+
gTup SETGLOB
291+
x{} PUSHSLICE // t1 t2 '25
292+
STRDUMP DROP
293+
1 PUSHINT // t1 t2 '28=1
294+
2 PUSHINT // t1 t2 '26=1 '27=2
295+
PAIR
296+
gTens SETGLOB
297+
4 PUSHINT // t1 t2 g_gTens.1=4
298+
gTens GETGLOB
299+
UNPAIR // t1 t2 g_gTens.1=4 g_gTens.0 g_gTens.1
300+
DROP // t1 t2 g_gTens.1=4 g_gTens.0
301+
SWAP // t1 t2 g_gTens.0 g_gTens.1=4
302+
PAIR
303+
gTens SETGLOB
304+
x{} PUSHSLICE // t1 t2 '36
305+
STRDUMP DROP
306+
gTup GETGLOB // t1 t2 g_gTup
307+
gTens GETGLOB
308+
UNPAIR // t1 t2 g_gTup g_gTens.0 g_gTens.1
247309
}>
248310
"""
249311

250312
@fif_codegen
251313
"""
252-
test104 PROC:<{
314+
testCodegenNoPureIndexedAccess PROC:<{
253315
//
254-
5 PUSHINT // '2=5
255-
DUP // '2=5 '3=5
256-
PAIR // '1
257-
SINGLE // m
258-
10 PUSHINT // m '5=10
259-
20 PUSHINT // m '5=10 '6=20
260-
s2 PUSH // m '5=10 '6=20 m
261-
0 INDEX // m '10=10 '12=20 '8
262-
SWAP // m '10=10 '8 '12=20
263-
1 SETINDEX // m '10=10 '8
264-
SWAP // m '8 '10=10
265-
0 SETINDEX // m '8
266-
0 SETINDEX // m
267-
...
316+
0 PUSHINT // '8=0
317+
}>
268318
"""
269319

270320
@fif_codegen
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
fun main() {
2-
var c = 1;
3-
(c, c) = (2, 3);
2+
var t = createEmptyTuple();
3+
t.0 = (1, 2);
44
}
55

66
/**
77
@compilation_should_fail
8-
@stderr one variable modified twice inside the same expression
8+
@stderr a tuple can not have `(int, int)` inside, because it occupies 2 stack slots in TVM, not 1
99
*/
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
fun incThree(mutate a: int, mutate b: int, mutate c: int) {}
2-
3-
fun main() {
4-
var c = [[[1, 2]]];
5-
incThree(mutate c.0.0.0, mutate c.0.0.1, mutate c.0.0.0);
1+
fun main(cs: slice) {
2+
var cb = cs.tupleSize;
63
}
74

85
/**
96
@compilation_should_fail
10-
@stderr one variable modified twice inside the same expression
7+
@stderr referencing a method for `tuple` with object of type `slice`
118
*/

0 commit comments

Comments
 (0)