mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals (previously forked from FunC). The goal is to convert AST directly to Op (a kind of IR representation), doing all code analysis at AST level. Noteable changes: - AST-based semantic kernel includes: registering global symbols, scope handling and resolving local/global identifiers, lvalue/rvalue calc and check, implicit return detection, mutability analysis, pure/impure validity checks, simple constant folding - values of `const` variables are calculated NOT based on CodeBlob, but via a newly-introduced AST-based constant evaluator - AST vertices are now inherited from expression/statement/other; expression vertices have common properties (TypeExpr, lvalue/rvalue) - symbol table is rewritten completely, SymDef/SymVal no longer exist, lexer now doesn't need to register identifiers - AST vertices have references to symbols, filled at different stages of pipeline - the remaining "FunC legacy part" is almost unchanged besides Expr which was fully dropped; AST is converted to Ops (IR) directly
This commit is contained in:
parent
ea0dc16163
commit
3540424aa1
71 changed files with 4270 additions and 3060 deletions
|
@ -2,85 +2,112 @@ fun unsafe_tuple<X>(x: X): tuple
|
|||
asm "NOP";
|
||||
|
||||
fun inc(x: int, y: int): (int, int) {
|
||||
return (x + y, y * 10);
|
||||
return (x + y, y * 10);
|
||||
}
|
||||
fun `~inc`(mutate self: int, y: int): int {
|
||||
val (newX, newY) = inc(self, y);
|
||||
self = newX;
|
||||
return newY;
|
||||
val (newX, newY) = inc(self, y);
|
||||
self = newX;
|
||||
return newY;
|
||||
}
|
||||
|
||||
fun eq<X>(v: X): X { return v; }
|
||||
fun eq2(v: (int, int)) { return v; }
|
||||
fun mul2(mutate dest: int, v: int): int { dest = v*2; return dest; }
|
||||
fun multens(mutate self: (int, int), v: (int, int)): (int, int) { var (f, s) = self; var (m1, m2) = v; self = (f*m1, s*m2); return self; }
|
||||
|
||||
@method_id(11)
|
||||
fun test_return(x: int): (int, int, int, int, int, int, int) {
|
||||
return (x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
return (x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
}
|
||||
|
||||
@method_id(12)
|
||||
fun test_assign(x: int): (int, int, int, int, int, int, int) {
|
||||
var (x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int) = (x, x.`~inc`(x / 20), x, x=x*2, x, x+=1, x);
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
var (x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int) = (x, x.`~inc`(x / 20), x, x=x*2, x, x+=1, x);
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
}
|
||||
|
||||
@method_id(13)
|
||||
fun test_tuple(x: int): tuple {
|
||||
var t: tuple = unsafe_tuple([x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x]);
|
||||
return t;
|
||||
var t: tuple = unsafe_tuple([x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x]);
|
||||
return t;
|
||||
}
|
||||
|
||||
@method_id(14)
|
||||
fun test_tuple_assign(x: int): (int, int, int, int, int, int, int) {
|
||||
var [x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int] = [x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x];
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
var [x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int] = [x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x];
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
}
|
||||
|
||||
fun foo1(x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int): (int, int, int, int, int, int, int) {
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
}
|
||||
|
||||
@method_id(15)
|
||||
fun test_call_1(x: int): (int, int, int, int, int, int, int) {
|
||||
return foo1(x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
return foo1(x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
}
|
||||
|
||||
fun foo2(x1: int, x2: int, x3456: (int, int, int, int), x7: int): (int, int, int, int, int, int, int) {
|
||||
var (x3: int, x4: int, x5: int, x6: int) = x3456;
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
var (x3: int, x4: int, x5: int, x6: int) = x3456;
|
||||
return (x1, x2, x3, x4, x5, x6, x7);
|
||||
}
|
||||
|
||||
@method_id(16)
|
||||
fun test_call_2(x: int): (int, int, int, int, int, int, int) {
|
||||
return foo2(x, x.`~inc`(x / 20), (x, x = x * 2, x, x += 1), x);
|
||||
return foo2(x, x.`~inc`(x / 20), (x, x = x * 2, x, x += 1), x);
|
||||
}
|
||||
|
||||
fun asm_func(x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int): (int, int, int, int, int, int, int)
|
||||
asm
|
||||
(x4 x5 x6 x7 x1 x2 x3->0 1 2 3 4 5 6) "NOP";
|
||||
asm (x4 x5 x6 x7 x1 x2 x3->0 1 2 3 4 5 6) "NOP";
|
||||
|
||||
@method_id(17)
|
||||
fun test_call_asm_old(x: int): (int, int, int, int, int, int, int) {
|
||||
return asm_func(x, x += 1, x, x, x.`~inc`(x / 20), x, x = x * 2);
|
||||
return asm_func(x, x += 1, x, x, x.`~inc`(x / 20), x, x = x * 2);
|
||||
}
|
||||
|
||||
@method_id(18)
|
||||
fun test_call_asm_new(x: int): (int, int, int, int, int, int, int) {
|
||||
return asm_func(x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
return asm_func(x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
|
||||
}
|
||||
|
||||
global xx: int;
|
||||
@method_id(19)
|
||||
fun test_global(x: int): (int, int, int, int, int, int, int) {
|
||||
xx = x;
|
||||
return (xx, xx.`~inc`(xx / 20), xx, xx = xx * 2, xx, xx += 1, xx);
|
||||
fun test_global(x: int) {
|
||||
xx = x;
|
||||
return (x, xx, xx.`~inc`(xx / 20), eq(xx += (x *= 0)), xx = xx * 2, xx, xx += 1, xx, x);
|
||||
}
|
||||
|
||||
@method_id(20)
|
||||
fun test_if_else(x: int): (int, int, int, int, int) {
|
||||
if (x > 10) {
|
||||
return (x.`~inc`(8), x + 1, x = 1, x <<= 3, x);
|
||||
} else {
|
||||
xx = 9;
|
||||
return (x, x.`~inc`(-4), x.`~inc`(-1), x >= 1, x = x + xx);
|
||||
}
|
||||
if (x > 10) {
|
||||
return (x.`~inc`(8), x + 1, x = 1, x <<= 3, x);
|
||||
} else {
|
||||
xx = 9;
|
||||
return (x, x.`~inc`(-4), x.`~inc`(-1), x >= 1, x = x + xx);
|
||||
}
|
||||
}
|
||||
|
||||
@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))));
|
||||
}
|
||||
|
||||
@method_id(22)
|
||||
fun test_assign_with_mutate(x: int) {
|
||||
return (x, mul2(mutate x, x += 5), x.`~inc`(mul2(mutate x, x)), x);
|
||||
}
|
||||
|
||||
@method_id(23)
|
||||
fun test_assign_tensor(x: (int, int)) {
|
||||
var fs = (0, 0);
|
||||
return (x, x = (20, 30), fs = x.multens((1, 2)), fs.multens(multens(mutate x, (-1, -1))), x, fs);
|
||||
}
|
||||
|
||||
global fs: (int, int);
|
||||
@method_id(24)
|
||||
fun test_assign_tensor_global(x: (int, int)) {
|
||||
fs = (0, 0);
|
||||
return (x, x = (20, 30), fs = x.multens((1, 2)), fs.multens(multens(mutate x, (-1, -1))), x, fs);
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
@ -96,9 +123,13 @@ fun main() {
|
|||
@testcase | 16 | 100 | 100 50 105 210 210 211 211
|
||||
@testcase | 17 | 100 | 101 50 106 212 100 101 101
|
||||
@testcase | 18 | 100 | 210 210 211 211 100 50 105
|
||||
@testcase | 19 | 100 | 100 50 105 210 210 211 211
|
||||
@testcase | 19 | 100 | 100 100 50 105 210 210 211 211 0
|
||||
@testcase | 20 | 80 | 80 89 1 8 8
|
||||
@testcase | 20 | 9 | 9 -40 -10 -1 13
|
||||
@testcase | 21 | 100 | 100 110 [ 110 130 80 80 ] 80 3200
|
||||
@testcase | 22 | 100 | 100 210 4200 630
|
||||
@testcase | 23 | 1 1 | 1 1 20 30 20 60 -400 -3600 -20 -60 -400 -3600
|
||||
@testcase | 24 | 1 1 | 1 1 20 30 20 60 -400 -3600 -20 -60 -400 -3600
|
||||
|
||||
@fif_codegen
|
||||
"""
|
||||
|
@ -107,5 +138,5 @@ fun main() {
|
|||
inc CALLDICT // self newY
|
||||
}>
|
||||
"""
|
||||
@code_hash 97139400653362069936987769894397430077752335662822462908581556703209313861576
|
||||
@code_hash 33262590582878205026101577472505372101182291690814957175155528952950621243206
|
||||
*/
|
||||
|
|
|
@ -216,15 +216,15 @@ Note, that since 'compute-asm-ltr' became on be default, chaining methods codege
|
|||
"""
|
||||
test6 PROC:<{
|
||||
//
|
||||
NEWC // _1
|
||||
1 PUSHINT // _1 _2=1
|
||||
SWAP // _2=1 _1
|
||||
NEWC // _0
|
||||
1 PUSHINT // _0 _1=1
|
||||
SWAP // _1=1 _0
|
||||
32 STU // _0
|
||||
2 PUSHINT // _0 _6=2
|
||||
SWAP // _6=2 _0
|
||||
2 PUSHINT // _0 _5=2
|
||||
SWAP // _5=2 _0
|
||||
32 STU // _0
|
||||
3 PUSHINT // _0 _10=3
|
||||
SWAP // _10=3 _0
|
||||
3 PUSHINT // _0 _9=3
|
||||
SWAP // _9=3 _0
|
||||
32 STU // _0
|
||||
}>
|
||||
"""
|
||||
|
|
|
@ -35,7 +35,7 @@ Below, I just give examples of @fif_codegen tag:
|
|||
"""
|
||||
main PROC:<{
|
||||
// s
|
||||
17 PUSHINT // s _3=17
|
||||
17 PUSHINT // s _1=17
|
||||
OVER // s z=17 t
|
||||
WHILE:<{
|
||||
...
|
||||
|
|
|
@ -5,5 +5,5 @@ fun main() {
|
|||
/**
|
||||
@compilation_should_fail
|
||||
The message is weird now, but later I'll rework error messages anyway.
|
||||
@stderr cannot apply expression of type int to an expression of type (): cannot unify type () -> ??3 with int
|
||||
@stderr cannot apply expression of type int to an expression of type (): cannot unify type () -> ??2 with int
|
||||
*/
|
||||
|
|
|
@ -8,6 +8,6 @@ fun main() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr rvalue expected
|
||||
@stderr `_` can't be used as a value; it's a placeholder for a left side of assignment
|
||||
@stderr inc(_)
|
||||
*/
|
||||
|
|
8
tolk-tester/tests/invalid-const-1.tolk
Normal file
8
tolk-tester/tests/invalid-const-1.tolk
Normal file
|
@ -0,0 +1,8 @@
|
|||
fun main() {
|
||||
return 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
|
||||
}
|
||||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr invalid integer constant
|
||||
*/
|
|
@ -7,5 +7,5 @@ fun cantAssignToVal() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `x`
|
||||
@stderr modifying immutable variable `x`
|
||||
*/
|
||||
|
|
|
@ -4,5 +4,5 @@ fun load32(self: slice): int {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying `self` (call a mutating method), which is immutable by default
|
||||
@stderr modifying `self`, which is immutable by default
|
||||
*/
|
||||
|
|
|
@ -6,5 +6,5 @@ fun cantAssignToVal() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `x`
|
||||
@stderr modifying immutable variable `x`
|
||||
*/
|
||||
|
|
|
@ -7,5 +7,5 @@ fun cantAssignToConst() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `op_increase`
|
||||
@stderr modifying immutable constant
|
||||
*/
|
||||
|
|
|
@ -10,5 +10,5 @@ fun cantPassToMutatingFunction() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `myVal`
|
||||
@stderr modifying immutable variable `myVal`
|
||||
*/
|
||||
|
|
|
@ -9,6 +9,6 @@ fun cantCallMutatingMethod(c: cell) {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `s` (call a mutating method)
|
||||
@stderr modifying immutable variable `s`
|
||||
@stderr s.loadUint
|
||||
*/
|
||||
|
|
|
@ -11,6 +11,6 @@ fun cantCallMutatingFunctionWithImmutable() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `op_increase` (call a mutating function)
|
||||
@stderr modifying immutable constant
|
||||
@stderr inc(mutate op_increase)
|
||||
*/
|
||||
|
|
|
@ -10,6 +10,6 @@ fun cantCallMutatingFunctionWithRvalue() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr lvalue expected (call a mutating function)
|
||||
@stderr literal can not be used as lvalue
|
||||
@stderr incBoth(mutate x, mutate 30)
|
||||
*/
|
||||
|
|
|
@ -6,5 +6,5 @@ fun cantRedefImmutable() {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying an immutable variable `x` (left side of assignment)
|
||||
@stderr `redef` for immutable variable
|
||||
*/
|
||||
|
|
|
@ -4,6 +4,6 @@ fun increment(self: int) {
|
|||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr modifying `self` (left side of assignment), which is immutable by default
|
||||
@stderr modifying `self`, which is immutable by default
|
||||
@stderr probably, you want to declare `mutate self`
|
||||
*/
|
||||
|
|
|
@ -4,7 +4,7 @@ fun f_pure(): int {
|
|||
return f_impure();
|
||||
}
|
||||
|
||||
fun f_impure(): int {}
|
||||
fun f_impure(): int { return 0; }
|
||||
|
||||
fun main(): int {
|
||||
return f_pure();
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
fun validate_input(input: cell): (int, int) {
|
||||
var (x, y, z, correct) = calculateCellSize(input, 10);
|
||||
assert(correct) throw 102;
|
||||
return (x, y);
|
||||
}
|
||||
|
||||
@pure
|
||||
|
|
16
tolk-tester/tests/invalid-pure-4.tolk
Normal file
16
tolk-tester/tests/invalid-pure-4.tolk
Normal file
|
@ -0,0 +1,16 @@
|
|||
global set: int;
|
||||
|
||||
@pure
|
||||
fun someF(): int {
|
||||
var set redef = 0;
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr
|
||||
"""
|
||||
an impure operation in a pure function
|
||||
var set
|
||||
"""
|
||||
*/
|
10
tolk-tester/tests/invalid-redefinition-6.tolk
Normal file
10
tolk-tester/tests/invalid-redefinition-6.tolk
Normal file
|
@ -0,0 +1,10 @@
|
|||
const s1 = "asdf";
|
||||
|
||||
fun main() {
|
||||
var s1 redef = "d";
|
||||
}
|
||||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr `redef` for unknown variable
|
||||
*/
|
8
tolk-tester/tests/invalid-typing-6.tolk
Normal file
8
tolk-tester/tests/invalid-typing-6.tolk
Normal file
|
@ -0,0 +1,8 @@
|
|||
fun failWhenTernaryConditionNotInt(cs: slice) {
|
||||
return cs ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@compilation_should_fail
|
||||
@stderr condition of ternary ?: operator must be an integer
|
||||
*/
|
|
@ -118,12 +118,19 @@ fun updateTwoItems(mutate self: (int, int), byValue: int) {
|
|||
self = (first + byValue, second + byValue);
|
||||
}
|
||||
|
||||
global t107_1: int;
|
||||
global t107_2: int;
|
||||
|
||||
@method_id(107)
|
||||
fun testMutableTensor() {
|
||||
var t = (40, 50);
|
||||
t.updateTwoItems(10);
|
||||
updateTwoItems(mutate t, 10);
|
||||
return t;
|
||||
t107_1 = 1;
|
||||
t107_2 = 2;
|
||||
(t107_1, t107_2).updateTwoItems(10);
|
||||
updateTwoItems(mutate (t107_1, t107_2), 10);
|
||||
return (t, t107_1, t107_2);
|
||||
}
|
||||
|
||||
@pure
|
||||
|
@ -278,7 +285,7 @@ fun main(){}
|
|||
@testcase | 104 | | 1 2 3
|
||||
@testcase | 105 | | 5 5 110
|
||||
@testcase | 106 | | 160 110
|
||||
@testcase | 107 | | 60 70
|
||||
@testcase | 107 | | 60 70 21 22
|
||||
@testcase | 110 | | 320
|
||||
@testcase | 111 | | 55 55
|
||||
@testcase | 112 | | [ 1 13 3 23 33 ]
|
||||
|
@ -300,7 +307,7 @@ fun main(){}
|
|||
...
|
||||
incrementTwoInPlace CALLDICT // x y sum1
|
||||
-ROT
|
||||
10 PUSHINT // sum1 x y _9=10
|
||||
10 PUSHINT // sum1 x y _8=10
|
||||
incrementTwoInPlace CALLDICT // sum1 x y sum2
|
||||
s1 s3 s0 XCHG3 // x y sum1 sum2
|
||||
}>
|
||||
|
@ -310,8 +317,8 @@ fun main(){}
|
|||
"""
|
||||
load_next PROC:<{
|
||||
// cs
|
||||
32 LDI // _1 cs
|
||||
SWAP // cs _1
|
||||
32 LDI // _3 cs
|
||||
SWAP // cs _3
|
||||
}>
|
||||
"""
|
||||
|
||||
|
@ -319,7 +326,7 @@ fun main(){}
|
|||
"""
|
||||
testStoreUintPureUnusedResult PROC:<{
|
||||
//
|
||||
0 PUSHINT // _12=0
|
||||
0 PUSHINT // _11=0
|
||||
}>
|
||||
"""
|
||||
|
||||
|
@ -330,7 +337,7 @@ fun main(){}
|
|||
NEWC // b
|
||||
STIX // _2
|
||||
DROP //
|
||||
0 PUSHINT // _12=0
|
||||
0 PUSHINT // _11=0
|
||||
}>
|
||||
"""
|
||||
|
||||
|
|
|
@ -145,14 +145,14 @@ fun main() {
|
|||
"""
|
||||
test7 PROC:<{
|
||||
...
|
||||
LDOPTREF // b _20 _19
|
||||
LDOPTREF // b _18 _17
|
||||
DROP // b c
|
||||
ISNULL // b _13
|
||||
10 MULCONST // b _15
|
||||
SWAP // _15 b
|
||||
ISNULL // _15 _16
|
||||
0 EQINT // _15 _17
|
||||
ADD // _18
|
||||
ISNULL // b _11
|
||||
10 MULCONST // b _13
|
||||
SWAP // _13 b
|
||||
ISNULL // _13 _14
|
||||
0 EQINT // _13 _15
|
||||
ADD // _16
|
||||
}>
|
||||
"""
|
||||
*/
|
||||
|
|
|
@ -158,6 +158,44 @@ fun testNotMutatingChainableSelfMutateAnother(initial: int) {
|
|||
return (arg, c108, c109, x);
|
||||
}
|
||||
|
||||
fun pickG110(mutate self: int, mutate pushTo: tuple): self {
|
||||
self += 10;
|
||||
pushTo.tuplePush(c110);
|
||||
return self;
|
||||
}
|
||||
|
||||
global tup110: tuple;
|
||||
global c110: int;
|
||||
|
||||
@method_id(110)
|
||||
fun testMutateGlobalsLValue(init: int) {
|
||||
c110 = init;
|
||||
tup110 = createEmptyTuple();
|
||||
c110.incChained().incChained().pickG110(mutate tup110).incChained().pickG110(mutate tup110).incChained();
|
||||
return (c110, tup110);
|
||||
}
|
||||
|
||||
fun myTuplePush<T>(mutate self: tuple, value: T): self {
|
||||
self.tuplePush(value);
|
||||
return self;
|
||||
}
|
||||
|
||||
fun myTupleAt<T>(self: tuple, idx: int): T {
|
||||
return self.tupleAt(idx);
|
||||
}
|
||||
|
||||
global tup111: tuple;
|
||||
|
||||
@method_id(111)
|
||||
fun testForallFunctionsWithSelf() {
|
||||
var t = createEmptyTuple();
|
||||
tup111 = createEmptyTuple();
|
||||
t.myTuplePush(10);
|
||||
tup111.myTuplePush(1).myTuplePush(2).myTuplePush(3);
|
||||
return (t.myTupleAt(0), tup111.myTupleAt(tup111.tupleSize() - 1), tup111);
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun main() { }
|
||||
|
||||
|
@ -179,6 +217,8 @@ fun main() { }
|
|||
@testcase | 109 | 200 | 200 3 1 2
|
||||
@testcase | 109 | 100 | 100 0 0 1
|
||||
@testcase | 109 | 102 | 102 2 1 2
|
||||
@testcase | 110 | 0 | 24 [ 2 13 ]
|
||||
@testcase | 111 | | 10 3 [ 1 2 3 ]
|
||||
|
||||
@fif_codegen
|
||||
"""
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
fun unsafeGetInt<X>(any: X): int
|
||||
asm "NOP";
|
||||
|
||||
@method_id(11)
|
||||
fun foo(x: int): int {
|
||||
try {
|
||||
if (x == 7) {
|
||||
|
@ -14,7 +13,6 @@ fun foo(x: int): int {
|
|||
}
|
||||
|
||||
@inline
|
||||
@method_id(12)
|
||||
fun foo_inline(x: int): int {
|
||||
try {
|
||||
assert(!(x == 7)) throw 44;
|
||||
|
@ -25,7 +23,6 @@ fun foo_inline(x: int): int {
|
|||
}
|
||||
|
||||
@inline_ref
|
||||
@method_id(13)
|
||||
fun foo_inlineref(x: int): int {
|
||||
try {
|
||||
if (x == 7) { throw (44, 2); }
|
||||
|
@ -35,26 +32,25 @@ fun foo_inlineref(x: int): int {
|
|||
}
|
||||
}
|
||||
|
||||
@method_id(1)
|
||||
@method_id(101)
|
||||
fun test(x: int, y: int, z: int): int {
|
||||
y = foo(y);
|
||||
return x * 100 + y * 10 + z;
|
||||
}
|
||||
|
||||
@method_id(2)
|
||||
@method_id(102)
|
||||
fun test_inline(x: int, y: int, z: int): int {
|
||||
y = foo_inline(y);
|
||||
return x * 100 + y * 10 + z;
|
||||
}
|
||||
|
||||
@method_id(3)
|
||||
@method_id(103)
|
||||
fun test_inlineref(x: int, y: int, z: int): int {
|
||||
y = foo_inlineref(y);
|
||||
return x * 100 + y * 10 + z;
|
||||
}
|
||||
|
||||
@inline
|
||||
@method_id(14)
|
||||
fun foo_inline_big(
|
||||
x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int, x8: int, x9: int, x10: int,
|
||||
x11: int, x12: int, x13: int, x14: int, x15: int, x16: int, x17: int, x18: int, x19: int, x20: int
|
||||
|
@ -69,7 +65,7 @@ fun foo_inline_big(
|
|||
}
|
||||
}
|
||||
|
||||
@method_id(4)
|
||||
@method_id(104)
|
||||
fun test_inline_big(x: int, y: int, z: int): int {
|
||||
y = foo_inline_big(
|
||||
y, y + 1, y + 2, y + 3, y + 4, y + 5, y + 6, y + 7, y + 8, y + 9,
|
||||
|
@ -77,7 +73,6 @@ fun test_inline_big(x: int, y: int, z: int): int {
|
|||
return x * 1000000 + y * 1000 + z;
|
||||
}
|
||||
|
||||
@method_id(15)
|
||||
fun foo_big(
|
||||
x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int, x8: int, x9: int, x10: int,
|
||||
x11: int, x12: int, x13: int, x14: int, x15: int, x16: int, x17: int, x18: int, x19: int, x20: int
|
||||
|
@ -92,7 +87,7 @@ fun foo_big(
|
|||
}
|
||||
}
|
||||
|
||||
@method_id(5)
|
||||
@method_id(105)
|
||||
fun test_big(x: int, y: int, z: int): int {
|
||||
y = foo_big(
|
||||
y, y + 1, y + 2, y + 3, y + 4, y + 5, y + 6, y + 7, y + 8, y + 9,
|
||||
|
@ -100,7 +95,7 @@ fun test_big(x: int, y: int, z: int): int {
|
|||
return x * 1000000 + y * 1000 + z;
|
||||
}
|
||||
|
||||
@method_id(16)
|
||||
@method_id(106)
|
||||
fun test_catch_into_same(x: int): int {
|
||||
var code = x;
|
||||
try {
|
||||
|
@ -112,7 +107,7 @@ fun test_catch_into_same(x: int): int {
|
|||
}
|
||||
|
||||
|
||||
@method_id(17)
|
||||
@method_id(107)
|
||||
fun test_catch_into_same_2(x: int): int {
|
||||
var code = x;
|
||||
try {
|
||||
|
@ -124,28 +119,77 @@ fun test_catch_into_same_2(x: int): int {
|
|||
return code;
|
||||
}
|
||||
|
||||
global after046: int;
|
||||
|
||||
// this bug existed in FunC and is fixed in v0.4.6
|
||||
fun bug_046_internal(op: int) {
|
||||
if (op == 1) {
|
||||
return;
|
||||
} else if (op == 2) {
|
||||
return;
|
||||
} else {
|
||||
throw 1;
|
||||
}
|
||||
}
|
||||
|
||||
fun bug_046_called() {
|
||||
after046 = 0;
|
||||
try {
|
||||
bug_046_internal(1337);
|
||||
after046 = 1; // shouldn't be called
|
||||
} catch(n) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@method_id(108)
|
||||
fun bug_046_entrypoint() {
|
||||
bug_046_called();
|
||||
return after046;
|
||||
}
|
||||
|
||||
global g_reg: int;
|
||||
|
||||
@method_id(109)
|
||||
fun test109(): (int, int) {
|
||||
var l_reg = 10;
|
||||
g_reg = 10;
|
||||
try {
|
||||
// note, that regardless of assignment, an exception RESTORES them to previous (to 10)
|
||||
// it's very unexpected, but is considered to be a TVM feature, not a bug
|
||||
g_reg = 999;
|
||||
l_reg = 999;
|
||||
bug_046_internal(999); // throws
|
||||
} catch {
|
||||
}
|
||||
// returns (10,10) because of an exception, see a comment above
|
||||
return (g_reg, l_reg);
|
||||
}
|
||||
|
||||
fun main() {
|
||||
}
|
||||
|
||||
/**
|
||||
method_id | in | out
|
||||
@testcase | 1 | 1 2 3 | 123
|
||||
@testcase | 1 | 3 8 9 | 389
|
||||
@testcase | 1 | 3 7 9 | 329
|
||||
@testcase | 2 | 1 2 3 | 123
|
||||
@testcase | 2 | 3 8 9 | 389
|
||||
@testcase | 2 | 3 7 9 | 329
|
||||
@testcase | 3 | 1 2 3 | 123
|
||||
@testcase | 3 | 3 8 9 | 389
|
||||
@testcase | 3 | 3 7 9 | 329
|
||||
@testcase | 4 | 4 8 9 | 4350009
|
||||
@testcase | 4 | 4 7 9 | 4001009
|
||||
@testcase | 5 | 4 8 9 | 4350009
|
||||
@testcase | 5 | 4 7 9 | 4001009
|
||||
@testcase | 16 | 5 | 5
|
||||
@testcase | 16 | 20 | 44
|
||||
@testcase | 17 | 5 | 5
|
||||
@testcase | 17 | 20 | 20
|
||||
method_id | in | out
|
||||
@testcase | 101 | 1 2 3 | 123
|
||||
@testcase | 101 | 3 8 9 | 389
|
||||
@testcase | 101 | 3 7 9 | 329
|
||||
@testcase | 102 | 1 2 3 | 123
|
||||
@testcase | 102 | 3 8 9 | 389
|
||||
@testcase | 102 | 3 7 9 | 329
|
||||
@testcase | 103 | 1 2 3 | 123
|
||||
@testcase | 103 | 3 8 9 | 389
|
||||
@testcase | 103 | 3 7 9 | 329
|
||||
@testcase | 104 | 4 8 9 | 4350009
|
||||
@testcase | 104 | 4 7 9 | 4001009
|
||||
@testcase | 105 | 4 8 9 | 4350009
|
||||
@testcase | 105 | 4 7 9 | 4001009
|
||||
@testcase | 106 | 5 | 5
|
||||
@testcase | 106 | 20 | 44
|
||||
@testcase | 107 | 5 | 5
|
||||
@testcase | 107 | 20 | 20
|
||||
@testcase | 108 | | 0
|
||||
|
||||
@code_hash 73240939343624734070640372352271282883450660826541545137654364443860257436623
|
||||
@code_hash 39307974281105539319288356721945232226028429128341177951717392648324358675585
|
||||
*/
|
||||
|
|
14
tolk-tester/tests/unreachable-1.tolk
Normal file
14
tolk-tester/tests/unreachable-1.tolk
Normal file
|
@ -0,0 +1,14 @@
|
|||
fun main(x: int) {
|
||||
if (x) {
|
||||
x = 10;;;;;
|
||||
return x;;;
|
||||
x = 20;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@testcase | 0 | 1 | 10
|
||||
@stderr warning: unreachable code
|
||||
@stderr x = 20;
|
||||
*/
|
22
tolk-tester/tests/unreachable-2.tolk
Normal file
22
tolk-tester/tests/unreachable-2.tolk
Normal file
|
@ -0,0 +1,22 @@
|
|||
fun main(x: int) {
|
||||
if (x) {
|
||||
if (x > 10) {
|
||||
return 1; // throw 1;
|
||||
} else if (true) {
|
||||
return -1;
|
||||
} else {
|
||||
return 2; // throw 2;
|
||||
}
|
||||
} else {
|
||||
{{return 1;}
|
||||
x = 30;}
|
||||
}
|
||||
assert(false, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
@testcase | 0 | 1 | -1
|
||||
@stderr warning: unreachable code
|
||||
@stderr assert(false, 10)
|
||||
@stderr x = 30
|
||||
*/
|
|
@ -15,8 +15,101 @@ fun testVarApply1() {
|
|||
return (s.loadInt(32), s.loadInt(32));
|
||||
}
|
||||
|
||||
@inline
|
||||
fun my_throw_always() {
|
||||
throw 1000;
|
||||
}
|
||||
|
||||
@inline
|
||||
fun get_raiser() {
|
||||
return my_throw_always;
|
||||
}
|
||||
|
||||
@method_id(102)
|
||||
fun testVarApplyWithoutSavingResult() {
|
||||
try {
|
||||
var raiser = get_raiser();
|
||||
raiser(); // `some_var()` is always impure, the compiler has no considerations about its runtime value
|
||||
return 0;
|
||||
} catch (code) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@inline
|
||||
fun sum(a: int, b: int) {
|
||||
assert(a + b < 24, 1000);
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@inline
|
||||
fun mul(a: int, b: int) {
|
||||
assert(a * b < 24, 1001);
|
||||
return a * b;
|
||||
}
|
||||
|
||||
fun demo_handler(op: int, query_id: int, a: int, b: int): int {
|
||||
if (op == 0xF2) {
|
||||
val func = query_id % 2 == 0 ? sum : mul;
|
||||
val result = func(a, b);
|
||||
return 0; // result not used, we test that func is nevertheless called
|
||||
}
|
||||
if (op == 0xF4) {
|
||||
val func = query_id % 2 == 0 ? sum : mul;
|
||||
val result = func(a, b);
|
||||
return result;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@method_id(103)
|
||||
fun testVarApplyInTernary() {
|
||||
var t: tuple = createEmptyTuple();
|
||||
try {
|
||||
t.tuplePush(demo_handler(0xF2, 122, 100, 200));
|
||||
} catch(code) {
|
||||
t.tuplePush(code);
|
||||
}
|
||||
try {
|
||||
t.tuplePush(demo_handler(0xF4, 122, 100, 200));
|
||||
} catch(code) {
|
||||
t.tuplePush(code);
|
||||
}
|
||||
try {
|
||||
t.tuplePush(demo_handler(0xF2, 122, 10, 10));
|
||||
} catch(code) {
|
||||
t.tuplePush(code);
|
||||
}
|
||||
try {
|
||||
t.tuplePush(demo_handler(0xF2, 123, 10, 10));
|
||||
} catch(code) {
|
||||
t.tuplePush(code);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
fun always_throw2(x: int) {
|
||||
throw 239 + x;
|
||||
}
|
||||
|
||||
global global_f: int -> ();
|
||||
|
||||
@method_id(104)
|
||||
fun testGlobalVarApply() {
|
||||
try {
|
||||
global_f = always_throw2;
|
||||
global_f(1);
|
||||
return 0;
|
||||
} catch (code) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {}
|
||||
|
||||
/**
|
||||
@testcase | 101 | | 1 2
|
||||
@testcase | 102 | | 1000
|
||||
@testcase | 103 | | [ 1000 1000 0 1001 ]
|
||||
@testcase | 104 | | 240
|
||||
*/
|
||||
|
|
|
@ -347,11 +347,11 @@ class TolkTestFile {
|
|||
if (exit_code === 0 && this.compilation_should_fail)
|
||||
throw new TolkCompilationSucceededError("compilation succeeded, but it should have failed")
|
||||
|
||||
if (exit_code !== 0 && this.compilation_should_fail) {
|
||||
for (let should_include of this.stderr_includes)
|
||||
should_include.check(stderr)
|
||||
for (let should_include of this.stderr_includes) // @stderr is used to check errors and warnings
|
||||
should_include.check(stderr)
|
||||
|
||||
if (exit_code !== 0 && this.compilation_should_fail)
|
||||
return
|
||||
}
|
||||
|
||||
if (exit_code !== 0 && !this.compilation_should_fail)
|
||||
throw new TolkCompilationFailedError(`tolk exit_code = ${exit_code}`, stderr)
|
||||
|
|
|
@ -327,9 +327,10 @@ class TolkTestFile:
|
|||
if exit_code == 0 and self.compilation_should_fail:
|
||||
raise TolkCompilationSucceededError("compilation succeeded, but it should have failed")
|
||||
|
||||
for should_include in self.stderr_includes: # @stderr is used to check errors and warnings
|
||||
should_include.check(stderr)
|
||||
|
||||
if exit_code != 0 and self.compilation_should_fail:
|
||||
for should_include in self.stderr_includes:
|
||||
should_include.check(stderr)
|
||||
return
|
||||
|
||||
if exit_code != 0 and not self.compilation_should_fail:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue