From 7a1602f591cc7db4ea9ce12a84dc3c4bf90dd32e Mon Sep 17 00:00:00 2001 From: tolk-vm Date: Mon, 27 Jan 2025 10:29:17 +0300 Subject: [PATCH] [Tolk] Support syntax `tensorVar.0` and `tupleVar.0` It works both for reading and writing: > var t = (1, 2); > t.0; // 1 > t.0 = 5; > t; // (5, 2) It also works for typed/untyped tuples, producing INDEX and SETINDEX. Global tensors and tuples works. Nesting `t.0.1.2` works. `mutate` works. Even mixing tuples inside tensors inside a global for writing works. --- crypto/smartcont/tolk-stdlib/common.tolk | 17 +- tolk-tester/tests/a10.tolk | 32 +- tolk-tester/tests/a6_1.tolk | 4 +- .../tests/allow_post_modification.tolk | 13 +- tolk-tester/tests/bit-operators.tolk | 54 +-- tolk-tester/tests/cells-slices.tolk | 20 +- tolk-tester/tests/codegen_check_demo.tolk | 6 +- tolk-tester/tests/generics-1.tolk | 19 +- tolk-tester/tests/if_stmt.tolk | 6 +- tolk-tester/tests/indexed-access.tolk | 287 ++++++++++++ tolk-tester/tests/invalid-assign-1.tolk | 9 + tolk-tester/tests/invalid-assign-2.tolk | 11 + tolk-tester/tests/invalid-assign-3.tolk | 10 + tolk-tester/tests/invalid-assign-4.tolk | 10 + tolk-tester/tests/invalid-assign-5.tolk | 9 + tolk-tester/tests/invalid-assign-6.tolk | 9 + tolk-tester/tests/invalid-assign-7.tolk | 8 + tolk-tester/tests/invalid-call-1.tolk | 2 +- tolk-tester/tests/invalid-call-10.tolk | 11 + tolk-tester/tests/invalid-call-11.tolk | 11 + tolk-tester/tests/invalid-call-7.tolk | 2 +- tolk-tester/tests/invalid-call-8.tolk | 4 +- tolk-tester/tests/invalid-generics-12.tolk | 15 + tolk-tester/tests/invalid-typing-13.tolk | 9 + tolk-tester/tests/known-bugs.tolk | 27 -- tolk-tester/tests/logical-operators.tolk | 40 +- tolk-tester/tests/mutate-methods.tolk | 12 +- tolk-tester/tests/no-spaces.tolk | 8 +- tolk-tester/tests/null-keyword.tolk | 38 +- tolk-tester/tests/op-priority.tolk | 24 +- tolk-tester/tests/use-before-declare.tolk | 2 +- tolk-tester/tests/var-apply.tolk | 10 + tolk/abscode.cpp | 63 +-- tolk/asmops.cpp | 19 +- tolk/ast-from-tokens.cpp | 5 +- tolk/ast.h | 10 +- tolk/builtins.cpp | 14 + tolk/codegen.cpp | 2 +- tolk/pipe-ast-to-legacy.cpp | 437 ++++++++++++++---- tolk/pipe-check-rvalue-lvalue.cpp | 4 +- tolk/pipe-infer-types-and-calls.cpp | 110 ++++- tolk/tolk.h | 54 +-- 42 files changed, 1119 insertions(+), 338 deletions(-) create mode 100644 tolk-tester/tests/indexed-access.tolk create mode 100644 tolk-tester/tests/invalid-assign-1.tolk create mode 100644 tolk-tester/tests/invalid-assign-2.tolk create mode 100644 tolk-tester/tests/invalid-assign-3.tolk create mode 100644 tolk-tester/tests/invalid-assign-4.tolk create mode 100644 tolk-tester/tests/invalid-assign-5.tolk create mode 100644 tolk-tester/tests/invalid-assign-6.tolk create mode 100644 tolk-tester/tests/invalid-assign-7.tolk create mode 100644 tolk-tester/tests/invalid-call-10.tolk create mode 100644 tolk-tester/tests/invalid-call-11.tolk create mode 100644 tolk-tester/tests/invalid-generics-12.tolk create mode 100644 tolk-tester/tests/invalid-typing-13.tolk delete mode 100644 tolk-tester/tests/known-bugs.tolk diff --git a/crypto/smartcont/tolk-stdlib/common.tolk b/crypto/smartcont/tolk-stdlib/common.tolk index 46068a20..4c0c4007 100644 --- a/crypto/smartcont/tolk-stdlib/common.tolk +++ b/crypto/smartcont/tolk-stdlib/common.tolk @@ -21,23 +21,32 @@ fun tuplePush(mutate self: tuple, value: T): void asm "TPUSH"; /// Returns the first element of a non-empty tuple. +/// `t.0` is actually the same as `t.tupleFirst()` @pure -fun tupleFirst(t: tuple): T +fun tupleFirst(self: tuple): T asm "FIRST"; /// Returns the [`index`]-th element of a tuple. +/// `t.i` is actually the same as `t.tupleAt(i)` @pure -fun tupleAt(t: tuple, index: int): T +fun tupleAt(self: tuple, index: int): T + builtin; + +/// Sets the [`index`]-th element of a tuple to a specified value +/// (element with this index must already exist, a new element isn't created). +/// `t.i = value` is actually the same as `t.tupleSetAt(value, i)` +@pure +fun tupleSetAt(mutate self: tuple, value: T, index: int): void builtin; /// Returns the size of a tuple (elements count in it). @pure -fun tupleSize(t: tuple): int +fun tupleSize(self: tuple): int asm "TLEN"; /// Returns the last element of a non-empty tuple. @pure -fun tupleLast(t: tuple): T +fun tupleLast(self: tuple): T asm "LAST"; diff --git a/tolk-tester/tests/a10.tolk b/tolk-tester/tests/a10.tolk index af104248..755a3bfb 100644 --- a/tolk-tester/tests/a10.tolk +++ b/tolk-tester/tests/a10.tolk @@ -78,6 +78,17 @@ fun testStartBalanceCodegen2() { return first; } +global cur: [int, int, int]; +global next: [int, int, int]; + +@method_id(95) +fun test95() { + cur = [1, 2, 3]; + next = [2, 3, 4]; + (cur, next) = (next, [3, 4, 5]); + return (cur, next); +} + /** method_id | in | out @testcase | 0 | 101 15 | 100 1 @@ -90,6 +101,7 @@ fun testStartBalanceCodegen2() { @testcase | 89 | 4 | 1 4 1 4 @testcase | 91 | | 10 @testcase | 92 | | 10 32 +@testcase | 95 | | [ 2 3 4 ] [ 3 4 5 ] @fif_codegen """ @@ -104,9 +116,9 @@ fun testStartBalanceCodegen2() { testDumpDontPolluteStack PROC:<{ ... DUMPSTK - x{6d79} PUSHSLICE // f s _5 + x{6d79} PUSHSLICE // f s '5 STRDUMP DROP - SBITS // f _6 + SBITS // f '6 }> """ @@ -127,4 +139,20 @@ fun testStartBalanceCodegen2() { FIRST // first }> """ + +@fif_codegen +""" + test95 PROC:<{ + ... + next GETGLOB // '10 + 3 PUSHINT // '10 '12=3 + 4 PUSHINT // '10 '12=3 '13=4 + 5 PUSHINT // '10 '12=3 '13=4 '14=5 + TRIPLE // '15 '16 + next SETGLOB + cur SETGLOB + cur GETGLOB // '17 + next GETGLOB // '17 '18 + }> +""" */ diff --git a/tolk-tester/tests/a6_1.tolk b/tolk-tester/tests/a6_1.tolk index 4995c42d..8079972b 100644 --- a/tolk-tester/tests/a6_1.tolk +++ b/tolk-tester/tests/a6_1.tolk @@ -7,7 +7,7 @@ fun main(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) { @method_id(101) fun testDivMod(x: int, y: int) { - return [divMod(x, y), modDiv(x, y), mulDivMod(x, y, 10)]; + return (divMod(x, y), modDiv(x, y), mulDivMod(x, y, 10)); } /** @@ -18,5 +18,5 @@ fun testDivMod(x: int, y: int) { @testcase | 0 | 448 -433 -444 792 150012 -356232 | -218 -572 @testcase | 0 | -40 -821 433 -734 -721629 -741724 | -206 889 @testcase | 0 | -261 -98 -494 868 -166153 733738 | 263 995 -@testcase | 101 | 112 3 | [ 37 1 1 37 33 6 ] +@testcase | 101 | 112 3 | 37 1 1 37 33 6 */ diff --git a/tolk-tester/tests/allow_post_modification.tolk b/tolk-tester/tests/allow_post_modification.tolk index e374f62b..df758a1e 100644 --- a/tolk-tester/tests/allow_post_modification.tolk +++ b/tolk-tester/tests/allow_post_modification.tolk @@ -89,12 +89,14 @@ fun test_if_else(x: int): (int, int, int, int, int) { @method_id(21) fun test_assign_with_inner(x: int) { - return (x, x += 10, [(x, x += 20, eq(x -= 50), x)], eq2((x, x *= eq(x /= 2)))); + var result = (x, x += 10, [x, x += 20, eq(x -= 50), x], eq2((x, x *= eq(x /= 2)))); + return result; } @method_id(22) fun test_assign_with_mutate(x: int) { - return (x, mul2(mutate x, x += 5), x.`~inc`(mul2(mutate x, x)), x); + var (result, _) = ((x, mul2(mutate x, x += 5), x.`~inc`(mul2(mutate x, x)), x), 0); + return result; } @method_id(23) @@ -138,5 +140,12 @@ fun main() { inc CALLDICT // self newY }> """ + +@fif_codegen +""" + test_assign_tensor_global PROC:<{ + // x.0 x.1 +""" + @code_hash 7627024945492125068389905298530400936797031708759561372406088054030801992712 */ diff --git a/tolk-tester/tests/bit-operators.tolk b/tolk-tester/tests/bit-operators.tolk index 4cb8e1ba..b0106883 100644 --- a/tolk-tester/tests/bit-operators.tolk +++ b/tolk-tester/tests/bit-operators.tolk @@ -127,10 +127,10 @@ fun testBoolCompareOptimized(x: bool) { """ boolWithBitwiseConst PROC:<{ // - 0 PUSHINT // _3 - -1 PUSHINT // _3 _5 - 0 PUSHINT // _3 _5 _7 - -1 PUSHINT // _3 _5 _7 _8 + 0 PUSHINT // '3 + -1 PUSHINT // '3 '5 + 0 PUSHINT // '3 '5 '7 + -1 PUSHINT // '3 '5 '7 '8 }> """ @@ -142,22 +142,22 @@ fun testBoolCompareOptimized(x: bool) { UNTIL:<{ INC // i n cnt s2 PUSH // i n cnt i - NOT // i n cnt _6 + NOT // i n cnt '6 }> // i n cnt UNTIL:<{ INC // i n cnt s2 PUSH // i n cnt i - NOT // i n cnt _9 + NOT // i n cnt '9 }> // i n cnt UNTIL:<{ INC // i n cnt OVER // i n cnt n - 0 EQINT // i n cnt _12 + 0 EQINT // i n cnt '12 }> // i n cnt s0 s2 XCHG // cnt n i - NOT // cnt n _13 - SWAP // cnt _13 n - 0 EQINT // cnt _13 _14 + NOT // cnt n '13 + SWAP // cnt '13 n + 0 EQINT // cnt '13 '14 }> """ @@ -165,12 +165,12 @@ fun testBoolCompareOptimized(x: bool) { """ testConstNegateCodegen PROC:<{ // - TRUE // _0 - FALSE // _0 _1 - FALSE // _0 _1 _2 - TRUE // _0 _1 _2 _3 - TRUE // _0 _1 _2 _3 _4 - FALSE // _0 _1 _2 _3 _4 _5 + TRUE // '0 + FALSE // '0 '1 + FALSE // '0 '1 '2 + TRUE // '0 '1 '2 '3 + TRUE // '0 '1 '2 '3 '4 + FALSE // '0 '1 '2 '3 '4 '5 }> """ @@ -179,11 +179,11 @@ fun testBoolCompareOptimized(x: bool) { testBoolNegateOptimized PROC:<{ // x DUP // x x - NOT // x _1 - OVER // x _1 x - NOT // x _1 _2 + NOT // x '1 + OVER // x '1 x + NOT // x '1 '2 s2 s(-1) PUXC - TRUE // x _1 x _2 _3 + TRUE // x '1 x '2 '3 }> """ @@ -192,13 +192,13 @@ fun testBoolCompareOptimized(x: bool) { testBoolCompareOptimized PROC:<{ // x DUP // x x - NOT // x _1 - OVER // x _1 x - eqX CALLDICT // x _1 _2 - NOT // x _1 _3 - s2 PUSH // x _1 _3 x - eqX CALLDICT // x _1 _3 _4 - s3 PUSH // x _1 _3 _4 x + NOT // x '1 + OVER // x '1 x + eqX CALLDICT // x '1 '2 + NOT // x '1 '3 + s2 PUSH // x '1 '3 x + eqX CALLDICT // x '1 '3 '4 + s3 PUSH // x '1 '3 '4 x }> """ */ diff --git a/tolk-tester/tests/cells-slices.tolk b/tolk-tester/tests/cells-slices.tolk index 0a2807e6..19e2e215 100644 --- a/tolk-tester/tests/cells-slices.tolk +++ b/tolk-tester/tests/cells-slices.tolk @@ -216,16 +216,16 @@ Note, that since 'compute-asm-ltr' became on be default, chaining methods codege """ test6 PROC:<{ // - NEWC // _0 - 1 PUSHINT // _0 _1=1 - SWAP // _1=1 _0 - 32 STU // _0 - 2 PUSHINT // _0 _4=2 - SWAP // _4=2 _0 - 32 STU // _0 - 3 PUSHINT // _0 _7=3 - SWAP // _7=3 _0 - 32 STU // _0 + NEWC // '0 + 1 PUSHINT // '0 '1=1 + SWAP // '1=1 '0 + 32 STU // '0 + 2 PUSHINT // '0 '4=2 + SWAP // '4=2 '0 + 32 STU // '0 + 3 PUSHINT // '0 '7=3 + SWAP // '7=3 '0 + 32 STU // '0 }> """ */ diff --git a/tolk-tester/tests/codegen_check_demo.tolk b/tolk-tester/tests/codegen_check_demo.tolk index e40f0377..b355a9b7 100644 --- a/tolk-tester/tests/codegen_check_demo.tolk +++ b/tolk-tester/tests/codegen_check_demo.tolk @@ -35,7 +35,7 @@ Below, I just give examples of @fif_codegen tag: """ main PROC:<{ // s - 17 PUSHINT // s _1=17 + 17 PUSHINT // s '1=17 OVER // s z=17 t WHILE:<{ ... @@ -63,7 +63,7 @@ main PROC:<{ @fif_codegen """ OVER - 0 GTINT // s z t _5 + 0 GTINT // s z t '5 """ @fif_codegen @@ -83,7 +83,7 @@ FALSE }> """ -@fif_codegen NOT // _8 +@fif_codegen NOT // '8 @fif_codegen main PROC:<{ @fif_codegen_avoid PROCINLINE diff --git a/tolk-tester/tests/generics-1.tolk b/tolk-tester/tests/generics-1.tolk index 5a649a44..453ec282 100644 --- a/tolk-tester/tests/generics-1.tolk +++ b/tolk-tester/tests/generics-1.tolk @@ -14,13 +14,19 @@ fun getTwo(): X { return 2 as X; } fun takeInt(a: int) { return a; } @method_id(102) -fun test102(): (int, int, int, [(int, int)]) { +fun test102(): (int, int, int, [int, int]) { var a: int = getTwo(); var _: int = getTwo(); var b = getTwo() as int; var c: int = 1 ? getTwo() : getTwo(); var c redef = getTwo(); - return (eq1(a), eq2(b), takeInt(getTwo()), [(getTwo(), getTwo())]); + var ab_tens = (0, (1, 2)); + ab_tens.0 = getTwo(); + ab_tens.1.1 = getTwo(); + var ab_tup = [0, [1, 2]]; + ab_tup.0 = getTwo(); + ab_tup.1.1 = getTwo(); + return (eq1(a), eq2(b), takeInt(getTwo()), [getTwo(), ab_tens.1.1]); } @method_id(103) @@ -43,9 +49,9 @@ fun manyEq(a: T1, b: T2, c: T3): [T1, T2, T3] { fun test104(f: int) { var result = ( manyEq(1 ? 1 : 1, f ? 0 : null, !f ? getTwo() as int : null), - manyEq((f ? null as int : eq2(2), beginCell().storeBool(true).endCell().beginParse().loadBool()), 0, eq4(f)) + manyEq(f ? null as int : eq2(2), beginCell().storeBool(true).endCell().beginParse().loadBool(), eq4(f)) ); - __expect_type(result, "([int, int, int], [(int, bool), int, int])"); + __expect_type(result, "([int, int, int], [int, bool, int])"); return result; } @@ -74,7 +80,8 @@ fun test106() { return [ abstractTransform(cellToSlice, calcLoad32, c), abstractTransform(calcYPlus1, calcYPlus1, 0), - abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2)) + abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2)).0, + abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2)).1 ]; } @@ -135,7 +142,7 @@ fun main(x: int): (int, [[int, int]]) { @testcase | 101 | 0 | 0 0 0 [ 0 0 ] 0 0 0 [ 0 0 ] 0 0 0 [] @testcase | 102 | | 2 2 2 [ 2 2 ] @testcase | 103 | 0 | 0 100 100 -@testcase | 104 | 0 | [ 1 (null) 2 ] [ 2 -1 0 0 ] +@testcase | 104 | 0 | [ 1 (null) 2 ] [ 2 -1 0 ] @testcase | 105 | | 3 @testcase | 106 | | [ 106 2 6 6 ] @testcase | 107 | | 6 6 1 1 6 6 diff --git a/tolk-tester/tests/if_stmt.tolk b/tolk-tester/tests/if_stmt.tolk index 2c51ac51..0f78a516 100644 --- a/tolk-tester/tests/if_stmt.tolk +++ b/tolk-tester/tests/if_stmt.tolk @@ -54,13 +54,13 @@ fun main() { test3 PROC:<{ // x DUP // x x - 20 NEQINT // x _2 + 20 NEQINT // x '2 IFNOTJMP:<{ // x DROP // - 20 PUSHINT // _3=20 + 20 PUSHINT // '3=20 }> // x DUP // x x - 50 EQINT // x _5 + 50 EQINT // x '5 IFNOTJMP:<{ // x """ */ diff --git a/tolk-tester/tests/indexed-access.tolk b/tolk-tester/tests/indexed-access.tolk new file mode 100644 index 00000000..38094fa5 --- /dev/null +++ b/tolk-tester/tests/indexed-access.tolk @@ -0,0 +1,287 @@ + +fun increment(mutate self: int) { + self += 1; +} + +fun increment2(mutate a: int, mutate b: int) { + a += 1; + b += 1; +} + +fun assign1020(mutate a: int, mutate b: int) { + a = 10; + b = 20; +} + +fun plus(mutate self: int, y: int): int { + val newVals = (self + y, y * 10); + self = newVals.0; + return newVals.1; +} + +fun eq(v: X): X { return v; } + +@method_id(101) +fun test101() { + var t = (1, (2, 3), [4, 5, [6, 7]], 8); + (t.0, t.1.0, t.2.0) = (2, 3, 5); + t.3.increment(); + t.2.1 += (t.1.1 += 1) - t.1.1 + 1; + increment2(mutate t.2.2.0, mutate t.2.2.1); + return t; +} + +global t102: (int, (int, int), [int, int, [int, int]], int); + +@method_id(102) +fun test102() { + t102 = (1, (2, 3), [4, 5, [6, 7]], 8); + (t102.0, t102.1.0, t102.2.0) = (2, 3, 5); + t102.3.increment(); + t102.2.1 += (t102.1.1 += 1) - t102.1.1 + 1; + increment2(mutate t102.2.2.0, mutate t102.2.2.1); + return t102; +} + +global t103: (int, int); + +@method_id(103) +fun test103() { + t103 = (5, 5); + assign1020(mutate t103.0, mutate t103.1); + var t = (5, 5); + assign1020(mutate t.0, mutate t.1); + return (t103, t); +} + +global t104: [[int, int]]; + +@method_id(104) +fun test104() { + var m = [[5, 5]]; + (m.0.0, m.0.1) = (10, 20); + t104 = [[5, 5]]; + (t104.0.0, t104.0.1) = (10, 20); + return (t104, m); +} + +@method_id(105) +fun test105(x: int, y: int): (tuple, int, (int, int), int, int) { + var ab = (createEmptyTuple(), (x, y), tupleSize); + ab.0.tuplePush(1); + tuplePush(mutate ab.0, 2); + ab.1.0 = null; + ab.1.1 += 10; + var cb = ab.2; + return (ab.0, ab.0.1, ab.1, cb(ab.0), ab.2(ab.0)); +} + +@method_id(106) +fun test106(x: int, y: int) { + var ab = [createEmptyTuple(), [x, y], tupleSize]; + ab.0.tuplePush(1); + tuplePush(mutate ab.0, 2); + ab.1.0 = null; + ab.1.1 += 10; + var cb = ab.2; + return (ab.0, ab.1, cb(ab.0), ab.2(ab.0)); +} + +@method_id(107) +fun test107() { + var ab = createEmptyTuple(); + ab.tuplePush(1); + ab.tuplePush(beginCell().storeInt(1, 32)); + return (ab.0 as int, getBuilderBitsCount(ab.1)); +} + +global t108: [int, [int, [int]]]; + +@method_id(108) +fun test108(last: int) { + t108 = [1, [2, [last]]]; + t108.1.1.0.increment(); + var t = [1, [2, [last]]]; + t.1.1.0.increment(); + return (t108, t); +} + +@method_id(109) +fun test109(x: (int, int)): (int, int, int, int, int, int, int) { + return (x.1, x.1.plus(x.1 / 20), x.1, x.1 = x.1 * 2, x.1, x.1 += 1, x.1); +} + +@method_id(110) +fun test110(f: int, s: int) { + var x = [f, s]; + return (x, x.1, x.1.plus(x.1 / 20), x.1, x.1 = x.1 * 2, x.1, x.1 += 1, x.1, x); +} + +global xx: (int, int); + +@method_id(111) +fun test111(x: (int, int)) { + xx = x; + return (x, xx.1, xx.1.plus(xx.1 / 20), eq(xx.1 += (x.1 *= 0)), xx.1 = xx.1 * 2, xx.1, xx.1 += 1, xx.1, x); +} + +global yy: [int, int]; + +@method_id(112) +fun test112(f: int, s: int) { + yy = [f, s]; + return (yy, yy.1, yy.1.plus(yy.1 / 20), eq(yy.1 += (yy.1 *= 0)), yy.1 = yy.1 * 2, yy.1, yy.1 += 1, yy.1, yy); +} + +@pure +fun getConstTuple() { + return [1,2]; +} + +fun testCodegenNoPureIndexedAccess() { + (getConstTuple().1, getConstTuple().0) = (3, 4); + return 0; +} + +@method_id(113) +fun test113() { + var x = [[1, 2]]; + return (x, x.0, plus(mutate x.0.0, 10), x.0, x, x.0 = [10, 20], x); +} + +@method_id(114) +fun test114(f: int, s: int) { + var x = ((), (f, s), ()); + return (x, x.1, plus(mutate x.1.0, 10), x.1, x, x.1 = (10, 20), x); +} + +@method_id(115) +fun test115() { + var y = [[[[true]]]]; + return (y, y.0.0.0.0 = !y.0.0.0.0, y.0); +} + +@method_id(116) +fun test116() { + var t = createEmptyTuple(); + t.tuplePush(1); + try { + return t.100500 as int; + } catch(excNo) { + return excNo; + } +} + +@method_id(117) +fun test117() { + var t = createEmptyTuple(); + t.tuplePush(1); + try { + return (t.0 as tuple).0 as int; + } catch(excNo) { + return excNo; + } +} + +@method_id(118) +fun testCodegenIndexPostfix1(x: (int, int)) { + var ab = (x.1, x.0); + return ab; +} + +@method_id(119) +fun testCodegenIndexPostfix2(x: (int, (int, int), int)) { + var y = x; + return (y.2, y.0, y.1.1); +} + +fun getT() { return (1, 2); } + +@method_id(120) +fun test120() { + return (getT().0 = 3, getT().0 = 4, [getT().0 = 5, getT().0 = 6]); +} + +@method_id(121) +fun test121(zero: int) { + var t = createEmptyTuple(); + t.tuplePush(-100); + t.tupleSetAt(0, zero); + (t.0 as int).increment(); + (((t.0) as int) as int).increment(); + increment(mutate t.0 as int); + return t; +} + +fun main(){} + + +/** +@testcase | 101 | | 2 3 4 [ 5 6 [ 7 8 ] ] 9 +@testcase | 102 | | 2 3 4 [ 5 6 [ 7 8 ] ] 9 +@testcase | 103 | | 10 20 10 20 +@testcase | 104 | | [ [ 10 20 ] ] [ [ 10 20 ] ] +@testcase | 105 | 5 6 | [ 1 2 ] 2 (null) 16 2 2 +@testcase | 106 | 5 6 | [ 1 2 ] [ (null) 16 ] 2 2 +@testcase | 107 | | 1 32 +@testcase | 108 | 3 | [ 1 [ 2 [ 4 ] ] ] [ 1 [ 2 [ 4 ] ] ] +@testcase | 109 | 0 100 | 100 50 105 210 210 211 211 +@testcase | 110 | 0 100 | [ 0 100 ] 100 50 105 210 210 211 211 [ 0 211 ] +@testcase | 111 | 0 100 | 0 100 100 50 105 210 210 211 211 0 0 +@testcase | 112 | 0 100 | [ 0 100 ] 100 50 105 210 210 211 211 [ 0 211 ] +@testcase | 113 | | [ [ 1 2 ] ] [ 1 2 ] 100 [ 11 2 ] [ [ 11 2 ] ] [ 10 20 ] [ [ 10 20 ] ] +@testcase | 114 | 1 2 | 1 2 1 2 100 11 2 11 2 10 20 10 20 +@testcase | 115 | | [ [ [ [ -1 ] ] ] ] 0 [ [ [ 0 ] ] ] +@testcase | 116 | | 5 +@testcase | 117 | | 7 +@testcase | 118 | 1 2 | 2 1 +@testcase | 119 | 1 2 3 4 | 4 1 3 +@testcase | 120 | | 3 4 [ 5 6 ] +@testcase | 121 | 0 | [ 3 ] + +@fif_codegen +""" + testCodegenNoPureIndexedAccess PROC:<{ + // + 0 PUSHINT // '8=0 + }> +""" + +@fif_codegen +""" + test104 PROC:<{ + // + 5 PUSHINT // '2=5 + DUP // '2=5 '3=5 + PAIR // '1 + SINGLE // m + 10 PUSHINT // m '5=10 + 20 PUSHINT // m '5=10 '6=20 + s2 PUSH // m '5=10 '6=20 m + 0 INDEX // m '10=10 '12=20 '8 + SWAP // m '10=10 '8 '12=20 + 1 SETINDEX // m '10=10 '8 + SWAP // m '8 '10=10 + 0 SETINDEX // m '8 + 0 SETINDEX // m + ... +""" + +@fif_codegen +""" + testCodegenIndexPostfix1 PROC:<{ + // x.0 x.1 + // ab.1 ab.0 + SWAP // ab.0 ab.1 + }> +""" + +@fif_codegen +""" + testCodegenIndexPostfix2 PROC:<{ + // x.0 x.1.0 x.1.1 x.2 + s2 POP // y.0 y.2 y.1.1 + s1 s2 XCHG // y.2 y.0 y.1.1 + }> +""" + */ diff --git a/tolk-tester/tests/invalid-assign-1.tolk b/tolk-tester/tests/invalid-assign-1.tolk new file mode 100644 index 00000000..f605056e --- /dev/null +++ b/tolk-tester/tests/invalid-assign-1.tolk @@ -0,0 +1,9 @@ +fun main() { + var c = 1; + (c, c) = (2, 3); +} + +/** +@compilation_should_fail +@stderr one variable modified twice inside the same expression +*/ diff --git a/tolk-tester/tests/invalid-assign-2.tolk b/tolk-tester/tests/invalid-assign-2.tolk new file mode 100644 index 00000000..2838ed9a --- /dev/null +++ b/tolk-tester/tests/invalid-assign-2.tolk @@ -0,0 +1,11 @@ +fun incThree(mutate a: int, mutate b: int, mutate c: int) {} + +fun main() { + var c = [[[1, 2]]]; + incThree(mutate c.0.0.0, mutate c.0.0.1, mutate c.0.0.0); +} + +/** +@compilation_should_fail +@stderr one variable modified twice inside the same expression +*/ diff --git a/tolk-tester/tests/invalid-assign-3.tolk b/tolk-tester/tests/invalid-assign-3.tolk new file mode 100644 index 00000000..d3f5d1f1 --- /dev/null +++ b/tolk-tester/tests/invalid-assign-3.tolk @@ -0,0 +1,10 @@ +global gg: (int, int); + +fun main() { + [gg.0, gg.1, gg.0] = [0, 1, 0]; +} + +/** +@compilation_should_fail +@stderr one variable modified twice inside the same expression +*/ diff --git a/tolk-tester/tests/invalid-assign-4.tolk b/tolk-tester/tests/invalid-assign-4.tolk new file mode 100644 index 00000000..67340b20 --- /dev/null +++ b/tolk-tester/tests/invalid-assign-4.tolk @@ -0,0 +1,10 @@ +global gg: (int, [int, int]); + +fun main() { + (gg.1.0, gg.1, gg.1.1) = (0, [1, 2], 3); +} + +/** +@compilation_should_fail +@stderr one variable both modified and read inside the same expression +*/ diff --git a/tolk-tester/tests/invalid-assign-5.tolk b/tolk-tester/tests/invalid-assign-5.tolk new file mode 100644 index 00000000..f3fe59f7 --- /dev/null +++ b/tolk-tester/tests/invalid-assign-5.tolk @@ -0,0 +1,9 @@ +fun main() { + var ab = (1, 2); + (ab, ab.1) = ((2, 3), 4); +} + +/** +@compilation_should_fail +@stderr one variable both modified and read inside the same expression +*/ diff --git a/tolk-tester/tests/invalid-assign-6.tolk b/tolk-tester/tests/invalid-assign-6.tolk new file mode 100644 index 00000000..59d769e9 --- /dev/null +++ b/tolk-tester/tests/invalid-assign-6.tolk @@ -0,0 +1,9 @@ +fun main() { + var t = createEmptyTuple(); + t.0 = (1, 2); +} + +/** +@compilation_should_fail +@stderr can not put `(int, int)` into a tuple, because it occupies 2 stack slots in TVM, not 1 +*/ diff --git a/tolk-tester/tests/invalid-assign-7.tolk b/tolk-tester/tests/invalid-assign-7.tolk new file mode 100644 index 00000000..6a33e696 --- /dev/null +++ b/tolk-tester/tests/invalid-assign-7.tolk @@ -0,0 +1,8 @@ +fun main(cs: slice) { + var cb = cs.tupleSize; +} + +/** +@compilation_should_fail +@stderr referencing a method for `tuple` with object of type `slice` +*/ diff --git a/tolk-tester/tests/invalid-call-1.tolk b/tolk-tester/tests/invalid-call-1.tolk index 3542f580..7435bb3c 100644 --- a/tolk-tester/tests/invalid-call-1.tolk +++ b/tolk-tester/tests/invalid-call-1.tolk @@ -6,5 +6,5 @@ fun main(x: int) { /** @compilation_should_fail -@stderr calling a non-function +@stderr non-existing method `asdf` of type `int` */ diff --git a/tolk-tester/tests/invalid-call-10.tolk b/tolk-tester/tests/invalid-call-10.tolk new file mode 100644 index 00000000..9a28c004 --- /dev/null +++ b/tolk-tester/tests/invalid-call-10.tolk @@ -0,0 +1,11 @@ +fun takeInvalidTuple(t: [int, (int, builder), int]) { +} + +fun main() { + takeInvalidTuple([1, (2, beginCell()), 0]); +} + +/** +@compilation_should_fail +@stderr can not put `(int, builder)` into a tuple, because it occupies 2 stack slots in TVM, not 1 + */ diff --git a/tolk-tester/tests/invalid-call-11.tolk b/tolk-tester/tests/invalid-call-11.tolk new file mode 100644 index 00000000..f631c546 --- /dev/null +++ b/tolk-tester/tests/invalid-call-11.tolk @@ -0,0 +1,11 @@ +fun main() { + var functions = (beginCell, beginCell); + var b = functions.1(); // ok + var c = functions.2(); // error +} + +/** +@compilation_should_fail +@stderr invalid tensor index, expected 0..1 +@stderr functions.2() + */ diff --git a/tolk-tester/tests/invalid-call-7.tolk b/tolk-tester/tests/invalid-call-7.tolk index 4ad038c9..cf8c788c 100644 --- a/tolk-tester/tests/invalid-call-7.tolk +++ b/tolk-tester/tests/invalid-call-7.tolk @@ -9,6 +9,6 @@ fun main() { /** @compilation_should_fail -@stderr undefined symbol `storeUnexisting` +@stderr non-existing method `storeUnexisting` of type `builder` @stderr .storeUnexisting() */ diff --git a/tolk-tester/tests/invalid-call-8.tolk b/tolk-tester/tests/invalid-call-8.tolk index c613d7d9..199aa681 100644 --- a/tolk-tester/tests/invalid-call-8.tolk +++ b/tolk-tester/tests/invalid-call-8.tolk @@ -1,8 +1,10 @@ +fun get_incoming_value() { return 3; } + fun main() { var incoming_ton: int = get_incoming_value().3(); } /** @compilation_should_fail -@stderr expected method name, got `3` +@stderr type `int` is not indexable */ diff --git a/tolk-tester/tests/invalid-generics-12.tolk b/tolk-tester/tests/invalid-generics-12.tolk new file mode 100644 index 00000000..62a6f5da --- /dev/null +++ b/tolk-tester/tests/invalid-generics-12.tolk @@ -0,0 +1,15 @@ +fun getTwo(): X { return 2; } + +fun cantDeduceNonArgumentGeneric() { + var t1: [int] = [0]; + t1.0 = getTwo(); // ok + var t2 = createEmptyTuple(); + t2.tuplePush(0); + t2.0 = getTwo(); // error, can't decude X +} + +/** +@compilation_should_fail +@stderr can not deduce X for generic function `getTwo` +@stderr t2.0 = getTwo(); + */ diff --git a/tolk-tester/tests/invalid-typing-13.tolk b/tolk-tester/tests/invalid-typing-13.tolk new file mode 100644 index 00000000..e356d0f3 --- /dev/null +++ b/tolk-tester/tests/invalid-typing-13.tolk @@ -0,0 +1,9 @@ +fun failAssignToInvalidTupleIndex() { + var ab = [1, 2]; + ab.100500 = 5; +} + +/** +@compilation_should_fail +@stderr invalid tuple index, expected 0..1 + */ diff --git a/tolk-tester/tests/known-bugs.tolk b/tolk-tester/tests/known-bugs.tolk deleted file mode 100644 index 4de6a375..00000000 --- a/tolk-tester/tests/known-bugs.tolk +++ /dev/null @@ -1,27 +0,0 @@ -fun increment(mutate x: int): int { - x = x + 1; - return x; -} - -@method_id(101) -fun bugWithModifyingMethodInsideSameExpression() { - /* - The same bug existed in FunC: -#pragma allow-post-modification; -(int, int) ~increment(int x) { x = x + 5; return (x, x); } -int main() { int x = 0; x += x~increment(); return x; } - It's related to using a variable modified by ~method inside the same expression. - */ - var x = 0; - x = x + increment(mutate x); - return x; -} - -fun main() { - -} - -/** -// correct: 2 -@testcase | 101 | | 1 - */ diff --git a/tolk-tester/tests/logical-operators.tolk b/tolk-tester/tests/logical-operators.tolk index 9ef4858d..29cd1d10 100644 --- a/tolk-tester/tests/logical-operators.tolk +++ b/tolk-tester/tests/logical-operators.tolk @@ -210,15 +210,15 @@ fun main() { compileTimeEval1 PROC:<{ // x DUP // x x - 0 EQINT // x _1 - FALSE // x _1 _4 - TRUE // x _1 _4 _7 - FALSE // x _1 _4 _7 _11 - s0 s4 XCHG // _11 _1 _4 _7 x - 0 EQINT // _11 _1 _4 _7 _12 - -10 EQINT // _11 _1 _4 _7 _14 + 0 EQINT // x '1 + FALSE // x '1 '4 + TRUE // x '1 '4 '7 + FALSE // x '1 '4 '7 '11 + s0 s4 XCHG // '11 '1 '4 '7 x + 0 EQINT // '11 '1 '4 '7 '12 + -10 EQINT // '11 '1 '4 '7 '14 s3 s4 XCHG - s1 s3 s0 XCHG3 // _1 _4 _7 _11 _14 + s1 s3 s0 XCHG3 // '1 '4 '7 '11 '14 }> """ @@ -230,15 +230,15 @@ fun main() { OVER // x y x IFNOTJMP:<{ // x y 2DROP // - 10 PUSHINT // _2=10 + 10 PUSHINT // '2=10 }> // x y DUP // x y y IFNOTJMP:<{ // x y 2DROP // - 20 PUSHINT // _3=20 + 20 PUSHINT // '3=20 RETALT }> // x y - ADD // _4 + ADD // '4 }> """ @@ -297,10 +297,10 @@ These are moments of future optimizations. For now, it's more than enough. // a b SWAP // b a IF:<{ // b - 0 NEQINT // _2 + 0 NEQINT // '2 }>ELSE<{ // b DROP // - 0 PUSHINT // _2=0 + 0 PUSHINT // '2=0 }> }> """ @@ -310,13 +310,13 @@ These are moments of future optimizations. For now, it's more than enough. testOrSimpleCodegen PROC:<{ // a b SWAP // b a - 0 GTINT // b _3 + 0 GTINT // b '3 IF:<{ // b DROP // - -1 PUSHINT // _4=-1 + -1 PUSHINT // '4=-1 }>ELSE<{ // b - 0 GTINT // _7 - 0 NEQINT // _4 + 0 GTINT // '7 + 0 NEQINT // '4 }> }> """ @@ -332,15 +332,15 @@ These are moments of future optimizations. For now, it's more than enough. DUP // x x IFNOTJMP:<{ // x DROP // - 1 PUSHINT // _5=1 + 1 PUSHINT // '5=1 }> // x DUP // x x IFNOTJMP:<{ // x DROP // - 1 PUSHINT // _6=1 + 1 PUSHINT // '6=1 }> // x 100 THROWIFNOT - -4 PUSHINT // _9=-4 + -4 PUSHINT // '9=-4 }> """ diff --git a/tolk-tester/tests/mutate-methods.tolk b/tolk-tester/tests/mutate-methods.tolk index 0a5ca191..ebd07aca 100644 --- a/tolk-tester/tests/mutate-methods.tolk +++ b/tolk-tester/tests/mutate-methods.tolk @@ -307,7 +307,7 @@ fun main(){} ... incrementTwoInPlace CALLDICT // x y sum1 -ROT - 10 PUSHINT // sum1 x y _10=10 + 10 PUSHINT // sum1 x y '10=10 incrementTwoInPlace CALLDICT // sum1 x y sum2 s1 s3 s0 XCHG3 // x y sum1 sum2 }> @@ -317,8 +317,8 @@ fun main(){} """ load_next PROC:<{ // cs - 32 LDI // _4 cs - SWAP // cs _4 + 32 LDI // '4 cs + SWAP // cs '4 }> """ @@ -326,7 +326,7 @@ fun main(){} """ testStoreUintPureUnusedResult PROC:<{ // - 0 PUSHINT // _11=0 + 0 PUSHINT // '11=0 }> """ @@ -335,9 +335,9 @@ fun main(){} testStoreUintImpureUnusedResult PROC:<{ // NEWC // b - STIX // _2 + STIX // '2 DROP // - 0 PUSHINT // _11=0 + 0 PUSHINT // '11=0 }> """ diff --git a/tolk-tester/tests/no-spaces.tolk b/tolk-tester/tests/no-spaces.tolk index da733898..409bb342 100644 --- a/tolk-tester/tests/no-spaces.tolk +++ b/tolk-tester/tests/no-spaces.tolk @@ -104,10 +104,10 @@ fun`main`(){} DUP // fst1=-1 snd1=-1 2 PUSHINT // fst1=-1 snd1=-1 trd1=2 s1 s1 s0 PUSH3 // fst1=-1 snd1=-1 trd1=2 fst2=-1 snd2=-1 trd2=2 - add3 CALLDICT // fst1=-1 snd1=-1 trd1=2 _13 - 3 -ROLL // _13 fst1=-1 snd1=-1 trd1=2 - add3 CALLDICT // _13 _14 - PAIR // _12 + add3 CALLDICT // fst1=-1 snd1=-1 trd1=2 '13 + 3 -ROLL // '13 fst1=-1 snd1=-1 trd1=2 + add3 CALLDICT // '13 '14 + PAIR // '12 }> """ diff --git a/tolk-tester/tests/null-keyword.tolk b/tolk-tester/tests/null-keyword.tolk index 40e391c8..69678434 100644 --- a/tolk-tester/tests/null-keyword.tolk +++ b/tolk-tester/tests/null-keyword.tolk @@ -99,21 +99,21 @@ fun main() { test1 PROC:<{ // PUSHNULL // numbers - 1 PUSHINT // numbers _2=1 - SWAP // _2=1 numbers + 1 PUSHINT // numbers '2=1 + SWAP // '2=1 numbers CONS // numbers - 2 PUSHINT // numbers _4=2 - SWAP // _4=2 numbers + 2 PUSHINT // numbers '4=2 + SWAP // '4=2 numbers CONS // numbers - 3 PUSHINT // numbers _6=3 - SWAP // _6=3 numbers + 3 PUSHINT // numbers '6=3 + SWAP // '6=3 numbers CONS // numbers - 4 PUSHINT // numbers _8=4 - SWAP // _8=4 numbers + 4 PUSHINT // numbers '8=4 + SWAP // '8=4 numbers CONS // numbers UNCONS // h numbers DUP // h numbers numbers - CAR // h numbers _13 + CAR // h numbers '13 """ @fif_codegen @@ -121,11 +121,11 @@ fun main() { main PROC:<{ // PUSHNULL // i - ISNULL // _2 + ISNULL // '2 IFJMP:<{ // - 1 PUSHINT // _3=1 + 1 PUSHINT // '3=1 }> // - 10 PUSHINT // _4=10 + 10 PUSHINT // '4=10 }> """ @@ -133,14 +133,14 @@ fun main() { """ test7 PROC:<{ ... - LDOPTREF // b _8 _7 + LDOPTREF // b '8 '7 DROP // b c - ISNULL // b _11 - 10 MULCONST // b _13 - SWAP // _13 b - ISNULL // _13 _14 - NOT // _13 _15 - ADD // _16 + ISNULL // b '11 + 10 MULCONST // b '13 + SWAP // '13 b + ISNULL // '13 '14 + NOT // '13 '15 + ADD // '16 }> """ */ diff --git a/tolk-tester/tests/op-priority.tolk b/tolk-tester/tests/op-priority.tolk index 8a57b394..e3c6bb6e 100644 --- a/tolk-tester/tests/op-priority.tolk +++ b/tolk-tester/tests/op-priority.tolk @@ -95,26 +95,26 @@ fun main() { unary_minus_1 PROC:<{ // a b c -ROT // c a b - ADD // c _3 - NEGATE // c _4 - SWAP // _4 c - MUL // _5 + ADD // c '3 + NEGATE // c '4 + SWAP // '4 c + MUL // '5 }> unary_minus_2 PROC:<{ // a b c -ROT // c a b - ADD // c _3 - NEGATE // c _4 - SWAP // _4 c - MUL // _5 + ADD // c '3 + NEGATE // c '4 + SWAP // '4 c + MUL // '5 }> unary_minus_3 PROC:<{ // a b c -ROT // c a b - ADD // c _3 - SWAP // _3 c - MUL // _4 - NEGATE // _5 + ADD // c '3 + SWAP // '3 c + MUL // '4 + NEGATE // '5 }> """ diff --git a/tolk-tester/tests/use-before-declare.tolk b/tolk-tester/tests/use-before-declare.tolk index 384569b9..d3e6b165 100644 --- a/tolk-tester/tests/use-before-declare.tolk +++ b/tolk-tester/tests/use-before-declare.tolk @@ -43,7 +43,7 @@ const demo_20: int = 20; """ test1 PROC:<{ // - 30 PUSHINT // _10 + 30 PUSHINT // '10 }> """ */ diff --git a/tolk-tester/tests/var-apply.tolk b/tolk-tester/tests/var-apply.tolk index a0918c18..16863560 100644 --- a/tolk-tester/tests/var-apply.tolk +++ b/tolk-tester/tests/var-apply.tolk @@ -129,6 +129,15 @@ fun testVarApply3() { return (getIntAt(t, 0), getTupleFirstInt(t), getTupleLastTuple(t), getTupleLastGetter()(t)); } +@method_id(107) +fun testIndexedAccessApply() { + var functions1 = (beginCell, endCell); + var functions2 = [beginParse]; + var b = functions1.0().storeInt(1, 16); + b.storeInt(1, 16); + return functions2.0(functions1.1(b)).loadInt(32); +} + fun main() {} /** @@ -138,4 +147,5 @@ fun main() {} @testcase | 104 | | 240 @testcase | 105 | | 1 @testcase | 106 | | 1 1 [ 2 ] [ 2 ] +@testcase | 107 | | 65537 */ diff --git a/tolk/abscode.cpp b/tolk/abscode.cpp index 25ddd10e..b465b72b 100644 --- a/tolk/abscode.cpp +++ b/tolk/abscode.cpp @@ -26,22 +26,28 @@ namespace tolk { * */ -void TmpVar::dump(std::ostream& os) const { - show(os); - os << " : " << v_type << " (width "; - os << v_type->calc_width_on_stack(); - os << ")"; - os << std::endl; +void TmpVar::show_as_stack_comment(std::ostream& os) const { + if (!name.empty()) { + os << name; + } else { + os << '\'' << ir_idx; + } +#ifdef TOLK_DEBUG + // uncomment for detailed stack output, like `'15(binary-op) '16(glob-var)` + // if (desc) os << desc; +#endif } -void TmpVar::show(std::ostream& os, int omit_idx) const { - if (v_sym) { - os << v_sym->name; - if (omit_idx >= 2) { - return; - } +void TmpVar::show(std::ostream& os) const { + os << '\'' << ir_idx; // vars are printed out as `'1 '2` (in stack comments, debug info, etc.) + if (!name.empty()) { + os << '_' << name; } - os << '_' << ir_idx; +#ifdef TOLK_DEBUG + if (desc) { + os << ' ' << desc; // "origin" of implicitly created tmp var, like `'15 (binary-op) '16 (glob-var)` + } +#endif } std::ostream& operator<<(std::ostream& os, const TmpVar& var) { @@ -95,7 +101,7 @@ void VarDescr::show(std::ostream& os, const char* name) const { if (name) { os << name; } - os << '_' << idx; + os << '\'' << idx; show_value(os); } @@ -333,7 +339,7 @@ void Op::show_var_list(std::ostream& os, const std::vector& idx_list, } else { os << "(" << vars.at(idx_list[0]); for (std::size_t i = 1; i < idx_list.size(); i++) { - os << "," << vars.at(idx_list[i]); + os << ", " << vars.at(idx_list[i]); } os << ")"; } @@ -378,11 +384,12 @@ void CodeBlob::print(std::ostream& os, int flags) const { os << "CODE BLOB: " << var_cnt << " variables, " << in_var_cnt << " input\n"; if ((flags & 8) != 0) { for (const auto& var : vars) { - var.dump(os); - if (var.where.is_defined() && (flags & 1) != 0) { - var.where.show(os); + var.show(os); + os << " : " << var.v_type << std::endl; + if (var.loc.is_defined() && (flags & 1) != 0) { + var.loc.show(os); os << " defined here:\n"; - var.where.show_context(os); + var.loc.show_context(os); } } } @@ -393,21 +400,25 @@ void CodeBlob::print(std::ostream& os, int flags) const { os << "-------- END ---------\n\n"; } -std::vector CodeBlob::create_var(TypePtr var_type, const LocalVarData* v_sym, SrcLocation loc) { +std::vector CodeBlob::create_var(TypePtr var_type, SrcLocation loc, std::string name) { std::vector ir_idx; - ir_idx.reserve(var_type->calc_width_on_stack()); + int stack_w = var_type->calc_width_on_stack(); + ir_idx.reserve(stack_w); if (const TypeDataTensor* t_tensor = var_type->try_as()) { - for (TypePtr item : t_tensor->items) { - std::vector nested = create_var(item, v_sym, loc); + for (int i = 0; i < t_tensor->size(); ++i) { + std::string sub_name = name.empty() ? name : name + "." + std::to_string(i); + std::vector nested = create_var(t_tensor->items[i], loc, std::move(sub_name)); ir_idx.insert(ir_idx.end(), nested.begin(), nested.end()); } } else if (var_type != TypeDataVoid::create()) { - tolk_assert(var_type->calc_width_on_stack() == 1); - vars.emplace_back(var_cnt, var_type, v_sym, loc); +#ifdef TOLK_DEBUG + tolk_assert(stack_w == 1); +#endif + vars.emplace_back(var_cnt, var_type, std::move(name), loc); ir_idx.emplace_back(var_cnt); var_cnt++; } - tolk_assert(static_cast(ir_idx.size()) == var_type->calc_width_on_stack()); + tolk_assert(static_cast(ir_idx.size()) == stack_w); return ir_idx; } diff --git a/tolk/asmops.cpp b/tolk/asmops.cpp index 547922da..2618ed26 100644 --- a/tolk/asmops.cpp +++ b/tolk/asmops.cpp @@ -302,24 +302,13 @@ Const AsmOpList::get_const(const_idx_t idx) { } } -void AsmOpList::show_var(std::ostream& os, var_idx_t idx) const { - if (!var_names_ || (unsigned)idx >= var_names_->size()) { - os << '_' << idx; - } else { - var_names_->at(idx).show(os, 2); - } -} - void AsmOpList::show_var_ext(std::ostream& os, std::pair idx_pair) const { - auto i = idx_pair.first; - auto j = idx_pair.second; + var_idx_t i = idx_pair.first; + const_idx_t j = idx_pair.second; if (!var_names_ || (unsigned)i >= var_names_->size()) { - os << '_' << i; + os << '\'' << i; } else { - var_names_->at(i).show(os, 2); - // if (!var_names_->at(i).v_type->is_int()) { - // os << '<'; var_names_->at(i).v_type->print(os); os << '>'; - // } + var_names_->at(i).show_as_stack_comment(os); } if ((unsigned)j < constants_.size() && constants_[j].not_null()) { os << '=' << constants_[j]; diff --git a/tolk/ast-from-tokens.cpp b/tolk/ast-from-tokens.cpp index 58592011..3eb4385a 100644 --- a/tolk/ast-from-tokens.cpp +++ b/tolk/ast-from-tokens.cpp @@ -405,12 +405,15 @@ static AnyExprV parse_expr80(Lexer& lex) { lex.next(); V v_ident = nullptr; V v_instantiationTs = nullptr; - if (lex.tok() == tok_identifier) { + if (lex.tok() == tok_identifier) { // obj.field / obj.method v_ident = createV(lex.cur_location(), lex.cur_str()); lex.next(); if (lex.tok() == tok_lt) { v_instantiationTs = parse_maybe_instantiationTs_after_identifier(lex); } + } else if (lex.tok() == tok_int_const) { // obj.0 (indexed access) + v_ident = createV(lex.cur_location(), lex.cur_str()); + lex.next(); } else { lex.unexpected("method name"); } diff --git a/tolk/ast.h b/tolk/ast.h index b90507e7..d2db49f8 100644 --- a/tolk/ast.h +++ b/tolk/ast.h @@ -529,8 +529,14 @@ private: public: - typedef const FunctionData* DotTarget; // for `t.tupleAt` target is `tupleAt` global function - DotTarget target = nullptr; // filled at type inferring + typedef std::variant< + const FunctionData*, // for `t.tupleAt` target is `tupleAt` global function + int // for `t.0` target is "indexed access" 0 + > DotTarget; + DotTarget target = static_cast(nullptr); // filled at type inferring + + bool is_target_fun_ref() const { return std::holds_alternative(target); } + bool is_target_indexed_access() const { return std::holds_alternative(target); } AnyExprV get_obj() const { return child; } auto get_identifier() const { return identifier; } diff --git a/tolk/builtins.cpp b/tolk/builtins.cpp index 73aef2d9..2b207c25 100644 --- a/tolk/builtins.cpp +++ b/tolk/builtins.cpp @@ -1060,6 +1060,17 @@ AsmOp compile_tuple_at(std::vector& res, std::vector& args, return exec_op("INDEXVAR", 2, 1); } +// fun tupleSetAt(mutate self: tuple, value: X, index: int): void asm "SETINDEXVAR"; +AsmOp compile_tuple_set_at(std::vector& res, std::vector& args, SrcLocation) { + tolk_assert(args.size() == 3 && res.size() == 1); + auto& y = args[2]; + if (y.is_int_const() && y.int_const >= 0 && y.int_const < 16) { + y.unused(); + return exec_arg_op("SETINDEX", y.int_const, 1, 1); + } + return exec_op("SETINDEXVAR", 2, 1); +} + // fun __isNull(X arg): bool AsmOp compile_is_null(std::vector& res, std::vector& args, SrcLocation) { tolk_assert(args.size() == 1 && res.size() == 1); @@ -1246,6 +1257,9 @@ void define_builtins() { define_builtin_func("tupleAt", {Tuple, Int}, typeT, declGenericT, compile_tuple_at, FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf); + define_builtin_func("tupleSetAt", {Tuple, typeT, Int}, Unit, declGenericT, + compile_tuple_set_at, + FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf); define_builtin_func("debugPrint", {typeT}, Unit, declGenericT, AsmOp::Custom("s0 DUMP DROP", 1, 1), 0); diff --git a/tolk/codegen.cpp b/tolk/codegen.cpp index 3830f7ae..ad61b8a5 100644 --- a/tolk/codegen.cpp +++ b/tolk/codegen.cpp @@ -132,7 +132,7 @@ int Stack::drop_vars_except(const VarDescrList& var_info, int excl_var) { return dropped; } -void Stack::show(int flags) { +void Stack::show() { std::ostringstream os; for (auto i : s) { os << ' '; diff --git a/tolk/pipe-ast-to-legacy.cpp b/tolk/pipe-ast-to-legacy.cpp index c64fe751..f5eca22c 100644 --- a/tolk/pipe-ast-to-legacy.cpp +++ b/tolk/pipe-ast-to-legacy.cpp @@ -21,6 +21,7 @@ #include "type-system.h" #include "common/refint.h" #include "constant-evaluator.h" +#include /* * This pipe is the last one operating AST: it transforms AST to IR. @@ -28,38 +29,218 @@ * kernel (initially forked from FunC) comes into play. * Up to this point, all types have been inferred, all validity checks have been passed, etc. * All properties in AST nodes are assigned and can be safely used (fun_ref, etc.). - * So, if execution reaches this pass, the input is correct, and code generation should succeed. + * So, if execution reaches this pass, the input is (almost) correct, and code generation should succeed. + * The only thing additionally checked during this pass is tricky lvalue, like one and the same variable + * assigned/mutated multiple times in same expression, e.g. `(t.0, t.0) = rhs` / `f(mutate x.1.2, mutate x)`. */ namespace tolk { -struct LValGlobs { - std::vector>> globs; +// fire error on cases like `(a, a) = rhs` / `f(mutate t.1.0, mutate t.1.0)` +GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD +static void fire_error_variable_modified_twice_inside_same_expression(SrcLocation loc) { + throw ParseError(loc, "one variable modified twice inside the same expression"); +} - void add_modified_glob(const GlobalVarData* g_sym, std::vector local_ir_idx) { - globs.emplace_back(g_sym, std::move(local_ir_idx)); +// fire error on cases like `(m.1.0, m.1) = rhs` (m.1 inside m.1.0 is "rval inside lval") +GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD +static void fire_error_variable_modified_and_read_inside_same_expression(SrcLocation loc) { + throw ParseError(loc, "one variable both modified and read inside the same expression"); +} + +// Main goal of LValContext is to handle non-primitive lvalues. At IR level, a usual local variable +// exists, but on its change, something non-trivial should happen. +// Example: `globalVar = 9` actually does `Const $5 = 9` + `Let $6 = $5` + `SetGlob "globVar" = $6` +// Example: `tupleVar.0 = 9` actually does `Const $5 = 9` + `Let $6 = $5` + `Const $7 = 0` + `Call tupleSetAt($4, $6, $7)` +// Of course, mixing globals with tuples should also be supported. +// To achieve this, treat tupleObj inside "tupleObj.i" like "rvalue inside lvalue". +// For instance, `globalTuple.0 = 9` reads global (like rvalue), assigns 9 to tmp var, modifies tuple, writes global. +// A challenging thing is handling "unique" parts, to be read/updated only once. +// Example: `f(mutate globalTensor.0, mutate globalTensor.1)`, then globalTensor should be read/written once. +// Example: `(t.0.0, t.0.1) = rhs` (m is [[int, int]]), then t.0 should be read/updated once. +// Solving this by calculating hashes of every lvalue or rvalue inside lvalue automatically gives an ability +// to detect and fire "multiple writes inside expression", like `(a, a) = rhs` / `[t.0, (t.0.1, c)] = rhs`. +// Note, that tensors (not tuples) `tensorVar.0 = 9` do not emit anything special (unless global). +class LValContext { + // every global variable used as lvalue is registered here + // example: `globalInt = 9`, implicit var is created `$tmp = 9`, and `SetGlob "globalInt" $tmp` is done after + // global tensors are stored as tuples (unpacked on reading, packed on writing), then multiple tmp vars are created + struct ModifiedGlob { + const GlobalVarData* glob_ref; + std::vector local_ir_idx; // typically 1, generally calc_width_on_stack() of global var (tensors) + + void apply(CodeBlob& code, SrcLocation loc) const { + Op& op = code.emplace_back(loc, Op::_SetGlob, std::vector{}, local_ir_idx, glob_ref); + op.set_impure_flag(); + } + }; + + // every tuple index used as lvalue is registered here + // example: `t.0 = 9`, implicit var is created `$tmp = 9`, as well as `$tmp_idx = 0` and `tupleSetAt()` is done after + // for `t.0.0` if t is `[[int, ...]]`, `tupleAt()` for it is done since it's rvalue, and `tupleSetAt()` is done 2 times + struct ModifiedTupleIndex { + uint64_t hash; + var_idx_t tuple_ir_idx; + var_idx_t index_ir_idx; + var_idx_t field_ir_idx; + + void apply(CodeBlob& code, SrcLocation loc) const { + const FunctionData* builtin_sym = lookup_global_symbol("tupleSetAt")->as(); + code.emplace_back(loc, Op::_Call, std::vector{tuple_ir_idx}, std::vector{tuple_ir_idx, field_ir_idx, index_ir_idx}, builtin_sym); + } + }; + + int level_rval_inside_lval = 0; + std::vector> modifications; + std::unordered_set all_modified_hashes; + + void fire_if_one_variable_modified_twice(SrcLocation loc, uint64_t modified_hash) { + if (!is_rval_inside_lval()) { + if (!all_modified_hashes.insert(modified_hash).second) { + fire_error_variable_modified_twice_inside_same_expression(loc); + } + if (all_modified_hashes.contains(~modified_hash)) { + fire_error_variable_modified_and_read_inside_same_expression(loc); + } + } else { + all_modified_hashes.insert(~modified_hash); + if (all_modified_hashes.contains(modified_hash)) { + fire_error_variable_modified_and_read_inside_same_expression(loc); + } + } } - void gen_ops_set_globs(CodeBlob& code, SrcLocation loc) const { - for (const auto& [g_sym, ir_idx] : globs) { - Op& op = code.emplace_back(loc, Op::_SetGlob, std::vector{}, ir_idx, g_sym); - op.set_impure_flag(); +public: + void enter_rval_inside_lval() { level_rval_inside_lval++; } + void exit_rval_inside_lval() { level_rval_inside_lval--; } + bool is_rval_inside_lval() const { return level_rval_inside_lval > 0; } + + uint64_t register_lval(SrcLocation loc, const LocalVarData* var_ref) { + uint64_t hash = reinterpret_cast(var_ref); + fire_if_one_variable_modified_twice(loc, hash); + return hash; + } + + uint64_t register_lval(SrcLocation loc, const GlobalVarData* glob_ref) { + uint64_t hash = reinterpret_cast(glob_ref); + fire_if_one_variable_modified_twice(loc, hash); + return hash; + } + + uint64_t register_lval(SrcLocation loc, V v) { + uint64_t hash = 7; + AnyExprV leftmost_obj = v; + while (auto v_dot = leftmost_obj->try_as()) { + if (!v_dot->is_target_indexed_access()) { + break; + } + hash = hash * 1915239017 + std::get(v_dot->target); + leftmost_obj = v_dot->get_obj(); + } + if (auto v_ref = leftmost_obj->try_as()) { + hash *= reinterpret_cast(v_ref->sym); // `v.0` and `v.0` in 2 places is the same + } else { + hash *= reinterpret_cast(leftmost_obj); // unlike `f().0` and `f().0` (pointers to AST nodes differ) + } + fire_if_one_variable_modified_twice(loc, hash); + return hash; + } + + const std::vector* exists_already_known_global(const GlobalVarData* glob_ref) const { + for (const auto& m : modifications) { + if (const auto* m_glob = std::get_if(&m); m_glob && m_glob->glob_ref == glob_ref) { + return &m_glob->local_ir_idx; + } + } + return nullptr; + } + + const var_idx_t* exists_already_known_tuple_index(uint64_t hash) const { + for (const auto& m : modifications) { + if (const auto* m_tup = std::get_if(&m); m_tup && m_tup->hash == hash) { + return &m_tup->field_ir_idx; + } + } + return nullptr; + } + + void register_modified_global(const GlobalVarData* glob_ref, std::vector local_ir_idx) { + modifications.emplace_back(ModifiedGlob{glob_ref, std::move(local_ir_idx)}); + } + + void register_modified_tuple_index(uint64_t hash, var_idx_t tuple_ir_idx, var_idx_t index_ir_idx, var_idx_t field_ir_idx) { + modifications.emplace_back(ModifiedTupleIndex{hash, tuple_ir_idx, index_ir_idx, field_ir_idx}); + } + + void gen_ops_if_nonempty(CodeBlob& code, SrcLocation loc) const { + for (auto it = modifications.rbegin(); it != modifications.rend(); ++it) { // reverse, it's important + if (const auto* m_glob = std::get_if(&*it)) { + m_glob->apply(code, loc); + } else if (const auto* m_tup = std::get_if(&*it)) { + m_tup->apply(code, loc); + } } } }; -std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, LValGlobs* lval_globs = nullptr); +// The goal of VarsModificationWatcher is to detect such cases: `return (x, x += y, x)`. +// Without any changes, ops will be { _Call $2 = +($0_x, $1_y); _Return $0_x, $2, $0_x } - incorrect +// Correct will be to introduce tmp var: { _Let $3 = $0_x; _Call $2 = ...; _Return $3, $2, $0_x } +// This "introducing" is done when compiling tensors, whereas this class allows to watch vars for modification. +class VarsModificationWatcher { + struct WatchedVar { + var_idx_t ir_idx; + std::function on_modification_callback; + + WatchedVar(var_idx_t ir_idx, std::function on_modification_callback) + : ir_idx(ir_idx), on_modification_callback(std::move(on_modification_callback)) {} + }; + + std::vector all_callbacks; + +public: + + bool empty() const { return all_callbacks.empty(); } + + void push_callback(var_idx_t ir_idx, std::function callback) { + all_callbacks.emplace_back(ir_idx, std::move(callback)); + } + + void pop_callback(var_idx_t ir_idx) { + for (auto it = all_callbacks.rbegin(); it != all_callbacks.rend(); ++it) { + if (it->ir_idx == ir_idx) { + all_callbacks.erase((it + 1).base()); + return; + } + } + tolk_assert(false); + } + + void trigger_callbacks(const std::vector& left_lval_indices, SrcLocation loc) const { + for (const WatchedVar& w : all_callbacks) { + for (var_idx_t changed_var : left_lval_indices) { + if (w.ir_idx == changed_var) { + w.on_modification_callback(loc, w.ir_idx); + } + } + } + } +}; + +static VarsModificationWatcher vars_modification_watcher; + +std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, LValContext* lval_ctx = nullptr); void process_any_statement(AnyV v, CodeBlob& code); static std::vector> pre_compile_tensor_inner(CodeBlob& code, const std::vector& args, - LValGlobs* lval_globs) { + LValContext* lval_ctx) { const int n = static_cast(args.size()); if (n == 0) { // just `()` return {}; } if (n == 1) { // just `(x)`: even if x is modified (e.g. `f(x=x+2)`), there are no next arguments - return {pre_compile_expr(args[0], code, lval_globs)}; + return {pre_compile_expr(args[0], code, lval_ctx)}; } // the purpose is to handle such cases: `return (x, x += y, x)` @@ -81,9 +262,9 @@ static std::vector> pre_compile_tensor_inner(CodeBlob& co void add_and_watch_modifications(std::vector&& vars_of_ith_arg, CodeBlob& code) { for (var_idx_t ir_idx : vars_of_ith_arg) { - if (code.vars[ir_idx].v_sym && !is_watched(ir_idx)) { + if (!code.vars[ir_idx].name.empty() && !is_watched(ir_idx)) { watched_vars.emplace_back(ir_idx); - code.vars[ir_idx].on_modification.emplace_back([this, &code, ir_idx](SrcLocation loc) { + vars_modification_watcher.push_callback(ir_idx, [this, &code](SrcLocation loc, var_idx_t ir_idx) { on_var_modified(ir_idx, loc, code); }); } @@ -93,7 +274,7 @@ static std::vector> pre_compile_tensor_inner(CodeBlob& co void on_var_modified(var_idx_t ir_idx, SrcLocation loc, CodeBlob& code) { tolk_assert(is_watched(ir_idx)); - std::vector tmp_idx_arr = code.create_tmp_var(code.vars[ir_idx].v_type, loc); + std::vector tmp_idx_arr = code.create_tmp_var(code.vars[ir_idx].v_type, loc, "(pre-modified)"); tolk_assert(tmp_idx_arr.size() == 1); var_idx_t tmp_idx = tmp_idx_arr[0]; code.emplace_back(loc, Op::_Let, std::vector{tmp_idx}, std::vector{ir_idx}); @@ -102,9 +283,9 @@ static std::vector> pre_compile_tensor_inner(CodeBlob& co } } - std::vector> clear_and_stop_watching(CodeBlob& code) { + std::vector> clear_and_stop_watching() { for (var_idx_t ir_idx : watched_vars) { - code.vars[ir_idx].on_modification.pop_back(); + vars_modification_watcher.pop_callback(ir_idx); } watched_vars.clear(); return std::move(res_lists); @@ -113,15 +294,15 @@ static std::vector> pre_compile_tensor_inner(CodeBlob& co WatchingVarList watched_vars(n); for (int arg_idx = 0; arg_idx < n; ++arg_idx) { - std::vector vars_of_ith_arg = pre_compile_expr(args[arg_idx], code, lval_globs); + std::vector vars_of_ith_arg = pre_compile_expr(args[arg_idx], code, lval_ctx); watched_vars.add_and_watch_modifications(std::move(vars_of_ith_arg), code); } - return watched_vars.clear_and_stop_watching(code); + return watched_vars.clear_and_stop_watching(); } static std::vector pre_compile_tensor(CodeBlob& code, const std::vector& args, - LValGlobs* lval_globs = nullptr) { - std::vector> res_lists = pre_compile_tensor_inner(code, args, lval_globs); + LValContext* lval_ctx = nullptr) { + std::vector> res_lists = pre_compile_tensor_inner(code, args, lval_ctx); std::vector res; for (const std::vector& list : res_lists) { res.insert(res.end(), list.cbegin(), list.cend()); @@ -133,11 +314,11 @@ static std::vector pre_compile_let(CodeBlob& code, AnyExprV lhs, AnyE // [lhs] = [rhs]; since type checking is ok, it's the same as "lhs = rhs" if (lhs->type == ast_typed_tuple && rhs->type == ast_typed_tuple) { std::vector right = pre_compile_tensor(code, rhs->as()->get_items()); - LValGlobs globs; - std::vector left = pre_compile_tensor(code, lhs->as()->get_items(), &globs); - code.on_var_modification(left, loc); + LValContext local_lval; + std::vector left = pre_compile_tensor(code, lhs->as()->get_items(), &local_lval); + vars_modification_watcher.trigger_callbacks(left, loc); code.emplace_back(loc, Op::_Let, std::move(left), right); - globs.gen_ops_set_globs(code, loc); + local_lval.gen_ops_if_nonempty(code, loc); return right; } // [lhs] = rhs; it's un-tuple to N left vars @@ -145,29 +326,37 @@ static std::vector pre_compile_let(CodeBlob& code, AnyExprV lhs, AnyE std::vector right = pre_compile_expr(rhs, code); const TypeDataTypedTuple* inferred_tuple = rhs->inferred_type->try_as(); std::vector types_list = inferred_tuple->items; - std::vector rvect = code.create_tmp_var(TypeDataTensor::create(std::move(types_list)), rhs->loc); + std::vector rvect = code.create_tmp_var(TypeDataTensor::create(std::move(types_list)), rhs->loc, "(unpack-tuple)"); code.emplace_back(lhs->loc, Op::_UnTuple, rvect, std::move(right)); - LValGlobs globs; - std::vector left = pre_compile_tensor(code, lhs->as()->get_items(), &globs); - code.on_var_modification(left, loc); + LValContext local_lval; + std::vector left = pre_compile_tensor(code, lhs->as()->get_items(), &local_lval); + vars_modification_watcher.trigger_callbacks(left, loc); code.emplace_back(loc, Op::_Let, std::move(left), rvect); - globs.gen_ops_set_globs(code, loc); + local_lval.gen_ops_if_nonempty(code, loc); return rvect; } + // small optimization: `var x = rhs` or `local_var = rhs` (90% cases), LValContext not needed actually + if (lhs->type == ast_local_var_lhs || (lhs->type == ast_reference && lhs->as()->sym->try_as())) { + std::vector right = pre_compile_expr(rhs, code); + std::vector left = pre_compile_expr(lhs, code); // effectively, local_var->ir_idx + vars_modification_watcher.trigger_callbacks(left, loc); + code.emplace_back(loc, Op::_Let, std::move(left), right); + return right; + } // lhs = rhs std::vector right = pre_compile_expr(rhs, code); - LValGlobs globs; - std::vector left = pre_compile_expr(lhs, code, &globs); - code.on_var_modification(left, loc); + LValContext local_lval; + std::vector left = pre_compile_expr(lhs, code, &local_lval); + vars_modification_watcher.trigger_callbacks(left, loc); code.emplace_back(loc, Op::_Let, std::move(left), right); - globs.gen_ops_set_globs(code, loc); + local_lval.gen_ops_if_nonempty(code, loc); return right; } -static std::vector gen_op_call(CodeBlob& code, TypePtr ret_type, SrcLocation here, - std::vector&& args_vars, const FunctionData* fun_ref) { - std::vector rvect = code.create_tmp_var(ret_type, here); - Op& op = code.emplace_back(here, Op::_Call, rvect, std::move(args_vars), fun_ref); +static std::vector gen_op_call(CodeBlob& code, TypePtr ret_type, SrcLocation loc, + std::vector&& args_vars, const FunctionData* fun_ref, const char* debug_desc) { + std::vector rvect = code.create_tmp_var(ret_type, loc, debug_desc); + Op& op = code.emplace_back(loc, Op::_Call, rvect, std::move(args_vars), fun_ref); if (!fun_ref->is_marked_as_pure()) { op.set_impure_flag(); } @@ -175,30 +364,42 @@ static std::vector gen_op_call(CodeBlob& code, TypePtr ret_type, SrcL } -static std::vector process_symbol(SrcLocation loc, const Symbol* sym, CodeBlob& code, LValGlobs* lval_globs) { +static std::vector pre_compile_symbol(SrcLocation loc, const Symbol* sym, CodeBlob& code, LValContext* lval_ctx) { if (const auto* glob_ref = sym->try_as()) { - std::vector rvect = code.create_tmp_var(glob_ref->declared_type, loc); - if (lval_globs) { - lval_globs->add_modified_glob(glob_ref, rvect); - return rvect; + if (!lval_ctx) { + // `globalVar` is used for reading, just create local IR var to represent its value, Op GlobVar will fill it + // note, that global tensors are stored as a tuple an unpacked to N vars on read, N determined by declared_type + std::vector local_ir_idx = code.create_tmp_var(glob_ref->declared_type, loc, "(glob-var)"); + code.emplace_back(loc, Op::_GlobVar, local_ir_idx, std::vector{}, glob_ref); + return local_ir_idx; } else { - code.emplace_back(loc, Op::_GlobVar, rvect, std::vector{}, glob_ref); - return rvect; + // `globalVar = rhs` / `mutate globalVar` / `globalTuple.0 = rhs` + lval_ctx->register_lval(loc, glob_ref); + if (const std::vector* local_ir_idx = lval_ctx->exists_already_known_global(glob_ref)) { + return *local_ir_idx; // `f(mutate g.0, mutate g.1)`, then g will be read only once + } + std::vector local_ir_idx = code.create_tmp_var(glob_ref->declared_type, loc, "(glob-var)"); + if (lval_ctx->is_rval_inside_lval()) { // for `globalVar.0` "globalVar" is rvalue inside lvalue + // for `globalVar = rhs` don't read a global actually, but for `globalVar.0 = rhs` do + code.emplace_back(loc, Op::_GlobVar, local_ir_idx, std::vector{}, glob_ref); + } + lval_ctx->register_modified_global(glob_ref, local_ir_idx); + return local_ir_idx; } } if (const auto* const_ref = sym->try_as()) { if (const_ref->is_int_const()) { - std::vector rvect = code.create_tmp_var(TypeDataInt::create(), loc); + std::vector rvect = code.create_tmp_var(TypeDataInt::create(), loc, "(glob-const)"); code.emplace_back(loc, Op::_IntConst, rvect, const_ref->as_int_const()); return rvect; } else { - std::vector rvect = code.create_tmp_var(TypeDataSlice::create(), loc); + std::vector rvect = code.create_tmp_var(TypeDataSlice::create(), loc, "(glob-const)"); code.emplace_back(loc, Op::_SliceConst, rvect, const_ref->as_slice_const()); return rvect; } } if (const auto* fun_ref = sym->try_as()) { - std::vector rvect = code.create_tmp_var(fun_ref->inferred_full_type, loc); + std::vector rvect = code.create_tmp_var(fun_ref->inferred_full_type, loc, "(glob-var-fun)"); code.emplace_back(loc, Op::_GlobVar, rvect, std::vector{}, fun_ref); return rvect; } @@ -206,9 +407,12 @@ static std::vector process_symbol(SrcLocation loc, const Symbol* sym, #ifdef TOLK_DEBUG tolk_assert(static_cast(var_ref->ir_idx.size()) == var_ref->declared_type->calc_width_on_stack()); #endif + if (lval_ctx) { + lval_ctx->register_lval(loc, var_ref); + } return var_ref->ir_idx; } - throw Fatal("process_symbol"); + throw Fatal("pre_compile_symbol"); } static std::vector process_assign(V v, CodeBlob& code) { @@ -234,7 +438,7 @@ static std::vector process_binary_operator(V v, if (v->fun_ref) { // almost all operators, fun_ref was assigned at type inferring std::vector args_vars = pre_compile_tensor(code, {v->get_lhs(), v->get_rhs()}); - return gen_op_call(code, v->inferred_type, v->loc, std::move(args_vars), v->fun_ref); + return gen_op_call(code, v->inferred_type, v->loc, std::move(args_vars), v->fun_ref, "(binary-op)"); } if (t == tok_logical_and || t == tok_logical_or) { // do the following transformations: @@ -249,7 +453,7 @@ static std::vector process_binary_operator(V v, v_b_ne_0->mutate()->assign_fun_ref(lookup_global_symbol("_!=_")->as()); std::vector cond = pre_compile_expr(v->get_lhs(), code); tolk_assert(cond.size() == 1); - std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc); + std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc, "(cond)"); Op& if_op = code.emplace_back(v->loc, Op::_If, cond); code.push_set_cur(if_op.block0); code.emplace_back(v->loc, Op::_Let, rvect, pre_compile_expr(t == tok_logical_and ? v_b_ne_0 : v_1, code)); @@ -265,13 +469,13 @@ static std::vector process_binary_operator(V v, static std::vector process_unary_operator(V v, CodeBlob& code) { std::vector args_vars = pre_compile_tensor(code, {v->get_rhs()}); - return gen_op_call(code, v->inferred_type, v->loc, std::move(args_vars), v->fun_ref); + return gen_op_call(code, v->inferred_type, v->loc, std::move(args_vars), v->fun_ref, "(unary-op)"); } static std::vector process_ternary_operator(V v, CodeBlob& code) { std::vector cond = pre_compile_expr(v->get_cond(), code); tolk_assert(cond.size() == 1); - std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc); + std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc, "(cond)"); Op& if_op = code.emplace_back(v->loc, Op::_If, cond); code.push_set_cur(if_op.block0); code.emplace_back(v->get_when_true()->loc, Op::_Let, rvect, pre_compile_expr(v->get_when_true(), code)); @@ -282,13 +486,67 @@ static std::vector process_ternary_operator(V v return rvect; } -static std::vector process_dot_access(V v, CodeBlob& code, LValGlobs* lval_globs) { +static std::vector process_dot_access(V v, CodeBlob& code, LValContext* lval_ctx) { // it's NOT a method call `t.tupleSize()` (since such cases are handled by process_function_call) // it's `t.0`, `getUser().id`, and `t.tupleSize` (as a reference, not as a call) - // currently, nothing except a global function can be a target of dot access - const FunctionData* fun_ref = v->target; + if (!v->is_target_fun_ref()) { + TypePtr obj_type = v->get_obj()->inferred_type; + int index_at = std::get(v->target); + // `tensorVar.0`; since a tensor of N elems are N vars on a stack actually, calculate offset + if (const auto* t_tensor = obj_type->try_as()) { + if (lval_ctx) lval_ctx->register_lval(v->loc, v); + if (lval_ctx) lval_ctx->enter_rval_inside_lval(); + std::vector lhs_vars = pre_compile_expr(v->get_obj(), code, lval_ctx); + if (lval_ctx) lval_ctx->exit_rval_inside_lval(); + int stack_width = t_tensor->items[index_at]->calc_width_on_stack(); + int stack_offset = 0; + for (int i = 0; i < index_at; ++i) { + stack_offset += t_tensor->items[i]->calc_width_on_stack(); + } + return {lhs_vars.begin() + stack_offset, lhs_vars.begin() + stack_offset + stack_width}; + } + // `tupleVar.0`; not to mess up, separate rvalue and lvalue cases + if (obj_type->try_as() || obj_type->try_as()) { + if (!lval_ctx) { + // `tupleVar.0` as rvalue: the same as "tupleAt(tupleVar, 0)" written in terms of IR vars + std::vector tuple_ir_idx = pre_compile_expr(v->get_obj(), code); + std::vector index_ir_idx = code.create_tmp_var(TypeDataInt::create(), v->get_identifier()->loc, "(tuple-idx)"); + code.emplace_back(v->loc, Op::_IntConst, index_ir_idx, td::make_refint(index_at)); + std::vector field_ir_idx = code.create_tmp_var(v->inferred_type, v->loc, "(tuple-field)"); + tolk_assert(tuple_ir_idx.size() == 1 && field_ir_idx.size() == 1); // tuples contain only 1-slot values + const FunctionData* builtin_sym = lookup_global_symbol("tupleAt")->as(); + code.emplace_back(v->loc, Op::_Call, field_ir_idx, std::vector{tuple_ir_idx[0], index_ir_idx[0]}, builtin_sym); + return field_ir_idx; + } else { + // `tupleVar.0 = rhs`: finally "tupleSetAt(tupleVar, rhs, 0)" will be done + uint64_t hash = lval_ctx->register_lval(v->loc, v); + if (const var_idx_t* field_ir_idx = lval_ctx->exists_already_known_tuple_index(hash)) { + return {*field_ir_idx}; // `(t.0.0, t.0.1) = rhs`, then "t.0" will be read (tupleAt) once + } + lval_ctx->enter_rval_inside_lval(); + std::vector tuple_ir_idx = pre_compile_expr(v->get_obj(), code, lval_ctx); + lval_ctx->exit_rval_inside_lval(); + std::vector index_ir_idx = code.create_tmp_var(TypeDataInt::create(), v->get_identifier()->loc, "(tuple-idx)"); + code.emplace_back(v->loc, Op::_IntConst, index_ir_idx, td::make_refint(index_at)); + std::vector field_ir_idx = code.create_tmp_var(v->inferred_type, v->loc, "(tuple-field)"); + if (lval_ctx->is_rval_inside_lval()) { // for `t.0.1 = rhs` "t.0" is rvalue inside lvalue + // for `t.0 = rhs` don't call tupleAt, but for `t.0.1 = rhs` do for t.0 (still don't for t.0.1) + const FunctionData* builtin_sym = lookup_global_symbol("tupleAt")->as(); + code.emplace_back(v->loc, Op::_Call, field_ir_idx, std::vector{tuple_ir_idx[0], index_ir_idx[0]}, builtin_sym); + } + lval_ctx->register_modified_tuple_index(hash, tuple_ir_idx[0], index_ir_idx[0], field_ir_idx[0]); + vars_modification_watcher.trigger_callbacks(tuple_ir_idx, v->loc); + return field_ir_idx; + } + } + tolk_assert(false); + } + + // okay, v->target refs a function, like `obj.method`, filled at type inferring + // (currently, nothing except a global function can be referenced, no object-scope methods exist) + const FunctionData* fun_ref = std::get(v->target); tolk_assert(fun_ref); - return process_symbol(v->loc, fun_ref, code, lval_globs); + return pre_compile_symbol(v->loc, fun_ref, code, lval_ctx); } static std::vector process_function_call(V v, CodeBlob& code) { @@ -304,7 +562,7 @@ static std::vector process_function_call(V v, Code std::vector tfunc = pre_compile_expr(v->get_callee(), code); tolk_assert(tfunc.size() == 1); args_vars.push_back(tfunc[0]); - std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc); + std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc, "(call-ind)"); Op& op = code.emplace_back(v->loc, Op::_CallInd, rvect, std::move(args_vars)); op.set_impure_flag(); return rvect; @@ -349,28 +607,28 @@ static std::vector process_function_call(V v, Code for (const std::vector& list : vars_per_arg) { args_vars.insert(args_vars.end(), list.cbegin(), list.cend()); } - std::vector rvect_apply = gen_op_call(code, op_call_type, v->loc, std::move(args_vars), fun_ref); + std::vector rvect_apply = gen_op_call(code, op_call_type, v->loc, std::move(args_vars), fun_ref, "(fun-call)"); if (fun_ref->has_mutate_params()) { - LValGlobs local_globs; + LValContext local_lval; std::vector left; for (int i = 0; i < delta_self + v->get_num_args(); ++i) { if (fun_ref->parameters[i].is_mutate_parameter()) { AnyExprV arg_i = obj_leftmost && i == 0 ? obj_leftmost : args[i]; tolk_assert(arg_i->is_lvalue || i == 0); if (arg_i->is_lvalue) { - std::vector ith_var_idx = pre_compile_expr(arg_i, code, &local_globs); + std::vector ith_var_idx = pre_compile_expr(arg_i, code, &local_lval); left.insert(left.end(), ith_var_idx.begin(), ith_var_idx.end()); } else { left.insert(left.end(), vars_per_arg[0].begin(), vars_per_arg[0].end()); } } } - std::vector rvect = code.create_tmp_var(real_ret_type, v->loc); + std::vector rvect = code.create_tmp_var(real_ret_type, v->loc, "(fun-call)"); left.insert(left.end(), rvect.begin(), rvect.end()); - code.on_var_modification(left, v->loc); + vars_modification_watcher.trigger_callbacks(left, v->loc); code.emplace_back(v->loc, Op::_Let, std::move(left), rvect_apply); - local_globs.gen_ops_set_globs(code, v->loc); + local_lval.gen_ops_if_nonempty(code, v->loc); rvect_apply = rvect; } @@ -385,29 +643,29 @@ static std::vector process_function_call(V v, Code return rvect_apply; } -static std::vector process_tensor(V v, CodeBlob& code, LValGlobs* lval_globs) { - return pre_compile_tensor(code, v->get_items(), lval_globs); +static std::vector process_tensor(V v, CodeBlob& code, LValContext* lval_ctx) { + return pre_compile_tensor(code, v->get_items(), lval_ctx); } -static std::vector process_typed_tuple(V v, CodeBlob& code, LValGlobs* lval_globs) { - if (lval_globs) { // todo some time, make "var (a, [b,c]) = (1, [2,3])" work +static std::vector process_typed_tuple(V v, CodeBlob& code, LValContext* lval_ctx) { + if (lval_ctx) { // todo some time, make "var (a, [b,c]) = (1, [2,3])" work v->error("[...] can not be used as lvalue here"); } - std::vector left = code.create_tmp_var(v->inferred_type, v->loc); - std::vector right = pre_compile_tensor(code, v->get_items()); + std::vector left = code.create_tmp_var(v->inferred_type, v->loc, "(pack-tuple)"); + std::vector right = pre_compile_tensor(code, v->get_items(), lval_ctx); code.emplace_back(v->loc, Op::_Tuple, left, std::move(right)); return left; } static std::vector process_int_const(V v, CodeBlob& code) { - std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc); + std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc, "(int-const)"); code.emplace_back(v->loc, Op::_IntConst, rvect, v->intval); return rvect; } static std::vector process_string_const(V v, CodeBlob& code) { ConstantValue value = eval_const_init_value(v); - std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc); + std::vector rvect = code.create_tmp_var(v->inferred_type, v->loc, "(str-const)"); if (value.is_int()) { code.emplace_back(v->loc, Op::_IntConst, rvect, value.as_int()); } else { @@ -418,21 +676,21 @@ static std::vector process_string_const(V v, CodeBl static std::vector process_bool_const(V v, CodeBlob& code) { const FunctionData* builtin_sym = lookup_global_symbol(v->bool_val ? "__true" : "__false")->as(); - return gen_op_call(code, v->inferred_type, v->loc, {}, builtin_sym); + return gen_op_call(code, v->inferred_type, v->loc, {}, builtin_sym, "(bool-const)"); } static std::vector process_null_keyword(V v, CodeBlob& code) { const FunctionData* builtin_sym = lookup_global_symbol("__null")->as(); - return gen_op_call(code, v->inferred_type, v->loc, {}, builtin_sym); + return gen_op_call(code, v->inferred_type, v->loc, {}, builtin_sym, "(null-literal)"); } static std::vector process_local_var(V v, CodeBlob& code) { if (v->marked_as_redef) { - return process_symbol(v->loc, v->var_ref, code, nullptr); + return pre_compile_symbol(v->loc, v->var_ref, code, nullptr); } tolk_assert(v->var_ref->ir_idx.empty()); - v->var_ref->mutate()->assign_ir_idx(code.create_var(v->inferred_type, v->var_ref, v->loc)); + v->var_ref->mutate()->assign_ir_idx(code.create_var(v->inferred_type, v->loc, v->var_ref->name)); return v->var_ref->ir_idx; } @@ -444,13 +702,13 @@ static std::vector process_local_vars_declaration(V process_underscore(V v, CodeBlob& code) { // when _ is used as left side of assignment, like `(cs, _) = cs.loadAndReturn()` - return code.create_tmp_var(v->inferred_type, v->loc); + return code.create_tmp_var(v->inferred_type, v->loc, "(underscore)"); } -std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, LValGlobs* lval_globs) { +std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, LValContext* lval_ctx) { switch (v->type) { case ast_reference: - return process_symbol(v->loc, v->as()->sym, code, lval_globs); + return pre_compile_symbol(v->loc, v->as()->sym, code, lval_ctx); case ast_assign: return process_assign(v->as(), code); case ast_set_assign: @@ -462,17 +720,17 @@ std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, LValGlobs* l case ast_ternary_operator: return process_ternary_operator(v->as(), code); case ast_cast_as_operator: - return pre_compile_expr(v->as()->get_expr(), code, lval_globs); + return pre_compile_expr(v->as()->get_expr(), code, lval_ctx); case ast_dot_access: - return process_dot_access(v->as(), code, lval_globs); + return process_dot_access(v->as(), code, lval_ctx); case ast_function_call: return process_function_call(v->as(), code); case ast_parenthesized_expression: - return pre_compile_expr(v->as()->get_expr(), code, lval_globs); + return pre_compile_expr(v->as()->get_expr(), code, lval_ctx); case ast_tensor: - return process_tensor(v->as(), code, lval_globs); + return process_tensor(v->as(), code, lval_ctx); case ast_typed_tuple: - return process_typed_tuple(v->as(), code, lval_globs); + return process_typed_tuple(v->as(), code, lval_ctx); case ast_int_const: return process_int_const(v->as(), code); case ast_string_const: @@ -515,14 +773,14 @@ static void process_assert_statement(V v, CodeBlob& code) const FunctionData* builtin_sym = lookup_global_symbol("__throw_if_unless")->as(); std::vector args_vars = pre_compile_tensor(code, args); - gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym); + gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym, "(throw-call)"); } static void process_catch_variable(AnyExprV v_catch_var, CodeBlob& code) { if (auto v_ref = v_catch_var->try_as(); v_ref && v_ref->sym) { // not underscore const LocalVarData* var_ref = v_ref->sym->as(); tolk_assert(var_ref->ir_idx.empty()); - var_ref->mutate()->assign_ir_idx(code.create_var(v_catch_var->inferred_type, var_ref, v_catch_var->loc)); + var_ref->mutate()->assign_ir_idx(code.create_var(v_catch_var->inferred_type, v_catch_var->loc, var_ref->name)); } } @@ -621,11 +879,11 @@ static void process_throw_statement(V v, CodeBlob& code) { if (v->has_thrown_arg()) { const FunctionData* builtin_sym = lookup_global_symbol("__throw_arg")->as(); std::vector args_vars = pre_compile_tensor(code, {v->get_thrown_arg(), v->get_thrown_code()}); - gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym); + gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym, "(throw-call)"); } else { const FunctionData* builtin_sym = lookup_global_symbol("__throw")->as(); std::vector args_vars = pre_compile_tensor(code, {v->get_thrown_code()}); - gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym); + gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym, "(throw-call)"); } } @@ -699,7 +957,7 @@ static void convert_function_body_to_CodeBlob(const FunctionData* fun_ref, Funct for (int i = 0; i < fun_ref->get_num_params(); ++i) { const LocalVarData& param_i = fun_ref->parameters[i]; - std::vector ir_idx = blob->create_var(param_i.declared_type, ¶m_i, param_i.loc); + std::vector ir_idx = blob->create_var(param_i.declared_type, param_i.loc, param_i.name); rvect_import.insert(rvect_import.end(), ir_idx.begin(), ir_idx.end()); param_i.mutate()->assign_ir_idx(std::move(ir_idx)); } @@ -716,6 +974,7 @@ static void convert_function_body_to_CodeBlob(const FunctionData* fun_ref, Funct blob->close_blk(v_body->loc_end); code_body->set_code(blob); + tolk_assert(vars_modification_watcher.empty()); } static void convert_asm_body_to_AsmOp(const FunctionData* fun_ref, FunctionBodyAsm* asm_body) { diff --git a/tolk/pipe-check-rvalue-lvalue.cpp b/tolk/pipe-check-rvalue-lvalue.cpp index 943dfb96..a824cc5d 100644 --- a/tolk/pipe-check-rvalue-lvalue.cpp +++ b/tolk/pipe-check-rvalue-lvalue.cpp @@ -123,8 +123,8 @@ class CheckRValueLvalueVisitor final : public ASTVisitorFunctionBody { void visit(V v) override { // a reference to a method used as rvalue, like `var v = t.tupleAt` - if (const FunctionData* fun_ref = v->target; v->is_rvalue) { - validate_function_used_as_noncall(v, fun_ref); + if (v->is_rvalue && v->is_target_fun_ref()) { + validate_function_used_as_noncall(v, std::get(v->target)); } } diff --git a/tolk/pipe-infer-types-and-calls.cpp b/tolk/pipe-infer-types-and-calls.cpp index 011c83d7..ba5f77a7 100644 --- a/tolk/pipe-infer-types-and-calls.cpp +++ b/tolk/pipe-infer-types-and-calls.cpp @@ -124,6 +124,19 @@ static void fire_error_cannot_apply_operator(SrcLocation loc, std::string_view o throw ParseError(loc, "can not apply operator `" + op + "` to " + to_string(lhs->inferred_type) + " and " + to_string(rhs->inferred_type)); } +// fire an error on `untypedTupleVar.0` when used without a hint +GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD +static void fire_error_cannot_deduce_untyped_tuple_access(SrcLocation loc, int index) { + std::string idx_access = "." + std::to_string(index); + throw ParseError(loc, "can not deduce type of `" + idx_access + "`; either assign it to variable like `var c: int = " + idx_access + "` or cast the result like `" + idx_access + " as int`"); +} + +// fire an error on `untypedTupleVar.0` when inferred as (int,int), or `[int, (int,int)]`, or other non-1 width in a tuple +GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD +static void fire_error_cannot_put_non1_stack_width_arg_to_tuple(SrcLocation loc, TypePtr inferred_type) { + throw ParseError(loc, "can not put " + to_string(inferred_type) + " into a tuple, because it occupies " + std::to_string(inferred_type->calc_width_on_stack()) + " stack slots in TVM, not 1"); +} + // check correctness of called arguments counts and their type matching static void check_function_arguments(const FunctionData* fun_ref, V v, AnyExprV lhs_of_dot_call) { int delta_self = lhs_of_dot_call ? 1 : 0; @@ -466,6 +479,22 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { return TypeDataTypedTuple::create(std::move(sub_hints)); } + // `a.0 = rhs` / `b.1.0 = rhs` (remember, its target is not assigned yet) + if (auto lhs_dot = lhs->try_as()) { + TypePtr obj_hint = calc_hint_from_assignment_lhs(lhs_dot->get_obj()); + std::string_view field_name = lhs_dot->get_field_name(); + if (field_name[0] >= '0' && field_name[0] <= '9') { + int index_at = std::stoi(std::string(field_name)); + if (const auto* t_tensor = obj_hint->try_as(); t_tensor && index_at < t_tensor->size()) { + return t_tensor->items[index_at]; + } + if (const auto* t_tuple = obj_hint->try_as(); t_tuple && index_at < t_tuple->size()) { + return t_tuple->items[index_at]; + } + } + return TypeDataUnknown::create(); + } + return TypeDataUnknown::create(); } @@ -562,8 +591,8 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { return; } - // here is something strange and unhandled, like `f() = rhs` - // it will fail on later compilation steps (like rvalue/lvalue checks), but type inferring should pass + // here is something unhandled like `a.0 = rhs`, run regular inferring on rhs + // for something strange like `f() = rhs` type inferring will pass, but will fail later infer_any_expr(lhs, rhs_type); if (!lhs->inferred_type->can_rhs_be_assigned(rhs_type)) { err_loc->error("can not assign " + to_string(rhs_type) + " to " + to_string(lhs)); @@ -839,25 +868,56 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { // it's NOT a method call `t.tupleSize()` (since such cases are handled by infer_function_call) // it's `t.0`, `getUser().id`, and `t.tupleSize` (as a reference, not as a call) infer_any_expr(v->get_obj()); + TypePtr obj_type = v->get_obj()->inferred_type; // our goal is to fill v->target knowing type of obj V v_ident = v->get_identifier(); // field/method name vertex V v_instantiationTs = v->get_instantiationTs(); std::string_view field_name = v_ident->name; - // for now, Tolk doesn't have structures, properties, and object-scoped methods - // so, only `t.tupleSize` is allowed, look up a global function - const Symbol* sym = lookup_global_symbol(field_name); - if (!sym) { - v_ident->error("undefined symbol `" + static_cast(field_name) + "`"); + // it can be indexed access (`tensorVar.0`, `tupleVar.1`) or a method (`t.tupleSize`) + // at first, check for indexed access + if (field_name[0] >= '0' && field_name[0] <= '9') { + int index_at = std::stoi(std::string(field_name)); + if (const auto* t_tensor = obj_type->try_as()) { + if (index_at >= t_tensor->size()) { + v_ident->error("invalid tensor index, expected 0.." + std::to_string(t_tensor->items.size() - 1)); + } + v->mutate()->assign_target(index_at); + assign_inferred_type(v, t_tensor->items[index_at]); + return; + } + if (const auto* t_tuple = obj_type->try_as()) { + if (index_at >= t_tuple->size()) { + v_ident->error("invalid tuple index, expected 0.." + std::to_string(t_tuple->items.size() - 1)); + } + v->mutate()->assign_target(index_at); + assign_inferred_type(v, t_tuple->items[index_at]); + return; + } + if (obj_type->try_as()) { + if (hint == nullptr) { + fire_error_cannot_deduce_untyped_tuple_access(v->loc, index_at); + } + if (hint->calc_width_on_stack() != 1) { + fire_error_cannot_put_non1_stack_width_arg_to_tuple(v->loc, hint); + } + v->mutate()->assign_target(index_at); + assign_inferred_type(v, hint); + return; + } + v_ident->error("type " + to_string(obj_type) + " is not indexable"); } - const FunctionData* fun_ref = sym->try_as(); + + // for now, Tolk doesn't have fields and object-scoped methods; `t.tupleSize` is a global function `tupleSize` + const Symbol* sym = lookup_global_symbol(field_name); + const FunctionData* fun_ref = sym ? sym->try_as() : nullptr; if (!fun_ref) { - v_ident->error("referencing a non-function"); + v_ident->error("non-existing field `" + static_cast(field_name) + "` of type " + to_string(obj_type)); } // `t.tupleSize` is ok, `cs.tupleSize` not - if (!fun_ref->parameters[0].declared_type->can_rhs_be_assigned(v->get_obj()->inferred_type)) { - v_ident->error("referencing a method for " + to_string(fun_ref->parameters[0]) + " with an object of type " + to_string(v->get_obj())); + if (!fun_ref->parameters[0].declared_type->can_rhs_be_assigned(obj_type)) { + v_ident->error("referencing a method for " + to_string(fun_ref->parameters[0]) + " with object of type " + to_string(obj_type)); } if (fun_ref->is_generic_function() && !v_instantiationTs) { @@ -896,21 +956,24 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { } else if (auto v_dot = callee->try_as()) { // `obj.someMethod()` / `obj.someMethod()` / `getF().someMethod()` / `obj.SOME_CONST()` + // note, that dot_obj->target is not filled yet, since callee was not inferred yet delta_self = 1; dot_obj = v_dot->get_obj(); v_instantiationTs = v_dot->get_instantiationTs(); // present for `obj.someMethod()` infer_any_expr(dot_obj); - // for now, Tolk doesn't have object-scoped methods, so method resolving doesn't depend on obj type - // (in other words, `globalFunction(a)` = `a.globalFunction()`) - std::string_view method_name = v_dot->get_field_name(); - const Symbol* sym = lookup_global_symbol(method_name); - if (!sym) { - v_dot->get_identifier()->error("undefined symbol `" + static_cast(method_name) + "`"); - } - fun_ref = sym->try_as(); - if (!fun_ref) { - v_dot->get_identifier()->error("calling a non-function"); + // it can be indexed access (`tensorVar.0()`, `tupleVar.1()`) or a method (`t.tupleSize()`) + std::string_view field_name = v_dot->get_field_name(); + if (field_name[0] >= '0' && field_name[0] <= '9') { + // indexed access `ab.2()`, then treat `ab.2` just like an expression, fun_ref remains nullptr + // infer_dot_access() will be called for a callee, it will check type, index correctness, etc. + } else { + // for now, Tolk doesn't have fields and object-scoped methods; `t.tupleSize` is a global function `tupleSize` + const Symbol* sym = lookup_global_symbol(field_name); + fun_ref = sym ? sym->try_as() : nullptr; + if (!fun_ref) { + v_dot->get_identifier()->error("non-existing method `" + static_cast(field_name) + "` of type " + to_string(dot_obj)); + } } } else { @@ -926,7 +989,7 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { assign_inferred_type(arg_i, arg_i->get_expr()); } - // handle `local_var()` / `getF()()` / `5()` / `SOME_CONST()` / `obj.method()()()` + // handle `local_var()` / `getF()()` / `5()` / `SOME_CONST()` / `obj.method()()()` / `tensorVar.0()` if (!fun_ref) { // treat callee like a usual expression, which must have "callable" inferred type infer_any_expr(callee); @@ -1017,6 +1080,9 @@ class InferCheckTypesAndCallsAndFieldsVisitor final { for (int i = 0; i < v->size(); ++i) { AnyExprV item = v->get_item(i); infer_any_expr(item, tuple_hint && i < tuple_hint->size() ? tuple_hint->items[i] : nullptr); + if (item->inferred_type->calc_width_on_stack() != 1) { + fire_error_cannot_put_non1_stack_width_arg_to_tuple(v->get_item(i)->loc, item->inferred_type); + } types_list.emplace_back(item->inferred_type); } assign_inferred_type(v, TypeDataTypedTuple::create(std::move(types_list))); diff --git a/tolk/tolk.h b/tolk/tolk.h index 7b44931e..4086d7f7 100644 --- a/tolk/tolk.h +++ b/tolk/tolk.h @@ -44,21 +44,23 @@ typedef int var_idx_t; typedef int const_idx_t; struct TmpVar { - TypePtr v_type; - var_idx_t ir_idx; - const LocalVarData* v_sym; // points to var defined in code; nullptr for implicitly created tmp vars - SrcLocation where; - std::vector> on_modification; + var_idx_t ir_idx; // every var in IR represents 1 stack slot + TypePtr v_type; // calc_width_on_stack() is 1 + std::string name; // "x" for vars originated from user sources; "x.0" for tensor components; empty for implicitly created tmp vars + SrcLocation loc; // location of var declaration in sources or where a tmp var was originated +#ifdef TOLK_DEBUG + const char* desc = nullptr; // "origin" of tmp var, for debug output like `'15 (binary-op) '16 (glob-var)` +#endif - TmpVar(var_idx_t ir_idx, TypePtr type, const LocalVarData* v_sym, SrcLocation loc) - : v_type(type) - , ir_idx(ir_idx) - , v_sym(v_sym) - , where(loc) { + TmpVar(var_idx_t ir_idx, TypePtr v_type, std::string name, SrcLocation loc) + : ir_idx(ir_idx) + , v_type(v_type) + , name(std::move(name)) + , loc(loc) { } - void show(std::ostream& os, int omit_idx = 0) const; - void dump(std::ostream& os) const; + void show_as_stack_comment(std::ostream& os) const; + void show(std::ostream& os) const; }; struct VarDescr { @@ -602,7 +604,6 @@ struct AsmOpList { } const_idx_t register_const(Const new_const); Const get_const(const_idx_t idx); - void show_var(std::ostream& os, var_idx_t idx) const; void show_var_ext(std::ostream& os, std::pair idx_pair) const; void adjust_last() { if (list_.back().is_nop()) { @@ -1018,13 +1019,10 @@ struct Stack { void rearrange_top(var_idx_t top, bool last); void merge_const(const Stack& req_stack); void merge_state(const Stack& req_stack); - void show(int _mode); - void show() { - show(mode); - } + void show(); void opt_show() { if ((mode & (_StkCmt | _Shown)) == _StkCmt) { - show(mode); + show(); } } bool operator==(const Stack& y) const & { @@ -1108,9 +1106,15 @@ struct CodeBlob { #endif return res; } - std::vector create_var(TypePtr var_type, const LocalVarData* v_sym, SrcLocation loc); - std::vector create_tmp_var(TypePtr var_type, SrcLocation loc) { - return create_var(var_type, nullptr, loc); + std::vector create_var(TypePtr var_type, SrcLocation loc, std::string name); + std::vector create_tmp_var(TypePtr var_type, SrcLocation loc, const char* desc) { + std::vector ir_idx = create_var(var_type, loc, {}); +#ifdef TOLK_DEBUG + for (var_idx_t v : ir_idx) { + vars[v].desc = desc; + } +#endif + return ir_idx; } bool compute_used_code_vars(); bool compute_used_code_vars(std::unique_ptr& ops, const VarDescrList& var_info, bool edit) const; @@ -1135,14 +1139,6 @@ struct CodeBlob { void mark_noreturn(); void generate_code(AsmOpList& out_list, int mode = 0); void generate_code(std::ostream& os, int mode = 0, int indent = 0); - - void on_var_modification(const std::vector& left_lval_indices, SrcLocation here) const { - for (var_idx_t ir_idx : left_lval_indices) { - for (auto& f : vars.at(ir_idx).on_modification) { - f(here); - } - } - } }; // defined in builtins.cpp