Skip to content

Commit

Permalink
Merge pull request #1503 from ton-blockchain/tolk-v0.8
Browse files Browse the repository at this point in the history
Tolk v0.8: preparation for structures; indexed access `var.0`
  • Loading branch information
EmelyanenkoK authored Feb 3, 2025
2 parents b1c9466 + e9d8f16 commit e5feb76
Show file tree
Hide file tree
Showing 64 changed files with 1,368 additions and 586 deletions.
19 changes: 14 additions & 5 deletions crypto/smartcont/tolk-stdlib/common.tolk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Standard library for Tolk (LGPL licence).
// It contains common functions that are available out of the box, the user doesn't have to import anything.
// More specific functions are required to be imported explicitly, like "@stdlib/tvm-dicts".
tolk 0.7
tolk 0.8

/**
Tuple manipulation primitives.
Expand All @@ -21,23 +21,32 @@ fun tuplePush<T>(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>(t: tuple): T
fun tupleFirst<T>(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>(t: tuple, index: int): T
fun tupleAt<T>(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<T>(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>(t: tuple): T
fun tupleLast<T>(self: tuple): T
asm "LAST";


Expand Down
2 changes: 1 addition & 1 deletion crypto/smartcont/tolk-stdlib/gas-payments.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.7
tolk 0.8

/**
Gas and payment related primitives.
Expand Down
2 changes: 1 addition & 1 deletion crypto/smartcont/tolk-stdlib/lisp-lists.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.7
tolk 0.8

/**
Lisp-style lists are nested 2-elements tuples: `(1, (2, (3, null)))` represents list `[1, 2, 3]`.
Expand Down
2 changes: 1 addition & 1 deletion crypto/smartcont/tolk-stdlib/tvm-dicts.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.7
tolk 0.8

/**
Dictionaries are represented as `cell` data type (cells can store anything, dicts in particular).
Expand Down
2 changes: 1 addition & 1 deletion crypto/smartcont/tolk-stdlib/tvm-lowlevel.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.7
tolk 0.8

/// Usually `c3` has a continuation initialized by the whole code of the contract. It is used for function calls.
/// The primitive returns the current value of `c3`.
Expand Down
32 changes: 30 additions & 2 deletions tolk-tester/tests/a10.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
"""
Expand All @@ -104,9 +116,9 @@ fun testStartBalanceCodegen2() {
testDumpDontPolluteStack PROC:<{
...
DUMPSTK
x{6d79} PUSHSLICE // f s _9
x{6d79} PUSHSLICE // f s '5
STRDUMP DROP
SBITS // f _11
SBITS // f '6
}>
"""

Expand All @@ -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
}>
"""
*/
3 changes: 3 additions & 0 deletions tolk-tester/tests/a6.tolk
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
fun f(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) {
// solve a 2x2 linear equation
var D: int = a*d - b*c;;;; var Dx: int = e*d-b*f ;;;; var Dy: int = a * f - e * c;
__expect_type(D, "int");
__expect_type(D*D, "int");
__expect_type(calc_phi, "() -> int");
return (Dx/D,Dy/D);
};;;;

Expand Down
4 changes: 2 additions & 2 deletions tolk-tester/tests/a6_1.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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
*/
13 changes: 11 additions & 2 deletions tolk-tester/tests/allow_post_modification.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -138,5 +140,12 @@ fun main() {
inc CALLDICT // self newY
}>
"""

@fif_codegen
"""
test_assign_tensor_global PROC:<{
// x.0 x.1
"""

@code_hash 7627024945492125068389905298530400936797031708759561372406088054030801992712
*/
12 changes: 12 additions & 0 deletions tolk-tester/tests/assignment-tests.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ fun autoInferIntNull(x: int) {
return x;
}

fun typesAsIdentifiers(builder: builder) {
var int = 1;
var cell = builder.endCell();
var slice = cell.beginParse();
{
var cell: cell = cell;
var tuple: tuple = createEmptyTuple();
var bool: bool = tuple.tupleAt<int>(0) > 0;
}
return int;
}

fun main(value: int) {
var (x: int, y) = (autoInferIntNull(value), autoInferIntNull(value * 2));
if (x == null && y == null) { return null; }
Expand Down
54 changes: 27 additions & 27 deletions tolk-tester/tests/bit-operators.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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
}>
"""

Expand All @@ -142,35 +142,35 @@ 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
}>
"""

@fif_codegen
"""
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
}>
"""

Expand All @@ -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
}>
"""

Expand All @@ -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
}>
"""
*/
20 changes: 10 additions & 10 deletions tolk-tester/tests/cells-slices.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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 _5=2
SWAP // _5=2 _0
32 STU // _0
3 PUSHINT // _0 _9=3
SWAP // _9=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
}>
"""
*/
6 changes: 3 additions & 3 deletions tolk-tester/tests/codegen_check_demo.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -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:<{
...
Expand Down Expand Up @@ -63,7 +63,7 @@ main PROC:<{
@fif_codegen
"""
OVER
0 GTINT // s z t _5
0 GTINT // s z t '5
"""

@fif_codegen
Expand All @@ -83,7 +83,7 @@ FALSE
}>
"""

@fif_codegen NOT // _8
@fif_codegen NOT // '8
@fif_codegen main PROC:<{

@fif_codegen_avoid PROCINLINE
Expand Down
Loading

0 comments on commit e5feb76

Please sign in to comment.