diff --git a/crypto/smartcont/tolk-stdlib/common.tolk b/crypto/smartcont/tolk-stdlib/common.tolk index 503b9195af..3656e5ab78 100644 --- a/crypto/smartcont/tolk-stdlib/common.tolk +++ b/crypto/smartcont/tolk-stdlib/common.tolk @@ -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/gas-payments". -tolk 1.4.2 +tolk 1.5 /** Built-in types. @@ -99,9 +99,6 @@ type string = builtin /// an error will be fired at serialization with [T.toCell] and similar, NOT at assignment. type bitsN = builtin -/// `bytes8`, `bytes99`, etc. is a convenient alias for `bits(N*8)` -type bytesN = builtin - /// `map` is "a map from a key K to a value V". /// Internally, it's an "optional cell": an empty map is `null`, a non-empty points to a root cell. /// Restrictions for K and V types: @@ -217,38 +214,38 @@ fun createEmptyTuple(): array /// If its size exceeds 255, throws a type check exception. @pure fun array.push(mutate self, value: T): void - builtin + asm "TPUSH" // for multi-slot T, the value is first TUPLE'd /// Returns the first element of a non-empty array. @pure fun array.first(self): T - builtin + asm "FIRST" // for multi-slot T, the result is additionally UNTUPLE'd /// Returns the `i`-th element of an array. @pure fun array.get(self, index: int): T - builtin + asm "INDEXVAR" // constant index uses `INDEX n`; multi-slot T is additionally UNTUPLE'd /// Sets the `i`-th element of an array to a specified value /// (an element with this index must already exist, a new element is not created). @pure fun array.set(mutate self, value: T, index: int): void - builtin + asm "SETINDEXVAR" // constant index uses `SETINDEX n`; multi-slot T is first TUPLE'd /// Returns the size of an array (elements count in it). @pure fun array.size(self): int - builtin + asm "TLEN" /// Returns the last element of a non-empty array. @pure fun array.last(self): T - builtin + asm "LAST" // for multi-slot T, the result is additionally UNTUPLE'd /// Pops and returns the last element of a non-empty array. @pure fun array.pop(mutate self): T - builtin + asm "TPOP" // for multi-slot T, the result is additionally UNTUPLE'd /// Packs any object from a stack to a tuple. /// An object occupies N slots on a stack — the tuple will be of size N. @@ -266,14 +263,14 @@ fun array.pop(mutate self): T /// See [T.fromTuple] counterpart. @pure fun T.toTuple(self): array - builtin + asm "1 TUPLE" // 1 is replaced by T's stack width; TUPLEVAR is used above 15 /// Unpacks a tuple back to an object on a stack. /// It works identically as casting `unknown` to an object. /// See [T.toTuple] for explanation and examples. @pure fun T.fromTuple(packedObject: array): T - builtin + asm "1 UNTUPLE" // 1 is replaced by T's stack width; UNTUPLEVAR is used above 15 /** Mathematical primitives. @@ -287,7 +284,7 @@ fun grams(floatString: string): coins builtin /// Deprecated alias for [grams]. -/// From Tolk v1.5, it will become @deprecated("""use `grams("0.05")` instead of `ton("0.05")`""") +@deprecated("""use `grams("0.05")` instead of `ton("0.05")`""") @pure fun ton(floatString: string): coins builtin @@ -332,22 +329,22 @@ fun modDiv(x: int, y: int): (int, int) /// The intermediate result is stored in a 513-bit integer to prevent precision loss. @pure fun mulDivFloor(x: int, y: int, z: int): int - builtin + asm "MULDIV" // constants may fold or use specialized shift/div instructions /// Similar to [mulDivFloor], but rounds the result: round(x * y / z). @pure fun mulDivRound(x: int, y: int, z: int): int - builtin + asm "MULDIVR" // constants may fold or use specialized shift/div instructions /// Similar to [mulDivFloor], but ceils the result: ceil(x * y / z). @pure fun mulDivCeil(x: int, y: int, z: int): int - builtin + asm "MULDIVC" // constants may fold or use specialized shift/div instructions /// Computes the quotient and remainder of (x * y / z). Example: mulDivMod(112,3,10) = (33,6) @pure fun mulDivMod(x: int, y: int, z: int): (int, int) - builtin + asm "MULDIVMOD" /** @@ -788,15 +785,15 @@ struct debug /// Dump a variable to the debug log as a raw TVM stack value. fun debug.print(x: T): void - builtin + asm "s0 DUMP" "DROP" // repeated for every stack slot of multi-slot T /// Dump a string to the debug log. fun debug.printString(x: string): void - builtin + asm "CTOS" "STRDUMP" "DROP" // only the first snake chunk is dumped /// Dumps the stack (at most the top 255 values) and shows the total stack depth. fun debug.dumpStack(): void - builtin + asm "DUMPSTK" /* @@ -822,14 +819,14 @@ fun debug.dumpStack(): void @pure @deprecated("use `[]` instead of `createEmptyMap`") fun createEmptyMap(): map - builtin + asm "NEWDICT" // an empty map is represented by TVM null /// Converts a low-level TVM dictionary to a typed map. /// Actually, does nothing: accepts an "optional cell" and returns the same "optional cell", /// so if you specify key/value types incorrectly, it will fail later, at `map.get` and similar. @pure fun createMapFromLowLevelDict(d: dict): map - builtin + asm "NOP" // map and low-level dict have the same runtime representation /// Converts a high-level map to a low-level TVM dictionary. /// Actually, does nothing: returns the same "optional cell". @@ -847,7 +844,7 @@ fun map.isEmpty(self): bool /// Checks whether a key exists in a map. @pure fun map.exists(self, key: K): bool - builtin + asm "DICTGET" // actual lowering depends on K/V serialization /// Gets an element by key. If not found, does NOT throw, just returns isFound = false. /// ``` @@ -858,12 +855,12 @@ fun map.exists(self, key: K): bool /// ``` @pure fun map.get(self, key: K): MapLookupResult - builtin + asm "DICTGET" // actual lowering depends on K/V serialization /// Gets an element by key and throws if it doesn't exist. @pure fun map.mustGet(self, key: K, throwIfNotFound: int = 9): V - builtin + asm "DICTGET" // actual lowering depends on K/V serialization /// Sets an element by key. /// ``` @@ -872,7 +869,7 @@ fun map.mustGet(self, key: K, throwIfNotFound: int = 9): V /// Since it returns `self`, calls may be chained. @pure fun map.set(mutate self, key: K, value: V): self - builtin + asm "DICTSET" // actual lowering depends on K/V serialization /// Sets an element and returns the previous element at that key. If no previous, isFound = false. /// ``` @@ -883,32 +880,32 @@ fun map.set(mutate self, key: K, value: V): self /// ``` @pure fun map.setAndGetPrevious(mutate self, key: K, value: V): MapLookupResult - builtin + asm "DICTSETGET" // actual lowering depends on K/V serialization /// Sets an element only if the key already exists. Returns whether an element was replaced. @pure fun map.replaceIfExists(mutate self, key: K, value: V): bool - builtin + asm "DICTREPLACE" // actual lowering depends on K/V serialization /// Sets an element only if the key already exists and returns the previous element at that key. @pure fun map.replaceAndGetPrevious(mutate self, key: K, value: V): MapLookupResult - builtin + asm "DICTREPLACEGET" // actual lowering depends on K/V serialization /// Sets an element only if the key does not exist. Returns whether an element was added. @pure fun map.addIfNotExists(mutate self, key: K, value: V): bool - builtin + asm "DICTADD" // actual lowering depends on K/V serialization /// Sets an element only if the key does not exist. If exists, returns an old value. @pure fun map.addOrGetExisting(mutate self, key: K, value: V): MapLookupResult - builtin + asm "DICTADDGET" // actual lowering depends on K/V serialization /// Delete an element at the key. Returns whether an element was deleted. @pure fun map.delete(mutate self, key: K): bool - builtin + asm "DICTDEL" // actual lowering depends on K/V serialization /// Delete an element at the key and returns the deleted element. If not exists, isFound = false. /// ``` @@ -919,7 +916,7 @@ fun map.delete(mutate self, key: K): bool /// ``` @pure fun map.deleteAndGetDeleted(mutate self, key: K): MapLookupResult - builtin + asm "DICTDELGET" // actual lowering depends on K/V serialization /// Finds the first (minimal) element in a map. If key are integers, it's the minimal integer. /// If keys are addresses or complex structures (represented as slices), it's lexicographically smallest. @@ -934,7 +931,7 @@ fun map.deleteAndGetDeleted(mutate self, key: K): MapLookupResult /// ``` @pure fun map.findFirst(self): MapEntry - builtin + asm "DICTMIN" // actual lowering depends on K/V serialization /// Finds the last (maximal) element in a map. If key are integers, it's the maximal integer. /// If keys are addresses or complex structures (represented as slices), it's lexicographically largest. @@ -949,31 +946,31 @@ fun map.findFirst(self): MapEntry /// ``` @pure fun map.findLast(self): MapEntry - builtin + asm "DICTMAX" // actual lowering depends on K/V serialization /// Finds an element with key > pivotKey. /// Don't forget to check `isFound` before using `getKey()` and `loadValue()` of the result. @pure fun map.findKeyGreater(self, pivotKey: K): MapEntry - builtin + asm "DICTGETNEXT" // actual lowering depends on K/V serialization /// Finds an element with key >= pivotKey. /// Don't forget to check `isFound` before using `getKey()` and `loadValue()` of the result. @pure fun map.findKeyGreaterOrEqual(self, pivotKey: K): MapEntry - builtin + asm "DICTGETNEXTEQ" // actual lowering depends on K/V serialization /// Finds an element with key < pivotKey. /// Don't forget to check `isFound` before using `getKey()` and `loadValue()` of the result. @pure fun map.findKeyLess(self, pivotKey: K): MapEntry - builtin + asm "DICTGETPREV" // actual lowering depends on K/V serialization /// Finds an element with key <= pivotKey. /// Don't forget to check `isFound` before using `getKey()` and `loadValue()` of the result. @pure fun map.findKeyLessOrEqual(self, pivotKey: K): MapEntry - builtin + asm "DICTGETPREVEQ" // actual lowering depends on K/V serialization /// Iterate over a map in ascending order. /// ``` @@ -986,7 +983,7 @@ fun map.findKeyLessOrEqual(self, pivotKey: K): MapEntry /// ``` @pure fun map.iterateNext(self, current: MapEntry): MapEntry - builtin + asm "DICTGETNEXT" // actual lowering depends on K/V serialization /// Iterate over a map in reverse order. /// ``` @@ -999,7 +996,7 @@ fun map.iterateNext(self, current: MapEntry): MapEntry /// ``` @pure fun map.iteratePrev(self, current: MapEntry): MapEntry - builtin + asm "DICTGETPREV" // actual lowering depends on K/V serialization /// `MapLookupResult` is a return value of `map.get`, `map.setAndGetPrevious`, and similar. /// Instead of returning a nullable value (that you'd check on null before usage), @@ -1015,7 +1012,6 @@ struct MapLookupResult { isFound: bool } -@pure fun MapLookupResult.loadValue(self): TValue { // it's assumed that you check for `isFound` before calling `loadValue()`; // note that `assertEnd` is called to ensure no remaining data left besides TValue @@ -1024,7 +1020,6 @@ fun MapLookupResult.loadValue(self): TValue { }) } -@pure fun MapLookupResult.loadValue(self): slice { return self.rawSlice! } @@ -1044,14 +1039,12 @@ struct MapEntry { isFound: bool } -@pure fun MapEntry.getKey(self): K { // it's assumed that you check for `isFound` before calling `getKey()`; // (otherwise, it will contain an incorrect stack slot with `null` value, not K) return self.key } -@pure fun MapEntry.loadValue(self): V { // it's assumed that you check for `isFound` before calling `loadValue()`; // note that `assertEnd` is inserted to ensure no remaining data left besides TValue @@ -1060,7 +1053,6 @@ fun MapEntry.loadValue(self): V { }) } -@pure fun MapEntry.loadValue(self): slice { return self.rawValue! } @@ -1183,32 +1175,32 @@ fun slice.preloadRef(self): cell /// Loads a signed len-bit integer from a slice. @pure fun slice.loadInt(mutate self, len: int): int - builtin + asm(-> 1 0) "LDIX" // constant len uses "LDI len" /// Loads an unsigned len-bit integer from a slice. @pure fun slice.loadUint(mutate self, len: int): int - builtin + asm(-> 1 0) "LDUX" // constant len uses "LDU len" /// Loads the first `0 ≤ len ≤ 1023` bits from a slice. @pure fun slice.loadBits(mutate self, len: int): slice - builtin + asm(-> 1 0) "LDSLICEX" // constant len uses "LDSLICE len" /// Preloads a signed len-bit integer from a slice. @pure fun slice.preloadInt(self, len: int): int - builtin + asm "PLDIX" // constant len uses "PLDI len" /// Preloads an unsigned len-bit integer from a slice. @pure fun slice.preloadUint(self, len: int): int - builtin + asm "PLDUX" // constant len uses "PLDU len" /// Preloads the first `0 ≤ len ≤ 1023` bits from a slice. @pure fun slice.preloadBits(self, len: int): slice - builtin + asm "PLDSLICEX" // constant len uses "PLDSLICE len" /// Loads the serialized amount of nanograms (any unsigned integer up to `2^120 - 1`). @pure @@ -1223,7 +1215,7 @@ fun slice.loadBool(mutate self): bool /// Shifts a slice pointer to len bits forward. @pure fun slice.skipBits(mutate self, len: int): self - builtin + asm "SDSKIPFIRST" // constant skips can be merged /// Returns the first `0 ≤ len ≤ 1023` bits of a slice. @pure @@ -1314,12 +1306,12 @@ fun builder.storeRef(mutate self, c: cell): self /// Stores a signed len-bit integer into a builder (`0 ≤ len ≤ 257`). @pure fun builder.storeInt(mutate self, x: int, len: int): self - builtin + asm(x self len) "STIX" // constant len uses "STI len" or even merged to STSLICECONST /// Stores an unsigned len-bit integer into a builder (`0 ≤ len ≤ 256`). @pure fun builder.storeUint(mutate self, x: int, len: int): self - builtin + asm(x self len) "STUX" // constant len uses "STU len" or even merged to STSLICECONST /// Stores a slice into a builder. @pure @@ -1355,12 +1347,12 @@ fun builder.storeAddressNone(mutate self): self /// Stores the amount of nanograms into a builder. @pure fun builder.storeCoins(mutate self, x: coins): self - builtin + asm "STGRAMS" // compile-time zero can be merged with adjacent stores /// Stores a boolean into a builder. @pure fun builder.storeBool(mutate self, x: bool): self - builtin + asm(x self) "1 STI" // compile-time value can be merged with adjacent stores /// Stores a low-level TVM dictionary (optional cell) into a builder. /// In other words, if `c` is null, stores `0`; if `c` is not null, stores `1` and a reference to `c`. diff --git a/crypto/smartcont/tolk-stdlib/exotic-cells.tolk b/crypto/smartcont/tolk-stdlib/exotic-cells.tolk index 23da53fe0e..661d8171d8 100644 --- a/crypto/smartcont/tolk-stdlib/exotic-cells.tolk +++ b/crypto/smartcont/tolk-stdlib/exotic-cells.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 enum ExoticCellType: int8 { Ordinary = -1 @@ -46,7 +46,6 @@ fun cell.loadSpecial(self): cell /// Transforms an ordinary or exotic cell into a library reference /// (into an exotic cell with contents "8 bits cell type" + "256 bits lib hash"). -@pure fun cell.toLibraryReference(self): cell { return beginCell() .storeInt(ExoticCellType.LibraryReference, 8) diff --git a/crypto/smartcont/tolk-stdlib/gas-payments.tolk b/crypto/smartcont/tolk-stdlib/gas-payments.tolk index ae692ad427..5dc056acd8 100644 --- a/crypto/smartcont/tolk-stdlib/gas-payments.tolk +++ b/crypto/smartcont/tolk-stdlib/gas-payments.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /** Gas and payment related primitives. diff --git a/crypto/smartcont/tolk-stdlib/lisp-lists.tolk b/crypto/smartcont/tolk-stdlib/lisp-lists.tolk index ab43b6a4ab..c460f9750e 100644 --- a/crypto/smartcont/tolk-stdlib/lisp-lists.tolk +++ b/crypto/smartcont/tolk-stdlib/lisp-lists.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /** A lisp-style list is a set of nested 2-element TVM tuples: @@ -21,7 +21,6 @@ tolk 1.4.2 ``` */ -@pure fun lisp_list.isEmpty(self): bool { return self.tvmTuple == null } @@ -29,7 +28,6 @@ fun lisp_list.isEmpty(self): bool { /// Inserts an element an the front. /// Before: `[a b c]`. /// After: `[new a b c]`. -@pure fun lisp_list.prependHead(mutate self, value: T): void { self.tvmTuple = [value, self.tvmTuple as unknown as lisp_list]; } @@ -38,7 +36,6 @@ fun lisp_list.prependHead(mutate self, value: T): void { /// Before: `[a b c]`. /// After: `[b c]`, returns `a`. /// Throws an exceptions if a list is empty. -@pure fun lisp_list.popHead(mutate self): T { var [head, tail] = self.tvmTuple!; self.tvmTuple = tail as unknown as [T, lisp_list]?; @@ -47,21 +44,18 @@ fun lisp_list.popHead(mutate self): T { /// Returns an element from the front: `a` for `[a b c]`. /// Throws an exceptions if a list is empty. -@pure fun lisp_list.getHead(self): T { return self.tvmTuple!.0 } /// Returns all but the frontmost element: `[b c]` for `[a b c]`. /// Throws an exceptions if a list is empty. -@pure fun lisp_list.getTail(self): lisp_list { return self.tvmTuple!.1 } /// Calculates the size of a list by looping all the elements (there is no other way to do it). /// The longer the list — the more gas consumption (O(N) complexity). -@pure fun lisp_list.calculateSize(self): int { var size = 0; var ptr = self; @@ -74,7 +68,6 @@ fun lisp_list.calculateSize(self): int { /// Returns the list with elements in reversed order. /// Given `[a b c]` returns `[c b a]`. -@pure fun lisp_list.calculateReversed(self): lisp_list { var reversed = [] as lisp_list; var ptr = self; @@ -86,7 +79,6 @@ fun lisp_list.calculateReversed(self): lisp_list { /// Returns the concatenation of two lists. /// Given `[a b]` and `[c d]`, returns `[a b c d]`. -@pure fun lisp_list.calculateConcatenation(self, other: lisp_list): lisp_list { var reversed = [] as lisp_list; var ptr = self; @@ -118,6 +110,7 @@ fun createEmptyList(): lisp_list /// `lisp_list` can be serialized if `T` is serializable. It's encoded as reversed snake refs: /// A list `[1 2 3]` (on a stack: `[1 [2 [3 null]]]`) is "3.ref(2.ref(1.ref(empty cell)))". /// An empty list `[]` (on a stack: `null`) is "empty cell" +@inline fun lisp_list.packToBuilder(self, mutate b: builder): void { var tail = createEmptyCell(); var ptr = self; @@ -129,6 +122,7 @@ fun lisp_list.packToBuilder(self, mutate b: builder): void { /// Unserialize `lisp_list` from snake refs: /// From "3.ref(2.ref(1.ref(empty cell)))" get a list `[1 2 3]` (on a stack: `[1 [2 [3 null]]]`). +@inline fun lisp_list.unpackFromSlice(mutate s: slice): lisp_list { var result = [] as lisp_list; var head = s.loadRef().beginParse(); diff --git a/crypto/smartcont/tolk-stdlib/reflection.tolk b/crypto/smartcont/tolk-stdlib/reflection.tolk index 6a1f3578ff..afd88f4b9c 100644 --- a/crypto/smartcont/tolk-stdlib/reflection.tolk +++ b/crypto/smartcont/tolk-stdlib/reflection.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /** Reflection API. diff --git a/crypto/smartcont/tolk-stdlib/strings.tolk b/crypto/smartcont/tolk-stdlib/strings.tolk index b9c8ba638d..a85e205154 100644 --- a/crypto/smartcont/tolk-stdlib/strings.tolk +++ b/crypto/smartcont/tolk-stdlib/strings.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /** A string is a language primitive — a cell containing snake-formatted text data. @@ -21,7 +21,6 @@ fun string.depth(self): int /// Calculates length of a string in bytes (not unicode symbols, just bytes). /// Since strings are stored in snake format (chain of cells), this requires traversing all cells. -@pure @inline_ref fun string.calculateLength(self): int { var bits = 0; @@ -38,7 +37,6 @@ fun string.calculateLength(self): int { /// The hash is the same regardless of snake layout: /// "abcd", "ab" + (ref "cd"), "a" + (ref "bcd" + (ref "")) all produce the same hash. /// Uses FNV-1a hashing with standard 128-bit parameters. -@pure @inline_ref fun string.hash(self): uint256 { val FNV_PRIME = 309485009821345068724781371; @@ -59,7 +57,6 @@ fun string.hash(self): uint256 { /// Compares two strings, considering snake cells. /// For example, "abcd" is equal to "ab" + (ref "cd") is equal to "a" + (ref "bc" + (ref "d")). -@pure fun string.equalTo(self, another: string): bool { return self.depth() + another.depth() != 0 // some is snake, so we have "ab->cd" / "a->bc->d" / etc., do full traverse diff --git a/crypto/smartcont/tolk-stdlib/tvm-dicts.tolk b/crypto/smartcont/tolk-stdlib/tvm-dicts.tolk index 216c9c2f5c..c7fb247b2c 100644 --- a/crypto/smartcont/tolk-stdlib/tvm-dicts.tolk +++ b/crypto/smartcont/tolk-stdlib/tvm-dicts.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /** Low-level API working with TVM dictionaries. diff --git a/crypto/smartcont/tolk-stdlib/tvm-lowlevel.tolk b/crypto/smartcont/tolk-stdlib/tvm-lowlevel.tolk index 1510d94737..8c2caf6eef 100644 --- a/crypto/smartcont/tolk-stdlib/tvm-lowlevel.tolk +++ b/crypto/smartcont/tolk-stdlib/tvm-lowlevel.tolk @@ -1,5 +1,5 @@ // A part of standard library for Tolk -tolk 1.4.2 +tolk 1.5 /// 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`. diff --git a/tolk-tester/tests/abi-tests.tolk b/tolk-tester/tests/abi-tests.tolk index 03f3cb6c89..721e5eec00 100644 --- a/tolk-tester/tests/abi-tests.tolk +++ b/tolk-tester/tests/abi-tests.tolk @@ -141,6 +141,17 @@ struct Defaults { a: int = C } +struct CalcSwapCostReply { + from: address + to: address + cost: coins +} + +get fun calc_swap_cost(from: address): CalcSwapCostReply + +/// @param precision number of decimal places +get fun pool_reserves(precision: uint8 = 9): (coins, coins); + /** @testcase | 0 | 7 5 | 12 @@ -389,6 +400,42 @@ struct Defaults { ] """ +@abi_json +""" + "name": "calc_swap_cost", + "parameters": [ + { + "name": "from", + ... +""" + +@abi_json +""" + "name": "pool_reserves", + "parameters": [ + { + "name": "precision", + ... + "description": "number of decimal places", + "default_value": { + "kind": "int", + "v": "9" + } + } + ], +""" + +@abi_json +""" + "kind": "struct", + "name": "CalcSwapCostReply", +""" + +@fif_codegen_avoid 86806 DECLMETHOD calc_swap_cost() +@fif_codegen_avoid 108535 DECLMETHOD pool_reserves() + +@cmd_line_option --allow-empty-get-fun + @abi_json_avoid UnusedOutgoing @abi_json_avoid ERR_UNUSED_ABI */ diff --git a/tolk-tester/tests/arrays-tuples-tests.tolk b/tolk-tester/tests/arrays-tuples-tests.tolk index 36f6098f62..a89300f19e 100644 --- a/tolk-tester/tests/arrays-tuples-tests.tolk +++ b/tolk-tester/tests/arrays-tuples-tests.tolk @@ -135,7 +135,7 @@ fun test101() { __expect_type((rand != 0 ? [1,2] : [3, null]) as array, "array"); __expect_type(rand != 0 ? [""] : [] as array, "array"); - __expect_type(rand != 0 ? [] : [""] as tuple, "array"); + __expect_type(rand != 0 ? [] : [""] as tuple as array, "array"); __expect_type(array [], "array"); __expect_type(tuple [1, "aba", []], "tuple"); @@ -144,7 +144,7 @@ fun test101() { __expect_type((rand != 0 ? array [1,2] : array [3, null]), "array"); __expect_type(rand != 0 ? [""] : array [], "array"); - __expect_type(rand != 0 ? [] : tuple [""], "array"); + __expect_type(rand != 0 ? [] : array [""], "array"); t1.push(123); t2.push(456); diff --git a/tolk-tester/tests/asm-arg-order.tolk b/tolk-tester/tests/asm-arg-order.tolk index 50812bb512..620e56e656 100644 --- a/tolk-tester/tests/asm-arg-order.tolk +++ b/tolk-tester/tests/asm-arg-order.tolk @@ -131,10 +131,8 @@ fun asmAPlus1TimesB(a: int, b: int): int fun int.plus1TimesB(self, b: int): int asm(b self) "1 ADDCONST MUL"; -@pure @noinline fun get2Pure() { return 2; } -@pure @noinline fun get10Pure() { return 10; } @@ -270,8 +268,9 @@ fun main() { @fif_codegen """ test28() PROC:<{ - get10Pure() CALLDICT get2Pure() CALLDICT + get10Pure() CALLDICT + SWAP 1 ADDCONST MUL }> """ @@ -316,5 +315,5 @@ fun main() { }> """ -@code_hash 98112237312599117361360505207413290833928615253348061305894339367683016107491 +@code_hash 34599016008274478064245614023722066100591789877753712714557731858383043392662 */ diff --git a/tolk-tester/tests/break-continue-tests.tolk b/tolk-tester/tests/break-continue-tests.tolk new file mode 100644 index 0000000000..5f79ff24df --- /dev/null +++ b/tolk-tester/tests/break-continue-tests.tolk @@ -0,0 +1,1255 @@ +fun main() {} + +// --- basic break --- + +@method_id(101) +fun break_immediate(): int { + var x = 0; + while (true) { + break; + x = 1; + } + return x; +} + +@method_id(102) +fun break_after_side_effect(): int { + var x = 0; + while (true) { + x = 7; + break; + x = 9; + } + return x; +} + +@method_id(103) +fun break_conditional(flag: int): int { + var x = 0; + while (x < 10) { + if (flag != 0) { + break; + } + x += 1; + } + return x; +} + +@method_id(104) +fun break_nested_if(n: int): int { + var i = 0; + while (i < 20) { + if (i > 3) { + if (i == n) { + break; + } + } + i += 1; + } + return i; +} + +@method_id(105) +fun break_nested_loops(): int { + var outer = 0; + var inner = 0; + while (outer < 5) { + inner = 0; + while (inner < 5) { + if (inner == 2) { + break; + } + inner += 1; + } + outer += 1; + } + return outer * 10 + inner; +} + +@method_id(106) +fun while_true_break_reaches_next(): int { + while (true) { + break; + } + return 42; +} + +// --- structural continue --- + +@method_id(107) +fun continue_direct(): int { + var i = 0; + var n = 0; + while (i < 5) { + i += 1; + continue; + n += 1; + } + return n * 10 + i; +} + +@method_id(108) +fun continue_one_guard(): int { + var i = 0; + var n = 0; + while (i < 6) { + i += 1; + if ((i & 1) == 0) { + continue; + } + n += 1; + } + return n; +} + +@method_id(109) +fun continue_two_guards(): int { + var i = 0; + var n = 0; + while (i < 10) { + i += 1; + if (i == 3) { + continue; + } + if (i == 7) { + continue; + } + n += i; + } + return n; +} + +@method_id(110) +fun continue_balanced_nested(): int { + var i = 0; + var n = 0; + while (i < 5) { + i += 1; + if (i == 2) { + continue; + } else { + if (i == 4) { + continue; + } else { + n += i; + } + } + } + return n; +} + +@method_id(111) +fun continue_match(x: int): int { + var i = 0; + var n = 0; + while (i < 4) { + i += 1; + match (i) { + 2 => { continue; } + else => { n += i; } + } + } + return n + x; +} + +@method_id(112) +fun continue_then_break(limit: int): int { + var i = 0; + var n = 0; + while (true) { + i += 1; + if (i == 2) { + continue; + } + n += i; + if (i >= limit) { + break; + } + } + return n; +} + +@method_id(113) +fun continue_then_return(limit: int): int { + var i = 0; + while (i < 10) { + i += 1; + if (i == 2) { + continue; + } + if (i == limit) { + return i * 10; + } + } + return -1; +} + +// --- do-while: continue evaluates cond, break does not --- + +global do_cond_hits: int; + +fun do_cond(n: int): bool { + do_cond_hits += 1; + return n < 3; +} + +@method_id(114) +fun do_while_continue(): int { + do_cond_hits = 0; + var n = 0; + do { + n += 1; + if (n == 1) { + continue; + } + } while (do_cond(n)); + return n * 10 + do_cond_hits; +} + +@method_id(115) +fun do_while_break(): int { + do_cond_hits = 0; + var n = 0; + do { + n += 1; + if (n == 1) { + break; + } + } while (do_cond(n)); + return n * 10 + do_cond_hits; +} + +@method_id(116) +fun do_while_fallthrough(): int { + do_cond_hits = 0; + var n = 0; + do { + n += 1; + } while (do_cond(n)); + return n * 10 + do_cond_hits; +} + +// --- repeat --- + +global repeat_count_hits: int; + +fun repeat_count(): int { + repeat_count_hits += 1; + return 3; +} + +@method_id(117) +fun repeat_count_once(): int { + repeat_count_hits = 0; + var n = 0; + repeat (repeat_count()) { + n += 1; + } + return n * 10 + repeat_count_hits; +} + +@method_id(118) +fun repeat_zero(): int { + var n = 0; + repeat (0) { + n += 1; + break; + } + return n; +} + +@method_id(119) +fun repeat_negative(): int { + var n = 0; + repeat (-5) { + n += 1; + break; + } + return n; +} + +@method_id(120) +fun repeat_continue_consumes(): int { + var n = 0; + var skipped = 0; + repeat (4) { + n += 1; + if (n == 2) { + skipped += 1; + continue; + } + skipped += 10; + } + return n * 100 + skipped; +} + +@method_id(121) +fun repeat_break_stops(): int { + var n = 0; + repeat (10) { + n += 1; + if (n == 3) { + break; + } + } + return n; +} + +// --- return is allowed when the surrounding loop has no break --- + +@method_id(122) +fun inner_break_then_outer_return(): int { + var n = 0; + while (n < 3) { + while (true) { + n += 1; + break; + } + if (n == 2) { + return 90; + } + } + return 91; +} + +@method_id(125) +fun return_from_try_in_loop(flag: int): int { + var i = 0; + while (i < 3) { + try { + if (flag != 0) { + return 55; + } + } catch (excNo, arg) { + return 66; + } + i += 1; + } + return 77; +} + +// --- FlowContext smart casts --- + +@method_id(128) +fun flow_break_nullable(x: int?, flag: int): int { + while (x != null) { + __expect_type(x, "int"); + if (flag != 0) { + break; + } + x = null; + } + __expect_type(x, "int?"); + return x == null ? 0 : x; +} + +@method_id(130) +fun break_nested_match(n: int): int { + var i = 0; + while (i < 20) { + match (i == n) { + true => { break; } + else => { i += 1; } + } + } + return i; +} + +@method_id(131) +fun continue_match_implicit_else(): int { + var i = 0; + var n = 0; + while (i < 4) { + i += 1; + match (i) { + 2 => { continue; } + } + n += i; + } + return n; +} + +@method_id(132) +fun continue_then_throw(flag: int): int { + var i = 0; + while (i < 5) { + i += 1; + if (i == 2) { + continue; + } + if (flag != 0 && i == 3) { + throw 99; + } + } + return i; +} + +fun generic_break(x: T): T { + while (true) { + break; + } + return x; +} + +@method_id(133) +fun test_generic_break(): int { + return generic_break(7); +} + +@inline +fun add1(x: int): int { + return x + 1; +} + +@method_id(134) +fun inline_inside_breakable(): int { + while (true) { + var x = add1(10); + if (x == 11) { + break; + } + } + return 11; +} + +@method_id(136) +fun continue_nested_loops(): int { + var outer = 0; + var n = 0; + while (outer < 3) { + outer += 1; + var inner = 0; + while (inner < 3) { + inner += 1; + if (inner == 2) { + continue; + } + n += 1; + } + n += 10; + } + return n; +} + +@method_id(129) +fun flow_continue_nullable(x: int?, flag: int): int { + var n = 0; + while (x != null) { + __expect_type(x, "int"); + if (flag != 0) { + flag = 0; + continue; + } + n += x; + x = null; + } + __expect_type(x, "null"); + return n; +} + +@method_id(140) +fun again_then_return(x: int): int { + if (x > 0) { + while (true) { + if (x == 1) { + break; + } + x -= 1; + } + return 10; + } + return 20; +} + +@method_id(141) +fun again_late_init_break(): int { + var x: int; + while (true) { + x = 1; + break; + } + return x; +} + +@method_id(142) +fun continue_after_not(flag: int): int { + var x = 0; + var ok = flag != 0; + repeat (2) { + if (!ok) { + continue; + } + x += 1; + } + return x; +} + +@method_id(144) +fun do_while_continue_or_break(flag: int): int { + do_cond_hits = 0; + var n = 0; + do { + n += 1; + if (flag != 0) { + continue; + } else { + break; + } + } while (do_cond(n)); + return n * 10 + do_cond_hits; +} + +fun generic_continue(x: T, skip: int): T { + var i = 0; + while (i < 2) { + i += 1; + if (skip != 0) { + continue; + } + } + return x; +} + +@method_id(145) +fun test_generic_continue(): int { + return generic_continue(7, 1); +} + +@inline +fun skip_neg(x: int): int { + if (x < 0) { + return 0; + } + return x; +} + +@method_id(146) +fun continue_with_inline_guard(): int { + var i = 0; + var n = 0; + while (i < 4) { + i += 1; + if (skip_neg(i - 3) == 0) { + continue; + } + n += i; + } + return n; +} + +@method_id(148) +fun continue_then_terminal(a: int, b: int): int { + var n = 0; + repeat (2) { + if (a != 0) { + if (b != 0) { + continue; + } + break; + } else { + n += 1; + } + n += 10; + } + return n; +} + +@method_id(149) +fun again_brk_backedge(n: int): int { + var x = n; + var done = 0; + while (true) { + if (x <= 0) { + done = 1; + break; + } + x -= 1; + } + return x * 10 + done; +} + +@method_id(150) +fun while_brk_const_tags(flag: int): (int, int) { + var x = 5; + var i = 0; + while (i < 1) { + if (flag != 0) { + x = 9; + break; + } + i += 1; + } + return (x, 5); +} + +@method_id(151) +fun until_brk_const_tags(flag: int): (int, int) { + var x = 5; + var i = 0; + do { + if (flag != 0) { + x = 9; + break; + } + i += 1; + } while (i < 1); + return (x, 5); +} + +@method_id(154) +fun both_arms_terminal_then_continue(a: int): int { + var n = 0; + repeat (3) { + if (a != 0) { + break; + } else { + throw 1; + } + if (true) { + continue; + } + n += 10; + } + return n; +} + +@method_id(155) +fun match_break_no_continue(a: int, x: int): int { + var n = 0; + while (true) { + if (a != 0) { + match (x) { + 0 => { break; } + } + } else { + n += 1; + } + n += 10; + break; + } + return n; +} + +@method_id(157) +fun nested_both_breakable(): int { + var outer = 0; + var inner = 0; + while (outer < 3) { + inner = 0; + while (inner < 5) { + if (inner == 2) { + break; + } + inner += 1; + } + if (outer == 1) { + break; + } + outer += 1; + } + return outer * 10 + inner; +} + +@inline +fun early_ret_helper(x: int): int { + if (x < 0) { + return 0; + } + return x; +} + +@method_id(158) +fun inline_helper_in_breakable(x: int): int { + var n = 0; + while (true) { + n = early_ret_helper(x); + break; + } + return n; +} + +@method_id(159) +fun try_then_break(flag: int): int { + var n = 0; + while (true) { + try { + if (flag != 0) { + throw 1; + } + n = 7; + } catch { + n = 8; + } + break; + } + return n; +} + +fun add3_ref(x: int): int { + return x + 3; +} + +@method_id(160) +fun again_funref_callxargs(): int { + var cb: (int) -> int = add3_ref; + var x = 1; + while (true) { + x = cb(x); + break; + } + return x; +} + +@method_id(161) +fun again_cond_lateinit(n: int, seed: int): int { + var done: int; + var x = n; + while (true) { + if (x <= 0) { + done = seed + x; + break; + } + x -= 1; + } + return done; +} + +// --- lazy match: same FallthroughTail routing as a regular match --- + +struct (0x00) LazyBcA { a: int8 } +struct (0x10) LazyBcB { b: int8 } +type LazyBcU = LazyBcA | LazyBcB + +@method_id(162) +fun lazy_continue_one_arm(flag: int): int { + var n = 0; + var i = 0; + while (i < 3) { + i += 1; + val s = (i == 2) == (flag != 0) ? "0005".hexToSlice() : "1007".hexToSlice(); + var x = lazy LazyBcU.fromSlice(s); + __expect_lazy("[x] lazy match"); + match (x) { + LazyBcA => { continue; } + LazyBcB => { n += x.b; } + } + n += 10; + } + return n; +} + +@method_id(163) +fun lazy_break_one_arm(flag: int): int { + var n = 0; + while (true) { + val s = flag != 0 ? "0005".hexToSlice() : "1007".hexToSlice(); + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { + n = x.a; + break; + } + LazyBcB => { n = x.b; } + } + n += 100; + break; + } + return n; +} + +@method_id(164) +fun lazy_continue_then_break(): int { + var i = 0; + var n = 0; + while (true) { + i += 1; + val s = i < 3 ? "0005".hexToSlice() : "1007".hexToSlice(); + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { + n += 1; + continue; + } + LazyBcB => { + n += 10; + break; + } + } + } + return n * 10 + i; +} + +@method_id(165) +fun lazy_else_break(flag: int): int { + var n = 0; + while (true) { + val s = flag != 0 ? "0005".hexToSlice() : "20FF".hexToSlice(); + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { + n = x.a; + break; + } + LazyBcB => { + n = x.b; + break; + } + else => { + n = 99; + break; + } + } + } + return n; +} + +@method_id(166) +fun lazy_else_continue(): int { + var i = 0; + var n = 0; + while (i < 3) { + i += 1; + val s = i == 2 ? "20FF".hexToSlice() : "0005".hexToSlice(); + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { n += x.a; } + LazyBcB => { continue; } + else => { continue; } + } + n += 10; + } + return n; +} + +@inline +fun lazy_inline_read(s: slice): int { + __expect_inline(true); + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { return x.a; } + LazyBcB => { return x.b; } + else => { return -1; } + } +} + +@method_id(167) +fun loop_calls_inline_lazy(kind: int): int { + var n = 0; + var i = 0; + while (i < 3) { + i += 1; + val s = kind == 0 ? "0005".hexToSlice() : kind == 1 ? "1007".hexToSlice() : "20FF".hexToSlice(); + val v = lazy_inline_read(s); + if (v < 0) { + continue; + } + n += v; + break; + } + return n * 10 + i; +} + +@inline +fun lazy_inline_skip_a(sA: slice, sB: slice): int { + __expect_inline(true); + var n = 0; + var i = 0; + while (i < 2) { + i += 1; + val s = i == 1 ? sA : sB; + var x = lazy LazyBcU.fromSlice(s); + match (x) { + LazyBcA => { continue; } + LazyBcB => { n += x.b; } + } + n += 10; + } + return n; +} + +@method_id(168) +fun test_lazy_inline_continue(): int { + return lazy_inline_skip_a("0005".hexToSlice(), "1007".hexToSlice()); +} + +@method_id(171) +fun break_from_match_expression(x: int): int { + var result = 10; + while (true) { + result = match (x) { + 0 => { + break; + 20 + } + else => 30 + }; + break; + } + return result; +} + +@method_id(172) +fun flow_merge_reachable_breaks(flag: int): int { + var x: int; + while (true) { + if (flag == 1) { + x = 4; + break; + } + if (flag == 2) { + x = 7; + break; + } + throw 100; + break; + } + return x; +} + +@method_id(173) +fun flow_continue_into_do_condition(flag: bool): int { + var x: int; + do { + if (flag) { + x = 4; + continue; + } + x = 7; + } while (x < 0); + return x; +} + +// An all-terminal inner `if` must not be counted as a fallthrough path. +@method_id(174) +fun continue_then_all_terminal_if(outer: int, skip: int, exitKind: int): int { + var n = 0; + repeat (3) { + n += 1; + if (outer != 0) { + n += 10; + if (skip != 0) { + continue; + } + if (exitKind != 0) { + n += 1000; + break; + } else { + n += 2000; + break; + } + } else { + n += 100; + if (skip != 0) { + continue; + } + if (exitKind != 0) { + n += 3000; + break; + } else { + n += 4000; + break; + } + } + } + return n; +} + +// An exhaustive all-terminal inner `match` must not be counted as a live arm. +@method_id(175) +fun continue_then_all_terminal_match(outer: int, skip: int, exitKind: int): int { + var n = 0; + repeat (3) { + n += 1; + match (outer) { + 0 => { + n += 10; + if (skip != 0) { + continue; + } + match (exitKind) { + 0 => { + n += 1000; + break; + } + else => { + n += 2000; + break; + } + } + } + else => { + n += 100; + if (skip != 0) { + continue; + } + match (exitKind) { + 0 => { + n += 3000; + break; + } + else => { + n += 4000; + break; + } + } + } + } + } + return n; +} + +// A break from the nested loop must reach the return materialized at the end of the outer IF branch. +@method_id(176) +fun breakable_loop_in_if_before_return(flag: int): int { + var n = 0; + if (flag != 0) { + while (true) { + n = 7; + break; + } + } else { + n = 9; + } + return n; +} + +@inline +fun returnOrLoopForever(c: bool): int { + if (c) { + return -1; + } + while (true) { + } + return 1; +} + +@method_id(177) +fun testReturnPath(c: bool): int { + return returnOrLoopForever(c); +} + + +@method_id(178) +fun while_break_before_unused_copy(v: int): int { + while (v > 10) { + break; + } + var w = v; + return 0; +} + +/** +@testcase | 101 | | 0 +@testcase | 102 | | 7 +@testcase | 103 | 1 | 0 +@testcase | 103 | 0 | 10 +@testcase | 104 | 6 | 6 +@testcase | 105 | | 52 +@testcase | 106 | | 42 +@testcase | 107 | | 5 +@testcase | 108 | | 3 +@testcase | 109 | | 45 +@testcase | 110 | | 9 +@testcase | 111 | 0 | 8 +@testcase | 112 | 4 | 8 +@testcase | 113 | 4 | 40 +@testcase | 114 | | 33 +@testcase | 115 | | 10 +@testcase | 116 | | 33 +@testcase | 117 | | 31 +@testcase | 118 | | 0 +@testcase | 119 | | 0 +@testcase | 120 | | 431 +@testcase | 121 | | 3 +@testcase | 122 | | 90 +@testcase | 125 | 1 | 55 +@testcase | 125 | 0 | 77 +@testcase | 128 | 5 1 | 5 +@testcase | 128 | 5 0 | 0 +@testcase | 129 | 5 1 | 5 +@testcase | 129 | 5 0 | 5 +@testcase | 130 | 6 | 6 +@testcase | 131 | | 8 +@testcase | 132 | 0 | 5 +@testcase | 133 | | 7 +@testcase | 134 | | 11 +@testcase | 136 | | 36 +@testcase | 140 | 3 | 10 +@testcase | 140 | 1 | 10 +@testcase | 140 | 0 | 20 +@testcase | 141 | | 1 +@testcase | 142 | 0 | 0 +@testcase | 142 | 1 | 2 +@testcase | 144 | 1 | 33 +@testcase | 144 | 0 | 10 +@testcase | 145 | | 7 +@testcase | 146 | | 4 +@testcase | 148 | 1 1 | 0 +@testcase | 148 | 1 0 | 0 +@testcase | 148 | 0 0 | 22 +@testcase | 149 | 2 | 1 +@testcase | 150 | 1 | 9 5 +@testcase | 150 | 0 | 5 5 +@testcase | 151 | 1 | 9 5 +@testcase | 151 | 0 | 5 5 +@testcase | 154 | 1 | 0 +@testcase | 155 | 1 0 | 0 +@testcase | 155 | 1 1 | 10 +@testcase | 155 | 0 0 | 11 +@testcase | 157 | | 12 +@testcase | 158 | 5 | 5 +@testcase | 158 | -3 | 0 +@testcase | 159 | 0 | 7 +@testcase | 159 | 1 | 8 +@testcase | 160 | | 4 +@testcase | 161 | 0 7 | 7 +@testcase | 161 | 2 7 | 7 +@testcase | 161 | -3 7 | 4 +@testcase | 162 | 1 | 34 +@testcase | 162 | 0 | 17 +@testcase | 163 | 1 | 5 +@testcase | 163 | 0 | 107 +@testcase | 164 | | 123 +@testcase | 165 | 1 | 5 +@testcase | 165 | 0 | 99 +@testcase | 166 | | 30 +@testcase | 167 | 0 | 51 +@testcase | 167 | 1 | 71 +@testcase | 167 | 2 | 3 +@testcase | 168 | | 17 +@testcase | 171 | 0 | 10 +@testcase | 171 | 1 | 30 +@testcase | 172 | 1 | 4 +@testcase | 172 | 2 | 7 +@testcase | 173 | 0 | 7 +@testcase | 173 | 1 | 4 +@testcase | 174 | 1 1 0 | 33 +@testcase | 174 | 0 1 0 | 303 +@testcase | 174 | 1 0 1 | 1011 +@testcase | 174 | 1 0 0 | 2011 +@testcase | 174 | 0 0 1 | 3101 +@testcase | 174 | 0 0 0 | 4101 +@testcase | 175 | 0 1 0 | 33 +@testcase | 175 | 1 1 0 | 303 +@testcase | 175 | 0 0 0 | 1011 +@testcase | 175 | 0 0 1 | 2011 +@testcase | 175 | 1 0 0 | 3101 +@testcase | 175 | 1 0 1 | 4101 +@testcase | 176 | 0 | 9 +@testcase | 176 | 1 | 7 +@testcase | 177 | 1 | -1 +@testcase | 178 | 5 | 0 +@testcase | 178 | 15 | 0 + +@fif_codegen +""" +break_immediate() PROC:<{ + ... + AGAINBRK:<{ + ... + RETALT + ... +}> +""" + +@fif_codegen +""" +continue_one_guard() PROC:<{ + ... + WHILE:<{ + ... +}> +""" + +@fif_codegen_avoid _Continue +@fif_codegen_avoid Kcontinue + +@fif_codegen_avoid JMPX +@fif_codegen_avoid +""" +break_immediate() PROC:<{ + c2 SAVE +""" + +@fif_codegen +""" +break_conditional() PROC:<{ + ... + WHILEBRK:<{ + ... + RETALT + ... +}> +""" + +@fif_codegen +""" +while_brk_const_tags() PROC:<{ + ... + WHILEBRK:<{ + ... +}> +""" + +@fif_codegen +""" +until_brk_const_tags() PROC:<{ + ... + UNTILBRK:<{ + ... +}> +""" + +@fif_codegen +""" +do_while_break() PROC:<{ + ... + UNTILBRK:<{ + ... + RETALT + ... +}> +""" + +@fif_codegen +""" +repeat_break_stops() PROC:<{ + ... + REPEATBRK:<{ + ... + RETALT + ... +}> +""" + +@fif_codegen +""" +repeat_continue_consumes() PROC:<{ + ... + REPEAT:<{ + ... +}> +""" + +@fif_codegen +""" +do_while_continue() PROC:<{ + ... + UNTIL:<{ + ... +}> +""" + +@fif_codegen +""" +breakable_loop_in_if_before_return() PROC:<{ + IFJMP:<{ + AGAINBRK:<{ + 7 PUSHINT + RETALT + }> + }> + 9 PUSHINT +}> +""" +*/ diff --git a/tolk-tester/tests/build-addr-tests.tolk b/tolk-tester/tests/build-addr-tests.tolk index 49af0a6455..212607be49 100644 --- a/tolk-tester/tests/build-addr-tests.tolk +++ b/tolk-tester/tests/build-addr-tests.tolk @@ -304,7 +304,7 @@ fun test15() { (any as address) == addr, any == (addr as any_address), (any as slice).bitsEqual(addr as slice), (any as bits123 as any_address as slice).remainingBitsCount(), - (addr as bytes1 as address as slice).remainingBitsCount(), + (addr as bits8 as address as slice).remainingBitsCount(), ) } diff --git a/tolk-tester/tests/bytesN-tests.tolk b/tolk-tester/tests/bytesN-tests.tolk index cefb0cca00..2c34d607e3 100644 --- a/tolk-tester/tests/bytesN-tests.tolk +++ b/tolk-tester/tests/bytesN-tests.tolk @@ -1,14 +1,14 @@ fun takeAnySlice(slice: slice) {} fun getSomeSlice() { return beginCell().endCell().beginParse(); } -fun getNullableBytes16(): bytes16? { return getSomeSlice() as bytes16; } +fun getNullableBytes16(): bits128? { return getSomeSlice() as bits128; } -const someBytes = "asdf".literalSlice() as bytes16; -const anotherBytes: bytes16 = "asdf".literalSlice() as bytes16; +const someBits = "asdf".literalSlice() as bits128; +const anotherBytes: bits128 = "asdf".literalSlice() as bits128; fun autoInferBytes16() { - if (random.uint256() != 0) { return someBytes; } - else if (random.uint256() != 0) { return true ? "".beginParse() as bytes16 : anotherBytes; } - else { return someBytes!; } + if (random.uint256() != 0) { return someBits; } + else if (random.uint256() != 0) { return true ? "".beginParse() as bits128 : anotherBytes; } + else { return someBits!; } } @method_id(101) @@ -17,9 +17,9 @@ fun test1() { __expect_type(ss, "bits32"); __expect_type(ss as slice, "slice"); __expect_type(ss as slice as slice?, "slice?"); - __expect_type(ss as bytes128, "bytes128"); - __expect_type(someBytes, "bytes16"); - __expect_type(10>3 ? (null, ss as bits1) : (ss as bytes1, null), "(bytes1?, bits1?)"); + __expect_type(ss as bits1023, "bits1023"); + __expect_type(someBits, "bits128"); + __expect_type(10>3 ? (null, ss as bits1) : (ss as bits8, null), "(bits8?, bits1?)"); var s = ss as slice; return (s.remainingBitsCount(), s.loadInt(32), s.loadInt(32), (ss as slice).remainingBitsCount()); } diff --git a/tolk-tester/tests/calls-tests.tolk b/tolk-tester/tests/calls-tests.tolk index f183f416bf..2b95fc033d 100644 --- a/tolk-tester/tests/calls-tests.tolk +++ b/tolk-tester/tests/calls-tests.tolk @@ -200,14 +200,11 @@ fun test12() { catch (ex) { return ex; } } -@pure fun int.increment(mutate self) { self += 1 } -@pure fun int.clone(self): int { return self } global g113: int -@pure fun pure113(mutate p113: int) { var l113 = 0; l113.increment(); diff --git a/tolk-tester/tests/cells-slices.tolk b/tolk-tester/tests/cells-slices.tolk index 394a57e33f..c551726d93 100644 --- a/tolk-tester/tests/cells-slices.tolk +++ b/tolk-tester/tests/cells-slices.tolk @@ -280,7 +280,7 @@ fun test19() { fun test20() { var x = false; var n = 4; - var b = true ? beginCell() : null; + var b = (true ? beginCell() : null)!; b.storeBool(true).storeBool(x); b = b.storeBool(true).storeUint(0, n *= 2).storeBool(!!true).storeCoins(0); var s = b.toSlice(); @@ -850,7 +850,7 @@ We test that consequtive storeInt/storeUint with constants are joined into a sin NEWC // x b SWAP // b x 0 GTINT // b '4 - IF:<{ // b + IFJMP:<{ // b 5 PUSHINT 8 STUR // b }> // b @@ -863,13 +863,12 @@ We test that consequtive storeInt/storeUint with constants are joined into a sin NEWC s3 POP 42 EQINT - IF:<{ + IFJMP:<{ SWAP b{000} STSLICECONST 8 STU // b - }>ELSE<{ - DROP // b }> + DROP // b }> """ diff --git a/tolk-tester/tests/constants-tests.tolk b/tolk-tester/tests/constants-tests.tolk index dbb11d777d..5957c16042 100644 --- a/tolk-tester/tests/constants-tests.tolk +++ b/tolk-tester/tests/constants-tests.tolk @@ -390,5 +390,5 @@ fun main() { @testcase | 126 | | 0 -1 @testcase | 127 | | 2000000000000000 0 -1 -@code_hash 81510367501635395392010839907601337703375917009254151502636239169360844553617 +@code_hash 32503099203396024790569736228115675124326770348191281279269235019931894432594 */ diff --git a/tolk-tester/tests/dicts-demo.tolk b/tolk-tester/tests/dicts-demo.tolk index cf662c6b0a..ab9190b3f1 100644 --- a/tolk-tester/tests/dicts-demo.tolk +++ b/tolk-tester/tests/dicts-demo.tolk @@ -1,10 +1,10 @@ import "@stdlib/tvm-dicts" -fun cell?.addIntToIDict(mutate self, key: int, number: int): void { +fun dict.addIntToIDict(mutate self, key: int, number: int): void { return self.iDictSetBuilder(32, key, beginCell().storeInt(number, 32)); } -fun calculateDictLen(d: cell?) { +fun calculateDictLen(d: dict) { var len = 0; var (k, v, f) = d.uDictGetFirst(32); while (f) { diff --git a/tolk-tester/tests/generics-1.tolk b/tolk-tester/tests/generics-1.tolk index ee516f25b8..6bf4a43433 100644 --- a/tolk-tester/tests/generics-1.tolk +++ b/tolk-tester/tests/generics-1.tolk @@ -110,7 +110,7 @@ fun test106() { fun X.callTupleFirst(self): Y { return self.first() as int; } fun callTupleFirst(t: X): Y { return t.first() as int; } -fun T.callTuplePush(mutate self, v1: V, v2: V): self { self.push(v1); tuplePush(mutate self, v2); return self; } +fun T.callTuplePush(mutate self, v1: V, v2: V): self { self.push(v1); var t: tuple = self; tuplePush(mutate t, v2); self = t; return self; } fun getTupleLastInt(t: tuple) { return t.last() as int; } fun getTupleSize(t: MTuple) { return t.size(); } fun callAnyFn(f: (TObj) -> TResult, arg: TObj) { return f(arg); } diff --git a/tolk-tester/tests/generics-2.tolk b/tolk-tester/tests/generics-2.tolk index 49fe253b53..6eb4630bf2 100644 --- a/tolk-tester/tests/generics-2.tolk +++ b/tolk-tester/tests/generics-2.tolk @@ -106,24 +106,20 @@ fun test2(c: Wrapper) { return (c, c2, c3, c4); } -@pure @noinline fun getWrappervalue1(c: Wrapper) { return c.value; } -@pure fun Wrapper.getWrappervalue1(self) { return self.value; } -@pure @noinline fun getWrappervalue2(c: T) { return c.value; } -@pure fun T.getWrappervalue2(self) { return self.value; } @@ -378,7 +374,7 @@ fun test19() { var i2: StrangeInt = 30; var i3: StrangeInt> = 40; i1 = i2 as StrangeInt; i1 = i3 as StrangeInt; - return (i1 as StrangeInt<()>, i3 as StrangeAlsoInt, i1 is StrangeInt, i1 is StrangeInt, i3 is StrangeInt, i3 is StrangeAlsoInt); + return (i1 as StrangeInt<()>, i3 as StrangeAlsoInt, i1 is StrangeInt, i3 is StrangeInt); } @method_id(120) @@ -419,12 +415,12 @@ fun test21(w: AnotherWrapper) { fun test22(w: int | WrapperAlias | slice) { match (w) { int => {} - WrapperAlias => {} + WrapperAlias => {} slice => {} } __expect_type(w, "int | WrapperAlias | slice"); match (w) { - MyInt => {} + int => {} slice => {} WrapperAlias => {} } @@ -573,7 +569,7 @@ fun getValueOfAlias(w: WrapperAlias): T { return w.value } fun test31(w1: Wrapper, w2: WrapperAlias) { __expect_type(getValueOfAlias(w1), "int"); __expect_type(getValueOfAlias(w2), "int"); - __expect_type((w1).getValueOfAlias(), "int"); + __expect_type((w1 as WrapperAlias).getValueOfAlias(), "int"); __expect_type((w2).getValueOfAlias(), "int"); } @@ -597,8 +593,8 @@ fun main(c: Wrapper, d: WrappedInt) { __expect_type(d.value, "int"); __expect_type(Wrapper { value: 200 } as WrappedInt, "WrappedInt"); __expect_type(Wrapper{ value: c.value }, "Wrapper"); - __expect_type(Wrapper{ value: c.value as bytes32 }, "Wrapper"); - __expect_type(Wrapper{ value: Wrapper { value: (c.value as bytes32 as bytes32?)! }.value }, "Wrapper"); + __expect_type(Wrapper{ value: c.value as bits256 }, "Wrapper"); + __expect_type(Wrapper{ value: Wrapper { value: (c.value as bits256 as bits256?)! }.value }, "Wrapper"); __expect_type(Wrapper{ value: c.value }, "Wrapper"); __expect_type(Wrapper{ value: c.value.loadInt(32) }, "Wrapper"); __expect_type(Wrapper{ value: 10 }, "Wrapper"); @@ -614,27 +610,27 @@ fun main(c: Wrapper, d: WrappedInt) { @testcase | 104 | | 11 11 13 13 13 2 @testcase | 105 | 0 | 80 @testcase | 105 | -1 | (null) -@testcase | 106 | | (null) (null) 0 777 1 2 typeid-17 +@testcase | 106 | | (null) (null) 0 777 1 2 typeid-1 @testcase | 107 | 5 | 20 20 5 5 20 777 (null) (null) (null) 0 -@testcase | 108 | 0 | 777 typeid-1 777 typeid-1 +@testcase | 108 | 0 | 777 typeid-2 777 typeid-2 @testcase | 108 | -1 | 777 0 777 0 @testcase | 109 | | 40 40 70 @testcase | 110 | | 20 1 20 42 -@testcase | 111 | | 5 1 typeid-2 5 1 typeid-2 5 1 typeid-2 5 1 typeid-2 -@testcase | 112 | -1 | 10 1 777 10 1 typeid-3 -@testcase | 112 | 0 | 20 1 777 (null) 0 typeid-4 +@testcase | 111 | | 5 1 typeid-3 5 1 typeid-3 5 1 typeid-3 5 1 typeid-3 +@testcase | 112 | -1 | 10 1 777 10 1 typeid-4 +@testcase | 112 | 0 | 20 1 777 (null) 0 typeid-5 @testcase | 113 | | 30 -1 @testcase | 114 | | 999 (null) 2 @testcase | 115 | | 10 0 200 1 -1 200 1 -1 @testcase | 116 | | 1 2 3 4 @testcase | 117 | | 100 123 @testcase | 118 | | 123 1 777 123 1 -1 2 -@testcase | 119 | | 40 40 -1 0 -1 0 -@testcase | 120 | | (null) typeid-9 777 (null) (null) 0 typeid-10 -1 -1 +@testcase | 119 | | 40 40 -1 -1 +@testcase | 120 | | (null) typeid-10 777 (null) (null) 0 typeid-11 -1 -1 @testcase | 123 | | 10 10 10 10 10 -@testcase | 126 | | 0 0 0 (null) 777 0 typeid-12 (null) typeid-12 (null) 0 +@testcase | 126 | | 0 0 0 (null) 777 0 typeid-13 (null) typeid-13 (null) 0 @testcase | 128 | | 0 -1 0 -1 -1 -@testcase | 129 | | -1 typeid-15 (null) typeid-16 +@testcase | 129 | | -1 typeid-14 (null) typeid-15 @testcase | 132 | | [ [ 2 3 ] 4 ] diff --git a/tolk-tester/tests/generics-3.tolk b/tolk-tester/tests/generics-3.tolk index bd3a4c0cec..9174fe7320 100644 --- a/tolk-tester/tests/generics-3.tolk +++ b/tolk-tester/tests/generics-3.tolk @@ -10,7 +10,6 @@ type OkAlias = Ok; type Response = Ok | Err; -@pure fun getResponse(success: bool): Response { return success ? Ok { result: 10 } : Err { errPayload: beginCell().storeInt(-1,32).endCell().beginParse() }; } @@ -30,7 +29,7 @@ fun test2() { Err => r as Err, } match (r) { - OkAlias => r as Ok, + Ok => r as OkAlias, Err => {} } match (r) { @@ -40,8 +39,7 @@ fun test2() { if (r is Ok) { __expect_type(r, "Ok"); } if (r is Ok) { __expect_type(r, "Ok"); } - if (r is OkAlias) { __expect_type(r, "OkAlias"); } - if (r !is OkAlias) { __expect_type(r, "Err"); } + if (r !is Ok) { __expect_type(r, "Err"); } if (r is Ok && r !is Err && r is Ok) { __expect_type(r, "Ok"); } } @@ -66,7 +64,7 @@ fun test4() { __expect_type(r2, "Err"); r2 = 10>3 ? Ok { result: Ok { result: 12 } } : r2; var mm1 = match (r2) { - OkAlias> => true, + Ok> => true, Err => false }; var mm2 = match (r2) { @@ -80,7 +78,7 @@ fun test4() { fun test5() { var o1: Ok = { result: 1 }; var o2: OkAlias = { result: 2 }; - return (o1 is Ok, o1 is OkAlias, o2 is Ok, o2 is OkAlias, o1 is Ok, o1 is OkAlias); + return (o1 is Ok, o2 is Ok, o2 is OkAlias, o1 is Ok, o1 is OkAlias); } type OkInt = Ok; @@ -92,7 +90,7 @@ fun test6(w: Response | int) { Err => {} } match (w) { - OkInt => {} + Ok => {} int => {} Err => {} } @@ -100,7 +98,7 @@ fun test6(w: Response | int) { fun test7(w: OkInt | Err | int) { match (w) { - Ok => {} + OkInt => {} Err => {} int => {} } @@ -131,12 +129,14 @@ fun main() { @testcase | 101 | 0 | -1 @testcase | 103 | | 10 -1 0 @testcase | 104 | | 12 typeid-1 typeid-4 -1 -1 -1 0 0 -1 0 0 -@testcase | 105 | | -1 -1 -1 -1 0 0 +@testcase | 105 | | -1 -1 -1 0 0 @testcase | 108 | | 50000000 (null) (null) 100000000 12 12 24 @fif_codegen """ test3() PROC:<{ + x{} PUSHSLICE + DROP 10 PUSHINT // r.USlot1=10 -1 PUSHINT // r.USlot1=10 '22=-1 FALSE // r.USlot1=10 '22=-1 '24 diff --git a/tolk-tester/tests/generics-4.tolk b/tolk-tester/tests/generics-4.tolk index 9089f68c0a..32bb3cf374 100644 --- a/tolk-tester/tests/generics-4.tolk +++ b/tolk-tester/tests/generics-4.tolk @@ -289,14 +289,14 @@ fun main() { """ test6() PROC:<{ // PUSHREF // p.init.USlot2 - -1 PUSHINT // p.init.USlot2 '11=-1 - 0 PUSHINT // p.init.USlot2 '11=-1 '12=0 - TRUE // p.init.USlot2 '11=-1 '12=0 '13 - s0 s3 XCHG // '13 '11=-1 '12=0 p.init.USlot2 - CDEPTH // '13 '11=-1 '12=0 '18 - 0 EQINT // '13 '11=-1 '12=0 '20 - 0 NEQINT // '13 '11=-1 '12=0 '17 - s1 s3 s0 XCHG3 // '11=-1 '12=0 '13 '17 + -1 PUSHINT // p.init.USlot2 '12=-1 + 0 PUSHINT // p.init.USlot2 '12=-1 '13=0 + TRUE // p.init.USlot2 '12=-1 '13=0 '14 + s0 s3 XCHG // '14 '12=-1 '13=0 p.init.USlot2 + CDEPTH // '14 '12=-1 '13=0 '19 + 0 EQINT // '14 '12=-1 '13=0 '21 + 0 NEQINT // '14 '12=-1 '13=0 '18 + s1 s3 s0 XCHG3 // '12=-1 '13=0 '14 '18 }> """ */ diff --git a/tolk-tester/tests/handle-msg-2.tolk b/tolk-tester/tests/handle-msg-2.tolk index 2f8a8b29f5..0424f45d68 100644 --- a/tolk-tester/tests/handle-msg-2.tolk +++ b/tolk-tester/tests/handle-msg-2.tolk @@ -4,8 +4,8 @@ fun test1() { return 0; } -fun onInternalMessage(in: InMessage) { - return in.valueExtra; +fun onInternalMessage(msg: InMessage) { + return msg.valueExtra; } /** diff --git a/tolk-tester/tests/handle-msg-3.tolk b/tolk-tester/tests/handle-msg-3.tolk index 41d3c1734b..ceebad4af5 100644 --- a/tolk-tester/tests/handle-msg-3.tolk +++ b/tolk-tester/tests/handle-msg-3.tolk @@ -4,9 +4,9 @@ fun test1() { return 0; } -fun onBouncedMessage(in: InMessageBounced) { - in.bouncedBody.skipBouncedPrefix(); - throw in.bouncedBody.loadUint(32); +fun onBouncedMessage(bounced: InMessageBounced) { + bounced.bouncedBody.skipBouncedPrefix(); + throw bounced.bouncedBody.loadUint(32); } fun onInternalMessage(in: InMessage) { diff --git a/tolk-tester/tests/handle-msg-5.tolk b/tolk-tester/tests/handle-msg-5.tolk index d5a30f8f98..c3c53712d0 100644 --- a/tolk-tester/tests/handle-msg-5.tolk +++ b/tolk-tester/tests/handle-msg-5.tolk @@ -30,7 +30,7 @@ fun onInternalMessage(in: InMessage) { onInternalMessage() PROC:<{ // in.body INMSG_BOUNCED // in.body '1 IFJMP:<{ // in.body - onBouncedMessage() CALLDICT // + onBouncedMessage() CALLDICT }> // in.body DROP // INMSG_VALUE // '3 diff --git a/tolk-tester/tests/handle-msg-6.tolk b/tolk-tester/tests/handle-msg-6.tolk index 57668f2bf5..66b3526459 100644 --- a/tolk-tester/tests/handle-msg-6.tolk +++ b/tolk-tester/tests/handle-msg-6.tolk @@ -5,6 +5,7 @@ fun test1() { } fun onBouncedMessage(in: InMessageBounced) { + __expect_inline(true); if (!in.valueExtra.isEmpty()) { return; } @@ -18,30 +19,17 @@ fun onInternalMessage(in: InMessage) { /** @testcase | 101 | | 0 -@fif_codegen -""" - onBouncedMessage() PROC:<{ - DROP - INMSG_VALUEEXTRA - DICTEMPTY - IFNOTJMP:<{ - }> - INMSG_FWDFEE - 0 PUSHINT - GETORIGINALFWDFEE - THROWANY - }> -""" +@fif_codegen_avoid DECLPROC onBouncedMessage() @fif_codegen """ onInternalMessage() PROC:<{ + DROP INMSG_BOUNCED IFJMP:<{ - onBouncedMessage() CALLDICT - }> - DROP - INMSG_SRC - }> + INMSG_VALUEEXTRA + DICTEMPTY + IFJMP:<{ + INMSG_FWDFEE """ */ diff --git a/tolk-tester/tests/if-else-tests.tolk b/tolk-tester/tests/if-else-tests.tolk index e019bd9ced..77c9e7b783 100644 --- a/tolk-tester/tests/if-else-tests.tolk +++ b/tolk-tester/tests/if-else-tests.tolk @@ -1,5 +1,6 @@ global t: tuple; + @method_id(101) fun test1(x: int): int { if (x > 200) { @@ -39,7 +40,6 @@ fun elseif(cond: int) { } } -@inline @method_id(104) fun test4(x: int): int { if (x==1) { @@ -74,6 +74,7 @@ fun test7() { // here we test that no "missing return" error } +@noinline fun withNoElse(op: int): int { if (op == 123) { return 100 } if (op == 234) { return 200 } @@ -81,6 +82,7 @@ fun withNoElse(op: int): int { throw 0xFF } +@noinline fun withElse(op: int): int { if (op == 123) { return 100 } else if (op == 234) { return 200 } @@ -88,6 +90,7 @@ fun withElse(op: int): int { return 0xFF } +@noinline fun withMatch(op: int): int { match (op) { 123 => return 100, @@ -207,7 +210,7 @@ fun demo12_3(x: int) { } fun demo12_4(x: int) { - __expect_inline(false); + __expect_inline(true); if (x != -100) { if (x > 0) { t.push(123); } return; @@ -353,6 +356,31 @@ fun test23(x: int): int { return chooseAsmIfElse(x); } +@method_id(124) +fun test24(c: int): int { + var r = 0; + if (c > 10) { r = 1 } // will transform to IFJMP + else { r = 2 } + return r; +} + +@method_id(125) +fun test25(a: int, b: int): int { + var r = 0; + if (a != 0) { + if (b != 0) { r = 1 } + else { r = 2 } + } + return r; +} + +@method_id(126) +fun test26(c: int): int { + var r = 0; + if (c == 0) { r = 1 } + else { r = 2 } + return r; +} fun main() { @@ -402,6 +430,13 @@ fun main() { @testcase | 122 | 7 | 14 @testcase | 123 | 0 | 2 @testcase | 123 | 4 | 1 +@testcase | 124 | 20 | 1 +@testcase | 124 | 5 | 2 +@testcase | 125 | 1 1 | 1 +@testcase | 125 | 1 0 | 2 +@testcase | 125 | 0 1 | 0 +@testcase | 126 | 0 | 1 +@testcase | 126 | 5 | 2 @fif_codegen """ @@ -557,21 +592,20 @@ fun main() { """ demo10_1() PROC:<{ DUP - 0 EQINT IFJMP:<{ - DROP - $t GETGLOB - 123 PUSHINT - TPUSH - $t SETGLOB - }> - 1 EQINT - IFJMP:<{ - $t GETGLOB - 456 PUSHINT - TPUSH - $t SETGLOB + 1 EQINT + IFJMP:<{ + $t GETGLOB + 456 PUSHINT + TPUSH + $t SETGLOB + }> }> + DROP + $t GETGLOB + 123 PUSHINT + TPUSH + $t SETGLOB }> """ @@ -626,10 +660,47 @@ fun main() { 4 ADDCONST }> SWAP - IF:<{ + IFJMP:<{ 8 ADDCONST }> }> """ +@fif_codegen +""" + test24() PROC:<{ + 10 GTINT + IFJMP:<{ + 1 PUSHINT + }> + 2 PUSHINT + }> +""" + +@fif_codegen +""" + test25() PROC:<{ + 0 PUSHINT + s0 s2 XCHG + IFJMP:<{ + NIP + IFJMP:<{ + 1 PUSHINT + }> + 2 PUSHINT + }> + DROP + }> +""" + +@fif_codegen +""" + test26() PROC:<{ + IFJMP:<{ + 2 PUSHINT + }> + 1 PUSHINT + }> +""" + */ diff --git a/tolk-tester/tests/imports-tests.tolk b/tolk-tester/tests/imports-tests.tolk index 8a421ae99f..5cd6ab39a6 100644 --- a/tolk-tester/tests/imports-tests.tolk +++ b/tolk-tester/tests/imports-tests.tolk @@ -15,6 +15,9 @@ fun main() { @fif_codegen """ main() PROC:<{ + NEWDICT + ... + DROP 3 PUSHINT }> """ diff --git a/tolk-tester/tests/imports/use-dicts.ext.tolk b/tolk-tester/tests/imports/use-dicts.ext.tolk index f4a0226755..d83c62798c 100644 --- a/tolk-tester/tests/imports/use-dicts.ext.tolk +++ b/tolk-tester/tests/imports/use-dicts.ext.tolk @@ -1,14 +1,14 @@ import "@stdlib/tvm-dicts" -fun prepareDict_3_30_4_40_5_x(valueAt5: int): cell? { - var dict: cell? = createEmptyDict(); +fun prepareDict_3_30_4_40_5_x(valueAt5: int): dict { + var dict: dict = createEmptyDict(); dict.iDictSetBuilder(32, 3, beginCell().storeInt(30, 32)); dict.iDictSetBuilder(32, 4, beginCell().storeInt(40, 32)); dict.iDictSetBuilder(32, 5, beginCell().storeInt(valueAt5, 32)); return dict; } -fun lookupIdxByValue(idict32: cell?, value: int): int { +fun lookupIdxByValue(idict32: dict, value: int): int { var cur_key: int? = -1; var cur_found: bool; do { diff --git a/tolk-tester/tests/indexed-access.tolk b/tolk-tester/tests/indexed-access.tolk index c9eb162cc9..1273003ccf 100644 --- a/tolk-tester/tests/indexed-access.tolk +++ b/tolk-tester/tests/indexed-access.tolk @@ -70,7 +70,7 @@ fun testCodegenSimple() { @method_id(101) fun test101() { - var t = (1, (2, 3), [4, 5, [6, 7]] as [int, int, [int, int]], 8); + var t = (1, (2, 3), [4, 5, [6, 7]] as [int, int, [int, int]], 8 as MInt); (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; @@ -142,13 +142,13 @@ fun test107() { return (ab.get(0), getBuilderBitsCount(ab.get(1) as builder)); } -global t108: [int, [int, [int]]]; +global t108: [int, [int, [MInt]]]; @method_id(108) fun test108(last: int) { t108 = [1, [2, [last]]]; t108.1.1.0.increment(); - var t: [int, [int, [int]]] = [1, [2, [last]]]; + var t: [int, [int, [MInt]]] = [1, [2, [last]]]; t.1.1.0.increment(); return (t108, t); } @@ -180,7 +180,6 @@ fun test112(f: int, s: int) { return (yy, yy.1, yy.1.plus(yy.1 / 20), eq(yy.1 += (yy.0 *= 0)), yy.1 = yy.1 * 2, yy.1, yy.1 += 1, yy.1, yy); } -@pure @noinline fun getConstTuple(): Tup2Int { return [1,2]; @@ -216,7 +215,7 @@ fun test116() { } @method_id(117) -fun test117() { +fun test117(): int { var t = createEmptyTuple(); t.push(1); try { @@ -361,7 +360,8 @@ fun main(){ 2 PUSHINT // t1 '7=2 0 SETINDEX // t1 x{} PUSHSLICE - STRDUMP DROP // t1 + STRDUMP + DROP // t1 1 PUSHINT // t1 '11=1 1 TUPLE // t1 '10 1 TUPLE // t1 t2 @@ -372,7 +372,8 @@ fun main(){ 0 SETINDEX // t1 t2 '23 0 SETINDEX // t1 t2 x{} PUSHSLICE - STRDUMP DROP // t1 t2 + STRDUMP + DROP // t1 t2 1 PUSHINT // t1 t2 '26=1 1 TUPLE // t1 t2 g_gTup $gTup SETGLOB // t1 t2 @@ -382,7 +383,8 @@ fun main(){ 0 SETINDEX // t1 t2 g_gTup $gTup SETGLOB // t1 t2 x{} PUSHSLICE - STRDUMP DROP // t1 t2 + STRDUMP + DROP // t1 t2 1 PUSHINT // t1 t2 '36=1 2 PUSHINT // t1 t2 g_gTens.0=1 g_gTens.1=2 2 TUPLE @@ -395,7 +397,8 @@ fun main(){ 2 TUPLE $gTens SETGLOB // t1 t2 x{} PUSHSLICE - STRDUMP DROP // t1 t2 + STRDUMP + DROP // t1 t2 $gTup GETGLOB // t1 t2 g_gTup $gTens GETGLOB 2 UNTUPLE // t1 t2 g_gTup g_gTens.0 g_gTens.1 diff --git a/tolk-tester/tests/inference-tests.tolk b/tolk-tester/tests/inference-tests.tolk index 7e8557bf56..88232104e5 100644 --- a/tolk-tester/tests/inference-tests.tolk +++ b/tolk-tester/tests/inference-tests.tolk @@ -36,6 +36,8 @@ fun test1(x: int, y: int) { __expect_type((((1,)),), "int"); __expect_type([((1, ), ), ], "array"); __expect_type([[[1, ], ], ], "array>>"); + __expect_type(true ? 5 : null, "int?"); + __expect_type(false ? beginCell() : null, "builder?"); } fun test2(x: int, y: bool) { @@ -202,6 +204,80 @@ fun test14() { return (a, b, c, i); } +type IntAlias1 = int +type IntAlias2 = int + +fun int.method(self): int { return 0 } +fun IntAlias1.method(self): int1 { return 1 } +fun IntAlias2.method(self): int2 { return 2 } + +@method_id(115) +fun test15(i: IntAlias1, j: IntAlias2) { + // all these are ok due to manual hint + var ok1: (int, IntAlias1, IntAlias2) = (i ?? j, i ?? j, i ?? j); + var ok2: (int?, IntAlias1?, IntAlias2?) = (i ?? j, i ?? j, i ?? j); + __expect_type(((i ?? j) as IntAlias1).method, "(IntAlias1) -> int1"); + __expect_type(((i == 0 ? i : j) as IntAlias2).method, "(IntAlias2) -> int2"); + if (i is int) { + __expect_type(i.method, "(int) -> int"); + } + match (j) { + int => { __expect_type(j.method, "(int) -> int") } + } + match (j) { + IntAlias2 => { __expect_type(j.method, "(IntAlias2) -> int2") } + } + return (i is IntAlias1, i is int, match (i) { int => i.method() }) +} + +fun test16() { + var x: int? = 1; + while (true) { + if (x == null) { + __expect_type(x, "null"); + break; + } + __expect_type(x, "int"); + x = null; + } + __expect_type(x, "null"); +} + +fun test17(x: int?, y: int?) { + while (x != null) { + __expect_type(x, "int"); + if (y == null) { + __expect_type(y, "null"); + continue; + } + __expect_type(y, "int"); + if (x > 0) { + break; + } + x = y; + y = null; + } + __expect_type(x, "int?"); + __expect_type(y, "int?"); +} + +fun test18(x: int | slice | null) { + do { + if (x is int) { + __expect_type(x, "int"); + break; + } + if (x is slice) { + __expect_type(x, "slice"); + x = 0; + continue; + } + __expect_type(x, "null"); + x = 1; + } while (true); + __expect_type(x, "int"); +} + fun main() { return 0; @@ -212,4 +288,5 @@ fun main() { @testcase | 108 | 1 10 | -1 @testcase | 112 | | 1 @testcase | 114 | | (null) (null) (null) 3 +@testcase | 115 | 0 0 | -1 -1 0 */ diff --git a/tolk-tester/tests/inline-tests.tolk b/tolk-tester/tests/inline-tests.tolk index a43e07c81c..b00f1f09c7 100644 --- a/tolk-tester/tests/inline-tests.tolk +++ b/tolk-tester/tests/inline-tests.tolk @@ -1,5 +1,5 @@ fun foo1(x: int): int { - __expect_inline(false); // returns in the middle, leave it for Fift + __expect_inline(true); if (x == 1) { return 1; } @@ -8,7 +8,7 @@ fun foo1(x: int): int { @inline fun foo2(x: int): int { - __expect_inline(false); // returns in the middle, leave it for Fift + __expect_inline(true); if (x == 1) { return 11; } @@ -151,7 +151,6 @@ fun test5() { st.save(); } -@inline fun factorial(x: int): int { __expect_inline(false); // due to recursion if (x <= 1) { @@ -187,7 +186,7 @@ fun builder.store32(mutate self, v: int32): self { } fun sum(a: int, b: int) { - __expect_inline(false); // because it's never called + __expect_inline(true); // even though it's never called return a + b; } @@ -242,7 +241,7 @@ fun test8() { fun test9(x: int) { var p1: Point = {x, y: 0}; var p2: Point = {x: 0, y: p1.getX()}; - var t = createEmptyTuple(); + var t: tuple = createEmptyTuple(); t.pushPoint(p1); t.pushPoint(p2); return (t.popPoint(), t.popPoint(), t); @@ -355,6 +354,8 @@ fun demoInlined2(x: int) { -1 => logT(10), else => logT(20), } + return; + throw 123; } fun wrapperNotInlined(x: int?) { @@ -406,6 +407,566 @@ fun test16() { return -1; } +fun guardChain17(x: int): int { + __expect_inline(true); + if (x < 0) { + return -1; + } + if (x == 0) { + return 0; + } + return x * 10; +} + +fun ifnotGuard17(x: int): int { + __expect_inline(true); + var isPositive = x > 0; + if (!isPositive) { + return 71; + } + return 72; +} + +fun tree17(x: int): int { + __expect_inline(true); + if (x < 0) { + if (x == -1) { + return 11; + } + return 12; + } else { + if (x == 0) { + return 20; + } + return 30; + } +} + +@method_id(117) +fun test17(x: int) { + return (guardChain17(x), ifnotGuard17(x), tree17(x)); +} + +fun maybeInc18(mutate x: int, shouldInc: int) { + __expect_inline(true); + if (shouldInc == 0) { + return; + } + x += 10; + return; +} + +@method_id(118) +fun test18(x: int, shouldInc: int): int { + maybeInc18(mutate x, shouldInc); + return x; +} + +fun genericGuard19(value: T, fallback: T, useFallback: bool): T { + __expect_inline(true); + if (useFallback) { + return fallback; + } + return value; +} + +@method_id(119) +fun test19(x: int): int { + return genericGuard19(x, 100, x == 0); +} + +fun matchAllTerminal20(x: int): int { + __expect_inline(true); + match (x) { + 0 => { return 5; } + 1 => { + var y = x + 6; + return y; + } + else => throw 123 + } +} + +fun matchGuard20(x: int): int { + __expect_inline(true); + match (x) { + 0 => { return 50; } + 1 => { return 60; } + } + return x + 100; +} + +fun matchOneFallthrough20(x: int): int { + __expect_inline(true); + var y = 10; + match (x) { + 0 => { + y += 1; + } + 1 => { return 101; } + else => { return 102; } + } + return y; +} + +fun matchType20(x: int | slice): int { + __expect_inline(true); + match (x) { + int => { return x + 1; } + slice => { return 200; } + } +} + +fun matchPartialArm20(x: int): int { + __expect_inline(true); + match (x) { + 0 => { + if (x == 0) { + return 70; + } + } + else => { return 80; } + } + return 90; +} + +@method_id(120) +fun test20(x: int): (int, int, int, int, int) { + return (matchAllTerminal20(x), matchGuard20(x), matchOneFallthrough20(x), matchType20(x as int | slice), matchPartialArm20(x)); +} + +fun returnThenThrow21(x: int): int { + __expect_inline(true); + if (x > 0) { + return 1; + } + throw 123; +} + +@noinline +fun alwaysThrow21(): never { + throw 777; +} + +fun returnThenNeverCall21(x: int): int { + __expect_inline(true); + if (x > 0) { + return 2; + } + alwaysThrow21(); +} + +@method_id(121) +fun test21(x: int): (int, int) { + return (returnThenThrow21(x), returnThenNeverCall21(x)); +} + +global g22: int; + +fun voidWithMidIf22(x: int) { + __expect_inline(true); + if (x > 0) { + g22 = 1; + } + g22 = g22 + 100; + return; + return; +} + +@method_id(122) +fun test22(x: int) { + voidWithMidIf22(x); + return g22; +} + +fun tree23(x: int, mutate z: int): int { + __expect_inline(true); + z += x; + if (x < 0) { + if (x == -1) { + return 11; + } + return 12; + } else { + if (x == 0) { + return 20; + } + return 30; + } +} + +@method_id(123) +fun test23(x: int) { + var z = 0; + return (tree23(x, mutate z), z); +} + +global g24: int; + +fun spliceIntoElse(x: int): int { + __expect_inline(true); + if (x > 0) { return 1; } + else { var a = 5; g24 = a * x; } + var y = x * 2; + return y; +} + +@method_id(124) +fun test24(x: int) { + return (spliceIntoElse(x), spliceIntoElse(-x), g24); +} + +global g25: int; + +// a loop and try/catch are okay inside an inlined function, as long as `return` is not inside them +fun guardThenLoopAndTry25(x: int): int { + __expect_inline(true); + if (x < 0) { + return -1; + } + var s = 0; + repeat (x) { + s += 2; + } + try { + g25 = 100 / (x - 5); + } catch { + g25 = 777; + } + return s + g25; +} + +@method_id(125) +fun test25(x: int) { return guardThenLoopAndTry25(x); } + +// an inlined function with early returns calling another inlined function with early returns +fun inner26(x: int): int { + __expect_inline(true); + if (x == 0) { + return 100; + } + return x * 2; +} + +fun outer26(x: int): int { + __expect_inline(true); + if (x < 0) { + return -5; + } + return inner26(x) + 1; +} + +@method_id(126) +fun test26(x: int) { return outer26(x); } + +// early returns in a method with `self` and in a method returning `self` +struct Point27 { x: int; y: int; } + +fun Point27.pick(self, useX: bool): int { + __expect_inline(true); + if (useX) { + return self.x; + } + return self.y; +} + +fun Point27.bump(mutate self, by: int): self { + __expect_inline(true); + if (by == 0) { + return self; + throw 123; // unreachable, not lowered + } + self.x += by; + return self; + assert (by == 0) throw 123; // unreachable, not lowered +} + +@method_id(127) +fun test27(x: int, by: int) { + var p = Point27{ x: x, y: x * 10 }; + return (p.pick(true), p.pick(false), p.bump(by).x); +} + +// an inlined function with early returns, called from a loop / try-catch / match arm of a caller +@inline +fun guard28(x: int): int { + if (x < 0) { + return -1; + throw 123; + } + return x * 2; +} + +@method_id(128) +fun test28(x: int): (int, int, int) { + var inLoop = 0; + repeat (3) { + inLoop += guard28(x); + x -= 1; + } + var inTry = 0; + try { + inTry = guard28(x) + guard28(-x); + } catch { + inTry = 999; + } + var inMatch = match (x) { + 0 => guard28(-1), + else => guard28(x), + }; + return (inLoop, inTry, inMatch); +} + +// a chain of nested tails: both `else` and `match` have tails +global g29: int; + +fun nestedSplice29(x: int): (int, int, int) { + __expect_inline(true); + if (x > 100) { + return (1, 1, 1); + } else { + match (x) { + 50 => { return (2, 2, 2); /* unreachable */ g29 = 123; } + 60 => { return (3, 3, 3); /* unreachable */ g29 = 124; } + } + g29 = x; + } + return (x, x + 1, x + 2); + g29 = 555; +} + +@method_id(129) +fun test29(x: int) { return nestedSplice29(x); } + +// `match` whose arms are expressions, not blocks, and one of them does `return` +fun matchExprArms30(x: int) { + __expect_inline(true); + match (x) { + 0 => return 5, + else => g29 = 2, + }; + return g29; +} + +@method_id(130) +fun test30(x: int) { return (matchExprArms30(x), g29); } + +fun matchSomeArmsThrow31(x: int): int { + __expect_inline(true); + match (x) { + -1 => {} + 0 => {} + 1 => {} + else => throw 123 + } + return x + 100; +} + +@method_id(131) +fun test31(x: int): int { + return matchSomeArmsThrow31(x); +} + +fun nestedIfThrow32(x: int): int { + __expect_inline(true); + if (x < 0) { + if (x < 100) { + throw 123; + } + x += 10; + } + return x; +} + +@method_id(132) +fun test32(x: int): int { + return nestedIfThrow32(x); +} + +struct TinyLazy { x: int8 } + +fun TinyLazy.getOrSkip(self, flag: int): int { + __expect_inline(true); + var dummy = lazy TinyLazy.fromSlice(stringHexToSlice("00")); + if (flag != 0) { + return dummy.x; + } + return self.x; +} + +@method_id(133) +fun testLazyEarlyReturn(flag: int): int { + var p = lazy TinyLazy.fromSlice(stringHexToSlice("09")); + return p.getOrSkip(flag); +} + +@inline +fun ret_then_dead_throw(flag: int, b: int): int { + if (flag != 0) { + if (b != 0) { + return 1; + } + throw 2; + } + return 9; +} + +@method_id(134) +fun testInlineRetThrow(flag: int, b: int): int { + return ret_then_dead_throw(flag, b); +} + +@inline +fun match_throw_then_ret(flag: int, x: int): int { + if (flag != 0) { + match (x) { + 0 => { throw 1; } + } + } else { + return 2; + } + return 9; +} + +@method_id(135) +fun testMatchThrowThenRet(flag: int, x: int): int { + return match_throw_then_ret(flag, x); +} + +struct (0x00) LazyRetA { a: int8 } +struct (0x10) LazyRetB { b: int16 } +type LazyRetU = LazyRetA | LazyRetB + +fun has_lazy_union_both_return(s: slice): int { + __expect_inline(true); + var x = lazy LazyRetU.fromSlice(s); + match (x) { + LazyRetA => { return x.a; } + LazyRetB => { return x.b; } + } +} + +@method_id(136) +fun testLazyUnionBothReturn(flag: int) { + val s = flag == 0 ? "0008".hexToSlice() : "10FFFF".hexToSlice(); + return has_lazy_union_both_return(s); +} + +fun has_lazy_union_single_return(s: slice): int { + __expect_inline(true); + var x = lazy LazyRetU.fromSlice(s); + match (x) { + LazyRetA => { return x.a; } + LazyRetB => { } + } + return 123; +} + +@method_id(137) +fun testLazyUnionSingleReturn(flag: int) { + val s = flag == 0 ? "0008".hexToSlice() : "10FFFF".hexToSlice(); + return has_lazy_union_single_return(s); +} + +fun has_lazy_union_multi_tail(s: slice, flag: int, mutate out: int): int { + __expect_inline(true); + var x = lazy LazyRetU.fromSlice(s); + __expect_lazy("[x] lazy match"); + match (x) { + LazyRetA => { + if (flag > 100) { + out -= 90; + return 100; + } + out += x.a; + } + LazyRetB => { + if (flag < -100) { + return -100; + } + throw 100; + } + } + out += 123; + return 123; +} + +@method_id(138) +fun testLazyUnionMultiTail(flag: int) { + var r = 0; + val s = flag > 0 ? "0008".hexToSlice() : "10FFFF".hexToSlice(); + return (has_lazy_union_multi_tail(s, flag, mutate r), r); +} + +// An all-terminal inner `if` after an early return must not be counted as fallthrough. +@inline +fun return_then_terminal_if_in_both_arms(useLeft: bool, doReturn: bool, chooseThrow: bool): int { + __expect_inline(true); + if (useLeft) { + if (doReturn) { + return 11; + } + if (chooseThrow) { + throw 201; + } else { + throw 202; + } + } else { + if (doReturn) { + return 21; + } + if (chooseThrow) { + throw 203; + } else { + throw 204; + } + } +} + +@method_id(139) +fun testReturnThenTerminalIfInBothArms(useLeft: bool, doReturn: bool, chooseThrow: bool): int { + try { + return return_then_terminal_if_in_both_arms(useLeft, doReturn, chooseThrow); + } catch (excNo) { + return excNo; + } +} + +// An exhaustive all-terminal inner `match` after an early return must not be counted as fallthrough. +@inline +fun return_then_terminal_match_in_both_arms(outerKind: int, doReturn: bool, throwKind: int): int { + __expect_inline(true); + match (outerKind) { + 0 => { + if (doReturn) { + return 31; + } + match (throwKind) { + 0 => { throw 211; } + else => { throw 212; } + } + } + else => { + if (doReturn) { + return 41; + } + match (throwKind) { + 0 => { throw 213; } + else => { throw 214; } + } + } + } +} + +@method_id(140) +fun testReturnThenTerminalMatchInBothArms(outerKind: int, doReturn: bool, throwKind: int): int { + try { + return return_then_terminal_match_in_both_arms(outerKind, doReturn, throwKind); + } catch (excNo) { + return excNo; + } +} + fun main() { // regardless of number of calls, it will be inlined, since it's lightweight in AST terms @@ -444,6 +1005,60 @@ fun usedIn10ButDeclaredBelow(x: int) { @testcase | 115 | -1 | [ 10 10 ] @testcase | 115 | 1 | [ 20 ] @testcase | 116 | | -1 +@testcase | 117 | -1 | -1 71 11 +@testcase | 117 | 0 | 0 71 20 +@testcase | 117 | 5 | 50 72 30 +@testcase | 118 | 5 0 | 5 +@testcase | 118 | 5 1 | 15 +@testcase | 119 | 0 | 100 +@testcase | 119 | 7 | 7 +@testcase | 120 | 0 | 5 50 11 1 70 +@testcase | 120 | 1 | 7 60 101 2 80 +@testcase | 121 | 5 | 1 2 +@testcase | 122 | 10 | 101 +@testcase | 123 | -1 | 11 -1 +@testcase | 124 | 8 | 1 -16 -40 +@testcase | 125 | -1 | -1 +@testcase | 125 | 3 | -44 +@testcase | 125 | 5 | 787 +@testcase | 126 | -1 | -5 +@testcase | 126 | 0 | 101 +@testcase | 126 | 3 | 7 +@testcase | 127 | 7 0 | 7 70 7 +@testcase | 127 | 7 3 | 7 70 10 +@testcase | 128 | 5 | 24 3 4 +@testcase | 128 | 1 | 1 3 -1 +@testcase | 129 | 200 | 1 1 1 +@testcase | 129 | 50 | 2 2 2 +@testcase | 129 | 7 | 7 8 9 +@testcase | 130 | 0 | 5 (null) +@testcase | 130 | 9 | 2 2 +@testcase | 131 | -1 | 99 +@testcase | 131 | 0 | 100 +@testcase | 131 | 1 | 101 +@testcase | 132 | 0 | 0 +@testcase | 132 | 5 | 5 +@testcase | 133 | 0 | 9 +@testcase | 133 | 1 | 0 +@testcase | 134 | 1 1 | 1 +@testcase | 134 | 0 0 | 9 +@testcase | 135 | 1 1 | 9 +@testcase | 135 | 0 0 | 2 +@testcase | 136 | 0 | 8 +@testcase | 136 | 5 | -1 +@testcase | 137 | 0 | 8 +@testcase | 137 | 5 | 123 +@testcase | 138 | 10 | 123 131 +@testcase | 138 | 500 | 100 -90 +@testcase | 138 | -500 | -100 0 +@testcase | 139 | 1 1 1 | 11 +@testcase | 139 | 0 1 0 | 21 +@testcase | 139 | 1 0 1 | 201 +@testcase | 139 | 0 0 0 | 204 +@testcase | 140 | 0 1 0 | 31 +@testcase | 140 | 1 1 1 | 41 +@testcase | 140 | 0 0 0 | 211 +@testcase | 140 | 1 0 1 | 214 @fif_codegen_avoid Storage.load @fif_codegen_avoid Storage.save @@ -523,34 +1138,26 @@ fun usedIn10ButDeclaredBelow(x: int) { @fif_codegen """ - wrapperNotInlined() PROC:<{ // x + test17() PROC:<{ // x DUP // x x - ISNULL // x '1 - IFNOTJMP:<{ // x - DUP // x x - -1 EQINT // x '3 - IFJMP:<{ // x + 0 LESSINT // x '4 + IF:<{ // x + -1 PUSHINT // x '1=-1 + }>ELSE<{ // x """ @fif_codegen """ - wrap16() PROC:<{ - IFJMP:<{ - 129 PUSHINT - OVER - EQUAL - IFJMP:<{ - DROP - 0 GTINT - 100 THROWIFNOT - }> - 130 PUSHINT - s2 POP - EQUAL - IFJMP:<{ - }> - }> - 123 THROW + test22() PROC:<{ // x + 0 GTINT // '3 + IF:<{ // + 1 PUSHINT // g_g22=1 + $g22 SETGLOB // + }> // + $g22 GETGLOB // g_g22 + 100 ADDCONST // g_g22 + $g22 SETGLOB // + $g22 GETGLOB // g_g22 }> """ */ diff --git a/tolk-tester/tests/intN-tests.tolk b/tolk-tester/tests/intN-tests.tolk index 13204f1caa..d2588c45c6 100644 --- a/tolk-tester/tests/intN-tests.tolk +++ b/tolk-tester/tests/intN-tests.tolk @@ -249,7 +249,7 @@ fun main() { @fif_codegen """ test12() PROC:<{ - 100000000 PUSHINT // '4=100000000 + 100000000 PUSHINT // '7=100000000 }> """ */ diff --git a/tolk-tester/tests/invalid-declaration/err-1190.tolk b/tolk-tester/tests/invalid-declaration/err-1190.tolk index 510e1d1cc0..d88bac9826 100644 --- a/tolk-tester/tests/invalid-declaration/err-1190.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1190.tolk @@ -4,6 +4,6 @@ fun main() { /** @compilation_should_fail -@stderr provide a type for a variable, because its default value is omitted: +@stderr provide a type for a variable, because its default value is omitted @stderr var i: ; */ diff --git a/tolk-tester/tests/invalid-declaration/err-1226.tolk b/tolk-tester/tests/invalid-declaration/err-1226.tolk index 12620289c3..45a0129ffd 100644 --- a/tolk-tester/tests/invalid-declaration/err-1226.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1226.tolk @@ -8,7 +8,9 @@ fun `MInt`.`check`(self) {} /** @compilation_should_fail -@stderr redefinition of symbol +@stderr redefinition of symbol `MInt.check` @stderr err-1226.tolk:7 +@stderr note: previous definition is here @stderr err-1226.tolk:5 +@stderr ^^^^^ */ diff --git a/tolk-tester/tests/invalid-declaration/err-1449.tolk b/tolk-tester/tests/invalid-declaration/err-1449.tolk index f682eef7a6..3e59487804 100644 --- a/tolk-tester/tests/invalid-declaration/err-1449.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1449.tolk @@ -1,8 +1,6 @@ -@pure get fun secret(): int { return 0; } -@pure get fun balanced(): int { return 1; } diff --git a/tolk-tester/tests/invalid-declaration/err-1450.tolk b/tolk-tester/tests/invalid-declaration/err-1450.tolk new file mode 100644 index 0000000000..b8c8e10b58 --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1450.tolk @@ -0,0 +1,17 @@ +fun main(): void { +} + +@method_id(104128) +fun manualCollision(): int { + return 2; +} + +get fun balance(): int { + return 1; +} + +/** +@compilation_should_fail +@stderr method_id collision: `manualCollision` and `balance` produce the same method_id=104128 +@stderr get fun balance +*/ diff --git a/tolk-tester/tests/invalid-declaration/err-1451.tolk b/tolk-tester/tests/invalid-declaration/err-1451.tolk new file mode 100644 index 0000000000..290b84740d --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1451.tolk @@ -0,0 +1,18 @@ +@method_id(42) +fun first(): int { + return 1; +} + +@method_id(42) +fun second(): int { + return 2; +} + +fun main(): void { +} + +/** +@compilation_should_fail +@stderr method_id collision: `first` and `second` produce the same method_id=42 +@stderr fun second +*/ diff --git a/tolk-tester/tests/invalid-declaration/err-1452.tolk b/tolk-tester/tests/invalid-declaration/err-1452.tolk new file mode 100644 index 0000000000..4398a3df15 --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1452.tolk @@ -0,0 +1,13 @@ +fun main(): void { +} + +@method_id(0) +fun alsoZero(): int { + return 1; +} + +/** +@compilation_should_fail +@stderr method_id collision: `main` and `alsoZero` produce the same method_id=0 +@stderr fun alsoZero +*/ diff --git a/tolk-tester/tests/invalid-declaration/err-1579.tolk b/tolk-tester/tests/invalid-declaration/err-1579.tolk index f8278e5cbb..c0bfe68773 100644 --- a/tolk-tester/tests/invalid-declaration/err-1579.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1579.tolk @@ -16,6 +16,6 @@ fun main(flag: bool, t: P256): int { @stderr generated TVM stack is too deep while compiling function `main` @stderr TVM can not store more than 255 elements on the stack @stderr hint: try splitting very wide tensors/structs, storing parts in cells/tuples -@stderr in function `main` -@stderr ^^^^ +@stderr fun main +@stderr ^^^^ */ diff --git a/tolk-tester/tests/invalid-declaration/err-1683.tolk b/tolk-tester/tests/invalid-declaration/err-1683.tolk index 5238a6801b..8f7ae81a62 100644 --- a/tolk-tester/tests/invalid-declaration/err-1683.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1683.tolk @@ -3,5 +3,7 @@ global mulDivMod: int; /** @compilation_should_fail @stderr global mulDivMod: int; -@stderr redefinition of built-in symbol +@stderr redefinition of symbol `mulDivMod` +@stderr note: previous definition is here +@stderr fun mulDivMod */ diff --git a/tolk-tester/tests/invalid-declaration/err-1876.tolk b/tolk-tester/tests/invalid-declaration/err-1876.tolk index 281a4af7d8..358aca3b90 100644 --- a/tolk-tester/tests/invalid-declaration/err-1876.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1876.tolk @@ -8,4 +8,6 @@ struct Pair { @compilation_should_fail @stderr redeclaration of field `first` @stderr first: int +@stderr note: previous declaration is here +@stderr first: T1 */ diff --git a/tolk-tester/tests/invalid-declaration/err-1918.tolk b/tolk-tester/tests/invalid-declaration/err-1918.tolk index 8fec98925e..6f26625aa0 100644 --- a/tolk-tester/tests/invalid-declaration/err-1918.tolk +++ b/tolk-tester/tests/invalid-declaration/err-1918.tolk @@ -7,6 +7,7 @@ fun hello(): int { /** @compilation_should_fail @stderr fun hello() -@stderr redefinition of symbol, previous was at +@stderr redefinition of symbol `hello` +@stderr previous definition is here @stderr err-1918.tolk:1 */ diff --git a/tolk-tester/tests/invalid-declaration/err-1990.tolk b/tolk-tester/tests/invalid-declaration/err-1990.tolk new file mode 100644 index 0000000000..59dca2a7ca --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1990.tolk @@ -0,0 +1,7 @@ +get fun calc_swap_cost() + +/** +@cmd_line_option --allow-empty-get-fun +@compilation_should_fail +@stderr get method prototype must declare return type + */ diff --git a/tolk-tester/tests/invalid-declaration/err-1991.tolk b/tolk-tester/tests/invalid-declaration/err-1991.tolk new file mode 100644 index 0000000000..f950cfc32b --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1991.tolk @@ -0,0 +1,14 @@ +contract First { +} + +contract Second { +} + +fun main() { + +} + +/** +@compilation_should_fail +@stderr a file can have only one `contract` directive + */ diff --git a/tolk-tester/tests/invalid-declaration/err-1992.tolk b/tolk-tester/tests/invalid-declaration/err-1992.tolk new file mode 100644 index 0000000000..75ae65faa2 --- /dev/null +++ b/tolk-tester/tests/invalid-declaration/err-1992.tolk @@ -0,0 +1,9 @@ +get fun first(): int +get fun second(): coins + +fun main() {} + +/** +@compilation_should_fail +@stderr empty `get fun` is allowed only with --allow-empty-get-fun + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4080.tolk b/tolk-tester/tests/invalid-semantics/err-4080.tolk index df569995fc..a6027ef9e3 100644 --- a/tolk-tester/tests/invalid-semantics/err-4080.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4080.tolk @@ -10,4 +10,6 @@ fun main() { /** @compilation_should_fail @stderr too many arguments in call to `int.methodWith1Param`, expected 1, have 2 +@stderr note: function declared here +@stderr err-4080.tolk:1 */ diff --git a/tolk-tester/tests/invalid-semantics/err-4081.tolk b/tolk-tester/tests/invalid-semantics/err-4081.tolk index 30839a5038..f9a60f003e 100644 --- a/tolk-tester/tests/invalid-semantics/err-4081.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4081.tolk @@ -27,4 +27,6 @@ fun main(x: int, s: slice) { @stderr too few arguments in call to `f6`, expected 4, have 1 @stderr too few arguments in call to `address`, expected 1, have 0 @stderr too few arguments in call to `string.crc32`, expected 1, have 0 +@stderr note: parameter declared here +@stderr note: function declared here */ diff --git a/tolk-tester/tests/invalid-semantics/err-4085.tolk b/tolk-tester/tests/invalid-semantics/err-4085.tolk index 11c4039088..6d20bfe3be 100644 --- a/tolk-tester/tests/invalid-semantics/err-4085.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4085.tolk @@ -17,6 +17,6 @@ fun main() { /** @compilation_should_fail @stderr can not detect struct name -@stderr use either `var v: StructName = { ... }` or `var v = StructName { ... }` +@stderr hint: use `var v = StructName { ... }` @stderr { value: 20 } */ diff --git a/tolk-tester/tests/invalid-semantics/err-4090.tolk b/tolk-tester/tests/invalid-semantics/err-4090.tolk index 7dfa456cdc..c692afae45 100644 --- a/tolk-tester/tests/invalid-semantics/err-4090.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4090.tolk @@ -17,4 +17,6 @@ fun main() { /** @compilation_should_fail @stderr field `value` missed in initialization of struct `TryHackCompilerByNever` +@stderr note: field declared here +@stderr value: never */ diff --git a/tolk-tester/tests/invalid-semantics/err-4256.tolk b/tolk-tester/tests/invalid-semantics/err-4256.tolk index eb7a74936e..7ac5c2b453 100644 --- a/tolk-tester/tests/invalid-semantics/err-4256.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4256.tolk @@ -1,18 +1,18 @@ type MInt = int; type IntOrSlice = MInt|slice; -fun (int | slice).method(self) {} +fun (MInt | slice).method(self) {} fun (slice | MInt).method(self) {} fun IntOrSlice.method(self) {} -fun main(p: int|slice) { +fun main(p: MInt|slice) { p.method(); } /** @compilation_should_fail -@stderr call to method `method` for type `int | slice` is ambiguous -@stderr candidate function: `(int | slice).method` +@stderr call to method `method` for type `MInt | slice` is ambiguous +@stderr candidate function: `(MInt | slice).method` @stderr candidate function: `(slice | MInt).method` -@stderr candidate function: `IntOrSlice.method` +@stderr_avoid candidate function: `IntOrSlice.method` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4301.tolk b/tolk-tester/tests/invalid-semantics/err-4301.tolk index 800495da3b..130c02978c 100644 --- a/tolk-tester/tests/invalid-semantics/err-4301.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4301.tolk @@ -12,4 +12,6 @@ fun main() { @compilation_should_fail @stderr using variable `i` before it's definitely assigned @stderr if (i == 0) +@stderr note: variable declared here +@stderr var i: int; */ diff --git a/tolk-tester/tests/invalid-semantics/err-4303.tolk b/tolk-tester/tests/invalid-semantics/err-4303.tolk new file mode 100644 index 0000000000..28be099888 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4303.tolk @@ -0,0 +1,15 @@ +fun main(flag: bool): int { + var x: int; + do { + if (flag) { + break; + } + x = 1; + } while (false); + return x; +} + +/** +@compilation_should_fail +@stderr using variable `x` before it's definitely assigned + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4305.tolk b/tolk-tester/tests/invalid-semantics/err-4305.tolk new file mode 100644 index 0000000000..6df6a0c6c2 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4305.tolk @@ -0,0 +1,15 @@ +fun main(flag: bool): int { + var x: int; + do { + if (flag) { + continue; + } + x = 1; + } while (false); + return x; +} + +/** +@compilation_should_fail +@stderr using variable `x` before it's definitely assigned + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4311.tolk b/tolk-tester/tests/invalid-semantics/err-4311.tolk index 5b1fa060c2..cabcc6ab79 100644 --- a/tolk-tester/tests/invalid-semantics/err-4311.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4311.tolk @@ -7,4 +7,6 @@ fun main(): int { /** @compilation_should_fail @stderr using variable `x` before it's definitely assigned +@stderr note: variable declared here +@stderr var x: int; */ diff --git a/tolk-tester/tests/invalid-semantics/err-4355.tolk b/tolk-tester/tests/invalid-semantics/err-4355.tolk index bb648dfa43..b2a613c079 100644 --- a/tolk-tester/tests/invalid-semantics/err-4355.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4355.tolk @@ -16,6 +16,6 @@ fun cantModifyValDeclaredInMatch() { /** @compilation_should_fail @stderr modifying immutable variable `b` -@stderr in function `cantModifyValDeclaredInMatch` +@stderr in function cantModifyValDeclaredInMatch @stderr increment(mutate b) */ diff --git a/tolk-tester/tests/invalid-semantics/err-4385.tolk b/tolk-tester/tests/invalid-semantics/err-4385.tolk deleted file mode 100644 index 767f524f6a..0000000000 --- a/tolk-tester/tests/invalid-semantics/err-4385.tolk +++ /dev/null @@ -1,22 +0,0 @@ -@pure -fun validate_input(input: cell): (int, int) { - var (x, y, z, correct) = input.calculateSize(10); - assert(correct) throw 102; - return (x, y); -} - -@pure -fun someF(): int { - var c: cell = beginCell().endCell(); - validate_input(c); - return 0; -} - -fun main() {} - -/** -@compilation_should_fail -@stderr an impure operation in a pure function -@stderr in function `validate_input` -@stderr assert(correct) -*/ diff --git a/tolk-tester/tests/invalid-semantics/err-4400.tolk b/tolk-tester/tests/invalid-semantics/err-4400.tolk new file mode 100644 index 0000000000..67e28e6755 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4400.tolk @@ -0,0 +1,16 @@ +struct CalcSwapCostReply { + cost: coins +} + +get fun calc_swap_cost(): CalcSwapCostReply + +fun main(): int { + calc_swap_cost(); + return 0; +} + +/** +@cmd_line_option --allow-empty-get-fun +@compilation_should_fail +@stderr get method `calc_swap_cost` is a prototype and can not be called + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4401.tolk b/tolk-tester/tests/invalid-semantics/err-4401.tolk new file mode 100644 index 0000000000..67bf871354 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4401.tolk @@ -0,0 +1,16 @@ +struct CalcSwapCostReply { + cost: coins +} + +get fun calc_swap_cost(): CalcSwapCostReply; + +fun main(): int { + var f = calc_swap_cost; + return 0; +} + +/** +@cmd_line_option --allow-empty-get-fun +@compilation_should_fail +@stderr can not get reference to this function, it's a get method prototype + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4410.tolk b/tolk-tester/tests/invalid-semantics/err-4410.tolk index c16768a13f..eddd6c2fdf 100644 --- a/tolk-tester/tests/invalid-semantics/err-4410.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4410.tolk @@ -10,4 +10,7 @@ fun main(v: int32 | int64): array { /** @compilation_should_fail @stderr type of `match` was inferred as `int32 | int64`; probably, it's not what you expected +@stderr note: this branch is `int32` +@stderr note: this branch is `int64` +@stderr ^^^^^ */ diff --git a/tolk-tester/tests/invalid-semantics/err-4480.tolk b/tolk-tester/tests/invalid-semantics/err-4480.tolk index a0f0da32dc..098c8d2da6 100644 --- a/tolk-tester/tests/invalid-semantics/err-4480.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4480.tolk @@ -5,6 +5,6 @@ fun main() { /** @compilation_should_fail @stderr unknown type name `Point2` -@stderr in function `main` +@stderr in function main @stderr Point2.create() */ diff --git a/tolk-tester/tests/invalid-semantics/err-4511.tolk b/tolk-tester/tests/invalid-semantics/err-4511.tolk new file mode 100644 index 0000000000..0800adbec9 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4511.tolk @@ -0,0 +1,21 @@ +struct A { } +struct B { } +type AOrB = A | B; + +@inline +fun AOrB.getSecondField(self): int8 { + return match (self) { + A => {} + B => {} + }; +} + +fun main(a: A, b: B) { + a.getSecondField(); +} + +/** +@compilation_should_fail +@stderr method `getSecondField` not found for type `A` +@stderr (but it exists for type `AOrB`) + */ \ No newline at end of file diff --git a/tolk-tester/tests/invalid-semantics/err-4539.tolk b/tolk-tester/tests/invalid-semantics/err-4539.tolk index cc15288ee6..30508020c6 100644 --- a/tolk-tester/tests/invalid-semantics/err-4539.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4539.tolk @@ -6,20 +6,12 @@ type AssetList = dict fun BalanceList.validate(self): int1 { return 0 } fun AssetList.validate(self): int2 { return 0 } -fun f(b: BalanceList) { - if (b == null) { return } - else { b = createEmptyDict() } - __expect_type(b, "BalanceList"); - - if (b == null) { return } - __expect_type(b, "cell"); - +fun f(b: cell) { b.validate() } /** @compilation_should_fail -@stderr call to method `validate` for type `cell` is ambiguous -@stderr candidate function: `BalanceList.validate` -@stderr candidate function: `AssetList.validate` +@stderr method `validate` not found for type `cell` +@stderr but it exists for types `BalanceList`, `AssetList` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4542.tolk b/tolk-tester/tests/invalid-semantics/err-4542.tolk index e1ef833e58..adbdd5ab69 100644 --- a/tolk-tester/tests/invalid-semantics/err-4542.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4542.tolk @@ -3,7 +3,7 @@ fun tuple.tupleMut(mutate self): int asm "TLEN"; fun main() { - var t = createEmptyTuple(); + var t: tuple = createEmptyTuple(); return [[t.tupleMut]]; } diff --git a/tolk-tester/tests/invalid-semantics/err-4560.tolk b/tolk-tester/tests/invalid-semantics/err-4560.tolk index 507b9ccc2e..02a81084b6 100644 --- a/tolk-tester/tests/invalid-semantics/err-4560.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4560.tolk @@ -1,17 +1,17 @@ struct Wrapper { item: T } type AssetBuilder = builder -type BalanceBuilder = builder +type AssetWrapper = Wrapper -fun Wrapper.validate(self) { return 0 } +fun AssetWrapper.validate(self) { return 0 } fun main() { - var b: BalanceBuilder = beginCell(); - Wrapper{item:b}.validate(); + var b: Wrapper = Wrapper{item: beginCell()}; + b.validate(); } /** @compilation_should_fail -@stderr method `validate` not found for type `Wrapper` -@stderr but it exists for type `Wrapper` +@stderr method `validate` not found for type `Wrapper` +@stderr but it exists for type `AssetWrapper` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4680.tolk b/tolk-tester/tests/invalid-semantics/err-4680.tolk index 175ec6b659..643de55580 100644 --- a/tolk-tester/tests/invalid-semantics/err-4680.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4680.tolk @@ -1,6 +1,5 @@ type BalanceDict = dict -fun dict.doSome(self) {} fun BalanceDict.doSome(self) {} fun m(d: dict) { @@ -9,7 +8,6 @@ fun m(d: dict) { /** @compilation_should_fail -@stderr call to method `doSome` for type `dict` is ambiguous -@stderr candidate function: `dict.doSome` -@stderr candidate function: `BalanceDict.doSome` +@stderr method `doSome` not found for type `dict` +@stderr but it exists for type `BalanceDict` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4681.tolk b/tolk-tester/tests/invalid-semantics/err-4681.tolk index 0974c50dde..cfa313aa26 100644 --- a/tolk-tester/tests/invalid-semantics/err-4681.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4681.tolk @@ -1,15 +1,14 @@ type BalanceDict = dict +type AssetDict = dict -fun dict.doSome(self) {} fun BalanceDict.doSome(self) {} -fun m(d: BalanceDict) { +fun m(d: AssetDict) { d.doSome() } /** @compilation_should_fail -@stderr call to method `doSome` for type `BalanceDict` is ambiguous -@stderr candidate function: `dict.doSome` -@stderr candidate function: `BalanceDict.doSome` +@stderr method `doSome` not found for type `AssetDict` +@stderr but it exists for type `BalanceDict` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4704.tolk b/tolk-tester/tests/invalid-semantics/err-4704.tolk new file mode 100644 index 0000000000..652740dcfd --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4704.tolk @@ -0,0 +1,28 @@ +enum AA { + One = 1, + Two, + Three, + Onne = 1, +} + +enum BB { + Zero, + One, + Two, + Three, + AlsoTwo = 2, + AlsoThree, +} + +/** +@compilation_should_fail +@stderr duplicate value `1` in enum `AA` +@stderr Onne = 1 +@stderr One = 1 +@stderr duplicate value `2` in enum `BB` +@stderr AlsoTwo +@stderr Two +@stderr duplicate value `3` in enum `BB` +@stderr AlsoThree +@stderr Three + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4707.tolk b/tolk-tester/tests/invalid-semantics/err-4707.tolk new file mode 100644 index 0000000000..98562f58e9 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4707.tolk @@ -0,0 +1,9 @@ +fun main(i: int): int { + var f: (int) -> int = `__InMessage.getInMsgParam`; + return f(i); +} + +/** +@compilation_should_fail +@stderr internal compiler functions are not allowed to be called + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4712.tolk b/tolk-tester/tests/invalid-semantics/err-4712.tolk index 2e210eb4b3..88c19c8e65 100644 --- a/tolk-tester/tests/invalid-semantics/err-4712.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4712.tolk @@ -6,5 +6,5 @@ fun main() { /** @compilation_should_fail @stderr field `asdf` doesn't exist in type `tuple` -@stderr in function `main` +@stderr in function main */ diff --git a/tolk-tester/tests/invalid-semantics/err-4847.tolk b/tolk-tester/tests/invalid-semantics/err-4847.tolk index 215c245c35..9e45ea23ce 100644 --- a/tolk-tester/tests/invalid-semantics/err-4847.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4847.tolk @@ -10,7 +10,9 @@ fun testCantCallWithoutMutate() { /** @compilation_should_fail @stderr function `increment` mutates parameter `x` -@stderr you need to specify `mutate` when passing an argument, like `mutate x` -@stderr in function `testCantCallWithoutMutate` +@stderr specify `mutate` when passing an argument, like `mutate x` +@stderr in function testCantCallWithoutMutate @stderr increment(x) +@stderr note: parameter declared here +@stderr fun increment(mutate x: int) */ diff --git a/tolk-tester/tests/invalid-semantics/err-4851.tolk b/tolk-tester/tests/invalid-semantics/err-4851.tolk index 5195da0700..18c35e61a6 100644 --- a/tolk-tester/tests/invalid-semantics/err-4851.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4851.tolk @@ -5,5 +5,7 @@ fun int.increment(self) { /** @compilation_should_fail @stderr modifying `self`, which is immutable by default -@stderr probably, you want to declare `mutate self` +@stderr note: probably, you want to declare `mutate self` +@stderr fun int.increment(self) { +@stderr ^^^^ */ diff --git a/tolk-tester/tests/invalid-semantics/err-4904.tolk b/tolk-tester/tests/invalid-semantics/err-4904.tolk index 062fa5af2a..96f21ba29d 100644 --- a/tolk-tester/tests/invalid-semantics/err-4904.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4904.tolk @@ -13,7 +13,6 @@ fun main() { /** @compilation_should_fail -@stderr call to method `validate` for type `builder` is ambiguous -@stderr candidate function: `AssetBuilder.validate` -@stderr candidate function: `BalanceBuilder.validate` +@stderr method `validate` not found for type `builder` +@stderr but it exists for types `AssetBuilder`, `BalanceBuilder` */ diff --git a/tolk-tester/tests/invalid-semantics/err-4905.tolk b/tolk-tester/tests/invalid-semantics/err-4905.tolk new file mode 100644 index 0000000000..6a7b81b561 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4905.tolk @@ -0,0 +1,20 @@ +struct Wrapper { + item: T +} + +type AssetBuilder = builder +type BalanceBuilder = builder + +fun Wrapper.validate(self): int3 { return 0 } +fun Wrapper.validate(self): int4 { return 0 } + +fun main() { + var a: builder = beginCell(); + return Wrapper{item: a}.validate(); +} + +/** +@compilation_should_fail +@stderr method `validate` not found for type `Wrapper` +@stderr but it exists for types `Wrapper`, `Wrapper` + */ diff --git a/tolk-tester/tests/invalid-semantics/err-4925.tolk b/tolk-tester/tests/invalid-semantics/err-4925.tolk index 858ff6f779..88ba23a905 100644 --- a/tolk-tester/tests/invalid-semantics/err-4925.tolk +++ b/tolk-tester/tests/invalid-semantics/err-4925.tolk @@ -7,7 +7,7 @@ type WrapperAlias = Wrapper; fun main(w: int | WrapperAlias | WrapperAlias) { match (w) { - MyInt => {} + int => {} WrapperAlias => {} WrapperAlias => {} } diff --git a/tolk-tester/tests/invalid-semantics/err-4990.tolk b/tolk-tester/tests/invalid-semantics/err-4990.tolk new file mode 100644 index 0000000000..a8151ea404 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-4990.tolk @@ -0,0 +1,78 @@ +global g1: int; +global g2: int; +global g3: int; +global g4: int; +global g5: int; +global g6: int; +global g7: int; +global g8: int; +global g9: int; +global g10: int; +global g11: int; +global g12: int; +global g13: int; +global g14: int; +global g15: int; +global g16: int; +global g17: int; +global g18: int; +global g19: int; +global g20: int; +global g21: int; +global g22: int; +global g23: int; +global g24: int; +global g25: int; +global g26: int; +global g27: int; +global g28: int; +global g29: int; +global g30: int; +global g31: int; +global g32: int; + +fun main(): int { + g1 = 1; + g2 = 2; + g3 = 3; + g4 = 4; + g5 = 5; + g6 = 6; + g7 = 7; + g8 = 8; + g9 = 9; + g10 = 10; + g11 = 11; + g12 = 12; + g13 = 13; + g14 = 14; + g15 = 15; + g16 = 16; + g17 = 17; + g18 = 18; + g19 = 19; + g20 = 20; + g21 = 21; + g22 = 22; + g23 = 23; + g24 = 24; + g25 = 25; + g26 = 26; + g27 = 27; + g28 = 28; + g29 = 29; + g30 = 30; + g31 = 31; + g32 = 32; + + return g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + + g9 + g10 + g11 + g12 + g13 + g14 + g15 + g16 + + g17 + g18 + g19 + g20 + g21 + g22 + g23 + g24 + + g25 + g26 + g27 + g28 + g29 + g30 + g31 + g32; +} + +/** +@compilation_should_fail +@stderr too many global variables (more than 31) +@stderr g32 + */ diff --git a/tolk-tester/tests/invalid-semantics/err-break-continue.tolk b/tolk-tester/tests/invalid-semantics/err-break-continue.tolk new file mode 100644 index 0000000000..4065fb2883 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-break-continue.tolk @@ -0,0 +1,204 @@ +// @compilation_should_fail + +// @stderr `break` used outside a loop +// @stderr in function break_outside +fun break_outside() { + break; +} + +// @stderr `continue` used outside a loop +// @stderr in function continue_outside +fun continue_outside() { + continue; +} + +// @stderr `break` is not allowed inside `try/catch` +// @stderr in function break_in_try +fun break_in_try() { + while (true) { + try { + break; + } catch { + } + } +} + +// @stderr `continue` is not allowed inside `try/catch` +// @stderr can not compile `continue`, because of `continue` in non-standard, unsupported position +// @stderr note: this prevents compiling `continue` +// @stderr in function continue_in_try +// @stderr try { +// @stderr continue ; +fun continue_in_try() { + while (true) { + try { + continue ; + } catch { + } + } +} + +// @stderr can not compile `continue`, because `if/else` has complicated `continue` and multiple exit points +// @stderr in function unbalanced_if_continue +fun unbalanced_if_continue() { + var a = true; + var b = true; + while (true) { + if (a) { + if (b) { + continue; + } + } + a = false; + } +} + +// @stderr can not compile `continue`, because some `match` arms do `continue`, some do not +// @stderr in function unbalanced_match_continue +fun unbalanced_match_continue(x: int) { + while (true) { + match (x) { + 0 => { continue; } + 1 => { x += 1; } + else => { x += 2; } + } + x += 3; + } +} + +// @stderr can not compile `continue`, because a terminal `if/else/match` is followed by unreachable statements +// @stderr in function terminal_if_then_dead_suffix +fun terminal_if_then_dead_suffix() { + while (true) { + if (true) { + continue; + } else { + break; + } + throw 1; + } +} + +// @stderr `return` is not allowed inside a loop that uses `break` +// @stderr in function return_and_break_same_loop +// @stderr } else { +// @stderr return; +// @stderr note: `break` is used here +// @stderr if (cond) { +// @stderr break; +fun return_and_break_same_loop(cond: bool) { + while (true) { + if (cond) { + break; + } else { + return; + } + } +} + +// @stderr `return` is not allowed in a loop condition +// @stderr in function return_in_while_cond +fun return_in_while_cond(): int { + while (match (true) { + true => { + return 70; + true + } + false => false + }) { + } + return 0; +} + +// @stderr in function break_in_try_then_return +fun break_in_try_then_return() { + while (true) { + try { + break; + } catch { + } + return; + } +} + +// @stderr in function continue_in_try_in_block +fun continue_in_try_in_block() { + while (true) { + { + try { + continue; + } catch { + } + } + } +} + +// @stderr in function break_in_loop_nested_in_condition +fun break_in_loop_nested_in_condition() { + while (match (0) { + 0 => { + while (false) { + break; + } + throw 1; + } + else => false + }) { + } +} + +// @stderr in function continue_in_loop_nested_in_condition +fun continue_in_loop_nested_in_condition() { + while (match (0) { + 0 => { + while (false) { + continue; + } + throw 1; + } + else => false + }) { + } +} + +// @stderr in function return_in_repeat_count +fun return_in_repeat_count(): int { + repeat (match (0) { + 0 => { + return 5; + 1 + } + else => 1 + }) { + } + return 0; +} + +// @stderr in function return_in_nested_while_cond +fun return_in_nested_while_cond(flag: bool): int { + while (true) { + while (match (flag) { + true => { + return 71; + true + } + false => false + }) { + } + break; + } + return 91; +} + +// @stderr in function bad_continue_after_terminal_throw +fun bad_continue_after_terminal_throw(x: int): int { + while (x > 0) { + throw 123; + var z = match (x) { + 1 => { continue; 10 } + else => 20 + }; + x = x - 1; + } + return x; +} diff --git a/tolk-tester/tests/invalid-semantics/err-impure-in-pure.tolk b/tolk-tester/tests/invalid-semantics/err-impure-in-pure.tolk deleted file mode 100644 index c2fd288dfa..0000000000 --- a/tolk-tester/tests/invalid-semantics/err-impure-in-pure.tolk +++ /dev/null @@ -1,41 +0,0 @@ -// @compilation_should_fail - -@pure -fun increment(mutate i: int) {} - -// @stderr an impure operation in a pure function -// @stderr modifying a global `gTens` in a pure function -// @stderr in function `f_pure` -@pure -fun f_pure(): int { - return f_impure(); - (gTens!).0 = 888; -} - -// @stderr_avoid in function `f_impure` -fun f_impure(): int { - (gTens!).0 = 888; - return 0; -} - -global gTens: (int, int); -global num: int; -global gShh: [slice, int]; - -// @stderr modifying a global `num` in a pure function -// @stderr modifying a global `gShh` in a pure function -// @stderr mutate gShh -// @stderr_avoid mutate i -// @stderr_avoid mutate sh -@pure -fun set_pure() { - (_, [num]) = (1, [2]); - // ok - var i = 0; - var sh: [string, int] = ["", 0]; - _ = increment(mutate i); - _ = increment(mutate sh.1); - // err - _ = increment(mutate gShh.1); -} - diff --git a/tolk-tester/tests/invalid-semantics/err-invalid-inline.tolk b/tolk-tester/tests/invalid-semantics/err-invalid-inline.tolk new file mode 100644 index 0000000000..5752188b04 --- /dev/null +++ b/tolk-tester/tests/invalid-semantics/err-invalid-inline.tolk @@ -0,0 +1,165 @@ +// @compilation_should_fail + +// @stderr function `badInlineCalledFromUnused` can't be inlined +// @stderr `@inline` is impossible, because of `return` inside a loop +// @stderr note: this prevents inlining +// @stderr repeat (1) { +// @stderr return x; // explanation +@inline +fun badInlineCalledFromUnused(x: int): int { + repeat (1) { + return x; // explanation + } + return 0; +} + +// @stderr function `badInlineRecursionA` can't be inlined +// @stderr `@inline` is impossible, because a function recursively calls itself +@inline +fun badInlineRecursionA(x: int): int { + if (x <= 0) { + return 0; + } + return badInlineRecursionB(x - 1); +} + +fun badInlineRecursionB(x: int): int { + return badInlineRecursionA(x); +} + +// @stderr function `badInlineMatch` can't be inlined +@inline +fun badInlineMatch(x: int): int { + return match (x) { + 0 => return 0, + else => 1, + }; +} + +// @stderr function `badInlineMatchStatement` can't be inlined +// @stderr `@inline` is impossible, because some `match` arms do `return`, some do not +@inline +fun badInlineMatchStatement(x: int | slice): int { + match (x) { + int => { + if (x == 0) { + return 1; + } + } + slice => {}, + } + return 0; +} + +// @stderr function `badInlinePartialIf` can't be inlined +// @stderr `@inline` is impossible, because `if/else` has complicated `return` and multiple exit points +@inline +fun badInlinePartialIf(x: int): int { + if (x > 0) { + if (x == 1) { + return 1; + } + } + return 2; +} + +// @stderr function `badInlineReturnInIfCond` can't be inlined +@inline +fun badInlineReturnInIfCond(x: int): int { + if ((match (x) { 0 => return 9, else => 1 }) > 0) { + return 2; + } + return 3; +} + +fun consume(a: int): void {} + +// @stderr function `badInlineReturnInCallArg` can't be inlined +// @stderr `@inline` is impossible, because of `return` in non-standard, unsupported position +@inline +fun badInlineReturnInCallArg(x: int): int { + consume(match (x) { 0 => return 1, else => 2 }); + return 0; +} + +// @stderr function `badInlineTry` can't be inlined +// @stderr `@inline` is impossible, because of `return` inside try/catch +@inline +fun badInlineTry(x: int): int { + try { + return x; + } catch { + return 0; + } +} + +// @stderr function `badInlineReference` can't be inlined +// @stderr `@inline` is impossible, because it's used as a reference +@inline +fun badInlineReference(): int { + return 1; +} + +// @stderr function `onBouncedMessage` can't be inlined +@inline +fun onBouncedMessage(in: InMessageBounced): void { + while (true) { + return; + } +} + +fun onInternalMessage(in: InMessage) { + badInlineReference; + return badInlineReference(); +} + +// @stderr function `badInlineMethodId` can't be inlined +// @stderr `@inline` is impossible, because `@inline` is incompatible with `@method_id` and `get fun` +@inline +@method_id(12345) +fun badInlineMethodId(): int { + return 1; +} + +// @stderr function `badInlineStandaloneBlock` can't be inlined +@inline +fun badInlineStandaloneBlock(x: int): int { + { + return x; + } + return 0; +} + +// @stderr function `badInlineReachableAfterTerminalIfElse` can't be inlined +// @stderr `@inline` is impossible, because a terminal `if/else/match` is followed by unreachable statements +@inline +fun badInlineReachableAfterTerminalIfElse(flag: int): int { + if (flag != 0) { + return 1; + } else { + return 2; + } + debug.print(99); +} + +// @stderr function `badInlineReachableAfterTerminalMatch` can't be inlined +@inline +fun badInlineReachableAfterTerminalMatch(x: int) { + match (x) { + -1 => return -1, + 0 => return 0, + else => return 100500, + } + return -100; +} + +// @stderr function `badInlineAfterTerminalThrow` can't be inlined +@inline +fun badInlineAfterTerminalThrow(x: int): int { + throw 123; + var z = match (x) { + 1 => { return 10; 10 } + else => 20 + }; + return z; +} diff --git a/tolk-tester/tests/invalid-semantics/err-modify-immutable.tolk b/tolk-tester/tests/invalid-semantics/err-modify-immutable.tolk index 7e47f64f0e..8e54d14d44 100644 --- a/tolk-tester/tests/invalid-semantics/err-modify-immutable.tolk +++ b/tolk-tester/tests/invalid-semantics/err-modify-immutable.tolk @@ -31,7 +31,7 @@ fun errMutateConstant() { // @stderr modifying immutable variable `myVal` // @stderr hint: it's declared `val myVal`, keyword 'val' means 'immutable' -// @stderr hint: declare `var myVal` to allow modifications +// @stderr declared as `val` here fun errMutateVal() { val myVal = 10; increment(mutate myVal); diff --git a/tolk-tester/tests/invalid-serialization/err-7307.tolk b/tolk-tester/tests/invalid-serialization/err-7307.tolk index af75d9e9c0..f23e269f92 100644 --- a/tolk-tester/tests/invalid-serialization/err-7307.tolk +++ b/tolk-tester/tests/invalid-serialization/err-7307.tolk @@ -2,7 +2,7 @@ struct Counter7Increment { byValue: int7 } struct Counter7Decrement { byValue: int7 } type MsgEitherCounter = Counter7Increment | Counter7Decrement; -fun f() { +fun onInternalMessage() { var msg = lazy MsgEitherCounter.fromSlice(createEmptySlice()); match (msg) { Counter7Increment => 1 as int32, diff --git a/tolk-tester/tests/invalid-serialization/err-7318.tolk b/tolk-tester/tests/invalid-serialization/err-7318.tolk new file mode 100644 index 0000000000..150418bab6 --- /dev/null +++ b/tolk-tester/tests/invalid-serialization/err-7318.tolk @@ -0,0 +1,21 @@ +struct Point { + x: int32; + y: int32; +} + +fun loadPoint(): Point { + if (random.uint256() != 0) { + return Point.fromSlice(createEmptySlice()); + } + return Point.fromSlice(createEmptySlice()); +} + +fun main() { + val p = lazy loadPoint(); + return p.x; +} + +/** +@compilation_should_fail +@stderr `lazy` operator can only be used with built-in functions like fromCell/fromSlice or simple wrappers over them + */ diff --git a/tolk-tester/tests/invalid-serialization/err-7390.tolk b/tolk-tester/tests/invalid-serialization/err-7390.tolk new file mode 100644 index 0000000000..19da7f72ea --- /dev/null +++ b/tolk-tester/tests/invalid-serialization/err-7390.tolk @@ -0,0 +1,20 @@ +type MyInt = int + +fun MyInt.packToBuilder(self, mutate b: builder) { + +} + +fun main() { + var x: MyInt = 42; + var s: slice = x.toCell().beginParse(); + var y: MyInt = MyInt.fromSlice(s); + return y; +} + +/** +@compilation_should_fail +@stderr auto-serialization via toCell() is not available for type `MyInt` +@stderr because type `int` is not serializable +@stderr x.toCell() +@stderr MyInt.fromSlice(s) + */ diff --git a/tolk-tester/tests/invalid-serialization/err-7394.tolk b/tolk-tester/tests/invalid-serialization/err-7394.tolk new file mode 100644 index 0000000000..c642a41091 --- /dev/null +++ b/tolk-tester/tests/invalid-serialization/err-7394.tolk @@ -0,0 +1,17 @@ +struct G { + x: int8 +} + +fun G.packToBuilder(self, mutate b: builder) { + b.storeInt(self.x, 8); +} + +fun main() { + return G { x: 1 }.toCell(); +} + +/** +@compilation_should_fail +@stderr can not deduce U for generic function `G.packToBuilder` +@stderr .toCell() +*/ diff --git a/tolk-tester/tests/invalid-serialization/err-7754.tolk b/tolk-tester/tests/invalid-serialization/err-7754.tolk deleted file mode 100644 index 08d1cf861f..0000000000 --- a/tolk-tester/tests/invalid-serialization/err-7754.tolk +++ /dev/null @@ -1,22 +0,0 @@ -type CustomInt = int; - -fun CustomInt.packToBuilder(self, mutate b: builder) { -} - -fun CustomInt.unpackFromSlice(mutate s: slice) { - if (s.remainingBitsCount() > 10) { - return 123; - } - return 600; -} - -fun main() { - var s = ""; - s.beginParse().loadAny(); -} - -/** -@compilation_should_fail -@stderr auto-serialization via loadAny() is not available for type `CustomInt` -@stderr because `CustomInt.unpackFromSlice()` can't be inlined; probably, it contains `return` in the middle - */ diff --git a/tolk-tester/tests/invalid-serialization/err-7777.tolk b/tolk-tester/tests/invalid-serialization/err-7777.tolk index 0c0eb6cce3..ebb4e4daa4 100644 --- a/tolk-tester/tests/invalid-serialization/err-7777.tolk +++ b/tolk-tester/tests/invalid-serialization/err-7777.tolk @@ -21,5 +21,9 @@ fun main() { /** @compilation_should_fail -@stderr ambiguous method, both `Pair.packToBuilder` and `Pair.packToBuilder` are applicable +@stderr ambiguous method, several serializers are applicable +@stderr note: this method is also applicable +@stderr fun Pair.packToBuilder +@stderr fun Pair.packToBuilder +@stderr fun Pair.packToBuilder */ diff --git a/tolk-tester/tests/invalid-serialization/err-7932.tolk b/tolk-tester/tests/invalid-serialization/err-7932.tolk new file mode 100644 index 0000000000..7caa6e34e3 --- /dev/null +++ b/tolk-tester/tests/invalid-serialization/err-7932.tolk @@ -0,0 +1,16 @@ +struct Point { + x: int8 + y: int8 +} + +fun main(s: slice): int { + // lazy declaration in match subject is only allowed when `match` is a statement + return 1 + match (val p = lazy Point.fromSlice(s)) { + Point => p.x + }; +} + +/** +@compilation_should_fail +@stderr error: incorrect `lazy` operator usage, it's not directly assigned to a variable + */ diff --git a/tolk-tester/tests/invalid-symbol/err-2218.tolk b/tolk-tester/tests/invalid-symbol/err-2218.tolk index a9a1e78874..42e1a30203 100644 --- a/tolk-tester/tests/invalid-symbol/err-2218.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2218.tolk @@ -9,5 +9,5 @@ fun main() { /** @compilation_should_fail @stderr method `demo` not found, but there is a global function named `demo` -@stderr (a function should be called `foo(arg)`, not `arg.foo()`) +@stderr a function should be called `foo(arg)`, not `arg.foo()` */ diff --git a/tolk-tester/tests/invalid-symbol/err-2715.tolk b/tolk-tester/tests/invalid-symbol/err-2715.tolk index 031d6a2a36..4d749f5670 100644 --- a/tolk-tester/tests/invalid-symbol/err-2715.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2715.tolk @@ -6,4 +6,6 @@ fun main(x: int): int { /** @compilation_should_fail @stderr redeclaration of local variable `x` +@stderr note: previous declaration is here +@stderr fun main */ diff --git a/tolk-tester/tests/invalid-symbol/err-2980.tolk b/tolk-tester/tests/invalid-symbol/err-2980.tolk index 1509420eab..1e1179d67f 100644 --- a/tolk-tester/tests/invalid-symbol/err-2980.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2980.tolk @@ -6,7 +6,8 @@ @compilation_should_fail On Linux/Mac, `realpath()` returns an error, and the error message is "cannot find file" On Windows, it fails after, on reading, with a message "cannot open file" -@stderr err-2980.tolk:2:14: error: Failed to import: +@stderr error: Failed to import: +@stderr /err-2980.tolk:2:14 @stderr import "unexisting.tolk"; @stderr ^^^^^^^^^^^^^^^^^ */ diff --git a/tolk-tester/tests/invalid-symbol/err-2984.tolk b/tolk-tester/tests/invalid-symbol/err-2984.tolk index 57ce027147..7bdf20bb14 100644 --- a/tolk-tester/tests/invalid-symbol/err-2984.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2984.tolk @@ -6,6 +6,6 @@ fun t(): int { /** @compilation_should_fail -@stderr in function `t` +@stderr in function t @stderr `seqno1` is a contract getter in `imported-contract-1.tolk` and is not accessible when imported */ diff --git a/tolk-tester/tests/invalid-symbol/err-2985.tolk b/tolk-tester/tests/invalid-symbol/err-2985.tolk index 655c305aeb..4f212fd3a8 100644 --- a/tolk-tester/tests/invalid-symbol/err-2985.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2985.tolk @@ -2,7 +2,7 @@ import "../imports/imported-contract-2" /** @compilation_should_fail -@stderr in function `sharedFunction2` +@stderr in function sharedFunction2 @stderr `seqno2` is a contract getter in `imported-contract-2.tolk` and is not accessible when imported @stderr return seqno2() */ diff --git a/tolk-tester/tests/invalid-symbol/err-2988.tolk b/tolk-tester/tests/invalid-symbol/err-2988.tolk index 7a7bd8f1f2..1e9a2de676 100644 --- a/tolk-tester/tests/invalid-symbol/err-2988.tolk +++ b/tolk-tester/tests/invalid-symbol/err-2988.tolk @@ -12,7 +12,7 @@ fun main(): int { /** @compilation_should_fail -@stderr in function `valueViaContract4` +@stderr in function valueViaContract4 @stderr `sharedValue4` is a contract getter in `imported-contract-4.tolk` and is not accessible when imported @stderr return sharedValue4() */ diff --git a/tolk-tester/tests/invalid-syntax/err-3004.tolk b/tolk-tester/tests/invalid-syntax/err-3004.tolk new file mode 100644 index 0000000000..bf22e9ceb8 --- /dev/null +++ b/tolk-tester/tests/invalid-syntax/err-3004.tolk @@ -0,0 +1,11 @@ +tolk 1.4.1 + +fun main() { + return 0; +} + +/** +@compilation_should_fail +@stderr the contract is written in Tolk v1.4.1, but you use Tolk compiler v1.5.0 +@stderr hint: replace this line with `tolk 1.5.0` + */ diff --git a/tolk-tester/tests/invalid-syntax/err-3898.tolk b/tolk-tester/tests/invalid-syntax/err-3898.tolk deleted file mode 100644 index 1180dbbff0..0000000000 --- a/tolk-tester/tests/invalid-syntax/err-3898.tolk +++ /dev/null @@ -1,13 +0,0 @@ -fun main(x: int) { - while (x > 0) { - if (x == 10) { - break; - } - x = x -1; - } -} - -/** -@compilation_should_fail -@stderr break/continue from loops are not supported yet - */ diff --git a/tolk-tester/tests/invalid-syntax/err-3921.tolk b/tolk-tester/tests/invalid-syntax/err-3921.tolk new file mode 100644 index 0000000000..b1f9a498d1 --- /dev/null +++ b/tolk-tester/tests/invalid-syntax/err-3921.tolk @@ -0,0 +1,8 @@ +fun main() {} + +@custom.orphan + +/** +@compilation_should_fail +@stderr unexpected end of file + */ diff --git a/tolk-tester/tests/invalid-typing/err-6101.tolk b/tolk-tester/tests/invalid-typing/err-6101.tolk new file mode 100644 index 0000000000..1aa11797d4 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6101.tolk @@ -0,0 +1,15 @@ +type UserId = int + +fun UserId.isPositive(self): bool { + return self > 0 +} + +fun main() { + var i: int = 1; + return i.isPositive(); +} + +/** +@compilation_should_fail +@stderr method `isPositive` not found for type `int` + */ diff --git a/tolk-tester/tests/invalid-typing/err-6249.tolk b/tolk-tester/tests/invalid-typing/err-6249.tolk index f5f4722d95..09d6ce5893 100644 --- a/tolk-tester/tests/invalid-typing/err-6249.tolk +++ b/tolk-tester/tests/invalid-typing/err-6249.tolk @@ -1,4 +1,4 @@ -fun cantUnifyBytesNAndSlice(n: slice, c: bytes16) { +fun cantUnifyBytesNAndSlice(n: slice, c: bits128) { __expect_type(random.uint256() ? n : c as slice, "slice"); __expect_type(random.uint256() ? n : c as slice?, "slice?"); random.uint256() ? n : c; @@ -6,6 +6,8 @@ fun cantUnifyBytesNAndSlice(n: slice, c: bytes16) { /** @compilation_should_fail -@stderr types of ternary branches are incompatible: `slice` and `bytes16` +@stderr types of ternary branches are incompatible: `slice` and `bits128` @stderr hint: maybe, you should use ` as ` to make them identical +@stderr note: this operand is `slice` +@stderr note: this operand is `bits128` */ diff --git a/tolk-tester/tests/invalid-typing/err-6274.tolk b/tolk-tester/tests/invalid-typing/err-6274.tolk index 6fe1deb800..68962e80e3 100644 --- a/tolk-tester/tests/invalid-typing/err-6274.tolk +++ b/tolk-tester/tests/invalid-typing/err-6274.tolk @@ -15,5 +15,6 @@ fun cantMatchAgainstNotVariant(a: int | slice | MBuilder) { /** @compilation_should_fail @stderr wrong pattern matching: `MCell` is not a variant of `int | slice | MBuilder` -@stderr MCell +@stderr wrong pattern matching: use `int` instead of `MInt2` +@stderr wrong pattern matching: use `slice` instead of `MSlice` */ diff --git a/tolk-tester/tests/invalid-typing/err-6314.tolk b/tolk-tester/tests/invalid-typing/err-6314.tolk index 99d16c3c2d..5cbe4c1b04 100644 --- a/tolk-tester/tests/invalid-typing/err-6314.tolk +++ b/tolk-tester/tests/invalid-typing/err-6314.tolk @@ -22,5 +22,5 @@ fun main(b: builder) { /** @compilation_should_fail -@stderr method `endCell` not found for type `cell | int` +@stderr can not call method for `builder` with object of type `cell | int` */ diff --git a/tolk-tester/tests/invalid-typing/err-6427.tolk b/tolk-tester/tests/invalid-typing/err-6427.tolk index 57d2951100..eee57d8b16 100644 --- a/tolk-tester/tests/invalid-typing/err-6427.tolk +++ b/tolk-tester/tests/invalid-typing/err-6427.tolk @@ -10,5 +10,8 @@ fun cantReturnVoidAndNonVoid(a: int | slice | builder) { /** @compilation_should_fail -@stderr mixing void and non-void returns in function +@stderr mixing void and non-void returns +@stderr int => { return; } +@stderr note: this return has a value +@stderr return 123; */ diff --git a/tolk-tester/tests/invalid-typing/err-6509.tolk b/tolk-tester/tests/invalid-typing/err-6509.tolk index 9482f6d0aa..574de7206a 100644 --- a/tolk-tester/tests/invalid-typing/err-6509.tolk +++ b/tolk-tester/tests/invalid-typing/err-6509.tolk @@ -6,7 +6,7 @@ fun cantApplyPlusOnNullable() { /** @compilation_should_fail -@stderr in function `calcSum` +@stderr in function calcSum @stderr can not apply operator `+` to `int?` and `int?` @stderr check on `null` first, or use unsafe operator `!` */ diff --git a/tolk-tester/tests/invalid-typing/err-6513.tolk b/tolk-tester/tests/invalid-typing/err-6513.tolk index e68bf89e5b..71a1f6bd3a 100644 --- a/tolk-tester/tests/invalid-typing/err-6513.tolk +++ b/tolk-tester/tests/invalid-typing/err-6513.tolk @@ -18,6 +18,6 @@ fun main() { /** @compilation_should_fail -@stderr wrong pattern matching: `OwnerId` is not a variant of `UserId | slice` +@stderr wrong pattern matching: use `UserId` instead of `OwnerId` @stderr OwnerId => {} */ diff --git a/tolk-tester/tests/invalid-typing/err-6519.tolk b/tolk-tester/tests/invalid-typing/err-6519.tolk index f276a0985e..7cfaf53519 100644 --- a/tolk-tester/tests/invalid-typing/err-6519.tolk +++ b/tolk-tester/tests/invalid-typing/err-6519.tolk @@ -9,4 +9,6 @@ fun main() { /** @compilation_should_fail @stderr type of operator `??` is `int | bool`; probably, it's not what you expected +@stderr note: this operand is `int?` +@stderr note: this operand is `bool` */ diff --git a/tolk-tester/tests/invalid-typing/err-6525.tolk b/tolk-tester/tests/invalid-typing/err-6525.tolk index d59a61f70d..be15986921 100644 --- a/tolk-tester/tests/invalid-typing/err-6525.tolk +++ b/tolk-tester/tests/invalid-typing/err-6525.tolk @@ -1,11 +1,14 @@ type UserId = int32 type OwnerId = int32 -fun main(c: bool, u: UserId, o: OwnerId): UserId | builder | slice { - return c ? (u as UserId | builder) : (o as OwnerId | slice); +fun main(v: UserId | slice) { + match (v) { + OwnerId => {} + slice => {} + } } /** @compilation_should_fail -@stderr can not convert type `OwnerId | slice` to ternary result type `UserId | builder | slice` +@stderr wrong pattern matching: use `UserId` instead of `OwnerId` */ diff --git a/tolk-tester/tests/invalid-typing/err-6526.tolk b/tolk-tester/tests/invalid-typing/err-6526.tolk deleted file mode 100644 index 58f4c5443c..0000000000 --- a/tolk-tester/tests/invalid-typing/err-6526.tolk +++ /dev/null @@ -1,14 +0,0 @@ -type UserId = int32 -type OwnerId = int32 - -fun main(c: bool, u: UserId, o: OwnerId): UserId { - return match (c) { - true => u, - false => o - }; -} - -/** -@compilation_should_fail -@stderr can not convert type `OwnerId` to match result type `UserId` - */ diff --git a/tolk-tester/tests/invalid-typing/err-6609.tolk b/tolk-tester/tests/invalid-typing/err-6609.tolk index dbc9a8f069..c9b28dac92 100644 --- a/tolk-tester/tests/invalid-typing/err-6609.tolk +++ b/tolk-tester/tests/invalid-typing/err-6609.tolk @@ -7,5 +7,5 @@ fun main() { /** @compilation_should_fail @stderr can not assign `()` to variable of type `int` -@stderr in lambda in function `main` +@stderr in lambda in function main */ diff --git a/tolk-tester/tests/invalid-typing/err-6610.tolk b/tolk-tester/tests/invalid-typing/err-6610.tolk index 4b8d41fc04..4d9d09b8c4 100644 --- a/tolk-tester/tests/invalid-typing/err-6610.tolk +++ b/tolk-tester/tests/invalid-typing/err-6610.tolk @@ -11,6 +11,6 @@ fun main() { /** @compilation_should_fail @stderr can not convert type `int` to return type `slice` -@stderr in lambda in function `some` +@stderr in lambda in function some @stderr return c */ diff --git a/tolk-tester/tests/invalid-typing/err-6640.tolk b/tolk-tester/tests/invalid-typing/err-6640.tolk new file mode 100644 index 0000000000..106efc3281 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6640.tolk @@ -0,0 +1,10 @@ +type TaggedInt = int + +fun choose(flag: bool, tagged: TaggedInt) { + return flag ? tagged : 0; +} + +/** +@compilation_should_fail +@stderr types of ternary branches are incompatible: `TaggedInt` and `int` + */ diff --git a/tolk-tester/tests/invalid-typing/err-6641.tolk b/tolk-tester/tests/invalid-typing/err-6641.tolk new file mode 100644 index 0000000000..08879e9623 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6641.tolk @@ -0,0 +1,11 @@ +type TaggedInt1 = int +type TaggedInt2 = int + +fun TaggedInt1?.choose(self, b: TaggedInt2) { + return self ?? b +} + +/** +@compilation_should_fail +@stderr type of operator `??` is `TaggedInt1`; probably, it's not what you expected + */ diff --git a/tolk-tester/tests/invalid-typing/err-6642.tolk b/tolk-tester/tests/invalid-typing/err-6642.tolk index 82563e3221..37454d9bf4 100644 --- a/tolk-tester/tests/invalid-typing/err-6642.tolk +++ b/tolk-tester/tests/invalid-typing/err-6642.tolk @@ -18,4 +18,6 @@ fun cantAssignFieldsOfDifferentTypes( @compilation_should_fail @stderr can not assign `()` to field of type `int` @stderr w1.value = w4.value +@stderr note: field declared here +@stderr value: T; */ diff --git a/tolk-tester/tests/invalid-typing/err-6729.tolk b/tolk-tester/tests/invalid-typing/err-6729.tolk new file mode 100644 index 0000000000..8b93dd23f9 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6729.tolk @@ -0,0 +1,13 @@ +fun fail(): never { + throw 1; +} + +fun main(): int { + val [unused: int, [a: int, b: int]] = [1, fail()]; + return 0; +} + +/** +@compilation_should_fail +@stderr can not assign `never` to `[...]` + */ diff --git a/tolk-tester/tests/invalid-typing/err-6820.tolk b/tolk-tester/tests/invalid-typing/err-6820.tolk index 0928582902..c1c9f91d37 100644 --- a/tolk-tester/tests/invalid-typing/err-6820.tolk +++ b/tolk-tester/tests/invalid-typing/err-6820.tolk @@ -1,9 +1,10 @@ -fun cantUnifyBytesNAndBytesM(n: bytes8, c: bytes16) { - __expect_type(random.uint256() ? n : c as bytes8, "bytes8"); +fun cantUnifyBitsNAndBitsM(n: bits64, c: bits128) { + __expect_type(random.uint256() ? n : c as bits64, "bits64"); random.uint256() ? n : c; } /** @compilation_should_fail -@stderr types of ternary branches are incompatible: `bytes8` and `bytes16` +@stderr types of ternary branches are incompatible: `bits64` and `bits128` +@stderr random.uint256() */ diff --git a/tolk-tester/tests/invalid-typing/err-6834.tolk b/tolk-tester/tests/invalid-typing/err-6834.tolk index 069e14576d..884d26a749 100644 --- a/tolk-tester/tests/invalid-typing/err-6834.tolk +++ b/tolk-tester/tests/invalid-typing/err-6834.tolk @@ -1,14 +1,17 @@ type SnakedCell = cell +type RopedCell = cell type MInt_v1 = int type MInt_v2 = int -fun m(a: SnakedCell, b: SnakedCell) { - a = b +fun SnakedCell.validate(self) {} + +fun m(b: RopedCell) { + b.validate() } /** @compilation_should_fail -@stderr can not assign `SnakedCell` to variable of type `SnakedCell` -@stderr hint: use `as` operator for UNSAFE casting: ` as SnakedCell` +@stderr method `validate` not found for type `RopedCell` +@stderr but it exists for type `SnakedCell` */ diff --git a/tolk-tester/tests/invalid-typing/err-6921.tolk b/tolk-tester/tests/invalid-typing/err-6921.tolk index 076b780422..98b30c5bf1 100644 --- a/tolk-tester/tests/invalid-typing/err-6921.tolk +++ b/tolk-tester/tests/invalid-typing/err-6921.tolk @@ -11,7 +11,7 @@ fun foo(value: X) : X { /** @compilation_should_fail -@stderr in function `bar` +@stderr in function bar @stderr can not convert type `int` to return type `slice` @stderr return 1 */ diff --git a/tolk-tester/tests/invalid-typing/err-6937.tolk b/tolk-tester/tests/invalid-typing/err-6937.tolk index b37e3cd722..cbc845254a 100644 --- a/tolk-tester/tests/invalid-typing/err-6937.tolk +++ b/tolk-tester/tests/invalid-typing/err-6937.tolk @@ -1,13 +1,13 @@ fun getAnySlice() { return beginCell().endCell().beginParse(); } fun cantAssignSliceToBytesN() { - var c1: bytes16 = getAnySlice() as bytes16; // ok - var c2: bytes16 = getAnySlice(); + var c1: bits128 = getAnySlice() as bits128; // ok + var c2: bits128 = getAnySlice(); } /** @compilation_should_fail -@stderr can not assign `slice` to variable of type `bytes16` -@stderr var c2: bytes16 = getAnySlice(); -@stderr hint: use `as` operator for UNSAFE casting: ` as bytes16` +@stderr can not assign `slice` to variable of type `bits128` +@stderr var c2: bits128 = getAnySlice(); +@stderr hint: use `as` operator for UNSAFE casting: ` as bits128` */ diff --git a/tolk-tester/tests/invalid-typing/err-6994.tolk b/tolk-tester/tests/invalid-typing/err-6994.tolk new file mode 100644 index 0000000000..5b6c406e06 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6994.tolk @@ -0,0 +1,14 @@ +type Same = int32 +type Wrapper = int32 + +fun use(mutate v: Wrapper): void { v = 9; } + +fun main() { + var x: Same = 1; + use(mutate x); +} + +/** +@compilation_should_fail +@stderr can not pass `Same` to mutate `Wrapper`, because mutation is not type compatible + */ diff --git a/tolk-tester/tests/invalid-typing/err-6995.tolk b/tolk-tester/tests/invalid-typing/err-6995.tolk new file mode 100644 index 0000000000..30c2096975 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-6995.tolk @@ -0,0 +1,16 @@ +type Same = int32 +type Wrapper = int32 + +fun use(mutate v: Wrapper): void { v = 9; } + +fun main(flag: bool) { + var x: Same | slice = flag ? (1 as Same) : beginCell().endCell().beginParse(); + if (x is Same) { + use(mutate x); + } +} + +/** +@compilation_should_fail +@stderr can not pass `Same | slice` to mutate `Wrapper`, because mutation is not type compatible + */ diff --git a/tolk-tester/tests/invalid-typing/err-incompatible-mutate.tolk b/tolk-tester/tests/invalid-typing/err-incompatible-mutate.tolk index 9a7351913d..c65a874293 100644 --- a/tolk-tester/tests/invalid-typing/err-incompatible-mutate.tolk +++ b/tolk-tester/tests/invalid-typing/err-incompatible-mutate.tolk @@ -65,9 +65,10 @@ fun firstMutateSecondNot(x77: int, mutate y77: int) { // @stderr incorrect `mutate`, since `firstMutateSecondNot` does not mutate parameter `x77` // @stderr function `firstMutateSecondNot` mutates parameter `y77` -// @stderr you need to specify `mutate` when passing an argument, like `mutate b77` -// @stderr function `slice.loadUint` mutates parameter `slice` -// @stderr incorrect `mutate`, since `slice.loadUint` does not mutate parameter `int` +// @stderr specify `mutate` when passing an argument, like `mutate b77` +// @stderr function `slice.loadUint` mutates parameter `self` +// @stderr incorrect `mutate`, since `slice.loadUint` does not mutate parameter `len` +// @stderr note: parameter declared here fun invalidMutateArgumentPassed(a77: int, b77: int, p77: slice) { firstMutateSecondNot(mutate a77, b77); slice.loadUint(p77, mutate b77); diff --git a/tolk-tester/tests/invalid-typing/err-mixed-1.tolk b/tolk-tester/tests/invalid-typing/err-mixed-1.tolk index 8685d125d0..e82227db25 100644 --- a/tolk-tester/tests/invalid-typing/err-mixed-1.tolk +++ b/tolk-tester/tests/invalid-typing/err-mixed-1.tolk @@ -113,7 +113,7 @@ fun unionTypesNotAllowedInIs(a: int | slice | builder) { type UserId = int type OwnerId = int -// @stderr wrong pattern matching: `OwnerId` is not a variant of `UserId` +// @stderr wrong pattern matching: use `UserId` instead of `OwnerId` fun f(v: UserId) { match (v) { OwnerId => {} @@ -135,3 +135,17 @@ fun invalidMapKeyInsideCasts() { fun tooFewArguments() { add1(); } + +// @stderr function `badInlineBothBranchesGuard` can't be inlined +// @stderr `@inline` is impossible, because `if/else` has complicated `return` and multiple exit points +@inline +fun badInlineBothBranchesGuard(x: int): int { + if (x > 0) { + if (x == 1) { return 1; } + x += 1; + } else { + if (x == -1) { return 2; } + x -= 1; + } + return x; +} diff --git a/tolk-tester/tests/invalid-typing/err-mixed-2.tolk b/tolk-tester/tests/invalid-typing/err-mixed-2.tolk index 10ce5b693b..b591d992c5 100644 --- a/tolk-tester/tests/invalid-typing/err-mixed-2.tolk +++ b/tolk-tester/tests/invalid-typing/err-mixed-2.tolk @@ -82,3 +82,17 @@ fun notIntegerLogicalNot(i: int, c: coins, i16: int16) { var b = !c; var d = !i16; } + +type V15UserIdForIs = int + +// @stderr wrong pattern matching: use `int` instead of `V15UserIdForIs` +fun aliasV15IsDownwardInt(i: int) { + return i is V15UserIdForIs; +} + +type V15StrangeInt = int + +// @stderr wrong pattern matching: use `V15StrangeInt` instead of `V15StrangeInt` +fun aliasV15GenericSibling(v: V15StrangeInt) { + return v is V15StrangeInt; +} diff --git a/tolk-tester/tests/invalid-typing/err-mixed-3.tolk b/tolk-tester/tests/invalid-typing/err-mixed-3.tolk index 905219f0a7..0ee3695419 100644 --- a/tolk-tester/tests/invalid-typing/err-mixed-3.tolk +++ b/tolk-tester/tests/invalid-typing/err-mixed-3.tolk @@ -35,5 +35,28 @@ type MInt = int type Pair2_v1 = | (int, int) type Pair2_v2 = (MInt, MInt) -// @stderr can not convert type `Pair2_v2` to ternary result type `Pair2_v1` -const INVALID_TERNARY = 10>3 ? (1, 2) as Pair2_v1 : (1, 2) as Pair2_v2 +type V15UserIdInUnion = int +type V15OwnerIdInUnion = int +type V15SomeThing = V15UserIdInUnion | slice + +// @stderr wrong pattern matching: use `V15UserIdInUnion` instead of `V15OwnerIdInUnion` +fun aliasV15UnionIsSpelling(v: V15SomeThing) { + return v is V15OwnerIdInUnion; +} + +struct V15MsgA { + value: int +} + +type V15A1 = V15MsgA +type V15A2 = V15A1 +type V15A3 = V15A2 +type V15SomeMsg = V15A2 | slice + +// @stderr wrong pattern matching: use `V15A2` instead of `V15A3` +fun aliasV15UnionMatchChain(v: V15SomeMsg) { + return match (v) { + V15A3 => 1, + slice => 2, + }; +} diff --git a/tolk-tester/tests/invalid-typing/err-mixed-4.tolk b/tolk-tester/tests/invalid-typing/err-mixed-4.tolk new file mode 100644 index 0000000000..1668f88d07 --- /dev/null +++ b/tolk-tester/tests/invalid-typing/err-mixed-4.tolk @@ -0,0 +1,57 @@ +// @compilation_should_fail +// here are mixed errors from different compilation stages, to check they are fired all at once + +enum Color { Red, Green, Blue } + +// @stderr can not apply operator `+` to `Color` and `int` +fun errTypingOperator(c: Color) { + c + 1; +} + +// @stderr `break` used outside a loop +fun errBreakOutside() { + break; +} + +// @stderr function `errInlineReturnInLoop` can't be inlined +// @stderr `@inline` is impossible, because of `return` inside a loop +// @stderr note: this prevents inlining +// @stderr repeat (1) { +// @stderr return x; // here +@inline +fun errInlineReturnInLoop(x: int): int { + repeat (1) { + return x; // here + } + return 0; +} + +// @stderr `continue` used outside a loop +fun errContinueInRepeatCount() { + repeat (2) { + repeat (match (true) { + true => { + continue; + 1 + } + false => 1 + }) { + } + } +} + +// @stderr `return` is not allowed in a loop condition +fun errReturnInDoWhileCond(takeBreak: bool, takeReturn: bool): int { + do { + if (takeBreak) { + break; + } + } while (match (takeReturn) { + true => { + return 72; + true + } + false => false + }); + return 92; +} diff --git a/tolk-tester/tests/invalid-typing/err-pattern-matching.tolk b/tolk-tester/tests/invalid-typing/err-pattern-matching.tolk index d6d672f1b9..791496404b 100644 --- a/tolk-tester/tests/invalid-typing/err-pattern-matching.tolk +++ b/tolk-tester/tests/invalid-typing/err-pattern-matching.tolk @@ -68,8 +68,8 @@ type WrapperAlias = Wrapper; // @stderr wrong pattern matching: duplicated `WrapperAlias` fun duplicateInStrangeUnion(w: int | WrapperAlias | WrapperAlias) { match (w) { - MyInt => {} - Wrapper => {} + int => {} + WrapperAlias => {} WrapperAlias => {} } } @@ -83,17 +83,18 @@ type AView2 = AForMatch type AView3 = AForMatch type AView4 = AForMatch -// @stderr wrong pattern matching: duplicated `AView2` +// @stderr wrong pattern matching: duplicated `AForMatch` // @stderr `match` does not cover all possible types; missing types are: `BForMatch` fun duplicateUnionVariantViaDifferentAliases(s: slice) { var msg: MessageForMatch = lazy MessageForMatch.fromSlice(s); match (msg) { - AView1 => {} - AView2 => {} + AForMatch => {} + AForMatch => {} } } -// @stderr wrong pattern matching: duplicated `AView4` +// @stderr wrong pattern matching: use `AForMatch` instead of `AView3` +// @stderr wrong pattern matching: use `AForMatch` instead of `AView4` fun duplicateNonUnionVariantViaDifferentAliases(msg: AForMatch) { match (msg) { AView3 => {} diff --git a/tolk-tester/tests/invalid-typing/err-type-check-2.tolk b/tolk-tester/tests/invalid-typing/err-type-check-2.tolk index ac4426d7e9..8ef6d069ca 100644 --- a/tolk-tester/tests/invalid-typing/err-type-check-2.tolk +++ b/tolk-tester/tests/invalid-typing/err-type-check-2.tolk @@ -55,14 +55,14 @@ fun Pair.compareWith(self, f: U, s: V) { // @stderr can not apply operator `==` to `int` and `int?` -// @stderr in function `Pair.demoInvalid` +// @stderr in function Pair.demoInvalid // @stderr check on `null` first, or use unsafe operator `!` fun instantiatePairAndCall(p: Pair) { p.demoInvalid(); } // @stderr can not apply operator `==` to `address` and `address?` -// @stderr in function `Pair.compareWith` +// @stderr in function Pair.compareWith // @stderr check on `null` first, or use unsafe operator `!` fun callGenericMethodOnGenericReceiver(p: Pair) { p.compareWith(null, null); diff --git a/tolk-tester/tests/lazy-algo-tests.tolk b/tolk-tester/tests/lazy-algo-tests.tolk index d6f7f278f1..d7bc6b4343 100644 --- a/tolk-tester/tests/lazy-algo-tests.tolk +++ b/tolk-tester/tests/lazy-algo-tests.tolk @@ -78,8 +78,9 @@ fun Point.getX(self) { return self.x; } +@noinline fun Point.getXNoInline(self) { - __expect_inline(false); // due to returns in the middle + __expect_inline(false); if (10>3) { return self.x; } @@ -223,7 +224,8 @@ fun loadWalletStorage() { } fun WalletStorage.load() { - return WalletStorage.fromCell(createEmptyCell()); + val emptyCell = createEmptyCell(); + return WalletStorage.fromCell(emptyCell); } // Uses only `other`, not `self`. @@ -1572,7 +1574,7 @@ fun algo316() { fun algo317(s: slice): int8 { val a = lazy A.fromSlice(s); __expect_lazy("[a] load a1 a2 a3"); - return a.getSecondField(); + return (a as AOrB).getSecondField(); } diff --git a/tolk-tester/tests/lazy-load-tests.tolk b/tolk-tester/tests/lazy-load-tests.tolk index a12276d5bd..96131e4ea3 100644 --- a/tolk-tester/tests/lazy-load-tests.tolk +++ b/tolk-tester/tests/lazy-load-tests.tolk @@ -60,6 +60,11 @@ struct Point { y: int8; } +@noinline +fun loadLazyPointNoInline(): Point { + return Point.fromSlice("0102".hexToSlice()); +} + fun Point.getX(self) { return self.x } @@ -258,6 +263,7 @@ struct WithInternalAddress { } global gModByCustom: int; +global lazyLoadWrapperCounter: int; type MagicGlobalModifier = (); @@ -346,10 +352,26 @@ fun loadWalletStorage() { return WalletStorage.fromSlice(stringHexToSlice("8000003d800000e4000000000000000000000000000000000000000000000000000000000000000020_")); } +fun loadWalletStorageViaLocalSlice() { + val storageSlice = stringHexToSlice("8000003d800000e4000000000000000000000000000000000000000000000000000000000000000020_"); + return WalletStorage.fromSlice(storageSlice); +} + fun WalletStorage.load() { return WalletStorage.fromCell(contract.getFakeData(777)); } +fun WalletStorage.loadWithPrelude() { + val p = lazy Point.fromSlice(stringHexToSlice("0102")); + __expect_lazy("[p] load x"); + lazyLoadWrapperCounter = p.x; + if (lazyLoadWrapperCounter == 1) { + lazyLoadWrapperCounter = 777; + } + val storageCell = contract.getFakeData(lazyLoadWrapperCounter); + return WalletStorage.fromCell(storageCell); +} + fun inc8(mutate x: int8) { x += 1; } @@ -1125,6 +1147,26 @@ fun demo163(nSkip: int) { return (x, (o.rest.skipBits(nSkip), o.rest.remainingBitsCount()).1); } +@method_id(164) +fun testNoInlineLazyWrapper(): int { + val p = lazy loadLazyPointNoInline(); + __expect_lazy("[p] skip (bits8), load y"); + return p.y; +} + +fun loadRestPayloadOfPoint() { + val header = lazy WithOneRestField.fromSlice("000000FF01".hexToSlice()); + __expect_lazy("[header] skip (bits32), load rest"); + return Point.fromSlice(header.rest); +} + +@method_id(165) +fun testLoadLazyOfLazy() { + val payloadP = lazy loadRestPayloadOfPoint(); + __expect_lazy("[payloadP] load x"); + return payloadP.x; +} + @method_id(200) fun demo200() { @@ -1914,6 +1956,21 @@ fun demo417(s: slice): int32 { }; } +@method_id(418) +fun demo418() { + lazyLoadWrapperCounter = 0; + val st = lazy WalletStorage.loadWithPrelude(); + __expect_lazy("[st] skip (bits1), load seqno"); + return (st.seqno, lazyLoadWrapperCounter); +} + +@method_id(419) +fun demo419() { + val st = lazy loadWalletStorageViaLocalSlice(); + __expect_lazy("[st] skip (bits33), load walletId"); + return st.walletId; +} + fun main() { val s = stringHexToSlice("0102"); @@ -2024,6 +2081,7 @@ fun main() { @testcase | 161 | | 1 @testcase | 162 | 8 | 17 @testcase | 163 | 4 | 240 4 +@testcase | 165 | | 1 @testcase | 200 | | 1 @testcase | 201 | | 1 2 -1 @@ -2099,6 +2157,8 @@ fun main() { @testcase | 416 | x{52} | 0 @testcase | 417 | x{000000154_} | 42 @testcase | 417 | x{800000154_} | 42 +@testcase | 418 | | 777 777 +@testcase | 419 | | 456 @fif_codegen """ @@ -2169,7 +2229,7 @@ fun main() { """ demo134() PROC:<{ // x{010f} PUSHSLICE // lazyS - x{01} SDBEGINSQ // lazyS '6 + x{01} SDBEGINSQ // lazyS '7 134 THROWIFNOT // lazyS 8 PLDI // cc }> @@ -2225,11 +2285,11 @@ fun main() { """ demo203() PROC:<{ // x{010203040506070809} PUSHSLICE // lazyS - DUP // '14 lazyS - 8 PLDU // '14 o.f1 - 1 EQINT // '14 '18 - IFJMP:<{ // '14 - NEWC // '14 b + DUP // '15 lazyS + 8 PLDU // '15 o.f1 + 1 EQINT // '15 '19 + IFJMP:<{ // '15 + NEWC // '15 b STSLICE // b BTOS // s """ @@ -2238,16 +2298,16 @@ fun main() { """ demo206() PROC:<{ // x{010203040506070809} PUSHSLICE // lazyS - DUP // '14 lazyS - 8 LDU // '14 o.f1 lazyS - 8 LDU // '14 o.f1 o.f2 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 o.f4 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 lazyS - 8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 lazyS - 8 PLDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 o.f9 + DUP // '15 lazyS + 8 LDU // '15 o.f1 lazyS + 8 LDU // '15 o.f1 o.f2 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 o.f4 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 o.f4 o.f5 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 lazyS + 8 LDU // '15 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 lazyS + 8 PLDU // '15 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 o.f9 """ @fif_codegen @@ -2298,36 +2358,34 @@ fun main() { """ demo404() PROC:<{ x{12345678} SDBEGINSQ - IF:<{ + IFJMP:<{ 32 PLDI 10 ADDCONST - }>ELSE<{ - x{23456789} SDBEGINSQ + }> + x{23456789} SDBEGINSQ + IFJMP:<{ + DROP + 9 PUSHINT + }> + x{34567890} SDBEGINSQ + IFJMP:<{ + 32 LDU + 8 LDU + NIP + 8 PLDI + OVER + 100 GTINT IF:<{ - DROP - 9 PUSHINT - }>ELSE<{ - x{34567890} SDBEGINSQ - IFNOTJMP:<{ - 16 PUSHPOW2DEC - THROWANY - }> - 32 LDU - 8 LDU - NIP - 8 PLDI - OVER - 100 GTINT - IF:<{ - 100 PUSHINT - s2 POP - }> - 100 EQINT - IF:<{ - 1 LSHIFT# - }> + 100 PUSHINT + s2 POP + }> + 100 EQINT + IFJMP:<{ + 1 LSHIFT# }> }> + 16 PUSHPOW2DEC + THROWANY }> """ diff --git a/tolk-tester/tests/logical-operators.tolk b/tolk-tester/tests/logical-operators.tolk index 753249017d..fac7160f72 100644 --- a/tolk-tester/tests/logical-operators.tolk +++ b/tolk-tester/tests/logical-operators.tolk @@ -388,8 +388,17 @@ fun main() { compileTimeEval1() PROC:<{ // x DUP // x x 0 EQINT // x '2 + OVER // x '2 x + 0 EQINT // x '2 '4 + DROP // x '2 FALSE // x '2 '6 + s2 PUSH // x '2 '6 x + 0 EQINT // x '2 '6 '8 + DROP // x '2 '6 TRUE // x '2 '6 '10 + s3 PUSH // x '2 '6 '10 x + 0 NEQINT // x '2 '6 '10 '12 + DROP // x '2 '6 '10 FALSE // x '2 '6 '10 '14 s0 s4 XCHG // '14 '2 '6 '10 x 0 EQINT // '14 '2 '6 '10 '16 @@ -468,12 +477,11 @@ These are moments of future optimizations. For now, it's more than enough. """ testAndSimpleCodegen() PROC:<{ // a b SWAP // b a - IF:<{ // b + IFJMP:<{ // b 0 NEQINT // '2 - }>ELSE<{ // b - DROP // - 0 PUSHINT // '2=0 - }> + }> // b + DROP // + 0 PUSHINT // '2=0 }> """ @@ -482,13 +490,12 @@ These are moments of future optimizations. For now, it's more than enough. testOrSimpleCodegen() PROC:<{ // a b SWAP // b a 0 GTINT // b '3 - IF:<{ // b + IFJMP:<{ // b DROP // -1 PUSHINT // '4=-1 - }>ELSE<{ // b - 0 GTINT // '7 - 0 NEQINT // '4 - }> + }> // b + 0 GTINT // '6 + 0 NEQINT // '4 }> """ diff --git a/tolk-tester/tests/maps-tests.tolk b/tolk-tester/tests/maps-tests.tolk index 913b1531b9..8f84ba78bf 100644 --- a/tolk-tester/tests/maps-tests.tolk +++ b/tolk-tester/tests/maps-tests.tolk @@ -17,6 +17,10 @@ struct JustAddress { type MInt32 = int32 type MSlice = slice +fun MapEntry.loadValue(self): MSlice { + return self.rawValue! +} + struct Point { x: int8 y: int8 @@ -717,6 +721,24 @@ fun test50(x: int): int { return keepBuilder.endCell().beginParse().loadUint(16) + storedSlice.loadUint(16); } +@method_id(151) +fun test51() { + gPackCalled = 0; + var m = map> []; + m.set(1, { value: 42 }); // calls custom pack, writes 8+7 bits + return (m.toLowLevelDict().iDictGet(32, 1).0!.remainingBitsCount(), gPackCalled); +} + +@method_id(152) +fun test52() { + gPackCalled = 0; + var ll = createEmptyDict(); + ll.iDictSet(32, 1, beginCell().storeUint(255, 8).storeInt(42, 7).toCell().beginParse()); + var m = createMapFromLowLevelDict>(ll); + return (m.mustGet(1), gPackCalled); +} + + fun main() { __expect_type(createEmptyMap, "() -> map"); } @@ -773,6 +795,8 @@ fun main() { @testcase | 148 | | 111 @testcase | 149 | | -1 @testcase | 150 | 4660 | 48350 +@testcase | 151 | | 15 1 +@testcase | 152 | | 42 10 @fif_codegen """ @@ -842,9 +866,9 @@ fun main() { test25() PROC:<{ // NEWDICT // m 1 PUSHINT // m '3=1 - x{0101} PUSHSLICE // m '3=1 '4 + x{0101} PUSHSLICE // m '3=1 '5 s0 s2 XCHG - 8 PUSHINT // '4 '3=1 m '8=8 + 8 PUSHINT // '5 '3=1 m '9=8 DICTISET // m }> """ diff --git a/tolk-tester/tests/match-by-expr-tests.tolk b/tolk-tester/tests/match-by-expr-tests.tolk index cd7cc5c7fa..2f0fbf4a37 100644 --- a/tolk-tester/tests/match-by-expr-tests.tolk +++ b/tolk-tester/tests/match-by-expr-tests.tolk @@ -231,12 +231,12 @@ fun test16(x: int) { type asdf = int; fun main() { - match (10) { + match (10 as asdf) { asdf => 1, // it's match by type }; var asdf = 5; - return match (10) { + return match (10 as asdf) { asdf => 2, // also match by type, regardless of local var }; } @@ -283,24 +283,21 @@ fun main() { test1() PROC:<{ // x DUP // x x 1 EQINT // x '3 - IF:<{ // x + IFJMP:<{ // x DROP // 100 PUSHINT // '1=100 - }>ELSE<{ // x - DUP // x x - 2 EQINT // x '6 - IF:<{ // x - DROP // - 200 PUSHINT // '1=200 - }>ELSE<{ // x - 3 EQINT // '11 - IF:<{ // - 300 PUSHINT // '1=300 - }>ELSE<{ // - 400 PUSHINT // '1=400 - }> - }> - }> + }> // x + DUP // x x + 2 EQINT // x '6 + IFJMP:<{ // x + DROP // + 200 PUSHINT // '1=200 + }> // x + 3 EQINT // '11 + IFJMP:<{ // + 300 PUSHINT // '1=300 + }> // + 400 PUSHINT // '1=400 }> """ @@ -327,6 +324,7 @@ fun main() { @fif_codegen """ test8() PROC:<{ // x + 0 MULCONST DROP // 30 PUSHINT // r1 }> @@ -363,10 +361,10 @@ fun main() { @fif_codegen """ 1 EQINT // '6 - IFNOTJMP:<{ // - 5 THROW + IFJMP:<{ // + 200 PUSHINT // '1=200 }> // - 200 PUSHINT // '1=200 + 5 THROW """ */ diff --git a/tolk-tester/tests/meaningful-1.tolk b/tolk-tester/tests/meaningful-1.tolk index 45278ad647..f42c7c6f9f 100644 --- a/tolk-tester/tests/meaningful-1.tolk +++ b/tolk-tester/tests/meaningful-1.tolk @@ -15,6 +15,7 @@ type MyMessage = | CounterReset; fun fakeParseMessage(mode: int): MyMessage { + __expect_inline(true); if (mode == 1) { return CounterIncrement1 {}; } else if (mode > 0) { @@ -44,6 +45,22 @@ fun onInternalMessage(curCounter: int, mode: int) { @fif_codegen """ onInternalMessage() PROC:<{ // curCounter mode - fakeParseMessage() CALLDICT // curCounter m.USlot1 m.UTag + DUP + 1 EQINT + IF:<{ + DROP + PUSHNULL + 129 PUSHINT + }>ELSE<{ + DUP + 0 GTINT + IF:<{ + 130 PUSHINT + }>ELSE<{ + DROP + 0 PUSHINT + 131 PUSHINT + }> + }> """ */ diff --git a/tolk-tester/tests/methods-tests.tolk b/tolk-tester/tests/methods-tests.tolk index 61562c79ed..774ffcdbeb 100644 --- a/tolk-tester/tests/methods-tests.tolk +++ b/tolk-tester/tests/methods-tests.tolk @@ -45,7 +45,7 @@ fun Pair.compareWith(self, f: U, s: V) { return self.first == f && self.second == s; } -type Tuple2Int = [int, int]; +type Tuple2Int = [int, MInt]; fun [int, int].getLast(self) { return self.1; } fun [int, MInt].assignLast(mutate self, v: int) { self.1 = v; } fun Tuple2Int.getSum(self) { return self.0 + self.1; } @@ -66,7 +66,7 @@ fun createWrapper(item: T) { return Wrapper.create(item); } @method_id(101) fun test1() { - var i = int.zero() + MInt.zero(); + var i: MInt = int.zero() + MInt.zero(); i = i.plus1(); return (i.plus1(), i, i.plusN(5), i.plusN(i.plus1())); } @@ -74,7 +74,7 @@ fun test1() { @method_id(102) fun test2(a: int8, b: int16) { var c = b as coins as coins?; - return (a.plus1(), b.plusN(a), c!.plus1()); + return (a.plus1(), (b as MInt).plusN(a), c!.plus1()); } @method_id(103) @@ -99,7 +99,7 @@ fun test4() { @method_id(105) fun test5(a: MInt, b: int) { - var t = [a,b] as [MInt, int]; + var t: Tuple2Int = [a,b]; return (t.getSum(), t.assignLast(a+b), t.getSum(), Tuple2Int.getLast(t)); } @@ -197,8 +197,8 @@ fun test12() { __expect_type(null.arbitraryMethod(), "never"); __expect_type((10 as coins?).arbitraryMethod(), "never"); - __expect_type(Wrapper { item: 10 }.arbitraryMethod(), "Wrapper"); - __expect_type(Wrapper { item: 10 as int8 }.arbitraryMethod(), "Wrapper"); + __expect_type(Wrapper { item: 10 }.arbitraryMethod(), "Wrapper"); + __expect_type(Wrapper { item: 10 as int8 }.arbitraryMethod(), "Wrapper"); __expect_type(Wrapper { item: 10 as int8 }.arbitraryMethod(), "builder"); __expect_type(Wrapper { item: Wrapper { item: 10 } }.arbitraryMethod(), "builder"); @@ -261,7 +261,7 @@ fun CounterMsg.onInternalMessage(self) { fun test16() { var inc: CounterIncrement = { inc_by: 100 }; var msg = inc as CounterMsg; - return (inc.onInternalMessage(), msg.onInternalMessage(), CounterMsg.onInternalMessage(CounterReset{initial_value:5})); + return ((inc as CounterMsg).onInternalMessage(), msg.onInternalMessage(), CounterMsg.onInternalMessage(CounterReset{initial_value:5})); } fun int.`0`() { return 0; } @@ -358,6 +358,17 @@ fun test21(zero: int) { return zero.eqSelfShadowed().eqSelfShadowed(); } +fun test22(b: BalanceList) { + if (b == null) { return } + else { b = createEmptyCell() as cell? } + __expect_type(b, "BalanceList"); + + if (b == null) { return } + __expect_type(b, "cell"); + // b is smart-casted to `cell`, but `b.validate` is not ambiguous since `b` is originally `BalanceList` + __expect_type(b.validate(), "int1"); +} + fun main() {} /** diff --git a/tolk-tester/tests/mutate-methods.tolk b/tolk-tester/tests/mutate-methods.tolk index bc9558c610..f9c8263bfb 100644 --- a/tolk-tester/tests/mutate-methods.tolk +++ b/tolk-tester/tests/mutate-methods.tolk @@ -167,12 +167,12 @@ global t107_2: int @method_id(107) fun testMutableTensor() { - var t = (40, 50); + var t: Pair2 = (40, 50); t.updateTwoItems(10); updateTwoItems(mutate t, 10); t107_1 = 1; t107_2 = 2; - var globalsPair = (t107_1, t107_2); + var globalsPair = (t107_1, t107_2) as Pair2; globalsPair.updateTwoItems(10); updateTwoItems(mutate globalsPair, 10); val before = t107_1; @@ -191,11 +191,9 @@ fun myStoreUint(mutate b: builder, x: int, len: int): void fun storeUint(mutate b: builder, x: int, len: int): void asm(x b len) "STUX"; -@pure fun builder.myStoreU32(mutate self, x: int): self { return self.storeUint(x, 32); } -@pure fun myStoreU32(mutate b: builder, x: int) { b.storeUint(x, 32); } @@ -491,28 +489,35 @@ fun main(){ @fif_codegen """ testStoreUintPureUnusedResult() PROC:<{ - 0 PUSHINT // '10=0 + NEWC + STIX + BTOS + 32 PLDU + DROP + 0 PUSHINT }> """ @fif_codegen """ testStoreUintImpureUnusedResult() PROC:<{ - NEWC // b - STIX // '2 - DROP // - 0 PUSHINT // '10=0 + NEWC + STIX + BTOS + 32 PLDU + DROP + 0 PUSHINT }> """ @fif_codegen """ testReceiverAndNot() PROC:<{ // - 30 PUSHINT // '2=30 - 40 PUSHINT // obj.f1=30 obj.f2=40 + 30 PUSHINT + 40 PUSHINT OVER - 0 PUSHINT // a1.f1=30 a2.f2=40 a2.f1=30 a1.f2=0 - s0 s2 XCHG // a1.f1=30 a1.f2=0 a2.f1=30 a2.f2=40 + 0 PUSHINT + s0 s2 XCHG }> """ diff --git a/tolk-tester/tests/never-type-tests.tolk b/tolk-tester/tests/never-type-tests.tolk index 47b6fbd9cc..e5ef4ca727 100644 --- a/tolk-tester/tests/never-type-tests.tolk +++ b/tolk-tester/tests/never-type-tests.tolk @@ -44,6 +44,23 @@ fun test2(): int { } } +@noinline +fun abortWith42() { + throw 42; +} + +@method_id(103) +fun testPreservedNeverCall(cond: bool): int { + var x = 0; + if (cond) { + abortWith42(); + } else { + x = 1; + } + return x; +} + + fun main() { __expect_type(test1, "(int?) -> int"); __expect_type(alwaysThrows(), "never"); @@ -54,6 +71,7 @@ fun main() { /** @testcase | 101 | null | 123 @testcase | 102 | | 101 +@testcase | 103 | 0 | 1 @fif_codegen """ diff --git a/tolk-tester/tests/optimize-if-conditions.tolk b/tolk-tester/tests/optimize-if-conditions.tolk index 47c2321600..ef8c151dd6 100644 --- a/tolk-tester/tests/optimize-if-conditions.tolk +++ b/tolk-tester/tests/optimize-if-conditions.tolk @@ -603,6 +603,25 @@ fun test54AssertNegatedConstFalse(): int { assert(!true, 5); } +// --- 55. Regression: condition folding can revive an op disabled by an earlier DCE pass. --- + +@method_id(155) +fun test55AssertSixFoldedAnds(x: int): int { + try { + assert ( + x > 0 && + x > 1 && + x > 2 && + x > 3 && + x > 4 && + x > 5 + ) throw 813; + return 1; + } catch (err) { + return err; + } +} + fun main(): int { return 0; } @@ -722,6 +741,8 @@ fun main(): int { @testcase | 153 | 0 | 123 @testcase | 153 | 5 | 5 @testcase | 153 | -3 | -3 +@testcase | 155 | 9 | 1 +@testcase | 155 | 4 | 813 @fif_codegen """ @@ -883,12 +904,12 @@ test22TernaryInlEq() PROC:<{ """ test23TernaryNested() PROC:<{ s0 s4 XCHG - IF:<{ + IFJMP:<{ s3 POP s1 s2 XCHG CONDSEL - }>ELSE<{ - 3 BLKDROP + }> + 3 BLKDROP """ @fif_codegen @@ -960,10 +981,10 @@ test31TernaryZeroOnLeft() PROC:<{ @fif_codegen """ test41MatchZero() PROC:<{ - IF:<{ + IFJMP:<{ 20 PUSHINT - }>ELSE<{ - 10 PUSHINT + }> + 10 PUSHINT """ @fif_codegen diff --git a/tolk-tester/tests/overloads-tests.tolk b/tolk-tester/tests/overloads-tests.tolk index 1eb0ad9c00..1374684ecc 100644 --- a/tolk-tester/tests/overloads-tests.tolk +++ b/tolk-tester/tests/overloads-tests.tolk @@ -57,9 +57,9 @@ fun MyPair.mapFn(self): int4 { return 0 } fun test3() { __expect_type(MyPair{}.mapFn(), "int1"); - __expect_type(MyPair{}.mapFn(), "int1"); + __expect_type(MyPair{}.mapFn(), "int2"); __expect_type(MyPair{}.mapFn(), "int2"); - __expect_type(MyPair{}.mapFn(), "int2"); + __expect_type(MyPair{}.mapFn(), "int4"); __expect_type(MyPair{}.mapFn(), "int3"); __expect_type(MyPair{}.mapFn(), "int4"); __expect_type(MyPair{}.mapFn(), "int4"); @@ -96,33 +96,46 @@ fun U?.myBAlias(self): int5 { return 0 } fun test6(b: builder, b1: AliasBuilder1, b2: AliasBuilder2, b1n: AliasBuilder1?, b2n: AliasBuilder2?) { __expect_type(b1.myBAlias(), "int1"); __expect_type(b2.myBAlias(), "int2"); - __expect_type(b1n.myBAlias(), "int3"); + __expect_type(b1n.myBAlias(), "int5"); __expect_type((b2n as builder?).myBAlias(), "int3"); __expect_type((b as builder|slice).myBAlias(), "int4"); __expect_type(0.myBAlias(), "int4"); __expect_type((0 as int|int16).myBAlias(), "int4"); - __expect_type(b2n.myBAlias(), "int3"); + __expect_type(b2n.myBAlias(), "int5"); __expect_type((null as builder|slice?).myBAlias(), "int5"); __expect_type((0 as int?).myBAlias(), "int5"); } type AssetBuilder = builder type BalanceBuilder = builder +type AssetBuilderWrapper = Wrapper +type BalanceBuilderWrapper = Wrapper -fun AssetBuilder.validate(self): int1 { return 0 } -fun BalanceBuilder.validate(self): int2 { return 0 } -fun Wrapper.validate(self): int3 { return 0 } -fun Wrapper.validate(self): int4 { return 0 } +fun builder.validate(self): int { return 0 } +fun AssetBuilder.validate(self): int1 { return 1 } +fun BalanceBuilder.validate(self): int2 { return 2 } +fun AssetBuilderWrapper.validate(self): int3 { return 3 } +fun BalanceBuilderWrapper.validate(self): int4 { return 4 } +@method_id(107) fun test7() { var a: AssetBuilder = beginCell(); var b: BalanceBuilder = beginCell(); __expect_type(a.validate(), "int1"); __expect_type(b.validate(), "int2"); - __expect_type(Wrapper{item:a}.validate(), "int3"); - __expect_type(Wrapper{item:b}.validate(), "int4"); + + var aw: AssetBuilderWrapper = Wrapper{item:a}; + var bw: BalanceBuilderWrapper = Wrapper{item:b}; + __expect_type(aw.validate(), "int3"); + __expect_type(bw.validate(), "int4"); + + return ( + match (a) { AssetBuilder => a.validate() }, // 1 + match (a) { builder => a.validate() }, // 0 + a.validate(), // 1 + ) } fun (slice|TT).supposeMe(self): TT { if (self is slice) { throw 123 } return self } @@ -156,7 +169,7 @@ fun test9( __expect_type(m1.mySize(), "int1"); __expect_type(m2.mySize(), "int2"); __expect_type(m3.mySize(), "int3"); - __expect_type(m4.mySize(), "int3"); + __expect_type(m4.mySize(), "int4"); __expect_type(m5.mySize(), "int4"); __expect_type(m6.mySize(), "int4"); __expect_type(m7.mySize(), "int5"); @@ -273,10 +286,10 @@ fun test14( m10: map, ) { __expect_type(m1.mySize(), "int5"); - __expect_type(m2.mySize(), "int5"); + __expect_type(m2.mySize(), "int4"); __expect_type(m3.mySize(), "int4"); - __expect_type(m4.mySize(), "int4"); - __expect_type(m5.mySize(), "int4"); + __expect_type(m4.mySize(), "int3"); + __expect_type(m5.mySize(), "int3"); __expect_type(m6.mySize(), "int3"); __expect_type(m7.mySize(), "int2"); __expect_type(m8.mySize(), "int2"); @@ -302,15 +315,15 @@ fun test15( arrArrInt8_2: ArrAlias>, arrArrInt8_3: ArrAlias>, ) { - __expect_type(arrInt.mySize(), "int1"); + __expect_type(arrInt.size(), "int"); __expect_type(arrArrInt.mySize(), "int2"); __expect_type(arrArrArrInt.mySize(), "int2"); - __expect_type(tup.mySize(), "int1"); + __expect_type(tup.size(), "int"); __expect_type(aliasInt.mySize(), "int1"); - __expect_type(aliasArrInt.mySize(), "int2"); + __expect_type(aliasArrInt.mySize(), "int1"); __expect_type(arrArrInt8_1.mySize(), "int3"); - __expect_type(arrArrInt8_2.mySize(), "int3"); - __expect_type(arrArrInt8_3.mySize(), "int3"); + __expect_type(arrArrInt8_2.mySize(), "int1"); + __expect_type(arrArrInt8_3.mySize(), "int1"); } type AA = cell @@ -370,7 +383,7 @@ fun test19() { val direct: GenericCellForWhich = createEmptyCell(); val throughAlias: GenericCellForWhichInt = createEmptyCell(); __expect_type(direct.which(), "int1"); - __expect_type(throughAlias.which(), "int1"); + __expect_type(throughAlias.which(), "int3"); } type TaggedCellForPick = cell @@ -415,6 +428,21 @@ fun test22(): int { return order1.tagRes() + order2.tagRes(); } +fun alwaysThrow() { throw 123 } + +fun test23(a: AssetBuilder?, b: BalanceBuilder?) { + var ok1: AssetBuilder = a ?? b ?? alwaysThrow(); + var ok2: BalanceBuilder = a ?? b ?? alwaysThrow(); + __expect_type(((a ?? b ?? ok1 ?? ok2) as AssetBuilder).validate(), "int1"); + __expect_type(((a ?? b ?? alwaysThrow()) as BalanceBuilder).validate(), "int2"); + __expect_type(((a == null ? b! : a) as BalanceBuilder).validate(), "int2"); + __expect_type(((a == null ? b! : a) as builder).validate(), "int"); + __expect_type(a, "AssetBuilder?"); + __expect_type(b!, "BalanceBuilder"); + __expect_type(match(a) { builder => a.validate(), null => alwaysThrow() }, "int"); + __expect_type(a, "builder"); +} + fun main() { // besides these positive tests, there are many negative resulting in "ambiguous call" return 0 @@ -422,6 +450,7 @@ fun main() { /** @testcase | 0 | | 0 +@testcase | 107 | | 1 0 1 @testcase | 113 | | [ 1 2 258 1 2 -777 ] @testcase | 116 | | 111222 @testcase | 118 | | 123 @@ -432,9 +461,15 @@ fun main() { @fif_codegen """ test16() PROC:<{ + PUSHREF + DROP + PUSHREF + DROP 111222 PUSHINT }> test18() PROC:<{ + PUSHREF + DROP 123 PUSHINT }> """ diff --git a/tolk-tester/tests/pack-unpack-1.tolk b/tolk-tester/tests/pack-unpack-1.tolk index 4641e3c0ee..d94afd67a7 100644 --- a/tolk-tester/tests/pack-unpack-1.tolk +++ b/tolk-tester/tests/pack-unpack-1.tolk @@ -78,22 +78,6 @@ fun test6() { return (s.remainingBitsCount(), b.bitsCount(), Point.fromSlice(s.getLastBits(64)), s.remainingBitsCount()); } -// this function is used in serialization codegen, it's not exposed to stdlib; -// it constructs "x{...}" slices for SDBEGINSQ, used for opcode matching; -// here we write some unit tests and fif codegen tests for it -fun slice.tryStripPrefix(mutate self, prefix: int, prefixLen: int): bool - builtin; - -@method_id(107) -fun test7(s: slice) { - if (s.tryStripPrefix(0x10, 32)) { return (1, s.remainingBitsCount()); } - if (s.tryStripPrefix(0x40, 31)) { return (2, s.remainingBitsCount()); } - if (s.tryStripPrefix(0xFF18, 16)) { return (3, s.remainingBitsCount()); } - if (s.tryStripPrefix(0b1001, 8)) { return (4, s.remainingBitsCount()); } - if (s.tryStripPrefix(0b01, 3)) { return (5, s.remainingBitsCount()); } - return (6, s.remainingBitsCount()); -} - @method_id(108) fun test8(): cell | int { try { @@ -140,10 +124,6 @@ fun main(c: cell) { @testcase | 104 | | 1 2 96 @testcase | 105 | | 1 2 32 @testcase | 106 | | 96 96 10 20 96 -@testcase | 107 | x{00000080} | 2 1 -@testcase | 107 | x{09332} | 4 12 -@testcase | 107 | x{2} | 5 1 -@testcase | 107 | x{0234} | 6 16 @testcase | 108 | | 5 1 @testcase | 109 | | 20 10 30 -1 @testcase | 110 | | 18 @@ -158,9 +138,4 @@ fun main(c: cell) { }> """ -@fif_codegen x{00000010} SDBEGINSQ -@fif_codegen b{0000000000000000000000001000000} SDBEGINSQ -@fif_codegen x{ff18} SDBEGINSQ -@fif_codegen x{09} SDBEGINSQ -@fif_codegen b{001} SDBEGINSQ */ diff --git a/tolk-tester/tests/pack-unpack-11.tolk b/tolk-tester/tests/pack-unpack-11.tolk new file mode 100644 index 0000000000..61d1a2f496 --- /dev/null +++ b/tolk-tester/tests/pack-unpack-11.tolk @@ -0,0 +1,20 @@ +struct G { + x: int10 +} + +fun G.packToBuilder(self, mutate b: builder) { + b.storeInt(self.x, 32); +} + +fun G.unpackFromSlice(mutate s: slice): G { + return { x: s.loadInt(32) }; +} + +fun main() { + var s = G { x: 1 }.toCell().beginParse(); + return (s.remainingBitsCount(), s.loadAny>(), s.remainingBitsCount()); +} + +/** +@testcase | 0 | | 32 1 0 +*/ diff --git a/tolk-tester/tests/pack-unpack-2.tolk b/tolk-tester/tests/pack-unpack-2.tolk index 6c59e582ca..7e5104acff 100644 --- a/tolk-tester/tests/pack-unpack-2.tolk +++ b/tolk-tester/tests/pack-unpack-2.tolk @@ -218,7 +218,7 @@ struct IntAndMaybeMaybe8 { */ struct SomeBytesFields { - f1: bytes1; + f1: bits8; f2: bits3; f3: bits20?; f4: bits100 | bits200; @@ -575,8 +575,8 @@ fun test_IntAndEitherMaybe8Or256() { @method_id(213) fun test_SomeBytesFields() { - run({ f1: stringHexToSlice("A4") as bytes1, f2: stringHexToSlice("7_") as bits3, f3: null, f4: stringHexToSlice("BBA87684B3DAA58C0FCC75230C4302C9D156102139D631FF56") as bits200 }, stringHexToSlice("A46DDD43B4259ED52C607E63A9186218164E8AB08109CEB18FFAB4_")); - run({ f1: stringHexToSlice("E6") as bytes1, f2: stringHexToSlice("D_") as bits3, f3: stringHexToSlice("2531C") as bits20, f4: stringHexToSlice("927E88FAB2D327D9468547217") as bits100 }, stringHexToSlice("E6D2531C493F447D596993ECA342A390BC_")); + run({ f1: stringHexToSlice("A4") as bits8, f2: stringHexToSlice("7_") as bits3, f3: null, f4: stringHexToSlice("BBA87684B3DAA58C0FCC75230C4302C9D156102139D631FF56") as bits200 }, stringHexToSlice("A46DDD43B4259ED52C607E63A9186218164E8AB08109CEB18FFAB4_")); + run({ f1: stringHexToSlice("E6") as bits8, f2: stringHexToSlice("D_") as bits3, f3: stringHexToSlice("2531C") as bits20, f4: stringHexToSlice("927E88FAB2D327D9468547217") as bits100 }, stringHexToSlice("E6D2531C493F447D596993ECA342A390BC_")); return SomeBytesFields.fromSlice(stringHexToSlice("E6D2531C493F447D596993ECA342A390BC_")).f4 is bits100; } diff --git a/tolk-tester/tests/pack-unpack-5.tolk b/tolk-tester/tests/pack-unpack-5.tolk index 00a2c19282..25ebfe2842 100644 --- a/tolk-tester/tests/pack-unpack-5.tolk +++ b/tolk-tester/tests/pack-unpack-5.tolk @@ -35,7 +35,7 @@ fun test1() { RemainingBitsAndRefs.estimate(), coins.estimate(), bits6.estimate(), - bytes8.estimate(), + bits64.estimate(), varint32.estimate(), ); } @@ -106,7 +106,7 @@ struct Test5_2 { struct Test5_3 { f1: int8 | Cell; - f2: bytes2 | Cell; + f2: bits16 | Cell; f3: Cell | cell; f4: Cell | coins; } diff --git a/tolk-tester/tests/pack-unpack-6.tolk b/tolk-tester/tests/pack-unpack-6.tolk index 58ca245cad..4b0de058ed 100644 --- a/tolk-tester/tests/pack-unpack-6.tolk +++ b/tolk-tester/tests/pack-unpack-6.tolk @@ -3,7 +3,7 @@ struct (0x03) CounterDecrementBy1 {} type CounterMsg = CounterIncrement | CounterDecrementBy1; struct SomeBytesFields { - f1: bytes1; + f1: bits8; } @method_id(101) @@ -43,18 +43,18 @@ fun test4() { @method_id(105) fun test5() { - return SomeBytesFields { f1: stringHexToSlice("11") as bytes1 }.toCell().hash() & 0xFFFF; + return SomeBytesFields { f1: stringHexToSlice("11") as bits8 }.toCell().hash() & 0xFFFF; } @method_id(106) fun test6() { - return SomeBytesFields { f1: stringHexToSlice("11") as bytes1 }.toCell({skipBitsNValidation: true}).hash() & 0xFFFF; + return SomeBytesFields { f1: stringHexToSlice("11") as bits8 }.toCell({skipBitsNValidation: true}).hash() & 0xFFFF; } @method_id(107) fun test7(believe: bool) { try { - return SomeBytesFields { f1: stringHexToSlice("ffff") as bytes1 }.toCell({skipBitsNValidation: believe}).hash() & 0xFFFF; + return SomeBytesFields { f1: stringHexToSlice("ffff") as bits8 }.toCell({skipBitsNValidation: believe}).hash() & 0xFFFF; } catch (excno) { return excno; } @@ -142,6 +142,7 @@ RETALT @fif_codegen """ IF:<{ + 8 PLDI DROP 129 PUSHINT }>ELSE<{ @@ -179,14 +180,14 @@ IF:<{ @fif_codegen """ test8() PROC:<{ // - x{010f} PUSHSLICE // '0 - 16 PUSHPOW2DEC // s '2=65535 - SWAP // '2=65535 s - x{01} SDBEGINSQ // '2=65535 s '4 - s1 s2 XCHG // s '2=65535 '4 + x{010f} PUSHSLICE // '1 + 16 PUSHPOW2DEC // s '3=65535 + SWAP // '3=65535 s + x{01} SDBEGINSQ // '3=65535 s '5 + s1 s2 XCHG // s '3=65535 '5 THROWANYIFNOT // s - 8 LDI // '8 s - ENDS // '8 + 8 LDI // '9 s + ENDS // '9 }> """ @@ -196,6 +197,7 @@ IF:<{ x{010f} PUSHSLICE x{01} SDBEGINSQ IF:<{ + 8 PLDI DROP 129 PUSHINT }>ELSE<{ diff --git a/tolk-tester/tests/pack-unpack-7.tolk b/tolk-tester/tests/pack-unpack-7.tolk index 466fd41cd7..2bc4e68446 100644 --- a/tolk-tester/tests/pack-unpack-7.tolk +++ b/tolk-tester/tests/pack-unpack-7.tolk @@ -19,6 +19,10 @@ fun Custom8.packToBuilder(self, mutate b: builder) { } fun Custom8.unpackFromSlice(mutate s: slice) { + __expect_inline(true); // even though multiple returns, the flow is trivial + if (s.remainingBitsCount() == 0) { + return 0; + } return s.loadUint(8) } @@ -377,6 +381,153 @@ fun test15(): int { return w.toCell().beginParse().loadUint(16); } +type MyIntNoinline = int + +fun MyIntNoinline.packToBuilder(self, mutate b: builder) { + __expect_inline(false); // too hard to be inlined, that's why called as CALLDICT + var sum_eq_self = 0; + repeat (self) { + if (sum_eq_self >= 128) { + return; + } + sum_eq_self += 1; + } + b.storeUint(self, 8); +} + +fun MyIntNoinline.unpackFromSlice(mutate s: slice): MyIntNoinline { + __expect_inline(false); + var num = s.loadUint(8); + repeat (num) { + if (num >= 128) { + return 128; + } + } + return num; +} + +@method_id(116) +fun test16() { + return (5 as MyIntNoinline).toCell().load(); +} + + +struct Box117 { + value: T +} + +type BoxAlias117 = Box117 + +fun Box117.packToBuilder(self, mutate b: builder) { + b.storeUint(10, 4); + b.storeAny(self.value); +} + +fun Box117.unpackFromSlice(mutate s: slice): Box117 { + assert (s.loadUint(4) == 10) throw 123; + return { value: s.loadAny() }; +} + +fun BoxAlias117.packToBuilder(self, mutate b: builder) { + b.storeUint(11, 4); + b.storeAny(self.value); +} + +fun BoxAlias117.unpackFromSlice(mutate s: slice): BoxAlias117 { + assert (s.loadUint(4) == 11) throw 123; + return { value: s.loadAny() }; +} + +@method_id(117) +fun test117() { + var box: Box117 = { value: 1 }; + var ali: BoxAlias117 = { value: 1 }; + var sBox = box.toCell().beginParse(); + var sAli = ali.toCell().beginParse(); + return ( + sBox.remainingBitsCount(), sBox.preloadUint(4), box = sBox.loadAny(), + sAli.remainingBitsCount(), sAli.preloadUint(4), ali = sAli.loadAny(), + ); +} + +type TryCatchSerialized = int + +fun TryCatchSerialized.packToBuilder(self, mutate b: builder) { + __expect_inline(true); + try { + if (self == 0) { + throw 1; + } + b.storeInt(self, 32); + } catch { + b.storeInt(555, 32); + } +} + +fun TryCatchSerialized.unpackFromSlice(mutate s: slice): TryCatchSerialized { + __expect_inline(true); + var value = -123; // returned if slice is short + try { + value = s.loadInt(32); + } catch {} + return value; +} + +@noinline +fun serializeTryCatch(v: int): cell { + return (v as TryCatchSerialized).toCell(); +} + +@noinline +fun deserializeTryCatch(c: cell): int { + return TryCatchSerialized.fromCell(c); +} + +@method_id(118) +fun test118(v: int) { + var keep1 = v + 1000; + var keep2 = v + 2000; + return (keep1, keep2, deserializeTryCatch(serializeTryCatch(v))); +} + +@noinline +fun setTryCatch(mutate m: map, value: TryCatchSerialized) { + m.set(1, value); +} + +@noinline +fun getTryCatch(m: map): TryCatchSerialized { + return m.mustGet(1); +} + +@method_id(119) +fun test119(value: TryCatchSerialized) { + var keep1 = value + 1000; + var keep2 = value + 2000; + var m: map = []; + setTryCatch(mutate m, value); + return (keep1, keep2, getTryCatch(m)); +} + +type CustomWithRawMap = uint8 + +fun CustomWithRawMap.packToBuilder(self, mutate b: builder) { + // a map is stored as an already serialized dictionary; its value serializer is not called here + var empty: map = []; + b.storeRef(empty.toCell()); // no "recursive call" for CustomWithRawMap + b.storeUint(self, 8); +} + +fun CustomWithRawMap.unpackFromSlice(mutate s: slice): CustomWithRawMap { + s.loadRef(); + return s.loadUint(8) as CustomWithRawMap; +} + +@method_id(120) +fun test120(value: uint8) { + var serialized = (value as CustomWithRawMap).toCell(); + return CustomWithRawMap.fromCell(serialized); +} fun main() { __expect_type(createEmptySlice() as TelegramString, "TelegramString"); @@ -401,12 +552,26 @@ fun main() { @testcase | 113 | | -1 -1 0 100 100 @testcase | 114 | | 8 0 @testcase | 115 | | 258 +@testcase | 116 | | 5 +@testcase | 117 | | 12 10 1 12 11 1 +@testcase | 118 | 123 | 1123 2123 123 +@testcase | 118 | 0 | 1000 2000 555 +@testcase | 119 | 123 | 1123 2123 123 +@testcase | 119 | 0 | 1000 2000 555 +@testcase | 120 | 123 | 123 @fif_codegen """ test3() PROC:<{ x{0102} PUSHSLICE - 8 LDU + DUP + SBITS + IF:<{ + 8 LDU + }>ELSE<{ + 0 PUSHINT + SWAP + }> 8 LDI ENDS }> diff --git a/tolk-tester/tests/pack-unpack-9.tolk b/tolk-tester/tests/pack-unpack-9.tolk index b273e35c41..e10f44242b 100644 --- a/tolk-tester/tests/pack-unpack-9.tolk +++ b/tolk-tester/tests/pack-unpack-9.tolk @@ -394,6 +394,32 @@ fun test18() { return list.toCell().load().calculateSize(); } +type ShiftedUint8 = uint8 + +fun ShiftedUint8.packToBuilder(self, mutate b: builder) { + b.storeUint(self + 1, 8); +} + +fun ShiftedUint8.unpackFromSlice(mutate s: slice): ShiftedUint8 { + return (s.loadUint(8) - 1) as ShiftedUint8; +} + +fun makeCellWithRawSix(): cell { + return beginCell() + .storeRef(beginCell().storeRef(createEmptyCell()).storeUint(6, 8).endCell()) + .endCell(); +} + +@method_id(119) +fun test19() { + val c = makeCellWithRawSix(); + val asShifted = lisp_list.fromCell(c).getHead(); + val asRaw = lisp_list.fromCell(c).getHead(); + val c2 = makeCellWithRawSix(); + val asRawFirst = lisp_list.fromCell(c2).getHead(); + val asShiftedAfter = lisp_list.fromCell(c2).getHead(); + return (asShifted, asRaw, asRawFirst, asShiftedAfter); +} fun main() { return ( @@ -424,4 +450,5 @@ fun main() { @testcase | 116 | | 2 [ 3 (null) ] [ 4 (null) ] @testcase | 117 | | -1 (null) 0 5 18792 @testcase | 118 | | 3 +@testcase | 119 | | 5 6 6 5 */ diff --git a/tolk-tester/tests/preserve-user-calls.tolk b/tolk-tester/tests/preserve-user-calls.tolk new file mode 100644 index 0000000000..749a4db121 --- /dev/null +++ b/tolk-tester/tests/preserve-user-calls.tolk @@ -0,0 +1,429 @@ +struct TwoUint8 { + a: uint8 + b: uint8 +} + +struct OneByte { + x: uint8 +} + +struct (0x01) VariantA { + x: int8 +} + +struct (0x03) VariantB { +} + +type PreserveUnion = VariantA | VariantB + +fun inlineDivResult(x: int): int { + return 1 / x; +} + +fun inlineArgOnly(x: int) { +} + +fun inlineBeginCell(): builder { + return beginCell(); +} + +@noinline +fun noinlineBeginCell(): builder { + return beginCell(); +} + +@method_id(101) +fun notPreservedBeginCellCodegen(): int { + beginCell(); // `NEWC; DROP` is removed by peephole, only `7 PUSHINT` remains + return 7; +} + +@method_id(102) +fun preserveLoadUintOnEmptySlice(): int { + try { + var s = createEmptySlice(); + s.loadUint(256); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(103) +fun preserveCascadeThroughUnusedVars(): int { + try { + var r1 = createEmptySlice(); + var r2 = r1; + r2.loadUint(256); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(104) +fun preserveInlineResult(x: int): int { + try { + inlineDivResult(x); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(105) +fun preserveInlineArgExpression(x: int): int { + try { + inlineArgOnly(1 / x); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(106) +fun preserveSpecialGenFromSlice(): int { + try { + TwoUint8.fromSlice("01".hexToSlice(), { assertEndAfterReading: false }); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(107) +fun preserveArrayGet(): int { + try { + var a = createEmptyTuple() as array; + a.get(0); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(108) +fun preservePrimitiveFromSlice(): int { + try { + int32.fromSlice(createEmptySlice(), { assertEndAfterReading: false }); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(109) +fun preserveBareOperatorStatement(x: int): int { + try { + 1 / x; + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(110) +fun inlineBeforeThrow() { + inlineBeginCell(); + throw 1; +} + +@method_id(111) +fun noinlineBeforeThrow() { + noinlineBeginCell(); + throw 1; +} + +@method_id(112) +fun preserveIndirectCall(): int { + try { + var cb: (int) -> int = fun(x) { return 1 / x }; + cb(0); + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(113) +fun preserveSetAssignExpression(x: int): int { + try { + var y = 1; + y /= x; + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(114) +fun foldIfNeqZero(x: int): int { + if (x != 0) { + return 1; + } + return 0; +} + +@method_id(115) +fun foldIfEqZero(x: int): int { + if (x == 0) { + return 1; + } + return 0; +} + +@method_id(116) +fun foldAssertNeqZero(x: int): int { + try { + assert(x != 0) throw 123; + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(117) +fun foldTernaryNeqZero(x: int): int { + return x != 0 ? 1 : 2; +} + +fun negationShouldRemain(x: int): bool { + return (-x * 0) == 0; +} + +@method_id(118) +fun negationShouldRemainExcno(x: int): int { + try { + return negationShouldRemain(x) ? 0 : -1; + } catch (excno) { + return excno; + } +} + +@method_id(119) +fun preservePureOperators(x: int, y: int): int { + x & y; + x < y; + return 7; +} + +@method_id(120) +fun preserveShiftOverflow(x: int): int { + try { + 1 << x; + return 0; + } catch (excno) { + return excno; + } +} + +@method_id(121) +fun constFoldedOperatorElided(): int { + 1 << 88; + return 7; +} + +@method_id(122) +fun preserveFromSliceAlwaysTrueCompare(): int { + try { + val p = OneByte.fromSlice(createEmptySlice(), { assertEndAfterReading: false }); + if (p.x >= 0) { + return 1; + } + return 2; + } catch (excno) { + return excno; + } +} + +@method_id(123) +fun preserveFromSliceUnusedSecondField(): int { + try { + val p = TwoUint8.fromSlice("01".hexToSlice(), { assertEndAfterReading: false }); + return p.a; + } catch (excno) { + return excno; + } +} + +@method_id(124) +fun preserveFromSliceTimesZero(): int { + try { + int32.fromSlice(createEmptySlice(), { assertEndAfterReading: false }) * 0; + return 123; + } catch (excno) { + return excno; + } +} + +@method_id(125) +fun preserveFromSliceUnionIsVariant(): int { + try { + PreserveUnion.fromSlice("01".hexToSlice(), { assertEndAfterReading: false }) is VariantA; + throw 123; + } catch (excno) { + return excno; + } +} + +@method_id(127) +fun preserveForceLoadLazyObject(): int { + try { + val p = lazy TwoUint8.fromSlice("01".hexToSlice()); + p.forceLoadLazyObject(); + return 0; + } catch (excno) { + return excno; + } +} + +fun main() {} + +/** +@testcase | 101 | | 7 +@testcase | 102 | | 9 +@testcase | 103 | | 9 +@testcase | 104 | 0 | 4 +@testcase | 104 | 2 | 0 +@testcase | 105 | 0 | 4 +@testcase | 105 | 2 | 0 +@testcase | 106 | | 9 +@testcase | 107 | | 5 +@testcase | 108 | | 9 +@testcase | 109 | 0 | 4 +@testcase | 109 | 5 | 0 +@testcase | 112 | | 4 +@testcase | 113 | 0 | 4 +@testcase | 113 | 1 | 0 +@testcase | 114 | 0 | 0 +@testcase | 114 | 9 | 1 +@testcase | 115 | 0 | 1 +@testcase | 115 | 9 | 0 +@testcase | 116 | 0 | 123 +@testcase | 116 | 9 | 0 +@testcase | 117 | 0 | 2 +@testcase | 117 | 9 | 1 +@testcase | 118 | 5 | 0 +@testcase | 118 | -1<<256 | 4 +@testcase | 119 | 5 3 | 7 +@testcase | 120 | 8 | 0 +@testcase | 120 | 300 | 4 +@testcase | 121 | | 7 +@testcase | 122 | | 9 +@testcase | 123 | | 9 +@testcase | 124 | | 9 +@testcase | 125 | | 9 +@testcase | 127 | | 9 + +@fif_codegen +""" +notPreservedBeginCellCodegen() PROC:<{ + 7 PUSHINT +}> +""" + +@fif_codegen +""" +inlineBeforeThrow() PROC:<{ + 1 THROW +}> +""" + +@fif_codegen +""" +noinlineBeforeThrow() PROC:<{ + noinlineBeginCell() CALLDICT + 1 THROW +}> +""" + +@fif_codegen +""" +foldIfNeqZero() PROC:<{ + IFJMP:<{ + 1 PUSHINT + }> + 0 PUSHINT +}> +""" + +@fif_codegen +""" +foldIfEqZero() PROC:<{ + IFNOTJMP:<{ + 1 PUSHINT + }> + 0 PUSHINT +}> +""" + +@fif_codegen +""" +foldAssertNeqZero() PROC:<{ + c2 SAVE + SAMEALTSAVE + CONT:<{ + NIP + RETALT + DROP + }> + 0b10111010 SETCONTMANY + CONT:<{ + 123 THROWIFNOT + 0 PUSHINT + RETALT + }> + c1 PUSH + COMPOSALT + SWAP + TRY +}> +""" + +@fif_codegen +""" +foldTernaryNeqZero() PROC:<{ + 1 PUSHINT + 2 PUSHINT + CONDSEL +}> +""" + +@fif_codegen +""" +preservePureOperators() PROC:<{ + 2DUP + AND + DROP + LESS + DROP + 7 PUSHINT +}> +""" + +@fif_codegen +""" +constFoldedOperatorElided() PROC:<{ + 7 PUSHINT +}> +""" + +@fif_codegen +""" + preserveFromSliceUnionIsVariant() PROC:<{ + c2 SAVE + SAMEALTSAVE + CONT:<{ + NIP + RETALT + DROP + }> + 0b10111010 SETCONTMANY + CONT:<{ + x{01} PUSHSLICE + x{01} SDBEGINSQ + IF:<{ + 8 PLDI + DROP +""" + */ diff --git a/tolk-tester/tests/pure-functions.tolk b/tolk-tester/tests/pure-functions.tolk deleted file mode 100644 index 285320dec3..0000000000 --- a/tolk-tester/tests/pure-functions.tolk +++ /dev/null @@ -1,46 +0,0 @@ - -@pure -fun f_pure1(): int { - return f_pure2(); -} - -@pure -fun f_pure2(): int { - return 2; -} - -@pure -fun get_contract_data(): (int, int) { - var c: cell = contract.getData(); - var cs: slice = c.beginParse(); - cs.loadBits(32); - var value: int = cs.loadUint(16); - return (1, value); -} - -fun save_contract_data(value: int) { - var b: builder = beginCell().storeInt(1, 32).storeUint(value, 16); - contract.setData(b.endCell()); -} - -@pure -@method_id(101) -fun test1(): int { - return f_pure1(); -} - -@method_id(102) -fun test2(value: int): int { - save_contract_data(value); - var (_, restored) = get_contract_data(); - return restored; -} - -fun main() { return; } - -/** - -@testcase | 101 | | 2 -@testcase | 102 | 44 | 44 - -*/ diff --git a/tolk-tester/tests/reflect-tests.tolk b/tolk-tester/tests/reflect-tests.tolk index e6f5768fcc..b6065825e8 100644 --- a/tolk-tester/tests/reflect-tests.tolk +++ b/tolk-tester/tests/reflect-tests.tolk @@ -146,6 +146,27 @@ fun optionalVoidArgs() { ) } +fun failNever(): never { + throw 123; +} + +fun assignTypeNameOfNever(mutate name: string) { + name = reflect.typeNameOfObject(failNever()); +} + +@method_id(113) +fun typeNameOfNeverArg() { + var name: string = ""; + var excNo = 0; + try { + assignTypeNameOfNever(mutate name); + } catch (e) { + excNo = e; + } + // failNever() must throw before the assignment; name stays empty + return (excNo, name.calculateLength()); +} + fun main() { __expect_type(reflect.sourceLocation(), "SourceLocation"); } @@ -161,4 +182,5 @@ fun main() { @testcase | 110 | | 1 9999 0 4 101 9999 0 4 @testcase | 111 | | 0 1 4 12 12 13 @testcase | 112 | | -1 -1 -1 -1 -1 -1 +@testcase | 113 | | 123 0 */ diff --git a/tolk-tester/tests/remove-unused-functions.tolk b/tolk-tester/tests/remove-unused-functions.tolk index 71f71d7622..85010aff21 100644 --- a/tolk-tester/tests/remove-unused-functions.tolk +++ b/tolk-tester/tests/remove-unused-functions.tolk @@ -14,16 +14,15 @@ global used_gv: int; fun receiveGetter(): () -> int { return used_as_noncall2; } -@pure @noinline -fun usedButOptimizedOut(x: int): int { return x + 2; } +fun fnUnusedResult(x: int): int { return x + 2; } fun main(): (int, int, int) { used_gv = 1; used_gv = used_gv + 2; var getter1 = used_as_noncall1; var getter2 = receiveGetter(); - usedButOptimizedOut(used_gv); + fnUnusedResult(used_gv); return (used_gv, getter1(), getter2()); } @@ -38,10 +37,9 @@ fun main(): (int, int, int) { @fif_codegen_avoid DECLPROC unused3() @fif_codegen_avoid DECLGLOBVAR $unused_gv -Note, that `usedButOptimizedOut()` (a pure function which result is unused) -is currently codegenerated, since it's formally reachable. -This is because optimizing code is a moment of codegen for now (later than marking unused symbols). +Note, that `fnUnusedResult()` is codegenerated and called even though its result is unused, +because user-written calls must be preserved. -@fif_codegen DECLPROC usedButOptimizedOut() -@fif_codegen_avoid usedButOptimizedOut() CALLDICT +@fif_codegen DECLPROC fnUnusedResult() +@fif_codegen fnUnusedResult() CALLDICT */ diff --git a/tolk-tester/tests/self-keyword.tolk b/tolk-tester/tests/self-keyword.tolk index e8bf3f81b7..3cbee77323 100644 --- a/tolk-tester/tests/self-keyword.tolk +++ b/tolk-tester/tests/self-keyword.tolk @@ -199,7 +199,7 @@ global tup111: tuple; @method_id(111) fun testForallFunctionsWithSelf(): (int, int, tuple) { - var t = createEmptyTuple(); + var t: tuple = createEmptyTuple(); tup111 = createEmptyTuple(); t.myTuplePush(10); tup111.myTuplePush(1).myTuplePush(2).myTuplePush(3); @@ -216,9 +216,18 @@ fun T.id(self): self { fun int.asmSelfId(self): self asm "NOP" +fun int.asmKeepSelfAndBump(self, mutate n: int): self + asm "INC" "SWAP" + @method_id(112) -fun test112() { - return (1.asmSelfId(), 2.asmSelfId().asmSelfId()); +fun test112(x: int) { + var n = 0; + return ( + 1.asmSelfId(), + x.asmSelfId().asmSelfId().asmSelfId(), + x.asmKeepSelfAndBump(mutate n).asmKeepSelfAndBump(mutate n).asmKeepSelfAndBump(mutate n), + n + ); } type PairShape = [int, int]; @@ -264,7 +273,7 @@ fun main() { } @testcase | 109 | 102 | 102 2 1 2 @testcase | 110 | 0 | 24 [ 2 13 ] @testcase | 111 | | 10 3 [ 1 2 3 ] -@testcase | 112 | | 1 2 +@testcase | 112 | 7 | 1 7 7 3 @testcase | 113 | | 3 3 3 6 @fif_codegen @@ -295,9 +304,13 @@ fun main() { } @fif_codegen """ - test112() PROC:<{ // - 1 PUSHINT // '1 - 2 PUSHINT // '1 '4 + test112() PROC:<{ // x + 0 PUSHINT // x n=0 + 1 PUSHINT // x n=0 '4 + s2 s1 s2 XCPUXC // '4 '7 x n=0 + INC // '4 '7 '9 n + INC // '4 '7 '11 n + INC // '4 '7 '13 n }> """ diff --git a/tolk-tester/tests/smart-cast-tests.tolk b/tolk-tester/tests/smart-cast-tests.tolk index abf766c657..b6f2c8dc73 100644 --- a/tolk-tester/tests/smart-cast-tests.tolk +++ b/tolk-tester/tests/smart-cast-tests.tolk @@ -101,8 +101,8 @@ fun test5() { fun test6() { var a: int? = 5; __expect_type(a, "int"); - __expect_type(a != null ? a : null, "int"); - __expect_type(a == null ? "" : a, "int"); + __expect_type(a != null ? a : null, "int?"); + __expect_type((a == null ? "" : a) as string | int, "string | int"); takeNullableInt(a); __expect_type(a, "int"); if (rand() != 0) { @@ -113,7 +113,7 @@ fun test6() { } __expect_type(a, "null"); takeNullableSlice(a); // ok, `slice?` is `slice | null`, here a definitely null - var b: int? = true ? null : "sl"; + var b: int? = null; __expect_type(b, "null"); takeNullableInt(b); takeNullableSlice(b); // same reason @@ -702,6 +702,7 @@ fun test63() { type BalanceDict = dict fun cell.myDepth(self): int1 { return 0 } +fun dict.myDepth(self): int2 { return 0 } fun BalanceDict.myDepth(self): int3 { return 0 } fun null.myDepth(self): int4 { return 0 } @@ -711,7 +712,7 @@ fun test64(d: dict, b: BalanceDict) { cell => { __expect_type(d.myDepth(), "int1") } } __expect_type(d, "dict"); - __expect_type(d.myDepth(), "int3"); + __expect_type(d.myDepth(), "int2"); match (b) { null => { __expect_type(b.myDepth(), "int4") } @@ -917,6 +918,266 @@ fun test83(case: int) { }; } +struct ReanchorMsg1 { + value: int +} + +struct ReanchorMsg2 { + value: int +} + +type OneMessage = ReanchorMsg1 +type AllowedMessages = ReanchorMsg1 | ReanchorMsg2 +type MaybeCell = cell? + +struct ReanchorHolder { + maybeCell: MaybeCell + oneMessage: OneMessage +} + +struct OtherHolder { + other: int +} + +struct DeepLeaf { + before1: int + before2: int + before3: int + sub: MaybeCell +} + +struct DeepOtherLeaf { + other: int +} + +type DeepNode = DeepLeaf | DeepOtherLeaf +type DeepField = (DeepNode, int)? + +struct DeepHolder { + before1: int + before2: int + field: DeepField + after: int +} + +type AnyDeepHolder = DeepHolder | OtherHolder + +struct DictTensorHolder { + before: int + pair: (dict, BalanceDict) +} + +fun ReanchorMsg1.kind(self): int1 { return 1 } +fun OneMessage.kind(self): int2 { return 2 } +fun cell.kind(self): int3 { return 3 } + +fun testIfReanchor(d: MaybeCell, msg: OneMessage) { + // d recovers to an alias + if (d == null) { + __expect_type(d, "null"); + } else { + __expect_type(d, "cell"); + } + __expect_type(d, "MaybeCell"); + + // msg recovers to an alias + if (msg is ReanchorMsg1) { + __expect_type(msg, "ReanchorMsg1"); + } else { + __expect_type(msg, "never"); + } + __expect_type(msg, "OneMessage"); + + match (msg) { + ReanchorMsg1 => { + __expect_type(msg, "ReanchorMsg1"); + __expect_type(msg.kind(), "int1"); + } + } + __expect_type(msg, "OneMessage"); + __expect_type(msg.kind(), "int2"); +} + +fun testUnionMatchReanchor(msg: AllowedMessages) { + match (msg) { + ReanchorMsg1 => { __expect_type(msg, "ReanchorMsg1"); } + ReanchorMsg2 => { __expect_type(msg, "ReanchorMsg2"); } + } + __expect_type(msg, "AllowedMessages"); + + match (msg) { + ReanchorMsg1 => { return } + ReanchorMsg2 => { __expect_type(msg, "ReanchorMsg2"); } + } + __expect_type(msg, "ReanchorMsg2"); +} + +fun testLogicalReanchor(d: MaybeCell, flag: bool) { + d == null && false; + __expect_type(d, "MaybeCell"); + + d != null || true; + __expect_type(d, "MaybeCell"); + + if (d != null && flag) { + __expect_type(d, "cell"); + } else { + __expect_type(d, "MaybeCell"); + } + __expect_type(d, "MaybeCell"); + + if (d == null || flag) { + __expect_type(d, "MaybeCell"); + } else { + __expect_type(d, "cell"); + } + __expect_type(d, "MaybeCell"); +} + +fun testTernaryReanchor(d: MaybeCell, msg: OneMessage) { + d == null ? 1 : 2; + __expect_type(d, "MaybeCell"); + + (msg is ReanchorMsg1) ? 1 : 2; + __expect_type(msg, "OneMessage"); +} + +fun testNullCoalesceReanchor(d: MaybeCell) { + d ?? beginCell().endCell(); + __expect_type(d, "MaybeCell"); + __expect_type(d ?? createEmptyCell(), "cell"); +} + +fun testFieldReanchor(holder: ReanchorHolder) { + if (holder.maybeCell == null) { + __expect_type(holder.maybeCell, "null"); + } else { + __expect_type(holder.maybeCell, "cell"); + } + __expect_type(holder.maybeCell, "MaybeCell"); + + match (holder.oneMessage) { + ReanchorMsg1 => { __expect_type(holder.oneMessage, "ReanchorMsg1"); } + } + __expect_type(holder.oneMessage, "OneMessage"); +} + +fun testFieldOfSmartCastedParent(holder: ReanchorHolder | OtherHolder) { + if (holder is ReanchorHolder) { + if (holder.maybeCell == null) { + __expect_type(holder.maybeCell, "null"); + } else { + __expect_type(holder.maybeCell, "cell"); + } + __expect_type(holder.maybeCell, "MaybeCell"); + } +} + +fun testFieldNarrowingSurvivesWithParent(holder: ReanchorHolder | OtherHolder) { + if (holder is ReanchorHolder) { + if (holder.maybeCell == null) { + return; + } + } else { + return; + } + __expect_type(holder, "ReanchorHolder"); + __expect_type(holder.maybeCell, "cell"); +} + +fun testDeepIndexPath(obj: AnyDeepHolder) { + if (obj is DeepHolder) { + __expect_type(obj, "DeepHolder"); + if (obj.field != null) { + __expect_type(obj.field, "(DeepNode, int)"); + if (obj.field.0 is DeepLeaf) { + __expect_type(obj.field.0, "DeepLeaf"); + if (obj.field.0.sub == null) { + __expect_type(obj.field.0.sub, "null"); + } else { + __expect_type(obj.field.0.sub, "cell"); + } + __expect_type(obj.field.0.sub, "MaybeCell"); + __expect_type(obj.field.0, "DeepLeaf"); + __expect_type(obj.field, "(DeepNode, int)"); + __expect_type(obj, "DeepHolder"); + } else { + __expect_type(obj.field.0, "DeepOtherLeaf"); + } + __expect_type(obj.field.0, "DeepNode"); + __expect_type(obj.field, "(DeepNode, int)"); + __expect_type(obj, "DeepHolder"); + } else { + __expect_type(obj.field, "null"); + } + __expect_type(obj.field, "DeepField"); + __expect_type(obj, "DeepHolder"); + } else { + __expect_type(obj, "OtherHolder"); + } + __expect_type(obj, "AnyDeepHolder"); +} + +fun testNestedReanchor(msg: OneMessage, flag: bool) { + if (msg is ReanchorMsg1) { + __expect_type(msg, "ReanchorMsg1"); + if (flag) { + } else { + } + __expect_type(msg, "ReanchorMsg1"); + } + __expect_type(msg, "OneMessage"); +} + +fun testTryCatchReanchor(d: MaybeCell) { + try { + d = null; + } catch (_) { + d = beginCell().endCell(); + } + __expect_type(d, "MaybeCell"); +} + +fun testTensorAssignmentAfterEarlyReturns(d: dict, b: BalanceDict) { + if (d == null) { return; } + __expect_type(d, "cell"); + __expect_type(b, "BalanceDict"); + if (b != null) { return; } + __expect_type(d, "cell"); + __expect_type(b, "null"); + + if (10 > 3) { + (b, d) = (beginCell().endCell(), null); + } + __expect_type(b, "BalanceDict"); + __expect_type(d, "dict"); +} + +fun testNestedAssignmentAfterEarlyReturns(holder: DictTensorHolder) { + if (holder.pair.0 == null) { return; } + if (holder.pair.1 != null) { return; } + + if (10 > 3) { + (holder.pair.1, holder.pair.0) = (beginCell().endCell(), null); + } + __expect_type(holder.pair.1, "BalanceDict"); + __expect_type(holder.pair.0, "dict"); +} + +@method_id(184) +fun test84(x: int?, stopAt: int): int { + while (x != null) { + __expect_type(x, "int"); + if (x == stopAt) { + break; + } + x = x > 1 ? x - 1 : null; + } + // without `break`, x would be `null` here; the break path keeps `int` + __expect_type(x, "int?"); + return x == null ? -1 : x; +} + fun main(x: int?): int { // mark used to codegen them test55; @@ -961,8 +1222,10 @@ fun main(x: int?): int { @testcase | 182 | 0 | 8 @testcase | 183 | 0 | 11 @testcase | 183 | 1 | 34 - -@stderr warning: expression of type `int` can never be `null`, this condition is always true +@testcase | 184 | 5 3 | 3 +@testcase | 184 | 5 0 | -1 +@testcase | 184 | null 1 | -1 +@testcase | 184 | 2 2 | 2 @fif_codegen eq55() PROC:<{ @fif_codegen_avoid eq55() PROC:<{ diff --git a/tolk-tester/tests/some-tests-2.tolk b/tolk-tester/tests/some-tests-2.tolk index a20269b0ba..913ebeeb75 100644 --- a/tolk-tester/tests/some-tests-2.tolk +++ b/tolk-tester/tests/some-tests-2.tolk @@ -44,7 +44,7 @@ fun test89(last: int): (unknown, unknown, unknown, unknown) { return (t.get(0), t.get(t.size() - 1), t.first(), t.last()); } -@pure @noinline fun get10() { return 10; } +@noinline fun get10() { return 10; } @method_id(91) fun touchCodegen2() { @@ -125,7 +125,8 @@ fun test96(p: Point) { ... DUMPSTK x{6d79} PUSHSLICE - STRDUMP DROP // f s + STRDUMP + DROP // f s CTOS // f '6 SBITS // f '7 }> diff --git a/tolk-tester/tests/struct-tests.tolk b/tolk-tester/tests/struct-tests.tolk index 536dd42d83..abd79b0f86 100644 --- a/tolk-tester/tests/struct-tests.tolk +++ b/tolk-tester/tests/struct-tests.tolk @@ -97,7 +97,7 @@ fun test2() { __expect_type(i1.value, "int"); __expect_type(i1.value + i2.value + i3.value, "int"); __expect_type(i3, "JustIntAlias2"); - __expect_type([i2, i3], "array"); + __expect_type(array [i2, i3], "array"); __expect_type([i2] as [JustIntAlias], "[JustIntAlias]"); return (i1, i2, i3, [i1, JustInt{value:5}]); } @@ -347,10 +347,10 @@ fun test22(x: JustInt) { @method_id(123) fun test23(p: Point) { - var complex = p as Point | (JustInt | Storage); + var complex = p as PointAlias | (JustInt | Storage); return match (complex) { JustInt => 10 * complex.value, - PointAlias => 20, + Point => 20, Storage => 30 + complex.lastPoint.x, }; } @@ -508,8 +508,8 @@ fun test34() { return o1; } -@pure @noinline fun getXPure() { return 1; } -@pure @noinline fun getYPure() { return 2; } +@noinline fun getX1() { return 1; } +@noinline fun getY2() { return 2; } global t_impure: tuple; @noinline fun getXImpure() { t_impure.push(1); return 1; } @noinline fun getYImpure() { t_impure.push(2); return 2; } @@ -517,8 +517,8 @@ global t_impure: tuple; @method_id(135) fun test35() { var p: Point = { - y: getYPure(), - x: getXPure(), + y: getY2(), + x: getX1(), }; return p; } @@ -789,9 +789,10 @@ type PointAlias = Point; @fif_codegen """ - test35() PROC:<{ // - getXPure() CALLDICT // '0 - getYPure() CALLDICT // p.x p.y + test35() PROC:<{ + getY2() CALLDICT + getX1() CALLDICT + SWAP }> """ diff --git a/tolk-tester/tests/ternary-tests.tolk b/tolk-tester/tests/ternary-tests.tolk index 042173aadc..eef17114c7 100644 --- a/tolk-tester/tests/ternary-tests.tolk +++ b/tolk-tester/tests/ternary-tests.tolk @@ -299,10 +299,9 @@ fun main() {} """ helper106() PROC:<{ SWAP - IF:<{ + IFJMP:<{ DROP $gTup GETGLOB - }>ELSE<{ }> }> """ @@ -361,6 +360,12 @@ fun main() {} @fif_codegen """ test202() PROC:<{ // x + DUP + 10 GTINT + DROP + DUP + 10 LESSINT + DROP }> """ @@ -375,29 +380,28 @@ fun main() {} """ test220() PROC:<{ s3 POP - IFNOTJMP:<{ - 20 PUSHINT - CONDSEL + IFJMP:<{ + NIP }> - NIP + 20 PUSHINT + CONDSEL }> """ @fif_codegen """ test304() PROC:<{ - IF:<{ + IFJMP:<{ Point.create0NoInline() CALLDICT """ @fif_codegen """ test305() PROC:<{ - IF:<{ + IFJMP:<{ 0 PUSHINT - }>ELSE<{ - 5 PUSHINT }> + 5 PUSHINT }> """ diff --git a/tolk-tester/tests/test-math.tolk b/tolk-tester/tests/test-math.tolk index 713bffc28d..627395bc04 100644 --- a/tolk-tester/tests/test-math.tolk +++ b/tolk-tester/tests/test-math.tolk @@ -119,7 +119,6 @@ fun is_nan(x: int): int /// computes sqrt(a*b) exactly rounded to the nearest integer /// for all 0 <= a, b <= 2^256-1 /// may be used with b=1 or b=scale of fixed-point numbers -@pure @inline_ref fun geom_mean(a: int, b: int): int { if (min(a, b) == 0) { @@ -141,7 +140,6 @@ fun geom_mean(a: int, b: int): int { /// integer square root, computes round(sqrt(a)) for all a>=0. /// note: `inline` is better than `inline_ref` for such simple functions -@pure @inline fun sqrt(a: int): int { return geom_mean(a, 1); @@ -149,28 +147,24 @@ fun sqrt(a: int): int { /// version for fixed248 = fixed-point numbers with scale 2^248 /// fixed248 sqrt(fixed248 x) -@pure @inline fun fixed248_sqrt(x: int): int { return geom_mean(x, 1 << 248); } /// fixed255 sqrt(fixed255 x) -@pure @inline fun fixed255_sqrt(x: int): int { return geom_mean(x, 1 << 255); } /// fixed248 sqr(fixed248 x); -@pure @inline fun fixed248_sqr(x: int): int { return mulDivRound(x, x, 1 << 248); } /// fixed255 sqr(fixed255 x); -@pure @inline fun fixed255_sqr(x: int): int { return mulDivRound(x, x, 1 << 255); @@ -184,62 +178,53 @@ const fixed255_One: int = (1 << 255); /// store huge constants in inline_ref functions for reuse /// (y,z) where y=round(log(2)*2^256), z=round((log(2)*2^256-y)*2^128) /// then log(2) = y/2^256 + z/2^384 -@pure @inline_ref fun log2_xconst_f256(): (int, int) { return (80260960185991308862233904206310070533990667611589946606122867505419956976172, -32272921378999278490133606779486332143); } /// (y,z) where Pi = y/2^254 + z/2^382 -@pure @inline_ref fun Pi_xconst_f254(): (int, int) { return (90942894222941581070058735694432465663348344332098107489693037779484723616546, 108051869516004014909778934258921521947); } /// atan(1/16) as fixed260 -@pure @inline_ref fun Atan1_16_f260(): int { return 115641670674223639132965820642403718536242645001775371762318060545014644837101; // true value is ...101.0089... } /// atan(1/8) as fixed259 -@pure @inline_ref fun Atan1_8_f259(): int { return 115194597005316551477397594802136977648153890007566736408151129975021336532841; // correction -0.1687... } /// atan(1/32) as fixed261 -@pure @inline_ref fun Atan1_32_f261(): int { return 115754418570128574501879331591757054405465733718902755858991306434399246026247; // correction 0.395... } /// inline is better than inline_ref for such very small functions -@pure @inline fun log2_const_f256(): int { var (c: int, _) = log2_xconst_f256(); return c; } -@pure @inline fun fixed248_log2_const(): int { return log2_const_f256() ~>> 8; } -@pure @inline fun Pi_const_f254(): int { var (c, _) = Pi_xconst_f254(); return c; } -@pure @inline fun fixed248_Pi_const(): int { return Pi_const_f254() ~>> 6; @@ -250,7 +235,6 @@ fun fixed248_Pi_const(): int { /// hyperbolic tangent of small x via n+2 terms of Lambert's continued fraction /// n=17: good for |x| < log(2)/4 = 0.173 /// fixed258 tanh_f258(fixed258 x, int n) -@pure @inline_ref fun tanh_f258(x: int, n: int): int { var x2: int = mulDivRound(x, x, 1 << 255); // x^2 as fixed261 @@ -268,7 +252,6 @@ fun tanh_f258(x: int, n: int): int { /// fixed257 expm1_f257(fixed257 x) /// computes exp(x)-1 for small x via 19 terms of Lambert's continued fraction for tanh(x/2) /// good for |x| < log(2)/2 = 0.347 (n=17); consumes ~3500 gas -@pure @inline_ref fun expm1_f257(x: int): int { // (almost) compute tanh(x/2) first; x/2 as fixed258 = x as fixed257 @@ -288,7 +271,6 @@ fun expm1_f257(x: int): int { /// expm1_f257() may be used to implement specific fixed-point exponentials /// example: /// fixed248 exp(fixed248 x) -@pure @inline_ref fun fixed248_exp(x_: int): int { var (l2c, l2d) = log2_xconst_f256(); @@ -304,7 +286,6 @@ fun fixed248_exp(x_: int): int { /// compute 2^x in fixed248 /// fixed248 exp2(fixed248 x) -@pure @inline_ref fun fixed248_exp2(x_: int): int { // (int q, x) = divmodr(x, 1 << 248); // no such built-in @@ -318,7 +299,6 @@ fun fixed248_exp2(x_: int): int { /// fixed260 tan(fixed260 x); /// computes tan(x) for small |x|= 0 /// fixed248 pow(fixed248 x, fixed248 y); -@pure @inline_ref fun fixed248_pow(x: int, y: int): int { if (y == 0) { @@ -704,7 +659,6 @@ fun fixed248_pow(x: int, y: int): int { /// number of terms n should be chosen as for atanh_f258() /// fixed259 atan(fixed259 x); -@pure @inline_ref fun atan_f259(x: int, n: int): int { var x2: int = mulrshiftr256(x, x); // x^2 as fixed262 @@ -723,7 +677,6 @@ fun atan_f259(x: int, n: int): int { /// number of terms n should be chosen as for atanh_f261() /// fixed261 atan(fixed261 x); -@pure @inline fun atan_f261_inlined(x: int, n: int): int { var x2: int = mulrshiftr256(x, x); // x^2 as fixed266 @@ -741,7 +694,6 @@ fun atan_f261_inlined(x: int, n: int): int { } /// fixed261 atan(fixed261 x); -@pure @inline_ref fun atan_f261(x: int, n: int): int { return atan_f261_inlined(x, n); @@ -751,7 +703,6 @@ fun atan_f261(x: int, n: int): int { /// then b/a=atan(q*atan(1/32)) exactly, and (a,b) is almost a unit vector pointing in the direction of (1,x) /// must have |x|<1.1, x is fixed24 /// (int, fixed255, fixed255) atan_aux_prereduce(fixed24 x); -@pure @inline_ref fun atan_aux_prereduce(x: int): (int, int, int) { var xu: int = abs(x); @@ -779,7 +730,6 @@ fun atan_aux_prereduce(x: int): (int, int, int) { /// this function is reasonably accurate (error < 7 ulp with ulp = 2^-261), but it consumes >7k gas /// this is sufficient for most purposes /// (int, fixed261) atan_aux(fixed256 x) -@pure @inline_ref fun atan_aux_f256(x: int): (int, int) { var (q, a, b) = atan_aux_prereduce(x ~>> 232); // convert x to fixed24 @@ -797,7 +747,6 @@ fun atan_aux_f256(x: int): (int, int) { /// this function is very accurate (error < 2 ulp), but it consumes >7k gas /// in most cases, faster function atan_aux_f256() should be used /// (int, fixed261) atan_auxx(fixed256 x) -@pure @inline_ref fun atan_auxx_f256(x: int): (int, int) { var (q, a, b) = atan_aux_prereduce(x ~>> 232); // convert x to fixed24 @@ -820,7 +769,6 @@ fun atan_auxx_f256(x: int): (int, int) { /// consumes ~ 8k gas /// fixed255 atan(fixed255 x); -@pure @inline_ref fun atan_f255(x: int): int { var s: int = (x ~>> 256); @@ -839,7 +787,6 @@ fun atan_f255(x: int): int { /// computes atan(x) for -1 <= x < 1 only /// fixed256 atan_small(fixed256 x); -@pure @inline_ref fun atan_f256_small(x: int): int { var (q, z) = atan_aux_f256(x); @@ -849,7 +796,6 @@ fun atan_f256_small(x: int): int { } /// fixed255 asin(fixed255 x); -@pure @inline_ref fun asin_f255(x: int): int { var a: int = fixed255_One - fixed255_sqr(x); // a:=1-x^2 @@ -862,7 +808,6 @@ fun asin_f255(x: int): int { } /// fixed254 acos(fixed255 x); -@pure @inline_ref fun acos_f255(x: int): int { var Pi: int = Pi_const_f254(); @@ -877,7 +822,6 @@ fun acos_f255(x: int): int { /// consumes ~ 10k gas /// fixed248 asin(fixed248 x) -@pure @inline fun fixed248_asin(x: int): int { return asin_f255(x << 7) ~>> 7; @@ -885,7 +829,6 @@ fun fixed248_asin(x: int): int { /// consumes ~ 10k gas /// fixed248 acos(fixed248 x) -@pure @inline fun fixed248_acos(x: int): int { return acos_f255(x << 7) ~>> 6; @@ -893,7 +836,6 @@ fun fixed248_acos(x: int): int { /// consumes ~ 7500 gas /// fixed248 atan(fixed248 x); -@pure @inline_ref fun fixed248_atan(x: int): int { var s: int = (x ~>> 249); @@ -910,7 +852,6 @@ fun fixed248_atan(x: int): int { } /// fixed248 acot(fixed248 x); -@pure @inline_ref fun fixed248_acot(x: int): int { var s: int = (x ~>> 249); @@ -1171,6 +1112,20 @@ fun quiet_shrmod_nan_shift_test(): (int, int, int, int, int, int) { return (is_nan(rshifted), is_nan(modded), is_nan(q1), is_nan(r1), is_nan(q2), is_nan(r2)); } +@method_id(10030) +fun muldiv_zero_num_unknown_div(z: int): int { + try { + return mulDivFloor(0, 5, z); + } catch (excNo) { + return -excNo; + } +} + +@method_id(10031) +fun muldiv_zero_num_known_div(y: int): int { + return mulDivFloor(0, y, 5); +} + fun main() { var One: int = 1; // repeat(76 / 4) { One *= 10000; } @@ -1313,4 +1268,15 @@ fun main() { @testcase | 10027 | (1<<248)//5 | 89284547973388213553327350968415123522888028497458323165947767504203347189 @testcase | 10028 | (1<<248)//239 | 708598849781543798951441405045469962900811296151941404481049216461523216127 @testcase | 10029 | | -1 -1 -1 -1 -1 -1 +@testcase | 10030 | 0 | -4 +@testcase | 10030 | 5 | 0 +@testcase | 10031 | 99 | 0 + +@fif_codegen +""" + muldiv_zero_num_known_div() PROC:<{ + DROP + 0 PUSHINT + }> +""" */ diff --git a/tolk-tester/tests/try-catch-tests.tolk b/tolk-tester/tests/try-catch-tests.tolk index e65ad763db..429e8d20b7 100644 --- a/tolk-tester/tests/try-catch-tests.tolk +++ b/tolk-tester/tests/try-catch-tests.tolk @@ -9,7 +9,7 @@ fun foo(x: int): int { } } -@inline +// it was @inline earlier (when inlining was deferred to Fift), but now it's not inlined due to try/catch fun foo_inline(x: int): int { try { assert(!(x == 7)) throw 44; @@ -47,7 +47,6 @@ fun test_inlineref(x: int, y: int, z: int): int { return x * 100 + y * 10 + z; } -@inline 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 @@ -192,6 +191,7 @@ fun testCodegen1(x: int) { return 0; } +@noinline fun testCodegen2(x: int) { if (x > 10) { alwaysThrow123(); @@ -356,9 +356,8 @@ fun testAssertWithTooBigThrow(x: int) { return true; } -@inline +// it was @inline earlier (when inlining was deferred to Fift), but now it's not inlined due to try/catch fun chooseInlineReturnInsideTry(flag: int): int { - __expect_inline(false); // inlining deferred to Fift due to returns in the middle try { if (flag == 1) { return 10; @@ -377,6 +376,76 @@ fun testInlineReturnInsideTry(flag: int): int { return chooseInlineReturnInsideTry(flag); } +@inline +fun returnThenThrowingTryCatch(x: int): int { + if (x == 0) { + return 10; + } + try { + throw 5; + } catch { + throw 20; + } +} + +@inline +fun returnOrThrowingTryCatch(b: bool): int { + if (b) { + return 1; + } else { + try { + throw 2; + } catch (e) { + throw e; + } + } + return 3; +} + +@inline +fun returnThenThrowingTryCatchWithDeadTail(cond: bool): int { + if (cond) { + return 1; + } + try { + throw 42; + } catch { + throw 2; + } + return 3; +} + +@method_id(122) +fun testReturnPath(): int { + return returnThenThrowingTryCatch(0); +} + +@method_id(123) +fun testThrowingTail(): int { + try { + return returnThenThrowingTryCatch(1); + } catch (e) { + return e; + } +} + +@method_id(124) +fun testThrowingElse(b: bool): int { + try { + return returnOrThrowingTryCatch(b); + } catch (e) { + return e; + } +} + +@method_id(125) +fun testThrowingTailWithDeadReturn(cond: bool): int { + try { + return returnThenThrowingTryCatchWithDeadTail(cond); + } catch (e) { + return e; + } +} fun main() { @@ -425,8 +494,14 @@ fun main() { @testcase | 121 | 1 | 10 @testcase | 121 | 2 | 20 @testcase | 121 | 3 | 30 +@testcase | 122 | | 10 +@testcase | 123 | | 20 +@testcase | 124 | 1 | 1 +@testcase | 124 | 0 | 2 +@testcase | 125 | 1 | 1 +@testcase | 125 | 0 | 2 -@code_hash 17473387520586690375058520669959417766875699647034456359443028989383748791753 +@code_hash 102476915625155372615694187729507210848966808643848453114230057254733224071873 @fif_codegen """ @@ -475,6 +550,7 @@ fun main() { SETCODE 0 THROW }> // paramVal + CTOS // '7 DROP // }> """ @@ -489,7 +565,7 @@ CONT:<{ @fif_codegen """ - chooseInlineReturnInsideTry() PROCINLINE:<{ + chooseInlineReturnInsideTry() PROC:<{ ... 10 PUSHINT RETALT @@ -501,8 +577,7 @@ CONT:<{ @fif_codegen """ -foo_inline() PROCINLINE:<{ - CONT:<{ + foo_inline() PROC:<{ c2 SAVE SAMEALTSAVE """ diff --git a/tolk-tester/tests/type-aliases-tests.tolk b/tolk-tester/tests/type-aliases-tests.tolk index eeb3d729ea..0a10125561 100644 --- a/tolk-tester/tests/type-aliases-tests.tolk +++ b/tolk-tester/tests/type-aliases-tests.tolk @@ -36,10 +36,9 @@ fun test1(x: MInt): MVoid { __expect_type(rand() != 0 ? x : y, "MInt"); __expect_type((x, 1, y), "(MInt, int, MInt)"); - __expect_type(rand() != 0 ? (1, 2) : (1, 2) as Pair2_v1, "(int, int)"); - __expect_type(rand() != 0 ? (1, 2) : (1, 2) as Pair2_v2, "(int, int)"); - __expect_type(rand() != 0 ? (1, 2) as Pair2_v1 : (1, 2), "Pair2_v1"); + __expect_type(rand() != 0 ? (1, 2) as Pair2_v1 : (3, 4) as Pair2_v1, "Pair2_v1"); __expect_type(rand() != 0 ? (1, 2) as Pair2_v2 : (1, 2) as Pair2_v2, "Pair2_v2"); + var hinted_pair: Pair2_v1 = rand() != 0 ? (1, 2) : (3, 4) as Pair2_v1; // exact duplicates collapsed var d1 = (1, 2) as Pair2_v1 | Pair2_v1; @@ -79,7 +78,7 @@ fun test3(x: MIntN, y: MInt?) { } @method_id(104) -fun test4(x: MIntN, y: MIntN) { +fun test4(x: MIntN, y: MIntN): int { if (x != null && y != null) { __expect_type(x, "MInt"); __expect_type(x + y, "int"); @@ -88,9 +87,6 @@ fun test4(x: MIntN, y: MIntN) { __expect_type(x, "MIntN"); __expect_type(x!, "MInt"); __expect_type(rand() != 0 ? x : y, "MIntN"); - __expect_type(rand() != 0 ? x : 0, "MInt?"); - __expect_type(rand() != 0 ? 0 : x, "int?"); - __expect_type(rand() != 0 ? 0 : x!, "int"); return y!; } @@ -248,12 +244,12 @@ fun test13(a: WrapperAlias, b: WrapperAlias, c: Wrapper, d: Wrap a as WrapperAliasOfMInt; match (a) { WrapperAlias => {} } - match (a) { WrapperAlias => {} } match (a) { Wrapper => {} } - match (a) { Wrapper => {} } + // match (a) { WrapperAlias => {} } // downward subtype check + // match (a) { Wrapper => {} } // downward subtype check // match (a) { WrapperOfInt => {} } // match (a) { WrapperOfMInt => {} } - match (a) { WrapperAliasOfInt => {} } + // match (a) { WrapperAliasOfInt => {} } // match (a) { WrapperAliasOfMInt => {} } b as WrapperAlias; @@ -272,14 +268,14 @@ fun test13(a: WrapperAlias, b: WrapperAlias, c: Wrapper, d: Wrap // match (b) { WrapperOfInt => {} } // match (b) { WrapperOfMInt => {} } // match (b) { WrapperAliasOfInt => {} } - match (b) { WrapperAliasOfMInt => {} } + // match (b) { WrapperAliasOfMInt => {} } match (e) { WrapperAliasOfInt => {} } match (f) { WrapperAliasOfMInt => {} } match (f) { Wrapper => {} } match (f) { Wrapper => {} } - // match (f) { WrapperAlias => {} } + match (f) { WrapperAlias => {} } match (f) { WrapperAlias => {} } __expect_type(a, "WrapperAlias"); @@ -311,10 +307,14 @@ fun test14(i: MInt, b: MBool, c: PointAlias2) { type MInt1 = int type MInt2 = MInt1 +type MInt3 = MInt2 +type UserId32 = int32 fun int.plus0(self) { return self + 0 } fun MInt1.plus1(self) { return self + 1 } fun MInt2.plus2(self) { return self + 2 } +fun MInt1.nearestAliasMethod(self): int1 { return 1 } +fun MInt2.nearestAliasMethod(self): int2 { return 2 } @method_id(115) fun test15() { @@ -329,8 +329,8 @@ fun test15() { } return ( - i.plus0(), i.plus1(), i.plus2(), - m.plus0(), m.plus1(), m.plus2(), + i.plus0(), + m.plus0(), m.plus1(), t.plus0(), t.plus1(), t.plus2(), ) } @@ -562,15 +562,86 @@ fun test24() { @method_id(125) fun test25(x: FlagA) { return ( - ((!!x) is FlagB) as int, - ((x == true) is FlagB) as int, - ((x == false) is FlagB) as int, - ((x != false) is FlagB) as int, - ((!!x) is FlagA) == ((!!x) is FlagB), + (x is bool) as int, + ((!!x) is bool) as int, + ((x == true) is bool) as int, + ((x == false) is bool) as int, + ((x != false) is bool) as int, ); } -fun main(x: MInt, y: MInt?) { +struct (0xAC) AliasSingleMsg { + value: uint8 +} + +type AllowedSingleMsg = AliasSingleMsg + +@method_id(126) +fun test26() { + val c = AliasSingleMsg { value: 7 }.toCell(); + val m = lazy AllowedSingleMsg.fromCell(c); + return match (m) { + AliasSingleMsg => m.value as int, + else => 100, + }; +} + +@method_id(127) +fun test27() { + var c: MInt3 = 30; + __expect_type(c.nearestAliasMethod(), "int2"); + return c.nearestAliasMethod(); +} + +fun test28(mapOfUserId: map, wrapperOfUserId: Wrapper) { + var m: map = mapOfUserId; + var w: Wrapper = wrapperOfUserId; + var wa: WrapperAlias = wrapperOfUserId; + m = mapOfUserId; + w = wrapperOfUserId; + wa = wrapperOfUserId; +} + +type MInt3OrSlice = MInt3 | slice + +fun testInferInMatchNarrow() { + var v = 5 as MInt3OrSlice; + match (v) { + MInt2 => { __expect_type(v, "MInt2"); } + slice => {} + }; + match (v) { + MInt1 => { __expect_type(v, "MInt1"); } + slice => {} + }; + match (v) { + int => { __expect_type(v, "int"); } + slice => {} + }; +} + +@method_id(129) +fun test29() { + var extensions: dict = createEmptyDict(); + extensions.iDictSet(32, 8, "12".hexToSlice()); + assert(extensions != null, 123); + __expect_type(extensions, "cell"); + var (value, found) = extensions.iDictGet(32, 8); + return (found as int, value!.preloadUint(8)); +} + +@inline +fun waitUntilNotOwner(x: UserId?): int { + while (!(x is slice)) { + } +} + +@method_id(130) +fun compileInlinedAlwaysTrueLoop(x: UserId?): int { + return waitUntilNotOwner(x); +} + +fun main(x: MInt, y: MInt?): int { return y == null ? x : x + y; } @@ -580,7 +651,7 @@ fun main(x: MInt, y: MInt?) { @testcase | 110 | | 41 @testcase | 112 | 0 | [ 1 1 ] @testcase | 112 | -1 | [ 2 1 ] -@testcase | 115 | | 0 1 2 10 11 12 20 21 22 +@testcase | 115 | | 0 10 11 20 21 22 @testcase | 118 | | 5 0 1 @testcase | 119 | | 8 8 9 @testcase | 120 | | 16 8 10 10 @@ -589,6 +660,9 @@ fun main(x: MInt, y: MInt?) { @testcase | 123 | | 8 7 16 4660 @testcase | 124 | | 8 9 3 -1 7 @testcase | 125 | 0 | -1 -1 -1 -1 -1 +@testcase | 126 | | 7 +@testcase | 127 | | 2 +@testcase | 129 | | -1 18 @fif_codegen """ @@ -602,6 +676,14 @@ fun main(x: MInt, y: MInt?) { }> """ +@fif_codegen +""" + compileInlinedAlwaysTrueLoop() PROC:<{ + DROP + AGAINEND + }> +""" + @abi_json """ { diff --git a/tolk-tester/tests/unbalanced-ret.tolk b/tolk-tester/tests/unbalanced-ret.tolk index 05c7f46d98..3ebeb04ecf 100644 --- a/tolk-tester/tests/unbalanced-ret.tolk +++ b/tolk-tester/tests/unbalanced-ret.tolk @@ -10,7 +10,6 @@ fun main(x: int): (int, int) { return (x + 1, y) } -@inline fun foo1(x: int): int { if (x < 0) { x *= 2; diff --git a/tolk-tester/tests/union-types-tests.tolk b/tolk-tester/tests/union-types-tests.tolk index 862432cc72..5303664e1c 100644 --- a/tolk-tester/tests/union-types-tests.tolk +++ b/tolk-tester/tests/union-types-tests.tolk @@ -255,7 +255,7 @@ fun test22(a: int, b: int | string) { } @method_id(123) -fun test23(p2: bool) { +fun test23(p2: bool): (int, int) { var p: Pair2Or3 = p2 ? (1, 2) : (3, 4, 5); __expect_type(p, "Pair2Or3"); if (p is (int, int) && 10 < 0) { @@ -558,7 +558,7 @@ type IntOrInt8OrInt16 = int | int8 | int16; fun test48(a: IntOrInt8OrInt16) { var b = a; __expect_type(b, "IntOrInt8OrInt16"); - match (b) { MInt => {} int8 => {} int16 => {} } + match (b) { int => {} int8 => {} int16 => {} } __expect_type(b, "IntOrInt8OrInt16"); match (b) { int => {}, int8 => { return ((0, null), (0, null)); }, int16 => "" }; __expect_type(b, "int | int16"); @@ -585,13 +585,13 @@ fun test50() { test49(a); test49(match (a) { int => a, slice => a, null => a, }); __expect_type(b, "VeryComplexType"); - match (b) { int => {} Pair2 => {} builder => {} null => {} } + match (b) { int => {} (MInt, int) => {} builder => {} null => {} } __expect_type(b, "VeryComplexType"); __expect_type(b!, "int | (MInt, int) | builder"); - match (b) { int => 100, Pair2 => {} builder => return -2, null => "null" } - __expect_type(b, "int | Pair2 | null"); + match (b) { int => 100, (MInt, int) => {} builder => return -2, null => "null" } + __expect_type(b, "int | (MInt, int) | null"); if (b !is null) { - match (b) { int => {}, Pair2 => {} } + match (b) { int => {}, (MInt, int) => {} } } else { match (b) { null => {} } } @@ -667,7 +667,7 @@ fun test53(a: int | builder) { }; __expect_type(cc, "int"); match (var (a, (b, c)) = (10, (20, 30))) { - (int, Pair2) => {} + (int, (int, int)) => {} } return cc } @@ -998,6 +998,15 @@ fun test75(case: int) { }; } +@method_id(176) +fun test76(v: int | slice) { + while (v is int) { + break; + } + var w = v; + return 0; +} + fun main() { return 0; @@ -1096,6 +1105,7 @@ fun main() { @testcase | 174 | 1 | 42 @testcase | 175 | 0 | 8 @testcase | 175 | 1 | 37 +@testcase | 176 | 0 1 | 0 @fif_codegen @@ -1111,17 +1121,15 @@ fun main() { NIP // a.UTag DUP // a.UTag a.UTag 1 EQINT // a.UTag '3 - IF:<{ // a.UTag + IFJMP:<{ // a.UTag DROP // 1 PUSHINT // '2=1 - }>ELSE<{ // a.UTag - 4 EQINT // '6 - IF:<{ // - 2 PUSHINT // '2=2 - }>ELSE<{ // - 3 PUSHINT // '2=3 - }> - }> + }> // a.UTag + 4 EQINT // '6 + IFJMP:<{ // + 2 PUSHINT // '2=2 + }> // + 3 PUSHINT // '2=3 }> """ diff --git a/tolk-tester/tests/var-apply-tests.tolk b/tolk-tester/tests/var-apply-tests.tolk index ce3b66551d..ffd45d4543 100644 --- a/tolk-tester/tests/var-apply-tests.tolk +++ b/tolk-tester/tests/var-apply-tests.tolk @@ -28,7 +28,6 @@ fun testVarApply1() { return (s.loadInt(32), s.loadInt(32)); } -@inline fun my_throw_always() { throw 1000; } @@ -49,13 +48,11 @@ fun testVarApplyWithoutSavingResult() { } } -@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; diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-1.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-1.tolk deleted file mode 100644 index d95212d9bc..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-1.tolk +++ /dev/null @@ -1,14 +0,0 @@ -fun main(x: int) { - if (x != 0) { - x = 10;;;;; - return x;;; - x = 20; - } - return -1; -} - -/** -@testcase | 0 | 1 | 10 -@stderr warning: unreachable code -@stderr x = 20; - */ diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-2.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-2.tolk deleted file mode 100644 index 77b41fcd7b..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-2.tolk +++ /dev/null @@ -1,22 +0,0 @@ -fun main(x: int) { - if (x != 0) { - 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 - */ diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-3.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-3.tolk deleted file mode 100644 index 14bc432644..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-3.tolk +++ /dev/null @@ -1,22 +0,0 @@ -fun main(x: int?) { - if (x != null && x == null) { - return 1 + 2; - } - if (x == null) { - return -1; - } - if (x != null) { - return -2; - } - return 3 + 4; -} - -/** -@testcase | 0 | 5 | -2 -@testcase | 0 | null | -1 - -@stderr warning: variable `x` of type `int` can never be `null`, this condition is always true -@stderr if (x != null) -@stderr warning: unreachable code -@stderr return 3 + 4 - */ diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-4.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-4.tolk deleted file mode 100644 index 5411bbec98..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-4.tolk +++ /dev/null @@ -1,24 +0,0 @@ -fun alwaysThrows(): never { - throw 456; -} - -fun testUnreachable(x: int) { - if (x != 0) { throw 123; } - else { alwaysThrows(); } - return 1; -} - -fun main() { - try { - testUnreachable(100); - throw 80; - } catch (excNo) { - return excNo; - } -} - -/** -@testcase | 0 | | 123 -@stderr warning: unreachable code -@stderr return 1; - */ diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-5.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-5.tolk deleted file mode 100644 index 9371ee1275..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-5.tolk +++ /dev/null @@ -1,24 +0,0 @@ -fun main(x: int | slice | null) { - if (x is builder) { - __expect_type(x, "never"); - return 10; - } - if (x !is cell) { - __expect_type(x, "int | slice | null"); - } - if (x is int) { - return x; - } - return (x is null) ? -1 : 20; -} - -/** -@testcase | 0 | 0 1 | 0 -@testcase | 0 | null 0 | -1 -@testcase | 0 | -1 2 | 20 - -@stderr warning: variable `x` of type `int | slice | null` can never be `builder` -@stderr if (x is builder) -@stderr warning: variable `x` of type `int | slice | null` can never be `cell` -@stderr if (x !is cell) - */ diff --git a/tolk-tester/tests/warnings-not-errors/unreachable-6.tolk b/tolk-tester/tests/warnings-not-errors/unreachable-6.tolk deleted file mode 100644 index 9471a88161..0000000000 --- a/tolk-tester/tests/warnings-not-errors/unreachable-6.tolk +++ /dev/null @@ -1,40 +0,0 @@ -//unreachable-6 -enum Role { - User, - Admin, -} - -@method_id(101) -fun checkRole(r: Role) { - match (r) { - Role.User => { - return 1; - } - Role.Admin => { - return 100; - } - } - throw 123; -} - -@method_id(102) -fun checkBool(b: bool) { - match (b) { - true => return 1, - false => return 100, - } - throw 456; -} - -fun main() {} - -/** -@testcase | 101 | 1 | 100 -@testcase | 102 | -1 | 1 -@testcase | 102 | 0 | 100 -@testcase | 102 | 1 | 100 - -@stderr warning: unreachable code -@stderr throw 123; -@stderr throw 456; - */ diff --git a/tolk-tester/tests/warnings-not-errors/warnings-1.tolk b/tolk-tester/tests/warnings-not-errors/warnings-1.tolk deleted file mode 100644 index ec77a2a66e..0000000000 --- a/tolk-tester/tests/warnings-not-errors/warnings-1.tolk +++ /dev/null @@ -1,28 +0,0 @@ -fun getNullableInt(): int? { return null; } - -fun main() { - var c: int? = 6; - __expect_type(c, "int"); - if (c == null) {} - - var d: int? = c; - if (((d)) != null && createEmptyTuple().size()) {} - - var e: int? = getNullableInt(); - if (e != null) { - return true; - } - __expect_type(e, "null"); - null == e; - - return null != null; -} - -/** -@testcase | 0 | | 0 - -@stderr warning: variable `c` of type `int` can never be `null`, this condition is always false -@stderr warning: variable `d` of type `int` can never be `null`, this condition is always true -@stderr warning: variable `e` is always `null`, this condition is always true -@stderr warning: expression is always `null`, this condition is always false - */ diff --git a/tolk-tester/tests/warnings-not-errors/warnings-2.tolk b/tolk-tester/tests/warnings-not-errors/warnings-2.tolk deleted file mode 100644 index 2c5d97e848..0000000000 --- a/tolk-tester/tests/warnings-not-errors/warnings-2.tolk +++ /dev/null @@ -1,26 +0,0 @@ -fun main() { - var (a, b, c, d, e) = (1, beginCell(), beginCell().endCell().beginParse(), [1], true as bool?); - - var alwaysInt = a != null ? 1 : null; - __expect_type(alwaysInt, "int"); - - if (!(c == null)) { - if (10 < 3) { assert(b == null, 100); } - } - while (d == null || false) {} - - return e! != null; -} - -/** -@testcase | 0 | | -1 - -@stderr warning: variable `a` of type `int` can never be `null`, this condition is always true -@stderr warning: condition of ternary operator is always true -@stderr warning: variable `c` of type `slice` can never be `null`, this condition is always false -@stderr warning: condition of `if` is always true -@stderr warning: variable `b` of type `builder` can never be `null`, this condition is always false -@stderr warning: condition of `assert` is always false -@stderr warning: condition of `while` is always false -@stderr warning: expression of type `bool` can never be `null`, this condition is always true - */ diff --git a/tolk-tester/tests/warnings-not-errors/warnings-3.tolk b/tolk-tester/tests/warnings-not-errors/warnings-3.tolk deleted file mode 100644 index 1198bc1049..0000000000 --- a/tolk-tester/tests/warnings-not-errors/warnings-3.tolk +++ /dev/null @@ -1,26 +0,0 @@ -fun main() { - var x = 0 as int | slice; - if (x !is builder) {} - while ((x = x) is builder) {} - - match (val tt = x) { - int => { - assert (tt is int, 505); - } - slice => {} - } - assert(x is int) throw 400; - __expect_type(x, "int"); - return x == null; -} - -/** -@testcase | 0 | | 0 - -@stderr warning: variable `x` of type `int | slice` can never be `builder`, this condition is always true -@stderr warning: condition of `if` is always true -@stderr warning: expression of type `int | slice` can never be `builder`, this condition is always false -@stderr warning: condition of `while` is always false -@stderr warning: variable `tt` is always `int`, this condition is always true -@stderr warning: condition of `assert` is always true - */ diff --git a/tolk-tester/tests/warnings-not-errors/warnings-4.tolk b/tolk-tester/tests/warnings-not-errors/warnings-4.tolk deleted file mode 100644 index bc843ecc7a..0000000000 --- a/tolk-tester/tests/warnings-not-errors/warnings-4.tolk +++ /dev/null @@ -1,15 +0,0 @@ -struct Wrapper { - value: T; -} - -fun main() { - var w = Wrapper { value: 16 as int16 }; - return (w is Wrapper, w is Wrapper, w is Wrapper, w !is Wrapper< Wrapper >); -} - -/** -@testcase | 0 | | -1 -1 0 -1 - -@stderr warning: variable `w` of type `Wrapper` can never be `Wrapper`, this condition is always false -@stderr warning: variable `w` of type `Wrapper` can never be `Wrapper>`, this condition is always true - */ diff --git a/tolk-tester/tests/warnings-not-errors/warnings-6.tolk b/tolk-tester/tests/warnings-not-errors/warnings-6.tolk deleted file mode 100644 index d597d59676..0000000000 --- a/tolk-tester/tests/warnings-not-errors/warnings-6.tolk +++ /dev/null @@ -1,37 +0,0 @@ -type UserId = int -type OwnerId = int - -fun directAliasCheck(x: UserId?): bool { - return x is OwnerId; -} - -@inline -fun waitUntilNotOwner(x: UserId?): int { - while (!(x is OwnerId)) { - } -} - -fun main(x: UserId?): bool { - return directAliasCheck(x); -} - -@method_id(102) -fun compileInlinedAlwaysTrueLoop(x: UserId?): int { - return waitUntilNotOwner(x); -} - -/** -@testcase | 0 | null | 0 -@testcase | 0 | 123 | 0 - -@stderr warning: variable `x` of type `UserId?` can never be `OwnerId`, this condition is always false -@stderr warning: condition of `while` is always true - -@fif_codegen -""" - compileInlinedAlwaysTrueLoop() PROC:<{ - DROP - AGAINEND - }> -""" - */ diff --git a/tolk-tester/tolk-tester.js b/tolk-tester/tolk-tester.js index 327050074f..21ba77a853 100644 --- a/tolk-tester/tolk-tester.js +++ b/tolk-tester/tolk-tester.js @@ -278,6 +278,8 @@ class TolkTestFile { this.expected_hash = null /** @type {Object} */ this.path_mappings = {} + /** @type {Object} */ + this.wasm_options = {} /** @type {boolean} */ this.enable_tolk_lines_comments = false /** @type {number} */ @@ -322,6 +324,15 @@ class TolkTestFile { } else if (line.startsWith("@path_mapping")) { let eq_pos = line.indexOf('=') this.path_mappings[line.substring(14, eq_pos)] = line.substring(eq_pos+1).replace('{DIR}', path.dirname(this.tolk_filename)).replace(/[\\\/]+$/, '') + } else if (line.startsWith("@cmd_line_option")) { + for (const opt of line.substring(16).trim().split(/\s+/).filter(s => s.length > 0)) { + if (opt === "--allow-empty-get-fun") + this.wasm_options.allowEmptyGetFun = true + else if (opt === "--allow-no-entrypoint") + this.wasm_options.allowNoEntrypoint = true + else + throw new ParseInputError(`unknown @cmd_line_option ${opt}`) + } } this.line_idx++ } @@ -369,7 +380,7 @@ class TolkTestFile { async run_and_check() { const wasmModule = await compileWasm(TOLKFIFTLIB_MODULE, TOLKFIFTLIB_WASM) - let outputStr = compileFile(wasmModule, this.tolk_filename, this.enable_tolk_lines_comments, this.path_mappings) + let outputStr = compileFile(wasmModule, this.tolk_filename, this.enable_tolk_lines_comments, this.path_mappings, this.wasm_options) /** @var {{status: string, message: string, fiftCode: string, codeBoc: string, codeHashHex: string, abiJson: Object}} */ let res = JSON.parse(outputStr); let exit_code = res.status === 'ok' ? 0 : 1 @@ -380,7 +391,7 @@ class TolkTestFile { if (exit_code === 0 && this.compilation_should_fail) throw new TolkCompilationSucceededError("compilation succeeded, but it should have failed") - for (let should_include of this.stderr_includes) // @stderr is used to check errors and warnings + for (let should_include of this.stderr_includes) // @stderr is used to check errors should_include.check(stderr) if (exit_code !== 0 && this.compilation_should_fail) @@ -521,7 +532,7 @@ function copyFromCString(mod, ptr) { } /** @return string */ -function compileFile(mod, filename, withSrcLineComments, pathMappings) { +function compileFile(mod, filename, withSrcLineComments, pathMappings, wasmOptions = {}) { // see tolk-wasm.cpp: typedef void (*WasmFsReadCallback)(int, char const*, char**, char**) const callbackPtr = mod.addFunction((kind, dataPtr, destContents, destError) => { switch (kind) { // enum ReadCallback::Kind in C++ @@ -578,11 +589,11 @@ function compileFile(mod, filename, withSrcLineComments, pathMappings) { }, 'viiiii'); const config = { - optimizationLevel: 2, withStackComments: true, withSrcLineComments: withSrcLineComments, withSymbolTypes: false, - entrypointFileName: filename + entrypointFileName: filename, + ...wasmOptions }; const configPtr = copyToCString(mod, JSON.stringify(config)); diff --git a/tolk-tester/tolk-tester.py b/tolk-tester/tolk-tester.py index ce6b1ddfb2..93fbb274a0 100644 --- a/tolk-tester/tolk-tester.py +++ b/tolk-tester/tolk-tester.py @@ -307,6 +307,8 @@ def parse_input_from_tolk_file(self): self.expected_hash = TolkTestCaseExpectedHash(self.parse_string_value(lines, False)[0]) elif line.startswith("@path_mapping"): self.more_cmd_line_options += ["--path-mapping", line[14:].replace('{DIR}', os.path.dirname(self.tolk_filename))] + elif line.startswith("@cmd_line_option"): + self.more_cmd_line_options += line[len("@cmd_line_option"):].strip().split() self.line_idx = self.line_idx + 1 if len(self.input_output) == 0 and not self.compilation_should_fail: @@ -349,7 +351,7 @@ def get_runner_fif_filename(self): def run_and_check(self): cmd_args = ([TOLK_EXECUTABLE, "-o", self.get_compiled_fif_filename(), - "--no-symbol-types", "--no-compiled-boc"] + "--no-symbol-types", "--no-compiled-boc", "--color", "never"] + self.more_cmd_line_options) if not self.abi_json: cmd_args += ["--no-contract-abi"] @@ -363,7 +365,7 @@ def run_and_check(self): 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 + for should_include in self.stderr_includes: # @stderr is used to check errors should_include.check(stderr) if exit_code != 0 and self.compilation_should_fail: diff --git a/tolk/CMakeLists.txt b/tolk/CMakeLists.txt index c8e81b690d..e450ac3962 100644 --- a/tolk/CMakeLists.txt +++ b/tolk/CMakeLists.txt @@ -15,6 +15,8 @@ set(TOLK_SOURCE pack-unpack-serializers.cpp send-message-api.cpp maps-kv-api.cpp + inline-return-analysis.cpp + loop-control-analysis.cpp pipe-discover-parse-sources.cpp pipe-register-symbols.cpp pipe-resolve-identifiers.cpp @@ -23,16 +25,16 @@ set(TOLK_SOURCE pipe-infer-types-and-calls.cpp pipe-check-inferred-types.cpp pipe-refine-lvalue-for-mutate.cpp + pipe-loop-break-continue.cpp pipe-check-rvalue-lvalue.cpp pipe-check-private-fields.cpp - pipe-check-pure-impure.cpp pipe-check-const-expr.cpp pipe-mini-borrow-checker.cpp pipe-optimize-boolean-expr.cpp pipe-detect-inline-in-place.cpp pipe-check-serialized-fields.cpp pipe-lazy-load-insertions.cpp - pipe-transform-on-message.cpp + pipe-check-on-message.cpp pipe-ast-to-legacy.cpp pipe-find-unused-symbols.cpp pipe-collect-symbol-types.cpp diff --git a/tolk/abscode.cpp b/tolk/abscode.cpp index b2af3e971d..0a64172d81 100644 --- a/tolk/abscode.cpp +++ b/tolk/abscode.cpp @@ -197,8 +197,8 @@ void Op::show(std::ostream& os, const std::vector& vars, const std::stri if (noreturn()) { dis += " "; } - if (impure()) { - dis += " "; + if (keep_even_if_unused()) { + dis += " "; } switch (cl) { case _Nop: @@ -332,6 +332,9 @@ void Op::show(std::ostream& os, const std::vector& vars, const std::stri block0.show(os, vars, indent, mode); os << std::endl; break; + case _BreakFromLoop: + os << indent << dis << "BREAK" << std::endl; + break; case _TryCatch: os << indent << dis << "TRYCATCH "; show_var_list(os, left, vars); diff --git a/tolk/analyzer.cpp b/tolk/analyzer.cpp index 0415fdc23c..30302814d3 100644 --- a/tolk/analyzer.cpp +++ b/tolk/analyzer.cpp @@ -29,6 +29,50 @@ static bool does_function_always_throw(FunctionPtr fun_ref) { return fun_ref->inferred_return_type == TypeDataNever::create(); } +bool OpList::has_reachable_direct_break() const { + for (const auto& op : *this) { + if (op->cl == Op::_BreakFromLoop) { + return true; + } + if (op->cl == Op::_If || op->cl == Op::_TryCatch) { + if (op->block0.has_reachable_direct_break() || op->block1.has_reachable_direct_break()) { + return true; + } + } + } + return false; +} + +// Successor of `_BreakFromLoop` is the enclosing loop's exit, not the next body instruction. +static thread_local const VarDescrList* current_loop_exit_var_info = nullptr; + +struct LoopExitGuard { + const VarDescrList* prev; + explicit LoopExitGuard(const VarDescrList& exit_info) + : prev(current_loop_exit_var_info) { + current_loop_exit_var_info = &exit_info; + } + ~LoopExitGuard() { + current_loop_exit_var_info = prev; + } +}; + +// Facts at `_BreakFromLoop` after fwd_analyze; same walk as has_reachable_direct_break +// (if/try only — nested loops keep their own breaks). +static VarDescrList collect_break_facts(const OpList& ops) { + VarDescrList collected; + collected.set_unreachable(); + for (const auto& op : ops) { + if (op->cl == Op::_BreakFromLoop) { + collected |= op->var_info; + } else if (op->cl == Op::_If || op->cl == Op::_TryCatch) { + collected |= collect_break_facts(op->block0); + collected |= collect_break_facts(op->block1); + } + } + return collected; +} + /* * * ANALYZE AND PREPROCESS ABSTRACT CODE @@ -329,13 +373,16 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { case _Tuple: case _UnTuple: { // left = EXEC right; - if (!next_var_info.count_used(left) && !impure()) { + if (!next_var_info.count_used(left) && !keep_even_if_unused()) { // all variables in `left` are not needed if (edit) { set_disabled(); } return std_compute_used_vars(next_var_info, true); } + if (edit) { + set_disabled(false); + } if (cl == _Call && does_function_always_throw(f_sym)) { VarDescrList new_var_info; // empty, not next_var_info if (args.size() == right.size()) { @@ -359,6 +406,7 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { case _Let: { // left = right std::size_t cnt = next_var_info.count_used(left); + bool force_keep = keep_even_if_unused(); tolk_assert(left.size() == right.size()); auto l_it = left.cbegin(), r_it = right.cbegin(); VarDescrList new_var_info{next_var_info}; @@ -368,7 +416,7 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { for (; l_it < left.cend(); ++l_it, ++r_it) { if (std::find(l_it + 1, left.cend(), *l_it) == left.cend()) { auto p = next_var_info[*l_it]; - new_var_info.add_var(*r_it, edit && (!p || p->is_unused())); + new_var_info.add_var(*r_it, !force_keep && edit && (!p || p->is_unused())); new_left.push_back(*l_it); new_right.push_back(*r_it); } @@ -377,9 +425,11 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { left = std::move(new_left); right = std::move(new_right); } - if (!cnt && edit) { + if (!cnt && edit && !force_keep) { // all variables in `left` are not needed set_disabled(); + } else if (edit) { + set_disabled(false); } return set_var_info(std::move(new_var_info)); } @@ -395,6 +445,11 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { } return true; } + case _BreakFromLoop: { + // successor is the enclosing loop's exit, not the next body instruction + tolk_assert(current_loop_exit_var_info); + return set_var_info(*current_loop_exit_var_info); + } case _Import: { // import left std::vector unique_vars = sort_unique_vars(left); @@ -421,6 +476,9 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { case _While: { // while (block0 || left) block1; // ... block0 left { block1 block0 left } next + // Break must leave the same stack as a normal loop exit, so keep header vars too. + VarDescrList break_exit{next_var_info}; + LoopExitGuard exit_guard(break_exit); VarDescrList new_var_info{next_var_info}; bool changes = false; do { @@ -431,6 +489,7 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { std::size_t n = new_var_info.size(); new_var_info += block1.entry_var_info(); new_var_info.clear_last(); + break_exit += new_var_info; if (changes) { break; } @@ -438,11 +497,15 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { } while (changes <= edit); new_var_info += left; block0.compute_used_code_vars(new_var_info, edit); - return set_var_info(block0.entry_var_info()); + break_exit += block0.entry_var_info(); + bool body_changed = block1.compute_used_code_vars(block0.entry_var_info(), edit); + return static_cast(body_changed) | static_cast(set_var_info(block0.entry_var_info())); } case _Until: { // until (block0 || left); // .. { block0 left } block0 left next + VarDescrList break_exit{next_var_info}; + LoopExitGuard exit_guard(break_exit); VarDescrList after_cond_first{next_var_info}; after_cond_first += left; block0.compute_used_code_vars(after_cond_first, false); @@ -456,6 +519,7 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { std::size_t n = new_var_info.size(); new_var_info += block0.entry_var_info(); new_var_info.clear_last(); + break_exit += new_var_info; if (changes) { break; } @@ -466,6 +530,8 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { case _Repeat: { // repeat (left) block0 // left { block0 } next + VarDescrList break_exit{next_var_info}; + LoopExitGuard exit_guard(break_exit); VarDescrList new_var_info{next_var_info}; bool changes = false; do { @@ -473,6 +539,7 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { std::size_t n = new_var_info.size(); new_var_info += block0.entry_var_info(); new_var_info.clear_last(); + break_exit += new_var_info; if (changes) { break; } @@ -489,6 +556,9 @@ bool Op::compute_used_vars(bool edit, const VarDescrList& next_var_info) { case _Again: { // for(;;) block0 // { block0 } + // no normal-exit: header liveness is the back-edge only; break sees next_var_info + VarDescrList break_exit{next_var_info}; + LoopExitGuard exit_guard(break_exit); VarDescrList new_var_info; bool changes = false; do { @@ -564,6 +634,7 @@ bool OpList::prune_unreachable() { reach = true; break; case Op::_Return: + case Op::_BreakFromLoop: reach = false; break; case Op::_Call: @@ -613,31 +684,30 @@ bool OpList::prune_unreachable() { continue; // remaining ops processed in next iterations } if (c_var && c_var->always_true()) { - if (!op.block1.prune_unreachable()) { - // block1 never returns — combine block0+block1, unreachable + if (!op.block1.prune_unreachable() && !op.block1.has_reachable_direct_break()) { + // body never returns and cannot break — unroll, code after the loop is dead for (auto& o : merge_blocks(op.block0, op.block1)) { result.push_back(std::move(o)); } ops = std::move(result); return false; } - // infinite loop: transform while → again, merge block0+block1 into block0 + // infinite loop (maybe exited by break): while → again op.cl = Op::_Again; op.block0 = merge_blocks(op.block0, op.block1); op.block1.clear(); op.left.clear(); - reach = false; + reach = op.block0.has_reachable_direct_break(); } else { - if (!op.block1.prune_unreachable()) { - // block1 never returns - // transform: while(block0; cond) block1; next → block0_content; if(cond) block1 else {}; next + if (!op.block1.prune_unreachable() && !op.block1.has_reachable_direct_break()) { + // body never returns and cannot break (a break would need *BRK, not a plain if) + // while(block0; cond) block1; next → block0_content; if(cond) block1 else {}; next inline_block_content(result, op.block0); op.cl = Op::_If; op.block0 = std::move(op.block1); // if-then = old while-body op.block1.clear(); op.block1.push_back(make_terminal_nop(op.origin)); // else = empty } - // keep the (possibly transformed) op reach = true; } break; @@ -650,8 +720,8 @@ bool OpList::prune_unreachable() { continue; } if (c_var && c_var->always_pos()) { - if (!op.block0.prune_unreachable()) { - // block0 executed at least once, and it never returns + if (!op.block0.prune_unreachable() && !op.block0.has_reachable_direct_break()) { + // executed at least once, never returns, cannot break inline_block_content(result, op.block0); result.push_back(make_terminal_nop(op.origin)); ops = std::move(result); @@ -666,14 +736,14 @@ bool OpList::prune_unreachable() { case Op::_Until: case Op::_Again: { // do block0 until left; ... - if (!op.block0.prune_unreachable()) { - // block0 never returns, replace loop by block0 + if (!op.block0.prune_unreachable() && !op.block0.has_reachable_direct_break()) { + // never returns and cannot break — replace loop by the body inline_block_content(result, op.block0); result.push_back(make_terminal_nop(op.origin)); ops = std::move(result); return false; } - reach = (op.cl != Op::_Again); + reach = (op.cl != Op::_Again) || op.block0.has_reachable_direct_break(); break; } case Op::_TryCatch: { @@ -699,7 +769,7 @@ bool OpList::prune_unreachable() { void CodeBlob::prune_unreachable_code() { if (ops.prune_unreachable()) { - err("control reaches end of function (stack is malformed, a compiler bug)").fire(fun_ref->ident_anchor, fun_ref); + err("control reaches end of function (stack is malformed, a compiler bug)").fire(fun_ref); } } @@ -765,6 +835,7 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { case _DebugMark: break; case _Return: + case _BreakFromLoop: values.set_unreachable(); break; case _IntConst: { @@ -797,6 +868,13 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { maybe_swap_builtin_args_to_compile(); } } + bool result_is_int_const = !left.empty(); + for (const VarDescr& r : res) { + result_is_int_const &= r.is_int_const(); + } + if (f_sym->is_removable_if_unused() && result_is_int_const) { + set_keep_flag(false); + } int j = 0; for (var_idx_t i : left) { values.add_newval(i).set_value(res[j++]); @@ -872,6 +950,7 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { if (atl1) { values = std::move(next_values); } + values |= collect_break_facts(block0); break; } case _While: { @@ -880,6 +959,7 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { if (values[left[0]] && values[left[0]]->always_false()) { // block1 never executed block1.fwd_analyze(values); + values |= collect_break_facts(block1); break; } while (true) { @@ -889,6 +969,7 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { } values = std::move(next_values); } + values |= collect_break_facts(block1); break; } case _Until: @@ -900,7 +981,12 @@ VarDescrList Op::fwd_analyze(VarDescrList values) { } values = std::move(next_values); } - values = block0.fwd_analyze(values); + if (cl == _Again) { + values = collect_break_facts(block0); + } else { + values = block0.fwd_analyze(values); + values |= collect_break_facts(block0); + } break; } case _TryCatch: { @@ -933,8 +1019,12 @@ bool Op::set_noreturn(bool flag) { return flag; } -void Op::set_impure_flag() { - flags |= _Impure; +void Op::set_keep_flag(bool flag) { + if (flag) { + flags |= _KeepEvenIfUnused; + } else { + flags &= ~_KeepEvenIfUnused; + } } void Op::set_arg_order_already_equals_asm_flag() { @@ -1018,6 +1108,7 @@ bool OpList::mark_noreturn() { op.set_noreturn(next_noreturn); break; case Op::_Return: + case Op::_BreakFromLoop: op.set_noreturn(); break; case Op::_Call: @@ -1030,10 +1121,10 @@ bool OpList::mark_noreturn() { op.set_noreturn((op.block0.is_noreturn() && op.block1.is_noreturn()) || next_noreturn); break; case Op::_Again: - op.set_noreturn(); // infinite loop = always noreturn + op.set_noreturn(!op.block0.has_reachable_direct_break() || next_noreturn); break; case Op::_Until: - op.set_noreturn(op.block0.is_noreturn() || next_noreturn); + op.set_noreturn((op.block0.is_noreturn() && !op.block0.has_reachable_direct_break()) || next_noreturn); break; case Op::_While: op.set_noreturn(op.block0.is_noreturn() || next_noreturn); @@ -1053,6 +1144,95 @@ void CodeBlob::mark_noreturn() { ops.mark_noreturn(); } +// See `materialize_immediate_returns` below for the purpose. +static void copy_trailing_return_into(OpList& into_blk, const OpList& src, size_t from, size_t ret_idx) { + // skip a branch that already returns + for (int i = static_cast(into_blk.size()) - 1; i >= 0; --i) { + Op::OpKind cl = into_blk[i]->cl; + if (cl == Op::_Nop || cl == Op::_DebugMark) { + continue; + } + if (cl == Op::_Return || cl == Op::_BreakFromLoop) { + return; + } + break; + } + tolk_assert(!into_blk.empty() && into_blk.back()->cl == Op::_Nop); + + // clone preceding Op::_DebugMark and the trailing Op::_Return + for (size_t j = from; j < ret_idx; ++j) { + if (src[j]->cl == Op::_DebugMark) { + auto dst = std::make_unique(src[j]->origin, Op::_DebugMark); + dst->debug_mark = src[j]->debug_mark; + into_blk.insert(into_blk.end() - 1, std::move(dst)); // -1: before the terminal _Nop + } + } + auto dst = std::make_unique(src[ret_idx]->origin, Op::_Return, src[ret_idx]->left); + dst->debug_mark = src[ret_idx]->debug_mark; // it's DebugMarkLeaveFunction + into_blk.insert(into_blk.end() - 1, std::move(dst)); +} + +// If `Op::_If` is followed by `Op::_Return` (debug marks in between), clone that return into both branches. +static void try_materialize_if(OpList& ops, size_t if_idx) { + size_t j = if_idx + 1; + while (j < ops.size() && ops[j]->cl == Op::_DebugMark) { + ++j; + } + if (j >= ops.size() || ops[j]->cl != Op::_Return) { + return; + } + + // skip multi-slot returns: they duplicate stack shuffles + const Op& ret = *ops[j]; + tolk_assert(!ret.disabled()); + if (ret.left.size() > 1) { + return; + } + copy_trailing_return_into(ops[if_idx]->block0, ops, if_idx + 1, j); + copy_trailing_return_into(ops[if_idx]->block1, ops, if_idx + 1, j); +} + +static void materialize_in_list(OpList& ops) { + for (size_t i = 0; i < ops.size(); ++i) { + Op& op = *ops[i]; + if (op.cl == Op::_If) { + try_materialize_if(ops, i); + } + if (!op.block0.empty()) { + materialize_in_list(op.block0); + } + if (!op.block1.empty()) { + materialize_in_list(op.block1); + } + } +} + +/* + * Clone a `_Return` that immediately follows `_If` into both branches. + * Then `mark_noreturn` + codegen emit IFJMP instead of IF + RET. + * + * Example: + * > fun demo() { + * > if (c) { A } + * > else { B } + * > } + * + * IR now: + * > Op::_If then={ A } else={ B } + * > Op::_Return // it's end of function + * + * IR after transformation: + * > Op::_If then={ A; Op::_Return } else={ B; Op::_Return } + * > Op::_Return // codegen does not emit it: Op::_If is already noreturn + * + * Result fif: + * > IFJMP:<{ A }> + * > B + */ +void CodeBlob::materialize_immediate_returns() { + materialize_in_list(ops); +} + /* * Strip redundant `!= 0` / `== 0` from `if`, `while`, etc. * `if (x != 0)` -> IF (no `0 NEQINT`) @@ -1069,8 +1249,8 @@ void CodeBlob::mark_noreturn() { * So we explicitly search for that shape — see `try_fold_condition`. * * How `_!=_` / `_==_` are removed. - * We don't disable the matched `_Call`: we just stop using its result. - * On the next iteration, `compute_used_code_vars` sees that nobody reads it anymore, and DCE prunes it. + * After rewiring the condition to `x`, clear keep flag on the matched `_Call`. + * On the next `compute_used_code_vars` pass, if nobody else reads its result, DCE prunes it. */ // Recognize `tmp := x != 0` or `tmp := x == 0`. On match: @@ -1262,6 +1442,7 @@ static bool try_fold_condition(Op& cond_op, OpList& search_list, int search_max_ if (is_eq) { std::swap(cond_op.block0, cond_op.block1); } + producer.op->set_keep_flag(false); any_changed = true; // keep looping to detect cases like `(x != 0) != 0` } @@ -1355,6 +1536,7 @@ static bool try_fold_call_cond_arg(Op& call_op, OpList& search_list, int search_ } // important: `args` is cached, wipe it. `compute_used_vars` reads variable indices from `args`, not from `right`. call_op.args.clear(); + producer.op->set_keep_flag(false); any_changed = true; } return any_changed; diff --git a/tolk/ast-aux-data.h b/tolk/ast-aux-data.h deleted file mode 100644 index 8d7626b86e..0000000000 --- a/tolk/ast-aux-data.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - This file is part of TON Blockchain Library. - - TON Blockchain Library is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - TON Blockchain Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with TON Blockchain Library. If not, see . -*/ -#pragma once - -#include "ast.h" -#include "lazy-helpers.h" - -/* - * This file contains a schema of aux_data inside ast_artificial_aux_vertex - * (it's a compiler-inserted vertex that can't occur in source code). - */ - -namespace tolk { - -struct CodeBlob; - -// AuxData_LazyObjectLoadFields is a special auto-inserted vertex to load fields of a lazy struct; -// example: `var p = lazy Point.fromSlice(s); aux "load x"; return p.x` -struct AuxData_LazyObjectLoadFields final : ASTAuxData { - LocalVarPtr var_ref; // comes from `lazy` - TypePtr union_variant; // not just `o` but `match(o) { V1 => here }` - StructFieldPtr field_ref; // not just `o` but `match(o.field) { V1 => here }` - LazyStructLoadInfo load_info; // instructions, which fields to load, which to skip, etc. - - AuxData_LazyObjectLoadFields(LocalVarPtr var_ref, TypePtr union_variant, StructFieldPtr field_ref, LazyStructLoadInfo load_info) - : var_ref(var_ref), union_variant(union_variant), field_ref(field_ref), load_info(std::move(load_info)) { - } -}; - -// AuxData_LazyMatchForUnion wraps `match(lazy_var)` or its field -struct AuxData_LazyMatchForUnion final : ASTAuxData { - LocalVarPtr var_ref; // comes from `lazy` - StructFieldPtr field_ref; // not `match(o)`, but `match(o.field)` - - AuxData_LazyMatchForUnion(LocalVarPtr var_ref, StructFieldPtr field_ref) - : var_ref(var_ref), field_ref(field_ref) { - } -}; - -struct AuxData_OnInternalMessage_getField final : ASTAuxData { - FunctionPtr f_onInternalMessage; - const std::string_view field_name; - - AuxData_OnInternalMessage_getField(FunctionPtr f_onInternalMessage, std::string_view field_name) - : f_onInternalMessage(f_onInternalMessage) - , field_name(field_name) { - } - - std::vector generate_get_InMessage_field(CodeBlob& code, AnyV origin) const; -}; - -} // namespace tolk diff --git a/tolk/ast-from-tokens.cpp b/tolk/ast-from-tokens.cpp index 3bd594925b..2118a76e1d 100644 --- a/tolk/ast-from-tokens.cpp +++ b/tolk/ast-from-tokens.cpp @@ -51,7 +51,6 @@ static bool is_add_or_sub_binary_op(TokenType tok) { } // make an error for a case "flags & 0xFF != 0" (equivalent to "flags & 1", probably unexpected) -// it would better be a warning, but we decided to make it a strict error static Error err_lower_precedence(std::string_view op_lower, std::string_view op_higher) { return err("{} has lower precedence than {}" ", probably this code won't work as you expected. " @@ -471,9 +470,10 @@ struct AnnotationsAbove { std::vector> above; std::string_view doc_lines_buf[100]; int n_doc_lines = 0; + bool has_ignored_custom = false; // @custom/@test/@deprecated are not stored in `above` bool empty() const { - return above.empty(); + return above.empty() && !has_ignored_custom; } void collect_doc_comment(Lexer& lex) { @@ -491,6 +491,7 @@ struct AnnotationsAbove { } above.clear(); n_doc_lines = 0; + has_ignored_custom = false; return result; } @@ -506,9 +507,11 @@ struct AnnotationsAbove { } } - if (v_annotation->kind != AnnotationKind::custom) { // totally ignore @custom, @deprecated, etc. + if (v_annotation->kind != AnnotationKind::custom) { // don't store @custom, @deprecated, etc. in AST above.push_back(v_annotation); // allow any arguments, don't analyze - } // don't even store them in AST tree + } else { + has_ignored_custom = true; // but still remember them (e.g. for EOF check) + } } }; @@ -756,7 +759,7 @@ static AnyExprV parse_var_declaration_lhs(Lexer& lex, bool is_immutable, bool al } if (lex.tok() == tok_semicolon && allow_lateinit) { if (declared_type == nullptr) { - lex.error("provide a type for a variable, because its default value is omitted:\n> var " + static_cast(v_ident->name) + ": ;"); + lex.error("provide a type for a variable, because its default value is omitted\n""hint:\n> var " + static_cast(v_ident->name) + ": ;"); } is_lateinit = true; } @@ -1042,7 +1045,8 @@ static V parse_match_arm(Lexer& lex) { range.end(body->range); if (pattern_expr == nullptr) { // for match by type / default case, empty vertex, not nullptr - pattern_expr = createV(SrcRange::span(range, 4)); + SrcRange range_before_arrow = exact_type ? exact_type->range : SrcRange::span(range, 4); + pattern_expr = createV(range_before_arrow); } return createV(range, pattern_kind, exact_type, pattern_expr, body); } @@ -1485,6 +1489,20 @@ AnyExprV parse_expr(Lexer& lex) { return parse_expr10(lex); } +static AnyV parse_break_statement(Lexer& lex) { + lex.check(tok_break, "`break`"); + SrcRange range = lex.cur_range(); + lex.next(); + return createV(range); +} + +static AnyV parse_continue_statement(Lexer& lex) { + lex.check(tok_continue, "`continue`"); + SrcRange range = lex.cur_range(); + lex.next(); + return createV(range); +} + static AnyV parse_return_statement(Lexer& lex) { lex.check(tok_return, "`return`"); SrcRange range = lex.cur_range(); @@ -1660,8 +1678,9 @@ AnyV parse_statement(Lexer& lex) { case tok_semicolon: return createV(lex.cur_range()); case tok_break: + return parse_break_statement(lex); case tok_continue: - lex.error("break/continue from loops are not supported yet"); + return parse_continue_statement(lex); default: return parse_expr(lex); } @@ -1774,11 +1793,13 @@ static AnyV parse_function_declaration(Lexer& lex, AnnotationsAbove& annotations } } bool is_code_function = lex.tok() == tok_opbrace; + // `get fun name(): T` without `{` is an ABI-only prototype (only with `--allow-empty-get-fun`, checked later) + bool is_get_prototype_only = is_contract_getter && !is_code_function && lex.tok() != tok_asm && lex.tok() != tok_builtin; if (is_entrypoint && (is_contract_getter || genericsT_list || n_mutate_params || !is_code_function)) { err("invalid declaration of a reserved function").fire(v_ident); } - if (is_contract_getter && (genericsT_list || n_mutate_params || receiver_type || !is_code_function)) { + if (is_contract_getter && (genericsT_list || n_mutate_params || receiver_type || (!is_code_function && !is_get_prototype_only))) { err("invalid declaration of a get method").fire(v_ident); } @@ -1794,6 +1815,11 @@ static AnyV parse_function_declaration(Lexer& lex, AnnotationsAbove& annotations lex.error("asm function must specify return type"); } v_body = parse_asm_func_body(lex, v_ident, v_param_list); + } else if (is_get_prototype_only) { + if (!ret_type) { + err("get method prototype must declare return type").fire(v_ident); + } + v_body = createV(lex.cur_range()); } else { lex.unexpected("{ function body }"); } @@ -1813,14 +1839,14 @@ static AnyV parse_function_declaration(Lexer& lex, AnnotationsAbove& annotations } int tvm_method_id = FunctionData::EMPTY_TVM_METHOD_ID; - FunctionInlineMode inline_mode = FunctionInlineMode::notCalculated; + FunctionInlineMode inline_mode = FunctionInlineMode::notAnnotated; for (auto v_annotation : annotations.above) { switch (v_annotation->kind) { case AnnotationKind::inline_simple: if (v_body->kind == ast_asm_body) { err("inline annotations are not applicable to asm functions").fire(v_annotation); } - inline_mode = FunctionInlineMode::inlineViaFif; // maybe will be replaced by inlineInPlace later + inline_mode = FunctionInlineMode::inlineInPlace; // will be checked in detect-inline-in-place.cpp break; case AnnotationKind::inline_ref: if (v_body->kind == ast_asm_body) { @@ -1835,7 +1861,10 @@ static AnyV parse_function_declaration(Lexer& lex, AnnotationsAbove& annotations inline_mode = FunctionInlineMode::noInline; break; case AnnotationKind::pure: - flags |= FunctionData::flagMarkedAsPure; + if (v_body->kind == ast_block_statement) { + err("@pure has no effect on a regular function since user calls are now always preserved\n""hint: remove `@pure`").fire(v_annotation); + } + flags |= FunctionData::flagRemovableIfUnused; break; case AnnotationKind::method_id: { if (is_contract_getter || genericsT_list || receiver_type || is_entrypoint || n_mutate_params || accepts_self || !is_code_function) { @@ -2127,7 +2156,7 @@ static AnyV parse_tolk_required_version(Lexer& lex) { // for simplicity, there is no syntax ">= version" and so on, just strict compare if (TOLK_VERSION != semver && TOLK_VERSION != semver + ".0") { // 0.6 = 0.6.0 - err("the contract is written in Tolk v{}, but you use Tolk compiler v{}; probably, it will lead to compilation errors or hash changes", semver, TOLK_VERSION).warning(range, nullptr); + err("the contract is written in Tolk v{}, but you use Tolk compiler v{}\n""hint: replace this line with `tolk {}`", semver, TOLK_VERSION, TOLK_VERSION).fire(range, nullptr); } return createV(range, std::move(semver)); diff --git a/tolk/ast-replacer.h b/tolk/ast-replacer.h index c2f5a7fe02..6446ff377c 100644 --- a/tolk/ast-replacer.h +++ b/tolk/ast-replacer.h @@ -97,7 +97,6 @@ class ASTReplacerInFunctionBody : public ASTReplacer { virtual AnyExprV replace(V v) { return replace_children(v); } virtual AnyExprV replace(V v) { return replace_children(v); } virtual AnyExprV replace(V v) { return replace_children(v); } - virtual AnyExprV replace(V v) { return replace_children(v); } virtual AnyExprV replace(V v) { return replace_children(v); } virtual AnyExprV replace(V v) { return replace_children(v); } virtual AnyExprV replace(V v) { return replace_children(v); } @@ -136,6 +135,8 @@ class ASTReplacerInFunctionBody : public ASTReplacer { virtual AnyV replace(V v) { return replace_children(v); } virtual AnyV replace(V v) { return replace_children(v); } virtual AnyV replace(V v) { return replace_children(v); } + virtual AnyV replace(V v) { return replace_children(v); } + virtual AnyV replace(V v) { return replace_children(v); } virtual AnyV replace(V v) { return replace_children(v); } virtual AnyV replace(V v) { return replace_children(v); } virtual AnyV replace(V v) { return replace_children(v); } @@ -145,7 +146,6 @@ class ASTReplacerInFunctionBody : public ASTReplacer { case ast_empty_expression: return replace(v->as()); case ast_braced_expression: return replace(v->as()); case ast_braced_yield_result: return replace(v->as()); - case ast_artificial_aux_vertex: return replace(v->as()); case ast_tensor: return replace(v->as()); case ast_square_brackets: return replace(v->as()); case ast_reference: return replace(v->as()); @@ -190,6 +190,8 @@ class ASTReplacerInFunctionBody : public ASTReplacer { case ast_repeat_statement: return replace(v->as()); case ast_while_statement: return replace(v->as()); case ast_do_while_statement: return replace(v->as()); + case ast_break_statement: return replace(v->as()); + case ast_continue_statement: return replace(v->as()); case ast_throw_statement: return replace(v->as()); case ast_assert_statement: return replace(v->as()); case ast_try_catch_statement: return replace(v->as()); @@ -221,16 +223,15 @@ class ASTReplacerInFunctionBody : public ASTReplacer { }; -const std::vector& get_all_builtin_functions(); -const std::vector& get_all_not_builtin_functions(); +const std::vector& get_all_functions(); const std::vector& get_all_declared_constants(); const std::vector& get_all_declared_structs(); const std::vector& get_all_declared_enums(); template void replace_ast_of_all_functions(BodyReplacerT& replacer) { - for (FunctionPtr fun_ref : get_all_not_builtin_functions()) { - if (replacer.should_visit_function(fun_ref)) { + for (FunctionPtr fun_ref : get_all_functions()) { + if (fun_ref->ast_root && replacer.should_visit_function(fun_ref)) { replacer.start_replacing_in_function(fun_ref, fun_ref->ast_root->as()); } } diff --git a/tolk/ast-replicator.h b/tolk/ast-replicator.h index d13eb392dc..70fa45a788 100644 --- a/tolk/ast-replicator.h +++ b/tolk/ast-replicator.h @@ -83,9 +83,6 @@ class ASTReplicator final { static V clone(V v) { return createV(v->range, clone(v->get_expr())); } - static V clone(V v) { - return createV(clone(v->get_wrapped_expr()), v->aux_data, v->inferred_type); - } static V clone(V v) { return createV(v->range, clone(v->get_items())); } @@ -200,6 +197,12 @@ class ASTReplicator final { static V clone(V v) { return createV(v->range, clone(v->get_body()), clone(v->get_cond())); } + static V clone(V v) { + return createV(v->range); + } + static V clone(V v) { + return createV(v->range); + } static V clone(V v) { return createV(v->range, clone(v->get_thrown_code()), clone(v->get_thrown_arg())); } @@ -253,6 +256,8 @@ class ASTReplicator final { case ast_repeat_statement: return clone(v->as()); case ast_while_statement: return clone(v->as()); case ast_do_while_statement: return clone(v->as()); + case ast_break_statement: return clone(v->as()); + case ast_continue_statement: return clone(v->as()); case ast_throw_statement: return clone(v->as()); case ast_assert_statement: return clone(v->as()); case ast_try_catch_statement: return clone(v->as()); @@ -281,7 +286,6 @@ class ASTReplicator final { case ast_empty_expression: return clone(v->as()); case ast_braced_expression: return clone(v->as()); case ast_braced_yield_result: return clone(v->as()); - case ast_artificial_aux_vertex: return clone(v->as()); case ast_tensor: return clone(v->as()); case ast_square_brackets: return clone(v->as()); case ast_reference: return clone(v->as()); @@ -402,7 +406,7 @@ class ASTReplicator final { DocCommentLines{}, FunctionData::EMPTY_TVM_METHOD_ID, FunctionData::flagIsLambda, - FunctionInlineMode::notCalculated + FunctionInlineMode::notAnnotated ); } }; diff --git a/tolk/ast-stringifier.h b/tolk/ast-stringifier.h index 334347c9b4..cdcf3fd164 100644 --- a/tolk/ast-stringifier.h +++ b/tolk/ast-stringifier.h @@ -47,7 +47,6 @@ class ASTStringifier final : public ASTVisitor { {ast_empty_expression, "ast_empty_expression"}, {ast_braced_expression, "ast_braced_expression"}, {ast_braced_yield_result, "ast_braced_yield_result"}, - {ast_artificial_aux_vertex, "ast_artificial_aux_vertex"}, {ast_tensor, "ast_tensor"}, {ast_square_brackets, "ast_square_brackets"}, {ast_reference, "ast_reference"}, @@ -86,6 +85,8 @@ class ASTStringifier final : public ASTVisitor { {ast_repeat_statement, "ast_repeat_statement"}, {ast_while_statement, "ast_while_statement"}, {ast_do_while_statement, "ast_do_while_statement"}, + {ast_break_statement, "ast_break_statement"}, + {ast_continue_statement, "ast_continue_statement"}, {ast_throw_statement, "ast_throw_statement"}, {ast_assert_statement, "ast_assert_statement"}, {ast_try_catch_statement, "ast_try_catch_statement"}, @@ -323,7 +324,6 @@ class ASTStringifier final : public ASTVisitor { case ast_empty_expression: return handle_vertex(v->as()); case ast_braced_expression: return handle_vertex(v->as()); case ast_braced_yield_result: return handle_vertex(v->as()); - case ast_artificial_aux_vertex: return handle_vertex(v->as()); case ast_tensor: return handle_vertex(v->as()); case ast_square_brackets: return handle_vertex(v->as()); case ast_reference: return handle_vertex(v->as()); @@ -362,6 +362,8 @@ class ASTStringifier final : public ASTVisitor { case ast_repeat_statement: return handle_vertex(v->as()); case ast_while_statement: return handle_vertex(v->as()); case ast_do_while_statement: return handle_vertex(v->as()); + case ast_break_statement: return handle_vertex(v->as()); + case ast_continue_statement: return handle_vertex(v->as()); case ast_throw_statement: return handle_vertex(v->as()); case ast_assert_statement: return handle_vertex(v->as()); case ast_try_catch_statement: return handle_vertex(v->as()); diff --git a/tolk/ast-visitor.h b/tolk/ast-visitor.h index 93e9bf38b4..ad04c5d221 100644 --- a/tolk/ast-visitor.h +++ b/tolk/ast-visitor.h @@ -106,7 +106,6 @@ class ASTVisitorFunctionBody : public ASTVisitor { virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } - virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } @@ -145,6 +144,8 @@ class ASTVisitorFunctionBody : public ASTVisitor { virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } + virtual void visit(V v) { return visit_children(v); } + virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } virtual void visit(V v) { return visit_children(v); } @@ -155,7 +156,6 @@ class ASTVisitorFunctionBody : public ASTVisitor { case ast_empty_expression: return visit(v->as()); case ast_braced_expression: return visit(v->as()); case ast_braced_yield_result: return visit(v->as()); - case ast_artificial_aux_vertex: return visit(v->as()); case ast_tensor: return visit(v->as()); case ast_square_brackets: return visit(v->as()); case ast_reference: return visit(v->as()); @@ -194,6 +194,8 @@ class ASTVisitorFunctionBody : public ASTVisitor { case ast_repeat_statement: return visit(v->as()); case ast_while_statement: return visit(v->as()); case ast_do_while_statement: return visit(v->as()); + case ast_break_statement: return visit(v->as()); + case ast_continue_statement: return visit(v->as()); case ast_throw_statement: return visit(v->as()); case ast_assert_statement: return visit(v->as()); case ast_try_catch_statement: return visit(v->as()); @@ -224,8 +226,7 @@ class ASTVisitorFunctionBody : public ASTVisitor { }; -const std::vector& get_all_builtin_functions(); -const std::vector& get_all_not_builtin_functions(); +const std::vector& get_all_functions(); const std::vector& get_all_declared_global_vars(); const std::vector& get_all_declared_constants(); const std::vector& get_all_declared_structs(); @@ -233,10 +234,8 @@ const std::vector& get_all_declared_enums(); template void visit_ast_of_all_functions(BodyVisitorT& visitor) { - const std::vector& all = get_all_not_builtin_functions(); - for (size_t i = 0; i < all.size(); ++i) { // NOLINT(*-loop-convert) - FunctionPtr fun_ref = all[i]; // not range-base loop to prevent iterator invalidation (push_back at generics) - if (visitor.should_visit_function(fun_ref)) { + for (FunctionPtr fun_ref : get_all_functions()) { + if (fun_ref->ast_root && visitor.should_visit_function(fun_ref)) { visitor.start_visiting_function(fun_ref, fun_ref->ast_root->as()); } } diff --git a/tolk/ast.cpp b/tolk/ast.cpp index 1ef3c7f9cb..aaca4dbcb4 100644 --- a/tolk/ast.cpp +++ b/tolk/ast.cpp @@ -161,6 +161,10 @@ void Vertex::assign_is_exhaustive(bool is_exhaustive) { this->is_exhaustive = is_exhaustive; } +void Vertex::assign_is_lazy_match() { + this->is_lazy_match = true; +} + void Vertex::assign_resolved_pattern(MatchArmKind pattern_kind, AnyExprV pattern_expr) { this->pattern_type_node = nullptr; this->pattern_kind = pattern_kind; @@ -199,14 +203,6 @@ void Vertex::assign_fun_ref(FunctionPtr fun_ref) { this->fun_ref = fun_ref; } -void Vertex::assign_first_unreachable(AnyV first_unreachable) { - this->first_unreachable = first_unreachable; -} - -void Vertex::assign_new_children(std::vector&& children) { - this->children = std::move(children); -} - void Vertex::assign_target(const DotTarget& target) { this->target = target; } diff --git a/tolk/ast.h b/tolk/ast.h index ce96672855..1f671404ca 100644 --- a/tolk/ast.h +++ b/tolk/ast.h @@ -77,7 +77,6 @@ enum ASTNodeKind { ast_empty_expression, ast_braced_expression, ast_braced_yield_result, - ast_artificial_aux_vertex, ast_tensor, ast_square_brackets, ast_reference, @@ -116,6 +115,8 @@ enum ASTNodeKind { ast_repeat_statement, ast_while_statement, ast_do_while_statement, + ast_break_statement, + ast_continue_statement, ast_throw_statement, ast_assert_statement, ast_try_catch_statement, @@ -162,10 +163,6 @@ enum class MatchArmKind { // for `match` expression, each of arms `pattern => else_branch, // `else => body` }; -struct ASTAuxData { // base class for data in ast_artificial_aux_vertex, see ast-aux-data.h - virtual ~ASTAuxData() = default; -}; - template struct Vertex; @@ -544,22 +541,6 @@ struct Vertex final : ASTExprUnary { : ASTExprUnary(ast_braced_yield_result, range, expr) {} }; -template<> -// ast_artificial_aux_vertex is a compiler-inserted vertex that can't occur in source code -// example: implicitly inserted loads after `lazy` operator -// example: `msg.isBounced` / `msg.xxx` in onInternalMessage are handled specially -struct Vertex final : ASTExprUnary { - const ASTAuxData* aux_data; // custom payload, see ast-aux-data.h - - AnyExprV get_wrapped_expr() const { return child; } - - Vertex(AnyExprV wrapped_expr, const ASTAuxData* aux_data, TypePtr inferred_type) - : ASTExprUnary(ast_artificial_aux_vertex, wrapped_expr->range, wrapped_expr) - , aux_data(aux_data) { - assign_inferred_type(inferred_type); - } -}; - template<> // ast_tensor is a set of expressions embraced by (parentheses) // in most languages, it's called "tuple", but in TVM, "tuple" is a TVM primitive, that's why "tensor" @@ -960,6 +941,7 @@ template<> // example: `match (var c = getIntOrSlice()) { int => return 0, slice => throw 123 }` struct Vertex final : ASTExprVararg { bool is_exhaustive = false; // if it has `else` or covers all cases without `else`; can be used as expression + bool is_lazy_match = false; // if it's `match (lazyVar)`, lowered as SDBEGINSQ without constructing union on a stack AnyExprV get_subject() const { return child(0); } int get_arms_count() const { return size() - 1; } @@ -971,6 +953,7 @@ struct Vertex final : ASTExprVararg { Vertex* mutate() const { return const_cast(this); } void assign_is_exhaustive(bool is_exhaustive); + void assign_is_lazy_match(); Vertex(SrcRange range, std::vector&& subject_and_arms) : ASTExprVararg(ast_match_expression, range, std::move(subject_and_arms)) {} @@ -1102,15 +1085,9 @@ template<> // example: function body is a block // example: do while body is a block struct Vertex final : ASTStatementVararg { - AnyV first_unreachable = nullptr; - const std::vector& get_items() const { return children; } AnyV get_item(int i) const { return children.at(i); } - Vertex* mutate() const { return const_cast(this); } - void assign_first_unreachable(AnyV first_unreachable); - void assign_new_children(std::vector&& children); - Vertex(SrcRange range, std::vector&& items) : ASTStatementVararg(ast_block_statement, range, std::move(items)) {} }; @@ -1182,6 +1159,20 @@ struct Vertex final : ASTStatementVararg { : ASTStatementVararg(ast_do_while_statement, range, {body, cond}) {} }; +template<> +// ast_break_statement is `break;` — exit the nearest enclosing loop +struct Vertex final : ASTStatementVararg { + explicit Vertex(SrcRange range) + : ASTStatementVararg(ast_break_statement, range, {}) {} +}; + +template<> +// ast_continue_statement is `continue;` — skip to the next iteration of the nearest enclosing loop +struct Vertex final : ASTStatementVararg { + explicit Vertex(SrcRange range) + : ASTStatementVararg(ast_continue_statement, range, {}) {} +}; + template<> // ast_throw_statement is throwing an exception, it accepts excNo and optional arg // examples: `throw 10` / `throw (ERR_LOW_BALANCE)` / `throw (1001, incomingAddr)` @@ -1318,13 +1309,13 @@ template<> // methods are still global functions, just accepting "self" first parameter // example: `fun f() { ... }` // functions can be generic, `fun f(params) { ... }` -// their body is either sequence (regular code function), or `asm`, or `builtin` +// their body is either sequence (regular code function), `asm`, `builtin`, or a get method prototype struct Vertex final : ASTOtherVararg { auto get_identifier() const { return children.at(0)->as(); } int get_num_params() const { return children.at(1)->as()->size(); } auto get_param_list() const { return children.at(1)->as(); } auto get_param(int i) const { return children.at(1)->as()->get_param(i); } - AnyV get_body() const { return children.at(2); } // ast_block_statement / ast_asm_body + AnyV get_body() const { return children.at(2); } // ast_block_statement / ast_asm_body / ast_empty_statement FunctionPtr fun_ref = nullptr; // filled after register AnyTypeV receiver_type_node; // for `fun builder.storeInt`, here is `builder` @@ -1337,7 +1328,8 @@ struct Vertex final : ASTOtherVararg { bool is_asm_function() const { return children.at(2)->kind == ast_asm_body; } bool is_code_function() const { return children.at(2)->kind == ast_block_statement; } - bool is_builtin_function() const { return children.at(2)->kind == ast_empty_statement; } + bool is_get_prototype() const { return children.at(2)->kind == ast_empty_statement && (flags & FunctionData::flagContractGetter) != 0; } + bool is_builtin_function() const { return children.at(2)->kind == ast_empty_statement && (flags & FunctionData::flagContractGetter) == 0; } Vertex* mutate() const { return const_cast(this); } void assign_fun_ref(FunctionPtr fun_ref); @@ -1507,7 +1499,7 @@ struct Vertex final : ASTOtherVararg { template<> // ast_tolk_required_version is a preamble fixating compiler's version at the top of the file // example: `tolk 0.6` -// when compiler version mismatches, it means, that another compiler was earlier for that sources, a warning is emitted +// when compiler version mismatches, it means that another compiler was used for these sources, so compilation fails struct Vertex final : ASTOtherLeaf { std::string semver; @@ -1544,6 +1536,8 @@ struct Vertex final : ASTOtherVararg { auto get_identifier() const { return children.at(0)->as(); } auto get_ith_item(int i) const { return children.at(i + 1)->as(); } + SrcRange keyword_range() const { return SrcRange::span(range, 8); } + Vertex(SrcRange range, std::vector&& name_and_items) : ASTOtherVararg(ast_contract_directive, range, std::move(name_and_items)) {} }; diff --git a/tolk/builtins.cpp b/tolk/builtins.cpp index 5c9ef2900a..f95d14849e 100644 --- a/tolk/builtins.cpp +++ b/tolk/builtins.cpp @@ -15,66 +15,51 @@ along with TON Blockchain Library. If not, see . */ #include "tolk.h" -#include "compilation-errors.h" #include "compiler-state.h" -#include "compiler-settings.h" #include "type-system.h" #include "generics-helpers.h" -#include "ast.h" namespace tolk { using namespace std::literals::string_literals; -// given func_type = `(slice, int) -> slice` and func flags, create SymLocalVarOrParameter -// param_names is optional: when provided, parameters get names (used for "big" built-in functions, so that -// their arguments appear in debug info / symbol types); when empty, parameters remain unnamed -static std::vector define_builtin_parameters(const std::vector& params_types, int func_flags, - const std::vector& param_names = {}) { - // `loadInt()`, `storeInt()`: they accept `self` and mutate it; no other options available in built-ins for now - bool is_mutate_self = func_flags & FunctionData::flagHasMutateParams; - std::vector parameters; - parameters.reserve(params_types.size()); - - for (int i = 0; i < static_cast(params_types.size()); ++i) { - std::string pname = (i < static_cast(param_names.size())) ? param_names[i] : ""; - LocalVarData p_sym(std::move(pname), {}, params_types[i], nullptr, (i == 0 && is_mutate_self) * LocalVarData::flagMutateParameter, i); - parameters.push_back(std::move(p_sym)); - } - - return parameters; +// The parsed stdlib declaration owns the signature, generics, parameter names, and source-level flags. +// The compiler only attaches an implementation and metadata that cannot be expressed in Tolk. +static void redefine_builtin_body_impl(const char* name, FunctionBody body, int compiler_flags) { + const Symbol* sym = lookup_global_symbol(name); + FunctionPtr fun_ref = sym ? sym->try_as() : nullptr; + tolk_assert(fun_ref && !fun_ref->is_code_function()); + fun_ref->mutate()->body = body; + fun_ref->mutate()->flags |= compiler_flags; } -static void define_builtin_func(const std::string& name, const std::vector& params_types, TypePtr return_type, const GenericsDeclaration* genericTs, const std::function& func, int flags) { - auto* f_sym = new FunctionData(name, {}, "", nullptr, return_type, define_builtin_parameters(params_types, flags), flags, FunctionInlineMode::notCalculated, genericTs, nullptr, {}, new FunctionBodyBuiltinAsmOp(func), nullptr); - G.symtable.add_function(f_sym); - G.all_builtins.push_back(f_sym); +static void redefine_builtin_body(const char* name, + const std::function& func, + int compiler_flags = 0) { + redefine_builtin_body_impl(name, new FunctionBodyBuiltinAsmOp(func), compiler_flags); } -static void define_builtin_func(const std::string& name, const std::vector& params_types, TypePtr return_type, const GenericsDeclaration* genericTs, const std::function& func, int flags, - const std::vector& param_names = {}) { - auto* f_sym = new FunctionData(name, {}, "", nullptr, return_type, define_builtin_parameters(params_types, flags, param_names), flags, FunctionInlineMode::notCalculated, genericTs, nullptr, {}, new FunctionBodyBuiltinGenerateOps(func), nullptr); - G.symtable.add_function(f_sym); - G.all_builtins.push_back(f_sym); +static void redefine_builtin_body(const char* name, + const std::function& func, + int compiler_flags = 0) { + redefine_builtin_body_impl(name, new FunctionBodyBuiltinGenerateOps(func), compiler_flags); } -static void define_builtin_method(const std::string& name, TypePtr receiver_type, const std::vector& params_types, TypePtr return_type, const GenericsDeclaration* genericTs, const std::function& func, int flags, - std::initializer_list arg_order = {}, std::initializer_list ret_order = {}) { - std::string method_name = name.substr(name.find('.') + 1); - auto* f_sym = new FunctionData(name, {}, std::move(method_name), receiver_type, return_type, define_builtin_parameters(params_types, flags), flags, FunctionInlineMode::notCalculated, genericTs, nullptr, {}, new FunctionBodyBuiltinAsmOp(func), nullptr); - f_sym->arg_order = arg_order; - f_sym->ret_order = ret_order; - G.symtable.add_function(f_sym); - G.all_builtins.push_back(f_sym); - G.all_methods.push_back(f_sym); -} +static void define_internal_builtin_func(const std::string& name, const std::vector& params_types, + TypePtr return_type, const GenericsDeclaration* genericTs, + const std::function& func, + int flags = FunctionData::flagRemovableIfUnused) { + std::vector parameters; + parameters.reserve(params_types.size()); + for (int i = 0; i < static_cast(params_types.size()); ++i) { + parameters.emplace_back("", nullptr, params_types[i], nullptr, 0, i); + } -void define_builtin_method(const std::string& name, TypePtr receiver_type, const std::vector& params_types, TypePtr return_type, const GenericsDeclaration* genericTs, const std::function& func, int flags, - const std::vector& param_names = {}) { - std::string method_name = name.substr(name.find('.') + 1); - auto* f_sym = new FunctionData(name, {}, std::move(method_name), receiver_type, return_type, define_builtin_parameters(params_types, flags, param_names), flags, FunctionInlineMode::notCalculated, genericTs, nullptr, {}, new FunctionBodyBuiltinGenerateOps(func), nullptr); + auto* body = new FunctionBodyBuiltinAsmOp(func); + auto* f_sym = new FunctionData(name, {}, "", nullptr, return_type, + std::move(parameters), flags, + FunctionInlineMode::notAnnotated, genericTs, nullptr, {}, body, nullptr); G.symtable.add_function(f_sym); - G.all_builtins.push_back(f_sym); - G.all_methods.push_back(f_sym); + G.all_functions.push_back(f_sym); } void FunctionBodyBuiltinAsmOp::compile(AsmOpList& dest, std::vector& out, std::vector& in, @@ -90,12 +75,6 @@ void FunctionBodyAsm::compile(AsmOpList& dest, AnyV origin) const { } } -// the option `-O2` (turned on by default) enables optimizations (particularly, peephole ones); -// if so, some asm instructions are generated to be transformed later (they are not valid to Fift as-is) -static bool will_run_peephole() { - return G_settings.optimization_level >= 2; -} - static std::string op_postfix_N_untuple(std::string cmd, int n_slots) { if (n_slots != 1) { cmd += " "; @@ -538,7 +517,7 @@ static AsmOp compile_logical_not(std::vector& res, std::vector `N THROWIFNOT`, but for `NOT` (generally) it's incorrect; // un-optimized `BOOLNOT` are later replaced with a regular `NOT` - return for_int_arg || !will_run_peephole() ? exec_op(origin, "0 EQINT", 1) : exec_op(origin, "BOOLNOT", 1); + return for_int_arg ? exec_op(origin, "0 EQINT", 1) : exec_op(origin, "BOOLNOT", 1); } static AsmOp compile_bitwise_and(std::vector& res, std::vector& args, AnyV origin) { @@ -837,8 +816,8 @@ static AsmOp compile_muldiv(std::vector& res, std::vector& a z.unused(); return push_const(origin, r.int_const); } - if (x.always_zero() || y.always_zero()) { - // dubious optimization for z=0... + // 0 * y / z = 0 only when z is known non-zero; otherwise preserve potential div-by-zero at runtime + if ((x.always_zero() || y.always_zero()) && z.always_nonzero()) { x.unused(); y.unused(); z.unused(); @@ -904,11 +883,6 @@ static AsmOp compile_muldiv(std::vector& res, std::vector& a return exec_op(origin, op, 3); } -// fun mulDivMod(x: int, y: int, z: int): (int, int) asm "MULDIVMOD"; -static AsmOp compile_muldivmod(std::vector&, std::vector&, AnyV origin) { - return AsmOp::Custom(origin, "MULDIVMOD", 3, 2); -} - static int compute_compare(td::RefInt256 x, td::RefInt256 y, int mode) { int s = td::cmp(x, y); if (mode == 7) { @@ -1001,7 +975,7 @@ static AsmOp compile_cmp_int(std::vector& res, std::vector& static AsmOp compile_throw(std::vector& res, std::vector& args, AnyV origin) { tolk_assert(res.empty() && args.size() == 1); VarDescr& x = args[0]; - if (x.is_int_const() && x.int_const >= 0 && x.int_const < (will_run_peephole() ? 65536 : 2048)) { + if (x.is_int_const() && x.int_const >= 0 && x.int_const < 65536) { // in Fift assembler, "N THROW" is valid if N < 2048; for big N (particularly, widely used 0xFFFF) // we now still generate "N THROW", and later, in optimizer, transform it to "PUSHINT" + "THROWANY" x.unused(); @@ -1115,7 +1089,7 @@ static AsmOp compile_fetch_int(std::vector& res, std::vector return exec_op(origin, (fetch ? "LD"s : "PLD"s) + (sgnd ? "IX" : "UX"), 2, 1 + (unsigned)fetch); } -// fun slice.__loadVarInt(mutate self, bits: int, unsigned: bool): int +// fun __loadVarInt(s: slice, bits: int, unsigned: bool): (int, slice) static AsmOp compile_fetch_varint(std::vector& res, std::vector& args, AnyV origin) { tolk_assert(args.size() == 3 && res.size() == 2); // it's a hidden function for auto-serialization (not exposed to stdlib), to bits/unsigned are not dynamic @@ -1143,7 +1117,7 @@ static AsmOp compile_store_int(std::vector& res, std::vector // purpose: to merge consecutive `b.storeUint(0, 1).storeUint(1, 1)` into one "1 PUSHINT + 2 STU", // when constant arguments are passed, keep them as a separate (fake) instruction, to be handled by optimizer later bool value_and_len_is_const = z.is_int_const() && x.is_int_const(); - if (value_and_len_is_const && x.int_const >= 0 && z.int_const > 0 && z.int_const <= 256 && will_run_peephole()) { + if (value_and_len_is_const && x.int_const >= 0 && z.int_const > 0 && z.int_const <= 256) { // don't handle negative numbers or potential overflow, merging them is incorrect int len = static_cast(z.int_const->to_long()); if (x.int_const->fits_bits(len, sgnd)) { @@ -1159,7 +1133,7 @@ static AsmOp compile_store_int(std::vector& res, std::vector return exec_op(origin, sgnd ? "STIX" : "STUX", 3, 1); } -// fun builder.__storeVarInt (mutate self, x: int, bits: int, unsigned: bool): self +// fun __storeVarInt(b: builder, x: int, bits: int, unsigned: bool): builder static AsmOp compile_store_varint(std::vector& res, std::vector& args, AnyV origin) { tolk_assert(args.size() == 4 && res.size() == 1); // it's a hidden function for auto-serialization (not exposed to stdlib), to bits/unsigned are not dynamic @@ -1184,11 +1158,11 @@ static AsmOp compile_store_bool(std::vector& res, std::vector& res, std::vector& res, std::vector& res, std::vector& args, AnyV origin) { tolk_assert(args.size() == 3 && res.size() == 2); @@ -1237,7 +1211,7 @@ static AsmOp compile_slice_sdbeginsq(std::vector& res, std::vectorto_long(), static_cast(prefix_len.int_const->to_long())); return AsmOp::Custom(origin, opcode.format_as_string(true) + " SDBEGINSQ", 0, 1); } - err("slice.tryStripPrefix can be used only with constant arguments").fire(origin); + err("__tryStripPrefix can be used only with constant arguments").fire(origin); } // fun slice.skipBits(mutate self, len: int): self "SDSKIPFIRST" @@ -1247,7 +1221,7 @@ static AsmOp compile_skip_bits_in_slice(std::vector& res, std::vector< // same technique as for storeUint: // consecutive `s.skipBits(8).skipBits(const_var_16)` will be joined into a single 24 // to track this, represent it as a separate fake instruction to be detected by optimizer later - if (len.is_int_const() && len.int_const >= 0 && len.int_const < 1024 && will_run_peephole()) { + if (len.is_int_const() && len.int_const >= 0 && len.int_const < 1024) { len.unused(); return AsmOp::Custom(origin, "MY_skip_bits " + len.int_const->to_dec_string(), 1); } @@ -1320,17 +1294,6 @@ static AsmOp compile_array_pop(std::vector& res, std::vector return exec_op(origin, op_postfix_N_untuple("TPOP", n_slots), 1, n_slots + 1); } -// fun debug.dumpStack(): void asm "DUMPSTK"; -static AsmOp compile_dumpstk(std::vector&, std::vector&, AnyV origin) { - return AsmOp::Custom(origin, "DUMPSTK", 0, 0); -} - -// fun debug.printString(x: string): void asm "STRDUMP"; -static AsmOp compile_strdump(std::vector&, std::vector&, AnyV origin) { - // a string (a parameter) is a TVM cell, which may be a snake string actually, it dumps the first chunk - return AsmOp::Custom(origin, "CTOS STRDUMP DROP", 1, 0); -} - // fun debug.print(x: T): void; static AsmOp compile_debug_print_to_string(std::vector&, std::vector& args, AnyV origin) { int n = static_cast(args.size()); @@ -1485,7 +1448,7 @@ CompileToAsmOpImpl compile_dict_addGet; CompileToAsmOpImpl compile_dict_del; CompileToAsmOpImpl compile_dict_delGet; -void define_builtins() { +void attach_builtins_implementation() { using namespace std::placeholders; TypePtr typeT = TypeDataGenericT::create("T"); @@ -1495,32 +1458,14 @@ void define_builtins() { TypePtr Slice = TypeDataSlice::create(); TypePtr String = TypeDataString::create(); TypePtr Builder = TypeDataBuilder::create(); - TypePtr Address = TypeDataAddress::internal(); - TypePtr ArrayOfT = TypeDataArray::create(typeT); TypePtr Never = TypeDataNever::create(); + TypePtr Unknown = TypeDataUnknown::create(); const GenericsDeclaration* declGenericT = new GenericsDeclaration(std::vector{{"T", nullptr}}, 0); - const GenericsDeclaration* declReceiverT = new GenericsDeclaration(std::vector{{"T", nullptr}}, 1); std::vector ParamsInt1 = {Int}; std::vector ParamsInt2 = {Int, Int}; std::vector ParamsInt3 = {Int, Int, Int}; - std::vector ParamsSliceInt = {Slice, Int}; - - // these types are defined in stdlib, currently unknown - // see patch_builtins_after_stdlib_loaded() below - TypePtr debug = TypeDataUnknown::create(); - TypePtr reflect = TypeDataUnknown::create(); - TypePtr CellT = TypeDataUnknown::create(); - TypePtr PackOptions = TypeDataUnknown::create(); - TypePtr UnpackOptions = TypeDataUnknown::create(); - TypePtr CreateMessageOptions = TypeDataUnknown::create(); - TypePtr CreateExternalLogMessageOptions = TypeDataUnknown::create(); - TypePtr OutMessage = TypeDataUnknown::create(); - TypePtr AddressShardingOptions = TypeDataUnknown::create(); - TypePtr AutoDeployAddress = TypeDataUnknown::create(); - TypePtr SourceLocation = TypeDataUnknown::create(); - const GenericsDeclaration* declTBody = new GenericsDeclaration(std::vector{{"TBody", nullptr}}, 0); // builtin operators // they are internally stored as functions, because at IR level, there is no difference @@ -1529,643 +1474,268 @@ void define_builtins() { // though it's a "hidden feature" and won't work well for overloads (`==` for int and bool, for example) // unary operators - define_builtin_func("-_", ParamsInt1, Int, nullptr, - compile_unary_minus, - FunctionData::flagMarkedAsPure); - define_builtin_func("+_", ParamsInt1, Int, nullptr, - compile_unary_plus, - FunctionData::flagMarkedAsPure); - define_builtin_func("!_", ParamsInt1, Bool, nullptr, - std::bind(compile_logical_not, _1, _2, _3, true), - FunctionData::flagMarkedAsPure); - define_builtin_func("!b_", {Bool}, Bool, nullptr, // "overloaded" separate version for bool - std::bind(compile_logical_not, _1, _2, _3, false), - FunctionData::flagMarkedAsPure); - define_builtin_func("~_", ParamsInt1, Int, nullptr, - compile_bitwise_not, - FunctionData::flagMarkedAsPure); + define_internal_builtin_func("-_", ParamsInt1, Int, nullptr, + compile_unary_minus); + define_internal_builtin_func("+_", ParamsInt1, Int, nullptr, + compile_unary_plus); + define_internal_builtin_func("!_", ParamsInt1, Bool, nullptr, + std::bind(compile_logical_not, _1, _2, _3, true)); + define_internal_builtin_func("!b_", {Bool}, Bool, nullptr, // "overloaded" separate version for bool + std::bind(compile_logical_not, _1, _2, _3, false)); + define_internal_builtin_func("~_", ParamsInt1, Int, nullptr, + compile_bitwise_not); // binary operators - define_builtin_func("_+_", ParamsInt2, Int, nullptr, - compile_add, - FunctionData::flagMarkedAsPure); - define_builtin_func("_-_", ParamsInt2, Int, nullptr, - compile_sub, - FunctionData::flagMarkedAsPure); - define_builtin_func("_*_", ParamsInt2, Int, nullptr, - compile_mul, - FunctionData::flagMarkedAsPure); - define_builtin_func("_/_", ParamsInt2, Int, nullptr, - std::bind(compile_div, _1, _2, _3, -1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_~/_", ParamsInt2, Int, nullptr, - std::bind(compile_div, _1, _2, _3, 0), - FunctionData::flagMarkedAsPure); - define_builtin_func("_^/_", ParamsInt2, Int, nullptr, - std::bind(compile_div, _1, _2, _3, 1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_%_", ParamsInt2, Int, nullptr, - std::bind(compile_mod, _1, _2, _3, -1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_<<_", ParamsInt2, Int, nullptr, - compile_lshift, - FunctionData::flagMarkedAsPure); - define_builtin_func("_>>_", ParamsInt2, Int, nullptr, - std::bind(compile_rshift, _1, _2, _3, -1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_~>>_", ParamsInt2, Int, nullptr, - std::bind(compile_rshift, _1, _2, _3, 0), - FunctionData::flagMarkedAsPure); - define_builtin_func("_^>>_", ParamsInt2, Int, nullptr, - std::bind(compile_rshift, _1, _2, _3, 1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_&_", ParamsInt2, Int, nullptr, // also works for bool - compile_bitwise_and, - FunctionData::flagMarkedAsPure); - define_builtin_func("_|_", ParamsInt2, Int, nullptr, // also works for bool - compile_bitwise_or, - FunctionData::flagMarkedAsPure); - define_builtin_func("_^_", ParamsInt2, Int, nullptr, // also works for bool - compile_bitwise_xor, - FunctionData::flagMarkedAsPure); - define_builtin_func("_==_", ParamsInt2, Int, nullptr, // also works for bool - std::bind(compile_cmp_int, _1, _2, _3, 2), - FunctionData::flagMarkedAsPure); - define_builtin_func("_!=_", ParamsInt2, Int, nullptr, // also works for bool - std::bind(compile_cmp_int, _1, _2, _3, 5), - FunctionData::flagMarkedAsPure); - define_builtin_func("_<_", ParamsInt2, Int, nullptr, - std::bind(compile_cmp_int, _1, _2, _3, 4), - FunctionData::flagMarkedAsPure); - define_builtin_func("_>_", ParamsInt2, Int, nullptr, - std::bind(compile_cmp_int, _1, _2, _3, 1), - FunctionData::flagMarkedAsPure); - define_builtin_func("_<=_", ParamsInt2, Int, nullptr, - std::bind(compile_cmp_int, _1, _2, _3, 6), - FunctionData::flagMarkedAsPure); - define_builtin_func("_>=_", ParamsInt2, Int, nullptr, - std::bind(compile_cmp_int, _1, _2, _3, 3), - FunctionData::flagMarkedAsPure); - define_builtin_func("_<=>_", ParamsInt2, Int, nullptr, - std::bind(compile_cmp_int, _1, _2, _3, 7), - FunctionData::flagMarkedAsPure); + define_internal_builtin_func("_+_", ParamsInt2, Int, nullptr, + compile_add); + define_internal_builtin_func("_-_", ParamsInt2, Int, nullptr, + compile_sub); + define_internal_builtin_func("_*_", ParamsInt2, Int, nullptr, + compile_mul); + define_internal_builtin_func("_/_", ParamsInt2, Int, nullptr, + std::bind(compile_div, _1, _2, _3, -1)); + define_internal_builtin_func("_~/_", ParamsInt2, Int, nullptr, + std::bind(compile_div, _1, _2, _3, 0)); + define_internal_builtin_func("_^/_", ParamsInt2, Int, nullptr, + std::bind(compile_div, _1, _2, _3, 1)); + define_internal_builtin_func("_%_", ParamsInt2, Int, nullptr, + std::bind(compile_mod, _1, _2, _3, -1)); + define_internal_builtin_func("_<<_", ParamsInt2, Int, nullptr, + compile_lshift); + define_internal_builtin_func("_>>_", ParamsInt2, Int, nullptr, + std::bind(compile_rshift, _1, _2, _3, -1)); + define_internal_builtin_func("_~>>_", ParamsInt2, Int, nullptr, + std::bind(compile_rshift, _1, _2, _3, 0)); + define_internal_builtin_func("_^>>_", ParamsInt2, Int, nullptr, + std::bind(compile_rshift, _1, _2, _3, 1)); + define_internal_builtin_func("_&_", ParamsInt2, Int, nullptr, // also works for bool + compile_bitwise_and); + define_internal_builtin_func("_|_", ParamsInt2, Int, nullptr, // also works for bool + compile_bitwise_or); + define_internal_builtin_func("_^_", ParamsInt2, Int, nullptr, // also works for bool + compile_bitwise_xor); + define_internal_builtin_func("_==_", ParamsInt2, Int, nullptr, // also works for bool + std::bind(compile_cmp_int, _1, _2, _3, 2)); + define_internal_builtin_func("_!=_", ParamsInt2, Int, nullptr, // also works for bool + std::bind(compile_cmp_int, _1, _2, _3, 5)); + define_internal_builtin_func("_<_", ParamsInt2, Int, nullptr, + std::bind(compile_cmp_int, _1, _2, _3, 4)); + define_internal_builtin_func("_>_", ParamsInt2, Int, nullptr, + std::bind(compile_cmp_int, _1, _2, _3, 1)); + define_internal_builtin_func("_<=_", ParamsInt2, Int, nullptr, + std::bind(compile_cmp_int, _1, _2, _3, 6)); + define_internal_builtin_func("_>=_", ParamsInt2, Int, nullptr, + std::bind(compile_cmp_int, _1, _2, _3, 3)); + define_internal_builtin_func("_<=>_", ParamsInt2, Int, nullptr, + std::bind(compile_cmp_int, _1, _2, _3, 7)); // special function used for internal compilation of some lexical constructs // for example, `throw 123;` is actually calling `__throw(123)` - define_builtin_func("__true", {}, Bool, nullptr, /* AsmOp::Const("TRUE") */ - std::bind(compile_bool_const, _1, _2, _3, true), - FunctionData::flagMarkedAsPure); - define_builtin_func("__false", {}, Bool, nullptr, /* AsmOp::Const("FALSE") */ - std::bind(compile_bool_const, _1, _2, _3, false), - FunctionData::flagMarkedAsPure); - define_builtin_func("__null", {}, typeT, declGenericT, - compile_push_null, - FunctionData::flagMarkedAsPure); - define_builtin_func("__isNull", {typeT}, Bool, declGenericT, - compile_is_null, - FunctionData::flagMarkedAsPure); - define_builtin_func("__throw", ParamsInt1, Never, nullptr, + define_internal_builtin_func("__true", {}, Bool, nullptr, /* AsmOp::Const("TRUE") */ + std::bind(compile_bool_const, _1, _2, _3, true)); + define_internal_builtin_func("__false", {}, Bool, nullptr, /* AsmOp::Const("FALSE") */ + std::bind(compile_bool_const, _1, _2, _3, false)); + define_internal_builtin_func("__null", {}, Unknown, nullptr, + compile_push_null); + define_internal_builtin_func("__isNull", {Unknown}, Bool, nullptr, + compile_is_null); + define_internal_builtin_func("__throw", ParamsInt1, Never, nullptr, compile_throw, 0); - define_builtin_func("__throw_arg", {TypeDataUnknown::create(), Int}, Never, nullptr, + define_internal_builtin_func("__throw_arg", {Unknown, Int}, Never, nullptr, compile_throw_arg, 0); - define_builtin_func("__throw_if", ParamsInt2, Unit, nullptr, + define_internal_builtin_func("__throw_if", ParamsInt2, Unit, nullptr, std::bind(compile_throw_if_ifnot, _1, _2, _3, false), 0); - define_builtin_func("__throw_ifnot", ParamsInt2, Unit, nullptr, + define_internal_builtin_func("__throw_ifnot", ParamsInt2, Unit, nullptr, std::bind(compile_throw_if_ifnot, _1, _2, _3, true), 0); - define_builtin_func("__InMessage.originalForwardFee", ParamsInt2, Int, nullptr, - compile_calc_InMessage_originalForwardFee, - FunctionData::flagMarkedAsPure); - define_builtin_func("__InMessage.getInMsgParam", ParamsInt1, Int, nullptr, - compile_calc_InMessage_getInMsgParam, - FunctionData::flagMarkedAsPure); - define_builtin_method("builder.__storeVarInt", Builder, {Builder, Int, Int, Bool}, Unit, nullptr, - compile_store_varint, // not exposed to stdlib, used in auto-serialization - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf); - define_builtin_method("slice.__loadVarInt", Slice, {Slice, Int, Bool}, Int, nullptr, - compile_fetch_varint, // not exposed to stdlib, used in auto-serialization - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf, - {}, {1, 0}); - define_builtin_func("__condsel", ParamsInt3, Int, nullptr, - compile_ternary_as_condsel, - FunctionData::flagMarkedAsPure); + define_internal_builtin_func("__InMessage.originalForwardFee", ParamsInt2, Int, nullptr, + compile_calc_InMessage_originalForwardFee); + define_internal_builtin_func("__InMessage.getInMsgParam", ParamsInt1, Int, nullptr, + compile_calc_InMessage_getInMsgParam); + define_internal_builtin_func("__storeVarInt", {Builder, Int, Int, Bool}, Builder, nullptr, + compile_store_varint); // not exposed to stdlib, used in auto-serialization + define_internal_builtin_func("__loadVarInt", {Slice, Int, Bool}, TypeDataTensor::create({Int, Slice}), nullptr, + compile_fetch_varint); // not exposed to stdlib, used in auto-serialization + define_internal_builtin_func("__condsel", ParamsInt3, Int, nullptr, + compile_ternary_as_condsel); + define_internal_builtin_func("__tryStripPrefix", {Slice, Int, Int}, TypeDataTensor::create({Slice, Bool}), nullptr, + compile_slice_sdbeginsq); + + // functions not presented in stdlib at all + // used in tolk-tester to check/expose internal compiler state + // each of them is handled in a special way, search by its name + define_internal_builtin_func("__expect_type", {typeT, String}, Unit, declGenericT, + compile_expect_type); + define_internal_builtin_func("__expect_inline", {Bool}, Unit, nullptr, + compile_expect_type); + define_internal_builtin_func("__expect_lazy", {String}, Unit, nullptr, + compile_expect_type); + + // low-level functions that actually emit TVM assembly, they work on a "dict" level + TypePtr PlainDict = TypeDataCell::create(); + TypePtr KeySliceOrInt = TypeDataUnknown::create(); + TypePtr ValueSlice = TypeDataSlice::create(); + TypePtr ValueFound = TypeDataInt::create(); + TypePtr LookupSliceFound = TypeDataTensor::create({TypeDataSlice::create(), TypeDataInt::create()}); + define_internal_builtin_func("__dict.get", {KeySliceOrInt, PlainDict, TypeDataInt::create()}, LookupSliceFound, + nullptr, compile_dict_get); + define_internal_builtin_func("__dict.getMin", {PlainDict}, + TypeDataTensor::create({ValueSlice, KeySliceOrInt, ValueFound}), nullptr, + compile_dict_getMin); + define_internal_builtin_func("__dict.getMax", {PlainDict}, + TypeDataTensor::create({ValueSlice, KeySliceOrInt, ValueFound}), nullptr, + compile_dict_getMax); + define_internal_builtin_func("__dict.getNext", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, + compile_dict_getNext); + define_internal_builtin_func("__dict.getNextEq", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, + compile_dict_getNextEq); + define_internal_builtin_func("__dict.getPrev", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, + compile_dict_getPrev); + define_internal_builtin_func("__dict.getPrevEq", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, + compile_dict_getPrevEq); + define_internal_builtin_func("__dict.set", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + PlainDict, nullptr, compile_dict_set); + define_internal_builtin_func("__dict.setGet", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, compile_dict_setGet); + define_internal_builtin_func("__dict.replace", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, compile_dict_replace); + define_internal_builtin_func("__dict.replaceGet", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, compile_dict_replaceGet); + define_internal_builtin_func("__dict.add", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, compile_dict_add); + define_internal_builtin_func("__dict.addGet", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, compile_dict_addGet); + define_internal_builtin_func("__dict.del", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, compile_dict_del); + define_internal_builtin_func("__dict.delGet", + {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, + TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, compile_dict_delGet); // compile-time only functions, evaluated essentially at compile-time, no runtime implementation // they are placed in stdlib and marked as `builtin` // note their parameter being `unknown`: in order to `grams(1)` pass type inferring but fire a more gentle error later - define_builtin_func("grams", {TypeDataUnknown::create()}, TypeDataCoins::create(), nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("ton", {TypeDataUnknown::create()}, TypeDataCoins::create(), nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringCrc32", {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringCrc16", {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringSha256", {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringSha256_32", {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringToBase256", {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("stringHexToSlice", {String}, Slice, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_func("address", {String}, TypeDataAddress::internal(), nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); + redefine_builtin_body("grams", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("ton", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringCrc32", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringCrc16", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringSha256", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringSha256_32", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringToBase256", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("stringHexToSlice", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("address", compile_time_only_function, FunctionData::flagCompileTimeVal); // string compile-time methods: "hello".crc32(), "hello".sha256(), etc. - define_builtin_method("string.crc32", String, {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.crc16", String, {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.sha256", String, {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.sha256_32", String, {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.hexToSlice", String, {String}, Slice, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.toBase256", String, {String}, Int, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); - define_builtin_method("string.literalSlice", String, {String}, Slice, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAcceptsSelf); + redefine_builtin_body("string.crc32", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.crc16", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.sha256", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.sha256_32", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.hexToSlice", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.toBase256", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("string.literalSlice", compile_time_only_function, FunctionData::flagCompileTimeVal); // array — a TVM tuple under the hood // implemented as built-in functions to support variable-width T (not 1-slot values are backed by sub-tuples) - define_builtin_method("array.get", ArrayOfT, {ArrayOfT, Int}, typeT, declReceiverT, - compile_array_get, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.set", ArrayOfT, {ArrayOfT, typeT, Int}, Unit, declReceiverT, - compile_array_set_at, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.push", ArrayOfT, {ArrayOfT, typeT}, Unit, declReceiverT, - compile_array_push, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.size", ArrayOfT, {ArrayOfT}, Int, declReceiverT, - compile_array_size, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.last", ArrayOfT, {ArrayOfT}, typeT, declReceiverT, - compile_array_last, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.first", ArrayOfT, {ArrayOfT}, typeT, declReceiverT, - compile_array_first, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("array.pop", ArrayOfT, {ArrayOfT}, typeT, declReceiverT, - compile_array_pop, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - - // functions from stdlib marked as `builtin`, implemented at compiler level for optimizations + redefine_builtin_body("array.get", compile_array_get); + redefine_builtin_body("array.set", compile_array_set_at); + redefine_builtin_body("array.push", compile_array_push); + redefine_builtin_body("array.size", compile_array_size); + redefine_builtin_body("array.last", compile_array_last); + redefine_builtin_body("array.first", compile_array_first); + redefine_builtin_body("array.pop", compile_array_pop); + + // functions from stdlib with illustrative asm bodies, implemented at compiler level for optimizations // (for example, `loadInt(1)` is `1 LDI`, but `loadInt(n)` for non-constant requires it be on a stack and `LDIX`) - define_builtin_func("mulDivFloor", ParamsInt3, Int, nullptr, - std::bind(compile_muldiv, _1, _2, _3, -1), - FunctionData::flagMarkedAsPure); - define_builtin_func("mulDivRound", ParamsInt3, Int, nullptr, - std::bind(compile_muldiv, _1, _2, _3, 0), - FunctionData::flagMarkedAsPure); - define_builtin_func("mulDivCeil", ParamsInt3, Int, nullptr, - std::bind(compile_muldiv, _1, _2, _3, 1), - FunctionData::flagMarkedAsPure); - define_builtin_func("mulDivMod", ParamsInt3, TypeDataTensor::create({Int, Int}), nullptr, - compile_muldivmod, - FunctionData::flagMarkedAsPure); - define_builtin_method("slice.loadInt", Slice, ParamsSliceInt, Int, nullptr, - std::bind(compile_fetch_int, _1, _2, _3, true, true), - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf, - {}, {1, 0}); - define_builtin_method("slice.loadUint", Slice, ParamsSliceInt, Int, nullptr, - std::bind(compile_fetch_int, _1, _2, _3, true, false), - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf, - {}, {1, 0}); - define_builtin_method("slice.loadBits", Slice, ParamsSliceInt, Slice, nullptr, - std::bind(compile_fetch_slice, _1, _2, _3, true), - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf, - {}, {1, 0}); - define_builtin_method("slice.skipBits", Slice, ParamsSliceInt, Slice, nullptr, - compile_skip_bits_in_slice, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf); - define_builtin_method("slice.preloadInt", Slice, ParamsSliceInt, Int, nullptr, - std::bind(compile_fetch_int, _1, _2, _3, false, true), - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf); - define_builtin_method("slice.preloadUint", Slice, ParamsSliceInt, Int, nullptr, - std::bind(compile_fetch_int, _1, _2, _3, false, false), - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf); - define_builtin_method("slice.preloadBits", Slice, ParamsSliceInt, Slice, nullptr, - std::bind(compile_fetch_slice, _1, _2, _3, false), - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf); - define_builtin_method("slice.tryStripPrefix", Slice, {Slice, Int, Int}, Bool, nullptr, - compile_slice_sdbeginsq, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf); - define_builtin_method("builder.storeInt", Builder, {Builder, Int, Int}, Unit, nullptr, - std::bind(compile_store_int, _1, _2, _3, true), - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf, - {1, 0, 2}, {}); - define_builtin_method("builder.storeUint", Builder, {Builder, Int, Int}, Unit, nullptr, - std::bind(compile_store_int, _1, _2, _3, false), - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf, - {1, 0, 2}, {}); - define_builtin_method("builder.storeBool", Builder, {Builder, Bool}, Unit, nullptr, - compile_store_bool, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf, - {1, 0}, {}); - define_builtin_method("builder.storeCoins", Builder, {Builder, TypeDataCoins::create()}, Unit, nullptr, - compile_store_coins, - FunctionData::flagMarkedAsPure | FunctionData::flagHasMutateParams | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf); - define_builtin_method("address.buildSameAddressInAnotherShard", Address, {Address, AddressShardingOptions}, Builder, nullptr, - generate_address_buildInAnotherShard, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf, - {"self", "options"}); - define_builtin_method("address.calculateSameAddressInAnotherShard", Address, {Address, AddressShardingOptions}, Address, nullptr, - generate_address_calculateInAnotherShard, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf, - {"self", "options"}); - define_builtin_method("debug.print", debug, {typeT}, Unit, declGenericT, - compile_debug_print_to_string, - FunctionData::flagAllowAnyWidthT); - define_builtin_method("debug.printString", debug, {String}, Unit, nullptr, - compile_strdump, - 0); - define_builtin_method("debug.dumpStack", debug, {}, Unit, nullptr, - compile_dumpstk, - 0); + redefine_builtin_body("mulDivFloor", std::bind(compile_muldiv, _1, _2, _3, -1)); + redefine_builtin_body("mulDivRound", std::bind(compile_muldiv, _1, _2, _3, 0)); + redefine_builtin_body("mulDivCeil", std::bind(compile_muldiv, _1, _2, _3, 1)); + redefine_builtin_body("slice.loadInt", std::bind(compile_fetch_int, _1, _2, _3, true, true)); + redefine_builtin_body("slice.loadUint", std::bind(compile_fetch_int, _1, _2, _3, true, false)); + redefine_builtin_body("slice.loadBits", std::bind(compile_fetch_slice, _1, _2, _3, true)); + redefine_builtin_body("slice.skipBits", compile_skip_bits_in_slice); + redefine_builtin_body("slice.preloadInt", std::bind(compile_fetch_int, _1, _2, _3, false, true)); + redefine_builtin_body("slice.preloadUint", std::bind(compile_fetch_int, _1, _2, _3, false, false)); + redefine_builtin_body("slice.preloadBits", std::bind(compile_fetch_slice, _1, _2, _3, false)); + redefine_builtin_body("builder.storeInt", std::bind(compile_store_int, _1, _2, _3, true)); + redefine_builtin_body("builder.storeUint", std::bind(compile_store_int, _1, _2, _3, false)); + redefine_builtin_body("builder.storeBool", compile_store_bool); + redefine_builtin_body("builder.storeCoins", compile_store_coins); + redefine_builtin_body("address.buildSameAddressInAnotherShard", generate_address_buildInAnotherShard); + redefine_builtin_body("address.calculateSameAddressInAnotherShard", generate_address_calculateInAnotherShard); + redefine_builtin_body("debug.print", compile_debug_print_to_string); // reflect — compile-time type introspection; // a couple of its methods are "consteval" and can be used in constants / fields defaults / etc. - define_builtin_method("reflect.typeNameOf", reflect, {}, String, declGenericT, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.typeNameOfObject", reflect, {typeT}, String, declGenericT, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.typeUniqueIdxOf", reflect, {}, Int, declGenericT, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.typeUniqueIdxOfObject", reflect, {typeT}, Int, declGenericT, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.stackSizeOf", reflect, {}, Int, declGenericT, - generate_reflect_stackSizeOf, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.stackSizeOfObject", reflect, {typeT}, Int, declGenericT, - compile_reflect_stackSizeOfObject, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.serializationPrefixOf", reflect, {}, TypeDataTensor::create({Int, Int}), declGenericT, - generate_reflect_serializationPrefixOf, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.estimateSerializationOf", typeT, {}, TypeDataTensor::create({Int, Int, Int, Int}), declGenericT, - generate_reflect_estimateSerializationOf, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_method("reflect.sourceLocation", reflect, {}, SourceLocation, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); - define_builtin_method("reflect.sourceLocationAsString", reflect, {}, String, nullptr, - compile_time_only_function, - FunctionData::flagMarkedAsPure | FunctionData::flagCompileTimeVal); + if (lookup_global_symbol("reflect")) { + redefine_builtin_body("reflect.typeNameOf", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("reflect.typeNameOfObject", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("reflect.typeUniqueIdxOf", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("reflect.typeUniqueIdxOfObject", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("reflect.stackSizeOf", generate_reflect_stackSizeOf); + redefine_builtin_body("reflect.stackSizeOfObject", compile_reflect_stackSizeOfObject); + redefine_builtin_body("reflect.serializationPrefixOf", generate_reflect_serializationPrefixOf); + redefine_builtin_body("reflect.estimateSerializationOf", generate_reflect_estimateSerializationOf); + redefine_builtin_body("reflect.sourceLocation", compile_time_only_function, FunctionData::flagCompileTimeVal); + redefine_builtin_body("reflect.sourceLocationAsString", compile_time_only_function, FunctionData::flagCompileTimeVal); + } // serialization/deserialization methods to/from cells (or, more low-level, slices/builders) // they work with structs (or, more low-level, with arbitrary types) - define_builtin_method("T.toCell", typeT, {typeT, PackOptions}, CellT, declReceiverT, - generate_T_toCell, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "options"}); - define_builtin_method("T.fromCell", typeT, {TypeDataCell::create(), UnpackOptions}, typeT, declReceiverT, - generate_T_fromCell, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT, - {"packedCell", "options"}); - define_builtin_method("T.fromSlice", typeT, {Slice, UnpackOptions}, typeT, declReceiverT, - generate_T_fromSlice, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT, - {"rawSlice", "options"}); - define_builtin_method("T.forceLoadLazyObject", typeT, {typeT}, Slice, declReceiverT, - generate_T_forceLoadLazyObject, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self"}); - define_builtin_method("Cell.load", CellT, {CellT, UnpackOptions}, typeT, declReceiverT, - generate_T_fromCell, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "options"}); - define_builtin_method("slice.loadAny", Slice, {Slice, UnpackOptions}, typeT, declGenericT, - generate_slice_loadAny, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "options"}); - define_builtin_method("slice.skipAny", Slice, {Slice, UnpackOptions}, Slice, declGenericT, - generate_slice_skipAny, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "options"}); - define_builtin_method("builder.storeAny", Builder, {Builder, typeT, PackOptions}, Builder, declGenericT, - generate_builder_storeAny, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagReturnsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "v", "options"}); - define_builtin_method("T.toTuple", typeT, {typeT}, TypeDataArray::create(TypeDataUnknown::create()), declReceiverT, - compile_T_to_tuple, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT); - define_builtin_method("T.fromTuple", typeT, {TypeDataArray::create(TypeDataUnknown::create())}, typeT, declReceiverT, - compile_T_from_tuple, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - - define_builtin_func("createMessage", {CreateMessageOptions}, OutMessage, declTBody, - generate_createMessage, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT, - {"options"}); - define_builtin_func("createExternalLogMessage", {CreateExternalLogMessageOptions}, OutMessage, declTBody, - generate_createExternalLogMessage, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT, - {"options"}); - define_builtin_method("AutoDeployAddress.buildAddress", AutoDeployAddress, {AutoDeployAddress}, Builder, nullptr, - generate_AutoDeployAddress_buildAddress, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf, - {"self"}); - define_builtin_method("AutoDeployAddress.calculateAddress", AutoDeployAddress, {AutoDeployAddress}, Address, nullptr, - generate_AutoDeployAddress_calculateAddress, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf, - {"self"}); - define_builtin_method("AutoDeployAddress.addressMatches", AutoDeployAddress, {AutoDeployAddress, Address}, Bool, nullptr, - generate_AutoDeployAddress_addressMatches, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf, - {"self", "addr"}); - - // functions not presented in stdlib at all - // used in tolk-tester to check/expose internal compiler state - // each of them is handled in a special way, search by its name - define_builtin_func("__expect_type", {typeT, String}, Unit, declGenericT, - compile_expect_type, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_func("__expect_inline", {Bool}, Unit, nullptr, - compile_expect_type, - FunctionData::flagMarkedAsPure); - define_builtin_func("__expect_lazy", {String}, Unit, nullptr, - compile_expect_type, - FunctionData::flagMarkedAsPure); - - TypePtr MapKV = TypeDataMapKV::create(TypeDataGenericT::create("K"), TypeDataGenericT::create("V")); - TypePtr TKey = TypeDataGenericT::create("K"); - TypePtr TValue = TypeDataGenericT::create("V"); - TypePtr LookupResultT = TypeDataUnknown::create(); - TypePtr EntryKV = TypeDataUnknown::create(); - const GenericsDeclaration* declGenericMapKV = new GenericsDeclaration(std::vector{{"K", nullptr}, {"V", nullptr}}, 0); - const GenericsDeclaration* declReceiverMapKV = new GenericsDeclaration(std::vector{{"K", nullptr}, {"V", nullptr}}, 2); + redefine_builtin_body("T.toCell", generate_T_toCell); + redefine_builtin_body("T.fromCell", generate_T_fromCell); + redefine_builtin_body("T.fromSlice", generate_T_fromSlice); + redefine_builtin_body("T.forceLoadLazyObject", generate_T_forceLoadLazyObject); + redefine_builtin_body("Cell.load", generate_T_fromCell); + redefine_builtin_body("slice.loadAny", generate_slice_loadAny); + redefine_builtin_body("slice.skipAny", generate_slice_skipAny); + redefine_builtin_body("builder.storeAny", generate_builder_storeAny); + redefine_builtin_body("T.toTuple", compile_T_to_tuple); + redefine_builtin_body("T.fromTuple", compile_T_from_tuple); + + redefine_builtin_body("createMessage", generate_createMessage); + redefine_builtin_body("createExternalLogMessage", generate_createExternalLogMessage); + redefine_builtin_body("AutoDeployAddress.buildAddress", generate_AutoDeployAddress_buildAddress); + redefine_builtin_body("AutoDeployAddress.calculateAddress", generate_AutoDeployAddress_calculateAddress); + redefine_builtin_body("AutoDeployAddress.addressMatches", generate_AutoDeployAddress_addressMatches); // high-level methods for maps; // they are generic, so all type checks are done automatically; // but all calls to them are handled at generating Ops from AST, their "simple compile" is not called - define_builtin_func("createEmptyMap", {}, MapKV, declGenericMapKV, - compile_createEmptyMap, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_func("createMapFromLowLevelDict", {TypeDataUnion::create_nullable(TypeDataCell::create())}, MapKV, declGenericMapKV, - compile_createMapFromLowLevelDict, - FunctionData::flagMarkedAsPure | FunctionData::flagAllowAnyWidthT); - define_builtin_method("map.exists", MapKV, {MapKV, TKey}, TypeDataBool::create(), declReceiverMapKV, - generate_mapKV_exists, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "key"}); - define_builtin_method("map.get", MapKV, {MapKV, TKey}, LookupResultT, declReceiverMapKV, - generate_mapKV_get, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "key"}); - define_builtin_method("map.mustGet", MapKV, {MapKV, TKey, TypeDataInt::create()}, TValue, declReceiverMapKV, - generate_mapKV_mustGet, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "key", "throwIfNotFound"}); - define_builtin_method("map.set", MapKV, {MapKV, TKey, TValue}, TypeDataVoid::create(), declReceiverMapKV, - generate_mapKV_set, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT | FunctionData::flagReturnsSelf, - {"self", "key", "value"}); - define_builtin_method("map.setAndGetPrevious", MapKV, {MapKV, TKey, TValue}, LookupResultT, declReceiverMapKV, - generate_mapKV_setGet, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key", "value"}); - define_builtin_method("map.replaceIfExists", MapKV, {MapKV, TKey, TValue}, TypeDataBool::create(), declReceiverMapKV, - generate_mapKV_replace, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key", "value"}); - define_builtin_method("map.replaceAndGetPrevious", MapKV, {MapKV, TKey, TValue}, LookupResultT, declReceiverMapKV, - generate_mapKV_replaceGet, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key", "value"}); - define_builtin_method("map.addIfNotExists", MapKV, {MapKV, TKey, TValue}, TypeDataBool::create(), declReceiverMapKV, - generate_mapKV_add, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key", "value"}); - define_builtin_method("map.addOrGetExisting", MapKV, {MapKV, TKey, TValue}, LookupResultT, declReceiverMapKV, - generate_mapKV_addGet, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key", "value"}); - define_builtin_method("map.delete", MapKV, {MapKV, TKey}, TypeDataBool::create(), declReceiverMapKV, - generate_mapKV_del, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key"}); - define_builtin_method("map.deleteAndGetDeleted", MapKV, {MapKV, TKey}, LookupResultT, declReceiverMapKV, - generate_mapKV_delGet, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagHasMutateParams | FunctionData::flagAllowAnyWidthT, - {"self", "key"}); - define_builtin_method("map.findFirst", MapKV, {MapKV}, EntryKV, declReceiverMapKV, - generate_mapKV_findFirst, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self"}); - define_builtin_method("map.findLast", MapKV, {MapKV}, EntryKV, declReceiverMapKV, - generate_mapKV_findLast, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self"}); - define_builtin_method("map.findKeyGreater", MapKV, {MapKV, TKey}, EntryKV, declReceiverMapKV, - generate_mapKV_findKeyGreater, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "pivotKey"}); - define_builtin_method("map.findKeyGreaterOrEqual", MapKV, {MapKV, TKey}, EntryKV, declReceiverMapKV, - generate_mapKV_findKeyGreaterOrEqual, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "pivotKey"}); - define_builtin_method("map.findKeyLess", MapKV, {MapKV, TKey}, EntryKV, declReceiverMapKV, - generate_mapKV_findKeyLess, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "pivotKey"}); - define_builtin_method("map.findKeyLessOrEqual", MapKV, {MapKV, TKey}, EntryKV, declReceiverMapKV, - generate_mapKV_findKeyLessOrEqual, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "pivotKey"}); - define_builtin_method("map.iterateNext", MapKV, {MapKV, EntryKV}, EntryKV, declReceiverMapKV, - generate_mapKV_iterateNext, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "current"}); - define_builtin_method("map.iteratePrev", MapKV, {MapKV, EntryKV}, EntryKV, declReceiverMapKV, - generate_mapKV_iteratePrev, - FunctionData::flagMarkedAsPure | FunctionData::flagAcceptsSelf | FunctionData::flagAllowAnyWidthT, - {"self", "current"}); - - // low-level functions that actually emit TVM assembly, they work on a "dict" level - TypePtr PlainDict = TypeDataCell::create(); - TypePtr KeySliceOrInt = TypeDataUnknown::create(); - TypePtr ValueSlice = TypeDataSlice::create(); - TypePtr ValueFound = TypeDataInt::create(); - TypePtr LookupSliceFound = TypeDataTensor::create({TypeDataSlice::create(), TypeDataInt::create()}); - - define_builtin_func("__dict.get", {KeySliceOrInt, PlainDict, TypeDataInt::create()}, LookupSliceFound, nullptr, - compile_dict_get, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getMin", {PlainDict}, TypeDataTensor::create({ValueSlice, KeySliceOrInt, ValueFound}), nullptr, - compile_dict_getMin, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getMax", {PlainDict}, TypeDataTensor::create({ValueSlice, KeySliceOrInt, ValueFound}), nullptr, - compile_dict_getMax, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getNext", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_getNext, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getNextEq", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_getNextEq, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getPrev", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_getPrev, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.getPrevEq", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_getPrevEq, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.set", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, PlainDict, nullptr, - compile_dict_set, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.setGet", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, - compile_dict_setGet, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.replace", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_replace, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.replaceGet", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, - compile_dict_replaceGet, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.add", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_add, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.addGet", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, - compile_dict_addGet, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.del", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, TypeDataBool::create()}), nullptr, - compile_dict_del, FunctionData::flagMarkedAsPure); - define_builtin_func("__dict.delGet", {KeySliceOrInt, TypeDataSlice::create(), PlainDict, TypeDataInt::create()}, TypeDataTensor::create({PlainDict, LookupSliceFound}), nullptr, - compile_dict_delGet, FunctionData::flagMarkedAsPure); -} - -// there are some built-in functions that operate on types declared in stdlib (like Cell) -// that's why these symbols were undefined, and when builtins were registered, they were set to unknown -// after all files have been loaded, symbols have been registered, and aliases exist, -// we patch that earlier registered built-in functions providing types that now exist -void patch_builtins_after_stdlib_loaded() { - TypePtr typeT = TypeDataGenericT::create("T"); - StructPtr struct_debug = lookup_global_symbol("debug")->try_as(); - TypePtr debug = TypeDataStruct::create(struct_debug); - - lookup_function("debug.print")->mutate()->receiver_type = debug; - lookup_function("debug.printString")->mutate()->receiver_type = debug; - lookup_function("debug.dumpStack")->mutate()->receiver_type = debug; - - const Symbol* sym_reflect = lookup_global_symbol("reflect"); - bool reflect_imported = sym_reflect && sym_reflect->ident_anchor->range.get_src_file()->is_stdlib_file; - TypePtr reflect = reflect_imported ? TypeDataStruct::create(sym_reflect->try_as()) : TypeDataNever::create(); - const Symbol* sym_SourceLocation = lookup_global_symbol("SourceLocation"); - TypePtr SourceLocation = reflect_imported ? TypeDataStruct::create(sym_SourceLocation->try_as()) : TypeDataNever::create(); - - lookup_function("reflect.typeNameOf")->mutate()->receiver_type = reflect; - lookup_function("reflect.typeNameOfObject")->mutate()->receiver_type = reflect; - lookup_function("reflect.typeUniqueIdxOf")->mutate()->receiver_type = reflect; - lookup_function("reflect.typeUniqueIdxOfObject")->mutate()->receiver_type = reflect; - lookup_function("reflect.stackSizeOf")->mutate()->receiver_type = reflect; - lookup_function("reflect.stackSizeOfObject")->mutate()->receiver_type = reflect; - lookup_function("reflect.serializationPrefixOf")->mutate()->receiver_type = reflect; - lookup_function("reflect.estimateSerializationOf")->mutate()->receiver_type = reflect; - - lookup_function("reflect.sourceLocation")->mutate()->declared_return_type = SourceLocation; - lookup_function("reflect.sourceLocation")->mutate()->receiver_type = reflect; - lookup_function("reflect.sourceLocationAsString")->mutate()->receiver_type = reflect; - - StructPtr struct_ref_AddressShardingOptions = lookup_global_symbol("AddressShardingOptions")->try_as(); - StructPtr struct_ref_AutoDeployAddress = lookup_global_symbol("AutoDeployAddress")->try_as(); - TypePtr AddressShardingOptions = TypeDataStruct::create(struct_ref_AddressShardingOptions); - TypePtr AutoDeployAddress = TypeDataStruct::create(struct_ref_AutoDeployAddress); - - lookup_function("address.buildSameAddressInAnotherShard")->mutate()->parameters[1].declared_type = AddressShardingOptions; - lookup_function("address.calculateSameAddressInAnotherShard")->mutate()->parameters[1].declared_type = AddressShardingOptions; - lookup_function("AutoDeployAddress.buildAddress")->mutate()->receiver_type = AutoDeployAddress; - lookup_function("AutoDeployAddress.buildAddress")->mutate()->parameters[0].declared_type = AutoDeployAddress; - lookup_function("AutoDeployAddress.calculateAddress")->mutate()->receiver_type = AutoDeployAddress; - lookup_function("AutoDeployAddress.calculateAddress")->mutate()->parameters[0].declared_type = AutoDeployAddress; - lookup_function("AutoDeployAddress.addressMatches")->mutate()->receiver_type = AutoDeployAddress; - lookup_function("AutoDeployAddress.addressMatches")->mutate()->parameters[0].declared_type = AutoDeployAddress; - - StructPtr struct_ref_CellT = lookup_global_symbol("Cell")->try_as(); - StructPtr struct_ref_PackOptions = lookup_global_symbol("PackOptions")->try_as(); - StructPtr struct_ref_UnpackOptions = lookup_global_symbol("UnpackOptions")->try_as(); - TypePtr CellT = TypeDataGenericTypeWithTs::create(struct_ref_CellT, nullptr, {typeT}); - TypePtr PackOptions = TypeDataStruct::create(struct_ref_PackOptions); - TypePtr UnpackOptions = TypeDataStruct::create(struct_ref_UnpackOptions); - - // in stdlib, there is a default parameter `options = {}`; since default parameters are evaluated with AST, - // emulate its presence in built-in functions; it looks ugly, but currently I don't have a better solution - auto v_empty_PackOptions = createV(SrcRange::undefined(), nullptr, createV(SrcRange::undefined(), {})); - v_empty_PackOptions->assign_struct_ref(struct_ref_PackOptions); - v_empty_PackOptions->assign_inferred_type(PackOptions); - auto v_empty_UnpackOptions = createV(SrcRange::undefined(), nullptr, createV(SrcRange::undefined(), {})); - v_empty_UnpackOptions->assign_struct_ref(struct_ref_UnpackOptions); - v_empty_UnpackOptions->assign_inferred_type(UnpackOptions); - - lookup_function("T.toCell")->mutate()->declared_return_type = CellT; - lookup_function("T.toCell")->mutate()->parameters[1].declared_type = PackOptions; - lookup_function("T.toCell")->mutate()->parameters[1].default_value = v_empty_PackOptions; - lookup_function("T.fromCell")->mutate()->parameters[1].declared_type = UnpackOptions; - lookup_function("T.fromCell")->mutate()->parameters[1].default_value = v_empty_UnpackOptions; - lookup_function("T.fromSlice")->mutate()->parameters[1].declared_type = UnpackOptions; - lookup_function("T.fromSlice")->mutate()->parameters[1].default_value = v_empty_UnpackOptions; - lookup_function("Cell.load")->mutate()->parameters[0].declared_type = CellT; - lookup_function("Cell.load")->mutate()->parameters[1].declared_type = UnpackOptions; - lookup_function("Cell.load")->mutate()->parameters[1].default_value = v_empty_UnpackOptions; - lookup_function("Cell.load")->mutate()->receiver_type = CellT; - lookup_function("slice.loadAny")->mutate()->parameters[1].declared_type = UnpackOptions; - lookup_function("slice.loadAny")->mutate()->parameters[1].default_value = v_empty_UnpackOptions; - lookup_function("slice.skipAny")->mutate()->parameters[1].declared_type = UnpackOptions; - lookup_function("slice.skipAny")->mutate()->parameters[1].default_value = v_empty_UnpackOptions; - lookup_function("builder.storeAny")->mutate()->parameters[2].declared_type = PackOptions; - lookup_function("builder.storeAny")->mutate()->parameters[2].default_value = v_empty_PackOptions; - - StructPtr struct_ref_CreateMessageOptions = lookup_global_symbol("CreateMessageOptions")->try_as(); - StructPtr struct_ref_CreateExternalLogMessageOptions = lookup_global_symbol("CreateExternalLogMessageOptions")->try_as(); - StructPtr struct_ref_OutMessage = lookup_global_symbol("OutMessage")->try_as(); - TypePtr CreateMessageOptions = TypeDataGenericTypeWithTs::create(struct_ref_CreateMessageOptions, nullptr, {TypeDataGenericT::create("TBody")}); - TypePtr CreateExternalLogMessageOptions = TypeDataGenericTypeWithTs::create(struct_ref_CreateExternalLogMessageOptions, nullptr, {TypeDataGenericT::create("TBody")}); - TypePtr OutMessage = TypeDataStruct::create(struct_ref_OutMessage); - - lookup_function("createMessage")->mutate()->parameters[0].declared_type = CreateMessageOptions; - lookup_function("createMessage")->mutate()->declared_return_type = OutMessage; - lookup_function("createExternalLogMessage")->mutate()->parameters[0].declared_type = CreateExternalLogMessageOptions; - lookup_function("createExternalLogMessage")->mutate()->declared_return_type = OutMessage; - - if (!lookup_global_symbol("MapLookupResult")) return; - StructPtr struct_ref_LookupResultT = lookup_global_symbol("MapLookupResult")->try_as(); - StructPtr struct_ref_EntryKV = lookup_global_symbol("MapEntry")->try_as(); - TypePtr TKey = TypeDataGenericT::create("K"); - TypePtr TValue = TypeDataGenericT::create("V"); - TypePtr LookupResultT = TypeDataGenericTypeWithTs::create(struct_ref_LookupResultT, nullptr, {TValue}); - TypePtr EntryKV = TypeDataGenericTypeWithTs::create(struct_ref_EntryKV, nullptr, {TKey, TValue}); - - lookup_function("map.get")->mutate()->declared_return_type = LookupResultT; - lookup_function("map.setAndGetPrevious")->mutate()->declared_return_type = LookupResultT; - lookup_function("map.replaceAndGetPrevious")->mutate()->declared_return_type = LookupResultT; - lookup_function("map.addOrGetExisting")->mutate()->declared_return_type = LookupResultT; - lookup_function("map.deleteAndGetDeleted")->mutate()->declared_return_type = LookupResultT; - - auto v_def_throwCode = createV(SrcRange::undefined(), td::make_refint(9), "9"); - v_def_throwCode->assign_inferred_type(TypeDataInt::create()); - lookup_function("map.mustGet")->mutate()->parameters[2].assign_default_value(v_def_throwCode); - - lookup_function("map.findFirst")->mutate()->declared_return_type = EntryKV; - lookup_function("map.findLast")->mutate()->declared_return_type = EntryKV; - lookup_function("map.findKeyGreater")->mutate()->declared_return_type = EntryKV; - lookup_function("map.findKeyGreaterOrEqual")->mutate()->declared_return_type = EntryKV; - lookup_function("map.findKeyLess")->mutate()->declared_return_type = EntryKV; - lookup_function("map.findKeyLessOrEqual")->mutate()->declared_return_type = EntryKV; - lookup_function("map.iterateNext")->mutate()->declared_return_type = EntryKV; - lookup_function("map.iterateNext")->parameters[1].mutate()->declared_type = EntryKV; - lookup_function("map.iteratePrev")->mutate()->declared_return_type = EntryKV; - lookup_function("map.iteratePrev")->parameters[1].mutate()->declared_type = EntryKV; + redefine_builtin_body("createEmptyMap", compile_createEmptyMap); + redefine_builtin_body("createMapFromLowLevelDict", compile_createMapFromLowLevelDict); + redefine_builtin_body("map.exists", generate_mapKV_exists); + redefine_builtin_body("map.get", generate_mapKV_get); + redefine_builtin_body("map.mustGet", generate_mapKV_mustGet); + redefine_builtin_body("map.set", generate_mapKV_set); + redefine_builtin_body("map.setAndGetPrevious", generate_mapKV_setGet); + redefine_builtin_body("map.replaceIfExists", generate_mapKV_replace); + redefine_builtin_body("map.replaceAndGetPrevious", generate_mapKV_replaceGet); + redefine_builtin_body("map.addIfNotExists", generate_mapKV_add); + redefine_builtin_body("map.addOrGetExisting", generate_mapKV_addGet); + redefine_builtin_body("map.delete", generate_mapKV_del); + redefine_builtin_body("map.deleteAndGetDeleted", generate_mapKV_delGet); + redefine_builtin_body("map.findFirst", generate_mapKV_findFirst); + redefine_builtin_body("map.findLast", generate_mapKV_findLast); + redefine_builtin_body("map.findKeyGreater", generate_mapKV_findKeyGreater); + redefine_builtin_body("map.findKeyGreaterOrEqual", generate_mapKV_findKeyGreaterOrEqual); + redefine_builtin_body("map.findKeyLess", generate_mapKV_findKeyLess); + redefine_builtin_body("map.findKeyLessOrEqual", generate_mapKV_findKeyLessOrEqual); + redefine_builtin_body("map.iterateNext", generate_mapKV_iterateNext); + redefine_builtin_body("map.iteratePrev", generate_mapKV_iteratePrev); } } // namespace tolk diff --git a/tolk/codegen.cpp b/tolk/codegen.cpp index 6a5d36c88d..ca2ef72841 100644 --- a/tolk/codegen.cpp +++ b/tolk/codegen.cpp @@ -27,9 +27,35 @@ namespace tolk { // also, `}>ELSE<{` and similar don't have origin, it's not actually an asm instruction static constexpr AnyV NULL_ORIGIN = nullptr; +// Canonical stack after the current breakable loop; `_BreakFromLoop` restores it, then RETALT. +static thread_local const StackLayoutVars* current_break_layout = nullptr; + +struct BreakLayoutGuard { + const StackLayoutVars* prev; + explicit BreakLayoutGuard(const StackLayoutVars* layout) + : prev(current_break_layout) { + current_break_layout = layout; + } + ~BreakLayoutGuard() { + current_break_layout = prev; + } +}; + +// Liveness as a stack layout (analyzer list order). Used when the successor +// stack is not a rearrangement of the current stack — Again's break-exit. +static StackLayoutVars used_vars_layout(const VarDescrList& lives) { + StackLayoutVars out; + for (const VarDescr& vd : lives.list) { + if (!vd.is_unused()) { + out.push_back(vd.idx); + } + } + return out; +} + static void sanitize_fift_name(std::string &name) { for (char &c : name) { - if (c == ' ' || c == '\t') { + if (c == ' ' || c == '\t' || c == '\r') { c = '`'; } } @@ -221,6 +247,19 @@ void Stack::do_copy_var(var_idx_t new_idx, var_idx_t old_idx) { assign_var(new_idx, old_idx); } +// Opposite of enforce_state: do not emit rearranges, just believe the stack is already `req_stack`. +// After RETALT (break) or a noreturn try, the successor never sees the dead-end layout. +void Stack::assume_state(const StackLayoutVars& req_stack) { + s.clear(); + for (var_idx_t v : req_stack) { + s.emplace_back(v, -1); + } +} + +void Stack::assume_state(Stack&& other) { + s = std::move(other.s); +} + void Stack::enforce_state(const StackLayoutVars& req_stack) { int k = (int)req_stack.size(); for (int i = 0; i < k; i++) { @@ -317,7 +356,7 @@ void Stack::apply_wrappers_if_retalt(AnyV origin, int callxargs_count) { o.insert(pos0, NULL_ORIGIN, "SAMEALTSAVE"); o.insert(pos0, NULL_ORIGIN, "c2 SAVE"); } - if (callxargs_count != -1 || ((mode & _InlineFunc) && o.retalt_)) { + if (callxargs_count != -1) { o.insert(pos0, origin, "CONT:<{"); o << AsmOp::Custom(NULL_ORIGIN, "}>"); if (callxargs_count != -1) { @@ -376,6 +415,12 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ stack.o << AsmOp::DebugMark(debug_mark); // DebugMarkLeaveFunction return false; } + case _BreakFromLoop: { + tolk_assert(current_break_layout && "break without a breakable-loop stack layout"); + stack.enforce_state(*current_break_layout); + stack.o << AsmOp::Custom(origin, "RETALT"); + return false; + } case _IntConst: { auto p = next_op.var_info[left[0]]; if (!p || p->is_unused()) { @@ -626,9 +671,9 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ } } } else { - if (f_sym->inline_mode == FunctionInlineMode::inlineViaFif || f_sym->inline_mode == FunctionInlineMode::inlineRef) { + if (f_sym->inline_mode == FunctionInlineMode::inlineRef) { stack.o << AsmOp::Custom(origin, CodeBlob::fift_name(f_sym) + " INLINECALLDICT", (int)right.size(), (int)left.size()); - } else if (f_sym->is_code_function() && std::get(f_sym->body)->code->require_callxargs) { + } else if (f_sym->requires_callxargs()) { stack.o << AsmOp::Custom(origin, CodeBlob::fift_name(f_sym) + " PREPAREDICT", 0, 2); exec_callxargs((int)right.size() + 1, (int)left.size()); } else { @@ -640,7 +685,7 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ int j = ret_order ? ret_order->at(i) : i; stack.push_new_var(left.at(j)); } - return !f_sym || f_sym->declared_return_type != TypeDataNever::create(); + return !f_sym || f_sym->inferred_return_type != TypeDataNever::create(); } case _SetGlob: { tolk_assert(g_sym); @@ -713,24 +758,6 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ stack.rearrange_top(x, var_info[x] && var_info[x]->is_last()); tolk_assert(stack.get(0).var_idx == x); stack.s.pop_back(); - if ((stack.mode & Stack::_InlineFunc) && (block0.is_noreturn() || block1.is_noreturn())) { - bool is0 = block0.is_noreturn(); - OpList& blk_noreturn = is0 ? block0 : block1; - OpList& blk_other = is0 ? block1 : block0; - stack.mode &= ~Stack::_InlineFunc; - stack.o << AsmOp::Custom(origin, is0 ? "IF:<{" : "IFNOT:<{"); - Stack stack_copy{stack}; - stack_copy.mode |= (blk_other.is_noreturn() || next_op.noreturn()) ? 0 : Stack::_NeedRetAlt; - blk_noreturn.generate_code_all(stack_copy); - stack.o << AsmOp::Custom(NULL_ORIGIN, "}>ELSE<{"); - stack.save_stack_comment(); - blk_other.generate_code_all(stack); - if (!blk_other.is_noreturn()) { - parent_ops.generate_code_all(stack, self_idx + 1); - } - stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); - return false; - } if (block1.is_empty_block() || block0.is_empty_block()) { bool is0 = block1.is_empty_block(); OpList& blk = is0 ? block0 : block1; @@ -739,7 +766,6 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ if (blk.is_noreturn()) { stack.o << AsmOp::Custom(origin, is0 ? "IFJMP:<{" : "IFNOTJMP:<{"); Stack stack_copy{stack}; - stack_copy.mode &= ~Stack::_InlineFunc; stack_copy.mode |= next_op.noreturn() ? 0 : Stack::_NeedRetAlt; blk.generate_code_all(stack_copy); stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); @@ -749,7 +775,6 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ Stack stack_copy{stack}, stack_target{stack}; stack_target.disable_output(); stack_target.drop_vars_except(next_op.var_info); - stack_copy.mode &= ~Stack::_InlineFunc; blk.generate_code_all(stack_copy); stack_copy.drop_vars_except(var_info); if ((is0 && stack_copy.s == stack.s) || (!is0 && stack_copy.vars() == stack.vars())) { @@ -777,7 +802,6 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ OpList& blk_other = is0 ? block1 : block0; stack.o << AsmOp::Custom(origin, is0 ? "IFJMP:<{" : "IFNOTJMP:<{"); Stack stack_copy{stack}; - stack_copy.mode &= ~Stack::_InlineFunc; stack_copy.mode |= (blk_other.is_noreturn() || next_op.noreturn()) ? 0 : Stack::_NeedRetAlt; blk_noreturn.generate_code_all(stack_copy); stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); @@ -786,12 +810,10 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ } stack.o << AsmOp::Custom(origin, "IF:<{"); Stack stack_copy{stack}; - stack_copy.mode &= ~Stack::_InlineFunc; block0.generate_code_all(stack_copy); stack_copy.drop_vars_except(next_op.var_info); stack.o << AsmOp::Custom(NULL_ORIGIN, "}>ELSE<{"); stack.save_stack_comment(); - stack.mode &= ~Stack::_InlineFunc; block1.generate_code_all(stack); stack.merge_state(stack_copy); stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); @@ -802,24 +824,30 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ stack.rearrange_top(x, var_info[x] && var_info[x]->is_last()); tolk_assert(stack.get(0).var_idx == x); stack.s.pop_back(); + bool brk = block0.has_reachable_direct_break(); + if (brk) { + stack.drop_vars_except(block0.entry_var_info() + next_op.var_info); + } if (block0.is_noreturn()) { stack.o.retalt_ = true; } if (true || !next_is_terminal_nop) { - stack.o << AsmOp::Custom(origin, "REPEAT:<{"); + stack.o << AsmOp::Custom(origin, brk ? "REPEATBRK:<{" : "REPEAT:<{"); stack.forget_const(); - if (block0.is_noreturn()) { + StackLayoutVars layout1 = stack.vars(); + stack.mode |= Stack::_NeedRetAlt; + BreakLayoutGuard brk_layout(brk ? &layout1 : nullptr); + if (block0.is_noreturn() && !brk) { Stack stack_copy{stack}; - StackLayoutVars layout1 = stack.vars(); - stack_copy.mode &= ~Stack::_InlineFunc; stack_copy.mode |= Stack::_NeedRetAlt; block0.generate_code_all(stack_copy); } else { - StackLayoutVars layout1 = stack.vars(); - stack.mode &= ~Stack::_InlineFunc; - stack.mode |= Stack::_NeedRetAlt; block0.generate_code_all(stack); - stack.enforce_state(layout1); + if (!block0.is_noreturn()) { + stack.enforce_state(layout1); + } else if (brk) { + stack.assume_state(layout1); + } // repeat may execute zero times, so body constants are not valid after the loop stack.forget_const(); } @@ -835,20 +863,29 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ } } case _Again: { + // No normal-exit: back-edge and break are independent stacks. + // header = iteration entry; break-exit may name vars not on the stack yet. stack.drop_vars_except(block0.entry_var_info()); + bool brk = block0.has_reachable_direct_break(); if (block0.is_noreturn()) { stack.o.retalt_ = true; } - if (!next_is_terminal_nop || (stack.mode & Stack::_InlineFunc)) { - stack.o << AsmOp::Custom(origin, "AGAIN:<{"); + if (!next_is_terminal_nop || brk) { + stack.o << AsmOp::Custom(origin, brk ? "AGAINBRK:<{" : "AGAIN:<{"); stack.forget_const(); - StackLayoutVars layout1 = stack.vars(); - stack.mode &= ~Stack::_InlineFunc; + StackLayoutVars header_layout = stack.vars(); + StackLayoutVars break_exit_layout = brk ? used_vars_layout(next_op.var_info) : StackLayoutVars{}; stack.mode |= Stack::_NeedRetAlt; + BreakLayoutGuard brk_layout(brk ? &break_exit_layout : nullptr); block0.generate_code_all(stack); - stack.enforce_state(layout1); + if (!block0.is_noreturn()) { + stack.enforce_state(header_layout); + } + if (brk) { + stack.assume_state(break_exit_layout); + } stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); - return true; + return brk; } else { stack.o << AsmOp::Custom(origin, "AGAINEND"); stack.forget_const(); @@ -869,17 +906,28 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ stack.push_new_var(vd.idx); } } + bool brk = block0.has_reachable_direct_break(); + if (brk) { + stack.drop_vars_except(block0.entry_var_info() + next_op.var_info); + } if (true || !next_is_terminal_nop) { - stack.o << AsmOp::Custom(origin, "UNTIL:<{"); + stack.o << AsmOp::Custom(origin, brk ? "UNTILBRK:<{" : "UNTIL:<{"); stack.forget_const(); auto layout1 = stack.vars(); - stack.mode &= ~Stack::_InlineFunc; stack.mode |= Stack::_NeedRetAlt; + BreakLayoutGuard brk_layout(brk ? &layout1 : nullptr); block0.generate_code_all(stack); - layout1.push_back(left[0]); - stack.enforce_state(layout1); + if (!block0.is_noreturn()) { + layout1.push_back(left[0]); + stack.enforce_state(layout1); + stack.s.pop_back(); + } else if (brk) { + stack.assume_state(layout1); + } stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); - stack.s.pop_back(); + if (brk) { + stack.forget_const(); + } return true; } else { stack.o << AsmOp::Custom(origin, "UNTILEND"); @@ -894,25 +942,36 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ case _While: { // while (block0 | left) block1; ...next var_idx_t x = left[0]; - stack.drop_vars_except(block0.entry_var_info()); + bool brk = block1.has_reachable_direct_break(); + if (brk) { + stack.drop_vars_except(block0.entry_var_info() + next_op.var_info); + } else { + stack.drop_vars_except(block0.entry_var_info()); + } StackLayoutVars layout1 = stack.vars(); bool next_empty = false && next_is_terminal_nop; if (block0.is_noreturn()) { stack.o.retalt_ = true; } - stack.o << AsmOp::Custom(origin, "WHILE:<{"); + stack.o << AsmOp::Custom(origin, brk ? "WHILEBRK:<{" : "WHILE:<{"); stack.forget_const(); - stack.mode &= ~Stack::_InlineFunc; stack.mode |= Stack::_NeedRetAlt; block0.generate_code_all(stack); stack.rearrange_top(x, !next_op.var_info[x] && !block1.entry_var_info()[x]); stack.s.pop_back(); + StackLayoutVars exit_layout = stack.vars(); Stack stack_copy{stack}; stack.o << AsmOp::Custom(origin, next_empty ? "}>DO:" : "}>DO<{"); + BreakLayoutGuard brk_layout(brk ? &exit_layout : nullptr); block1.generate_code_all(stack_copy); - stack_copy.enforce_state(layout1); + if (!block1.is_noreturn()) { + stack_copy.enforce_state(layout1); + } if (!next_empty) { stack.o << AsmOp::Custom(NULL_ORIGIN, "}>"); + if (brk) { + stack.forget_const(); + } return true; } else { return false; @@ -973,7 +1032,7 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ } block0.generate_code_all(stack); if (block0.is_noreturn()) { - stack.s = std::move(catch_stack.s); + stack.assume_state(std::move(catch_stack)); } else if (!block1.is_noreturn()) { stack.merge_state(catch_stack); } @@ -982,7 +1041,7 @@ bool Op::generate_code_step(Stack& stack, const OpList& parent_ops, size_t self_ stack.o << AsmOp::Custom(origin, "COMPOSALT"); stack.o << AsmOp::Custom(origin, "SWAP"); stack.o << AsmOp::Custom(origin, "TRY"); - return true; + return !(block0.is_noreturn() && block1.is_noreturn()); } default: err("unknown operation in generate_code()").fire(origin); @@ -1034,7 +1093,7 @@ std::vector CodeBlob::generate_asm_code(int mode) const { stack.push_new_var(x); } ops.generate_code_all(stack); - stack.apply_wrappers_if_retalt(fun_ref->ident_anchor, require_callxargs && (mode & Stack::_InlineAny) ? n_import_width : -1); + stack.apply_wrappers_if_retalt(fun_ref->ident_anchor, fun_ref->requires_callxargs() && (mode & Stack::_InlineRef) ? n_import_width : -1); return std::move(out_list.list_); } diff --git a/tolk/compilation-errors.cpp b/tolk/compilation-errors.cpp index 8ba620be05..64b17f99d6 100644 --- a/tolk/compilation-errors.cpp +++ b/tolk/compilation-errors.cpp @@ -16,7 +16,7 @@ */ #include "compilation-errors.h" #include "ast.h" -#include "type-system.h" +#include "compiler-settings.h" #include "compiler-state.h" #include "json-output.h" @@ -28,18 +28,48 @@ * Be very careful of using collect()! Bulk errors are tricky. Double-check that no forward logic * relies on this. For example, collecting errors in a type checker (after type inferring) is safe, * but when resolving symbols, if a symbol not found, we can do only fire() — otherwise, nullptr is left. + * + * Optional related locations can be attached with `with_secondary()` before fire()/collect(). + * + * Note: the compiler emits only errors, not warnings (warnings are the goal of a linter). */ namespace tolk { -static std::string str_in_function(FunctionPtr f) { - if (f == nullptr) { - return ""; +static constexpr char ANSI_RESET[] = "\x1b[0m"; +static constexpr char ANSI_BOLD[] = "\x1b[1m"; +static constexpr char ANSI_DIM[] = "\x1b[2m"; +static constexpr char ANSI_BOLD_RED[] = "\x1b[1;31m"; +static constexpr char ANSI_BOLD_BLUE[] = "\x1b[1;34m"; +static constexpr char ANSI_BOLD_MAGENTA[] = "\x1b[1;35m"; +static constexpr char ANSI_BOLD_CYAN[] = "\x1b[1;36m"; + +struct AnsiStyled { + std::string_view text; + const char* ansi_style; +}; + +static std::ostream& operator<<(std::ostream& os, const AnsiStyled& value) { + if (G_settings.colorize_errors) { + os << value.ansi_style; } + os << value.text; + if (G_settings.colorize_errors) { + os << ANSI_RESET; + } + return os; +} + +// generate prefix "in function" or "in lambda in function", returning topmost non-lambda +static std::string str_in_function(FunctionPtr& f) { if (f->is_lambda()) { - return "in lambda " + str_in_function(f->base_fun_ref); + if (!f->base_fun_ref) { + return "in lambda "; + } + f = f->base_fun_ref; + return "in lambda " + str_in_function(f); } - return "in function `" + f->as_human_readable() + "`"; + return "in function "; } void on_assertion_failed(const char *description, const char *file_name, int line_number) { @@ -114,7 +144,7 @@ void ErrorBuilder::push(bool v) { add_arg(v ? "true" : "false"); } -Error ErrorBuilder::build() const { +std::string ErrorBuilder::build_string() const { std::string replaced = tpl; size_t arg_i = 0, pos; while ((pos = replaced.find("{}")) != std::string::npos) { @@ -131,94 +161,154 @@ Error ErrorBuilder::build() const { throw Fatal(std::string("mismatch err() tpl: ") + tpl); } #endif - return Error(std::move(replaced)); + return replaced; } -bool ErrorCollector::empty() const { - for (const ThrownParseError& err : errors) { - if (!err.is_warning) { - return false; - } + +Error& Error::with_secondary(AnyV at, std::string note) { + // `at` may be nullptr for `fun_ref->return_type_node` and other optional; + // then just don't add a secondary location, only a primary will be shown + if (at) { + secondary_locations.push_back(ErrorSecondaryLocation{at->range, std::move(note)}); } - return true; + return *this; } +Error& Error::with_secondary(const Symbol* at_sym, std::string note) { + // compiler-only built-ins like `__throw` have no ident_anchor + if (at_sym->ident_anchor) { + secondary_locations.push_back(ErrorSecondaryLocation{at_sym->ident_anchor->range, std::move(note)}); + } + return *this; +} -void Error::fire(AnyV at, FunctionPtr in_function) const { - throw ThrownParseError(str_in_function(in_function), at->range, message); +Error& Error::with_secondary(SrcRange range, std::string note) { + secondary_locations.push_back(ErrorSecondaryLocation{range, std::move(note)}); + return *this; } -void Error::fire(SrcRange range, FunctionPtr in_function) const { - throw ThrownParseError(str_in_function(in_function), range, message); +void Error::fire(AnyV at, FunctionPtr in_function) { + throw ThrownParseError(in_function, at->range, std::move(message), std::move(secondary_locations)); } -void Error::warning(AnyV at, FunctionPtr in_function) const { - tolk_assert(G.error_collector); - G.error_collector->add(ThrownParseError(str_in_function(in_function), at->range, message, true)); +void Error::fire(const Symbol* at_sym, FunctionPtr in_function) { + throw ThrownParseError(in_function, at_sym->ident_anchor->range, std::move(message), std::move(secondary_locations)); +} + +void Error::fire(SrcRange range, FunctionPtr in_function) { + throw ThrownParseError(in_function, range, std::move(message), std::move(secondary_locations)); } -void Error::warning(SrcRange range, FunctionPtr in_function) const { +void Error::collect(AnyV at, FunctionPtr in_function) { tolk_assert(G.error_collector); - G.error_collector->add(ThrownParseError(str_in_function(in_function), range, message, true)); + G.error_collector->add(ThrownParseError(in_function, at->range, std::move(message), std::move(secondary_locations))); } -void Error::collect(AnyV at, FunctionPtr in_function) const { +void Error::collect(const Symbol* at_sym, FunctionPtr in_function) { tolk_assert(G.error_collector); - G.error_collector->add(ThrownParseError(str_in_function(in_function), at->range, message)); + G.error_collector->add(ThrownParseError(in_function, at_sym->ident_anchor->range, std::move(message), std::move(secondary_locations))); } -void Error::collect(SrcRange range, FunctionPtr in_function) const { +void Error::collect(SrcRange range, FunctionPtr in_function) { tolk_assert(G.error_collector); - G.error_collector->add(ThrownParseError(str_in_function(in_function), range, message)); + G.error_collector->add(ThrownParseError(in_function, range, std::move(message), std::move(secondary_locations))); +} + +struct JsonErrorRange { + SrcRange range; +}; + +static void to_json(JsonPrettyOutput& json, JsonErrorRange v) { + SrcRange::DecodedRange decoded = v.range.decode_offsets(); + json.start_object(); + json.key_value("file_name", v.range.get_src_file()->realpath); + json.key_value("start_line_no", decoded.start_line_no); + json.key_value("start_char_no", decoded.start_char_no); + json.key_value("end_line_no", decoded.end_line_no); + json.key_value("end_char_no", decoded.end_char_no); + json.key_value("text_inside", decoded.text_inside); + json.end_object(); } void ThrownParseError::output_to_console(std::ostream& os) const { - std::string loc_text = range.stringify_start_location(true); - os << loc_text << ": " << (is_warning ? "warning: " : "error: "); - - if (message.find('\n') == std::string::npos) { - // just print a single-line message after "error:" - os << message << std::endl; - } else { - // print "location: line1 \n (spaces) line2 \n ..." - std::string loc_spaces(std::min(static_cast(loc_text.size()), 9), ' '); - size_t start = 0, end; - while ((end = message.find('\n', start)) != std::string::npos) { - if (start > 0) { - os << loc_spaces << " "; - } - os << message.substr(start, end - start) << std::endl; - start = end + 1; - } - if (start < message.size()) { - os << loc_spaces << " " << message.substr(start) << std::endl; - } + os << AnsiStyled{"error", ANSI_BOLD_RED} + << AnsiStyled{": ", ANSI_BOLD}; + + // message is: + // - a single string "can not parse" + // - a multi-line string "can not parse\n""because it's bad" + // - a multi-line string with hints: "can not parse\n""hint: first\n""hint: second" + std::string_view msg_text = message; + std::string_view hint_text; + if (size_t hint_pos = message.find("\nhint:"); hint_pos != std::string_view::npos) { + hint_text = msg_text.substr(hint_pos + 1); + msg_text = msg_text.substr(0, hint_pos); + } + + size_t first_eol = msg_text.find('\n'); + // a bold message after "error:" + os << AnsiStyled{msg_text.substr(0, first_eol), ANSI_BOLD} << "\n"; + // multi-line message: print "(spaces) line2 \n (spaces) line3", non-bold + size_t start = first_eol == std::string_view::npos ? msg_text.size() : first_eol + 1; + while (start < msg_text.size()) { + size_t eol = msg_text.find('\n', start); + size_t end = eol == std::string_view::npos ? msg_text.size() : eol; + os << " " << msg_text.substr(start, end - start) << "\n"; + start = end + 1; + } + + os << AnsiStyled{" --> ", ANSI_BOLD_BLUE} + << range.stringify_start_location(true) << "\n"; + + if (in_function) { + FunctionPtr f_non_lambda = in_function; + os << AnsiStyled{" | ", ANSI_BOLD_BLUE} + << AnsiStyled{str_in_function(f_non_lambda), ANSI_DIM} + << AnsiStyled{f_non_lambda->as_human_readable(), ANSI_BOLD_MAGENTA} << "\n"; + } + + range.output_underlined(os, + G_settings.colorize_errors ? ANSI_BOLD_BLUE : "", + G_settings.colorize_errors ? ANSI_BOLD_RED : "", + G_settings.colorize_errors ? ANSI_RESET : ""); + + // print hints, probably multi-line (one hint per line), non-bold, don't highlight "hint" + if (!hint_text.empty()) { + os << hint_text << "\n"; } - if (!in_function.empty()) { - os << std::endl << " // " << in_function << std::endl; + + for (const ErrorSecondaryLocation& loc : secondary_locations) { + os << "\n" << AnsiStyled{"note", ANSI_BOLD_CYAN} << ": " << loc.note << "\n" + << AnsiStyled{" --> ", ANSI_BOLD_BLUE} + << loc.range.stringify_start_location(true) << "\n"; + loc.range.output_underlined(os, + G_settings.colorize_errors ? ANSI_BOLD_BLUE : "", + G_settings.colorize_errors ? ANSI_BOLD_CYAN : "", + G_settings.colorize_errors ? ANSI_RESET : ""); } - range.output_underlined(os); } void ThrownParseError::output_to_json(JsonPrettyOutput& json) const { json.start_object(); json.key_value("message", message); - if (is_warning) { - json.key_value("is_warning", true); - } - if (!in_function.empty()) { - json.key_value("in_function", in_function); + if (in_function) { + FunctionPtr f_non_lambda = in_function; + std::string in_prefix = str_in_function(f_non_lambda); + json.key_value("in_function", in_prefix + f_non_lambda->as_human_readable()); } if (range.is_valid()) { - SrcRange::DecodedRange r = range.decode_offsets(); - json.start_object("range"); - json.key_value("file_name", range.get_src_file()->realpath); - json.key_value("start_line_no", r.start_line_no); - json.key_value("start_char_no", r.start_char_no); - json.key_value("end_line_no", r.end_line_no); - json.key_value("end_char_no", r.end_char_no); - json.key_value("text_inside", r.text_inside); - json.end_object(); + json.key_value("range", JsonErrorRange{range}); + } + if (!secondary_locations.empty()) { + json.start_array("secondary_locations"); + for (const ErrorSecondaryLocation& loc : secondary_locations) { + json.next_array_item(); + json.start_object(); + json.key_value("note", loc.note); + json.key_value("range", JsonErrorRange{loc.range}); + json.end_object(); + } + json.end_array(); } json.end_object(); } diff --git a/tolk/compilation-errors.h b/tolk/compilation-errors.h index b1ebd7c1e1..adf94248f0 100644 --- a/tolk/compilation-errors.h +++ b/tolk/compilation-errors.h @@ -31,25 +31,44 @@ void on_assertion_failed(const char *description, const char *file_name, int lin class JsonPrettyOutput; // forward declaration +// A compilation error may optionally have several related locations. +// For example, can't `break` from a loop if it has `return` (break is secondary, also highlighted). +struct ErrorSecondaryLocation { + SrcRange range; + std::string note; +}; + +// Error is a compilation error created with `err("...")`: +// 1) err("...").fire() — throw an error immediately and interrupt compilation, it's [[noreturn]] +// 2) err("...").collect() — store errors in a buffer, all collected will be thrown before AST->IR stage class [[nodiscard]] Error { std::string message; + std::vector secondary_locations; + + Error& with_secondary(AnyV at, std::string note); + Error& with_secondary(const Symbol* at_sym, std::string note); + Error& with_secondary(SrcRange range, std::string note); public: explicit Error(std::string message) : message(std::move(message)) {} + template + Error& with_secondary(Loc at, const char* note_tpl, Args&&... args); + GNU_ATTRIBUTE_NORETURN - void fire(AnyV at, FunctionPtr in_function = nullptr) const; + void fire(AnyV at, FunctionPtr in_function = nullptr); GNU_ATTRIBUTE_NORETURN - void fire(SrcRange range, FunctionPtr in_function = nullptr) const; - - void warning(AnyV at, FunctionPtr in_function = nullptr) const; - void warning(SrcRange range, FunctionPtr in_function = nullptr) const; + void fire(const Symbol* at_sym, FunctionPtr in_function = nullptr); + GNU_ATTRIBUTE_NORETURN + void fire(SrcRange range, FunctionPtr in_function = nullptr); - void collect(AnyV at, FunctionPtr in_function = nullptr) const; - void collect(SrcRange range, FunctionPtr in_function = nullptr) const; + void collect(AnyV at, FunctionPtr in_function = nullptr); + void collect(const Symbol* at_sym, FunctionPtr in_function = nullptr); + void collect(SrcRange range, FunctionPtr in_function = nullptr); }; +// `err("format {} string", arg)` and `with_secondary(..., "note {}", arg)` share this formatter. class ErrorBuilder { const char* tpl; std::vector args; @@ -81,16 +100,25 @@ class ErrorBuilder { void push(const void*) = delete; void push(std::nullptr_t) = delete; - [[nodiscard]] Error build() const; + [[nodiscard]] std::string build_string() const; }; +template +inline std::string format_err_message(const char* tpl, Args&&... args) { + ErrorBuilder b(tpl); + (b.push(std::forward(args)), ...); + return b.build_string(); +} + +template +inline Error& Error::with_secondary(Loc at, const char* note_tpl, Args&&... args) { + return with_secondary(at, format_err_message(note_tpl, std::forward(args)...)); +} template GNU_ATTRIBUTE_COLD inline Error err(const char* tpl, Args&&... args) { - ErrorBuilder b(tpl); - (b.push(std::forward(args)), ...); - return b.build(); + return Error(format_err_message(tpl, std::forward(args)...)); } @@ -112,13 +140,15 @@ struct TooDeepStackFatal final : std::exception { }; struct ThrownParseError final : std::exception { - std::string in_function; + FunctionPtr in_function; SrcRange range; std::string message; - bool is_warning = false; + std::vector secondary_locations; - ThrownParseError(std::string in_function, SrcRange range, std::string message, bool is_warning = false) - : in_function(std::move(in_function)), range(range), message(std::move(message)), is_warning(is_warning) {} + ThrownParseError(FunctionPtr in_function, SrcRange range, std::string message, + std::vector secondary_locations) + : in_function(in_function), range(range), message(std::move(message)) + , secondary_locations(std::move(secondary_locations)) {} const char* what() const noexcept override { return message.c_str(); @@ -138,6 +168,7 @@ struct UnexpectedASTNodeKind final : std::exception { } }; +// `err("...").collect()` (not `fire`) are stored in a global buffer. class ErrorCollector { std::vector errors; @@ -146,7 +177,9 @@ class ErrorCollector { errors.push_back(std::move(err)); } - bool empty() const; + bool empty() const { + return errors.empty(); + } std::vector&& flush() { return std::move(errors); diff --git a/tolk/compiler-settings.h b/tolk/compiler-settings.h index c3f204ca18..f5e93234fe 100644 --- a/tolk/compiler-settings.h +++ b/tolk/compiler-settings.h @@ -39,7 +39,6 @@ struct CompilerSettings { using FsReadCallback = std::function(FsReadCallbackKind, const char*, void* callback_payload)>; int verbosity = 0; - int optimization_level = 2; bool stack_layout_comments = true; bool tolk_src_as_line_comments = true; bool emit_compiled_boc = true; @@ -49,6 +48,8 @@ struct CompilerSettings { bool show_errors_as_json = false; bool check_only_no_output = false; bool allow_no_entrypoint = false; + bool allow_empty_get_fun = false; + bool colorize_errors = false; std::string output_filename; // "out.fif"; other outputs are "out.abi.json", "out.boc64.txt", etc. std::string stdlib_folder; // path to tolk-stdlib/; note: from tolk-js it's empty! tolk-js reads files via js callback diff --git a/tolk/compiler-state.cpp b/tolk/compiler-state.cpp index 392a767c6c..36cf24a09d 100644 --- a/tolk/compiler-state.cpp +++ b/tolk/compiler-state.cpp @@ -56,11 +56,7 @@ std::string_view CompilerSettings::get_path_mapping(std::string_view at_prefix) return ""; } -const std::vector& get_all_builtin_functions() { - return G.all_builtins; -} - -const std::vector& get_all_not_builtin_functions() { +const std::vector& get_all_functions() { return G.all_functions; } diff --git a/tolk/compiler-state.h b/tolk/compiler-state.h index 0a7b3f1202..1718fc9554 100644 --- a/tolk/compiler-state.h +++ b/tolk/compiler-state.h @@ -32,8 +32,7 @@ class ErrorCollector; // forward declaration struct CompilerState { GlobalSymbolTable symtable; - std::vector all_builtins; // all built-in functions - std::vector all_functions; // all user-defined (not built-in) global-scope functions, with generic instantiations, with lambdas + std::vector all_functions; // all functions traversed by pipelines, including built-ins, generic instantiations, and lambdas std::vector all_methods; // all user-defined and built-in extension methods for arbitrary types (receivers) std::vector all_global_vars; std::vector all_constants; diff --git a/tolk/constant-evaluator.cpp b/tolk/constant-evaluator.cpp index 5090366ee7..54c958d150 100644 --- a/tolk/constant-evaluator.cpp +++ b/tolk/constant-evaluator.cpp @@ -681,7 +681,7 @@ ConstValExpression eval_and_cache_const_init_val(GlobalConstPtr const_ref) { static thread_local std::vector called_stack; bool contains = std::find(called_stack.begin(), called_stack.end(), const_ref) != called_stack.end(); if (contains) { - err("const `{}` appears, directly or indirectly, in its own initializer", const_ref).fire(const_ref->ident_anchor); + err("const `{}` appears, directly or indirectly, in its own initializer", const_ref).fire(const_ref); } called_stack.push_back(const_ref); RecursionGuard guard([&] { @@ -701,7 +701,7 @@ ConstValExpression eval_field_default_value(StructFieldPtr field_ref) { static thread_local std::vector called_stack; bool contains = std::find(called_stack.begin(), called_stack.end(), field_ref) != called_stack.end(); if (contains) { - err("field `{}` default value circularly references itself", field_ref).fire(field_ref->ident_anchor); + err("field `{}` default value circularly references itself", field_ref).fire(field_ref); } called_stack.push_back(field_ref); RecursionGuard guard([&] { @@ -719,7 +719,7 @@ std::vector calculate_enum_members_with_values(EnumDefPtr enum_re // (unlike constants, enums initializers were not checked earlier for recursion) bool contains = std::find(called_stack.begin(), called_stack.end(), enum_ref) != called_stack.end(); if (contains) { - err("enum `{}` initializers circularly references itself", enum_ref).fire(enum_ref->ident_anchor); + err("enum `{}` initializers circularly references itself", enum_ref).fire(enum_ref); } std::vector values; @@ -736,12 +736,12 @@ std::vector calculate_enum_members_with_values(EnumDefPtr enum_re ConstValExpression assigned = unwrap_const_cast(ConstExpressionEvaluator::eval_any_v_or_fire(member_ref->init_value)); ConstValInt* assigned_int = std::get_if(&assigned); if (!assigned_int) { - err("invalid enum member initializer, not an integer").fire(member_ref->ident_anchor); + err("invalid enum member initializer, not an integer").fire(member_ref); } cur_value = assigned_int->int_val; } if (!cur_value->is_valid() || !cur_value->signed_fits_bits(257)) { - err("integer overflow").fire(member_ref->ident_anchor); + err("integer overflow").fire(member_ref); } values.push_back(cur_value); diff --git a/tolk/contract-directive.cpp b/tolk/contract-directive.cpp index 2b0f2deb72..6e1561216f 100644 --- a/tolk/contract-directive.cpp +++ b/tolk/contract-directive.cpp @@ -53,6 +53,7 @@ ContractDirective* parse_contract_directive(AnyV v) { // an object is allocated on a heap (and is present only for files having `contract` within) ContractDirective* d = new ContractDirective; + d->v_contract = v_contract; d->contractName = static_cast(v_contract->get_identifier()->name); std::vector occurred; diff --git a/tolk/contract-directive.h b/tolk/contract-directive.h index 9630eccdf8..c384bb9e5b 100644 --- a/tolk/contract-directive.h +++ b/tolk/contract-directive.h @@ -23,6 +23,8 @@ namespace tolk { struct ContractDirective { + AnyV v_contract; // ast_contract_directive + std::string contractName; std::string author; std::string version; diff --git a/tolk/fwd-declarations.h b/tolk/fwd-declarations.h index eb817fcaef..6ec5a11398 100644 --- a/tolk/fwd-declarations.h +++ b/tolk/fwd-declarations.h @@ -55,15 +55,15 @@ class TypeData; using TypePtr = const TypeData*; struct GenericsSubstitutions; +struct LazyLoadPlan; struct SrcFile; enum class FunctionInlineMode { - notCalculated, - inlineViaFif, - inlineRef, - inlineInPlace, - noInline, + notAnnotated, + inlineRef, // has `@inline_ref` annotation + inlineInPlace, // has `@inline` annotation or auto-detected to be inlined + noInline, // has `@noinline` annotation }; typedef int var_idx_t; diff --git a/tolk/gen-entrypoints.cpp b/tolk/gen-entrypoints.cpp index 3a3d279547..ff5f680a4a 100644 --- a/tolk/gen-entrypoints.cpp +++ b/tolk/gen-entrypoints.cpp @@ -26,7 +26,7 @@ * In Tolk: * > fun onInternalMessage(in: InMessage) * And to use `in.senderAddress`, `in.body`, `in.originalForwardFee`, etc. in the function. - * Under the hood, `in.senderAddress` is transformed into `INMSG_SRC`, and so on. + * Under the hood, `in.senderAddress` is lowered into `INMSG_SRC`, and so on. * * Also, if `onBouncedMessage` exists, it's embedded directly, like * > if (INMSG_BOUNCED) { onBouncedMessage(in.body); return; } @@ -35,17 +35,17 @@ namespace tolk { // implemented in ast-from-legacy.cpp -std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, bool is_before_immediate_return, const std::vector>& vars_per_arg); +std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, const std::vector>& vars_per_arg); -// check for "modern" `fun onInternalMessage(in: InMessage)`, -// because a "FunC-style" (msgCell: cell, msgBody: slice) also works; -// after transformation `in.xxx` to TVM aux vertices, the last parameter is named `in.body` -static bool is_modern_onInternalMessage(FunctionPtr f_onInternalMessage) { - return f_onInternalMessage->get_num_params() == 1 && f_onInternalMessage->get_param(0).name == "in.body"; -} +std::vector generate_get_InMessage_field(CodeBlob& code, AnyV origin, std::string_view field_name, LocalVarPtr param_in_body) { + // `in.body` and `in.bouncedBody` are actually `slice` from a stack; + // beforehand, `onInternalMessage` was transformed from `in:InMessage` to `in.body:slice` + if (field_name == "body" || field_name == "bouncedBody") { + tolk_assert(param_in_body->ir_idx.size() == 1); + return param_in_body->ir_idx; + } -std::vector AuxData_OnInternalMessage_getField::generate_get_InMessage_field(CodeBlob& code, AnyV origin) const { int idx = -1; if (field_name == "isBounced") idx = 1; else if (field_name == "senderAddress") idx = 2; @@ -54,7 +54,7 @@ std::vector AuxData_OnInternalMessage_getField::generate_get_InMessag else if (field_name == "createdAt") idx = 5; else if (field_name == "valueCoins") idx = 7; else if (field_name == "valueExtra") idx = 8; - tolk_assert(idx != -1); // `in.body` and `in.bouncedBody` are regular parameters, not aux vertices + tolk_assert(idx != -1); std::vector ir_msgparam = code.create_tmp_var(TypeDataInt::create(), origin, "(inmsg-field)"); code.add_call(origin, ir_msgparam, {code.create_int(origin, idx, "(param-idx)")}, lookup_function("__InMessage.getInMsgParam")); @@ -66,11 +66,10 @@ std::vector AuxData_OnInternalMessage_getField::generate_get_InMessag return ir_msgparam; } -void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, const std::vector& rvect_params, CodeBlob& code, AnyV origin) { - // ignore FunC-style `onInternalMessage(msgCell, msgBody)` - if (!is_modern_onInternalMessage(f_onInternalMessage)) { - return; - } +void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, const std::vector& ir_body_slice, CodeBlob& code, AnyV origin) { + // parameter `in:InMessage` was transformed to `in.body:slice` + tolk_assert(f_onInternalMessage->is_onInternalMessage() && ir_body_slice.size() == 1); + // ignore `@on_bounced_policy("manual")`, don't insert "if (isBounced) return" if (f_onInternalMessage->is_manual_on_bounce()) { return; @@ -79,8 +78,7 @@ void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, con const Symbol* sym = lookup_global_symbol("onBouncedMessage"); FunctionPtr f_onBouncedMessage = sym ? sym->try_as() : nullptr; - AuxData_OnInternalMessage_getField get_isBounced(f_onInternalMessage, "isBounced"); - std::vector ir_isBounced = get_isBounced.generate_get_InMessage_field(code, origin); + std::vector ir_isBounced = generate_get_InMessage_field(code, origin, "isBounced", nullptr); if (f_onBouncedMessage) { // generate: `if (isBounced) { onBouncedMessage(); return; } @@ -88,11 +86,10 @@ void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, con Op& if_isBounced = code.add_if_else(origin, ir_isBounced); { code.push_set_cur(if_isBounced.block0); - std::vector ir_bodySlice(rvect_params.end() - 1, rvect_params.end()); if (f_onBouncedMessage->is_inlined_in_place()) { - gen_inline_fun_call_in_place(code, TypeDataVoid::create(), origin, f_onBouncedMessage, nullptr, true, {ir_bodySlice}); + gen_inline_fun_call_in_place(code, TypeDataVoid::create(), origin, f_onBouncedMessage, nullptr, {ir_body_slice}); } else { - code.add_call(origin, {}, ir_bodySlice, f_onBouncedMessage); + code.add_call(origin, {}, ir_body_slice, f_onBouncedMessage); } code.add_return(origin, {}, f_onInternalMessage); code.close_pop_cur(origin); diff --git a/tolk/gen-entrypoints.h b/tolk/gen-entrypoints.h index 5a47346066..ca10e35bbf 100644 --- a/tolk/gen-entrypoints.h +++ b/tolk/gen-entrypoints.h @@ -16,13 +16,12 @@ */ #pragma once -#include "ast-aux-data.h" #include "fwd-declarations.h" #include "tolk.h" namespace tolk { -void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, const std::vector& rvect_params, CodeBlob& code, AnyV origin); -std::vector generate_get_requested_field_parsing_on_demand(const AuxData_OnInternalMessage_getField* aux_data, CodeBlob& code, AnyV origin); +void handle_onInternalMessage_codegen_start(FunctionPtr f_onInternalMessage, const std::vector& ir_body_slice, CodeBlob& code, AnyV origin); +std::vector generate_get_InMessage_field(CodeBlob& code, AnyV origin, std::string_view field_name, LocalVarPtr param_in_body); } // namespace tolk diff --git a/tolk/generics-helpers.cpp b/tolk/generics-helpers.cpp index 33d68332cf..eba00bbfdc 100644 --- a/tolk/generics-helpers.cpp +++ b/tolk/generics-helpers.cpp @@ -112,9 +112,10 @@ void GenericsSubstitutions::set_typeT(std::string_view nameT, TypePtr typeT) { void GenericsSubstitutions::provide_type_arguments(const std::vector& type_arguments) { tolk_assert(genericTs != nullptr); int start_from = genericTs->n_from_receiver; // for `Container.wrap` user should specify only U - tolk_assert(static_cast(type_arguments.size()) + start_from == genericTs->size()); - for (int i = start_from; i < genericTs->size(); ++i) { - valuesTs[i] = type_arguments[i - start_from]; + int n_provided = static_cast(type_arguments.size()); + tolk_assert(n_provided + start_from <= genericTs->size()); + for (int i = 0; i < n_provided; ++i) { + valuesTs[start_from + i] = type_arguments[i]; } } @@ -431,6 +432,14 @@ TypePtr GenericsSubstitutions::get_default_for_nameT(std::string_view nameT) con return idx == -1 ? nullptr : genericTs->get_defaultT(idx); } +bool GenericsSubstitutions::all_Ts_initialized() const { + bool any_nullptr = false; + for (TypePtr ithT : valuesTs) { + any_nullptr |= ithT == nullptr; + } + return !any_nullptr; +} + // given this= and rhs=, check that T1 is equal to T2 in terms of "equal_to" of TypePtr // for example, `Wrapper>` / `Wrapper>` / `Wrapper` are equal bool GenericsSubstitutions::equal_to(const GenericsSubstitutions* rhs) const { @@ -487,7 +496,7 @@ FunctionPtr instantiate_generic_function(FunctionPtr fun_ref, GenericsSubstituti if (FunctionPtr f = existing_sym->try_as(); f && f->base_fun_ref == fun_ref) { return f; } - err_instantiated_name_conflict(new_name).fire(existing_sym->ident_anchor); + err_instantiated_name_conflict(new_name).fire(existing_sym); } // to store permanently, allocate an object in heap @@ -501,11 +510,11 @@ FunctionPtr instantiate_generic_function(FunctionPtr fun_ref, GenericsSubstituti new_parameters.reserve(fun_ref->get_num_params()); for (const LocalVarData& orig_p : fun_ref->parameters) { TypePtr new_param_type = replace_genericT_with_deduced(orig_p.declared_type, allocatedTs); - new_parameters.emplace_back(orig_p.name, nullptr, new_param_type, orig_p.default_value, orig_p.flags, orig_p.param_idx); + new_parameters.emplace_back(orig_p.name, orig_p.ident_anchor, new_param_type, orig_p.default_value, orig_p.flags, orig_p.param_idx); } TypePtr new_return_type = replace_genericT_with_deduced(fun_ref->declared_return_type, allocatedTs); TypePtr new_receiver_type = replace_genericT_with_deduced(fun_ref->receiver_type, allocatedTs); - FunctionData* new_fun_ref = new FunctionData(new_name, nullptr, fun_ref->method_name, new_receiver_type, new_return_type, std::move(new_parameters), fun_ref->flags, fun_ref->inline_mode, nullptr, allocatedTs, {}, fun_ref->body, fun_ref->ast_root); + FunctionData* new_fun_ref = new FunctionData(new_name, fun_ref->ident_anchor, fun_ref->method_name, new_receiver_type, new_return_type, std::move(new_parameters), fun_ref->flags, fun_ref->inline_mode, nullptr, allocatedTs, {}, fun_ref->body, fun_ref->ast_root); new_fun_ref->arg_order = fun_ref->arg_order; new_fun_ref->ret_order = fun_ref->ret_order; new_fun_ref->base_fun_ref = fun_ref; @@ -525,7 +534,7 @@ FunctionPtr instantiate_generic_function(FunctionPtr fun_ref, GenericsSubstituti instantiation_depth--; }); if (instantiation_depth > 64) { - err_instantiate_recursive(fun_ref).fire(fun_ref->ident_anchor); + err_instantiate_recursive(fun_ref).fire(fun_ref); } FunctionPtr new_fun_ref = pipeline_register_instantiated_generic_function(fun_ref, new_root, std::move(new_name), allocatedTs); @@ -543,7 +552,7 @@ StructPtr instantiate_generic_struct(StructPtr struct_ref, GenericsSubstitutions if (StructPtr s = existing_sym->try_as(); s && s->base_struct_ref == struct_ref) { return s; } - err_instantiated_name_conflict(new_name).fire(struct_ref->ident_anchor); + err_instantiated_name_conflict(new_name).fire(struct_ref); } const GenericsSubstitutions* allocatedTs = new GenericsSubstitutions(std::move(substitutedTs)); @@ -557,7 +566,7 @@ StructPtr instantiate_generic_struct(StructPtr struct_ref, GenericsSubstitutions instantiation_depth--; }); if (instantiation_depth > 64) { - err_instantiate_recursive(struct_ref).fire(struct_ref->ident_anchor); + err_instantiate_recursive(struct_ref).fire(struct_ref); } StructPtr new_struct_ref = pipeline_register_instantiated_generic_struct(struct_ref, new_root, std::move(new_name), allocatedTs); @@ -578,11 +587,11 @@ AliasDefPtr instantiate_generic_alias(AliasDefPtr alias_ref, GenericsSubstitutio if (const Symbol* existing_sym = lookup_global_symbol(new_name)) { if (AliasDefPtr a = existing_sym->try_as(); a && a->base_alias_ref == alias_ref) { if (a->underlying_type == nullptr) { - err("type `{}` circularly references itself", a).fire(a->ident_anchor); + err("type `{}` circularly references itself", a).fire(a); } return a; } - err_instantiated_name_conflict(new_name).fire(alias_ref->ident_anchor); + err_instantiated_name_conflict(new_name).fire(alias_ref); } const GenericsSubstitutions* allocatedTs = new GenericsSubstitutions(std::move(substitutedTs)); @@ -596,7 +605,7 @@ AliasDefPtr instantiate_generic_alias(AliasDefPtr alias_ref, GenericsSubstitutio instantiation_depth--; }); if (instantiation_depth > 64) { - err_instantiate_recursive(alias_ref).fire(alias_ref->ident_anchor); + err_instantiate_recursive(alias_ref).fire(alias_ref); } AliasDefPtr new_alias_ref = pipeline_register_instantiated_generic_alias(alias_ref, new_root, std::move(new_name), allocatedTs); @@ -644,13 +653,8 @@ FunctionPtr instantiate_lambda_function(AnyV v_lambda, FunctionPtr parent_fun_re } // a function `myFunPTuplePush(self, v: T) asm "TPUSH"` can't be called with T=Point (2 stack slots); -// almost all asm/built-in generic functions expect one stack slot, but there are exceptions +// almost all asm generic functions expect one stack slot, but there are exceptions bool is_allowed_asm_generic_function_with_non1_width_T(FunctionPtr fun_ref, const GenericsSubstitutions& substitutedTs, int idxT) { - // if a built-in function is marked with a special flag - if (fun_ref->is_variadic_width_T_allowed()) { - return true; - } - // allow `fun Cell.hash(self)` or `fun map.isEmpty(self)` for any generics, because asm does not depend on T/K/V; // more specifically: can we use T=Point? yes, if substituting T=Point and T=int gives equal stack width everywhere GenericsSubstitutions probeTs(fun_ref->genericTs); diff --git a/tolk/generics-helpers.h b/tolk/generics-helpers.h index 13f19748f5..32ac4d9911 100644 --- a/tolk/generics-helpers.h +++ b/tolk/generics-helpers.h @@ -76,6 +76,7 @@ struct GenericsSubstitutions { bool has_nameT(std::string_view nameT) const; TypePtr get_substitution_for_nameT(std::string_view nameT) const; TypePtr get_default_for_nameT(std::string_view nameT) const; + bool all_Ts_initialized() const; std::string_view nameT_at(int idx) const { return genericTs->get_nameT(idx); } TypePtr typeT_at(int idx) const { return valuesTs.at(idx); } bool equal_to(const GenericsSubstitutions* rhs) const; diff --git a/tolk/inline-return-analysis.cpp b/tolk/inline-return-analysis.cpp new file mode 100644 index 0000000000..749bb097d0 --- /dev/null +++ b/tolk/inline-return-analysis.cpp @@ -0,0 +1,339 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#include "inline-return-analysis.h" +#include "ast-visitor.h" +#include "type-system.h" + +/* + * This file answers a single question: "can a function be inlined in place, and if yes, + * where does the control flow go after every `return` in the middle?". + * Outcome: + * - either `InlineReturnPlan`: "here are the if/match nodes that route the flow" + * - or "no, because ..." + * It's used by pipe-detect-inline-in-place (to decide / to fire an error) and by lowering AST->IR. + * + * Inlining in place means "paste a function body into a caller". A `return` inside that body + * can not emit RET, since RET would exit the caller. Instead, `return` is "assign to OUT IR vars". + * What's the problem? Assigning OUT is easy. The hard part is: + * - everything below must not execute, and there is no goto/jump at IR level to skip it + * + * The compiler supports returns in the middle — not everywhere, but in practical cases. + * + * Examples: => Rewritten as (at lowering) + * + * fun demo(x: int) { fun demo(x: int) { + * if (x < 0) { return 0 } if (x < 0) { OUT = 0 } + * return 2; else { OUT = 2 } + * } } + * + * fun demo() { fun demo() { + * if (cond1) { return } if (cond1) { } + * if (cond2) { xxx; return } else { if (cond2) { xxx } + * else { yyy } else { yyy; zzz } } + * zzz } + * } + * + * fun demo() { fun demo() { + * match (subj) { match (subj) { + * 0 => return 0, 0 => { OUT = 0 }, + * 1 => throw 1, 1 => { throw 1 }, + * } else => { + * if (cond) { return 2 } if (cond) { OUT = 2 } + * zzz else { zzz; + * return 3; OUT = 3; } + * } } } } + * + * So, what is supported: + * - `if/else` where both branches terminate (or neither of them contains `return` at all) + * - `if/else` where one branch terminates and the other does not: tail goes to the other + * - `match` where all arms terminate (or none of them contains `return` at all); + * if it's not exhaustive, tail goes to its implicit `else` + * - `match` where all but one arms terminate, and it's exhaustive: tail goes to non-terminating + * + * What is NOT supported: + * - unbalanced if/else, e.g. `if (cond) { if (cond) { return 1 } }` + * - unbalanced match, e.g. 2 arms return, 3 do not + * - `return` in other positions: in a loop, in try/catch, in a standalone block, etc. + * - `return` hidden inside an expression, e.g. `if (match(subj) { 0 => return 0 })` + * + * In one sentence: + * Inlinable <=> every if/match that contain `return` leaves AT MOST ONE FALLTHROUGH PATH — + * a single place where all the code below gets relocated. + * + * The analysis is a walk over statements classifying each one as `InlineStmtFlow` (see below). + * Only `goesToBranch` statements are interesting: each is saved into `plan.nodes` as a pair + * [ pivot -> the branch that continues ], and lowering uses it to know where to place the tail. + */ + +namespace tolk { + +class FindFirstReturnVisitor final : public ASTVisitorFunctionBody { + AnyV found = nullptr; + + void visit(V v) override { + if (!found) { + found = v; + } + } + +public: + bool should_visit_function(FunctionPtr) override { + return false; + } + + AnyV find(AnyV v) { + found = nullptr; + parent::visit(v); + return found; + } +}; + +AnyV find_first_return(AnyV v) { + FindFirstReturnVisitor visitor; + return visitor.find(v); +} + +// How control flow leaves a statement (or a list of statements). +// A statement that contains a `return` but fits none of these shapes is unsupported: +// it makes the whole function non-inlinable, see `fail()`. +enum class InlineStmtFlow { + // control flow reaches the next statement; no `return` that needs routing + fallsThrough, + // all paths leave via `return` (possibly mixed with `throw`); all below should not be lowered + returns, + // all paths leave via `throw`; no `return` on any path + throws, + // contains `return`, but exactly one path still falls through (one if/else/match branch); + // control flow reaches the next statement only after that specific branch executes; + // added to plan nodes for lowering + goesToBranch, +}; + +// Counters over if/match arms: alive = fallsThrough | goesToBranch; dead = returns | throws. +struct InlineArmCounts { + int n_fall_through = 0; + int n_goes_to_branch = 0; + int n_returns = 0; + AnyV last_alive = nullptr; // the last arm/body that does not fully leave +}; + +class InlineReturnPlanBuilder { + InlineReturnPlan plan; + + void fail(const char* because, SrcRange at = SrcRange::undefined()) { + if (plan.ok()) { // store the first "because" reason + plan.cant_inline_because = because; + plan.cant_inline_at = at; + } + } + + void reject_return_inside(AnyV subtree) { + if (AnyV ret = find_first_return(subtree)) { + fail("because of `return` in non-standard, unsupported position", ret->range); + } + } + + // after a terminal statement, we immediately return from `analyze`, but still check statements after it + void analyze_terminal_and_unreachable_suffix(const std::vector& statements, size_t terminal_idx, InlineStmtFlow terminal_flow) { + for (size_t i = terminal_idx; i < statements.size(); ++i) { + AnyV stmt = statements[i]; + InlineStmtFlow stmt_flow = i == terminal_idx ? terminal_flow : analyze(stmt); + if (stmt_flow == InlineStmtFlow::returns && stmt->kind != ast_return_statement && i + 1 < statements.size()) { + // we do not allow such code (to avoid extra state in lowering, since `next` should be erased) + // > if (...) { return } + // > else { return } + // > next + fail("because a terminal `if/else/match` is followed by unreachable statements", statements[i + 1]->range); + break; + } + } + } + + InlineStmtFlow analyze(const std::vector& statements) { + bool has_return = false; + for (size_t i = 0; i < statements.size(); ++i) { + AnyV stmt = statements[i]; + InlineStmtFlow stmt_flow = analyze(stmt); + switch (stmt_flow) { + case InlineStmtFlow::fallsThrough: + break; // proceed to the next statement + case InlineStmtFlow::returns: + analyze_terminal_and_unreachable_suffix(statements, i, stmt_flow); + return InlineStmtFlow::returns; + case InlineStmtFlow::throws: + analyze_terminal_and_unreachable_suffix(statements, i, stmt_flow); + return has_return // example: `if { if { return } throw }` + ? InlineStmtFlow::returns // then mark an outer if as "returns" + : InlineStmtFlow::throws; + case InlineStmtFlow::goesToBranch: + has_return = true; // the tail below will be moved after that fallthrough branch + break; + } + } + return has_return ? InlineStmtFlow::goesToBranch : InlineStmtFlow::fallsThrough; + } + + void analyze_and_add(InlineArmCounts& c, const std::vector& statements, AnyV body) { + switch (analyze(statements)) { + case InlineStmtFlow::fallsThrough: + c.n_fall_through++; + c.last_alive = body; + break; + case InlineStmtFlow::goesToBranch: + c.n_goes_to_branch++; + c.last_alive = body; + break; + case InlineStmtFlow::returns: + c.n_returns++; + break; + case InlineStmtFlow::throws: + break; + } + } + + InlineStmtFlow analyze_if(V v) { + reject_return_inside(v->get_cond()); + // an `if` is a 2-arm exhaustive match: if-body and else-body (else may be empty) + InlineArmCounts c; + analyze_and_add(c, v->get_if_body()->get_items(), v->get_if_body()); + analyze_and_add(c, v->get_else_body()->get_items(), v->get_else_body()); + + // no branch contains `return` — paste as-is, no routing + if (c.n_returns == 0 && c.n_goes_to_branch == 0) { + bool both_branches_terminate = c.n_fall_through == 0; + return both_branches_terminate ? InlineStmtFlow::throws : InlineStmtFlow::fallsThrough; + } + // both branches leave (returns / throws, at least one returns) + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0) { + return InlineStmtFlow::returns; + } + // one branch leaves, tail goes to the other + if (c.n_fall_through + c.n_goes_to_branch == 1) { + plan.nodes.emplace_back(TailRoutingNode{v, c.last_alive}); + return InlineStmtFlow::goesToBranch; + } + + fail("because `if/else` has complicated `return` and multiple exit points", v->keyword_range()); + return InlineStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + InlineStmtFlow analyze_match(V v) { + reject_return_inside(v->get_subject()); + tolk_assert(v->is_statement()); + + InlineArmCounts c; + for (int i = 0; i < v->get_arms_count(); ++i) { + auto v_arm = v->get_arm(i); + if (v_arm->pattern_kind == MatchArmKind::const_expression) { + reject_return_inside(v_arm->get_pattern_expr()); + } + analyze_and_add(c, v_arm->get_body()->get_block_statement()->get_items(), v_arm); + } + + // no arm contains `return` — paste as-is, no routing + if (c.n_returns == 0 && c.n_goes_to_branch == 0) { + bool all_arms_terminate = c.n_fall_through == 0 && v->is_exhaustive; + return all_arms_terminate ? InlineStmtFlow::throws : InlineStmtFlow::fallsThrough; + } + // all arms leave (via `return` / `throw`) + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0 && v->is_exhaustive) { + return InlineStmtFlow::returns; + } + // all arms leave, tail goes to implicit `else` + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0) { + plan.nodes.emplace_back(TailRoutingNode{v, nullptr}); + return InlineStmtFlow::goesToBranch; + } + // all but one arms leave, tail goes to that arm + if (c.n_fall_through + c.n_goes_to_branch == 1 && v->is_exhaustive) { + plan.nodes.emplace_back(TailRoutingNode{v, c.last_alive}); + return InlineStmtFlow::goesToBranch; + } + + fail("because some `match` arms do `return`, some do not", v->keyword_range()); + return InlineStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + InlineStmtFlow analyze(AnyV stmt) { + if (auto v_return = stmt->try_as()) { + reject_return_inside(v_return->get_return_value()); + return InlineStmtFlow::returns; + } + if (auto v_throw = stmt->try_as()) { + reject_return_inside(v_throw); + return InlineStmtFlow::throws; + } + if (auto v_if = stmt->try_as()) { + return analyze_if(v_if); + } + if (auto v_match = stmt->try_as()) { + return analyze_match(v_match); + } + // any other statement is okay, unless it hides a `return` in a position we can't route + if (AnyV ret = find_first_return(stmt)) { + // make a reasonable message for common cases + switch (stmt->kind) { + case ast_repeat_statement: + case ast_while_statement: + case ast_do_while_statement: + fail("because of `return` inside a loop", ret->range); + break; + case ast_try_catch_statement: + fail("because of `return` inside try/catch", ret->range); + break; + default: + fail("because of `return` in non-standard, unsupported position", ret->range); + break; + } + return InlineStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + // a call to a `never`-returning function ends control flow, just like `throw` + if (auto v_call = stmt->try_as()) { + bool is_never = v_call->fun_maybe && v_call->fun_maybe->inferred_return_type == TypeDataNever::create(); + return is_never ? InlineStmtFlow::throws : InlineStmtFlow::fallsThrough; + } + + // (more sophisticated cases like `cond ? alwaysThrows() : alwaysThrows()` are impractical to detect) + return InlineStmtFlow::fallsThrough; + } + +public: + InlineReturnPlan build(FunctionPtr fun_ref) { + tolk_assert(fun_ref->ast_root); + auto v_body = fun_ref->ast_root->as()->get_body()->try_as(); + if (v_body == nullptr) { + fail("because `@inline` is applicable only to code functions, not to `asm`"); + } else if (fun_ref->n_times_called >= 9999) { + fail("because a function recursively calls itself"); + } else if (fun_ref->has_tvm_method_id()) { + fail("because `@inline` is incompatible with `@method_id` and `get fun`"); + } else if (fun_ref->is_used_as_noncall()) { + fail("because it's used as a reference, like `var callback = myFunction` or `f(myFunction)`"); + } else { + analyze(v_body->get_items()); + } + return std::move(plan); + } +}; + +InlineReturnPlan build_inlining_plan_for_function(FunctionPtr fun_ref) { + InlineReturnPlanBuilder builder; + return builder.build(fun_ref); +} + +} // namespace tolk diff --git a/tolk/inline-return-analysis.h b/tolk/inline-return-analysis.h new file mode 100644 index 0000000000..c51b6672e3 --- /dev/null +++ b/tolk/inline-return-analysis.h @@ -0,0 +1,50 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#pragma once + +#include "fwd-declarations.h" +#include "src-file.h" +#include "tail-routing.h" +#include + +namespace tolk { + +// Per-function plan for every inlined function (auto-detected or marked as `@inline`). +// Note that `continue` in loops (LoopContinuePlan) is almost the same algorithm as returns in the middle. +struct InlineReturnPlan { + const char* cant_inline_because = nullptr; // error text reason: "because of `return` inside a loop" + SrcRange cant_inline_at = SrcRange::undefined(); // underlined secondary location (e.g. that return statement) + std::vector nodes; + + bool ok() const { + return cant_inline_because == nullptr; + } + + const TailRoutingNode* find_branching(AnyV stmt_pivot) const { + for (const TailRoutingNode& node : nodes) { + if (node.pivot == stmt_pivot) { + return &node; + } + } + return nullptr; + } +}; + +AnyV find_first_return(AnyV v); +InlineReturnPlan build_inlining_plan_for_function(FunctionPtr fun_ref); + +} // namespace tolk diff --git a/tolk/json-output.h b/tolk/json-output.h index 2ca5a840df..4ef164a7a8 100644 --- a/tolk/json-output.h +++ b/tolk/json-output.h @@ -125,10 +125,12 @@ class JsonPrettyOutput { for (char c: value) { if (c == '"') os << '\\' << '"'; else if (c == '\n') os << '\\' << 'n'; - else if (c == '\r') os << '\\' << 'r'; - else if (c == '\t') os << '\\' << 't'; - else if (c == '\b') os << '\\' << 'b'; else if (c == '\\') os << '\\' << '\\'; + else if (static_cast(c) < 0x20) { + static constexpr char hex[] = "0123456789abcdef"; + unsigned char uc = static_cast(c); + os << "\\u00" << hex[uc >> 4] << hex[uc & 0x0f]; + } else os << c; } os << '"'; diff --git a/tolk/lazy-helpers.cpp b/tolk/lazy-helpers.cpp index fd3a11712b..a1311dca41 100644 --- a/tolk/lazy-helpers.cpp +++ b/tolk/lazy-helpers.cpp @@ -23,7 +23,7 @@ namespace tolk { /* * This file contains "lazy" state across multiple files. - * They all are used after `lazy` operators have been processed and "load xxx" vertices have been inserted. + * They all are used after `lazy` operators have been processed and LazyLoadPlan has been built. * Particularly, while transforming AST to Ops. * For comments about laziness, see pipe-lazy-load-insertions.cpp. */ @@ -83,7 +83,7 @@ void LazyStructLoadedState::on_started_loading(StructPtr hidden_struct) { void LazyStructLoadedState::on_original_field_loaded(StructFieldPtr hidden_field) { this->ith_field_was_loaded[hidden_field->field_idx] = true; - // for example, `var p = lazy Point; aux "load x"; return p.x`; + // for example, `var p = lazy Point; /* plan: load x */ return p.x`; // we are at "load x", it exists in Point, here just save it was loaded (for assertions and debugging); // apart from saving, stack is also updated when loading, `p` becomes `valueX null` } @@ -91,12 +91,12 @@ void LazyStructLoadedState::on_original_field_loaded(StructFieldPtr hidden_field void LazyStructLoadedState::on_aside_field_loaded(StructFieldPtr hidden_field, std::vector&& ir_field_gap) { this->ith_field_was_loaded[hidden_field->field_idx] = true; this->aside_gaps_and_tail.emplace_back(hidden_field, std::move(ir_field_gap)); - // for example, `var st = lazy Storage; aux "load gap, load seqno"; st.seqno += 1; st.toCell()`; + // for example, `var st = lazy Storage; /* plan: load gap, load seqno */ st.seqno += 1; st.toCell()`; // we are at "load gap", it does not exist in Storage, so save loaded value separately } std::vector LazyStructLoadedState::get_ir_loaded_aside_field(StructFieldPtr hidden_field) const { - // for example, `var st = lazy Storage; aux "load gap, load seqno"; st.seqno += 1; st.toCell()`; + // for example, `var st = lazy Storage; /* plan: load gap, load seqno */ st.seqno += 1; st.toCell()`; // we are at "st.toCell()" that stores immutable gap before modified "seqno" for (const auto& [gap_field, ir_field_gap] : this->aside_gaps_and_tail) { if (gap_field == hidden_field) { diff --git a/tolk/lazy-helpers.h b/tolk/lazy-helpers.h index 8fea95b7bd..757fd2c843 100644 --- a/tolk/lazy-helpers.h +++ b/tolk/lazy-helpers.h @@ -17,12 +17,13 @@ #pragma once #include "fwd-declarations.h" +#include #include namespace tolk { // LazyStructLoadInfo describes how to load a struct: which fields to load, which to skip. -// It's calculated based on variable usages and passed through the pipeline carried by auxiliary AST vertices. +// It's calculated based on variable usages and stored in LazyLoadPlan for the function. // Based on it, lazy loading Ops are generated in pack-unpack api. // To understand `hidden_struct`, read pipe-lazy-load-insertions.cpp. struct LazyStructLoadInfo { @@ -44,9 +45,33 @@ struct LazyStructLoadInfo { } }; -// LazyStructLoadedState represents state (which fields were already loaded) while generating AST to Ops. -// For example, variable `var p = lazy Point.fromSlice(s); aux "load x"; return p.x` is initially "nothing loaded", -// and after "load x" ith_field_action[0] becomes true (and `p` is updated on a stack and becomes `valueX null`). +// LazyLoadAction is one prelude operation: load/skip fields of a lazy object immediately before a statement. +struct LazyLoadAction { + LocalVarPtr var_ref; // comes from `lazy` + TypePtr union_variant; // not just `o` but `match(o) { V1 => here }` + StructFieldPtr field_ref; // not just `o` but `match(o.field) { V1 => here }` + LazyStructLoadInfo load_info; // instructions, which fields to load, which to skip, etc. + + LazyLoadAction(LocalVarPtr var_ref, TypePtr union_variant, StructFieldPtr field_ref, LazyStructLoadInfo load_info) + : var_ref(var_ref), union_variant(union_variant), field_ref(field_ref), load_info(std::move(load_info)) { + } +}; + +// LazyLoadPlan is an immutable per-function side table: which lazy-load actions to emit before which statement. +// Built by pipe-lazy-load-insertions, consumed by AST->IR lowering. +struct LazyLoadPlan { + std::unordered_map> loads_before_statement; + + const std::vector& loads_before(AnyV stmt) const { + static const std::vector empty; + auto it = loads_before_statement.find(stmt); + return it != loads_before_statement.end() ? it->second : empty; + } +}; + +// LazyStructLoadedState represents state (which fields were already loaded) while lowering AST->IR. +// For example, variable `var p = lazy Point.fromSlice(s)` is initially "nothing loaded", +// and after the planned "load x" ith_field_action[0] becomes true (and `p` is updated on a stack and becomes `valueX null`). struct LazyStructLoadedState { StructPtr original_struct; // original (e.g. `Point`) StructPtr hidden_struct = nullptr; // "lazy Point" — only requested fields, matching binary shape diff --git a/tolk/loop-control-analysis.cpp b/tolk/loop-control-analysis.cpp new file mode 100644 index 0000000000..2f27e3cea3 --- /dev/null +++ b/tolk/loop-control-analysis.cpp @@ -0,0 +1,310 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#include "loop-control-analysis.h" +#include "ast-visitor.h" +#include "type-system.h" + +/* + * This file answers a single question: "can break/continue be compiled in this loop, and if yes, + * where does the control flow go after every `continue`?". + * Outcome: + * - either `LoopContinuePlan`: "here are the if/match nodes that route the flow" + * - or "no, because ..." + * It's used by pipe-loop-break-continue (to fire an error) and by lowering AST->IR. + * + * Note: an algorithm is VERY similar to "inline with multiple returns", almost identical. + * + * `continue` can not emit a jump at IR level. Instead, the remaining suffix of the loop + * is relocated into the single live if/match branch — the same FallthroughTail as inline returns. + * `break` is different: it emits native *BRK / RETALT, so it does not need routing. + * + * Examples: => Rewritten as (at lowering) + * + * while (true) { while (true) { + * if (x) { continue } if (x) { } + * yyy else { yyy } + * } } + * + * while (true) { while (true) { + * if (cond1) { continue } if (cond1) { } + * if (cond2) { xxx; continue } else { if (cond2) { xxx } + * else { yyy } else { yyy; zzz } } + * zzz } + * } + * + * while (true) { while (true) { + * match (subj) { match (subj) { + * 0 => continue, 0 => { }, + * 1 => throw 1, 1 => { throw 1 }, + * } else => { + * zzz zzz + * } } } + * + * See `inline-return-analysis.cpp` for what is supported and what is not. + * + * In one sentence: + * Compilable continue <=> every if/match that contain `continue` leaves AT MOST ONE FALLTHROUGH PATH — + * a single place where all the code below gets relocated. + * + * Nested loops are opaque: their transfers belong to them, not to the outer loop. + */ + +namespace tolk { + +class FindFirstContinueVisitor final : public ASTVisitorFunctionBody { + AnyV found = nullptr; + + void visit(V v) override { + if (!found) { + found = v; + } + } + + // in the current loop only: nested `continue` is their, not ours + void visit(V) override {} + void visit(V) override {} + void visit(V) override {} + +public: + bool should_visit_function(FunctionPtr) override { + return false; + } + + AnyV find(AnyV v) { + found = nullptr; + parent::visit(v); + return found; + } +}; + +static AnyV find_first_continue(AnyV v) { + FindFirstContinueVisitor visitor; + return visitor.find(v); +} + +// How control flow leaves a statement (or a list of statements). +// A statement that contains a `continue` but fits none of these shapes is unsupported: +// it makes the whole loop non-compilable, see `fail()`. +enum class LoopStmtFlow { + // control flow reaches the next statement; no `continue` that needs routing + fallsThrough, + // all paths leave via `continue` (possibly mixed with break / return / throw); all below should not be lowered + continues, + // all paths leave via break / return / throw / never-call; no `continue` on any path + terminates, + // contains `continue`, but exactly one path still falls through (one if/else/match branch); + // control flow reaches the next statement only after that specific branch executes; + // added to plan nodes for lowering + goesToBranch, +}; + +// Counters over if/match arms: alive = fallsThrough | goesToBranch; dead = continues | terminates. +struct LoopArmCounts { + int n_fall_through = 0; + int n_goes_to_branch = 0; + int n_continues = 0; + AnyV last_alive = nullptr; // the last arm/body that does not fully leave +}; + +class LoopContinuePlanBuilder { + LoopContinuePlan plan; + + void fail(const char* because, SrcRange at = SrcRange::undefined()) { + if (plan.ok()) { // store the first "because" reason + plan.cant_continue_because = because; + plan.cant_continue_at = at; + } + } + + void reject_continue_inside(AnyV subtree) { + if (AnyV cont = find_first_continue(subtree)) { + fail("because of `continue` in non-standard, unsupported position", cont->range); + } + } + + // after a terminal statement, we immediately return from `analyze`, but still check statements after it + void analyze_terminal_and_unreachable_suffix(const std::vector& statements, size_t terminal_idx, LoopStmtFlow terminal_flow) { + for (size_t i = terminal_idx; i < statements.size(); ++i) { + AnyV stmt = statements[i]; + LoopStmtFlow stmt_flow = i == terminal_idx ? terminal_flow : analyze(stmt); + if (stmt_flow == LoopStmtFlow::continues && stmt->kind != ast_continue_statement && i + 1 < statements.size()) { + // we do not allow such code (to avoid extra state in lowering, since `next` should be erased) + // > if (...) { continue } + // > else { break } + // > next + fail("because a terminal `if/else/match` is followed by unreachable statements", statements[i + 1]->range); + break; + } + } + } + + LoopStmtFlow analyze(const std::vector& statements) { + bool has_continue = false; + for (size_t i = 0; i < statements.size(); ++i) { + AnyV stmt = statements[i]; + LoopStmtFlow stmt_flow = analyze(stmt); + switch (stmt_flow) { + case LoopStmtFlow::fallsThrough: + break; // proceed to the next statement + case LoopStmtFlow::continues: + analyze_terminal_and_unreachable_suffix(statements, i, stmt_flow); + return LoopStmtFlow::continues; + case LoopStmtFlow::terminates: + analyze_terminal_and_unreachable_suffix(statements, i, stmt_flow); + return has_continue // example: `if { if { continue } throw }` + ? LoopStmtFlow::continues // then mark an outer if as "continues" + : LoopStmtFlow::terminates; + case LoopStmtFlow::goesToBranch: + has_continue = true; // the tail below will be moved after that fallthrough branch + break; + } + } + return has_continue ? LoopStmtFlow::goesToBranch : LoopStmtFlow::fallsThrough; + } + + void analyze_and_add(LoopArmCounts& c, const std::vector& statements, AnyV body) { + switch (analyze(statements)) { + case LoopStmtFlow::fallsThrough: + c.n_fall_through++; + c.last_alive = body; + break; + case LoopStmtFlow::goesToBranch: + c.n_goes_to_branch++; + c.last_alive = body; + break; + case LoopStmtFlow::continues: + c.n_continues++; + break; + case LoopStmtFlow::terminates: + break; + } + } + + LoopStmtFlow analyze_if(V v) { + reject_continue_inside(v->get_cond()); + // an `if` is a 2-arm exhaustive match: if-body and else-body (else may be empty) + LoopArmCounts c; + analyze_and_add(c, v->get_if_body()->get_items(), v->get_if_body()); + analyze_and_add(c, v->get_else_body()->get_items(), v->get_else_body()); + + // no branch contains `continue` — paste as-is, no routing + if (c.n_continues == 0 && c.n_goes_to_branch == 0) { + bool both_branches_terminate = c.n_fall_through == 0; + return both_branches_terminate ? LoopStmtFlow::terminates : LoopStmtFlow::fallsThrough; + } + // both branches leave (continues / terminates, at least one continues) + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0) { + return LoopStmtFlow::continues; + } + // one branch leaves, tail goes to the other + if (c.n_fall_through + c.n_goes_to_branch == 1) { + plan.nodes.emplace_back(TailRoutingNode{v, c.last_alive}); + return LoopStmtFlow::goesToBranch; + } + + fail("because `if/else` has complicated `continue` and multiple exit points", v->keyword_range()); + return LoopStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + LoopStmtFlow analyze_match(V v) { + reject_continue_inside(v->get_subject()); + tolk_assert(v->is_statement()); + + LoopArmCounts c; + for (int i = 0; i < v->get_arms_count(); ++i) { + auto v_arm = v->get_arm(i); + if (v_arm->pattern_kind == MatchArmKind::const_expression) { + reject_continue_inside(v_arm->get_pattern_expr()); + } + analyze_and_add(c, v_arm->get_body()->get_block_statement()->get_items(), v_arm); + } + + // no arm contains `continue` — paste as-is, no routing + if (c.n_continues == 0 && c.n_goes_to_branch == 0) { + bool all_arms_terminate = c.n_fall_through == 0 && v->is_exhaustive; + return all_arms_terminate ? LoopStmtFlow::terminates : LoopStmtFlow::fallsThrough; + } + // all arms leave (via `continue` / break / return / throw) + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0 && v->is_exhaustive) { + return LoopStmtFlow::continues; + } + // all arms leave, tail goes to implicit `else` + if (c.n_fall_through == 0 && c.n_goes_to_branch == 0) { + plan.nodes.emplace_back(TailRoutingNode{v, nullptr}); + return LoopStmtFlow::goesToBranch; + } + // all but one arms leave, tail goes to that arm + if (c.n_fall_through + c.n_goes_to_branch == 1 && v->is_exhaustive) { + plan.nodes.emplace_back(TailRoutingNode{v, c.last_alive}); + return LoopStmtFlow::goesToBranch; + } + + fail("because some `match` arms do `continue`, some do not", v->keyword_range()); + return LoopStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + LoopStmtFlow analyze(AnyV stmt) { + if (stmt->try_as()) { + return LoopStmtFlow::continues; + } + if (stmt->try_as()) { + return LoopStmtFlow::terminates; + } + if (auto v_return = stmt->try_as()) { + reject_continue_inside(v_return->get_return_value()); + return LoopStmtFlow::terminates; + } + if (auto v_throw = stmt->try_as()) { + reject_continue_inside(v_throw); + return LoopStmtFlow::terminates; + } + if (auto v_if = stmt->try_as()) { + return analyze_if(v_if); + } + if (auto v_match = stmt->try_as()) { + return analyze_match(v_match); + } + // any other statement is okay, unless it hides a `continue` in a position we can't route; + // nested loops are skipped by find_first_continue (their transfers are not ours) + if (AnyV cont = find_first_continue(stmt)) { + fail("because of `continue` in non-standard, unsupported position", cont->range); + return LoopStmtFlow::fallsThrough; // the plan is already invalid, the value doesn't matter + } + + // a call to a `never`-returning function ends control flow, just like `throw` + if (auto v_call = stmt->try_as()) { + bool is_never = v_call->fun_maybe && v_call->fun_maybe->inferred_return_type == TypeDataNever::create(); + return is_never ? LoopStmtFlow::terminates : LoopStmtFlow::fallsThrough; + } + + // (more sophisticated cases like `cond ? alwaysThrows() : alwaysThrows()` are impractical to detect) + return LoopStmtFlow::fallsThrough; + } + +public: + LoopContinuePlan build(V loop_body) { + analyze(loop_body->get_items()); + return std::move(plan); + } +}; + +LoopContinuePlan build_continue_plan_for_loop(AnyV loop_body_block) { + LoopContinuePlanBuilder builder; + return builder.build(loop_body_block->as()); +} + +} // namespace tolk diff --git a/tolk/loop-control-analysis.h b/tolk/loop-control-analysis.h new file mode 100644 index 0000000000..c3cba0115c --- /dev/null +++ b/tolk/loop-control-analysis.h @@ -0,0 +1,49 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#pragma once + +#include "fwd-declarations.h" +#include "src-file.h" +#include "tail-routing.h" +#include + +namespace tolk { + +// Per-loop plan for structural `continue`; analogous to InlineReturnPlan. +// See the cpp file for comments. +struct LoopContinuePlan { + const char* cant_continue_because = nullptr; // error text reason: "because some `match` arms do `continue`, some do not" + SrcRange cant_continue_at = SrcRange::undefined(); // underlined secondary location (e.g. that match keyword) + std::vector nodes; + + bool ok() const { + return cant_continue_because == nullptr; + } + + const TailRoutingNode* find_branching(AnyV stmt_pivot) const { + for (const TailRoutingNode& node : nodes) { + if (node.pivot == stmt_pivot) { + return &node; + } + } + return nullptr; + } +}; + +LoopContinuePlan build_continue_plan_for_loop(AnyV loop_body_block); + +} // namespace tolk diff --git a/tolk/maps-kv-api.cpp b/tolk/maps-kv-api.cpp index 0783056344..86fa2a41f8 100644 --- a/tolk/maps-kv-api.cpp +++ b/tolk/maps-kv-api.cpp @@ -98,7 +98,7 @@ static int is_TKey_TVM_slice(TypePtr TKey) { return t_address->is_internal() ? 3 + 8 + 256 : 0; } if (const TypeDataBitsN* t_bitsN = TKey->try_as()) { - return t_bitsN->is_bits ? t_bitsN->n_width : t_bitsN->n_width * 8; + return t_bitsN->n_bits; } if (const TypeDataAlias* t_alias = TKey->try_as()) { return is_TKey_TVM_slice(t_alias->underlying_type); @@ -293,7 +293,7 @@ static std::vector construct_MapEntry_with_non_trivial_key(CodeBlob& Op& if_found = code.add_if_else(origin, {ir_entry[2]}); { code.push_set_cur(if_found.block0); - UnpackContext ctx(code, origin, std::vector(ir_entry.begin() + 1, ir_entry.begin() + 2), create_default_UnpackOptions(code, origin)); + UnpackContext ctx(code, origin, std::vector(ir_entry.begin() + 1, ir_entry.begin() + 2), create_default_UnpackOptions(code, origin), GeneratedLoadPolicy::AllowRemoving); std::vector ir_unpacked_key = ctx.generate_unpack_any(TKey); code.add_let(origin, ir_key, std::move(ir_unpacked_key)); code.close_pop_cur(origin); diff --git a/tolk/overload-resolution.cpp b/tolk/overload-resolution.cpp index 48953b98c0..b834de16b7 100644 --- a/tolk/overload-resolution.cpp +++ b/tolk/overload-resolution.cpp @@ -150,6 +150,33 @@ static bool is_more_specific_generic(TypePtr typeA, TypePtr typeB, const Generic && !can_substitute_Ts_to_reach_actual(typeA, typeB, genericTsA); } +// Checks if `provided` can be used to call `fun receiver.method`. +// It's "directionally assignable" relation: `IntAlias->int` is ok, but `int->IntAlias` not. +// Example for `fun int.method` (receiver=`int`): +// - distance = 0 for provided=`int` +// - distance = 1 for provided=`IntAlias` +// - incomparable for provided=`slice` (not applicable) +// Note that `fun IntAlias.method` can't be called with receiver=`int` (also inapplicable). +static SubtypeDistance does_receiver_accept(TypePtr receiver, TypePtr provided) { + // fast path for `int` and `int` and other primitives which are singletons (90% cases) + if (receiver == provided) { + return SubtypeDistance::ok(0); + } + + // accept `array` to `array` (distance 1), `array` to `array` (equal, distance 0), etc. + SubtypeDistance d = SubtypeDistance::calc_between(provided, receiver); + if (d.is_applicable()) { + return d; + } + + // accept `int8` to `int` and other type coercions + if (!receiver->equal_to(provided) && receiver->can_rhs_be_assigned(provided) && !receiver->try_as()) { + // return a "large distance": finally all available methods are sorted by distance + return SubtypeDistance::ok(1000000); + } + return SubtypeDistance::incomparable(); +} + // Find a receiver pattern that dominates all other applicable overloads: // - `map` beats both `map` and `map` // - `Container` beats `Container` @@ -190,12 +217,28 @@ std::vector resolve_methods_for_call(TypePtr provided_recei try { // check whether exist some T to make it a valid call (probably with type coercion) GenericSubstitutionsDeducing deducingTs(method_ref); TypePtr replaced = deducingTs.auto_deduce_from_argument(receiver, provided_receiver); - if (replaced->can_rhs_be_assigned(provided_receiver) && !replaced->has_genericT_inside()) { - viable.emplace_back(receiver, replaced, method_ref, deducingTs.flush()); + SubtypeDistance receiver_accepted = does_receiver_accept(replaced, provided_receiver); + if (receiver_accepted.is_applicable() && !replaced->has_genericT_inside()) { + viable.push_back(MethodCallCandidate{ + .original_receiver = receiver, + .instantiated_receiver = replaced, + .receiver_distance = receiver_accepted.get_distance(), + .method_ref = method_ref, + .substitutedTs = deducingTs.flush(), + }); } } catch (...) {} - } else if (receiver->can_rhs_be_assigned(provided_receiver) && provided_receiver != TypeDataNever::create()) { - viable.emplace_back(receiver, receiver, method_ref, GenericsSubstitutions(method_ref->genericTs)); + } else { + SubtypeDistance receiver_accepted = does_receiver_accept(receiver, provided_receiver); + if (receiver_accepted.is_applicable() && provided_receiver != TypeDataNever::create()) { + viable.push_back(MethodCallCandidate{ + .original_receiver = receiver, + .instantiated_receiver = receiver, + .receiver_distance = receiver_accepted.get_distance(), + .method_ref = method_ref, + .substitutedTs = GenericsSubstitutions(method_ref->genericTs), + }); + } } } } @@ -206,21 +249,27 @@ std::vector resolve_methods_for_call(TypePtr provided_recei } // okay, we have multiple viable methods, and need to locate the better - // 1) exact match candidates with equal_to() - // (for instance, an alias equals to its underlying type, as well as `T1|T2` equals to `T2|T1`) - std::vector exact; + // 1) nearest match candidates by directional receiver distance + // (find `int.method` for receiver=`int`) + int best_distance = 1000001; + for (const MethodCallCandidate& candidate : viable) { + best_distance = std::min(best_distance, candidate.receiver_distance); + } + + // for provided=`IntAlias`, both `int.method` and `IntAlias.method` applicable, take the last + std::vector nearest; size_t n_generics = 0; for (const MethodCallCandidate& candidate : viable) { - if (candidate.instantiated_receiver->equal_to(provided_receiver)) { - exact.push_back(candidate); + if (candidate.receiver_distance == best_distance) { + nearest.push_back(candidate); + n_generics += candidate.is_generic(); } - n_generics += candidate.is_generic(); } - if (exact.size() == 1) { - return exact; + if (nearest.size() == 1) { + return nearest; } - if (!exact.empty()) { - viable = std::move(exact); + if (!nearest.empty()) { + viable = std::move(nearest); } if (n_generics == 0) { // with only non-generic candidates, do not use shape as a tie-breaker: diff --git a/tolk/overload-resolution.h b/tolk/overload-resolution.h index 9ac6b31e2b..acc80b0c2e 100644 --- a/tolk/overload-resolution.h +++ b/tolk/overload-resolution.h @@ -27,15 +27,10 @@ namespace tolk { struct MethodCallCandidate { TypePtr original_receiver; TypePtr instantiated_receiver; + int receiver_distance; FunctionPtr method_ref; GenericsSubstitutions substitutedTs; - MethodCallCandidate(TypePtr original_receiver, TypePtr instantiated_receiver, FunctionPtr method_ref, GenericsSubstitutions&& substitutedTs) - : original_receiver(original_receiver) - , instantiated_receiver(instantiated_receiver) - , method_ref(method_ref) - , substitutedTs(std::move(substitutedTs)) {} - bool is_generic() const { return original_receiver != instantiated_receiver; } }; diff --git a/tolk/pack-unpack-api.cpp b/tolk/pack-unpack-api.cpp index e65d276e74..dfcf9708fd 100644 --- a/tolk/pack-unpack-api.cpp +++ b/tolk/pack-unpack-api.cpp @@ -57,24 +57,19 @@ class PackUnpackAvailabilityChecker { }; std::vector called_stack; // to prevent recursion unless it passes through `Cell` - std::vector* out_un_pack_candidates; + std::vector* out_un_pack_candidates = nullptr; + std::vector* out_un_pack_functions = nullptr; // check custom pack/unpack functions; their prototypes have already been checked at type inferring, // so here we analyze other consistency (properties filled after type inferring) static std::optional check_custom_pack_unpack(TypePtr receiver_type, CustomPackUnpackF f, bool is_pack) { std::string receiver_name = receiver_type->as_human_readable(); if (f.f_pack) { - if (!f.f_pack->is_inlined_in_place()) { - return CantSerializeBecause("because `" + receiver_name + ".packToBuilder()` can't be inlined; probably, it contains `return` in the middle"); - } if (!is_pack && !f.f_unpack) { return CantSerializeBecause("because type `" + receiver_name + "` defines a custom pack function, but does not define unpack\n""hint: declare unpacker like this:\n> fun " + receiver_name + ".unpackFromSlice(mutate s: slice): " + receiver_name); } } if (f.f_unpack) { - if (!f.f_unpack->is_inlined_in_place()) { - return CantSerializeBecause("because `" + receiver_name + ".unpackFromSlice()` can't be inlined; probably, it contains `return` in the middle"); - } if (is_pack && !f.f_pack) { return CantSerializeBecause("because type `" + receiver_name + "` defines a custom unpack function, but does not define pack\n""hint: declare packer like this:\n> fun " + receiver_name + ".packToBuilder(self, mutate b: builder)"); } @@ -83,13 +78,21 @@ class PackUnpackAvailabilityChecker { } public: - explicit PackUnpackAvailabilityChecker(std::vector* out_un_pack_candidates) - : out_un_pack_candidates(out_un_pack_candidates) {} + PackUnpackAvailabilityChecker() = default; + PackUnpackAvailabilityChecker(std::vector* out_un_pack_candidates, std::vector* out_un_pack_functions) + : out_un_pack_candidates(out_un_pack_candidates), out_un_pack_functions(out_un_pack_functions) {} + + // This class is also used only to collect custom pack/unpack methods, either for generic instantiation + // during type inference or for the call graph. In that mode, skip checks that estimate finalized binary layout. + bool is_collecting_only() const { + return out_un_pack_candidates != nullptr || out_un_pack_functions != nullptr; + } - // During type inference, this class is also used only to collect custom pack/unpack methods - // that need generic instantiation. In that mode, skip checks that estimate finalized binary layout. - bool is_collecting_out_candidates_only() const { - return out_un_pack_candidates != nullptr; + void collect_custom_serializer(CustomPackUnpackF f, bool is_pack) const { + FunctionPtr f_serializer = is_pack ? f.f_pack : f.f_unpack; + if (out_un_pack_functions && f_serializer) { + out_un_pack_functions->push_back(f_serializer); + } } std::optional detect_why_cant_serialize(TypePtr any_type, bool is_pack, int cell_ref_depth) { @@ -119,8 +122,11 @@ class PackUnpackAvailabilityChecker { } if (const auto* t_map = any_type->try_as()) { - detect_why_cant_serialize(t_map->TKey, is_pack, cell_ref_depth); // collect out_un_pack_candidates if custom values - detect_why_cant_serialize(t_map->TValue, is_pack, cell_ref_depth); + // a map is already serialized: K/V serializers are checked, but are not called for the map itself + if (!out_un_pack_functions) { + detect_why_cant_serialize(t_map->TKey, is_pack, cell_ref_depth); // collect out_un_pack_candidates if custom values + detect_why_cant_serialize(t_map->TValue, is_pack, cell_ref_depth); + } return {}; } @@ -128,7 +134,8 @@ class PackUnpackAvailabilityChecker { StructPtr struct_ref = t_struct->struct_ref; if (CustomPackUnpackF f = get_custom_pack_unpack_function(t_struct, out_un_pack_candidates)) { - return is_collecting_out_candidates_only() ? std::nullopt : check_custom_pack_unpack(t_struct, f, is_pack); + collect_custom_serializer(f, is_pack); + return is_collecting_only() ? std::nullopt : check_custom_pack_unpack(t_struct, f, is_pack); } if (struct_ref->is_instantiation_of_CellT()) { @@ -162,7 +169,8 @@ class PackUnpackAvailabilityChecker { if (const auto* t_enum = any_type->try_as()) { if (CustomPackUnpackF f = get_custom_pack_unpack_function(t_enum, out_un_pack_candidates)) { - return is_collecting_out_candidates_only() ? std::nullopt : check_custom_pack_unpack(t_enum, f, is_pack); + collect_custom_serializer(f, is_pack); + return is_collecting_only() ? std::nullopt : check_custom_pack_unpack(t_enum, f, is_pack); } if (t_enum->enum_ref->members.empty()) { @@ -187,7 +195,7 @@ class PackUnpackAvailabilityChecker { return CantSerializeBecause("because variant #" + std::to_string(i + 1) + " of type `" + variant->as_human_readable() + "` can't be serialized", why.value()); } } - if (is_collecting_out_candidates_only()) { + if (is_collecting_only()) { return {}; } if (t_union->or_null == TypeDataVoid::create()) { @@ -232,7 +240,7 @@ class PackUnpackAvailabilityChecker { if (auto why = detect_why_cant_serialize(t_array->innerT, is_pack, cell_ref_depth)) { return CantSerializeBecause("because array of type `" + t_array->innerT->as_human_readable() + "` can't be serialized", why.value()); } - if (is_collecting_out_candidates_only()) { + if (is_collecting_only()) { return {}; } PackSize sizeT = estimate_serialization_size(t_array->innerT); @@ -251,7 +259,8 @@ class PackUnpackAvailabilityChecker { } if (CustomPackUnpackF f = get_custom_pack_unpack_function(t_alias, out_un_pack_candidates)) { - return is_collecting_out_candidates_only() ? std::nullopt : check_custom_pack_unpack(t_alias, f, is_pack); + collect_custom_serializer(f, is_pack); + return is_collecting_only() ? std::nullopt : check_custom_pack_unpack(t_alias, f, is_pack); } if (auto why = detect_why_cant_serialize(t_alias->underlying_type, is_pack, cell_ref_depth)) { @@ -288,8 +297,8 @@ class PackUnpackAvailabilityChecker { } }; -bool check_struct_can_be_packed_or_unpacked(TypePtr any_type, bool is_pack, std::string* because_msg, std::vector* out_un_pack_candidates) { - PackUnpackAvailabilityChecker checker(out_un_pack_candidates); +bool check_struct_can_be_packed_or_unpacked(TypePtr any_type, bool is_pack, std::string* because_msg) { + PackUnpackAvailabilityChecker checker; if (auto why = checker.detect_why_cant_serialize(any_type, is_pack, 0)) { if (because_msg != nullptr) { *because_msg = why.value().because_msg; @@ -299,6 +308,15 @@ bool check_struct_can_be_packed_or_unpacked(TypePtr any_type, bool is_pack, std: return true; } +void collect_recursive_pack_unpack_when_f_called(FunctionPtr called_f, std::vector* out_generic_candidates, std::vector* out_un_pack_functions) { + TypePtr serialized_type = nullptr; + bool is_pack = false; + PackUnpackAvailabilityChecker checker(out_generic_candidates, out_un_pack_functions); + if (is_serialization_builtin_function(called_f, &serialized_type, &is_pack)) { + checker.detect_why_cant_serialize(serialized_type, is_pack, 0); + } +} + static int calc_offset_on_stack(StructPtr struct_ref, int field_idx) { int stack_offset = 0; for (int i = 0; i < field_idx; ++i) { @@ -325,6 +343,26 @@ bool is_serialization_builtin_function(FunctionPtr fun_ref, TypePtr* serialized_ *is_pack = f_name == "T.toCell" || f_name == "builder.storeAny" || f_name == "reflect.estimateSerializationOf" || f_name == "createMessage" || f_name == "createExternalLogMessage"; return true; } + + // `map.set(k, v)` implicitly invokes `packToBuilder` for v if defined + bool is_map_builtin = fun_ref->receiver_type && fun_ref->receiver_type->try_as(); + if (const TypeDataMapKV* t_map = is_map_builtin ? fun_ref->receiver_type->try_as() : nullptr) { + // mutating methods, like `set` and `setAndGetPrevious`, which take K and V params, they pack v + if (fun_ref->does_mutate_self() && fun_ref->get_num_params() > 2) { + *serialized_type = t_map->TValue; + *is_pack = true; + return true; + } + // reads, like `map.get`, return a raw slice on a stack, the user calls `result.loadValue()` after; + // exception: `map.mustGet` immediately unpacks v, does not return MapLookupResult + if (fun_ref->declared_return_type->equal_to(t_map->TValue) && t_map->TValue != TypeDataSlice::create()) { + *serialized_type = t_map->TValue; + *is_pack = false; + return true; + } + // map keys cannot have custom serializers (binary width must be constant) + } + return false; } @@ -427,7 +465,7 @@ void generate_lazy_struct_from_slice(CodeBlob& code, AnyV origin, const LazyVari tolk_assert(loaded_state && !loaded_state->was_loaded_once()); loaded_state->mutate()->on_started_loading(hidden_struct); - UnpackContext ctx(code, origin, lazy_variable->ir_slice, lazy_variable->ir_options); + UnpackContext ctx(code, origin, lazy_variable->ir_slice, lazy_variable->ir_options, GeneratedLoadPolicy::AllowRemoving); if (hidden_struct->opcode.exists()) { ctx.loadAndCheckOpcode(hidden_struct->opcode); @@ -517,15 +555,14 @@ std::vector generate_lazy_struct_to_cell(CodeBlob& code, AnyV origin, return rvect_cell; } -std::vector generate_lazy_match_for_union(CodeBlob& code, AnyV origin, TypePtr union_type, const LazyVariableLoadedState* lazy_variable, const LazyMatchOptions& options) { +void generate_lazy_match_for_union(CodeBlob& code, AnyV origin, TypePtr union_type, const LazyVariableLoadedState* lazy_variable, const LazyMatchOptions& options) { tolk_assert(lazy_variable->ir_options.size() == 2); if (options.match_blocks.empty()) { // empty `match` statement, no arms - return {}; + return; } - UnpackContext ctx(code, origin, lazy_variable->ir_slice, lazy_variable->ir_options); - std::vector rvect_match = ctx.generate_lazy_match_any(union_type, options); - - return rvect_match; + UnpackContext ctx(code, origin, lazy_variable->ir_slice, lazy_variable->ir_options, GeneratedLoadPolicy::AllowRemoving); + ctx.generate_lazy_match_any(union_type, options); + // return void: lazy match generator called a callback which updated ir_match_result of ast_match_expression } std::vector generate_lazy_object_finish_loading(CodeBlob& code, AnyV origin, const LazyVariableLoadedState* lazy_variable, std::vector&& ir_obj) { @@ -539,7 +576,8 @@ std::vector generate_lazy_object_finish_loading(CodeBlob& code, AnyV static_cast(ir_obj); std::vector ir_slice = code.create_tmp_var(TypeDataSlice::create(), origin, "(lazy-slice)"); - code.add_let(origin, ir_slice, lazy_variable->ir_slice); + // pass force_keep true + code.add_let(origin, ir_slice, lazy_variable->ir_slice, true); return ir_slice; } diff --git a/tolk/pack-unpack-api.h b/tolk/pack-unpack-api.h index c55bbd866c..c361eabf47 100644 --- a/tolk/pack-unpack-api.h +++ b/tolk/pack-unpack-api.h @@ -24,7 +24,8 @@ namespace tolk { struct MethodCallCandidate; bool is_serialization_builtin_function(FunctionPtr fun_ref, TypePtr* serialized_type, bool* is_pack); -bool check_struct_can_be_packed_or_unpacked(TypePtr any_type, bool is_pack, std::string* because_msg, std::vector* out_un_pack_candidates = nullptr); +bool check_struct_can_be_packed_or_unpacked(TypePtr any_type, bool is_pack, std::string* because_msg); +void collect_recursive_pack_unpack_when_f_called(FunctionPtr called_f, std::vector* out_generic_candidates, std::vector* out_un_pack_functions); PackSize estimate_serialization_size(TypePtr any_type); // functions like T.toCell() are not declared here: they are implemented in a .cpp file, @@ -35,8 +36,8 @@ struct LazyStructLoadedState; struct LazyVariableLoadedState; void generate_lazy_struct_from_slice(CodeBlob& code, AnyV origin, const LazyVariableLoadedState* lazy_variable, const LazyStructLoadInfo& load_info, const std::vector& ir_obj); +void generate_lazy_match_for_union(CodeBlob& code, AnyV origin, TypePtr union_type, const LazyVariableLoadedState* lazy_variable, const LazyMatchOptions& options); std::vector generate_lazy_struct_to_cell(CodeBlob& code, AnyV origin, const LazyStructLoadedState* loaded_state, std::vector&& ir_obj, const std::vector& ir_options); -std::vector generate_lazy_match_for_union(CodeBlob& code, AnyV origin, TypePtr union_type, const LazyVariableLoadedState* lazy_variable, const LazyMatchOptions& options); std::vector generate_lazy_object_finish_loading(CodeBlob& code, AnyV origin, const LazyVariableLoadedState* lazy_variable, std::vector&& ir_obj); } // namespace tolk diff --git a/tolk/pack-unpack-serializers.cpp b/tolk/pack-unpack-serializers.cpp index 717f8c7c10..601bd88494 100644 --- a/tolk/pack-unpack-serializers.cpp +++ b/tolk/pack-unpack-serializers.cpp @@ -38,48 +38,17 @@ namespace tolk { class LValContext; -std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, TypePtr target_type = nullptr, LValContext* lval_ctx = nullptr); -std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* debug_desc); +std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* purpose); std::vector transition_to_target_type(std::vector&& rvect, CodeBlob& code, TypePtr original_type, TypePtr target_type, AnyV origin); -std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, bool is_before_immediate_return, const std::vector>& vars_per_arg); - -// Unlike regular methods, custom serializers must not leak from aliases to underlying types. -// Example: -// > struct Box { ... } -// > type BoxAlias = Box -// > fun BoxAlias.packToBuilder -// It must NOT be auto-applicable to `Box`. Whereas for other methods, like `fun BoxAlias.xyz`, `box.xyz()` is okay. -static bool is_same_custom_serialization_receiver(TypePtr receiver_type, TypePtr candidate_receiver) { - if (const TypeDataAlias* t_alias = receiver_type->try_as()) { - const TypeDataAlias* c_alias = candidate_receiver->try_as(); - return c_alias && c_alias->alias_ref == t_alias->alias_ref; - } - if (receiver_type->try_as()) { - return candidate_receiver->try_as() && candidate_receiver->equal_to(receiver_type); - } - if (receiver_type->try_as()) { - return candidate_receiver->try_as() && candidate_receiver->equal_to(receiver_type); - } - return false; -} - -static std::vector filter_pack_candidates(TypePtr receiver_type, const std::vector& candidates) { - int min_generics = 100; - for (const MethodCallCandidate& c : candidates) { - if (is_same_custom_serialization_receiver(receiver_type, c.instantiated_receiver)) { - int n_generics = c.is_generic() ? c.substitutedTs.size() : 0; - min_generics = std::min(min_generics, n_generics); - } - } +std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, const std::vector>& vars_per_arg); - std::vector filtered; - for (MethodCallCandidate c : candidates) { - int n_generics = c.is_generic() ? c.substitutedTs.size() : 0; - if (n_generics == min_generics && is_same_custom_serialization_receiver(receiver_type, c.instantiated_receiver)) { - filtered.emplace_back(std::move(c)); - } +// make an error "ambiguous method" enumerating all available candidates +static Error err_ambiguous_pack_unpack_method(const std::vector& applicable) { + Error diagnostic = err("ambiguous method, several serializers are applicable"); + for (size_t i = 1; i < applicable.size(); ++i) { // from 1, because primary is 0 + diagnostic.with_secondary(applicable[i].method_ref, "this method is also applicable"); } - return filtered; + return diagnostic; } // Any type alias or struct can have custom pack/unpack functions declared: @@ -102,26 +71,21 @@ CustomPackUnpackF get_custom_pack_unpack_function(TypePtr receiver_type, std::ve std::vector c_pack = resolve_methods_for_call(receiver_type, "packToBuilder", false); std::vector c_unpack = resolve_methods_for_call(receiver_type, "unpackFromSlice", false); - c_pack = filter_pack_candidates(receiver_type, c_pack); if (c_pack.size() > 1) { - err("ambiguous method, both `{}` and `{}` are applicable", c_pack[0].method_ref, c_pack[1].method_ref).fire(c_pack[0].method_ref->ident_anchor); + err_ambiguous_pack_unpack_method(c_pack).fire(c_pack[0].method_ref); } if (!c_pack.empty()) { - f.f_pack = c_pack[0].method_ref; + if (!c_pack[0].method_ref->is_generic_function() || c_pack[0].substitutedTs.all_Ts_initialized()) { + f.f_pack = c_pack[0].method_ref; + } } - c_unpack = filter_pack_candidates(receiver_type, c_unpack); if (c_unpack.size() > 1) { - err("ambiguous method, both `{}` and `{}` are applicable", c_unpack[0].method_ref, c_unpack[1].method_ref).fire(c_unpack[0].method_ref->ident_anchor); + err_ambiguous_pack_unpack_method(c_unpack).fire(c_unpack[0].method_ref); } if (!c_unpack.empty()) { - f.f_unpack = c_unpack[0].method_ref; - } - - // for `type A = B`, if `A` does not declare its own serializers, take them from B - if (!f) { - if (const TypeDataAlias* t_alias = receiver_type->try_as()) { - f = get_custom_pack_unpack_function(t_alias->underlying_type, out_candidates); + if (!c_unpack[0].method_ref->is_generic_function() || c_unpack[0].substitutedTs.all_Ts_initialized()) { + f.f_unpack = c_unpack[0].method_ref; } } @@ -152,66 +116,67 @@ PackContext::PackContext(CodeBlob& code, AnyV origin, std::vector ir_ void PackContext::storeInt(var_idx_t ir_idx, int len) const { std::vector args = { ir_builder0, ir_idx, code.create_int(origin, len, "(storeW)") }; - code.add_call(origin, ir_builder, std::move(args), f_storeInt); + code.add_call(origin, ir_builder, std::move(args), f_storeInt, /* force_keep = */ true); } void PackContext::storeUint(var_idx_t ir_idx, int len) const { std::vector args = { ir_builder0, ir_idx, code.create_int(origin, len, "(storeW)") }; - code.add_call(origin, ir_builder, std::move(args), f_storeUint); + code.add_call(origin, ir_builder, std::move(args), f_storeUint, /* force_keep = */ true); } void PackContext::storeUint_var(var_idx_t ir_idx, var_idx_t ir_len) const { std::vector args = { ir_builder0, ir_idx, ir_len }; - code.add_call(origin, ir_builder, std::move(args), f_storeUint); + code.add_call(origin, ir_builder, std::move(args), f_storeUint, /* force_keep = */ true); } void PackContext::storeBool(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeBool")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeBool"), /* force_keep = */ true); } void PackContext::storeCoins(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeCoins")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeCoins"), /* force_keep = */ true); } void PackContext::storeRef(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeRef")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeRef"), /* force_keep = */ true); } void PackContext::storeMaybeRef(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeMaybeRef")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeMaybeRef"), /* force_keep = */ true); } void PackContext::storeAddressInt(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeAddress")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeAddress"), /* force_keep = */ true); } void PackContext::storeAddressAny(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeAddressAny")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeAddressAny"), /* force_keep = */ true); } void PackContext::storeBuilder(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeBuilder")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeBuilder"), /* force_keep = */ true); } void PackContext::storeSlice(var_idx_t ir_idx) const { std::vector args = { ir_builder0, ir_idx }; - code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeSlice")); + code.add_call(origin, ir_builder, std::move(args), lookup_function("builder.storeSlice"), /* force_keep = */ true); } void PackContext::storeOpcode(PackOpcode opcode) const { std::vector args = { ir_builder0, code.create_int(origin, opcode.pack_prefix, "(struct-prefix)"), code.create_int(origin, opcode.prefix_len, "(storeW)") }; - code.add_call(origin, ir_builder, std::move(args), f_storeUint); + code.add_call(origin, ir_builder, std::move(args), f_storeUint, /* force_keep = */ true); } -UnpackContext::UnpackContext(CodeBlob& code, AnyV origin, std::vector ir_slice, std::vector ir_options) +UnpackContext::UnpackContext(CodeBlob& code, AnyV origin, std::vector ir_slice, std::vector ir_options, + GeneratedLoadPolicy generated_load_policy) : code(code) , origin(origin) , f_loadInt(lookup_function("slice.loadInt")) @@ -219,70 +184,71 @@ UnpackContext::UnpackContext(CodeBlob& code, AnyV origin, std::vector , f_skipBits(lookup_function("slice.skipBits")) , ir_options(std::move(ir_options)) , ir_slice(std::move(ir_slice)) - , ir_slice0(this->ir_slice[0]) { + , ir_slice0(this->ir_slice[0]) + , generated_load_policy(generated_load_policy) { } -std::vector UnpackContext::loadInt(int len, const char* debug_desc) const { +std::vector UnpackContext::loadInt(int len, const char* purpose) const { std::vector args = { ir_slice0, code.create_int(origin, len, "(loadW)") }; - std::vector result = code.create_tmp_var(TypeDataInt::create(), origin, debug_desc); - code.add_call(origin, {ir_slice0, result[0]}, std::move(args), f_loadInt); + std::vector result = code.create_tmp_var(TypeDataInt::create(), origin, purpose); + code.add_call(origin, {ir_slice0, result[0]}, std::move(args), f_loadInt, force_keep_arg()); return result; } -std::vector UnpackContext::loadUint(int len, const char* debug_desc) const { +std::vector UnpackContext::loadUint(int len, const char* purpose) const { std::vector args = { ir_slice0, code.create_int(origin, len, "(loadW)") }; - std::vector result = code.create_tmp_var(TypeDataInt::create(), origin, debug_desc); - code.add_call(origin, {ir_slice0, result[0]}, std::move(args), f_loadUint); + std::vector result = code.create_tmp_var(TypeDataInt::create(), origin, purpose); + code.add_call(origin, {ir_slice0, result[0]}, std::move(args), f_loadUint, force_keep_arg()); return result; } -std::vector UnpackContext::loadRef(const char* debug_desc) const { +std::vector UnpackContext::loadRef(const char* purpose) const { std::vector args = ir_slice; - std::vector result = code.create_tmp_var(TypeDataCell::create(), origin, debug_desc); - code.add_call(origin, {ir_slice0, result[0]}, std::move(args), lookup_function("slice.loadRef")); + std::vector result = code.create_tmp_var(TypeDataCell::create(), origin, purpose); + code.add_call(origin, {ir_slice0, result[0]}, std::move(args), lookup_function("slice.loadRef"), force_keep_arg()); return result; } -std::vector UnpackContext::loadMaybeRef(const char* debug_desc) const { +std::vector UnpackContext::loadMaybeRef(const char* purpose) const { std::vector args = ir_slice; - std::vector ir_result = code.create_tmp_var(TypeDataCell::create(), origin, debug_desc); - code.add_call(origin, {ir_slice0, ir_result[0]}, std::move(args), lookup_function("slice.loadMaybeRef")); + std::vector ir_result = code.create_tmp_var(TypeDataCell::create(), origin, purpose); + code.add_call(origin, {ir_slice0, ir_result[0]}, std::move(args), lookup_function("slice.loadMaybeRef"), force_keep_arg()); return ir_result; } void UnpackContext::loadAndCheckOpcode(PackOpcode opcode) const { std::vector ir_prefix_eq = code.create_tmp_var(TypeDataInt::create(), origin, "(prefix-eq)"); std::vector args = { ir_slice0, code.create_int(origin, opcode.pack_prefix, "(pack-prefix)"), code.create_int(origin, opcode.prefix_len, "(prefix-len)") }; - code.add_call(origin, {ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("slice.tryStripPrefix")); + code.add_call(origin, {ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("__tryStripPrefix"), force_keep_arg()); std::vector args_throwifnot = { option_throwIfOpcodeDoesNotMatch(), ir_prefix_eq[0] }; code.add_call(origin, {}, std::move(args_throwifnot), lookup_function("__throw_ifnot")); } void UnpackContext::skipBits(int len) const { std::vector args = { ir_slice0, code.create_int(origin, len, "(skipW)") }; - code.add_call(origin, ir_slice, std::move(args), f_skipBits); + code.add_call(origin, ir_slice, std::move(args), f_skipBits, force_keep_arg()); } void UnpackContext::skipBits_var(var_idx_t ir_len) const { std::vector args = { ir_slice0, ir_len }; - code.add_call(origin, ir_slice, std::move(args), f_skipBits); + code.add_call(origin, ir_slice, std::move(args), f_skipBits, force_keep_arg()); } void UnpackContext::skipRef() const { std::vector args = ir_slice; std::vector dummy_loaded = code.create_tmp_var(TypeDataCell::create(), origin, "(loaded-cell)"); - code.add_call(origin, {ir_slice0, dummy_loaded[0]}, std::move(args), lookup_function("slice.loadRef")); + code.add_call(origin, {ir_slice0, dummy_loaded[0]}, std::move(args), lookup_function("slice.loadRef"), force_keep_arg()); } void UnpackContext::skipMaybeRef() const { - code.add_call(origin, ir_slice, ir_slice, lookup_function("slice.skipMaybeRef")); + code.add_call(origin, ir_slice, ir_slice, lookup_function("slice.skipMaybeRef"), force_keep_arg()); } void UnpackContext::assertEndIfOption() const { Op& if_assertEnd = code.add_if_else(origin, {option_assertEndAfterReading()}); { code.push_set_cur(if_assertEnd.block0); - code.add_call(origin, {}, ir_slice, lookup_function("slice.assertEnd")); + code.add_call(origin, {}, ir_slice, lookup_function("slice.assertEnd"), force_keep_arg()); code.close_pop_cur(origin); } { @@ -305,18 +271,6 @@ const LazyMatchOptions::MatchBlock* LazyMatchOptions::find_match_block(TypePtr v tolk_assert(false); } -void LazyMatchOptions::save_match_result_on_arm_end(CodeBlob& code, AnyV origin, const MatchBlock* arm_block, std::vector&& ir_arm_result, const std::vector& ir_match_expr_result) const { - if (!is_statement) { - // if it's `match` expression (not statement), then every arm has a result, assigned to a whole `match` result - ir_arm_result = transition_to_target_type(std::move(ir_arm_result), code, arm_block->block_expr_type, match_expr_type, origin); - code.add_let(origin, ir_match_expr_result, std::move(ir_arm_result)); - } else if (add_return_to_all_arms) { - // if it's `match` statement, even if an arm is an expression, it's void, actually - // moreover, if it's the last statement in a function, add implicit "return" to all match cases to produce IFJMP - code.add_return(origin, {}, code.fun_ref); - } -} - // -------------------------------------------- // serializers with pack/unpack/skip/estimate @@ -379,16 +333,16 @@ struct S_VariadicIntN final : ISerializer { : n_bits(n_bits), is_unsigned(is_unsigned) {} void pack(const PackContext* ctx, CodeBlob& code, AnyV origin, std::vector&& rvect) override { - FunctionPtr f_storeVarInt = lookup_function("builder.__storeVarInt"); + FunctionPtr f_storeVarInt = lookup_function("__storeVarInt"); std::vector args = { ctx->ir_builder0, rvect[0], code.create_int(origin, n_bits, "(n-bits)"), code.create_int(origin, is_unsigned, "(is-unsigned)") }; - code.add_call(origin, ctx->ir_builder, std::move(args), f_storeVarInt); + code.add_call(origin, ctx->ir_builder, std::move(args), f_storeVarInt, /* force_keep = */ true); } std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { - FunctionPtr f_loadVarInt = lookup_function("slice.__loadVarInt"); + FunctionPtr f_loadVarInt = lookup_function("__loadVarInt"); std::vector args = { ctx->ir_slice0, code.create_int(origin, n_bits, "(n-bits)"), code.create_int(origin, is_unsigned, "(is-unsigned)") }; std::vector result = code.create_tmp_var(TypeDataInt::create(), origin, "(loaded-varint)"); - code.add_call(origin, {ctx->ir_slice0, result[0]}, std::move(args), f_loadVarInt); + code.add_call(origin, {result[0], ctx->ir_slice0}, std::move(args), f_loadVarInt, ctx->force_keep_arg()); return result; } @@ -409,8 +363,8 @@ struct S_VariadicIntN final : ISerializer { struct S_BitsN final : ISerializer { const int n_bits; - explicit S_BitsN(int n_width, bool is_bits) - : n_bits(is_bits ? n_width : n_width * 8) {} + explicit S_BitsN(int n_bits) + : n_bits(n_bits) {} void pack(const PackContext* ctx, CodeBlob& code, AnyV origin, std::vector&& rvect) override { tolk_assert(rvect.size() == 1); @@ -442,7 +396,7 @@ struct S_BitsN final : ISerializer { FunctionPtr f_loadBits = lookup_function("slice.loadBits"); std::vector args = { ctx->ir_slice0, code.create_int(origin, n_bits, "(loadW)") }; std::vector ir_result = code.create_tmp_var(TypeDataSlice::create(), origin, "(loaded-slice)"); - code.add_call(origin, {ctx->ir_slice0, ir_result[0]}, std::move(args), f_loadBits); + code.add_call(origin, {ctx->ir_slice0, ir_result[0]}, std::move(args), f_loadBits, ctx->force_keep_arg()); return ir_result; } @@ -541,7 +495,7 @@ struct S_Coins final : ISerializer { FunctionPtr f_loadCoins = lookup_function("slice.loadCoins"); std::vector args = ctx->ir_slice; std::vector ir_result = code.create_tmp_var(TypeDataInt::create(), origin, "(loaded-coins)"); - code.add_call(origin, {ctx->ir_slice0, ir_result[0]}, std::move(args), f_loadCoins); + code.add_call(origin, {ctx->ir_slice0, ir_result[0]}, std::move(args), f_loadCoins, ctx->force_keep_arg()); return ir_result; } @@ -564,7 +518,7 @@ struct S_AddressInt final : ISerializer { std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { FunctionPtr f_loadAddress = lookup_function("slice.loadAddress"); std::vector ir_address = code.create_tmp_var(TypeDataSlice::create(), origin, "(loaded-addr)"); - code.add_call(origin, {ctx->ir_slice0, ir_address[0]}, ctx->ir_slice, f_loadAddress); + code.add_call(origin, {ctx->ir_slice0, ir_address[0]}, ctx->ir_slice, f_loadAddress, ctx->force_keep_arg()); return ir_address; } @@ -585,13 +539,13 @@ struct S_AddressIntOrNull final : ISerializer { // `address?`, when null, is stored as '00' (addr_none), so `address?` is not TL/B (Maybe MsgAddressInt) tolk_assert(rvect.size() == 1); std::vector args = { ctx->ir_builder0, rvect[0] }; - code.add_call(origin, ctx->ir_builder, std::move(args), lookup_function("builder.storeAddressOpt")); + code.add_call(origin, ctx->ir_builder, std::move(args), lookup_function("builder.storeAddressOpt"), /* force_keep = */ true); } std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { FunctionPtr f_loadAddressOpt = lookup_function("slice.loadAddressOpt"); std::vector ir_address_orN = code.create_tmp_var(TypeDataSlice::create(), origin, "(loaded-addr)"); - code.add_call(origin, {ctx->ir_slice0, ir_address_orN[0]}, ctx->ir_slice, f_loadAddressOpt); + code.add_call(origin, {ctx->ir_slice0, ir_address_orN[0]}, ctx->ir_slice, f_loadAddressOpt, ctx->force_keep_arg()); return ir_address_orN; } @@ -613,7 +567,7 @@ struct S_AddressAny final : ISerializer { std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { FunctionPtr f_loadAddressAny = lookup_function("slice.loadAddressAny"); std::vector ir_address_any = code.create_tmp_var(TypeDataSlice::create(), origin, "(loaded-addr)"); - code.add_call(origin, {ctx->ir_slice0, ir_address_any[0]}, ctx->ir_slice, f_loadAddressAny); + code.add_call(origin, {ctx->ir_slice0, ir_address_any[0]}, ctx->ir_slice, f_loadAddressAny, ctx->force_keep_arg()); return ir_address_any; } @@ -652,7 +606,7 @@ struct S_Builder final : ISerializer { void pack(const PackContext* ctx, CodeBlob& code, AnyV origin, std::vector&& rvect) override { tolk_assert(rvect.size() == 1); std::vector args = { ctx->ir_builder0, rvect[0] }; - code.add_call(origin, ctx->ir_builder, std::move(args), lookup_function("builder.storeBuilder")); + code.add_call(origin, ctx->ir_builder, std::move(args), lookup_function("builder.storeBuilder"), /* force_keep = */ true); } std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { @@ -844,35 +798,31 @@ struct S_Either final : ISerializer { return ir_result; } - std::vector lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { + void lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { for (const LazyMatchOptions::MatchBlock& m : options.match_blocks) { if (m.arm_variant == nullptr) { // `else => ...` not allowed for Either // it's not the best place to fire an error, but let it be - err("`else` is unreachable, because this `match` has only two options (0/1 prefixes)").fire(SrcRange::empty_at_start(m.v_body->range)); + err("`else` is unreachable, because this `match` has only two options (0/1 prefixes)").fire(SrcRange::empty_at_start(m.v_arm->range)); } } tolk_assert(options.match_blocks.size() == 2); - std::vector ir_result = code.create_tmp_var(options.match_expr_type, origin, "(match-expression)"); std::vector ir_prefix_eq = code.create_tmp_var(TypeDataInt::create(), origin, "(prefix-eq)"); std::vector args = { ctx->ir_slice0, code.create_int(origin, 1, "(pack-prefix)"), code.create_int(origin, 1, "(prefix-len)") }; - code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("slice.tryStripPrefix")); + code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("__tryStripPrefix"), ctx->force_keep_arg()); Op& if_op = code.add_if_else(origin, ir_prefix_eq); { code.push_set_cur(if_op.block0); const LazyMatchOptions::MatchBlock* m_block = options.find_match_block(t_right); - std::vector ith_result = pre_compile_expr(m_block->v_body, code); - options.save_match_result_on_arm_end(code, origin, m_block, std::move(ith_result), ir_result); + options.lower_match_arm(m_block->v_arm, code); code.close_pop_cur(origin); } { code.push_set_cur(if_op.block1); ctx->loadAndCheckOpcode(PackOpcode(0, 1)); const LazyMatchOptions::MatchBlock* m_block = options.find_match_block(t_left); - std::vector ith_result = pre_compile_expr(m_block->v_body, code); - options.save_match_result_on_arm_end(code, origin, m_block, std::move(ith_result), ir_result); + options.lower_match_arm(m_block->v_arm, code); code.close_pop_cur(origin); } - return ir_result; } void skip(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { @@ -964,12 +914,12 @@ struct S_MultipleConstructors final : ISerializer { return ir_result; } - FunctionPtr f_tryStripPrefix = lookup_function("slice.tryStripPrefix"); + FunctionPtr f_tryStripPrefix = lookup_function("__tryStripPrefix"); std::vector ir_prefix_eq = code.create_tmp_var(TypeDataInt::create(), origin, "(prefix-eq)"); for (int i = 0; i < t_union->size() - has_void(); ++i) { TypePtr variant = t_union->variants[i]; std::vector args = { ctx->ir_slice0, code.create_int(origin, opcodes[i].pack_prefix, "(pack-prefix)"), code.create_int(origin, opcodes[i].prefix_len, "(prefix-len)") }; - code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), f_tryStripPrefix); + code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), f_tryStripPrefix, ctx->force_keep_arg()); Op& if_prefix_eq = code.add_if_else(origin, ir_prefix_eq); code.push_set_cur(if_prefix_eq.block0); std::vector ith_rvect = ctx->generate_unpack_any(variant, PrefixReadMode::DoNothingAlreadyLoaded); @@ -997,12 +947,12 @@ struct S_MultipleConstructors final : ISerializer { return ir_result; } - std::vector lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { + void lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { std::vector opcodes_order_mapping(t_union->size(), -1); const LazyMatchOptions::MatchBlock* else_block = nullptr; for (int i = 0; i < static_cast(options.match_blocks.size()); ++i) { if (options.match_blocks[i].arm_variant) { - int variant_idx = t_union->get_variant_idx(options.match_blocks[i].arm_variant); + int variant_idx = t_union->get_variant_equal_to(options.match_blocks[i].arm_variant); tolk_assert(variant_idx != -1); opcodes_order_mapping[i] = variant_idx; } else { @@ -1011,16 +961,14 @@ struct S_MultipleConstructors final : ISerializer { } } - FunctionPtr f_tryStripPrefix = lookup_function("slice.tryStripPrefix"); - - std::vector ir_result = code.create_tmp_var(options.match_expr_type, origin, "(match-expression)"); + FunctionPtr f_tryStripPrefix = lookup_function("__tryStripPrefix"); std::vector ir_prefix_eq = code.create_tmp_var(TypeDataInt::create(), origin, "(prefix-eq)"); for (int i = 0; i < t_union->size(); ++i) { StructData::PackOpcode opcode = opcodes[opcodes_order_mapping[i]]; tolk_assert(opcode.prefix_len > 0); // prefix_len == 0 only for `void`, not allowed in lazy std::vector args = { ctx->ir_slice0, code.create_int(origin, opcode.pack_prefix, "(pack-prefix)"), code.create_int(origin, opcode.prefix_len, "(prefix-len)") }; - code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), f_tryStripPrefix); + code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), f_tryStripPrefix, ctx->force_keep_arg()); Op& if_op = code.add_if_else(origin, ir_prefix_eq); code.push_set_cur(if_op.block0); if (options.lazy_var_ref && options.match_blocks[i].arm_variant) { @@ -1034,25 +982,21 @@ struct S_MultipleConstructors final : ISerializer { .smart_cast_type = variant_ty, .ir_slots = std::move(ir_variant), }); - code.add_extra_mark_location(options.match_blocks[i].v_body->range); + code.add_extra_mark_location(options.match_blocks[i].v_arm->range); } - std::vector ith_result = pre_compile_expr(options.match_blocks[i].v_body, code); - options.save_match_result_on_arm_end(code, origin, &options.match_blocks[i], std::move(ith_result), ir_result); + options.lower_match_arm(options.match_blocks[i].v_arm, code); code.close_pop_cur(origin); code.push_set_cur(if_op.block1); // open ELSE } if (else_block) { - std::vector else_result = pre_compile_expr(else_block->v_body, code); - options.save_match_result_on_arm_end(code, origin, else_block, std::move(else_result), ir_result); + options.lower_match_arm(else_block->v_arm, code); } else { ctx->throwInvalidOpcode(); } for (int j = 0; j < t_union->size(); ++j) { code.close_pop_cur(origin); // close all outer IFs } - - return ir_result; } void skip(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { @@ -1203,7 +1147,7 @@ struct S_Array final : ISerializer { // var chunkB = beginCell().storeMaybeRef(tail); std::vector var_chunkB = code.create_tmp_var(TypeDataBuilder::create(), origin, "(var-chunkB)"); code.add_call(origin, var_chunkB, {}, lookup_function("beginCell")); - code.add_call(origin, var_chunkB, {var_chunkB[0], var_tail[0]}, lookup_function("builder.storeMaybeRef")); + code.add_call(origin, var_chunkB, {var_chunkB[0], var_tail[0]}, lookup_function("builder.storeMaybeRef"), /* force_keep = */ true); // var curChunkSize = min(idx, chunkSize); std::vector var_curChunkSize = code.create_tmp_var(TypeDataInt::create(), origin, "(var-curChunkSize)"); code.add_call(origin, var_curChunkSize, {var_chunkSize[0], var_idx[0]}, lookup_function("min")); @@ -1257,7 +1201,7 @@ struct S_Array final : ISerializer { // var s = head.beginParse(); std::vector var_s = code.create_tmp_var(TypeDataSlice::create(), origin, "(var-s)"); code.add_call(origin, var_s, var_head, lookup_function("cell.beginParse")); - UnpackContext chunk_ctx(code, origin, var_s, ctx->ir_options); + UnpackContext chunk_ctx(code, origin, var_s, ctx->ir_options, ctx->generated_load_policy); // head = s.loadMaybeRef(); code.add_let(origin, var_head, chunk_ctx.loadMaybeRef("(var-snakeTail)")); // do { outArr.push(s.loadAny()); } while (!s.isEmpty()); @@ -1333,7 +1277,7 @@ struct S_CustomStruct final : ISerializer { return ir_struct; } - std::vector lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { + void lazy_match(const UnpackContext* ctx, CodeBlob& code, AnyV origin, const LazyMatchOptions& options) const { const LazyMatchOptions::MatchBlock* when_block = nullptr; // Point => ... const LazyMatchOptions::MatchBlock* else_block = nullptr; // else => ... for (const LazyMatchOptions::MatchBlock& match_block : options.match_blocks) { @@ -1345,35 +1289,30 @@ struct S_CustomStruct final : ISerializer { } } - std::vector ir_result = code.create_tmp_var(options.match_expr_type, origin, "(match-expression)"); std::vector ir_prefix_eq = code.create_tmp_var(TypeDataInt::create(), origin, "(prefix-eq)"); StructData::PackOpcode opcode = struct_ref->opcode; if (opcode.exists()) { // it's `match` over a struct (makes sense for a struct with prefix and `else` branch) std::vector args = { ctx->ir_slice0, code.create_int(origin, opcode.pack_prefix, "(pack-prefix)"), code.create_int(origin, opcode.prefix_len, "(prefix-len)") }; - code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("slice.tryStripPrefix")); + code.add_call(origin, {ctx->ir_slice0, ir_prefix_eq[0]}, std::move(args), lookup_function("__tryStripPrefix"), ctx->force_keep_arg()); } else { code.add_let(origin, ir_prefix_eq, {code.create_int(origin, -1, "(true)")}); } Op& if_op = code.add_if_else(origin, ir_prefix_eq); { code.push_set_cur(if_op.block0); - std::vector when_result = pre_compile_expr(when_block->v_body, code); - options.save_match_result_on_arm_end(code, origin, when_block, std::move(when_result), ir_result); + options.lower_match_arm(when_block->v_arm, code); code.close_pop_cur(origin); } { code.push_set_cur(if_op.block1); if (else_block) { - std::vector else_result = pre_compile_expr(else_block->v_body, code); - options.save_match_result_on_arm_end(code, origin, else_block, std::move(else_result), ir_result); + options.lower_match_arm(else_block->v_arm, code); } else { ctx->throwInvalidOpcode(); } code.close_pop_cur(origin); } - - return ir_result; } void skip(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { @@ -1487,16 +1426,29 @@ struct S_CustomReceiverForPackUnpack final : ISerializer { void pack(const PackContext* ctx, CodeBlob& code, AnyV origin, std::vector&& rvect) override { CustomPackUnpackF f = get_custom_pack_unpack_function(receiver_type); tolk_assert(f.f_pack && f.f_pack->does_accept_self() && f.f_pack->inferred_return_type->get_width_on_stack() == 0); - std::vector vars_per_arg = { std::move(rvect), ctx->ir_builder }; - std::vector ir_mutated_builder = gen_inline_fun_call_in_place(code, TypeDataBuilder::create(), origin, f.f_pack, nullptr, false, vars_per_arg); - code.add_let(origin, ctx->ir_builder, std::move(ir_mutated_builder)); + // call `fun receiver.packToBuilder(self, mutate b: builder): void`, rvect is self + if (f.f_pack->is_inlined_in_place()) { + std::vector vars_per_arg = { std::move(rvect), ctx->ir_builder }; + std::vector ir_mutated_builder = gen_inline_fun_call_in_place(code, TypeDataBuilder::create(), origin, f.f_pack, nullptr, vars_per_arg); + code.add_let(origin, ctx->ir_builder, std::move(ir_mutated_builder)); + } else { + rvect.push_back(ctx->ir_builder0); + code.add_call(origin, ctx->ir_builder, rvect, f.f_pack, /* force_keep = */ true); + } } std::vector unpack(const UnpackContext* ctx, CodeBlob& code, AnyV origin) override { CustomPackUnpackF f = get_custom_pack_unpack_function(receiver_type); tolk_assert(f.f_unpack && f.f_unpack->inferred_return_type->get_width_on_stack() == receiver_type->get_width_on_stack()); + // call `fun receiver.unpackFromSlice(mutate s: slice): receiver` TypePtr ret_type = TypeDataTensor::create({TypeDataSlice::create(), receiver_type}); - std::vector ir_slice_and_res = gen_inline_fun_call_in_place(code, ret_type, origin, f.f_unpack, nullptr, false, {ctx->ir_slice}); + std::vector ir_slice_and_res; + if (f.f_unpack->is_inlined_in_place()) { + ir_slice_and_res = gen_inline_fun_call_in_place(code, ret_type, origin, f.f_unpack, nullptr, {ctx->ir_slice}); + } else { + ir_slice_and_res = code.create_tmp_var(ret_type, origin, "(slice-and-res)"); + code.add_call(origin, ir_slice_and_res, ctx->ir_slice, f.f_unpack, ctx->force_keep_arg()); + } code.add_let(origin, ctx->ir_slice, {ir_slice_and_res.front()}); return std::vector(ir_slice_and_res.begin() + 1, ir_slice_and_res.end()); } @@ -1711,7 +1663,7 @@ static std::unique_ptr get_serializer_for_type(TypePtr any_type) { return std::make_unique(t_intN->n_bits, t_intN->is_unsigned); } if (const auto* t_bitsN = any_type->try_as()) { - return std::make_unique(t_bitsN->n_width, t_bitsN->is_bits); + return std::make_unique(t_bitsN->n_bits); } if (any_type == TypeDataCoins::create()) { return std::make_unique(); @@ -1851,7 +1803,7 @@ void UnpackContext::generate_skip_any(TypePtr any_type, PrefixReadMode prefix_mo this->prefix_mode = backup; } -std::vector UnpackContext::generate_lazy_match_any(TypePtr any_type, const LazyMatchOptions& options) const { +void UnpackContext::generate_lazy_match_any(TypePtr any_type, const LazyMatchOptions& options) const { std::unique_ptr serializer = get_serializer_for_type(any_type); if (auto* s = dynamic_cast(serializer.get())) { return s->lazy_match(this, code, origin, options); diff --git a/tolk/pack-unpack-serializers.h b/tolk/pack-unpack-serializers.h index 79ba933caa..9fe955d2af 100644 --- a/tolk/pack-unpack-serializers.h +++ b/tolk/pack-unpack-serializers.h @@ -18,6 +18,7 @@ #include "fwd-declarations.h" #include "tolk.h" +#include namespace tolk { @@ -59,6 +60,14 @@ enum class PrefixWriteMode { DoNothingAlreadyWritten, }; +// Operation-liveness for compiler-emitted load/skip calls inside unpack. +// Not a property of `slice.loadUint` itself: preserved for `fromSlice` but removable in `lazy fromSlice`. +// Pack always preserves generated `builder.storeXXX` (no policy there). +enum class GeneratedLoadPolicy { + AllowRemoving, + Preserve, +}; + class PackContext { CodeBlob& code; AnyV origin; @@ -102,18 +111,14 @@ enum class PrefixReadMode { struct LazyMatchOptions { struct MatchBlock { TypePtr arm_variant; // left of `V => ...`; nullptr for `else => ...` - AnyExprV v_body; // right of `V => ...` - TypePtr block_expr_type; // for match expression, if `V => expr`, it's expr's inferred_type + AnyV v_arm; // ast_match_arm `V => ...` }; - TypePtr match_expr_type; // type of `match` expression, `void` for statement - bool is_statement; // it's `match` statement, not expression, so it does not return any result - bool add_return_to_all_arms; // it's the last statement in a function, add "return" to its cases for better Fift code std::vector match_blocks; LocalVarPtr lazy_var_ref = nullptr; // for emitting MARK_SMART_CAST at the start of each arm + std::function lower_match_arm; const MatchBlock* find_match_block(TypePtr variant) const; - void save_match_result_on_arm_end(CodeBlob& code, AnyV origin, const MatchBlock* arm_block, std::vector&& ir_arm_result, const std::vector& ir_match_expr_result) const; }; class UnpackContext { @@ -128,18 +133,21 @@ class UnpackContext { const std::vector ir_options; // struct UnpackOptions from stdlib const std::vector ir_slice; const var_idx_t ir_slice0; + const GeneratedLoadPolicy generated_load_policy; - UnpackContext(CodeBlob& code, AnyV origin, std::vector ir_slice, std::vector ir_options); + UnpackContext(CodeBlob& code, AnyV origin, std::vector ir_slice, std::vector ir_options, + GeneratedLoadPolicy generated_load_policy = GeneratedLoadPolicy::Preserve); PrefixReadMode get_prefix_mode() const { return prefix_mode; } + bool force_keep_arg() const { return generated_load_policy == GeneratedLoadPolicy::Preserve; } var_idx_t option_assertEndAfterReading() const { return ir_options[0]; } var_idx_t option_throwIfOpcodeDoesNotMatch() const { return ir_options[1]; } - std::vector loadInt(int len, const char* debug_desc) const; - std::vector loadUint(int len, const char* debug_desc) const; - std::vector loadRef(const char* debug_desc) const; - std::vector loadMaybeRef(const char* debug_desc) const; + std::vector loadInt(int len, const char* purpose) const; + std::vector loadUint(int len, const char* purpose) const; + std::vector loadRef(const char* purpose) const; + std::vector loadMaybeRef(const char* purpose) const; void loadAndCheckOpcode(PackOpcode opcode) const; void skipBits(int len) const; void skipBits_var(var_idx_t ir_len) const; @@ -150,7 +158,7 @@ class UnpackContext { std::vector generate_unpack_any(TypePtr any_type, PrefixReadMode prefix_mode = PrefixReadMode::LoadAndCheck) const; void generate_skip_any(TypePtr any_type, PrefixReadMode prefix_mode = PrefixReadMode::LoadAndCheck) const; - std::vector generate_lazy_match_any(TypePtr any_type, const LazyMatchOptions& options) const; + void generate_lazy_match_any(TypePtr any_type, const LazyMatchOptions& options) const; }; diff --git a/tolk/pipe-ast-to-legacy.cpp b/tolk/pipe-ast-to-legacy.cpp index 247a883408..8fa37c2fba 100644 --- a/tolk/pipe-ast-to-legacy.cpp +++ b/tolk/pipe-ast-to-legacy.cpp @@ -16,8 +16,8 @@ */ #include "tolk.h" #include "ast.h" -#include "ast-aux-data.h" #include "ast-visitor.h" +#include "lazy-helpers.h" #include "compilation-errors.h" #include "constant-evaluator.h" #include "type-system.h" @@ -25,6 +25,9 @@ #include "smart-casts-cfg.h" #include "pack-unpack-api.h" #include "gen-entrypoints.h" +#include "inline-return-analysis.h" +#include "loop-control-analysis.h" +#include /* * This pipe is the last one operating AST: it transforms AST to IR. @@ -55,7 +58,11 @@ std::vector pre_compile_symbol(const Symbol* sym, CodeBlob& code, Any void process_any_statement(AnyV v, CodeBlob& code); std::vector transition_rvect_to_runtime_type(std::vector&& rvect, CodeBlob& code, TypePtr from_type, TypePtr dest_type, AnyV origin); -static thread_local AnyV stmt_before_immediate_return = nullptr; +struct FallthroughTail; +static void gen_return_from_cur_fun(CodeBlob& code, AnyV origin, std::vector return_vars); +static void process_block_statement(V v, CodeBlob& code, const FallthroughTail* outer = nullptr); +static void process_if_statement(V v, CodeBlob& code, const FallthroughTail* tail = nullptr); +static std::vector process_match_expression(V v, CodeBlob& code, TypePtr target_type, const FallthroughTail* tail = nullptr); DebugMarkInfo CodeBlob::create_mark_leave_fun(FunctionPtr fun_ref, AnyV origin, std::vector ir_return) { // it's the location of `return` statement, or `}` for void functions @@ -125,6 +132,143 @@ static int calc_offset_on_stack(StructPtr struct_ref, int field_idx) { return stack_offset; } +// Every function with `lazy` vars has LazyLoadPlan: "which fields to load before which statement". +static void emit_lazy_loads_before(AnyV stmt, CodeBlob& code) { + for (const LazyLoadAction& action : code.fun_ref->lazy_load_plan->loads_before(stmt)) { + const LazyVariableLoadedState* lazy_variable = code.get_lazy_variable(action.var_ref); + tolk_assert(lazy_variable); + + std::vector ir_obj = action.var_ref->ir_idx; // loading will update these stack slots + TypePtr t_orig = action.var_ref->declared_type; + + if (action.field_ref) { // extract a field from a whole lazy variable + tolk_assert(lazy_variable->is_struct()); + int stack_offset = calc_offset_on_stack(lazy_variable->loaded_state.original_struct, action.field_ref->field_idx); + int stack_width = action.field_ref->declared_type->get_width_on_stack(); + ir_obj = std::vector(ir_obj.begin() + stack_offset, ir_obj.begin() + stack_offset + stack_width); + t_orig = action.field_ref->declared_type; + } + + if (action.union_variant) { // extract a variant from a union (a union variable or a union field of a struct) + ir_obj = transition_rvect_to_runtime_type(std::move(ir_obj), code, t_orig, action.union_variant, stmt); + } + + // `load_info` contains instructions to skip, load, save tail, etc.; + // it generates LETs to ir_obj, so stack slots of lazy_variable will contain loaded data + generate_lazy_struct_from_slice(code, stmt, lazy_variable, action.load_info, ir_obj); + } +} + + +// FallthroughTail is a suffix routed into a single live if/match branch. +// Used independently for inline returns and for structural `continue` in loops. +// > fun demo() { +// > if (cond) { return false } +// > else { xxx } +// > yyy +// > } +// Then `yyy` is a tail when compiling `else` block, resulting in approx +// > if (cond) { return false } +// > else { xxx; yyy } +struct FallthroughTail { + const FallthroughTail* outer = nullptr; // multiple if/else/match branching + const std::vector* statements = nullptr; // all statements of some block (in demo: [ast_if_statement, yyy]) + size_t next_idx = 0; // tail is `statements[next_idx..]` (in demo: 1, tail = yyy) + V scope_block = nullptr; // insert DebugMarkScopeEnd{.range} in the end + bool is_toplevel_block = false; // statements come from function's root body + bool emit_leave_fun = false; // need to insert implicit `return` (DebugMarkLeaveFunction + mutated vars) +}; + +// Installs `code.current_loop` for the duration of lowering a loop body, same backup as `code.inlining`. +struct LoopLoweringGuard { + CodeBlob& code; + const LoopFrameLowering* backup_outer_loop; + LoopContinuePlan plan; + LoopFrameLowering frame; + + LoopLoweringGuard(CodeBlob& code, V loop_body) + : code(code) + , backup_outer_loop(code.current_loop) + , plan(build_continue_plan_for_loop(loop_body)) + , frame{.plan = &plan, .body = loop_body} { + tolk_assert(plan.ok()); + code.current_loop = &frame; + } + + ~LoopLoweringGuard() { + code.current_loop = backup_outer_loop; + } +}; + +static const TailRoutingNode* find_routed_branch(AnyV pivot, CodeBlob& code) { + if (code.current_loop) { + if (const TailRoutingNode* route = code.current_loop->plan->find_branching(pivot)) { + return route; + } + } + if (code.inlining) { + return code.inlining->plan->find_branching(pivot); + } + return nullptr; +} + +static void process_tail(const FallthroughTail* tail, CodeBlob& code) { + for (; tail; tail = tail->outer) { + if (tail->statements) { + const std::vector& statements = *tail->statements; + + for (size_t i = tail->next_idx; i < statements.size(); ++i) { + AnyV stmt = statements[i]; + + if (code.fun_ref->lazy_load_plan) { + emit_lazy_loads_before(stmt, code); + } + + if (stmt->kind == ast_return_statement) { + process_any_statement(stmt, code); + if (tail->scope_block) { + code.add_debug_mark(DebugMarkScopeEnd{.range = tail->scope_block->range}); + } + return; + } + if (stmt->kind == ast_continue_statement) { + tolk_assert(code.current_loop); + code.add_debug_mark(DebugMarkScopeEnd{.range = code.current_loop->body->range}); + return; // remaining tails must not run + } + if (stmt->kind == ast_break_statement) { + process_any_statement(stmt, code); + return; // remaining tails must not run + } + + if (find_routed_branch(stmt, code)) { + FallthroughTail rest = *tail; // copies scope_block / emit_leave_fun + rest.next_idx = i + 1; + + if (auto v_if = stmt->try_as()) { + process_if_statement(v_if, code, &rest); + } else if (auto v_match = stmt->try_as()) { + process_match_expression(v_match, code, nullptr, &rest); + } else { + tolk_assert(false); + } + return; // lowering of if/match consumed `rest` and every outer tail + } + + process_any_statement(stmt, code); + } + } + + if (tail->scope_block) { + // same range as the block's START; already-closed range is a no-op for the replayer + code.add_debug_mark(DebugMarkScopeEnd{.range = tail->scope_block->range}); + } + if (tail->emit_leave_fun) { // no manual `return` (reached the end of a void function), + tolk_assert(code.inlining); // so place DebugMarkLeaveFunction and assign mutated_vars to rvect_out + gen_return_from_cur_fun(code, code.inlining->call_origin, {}); + } + } +} // The 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. @@ -259,7 +403,7 @@ class CheckReorderingForAsmArgOrderIsSafeVisitor final : public ASTVisitorFuncti bool has_side_effects = false; void visit(V v) override { - has_side_effects |= v->fun_maybe == nullptr || !v->fun_maybe->is_marked_as_pure() || v->fun_maybe->has_mutate_params(); + has_side_effects |= v->fun_maybe == nullptr || !v->fun_maybe->is_removable_if_unused() || v->fun_maybe->has_mutate_params(); parent::visit(v); } @@ -333,7 +477,7 @@ class ClearStateAfterInlineInPlace final : public ASTVisitorFunctionBody { } void on_enter_function(V v_function) override { - tolk_assert(cur_f->is_inlined_in_place()); + tolk_assert(cur_f->is_code_function()); for (int i = 0; i < cur_f->get_num_params(); ++i) { cur_f->get_param(i).mutate()->assign_ir_idx({}); @@ -341,6 +485,33 @@ class ClearStateAfterInlineInPlace final : public ASTVisitorFunctionBody { } }; +// Installs function-specific state while `f_inlined` AST body is lowered into caller's CodeBlob +struct FunctionBodyInliningGuard { + CodeBlob& code; + FunctionPtr f_inlined; + const InliningFrameLowering* backup_frame; + FunctionPtr backup_cur_fun; + std::vector backup_lazy_variables; + + FunctionBodyInliningGuard(CodeBlob& code, FunctionPtr f_inlined, const InliningFrameLowering* inlining_frame) + : code(code) + , f_inlined(f_inlined) + , backup_frame(code.inlining) + , backup_cur_fun(code.fun_ref) + , backup_lazy_variables(code.lazy_variables) { + code.fun_ref = f_inlined; + code.inlining = inlining_frame; + } + + ~FunctionBodyInliningGuard() { + ClearStateAfterInlineInPlace visitor; + visitor.start_visiting_function(f_inlined, f_inlined->ast_root->as()); + code.fun_ref = backup_cur_fun; + code.inlining = backup_frame; + code.lazy_variables = std::move(backup_lazy_variables); + } +}; + // CodeBlob has a mapping [st => ptr] const LazyVariableLoadedState* CodeBlob::get_lazy_variable(LocalVarPtr var_ref) const { @@ -639,11 +810,11 @@ static void pre_compile_let(CodeBlob& code, AnyExprV lhs, std::vector local_lval.after_let(std::move(ir_left), code, lhs); } -std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* debug_desc) { +std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* purpose) { FunctionPtr eq_sym = lookup_function("_==_"); FunctionPtr isnull_sym = lookup_function("__isNull"); FunctionPtr not_sym = lookup_function("!b_"); - std::vector ir_result = code.create_tmp_var(TypeDataBool::create(), origin, debug_desc); + std::vector ir_result = code.create_tmp_var(TypeDataBool::create(), origin, purpose); const TypeDataUnion* lhs_union = expr_type->unwrap_alias()->try_as(); if (!lhs_union && expr_type == TypeDataUnknown::create() && cmp_type == TypeDataNullLiteral::create()) { @@ -656,7 +827,7 @@ std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, Ty code.add_int_const(origin, ir_result, td::make_refint(types_eq ? -1 : 0)); } else if (!lhs_union->has_variant_equal_to(cmp_type)) { // at runtime, union tags store type_id, and aliases with the same underlying type share it; - // keep the lowering aligned with type-level `is`: distinct aliases are distinct variants + // type checker fired if `is` was used incorrectly with subtypes, so we safely know "false" here code.add_int_const(origin, ir_result, td::make_refint(0)); } else if (lhs_union->is_primitive_nullable() && cmp_type == TypeDataNullLiteral::create()) { // `int?` is `null` for primitive 1-slot nullables, they hold either value of TVM NULL, no extra union tag slot @@ -728,13 +899,7 @@ static std::vector gen_compile_time_code_instead_of_fun_call(CodeBlob return ir_generated; } -std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, bool is_before_immediate_return, const std::vector>& vars_per_arg) { - static thread_local std::vector called_stack; - if (std::find(called_stack.begin(), called_stack.end(), f_inlined) != called_stack.end()) { - std::string_view postfix = f_inlined->is_packToBuilder() || f_inlined->is_unpackFromSlice() ? " and leads to infinite serialization" : ""; - err("function `{}` is recursive{}", f_inlined, postfix).fire(f_inlined->ident_anchor, f_inlined); - } - +std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_type, AnyV origin, FunctionPtr f_inlined, AnyExprV self_obj, const std::vector>& vars_per_arg) { tolk_assert(vars_per_arg.size() == f_inlined->parameters.size()); std::vector ir_params; for (int i = 0; i < f_inlined->get_num_params(); ++i) { @@ -752,16 +917,17 @@ std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_ ir_params.insert(ir_params.end(), param_i.ir_idx.begin(), param_i.ir_idx.end()); } - std::vector rvect_call = code.create_tmp_var(ret_type, origin, "(inlined-return)"); - std::vector* backup_outer_inline = code.inline_rvect_out; - AnyV backup_outer_return_stmt = code.inline_return_stmt_out; - FunctionPtr backup_cur_fun = code.fun_ref; - bool backup_inline_before_return = code.inlining_before_immediate_return; - auto backup_lazy_variables = code.lazy_variables; - code.inline_rvect_out = &rvect_call; - code.inlining_before_immediate_return = is_before_immediate_return; - code.fun_ref = f_inlined; - called_stack.push_back(f_inlined); + InlineReturnPlan inlining_plan = build_inlining_plan_for_function(f_inlined); + tolk_assert(inlining_plan.ok()); + + InliningFrameLowering inlining{ + .f_inlined = f_inlined, + .call_origin = origin, + .plan = &inlining_plan, + .rvect_out = code.create_tmp_var(ret_type, origin, "(inlined-return)"), + }; + + FunctionBodyInliningGuard guard(code, f_inlined, &inlining); // specially handle `point.getX()` if point is a lazy var: to make `self.toCell()` work and `self.x` asserted; // (only methods preserve lazy, `getXOf(point)` does not, though theoretically can be done) const LazyVariableLoadedState* lazy_receiver = self_obj ? code.get_lazy_variable(self_obj) : nullptr; @@ -789,34 +955,10 @@ std::vector gen_inline_fun_call_in_place(CodeBlob& code, TypePtr ret_ auto v_ast_root = f_inlined->ast_root->as(); auto v_block = v_ast_root->get_body()->as(); - process_any_statement(v_block, code); + FallthroughTail tail_leave_fun{.emit_leave_fun = true}; + process_block_statement(v_block, code, &tail_leave_fun); - if (f_inlined->has_mutate_params() && f_inlined->inferred_return_type == TypeDataVoid::create()) { - std::vector mutated_vars; - for (const LocalVarData& p_sym: f_inlined->parameters) { - if (p_sym.is_mutate_parameter()) { - mutated_vars.insert(mutated_vars.end(), p_sym.ir_idx.begin(), p_sym.ir_idx.end()); - } - } - code.add_let(origin, rvect_call, std::move(mutated_vars)); - } - - code.add_debug_mark(DebugMarkLeaveFunction{ - .fun_ref = f_inlined, - .ir_return = *code.inline_rvect_out, - .range = code.inline_return_stmt_out ? code.inline_return_stmt_out->range : SrcRange::span_at_end(f_inlined->ast_root->range, 1), - }); - - ClearStateAfterInlineInPlace visitor; - visitor.start_visiting_function(f_inlined, v_ast_root); - - called_stack.pop_back(); - code.fun_ref = backup_cur_fun; - code.inline_rvect_out = backup_outer_inline; - code.inline_return_stmt_out = backup_outer_return_stmt; - code.inlining_before_immediate_return = backup_inline_before_return; - code.lazy_variables = std::move(backup_lazy_variables); - return rvect_call; + return inlining.rvect_out; } // convert a constant value (calculated by a "constant-evaluator") to IR vars; @@ -1008,7 +1150,7 @@ static std::vector process_set_assign(V v, CodeBlob& args_vars.insert(args_vars.end(), ir_lhs.begin(), ir_lhs.end()); args_vars.insert(args_vars.end(), ir_rhs.begin(), ir_rhs.end()); std::vector ir_result = code.create_tmp_var(v->inferred_type, v, "(set-assign)"); - code.add_call(v, ir_result, std::move(args_vars), v->fun_ref); + code.add_call(v, ir_result, std::move(args_vars), v->fun_ref, true); code.add_extra_mark_location(v->get_lhs()->range); code.add_let(v, ir_lhs, ir_result); // += and others for math only, transition not required @@ -1023,30 +1165,37 @@ 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()}); std::vector rvect = code.create_tmp_var(v->inferred_type, v, "(binary-op)"); - code.add_call(v, rvect, std::move(args_vars), v->fun_ref); + code.add_call(v, rvect, std::move(args_vars), v->fun_ref, true); return transition_to_target_type(std::move(rvect), code, target_type, v); } if (t == tok_logical_and || t == tok_logical_or) { - // do the following transformations: + // emit the following IR: // a && b -> a ? (b != 0) : 0 - // a || b -> a ? 1 : (b != 0) - AnyExprV v_0 = createV(v->range, td::make_refint(0), "0"); - v_0->mutate()->assign_inferred_type(TypeDataInt::create()); - AnyExprV v_1 = createV(v->range, td::make_refint(-1), "-1"); - v_1->mutate()->assign_inferred_type(TypeDataInt::create()); - auto v_b_ne_0 = createV(v->range, v->operator_range, "!=", tok_neq, v->get_rhs(), v_0); - v_b_ne_0->mutate()->assign_inferred_type(TypeDataInt::create()); - v_b_ne_0->mutate()->assign_fun_ref(lookup_function("_!=_")); + // a || b -> a ? -1 : (b != 0) std::vector ir_cond = pre_compile_expr(v->get_lhs(), code, nullptr); tolk_assert(ir_cond.size() == 1); - std::vector rvect = code.create_tmp_var(v->inferred_type, v, "(ternary)"); + std::vector rvect = code.create_tmp_var(v->inferred_type, v, "(logical-op)"); + FunctionPtr f_neq = lookup_function("_!=_"); Op& if_op = code.add_if_else(v, ir_cond); - code.push_set_cur(if_op.block0); - code.add_let(v, rvect, pre_compile_expr(t == tok_logical_and ? v_b_ne_0 : v_1, code, nullptr)); - code.close_pop_cur(v); - code.push_set_cur(if_op.block1); - code.add_let(v, rvect, pre_compile_expr(t == tok_logical_and ? v_0 : v_b_ne_0, code, nullptr)); - code.close_pop_cur(v); + if (t == tok_logical_and) { + code.push_set_cur(if_op.block0); + std::vector ir_rhs = pre_compile_expr(v->get_rhs(), code, nullptr); + tolk_assert(ir_rhs.size() == 1); + code.add_call(v, rvect, {ir_rhs[0], code.create_int(v, 0, "(zero)")}, f_neq); + code.close_pop_cur(v); + code.push_set_cur(if_op.block1); + code.add_int_const(v, rvect, td::make_refint(0)); + code.close_pop_cur(v); + } else { + code.push_set_cur(if_op.block0); + code.add_int_const(v, rvect, td::make_refint(-1)); + code.close_pop_cur(v); + code.push_set_cur(if_op.block1); + std::vector ir_rhs = pre_compile_expr(v->get_rhs(), code, nullptr); + tolk_assert(ir_rhs.size() == 1); + code.add_call(v, rvect, {ir_rhs[0], code.create_int(v, 0, "(zero)")}, f_neq); + code.close_pop_cur(v); + } return transition_to_target_type(std::move(rvect), code, target_type, v); } if (t == tok_eq || t == tok_neq) { @@ -1054,7 +1203,7 @@ static std::vector process_binary_operator(V v, tolk_assert(f_eq->name != "_==_"); std::vector args_vars = pre_compile_tensor(code, {v->get_lhs(), v->get_rhs()}); std::vector rvect = code.create_tmp_var(TypeDataBool::create(), v, "(eq-operator)"); - code.add_call(v, rvect, {args_vars[0], args_vars[1]}, f_eq); + code.add_call(v, rvect, {args_vars[0], args_vars[1]}, f_eq, true); if (t == tok_neq) { FunctionPtr not_sym = lookup_function("!b_"); code.add_call(v, rvect, rvect, not_sym); @@ -1068,7 +1217,7 @@ static std::vector process_binary_operator(V v, static std::vector process_unary_operator(V v, CodeBlob& code, TypePtr target_type) { std::vector rhs_vars = pre_compile_expr(v->get_rhs(), code, nullptr); std::vector rvect = code.create_tmp_var(v->inferred_type, v, "(unary-op)"); - code.add_call(v, rvect, std::move(rhs_vars), v->fun_ref); + code.add_call(v, rvect, std::move(rhs_vars), v->fun_ref, true); return transition_to_target_type(std::move(rvect), code, target_type, v); } @@ -1077,11 +1226,7 @@ static std::vector process_ternary_operator(V v tolk_assert(ir_cond.size() == 1); std::vector rvect = code.create_tmp_var(v->inferred_type, v, "(ternary)"); - if (v->get_cond()->is_always_true) { - code.add_let(v->get_when_true(), rvect, pre_compile_expr(v->get_when_true(), code, v->inferred_type)); - } else if (v->get_cond()->is_always_false) { - code.add_let(v->get_when_false(), rvect, pre_compile_expr(v->get_when_false(), code, v->inferred_type)); - } else if (v->inferred_type->get_width_on_stack() == 1 && is_ternary_arg_trivial_for_condsel(v->get_when_true()) && is_ternary_arg_trivial_for_condsel(v->get_when_false())) { + if (v->inferred_type->get_width_on_stack() == 1 && is_ternary_arg_trivial_for_condsel(v->get_when_true()) && is_ternary_arg_trivial_for_condsel(v->get_when_false())) { std::vector ir_true = pre_compile_expr(v->get_when_true(), code, v->inferred_type); std::vector ir_false = pre_compile_expr(v->get_when_false(), code, v->inferred_type); std::vector condsel_args = { ir_cond[0], ir_true[0], ir_false[0] }; @@ -1168,15 +1313,27 @@ static std::vector process_not_null_operator(V static std::vector process_lazy_operator(V v, CodeBlob& code, TypePtr target_type) { // `lazy Storage.fromSlice(s)` does not load anything here, it only saves a slice for future loads; - // "future loads" are special auxiliary AST vertices "load x" that were inserted in pipe-lazy-load-insertions.cpp + // "future loads" are prelude actions from LazyLoadPlan, built in pipe-lazy-load-insertions.cpp auto v_call = v->get_expr()->try_as(); tolk_assert(v_call && v_call->fun_maybe); + std::optional wrapper_guard; FunctionPtr called_f = v_call->fun_maybe; - if (called_f->is_code_function()) { // `lazy loadStorage()` is allowed, it contains just `return ...`, inline it here + if (called_f->is_code_function()) { // `lazy loadStorage()`, its body is `smth; return ...` + wrapper_guard.emplace(code, called_f, nullptr); auto f_body = called_f->ast_root->as()->get_body()->as(); - tolk_assert(f_body->size() == 1 && f_body->get_item(0)->kind == ast_return_statement); - auto f_returns = f_body->get_item(0)->as(); + // inline `smth` preceding the final `return` (no `return` in the middle) + for (int i = 0; i < f_body->size(); ++i) { + AnyV stmt = f_body->get_item(i); + if (called_f->lazy_load_plan) { // in case `loadStorage` has lazy vars itself + emit_lazy_loads_before(stmt, code); + } + if (i < f_body->size() - 1) { // the last is `return fromCell/fromSlice`, handled below + process_any_statement(stmt, code); + } + } + auto f_returns = f_body->get_item(f_body->size() - 1)->try_as(); + tolk_assert(f_returns); v_call = f_returns->get_return_value()->try_as(); tolk_assert(v_call && v_call->fun_maybe && v_call->fun_maybe->is_builtin()); called_f = v_call->fun_maybe; @@ -1208,11 +1365,12 @@ static std::vector process_lazy_operator(V v, Code // on `var p = lazy Point.fromSlice(s, options)`, save s and options (lazy_variable) AnyExprV v_options = has_passed_options ? v_call->get_arg(v_call->get_num_args() - 1)->get_expr() : called_f->parameters.back().default_value; std::vector ir_options = pre_compile_expr(v_options, code, called_f->parameters[1].declared_type); + wrapper_guard.reset(); const LazyVariableLoadedState* lazy_variable = new LazyVariableLoadedState(v->dest_var_ref->declared_type, std::move(ir_slice), std::move(ir_options)); code.lazy_variables.emplace_back(v->dest_var_ref, lazy_variable); // initially, all contents of `p` is filled by nulls, but before `p.x` or any other field usages, - // they will be loaded by separate AST aux vertices; + // they will be loaded by LazyLoadPlan prelude actions; // same for unions: `val msg = lazy MyMsgUnion`, msg is N+1 nulls, but next lazy `match` will transition slots, // which will be filled by loads std::vector ir_null = code.create_tmp_var(TypeDataNullLiteral::create(), v, "(init-null)"); @@ -1221,17 +1379,59 @@ static std::vector process_lazy_operator(V v, Code return transition_to_target_type(std::move(ir_initial_nulls), code, target_type, v); } -static std::vector process_match_expression(V v, CodeBlob& code, TypePtr target_type) { - TypePtr subject_type = v->get_subject()->inferred_type; - +static std::vector process_match_expression(V v, CodeBlob& code, TypePtr target_type, const FallthroughTail* tail) { int n_arms = v->get_arms_count(); std::vector ir_subj = pre_compile_expr(v->get_subject(), code, nullptr); - std::vector ir_result = code.create_tmp_var(v->inferred_type, v, "(match-expression)"); + TypePtr match_expr_type = v->inferred_type; + TypePtr subject_type = v->get_subject()->inferred_type; + std::vector ir_match_result = code.create_tmp_var(match_expr_type, v, "(match-expression)"); AnyExprV match_origin = v; // all "IF UTag==type_id" share this location: "step over" jumps into actual branch immediately - if (!n_arms) { // `match (subject) {}` - tolk_assert(v->is_statement()); - return {}; + const TailRoutingNode* route = find_routed_branch(v, code); + tolk_assert(!tail || route); + + // `match(lazyUnion)` / `match(obj.lastUnionField)` + if (v->is_lazy_match) { + LocalVarPtr var_ref = extract_sink_expression_from_vertex(v->get_subject()).var_ref; + const LazyVariableLoadedState* lazy_variable = code.get_lazy_variable(var_ref); + tolk_assert(lazy_variable); + + std::vector match_blocks; + match_blocks.reserve(n_arms); + for (int i = 0; i < n_arms; ++i) { + auto v_arm = v->get_arm(i); + TypePtr arm_variant = nullptr; + if (v_arm->pattern_kind == MatchArmKind::exact_type) { + arm_variant = v_arm->pattern_type_node->resolved_type; + } else { + tolk_assert(v_arm->pattern_kind == MatchArmKind::else_branch); // `else` allowed in a lazy match + } + match_blocks.emplace_back(LazyMatchOptions::MatchBlock{arm_variant, v_arm}); + } + + LazyMatchOptions options = { + .match_blocks = std::move(match_blocks), + .lazy_var_ref = var_ref, + .lower_match_arm = [v, tail, route, match_expr_type, &ir_match_result](AnyV v_arm_untyped, CodeBlob& code) { + auto v_arm = v_arm_untyped->as(); + if (v->is_statement()) { + FallthroughTail arm{ + .outer = tail && route->goes_to == v_arm ? tail : nullptr, + .statements = &v_arm->get_body()->get_block_statement()->get_items(), + }; + process_tail(&arm, code); + } else { + // if it's `match` expression (not statement), then every arm has a result, assigned to a whole `match` result + std::vector ir_ith_arm = pre_compile_expr(v_arm->get_body(), code); + ir_ith_arm = transition_to_target_type(std::move(ir_ith_arm), code, v_arm->get_body()->inferred_type, match_expr_type, v); + code.add_let(v, ir_match_result, std::move(ir_ith_arm)); + } + } + }; + + // it will generate match by a slice prefix, and for each arm, invoke lower_match_arm above, updating ir_match_result + generate_lazy_match_for_union(code, v, subject_type, lazy_variable, options); + return transition_to_target_type(std::move(ir_match_result), code, target_type, v); } bool has_type_arm = false; // it's either `match` by type (all arms are types covering all cases) @@ -1245,8 +1445,7 @@ static std::vector process_match_expression(V v } // `else` is not allowed in `match` by type; this was not fired at type checking, - // because it might have turned out to be a lazy match, where `else` is allowed; - // if we are here, it's not a lazy match, it's a regular one (the lazy one is handled specially, in aux vertex) + // because it might have turned out to be a lazy match, where `else` is allowed (handled above) if (has_type_arm && has_else_arm) { err("`else` is not allowed in `match` by type; you should cover all possible types").fire(v->get_arm(n_arms - 1)->get_pattern_expr()); } @@ -1294,13 +1493,14 @@ static std::vector process_match_expression(V v code.add_extra_mark_location(v_ith_arm->range); if (v->is_statement()) { - pre_compile_expr(v_ith_arm->get_body(), code); - if (v == stmt_before_immediate_return) { - code.add_return(v_ith_arm, {}, code.fun_ref); - } + FallthroughTail arm{ + .outer = tail && route->goes_to == v_ith_arm ? tail : nullptr, + .statements = &v_ith_arm->get_body()->get_block_statement()->get_items(), + }; + process_tail(&arm, code); } else { - std::vector arm_ir_idx = pre_compile_expr(v_ith_arm->get_body(), code, v->inferred_type); - code.add_let(v, ir_result, std::move(arm_ir_idx)); + std::vector ir_ith_arm = pre_compile_expr(v_ith_arm->get_body(), code, v->inferred_type); + code.add_let(v, ir_match_result, std::move(ir_ith_arm)); } if (!inside_last_branch) { @@ -1314,6 +1514,8 @@ static std::vector process_match_expression(V v // if it's auto-generated "unreachable", insert "N THROW" if (implicit_else_unreachable_throw) { code.add_call(v, {}, {code.create_int(v, implicit_else_unreachable_throw, "(throw-else)")}, lookup_function("__throw")); + } else if (tail && route->goes_to == nullptr) { // tail goes to implicit `else` + process_tail(tail, code); } // close all outer IFs @@ -1322,7 +1524,7 @@ static std::vector process_match_expression(V v code.close_pop_cur(v); } - return transition_to_target_type(std::move(ir_result), code, target_type, v); + return transition_to_target_type(std::move(ir_match_result), code, target_type, v); } static std::vector process_dot_access(V v, CodeBlob& code, TypePtr target_type, LValContext* lval_ctx) { @@ -1333,6 +1535,15 @@ static std::vector process_dot_access(V v, CodeBlob& // `user.id`; internally, a struct (an object) is a tensor if (const TypeDataStruct* t_struct = obj_type->try_as()) { StructFieldPtr field_ref = std::get(v->target); + // `in.senderAddress` / `in.body` in onInternalMessage: logical InMessage is one slice on the stack + if (code.fun_ref->is_entrypoint() && code.fun_ref->get_num_params() == 1) { + if (auto obj_ref = v->get_obj()->try_as()) { + if (obj_ref->sym == &code.fun_ref->parameters[0] && (code.fun_ref->is_onInternalMessage() || code.fun_ref->is_onBouncedMessage())) { + std::vector rvect = generate_get_InMessage_field(code, v, v->get_field_name(), &code.fun_ref->parameters[0]); + return transition_to_target_type(std::move(rvect), code, target_type, v); + } + } + } // handle `lazyPoint.x`, assert that slot for "x" is loaded (ensure lazy-loading correctness); // same for `val msg = lazy MyMsgUnion; match(...) msg.field` inside a specific variant (struct_ref) if (const LazyVariableLoadedState* lazy_variable = code.get_lazy_variable(v->get_obj())) { @@ -1430,13 +1641,6 @@ static std::vector process_function_call(V v, Code code.add_indirect_invoke(v, rvect, std::move(args_vars)); return transition_to_target_type(std::move(rvect), code, target_type, v); } - // `grams("0.05")` and others, we even don't need to calculate ir_idx for arguments, just replace with constexpr - if (fun_ref->is_compile_time_const_val()) { - ConstValExpression value = eval_expression_if_const_or_fire(v); - auto [type, rvect] = pre_compile_constant_expression(value, code, v); - return transition_to_target_type(std::move(rvect), code, target_type, v); - } - // for `num.inc()` use `inc` as origin for tmp vars; specifically, for `num.inc().inc()` (multi-line) // after each `inc` the debugger will remain here (as opposed to origin=v (whole range), producing jumps backward) AnyV call_origin = v; @@ -1525,11 +1729,7 @@ static std::vector process_function_call(V v, Code // detect what a function really returns from the stack perspective; // for instance, `fun f(mutate x: int): slice` puts `(int, slice)` onto a stack - bool returns_self_from_asm_stack = fun_ref->does_return_self() && !fun_ref->does_mutate_self() && fun_ref->is_asm_function(); TypePtr op_call_type = v->inferred_type; - if (fun_ref->does_return_self() && !fun_ref->does_mutate_self() && !returns_self_from_asm_stack) { - op_call_type = TypeDataVoid::create(); // `return self` actually puts nothing onto a stack - } if (fun_ref->has_mutate_params()) { std::vector types_list; for (int i = 0; i < delta_self + v->get_num_args(); ++i) { @@ -1537,25 +1737,33 @@ static std::vector process_function_call(V v, Code types_list.push_back(fun_ref->parameters[i].declared_type); } } - bool self_already_added = fun_ref->does_return_self() && !returns_self_from_asm_stack; - if (!self_already_added) { - types_list.push_back(v->inferred_type); + bool already_returned_self = fun_ref->does_return_self() && fun_ref->does_mutate_self(); + if (!already_returned_self) { // if not `return self` from a mutate-self function + types_list.push_back(v->inferred_type); // real return value goes after mutate parameters } op_call_type = TypeDataTensor::create(std::move(types_list)); } - std::vector args_vars; - for (const std::vector& list : vars_per_arg) { - args_vars.insert(args_vars.end(), list.cbegin(), list.cend()); - } std::vector rvect; - if (fun_ref->is_compile_time_special_gen()) { + if (fun_ref->is_compile_time_const_val()) { + // `grams("0.05")` / `reflect.typeNameOfObject(...)` — calculate value at compile-time; + // args already lowered above (for side effects and control flow); pure args will be DCE'd + ConstValExpression value = eval_expression_if_const_or_fire(v); + rvect = pre_compile_constant_expression(value, code, v).second; + } else if (fun_ref->is_compile_time_special_gen()) { + // `createMessage()` / `obj.toCell()` — generate IR code instead of Op::_Call rvect = gen_compile_time_code_instead_of_fun_call(code, v, vars_per_arg); } else if (fun_ref->is_inlined_in_place() && fun_ref->is_code_function()) { - rvect = gen_inline_fun_call_in_place(code, op_call_type, call_origin, v->fun_maybe, self_obj, v == stmt_before_immediate_return, vars_per_arg); + // `inlinedF()` — copy-paste f's body right here instead of Op::_Call + rvect = gen_inline_fun_call_in_place(code, op_call_type, call_origin, v->fun_maybe, self_obj, vars_per_arg); } else { + // asm or non-inline call: regular Op::_Call with flattened IR vars + std::vector args_vars; + for (const std::vector& list : vars_per_arg) { + args_vars.insert(args_vars.end(), list.cbegin(), list.cend()); + } rvect = code.create_tmp_var(op_call_type, v, "(fun-call)"); - code.add_call(call_origin, rvect, std::move(args_vars), fun_ref, arg_order_already_equals_asm); + code.add_call(call_origin, rvect, std::move(args_vars), fun_ref, true, arg_order_already_equals_asm); } // `x.inc().inc()` — mutating chaining was called in lval context, here we get `x` expression in an outer call; @@ -1567,14 +1775,6 @@ static std::vector process_function_call(V v, Code vars_per_arg[orig_self_i] = pre_compile_expr(obj_leftmost, code, fun_ref->parameters[0].declared_type, &self_lval); } - std::vector return_self_snapshot; - // preserve by-value `self` before separate `mutate` parameters are written back to caller lvalues - if (fun_ref->does_return_self() && !fun_ref->does_mutate_self() && !returns_self_from_asm_stack && fun_ref->has_mutate_params()) { - int orig_self_i = rev_arg_order.empty() ? 0 : rev_arg_order[0]; - return_self_snapshot = code.create_tmp_var(fun_ref->parameters[0].declared_type, call_origin, "(return-self)"); - code.add_let(call_origin, return_self_snapshot, vars_per_arg[orig_self_i]); - } - // `f(mutate x: int): slice` had put `(int, slice)` onto a stack, leave only `slice` (an actual return) if (fun_ref->has_mutate_params()) { std::vector left; @@ -1594,16 +1794,14 @@ static std::vector process_function_call(V v, Code // `beginCell().storeUint()` / `sb.append()` / etc. — dot call for methods returning `self` TypePtr rvect_type = v->inferred_type; - if (fun_ref->does_return_self() && !returns_self_from_asm_stack) { + if (fun_ref->does_return_self() && fun_ref->does_mutate_self()) { int orig_self_i = rev_arg_order.empty() ? 0 : rev_arg_order[0]; - rvect = return_self_snapshot.empty() ? vars_per_arg[orig_self_i] : std::move(return_self_snapshot); - if (fun_ref->does_mutate_self()) { - if (obj_leftmost == nullptr && self_obj && is_valid_mutation_path(self_obj)) { - obj_leftmost = self_obj; - } - if (lval_ctx && obj_leftmost) { - lval_ctx->set_mutated_self_obj(obj_leftmost); - } + rvect = vars_per_arg[orig_self_i]; + if (obj_leftmost == nullptr && self_obj && is_valid_mutation_path(self_obj)) { + obj_leftmost = self_obj; + } + if (lval_ctx && obj_leftmost) { + lval_ctx->set_mutated_self_obj(obj_leftmost); } rvect_type = fun_ref->parameters[0].declared_type; } @@ -1617,6 +1815,9 @@ static std::vector process_braced_expression(V // unless it's a special vertex "braced expression" (currently, only `match` arms) std::vector implicit_rvect; for (AnyV item : v->get_block_statement()->get_items()) { + if (code.fun_ref->lazy_load_plan) { + emit_lazy_loads_before(item, code); + } if (auto v_return = item->try_as()) { tolk_assert(implicit_rvect.empty()); implicit_rvect = pre_compile_expr(v_return->get_expr(), code); @@ -1627,6 +1828,10 @@ static std::vector process_braced_expression(V return transition_to_target_type(std::move(implicit_rvect), code, target_type, v); } +static std::vector process_braced_yield_result(V v, CodeBlob& code, TypePtr target_type) { + return pre_compile_expr(v->get_expr(), code, target_type); +} + static std::vector process_tensor(V v, CodeBlob& code, TypePtr target_type, LValContext* lval_ctx) { // tensor is compiled "as is", for example `(1, null)` occupies 2 slots // and if assigned/passed to something other, like `(int, (int,int)?)`, a whole tensor is transitioned, it works @@ -1843,79 +2048,6 @@ static std::vector process_empty_expression(V v return transition_to_target_type(std::move(empty_rvect), code, target_type, v); } -static std::vector process_artificial_aux_vertex(V v, CodeBlob& code, TypePtr target_type) { - AnyExprV wrapped = v->get_wrapped_expr(); - - // aux "load x"; example: `var p = lazy Point.fromSlice(s); aux "load x"; return p.x` - if (const auto* data = dynamic_cast(v->aux_data)) { - const LazyVariableLoadedState* lazy_variable = code.get_lazy_variable(data->var_ref); - tolk_assert(lazy_variable); - - std::vector ir_obj = data->var_ref->ir_idx; // loading will update stack slots of `p` - TypePtr t_orig = data->var_ref->declared_type; - - if (data->field_ref) { // extract a field from a whole lazy variable - tolk_assert(lazy_variable->is_struct()); - int stack_offset = calc_offset_on_stack(lazy_variable->loaded_state.original_struct, data->field_ref->field_idx); - int stack_width = data->field_ref->declared_type->get_width_on_stack(); - ir_obj = std::vector(ir_obj.begin() + stack_offset, ir_obj.begin() + stack_offset + stack_width); - t_orig = data->field_ref->declared_type; - } - - if (data->union_variant) { // extract a variant from a union (a union variable or a union field of a struct) - ir_obj = transition_to_target_type(std::move(ir_obj), code, t_orig, data->union_variant, wrapped); - } - - // `load_info` contains instructions to skip, load, save tail, etc.; - // it generates LETs to ir_obj, so stack slots of lazy_variable will contain loaded data - generate_lazy_struct_from_slice(code, wrapped, lazy_variable, data->load_info, ir_obj); - return transition_to_target_type({}, code, target_type, wrapped); - } - - // aux "match(lazyUnion)" / aux "match(obj.lastUnionField)" - if (const auto* data = dynamic_cast(v->aux_data)) { - V v_match = wrapped->as(); - pre_compile_expr(v_match->get_subject(), code, nullptr); - - const LazyVariableLoadedState* lazy_variable = code.get_lazy_variable(data->var_ref); - tolk_assert(lazy_variable); - TypePtr t_union = data->field_ref ? data->field_ref->declared_type : data->var_ref->declared_type; - - std::vector match_blocks; - match_blocks.reserve(v_match->get_arms_count()); - for (int i = 0; i < v_match->get_arms_count(); ++i) { - auto v_arm = v_match->get_arm(i); - TypePtr arm_variant = nullptr; - if (v_arm->pattern_kind == MatchArmKind::exact_type) { - arm_variant = v_arm->pattern_type_node->resolved_type; - } else { - tolk_assert(v_arm->pattern_kind == MatchArmKind::else_branch); // `else` allowed in a lazy match - } - match_blocks.emplace_back(LazyMatchOptions::MatchBlock{arm_variant, v_arm->get_body(), v_arm->get_body()->inferred_type}); - } - - LazyMatchOptions options = { - .match_expr_type = v->inferred_type, - .is_statement = v_match->is_statement(), - .add_return_to_all_arms = v == stmt_before_immediate_return, - .match_blocks = std::move(match_blocks), - .lazy_var_ref = data->var_ref, - }; - - // it will generate match by a slice prefix, and for each `match` arm, invoke pre_compile_expr(), - // which contains "aux load" particularly - std::vector ir_match = generate_lazy_match_for_union(code, v_match, t_union, lazy_variable, options); - return transition_to_target_type(std::move(ir_match), code, target_type, wrapped); - } - - if (const auto* data = dynamic_cast(v->aux_data)) { - std::vector rvect = data->generate_get_InMessage_field(code, wrapped); - return transition_to_target_type(std::move(rvect), code, target_type, wrapped); - } - - tolk_assert(false); -} - std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, TypePtr target_type, LValContext* lval_ctx) { switch (v->kind) { case ast_reference: @@ -1948,6 +2080,8 @@ std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, TypePtr targ return process_function_call(v->as(), code, target_type, lval_ctx); case ast_braced_expression: return process_braced_expression(v->as(), code, target_type); + case ast_braced_yield_result: + return process_braced_yield_result(v->as(), code, target_type); case ast_tensor: return process_tensor(v->as(), code, target_type, lval_ctx); case ast_square_brackets: @@ -1972,49 +2106,28 @@ std::vector pre_compile_expr(AnyExprV v, CodeBlob& code, TypePtr targ return process_underscore(v->as(), code); case ast_empty_expression: return process_empty_expression(v->as(), code, target_type); - case ast_artificial_aux_vertex: - return process_artificial_aux_vertex(v->as(), code, target_type); default: throw UnexpectedASTNodeKind(v, "pre_compile_expr"); } } - -static void process_block_statement(V v, CodeBlob& code) { +static void process_block_statement(V v, CodeBlob& code, const FallthroughTail* outer) { if (v->empty()) { - return; + return process_tail(outer, code); } - - FunctionPtr cur_f = code.fun_ref; - bool does_f_return_nothing = cur_f->inferred_return_type == TypeDataVoid::create() && !cur_f->does_return_self() && !cur_f->has_mutate_params(); - bool is_toplevel_block = v == cur_f->ast_root->as()->get_body(); - bool inlining_doesnt_prevent = code.inline_rvect_out == nullptr || code.inlining_before_immediate_return; - - // function body scope is tracked by enter_fun/leave_fun, sub-blocks need explicit scope marks + bool is_toplevel_block = v == code.fun_ref->ast_root->as()->get_body(); if (!is_toplevel_block) { + // function body scope is tracked by enter_fun/leave_fun, sub-blocks need explicit scope marks code.add_debug_mark(DebugMarkScopeStart{.range = v->range}); } - // we want to optimize `match` and `if/else`: if it's the last statement, implicitly add "return" to every branch - // (to generate IFJMP instead of nested IF ELSE); - // a competent way is to do it at the IR level (building CST, etc.), it's impossible to tweak Ops for now; - // so, for every `f() { here }` of `... here; return;`, save it into a global, and handle within match/if - AnyV backup = stmt_before_immediate_return; - for (int i = 0; i < v->size() - 1; ++i) { - AnyV stmt = v->get_item(i); - AnyV next_stmt = v->get_item(i + 1); - bool next_is_empty_return = next_stmt->kind == ast_return_statement && !next_stmt->as()->has_return_value(); - stmt_before_immediate_return = next_is_empty_return && does_f_return_nothing && inlining_doesnt_prevent ? stmt : nullptr; - process_any_statement(stmt, code); - } - AnyV last_stmt = v->get_item(v->size() - 1); - stmt_before_immediate_return = is_toplevel_block && does_f_return_nothing && inlining_doesnt_prevent ? last_stmt : nullptr; - process_any_statement(last_stmt, code); - stmt_before_immediate_return = backup; - - if (!is_toplevel_block) { - code.add_debug_mark(DebugMarkScopeEnd{}); - } + FallthroughTail body{ + .outer = outer, + .statements = &v->get_items(), + .scope_block = is_toplevel_block ? nullptr : v, + .is_toplevel_block = is_toplevel_block, + }; + process_tail(&body, code); } static void process_assert_statement(V v, CodeBlob& code) { @@ -2069,7 +2182,6 @@ static void process_catch_variable(AnyExprV v_catch_var, CodeBlob& code) { } static void process_try_catch_statement(V v, CodeBlob& code) { - code.require_callxargs = true; Op& try_catch_op = code.add_try_catch(v); code.push_set_cur(try_catch_op.block0); process_any_statement(v->get_try_body(), code); @@ -2086,7 +2198,7 @@ static void process_try_catch_statement(V v, CodeBlob& process_catch_variable(catch_vars[1], code); try_catch_op.left = pre_compile_tensor(code, {catch_vars[1], catch_vars[0]}); process_block_statement(catch_body, code); - code.add_debug_mark(DebugMarkScopeEnd{}); + code.add_debug_mark(DebugMarkScopeEnd{.range = catch_body->range}); code.close_pop_cur(catch_body); } @@ -2094,39 +2206,41 @@ static void process_repeat_statement(V v, CodeBlob& code) code.add_extra_mark_location(v->keyword_range()); std::vector tmp_vars = pre_compile_expr(v->get_cond(), code, nullptr); Op& repeat_op = code.add_repeat_loop(v, tmp_vars); + LoopLoweringGuard guard(code, v->get_body()); code.push_set_cur(repeat_op.block0); process_any_statement(v->get_body(), code); code.close_pop_cur(v->get_body()); } -static void process_if_statement(V v, CodeBlob& code) { +static void process_if_statement(V v, CodeBlob& code, const FallthroughTail* tail) { code.add_extra_mark_location(v->keyword_range()); // generate the condition as-is; redundant `(... != 0)` / `(... == 0)` are stripped in `optimize_conditional_branches` // (the same below for loops, ternary, and assert) std::vector ir_cond = pre_compile_expr(v->get_cond(), code, nullptr); tolk_assert(ir_cond.size() == 1); + const TailRoutingNode* route = find_routed_branch(v, code); + tolk_assert(!tail || route); + const FallthroughTail* if_tail = tail && route->goes_to == v->get_if_body() ? tail : nullptr; + const FallthroughTail* else_tail = tail && route->goes_to == v->get_else_body() ? tail : nullptr; + if (v->get_cond()->is_always_true) { - process_any_statement(v->is_ifnot ? v->get_else_body() : v->get_if_body(), code); + const FallthroughTail* live_tail = v->is_ifnot ? else_tail : if_tail; + process_block_statement(v->is_ifnot ? v->get_else_body() : v->get_if_body(), code, live_tail); return; } if (v->get_cond()->is_always_false) { - process_any_statement(v->is_ifnot ? v->get_if_body() : v->get_else_body(), code); + const FallthroughTail* live_tail = v->is_ifnot ? if_tail : else_tail; + process_block_statement(v->is_ifnot ? v->get_if_body() : v->get_else_body(), code, live_tail); return; } Op& if_op = code.add_if_else(v, std::move(ir_cond)); code.push_set_cur(if_op.block0); - process_any_statement(v->get_if_body(), code); - if (v == stmt_before_immediate_return) { - code.add_return(v->get_if_body(), {}, code.fun_ref); - } + process_block_statement(v->get_if_body(), code, if_tail); code.close_pop_cur(v->get_if_body()); code.push_set_cur(if_op.block1); - process_any_statement(v->get_else_body(), code); - if (v == stmt_before_immediate_return) { - code.add_return(v->get_else_body(), {}, code.fun_ref); - } + process_block_statement(v->get_else_body(), code, else_tail); code.close_pop_cur(v->get_else_body()); if (v->is_ifnot) { // pre-optimized to generate IFNOT instead of IF std::swap(if_op.block0, if_op.block1); @@ -2136,6 +2250,7 @@ static void process_if_statement(V v, CodeBlob& code) { static void process_do_while_statement(V v, CodeBlob& code) { code.add_extra_mark_location(v->keyword_range()); Op& until_op = code.add_until_loop(v); + LoopLoweringGuard guard(code, v->get_body()); code.push_set_cur(until_op.block0); process_any_statement(v->get_body(), code); @@ -2155,6 +2270,7 @@ static void process_do_while_statement(V v, CodeBlob& co static void process_while_statement(V v, CodeBlob& code) { code.add_extra_mark_location(v->keyword_range()); Op& while_op = code.add_while_loop(v); + LoopLoweringGuard guard(code, v->get_body()); code.push_set_cur(while_op.block0); while_op.left = pre_compile_expr(v->get_cond(), code, nullptr); tolk_assert(while_op.left.size() == 1); @@ -2176,19 +2292,13 @@ static void process_throw_statement(V v, CodeBlob& code) { } } -static void process_return_statement(V v, CodeBlob& code) { - // it's a function we're traversing AST of; - // probably, it's called and inlined into another (outer) function, we handle this below +// the only place a function returns from: an explicit `return`, or reaching the end of a body; +// `return_vars` are from `return xxx` (for void it's empty), mutated params are prepended here +static void gen_return_from_cur_fun(CodeBlob& code, AnyV origin, std::vector return_vars) { FunctionPtr fun_ref = code.fun_ref; - TypePtr child_target_type = fun_ref->inferred_return_type; - if (fun_ref->does_return_self()) { - child_target_type = fun_ref->parameters[0].declared_type; - } - std::vector return_vars = pre_compile_expr(v->get_return_value(), code, child_target_type); - - if (fun_ref->does_return_self()) { - return_vars = {}; + if (fun_ref->does_return_self() && fun_ref->does_mutate_self()) { + return_vars = {}; // `return self` from `mutate self` will put `self` in mutated_vars below } if (fun_ref->has_mutate_params()) { std::vector mutated_vars; @@ -2200,29 +2310,36 @@ static void process_return_statement(V v, CodeBlob& code) return_vars.insert(return_vars.begin(), mutated_vars.begin(), mutated_vars.end()); } - // if fun_ref is called and inlined into a parent, assign a result instead of generating a return statement - if (code.inline_rvect_out) { - code.inline_return_stmt_out = v; - code.add_let(v, *code.inline_rvect_out, std::move(return_vars)); + // `return xxx` when inlining function is actually assigning `rvect_out = ir_vars_xxx` + if (code.inlining) { + // sizes differ when falling off the end of a non-void function (this is unreachable point then) + if (!return_vars.empty() && return_vars.size() == code.inlining->rvect_out.size()) { + code.add_let(origin, code.inlining->rvect_out, std::move(return_vars)); + } + code.add_debug_mark(CodeBlob::create_mark_leave_fun(fun_ref, origin, code.inlining->rvect_out)); } else { - code.add_return(v, std::move(return_vars), code.fun_ref); + code.add_return(origin, std::move(return_vars), fun_ref); } } -// append "return" (void) to the end of the function -// if it's not reachable, it will be dropped -// (IR cfg reachability may differ from FlowContext in case of "never" types, so there may be situations, -// when IR will consider this "return" reachable and leave it, but actually execution will never reach it) -static void append_implicit_return_statement(CodeBlob& code) { - std::vector mutated_vars; - if (code.fun_ref->has_mutate_params()) { - for (const LocalVarData& p_sym: code.fun_ref->parameters) { - if (p_sym.is_mutate_parameter()) { - mutated_vars.insert(mutated_vars.end(), p_sym.ir_idx.begin(), p_sym.ir_idx.end()); - } - } - } - code.add_return(code.fun_ref->ident_anchor, std::move(mutated_vars), code.fun_ref); +static void process_break_statement(V v, CodeBlob& code) { + tolk_assert(code.current_loop); + code.add_debug_mark(DebugMarkScopeEnd{.range = code.current_loop->body->range}); + code.add_break_from_loop(v); +} + +static void process_continue_statement(V, CodeBlob&) { + tolk_assert(false && "continue must be absorbed by process_tail"); +} + +static void process_return_statement(V v, CodeBlob& code) { + FunctionPtr fun_ref = code.fun_ref; + TypePtr child_target_type = fun_ref->does_return_self() + ? fun_ref->parameters[0].declared_type + : fun_ref->inferred_return_type; + + std::vector ir_return_vars = pre_compile_expr(v->get_return_value(), code, child_target_type); + gen_return_from_cur_fun(code, v, std::move(ir_return_vars)); } @@ -2240,6 +2357,10 @@ void process_any_statement(AnyV v, CodeBlob& code) { return process_do_while_statement(v->as(), code); case ast_while_statement: return process_while_statement(v->as(), code); + case ast_break_statement: + return process_break_statement(v->as(), code); + case ast_continue_statement: + return process_continue_statement(v->as(), code); case ast_throw_statement: return process_throw_statement(v->as(), code); case ast_assert_statement: @@ -2270,7 +2391,7 @@ static void convert_function_body_to_CodeBlob(FunctionPtr fun_ref, FunctionBodyC rvect_import.insert(rvect_import.end(), ir_param.begin(), ir_param.end()); param_i.mutate()->assign_ir_idx(std::move(ir_param)); } - blob->add_import_fun_params(fun_ref->ident_anchor, rvect_import, fun_ref, DebugMarkEnterFunction{ + blob->add_import_fun_params(fun_ref->ident_anchor, rvect_import, DebugMarkEnterFunction{ .fun_ref = fun_ref, .is_inlined = false, .is_builtin = false, @@ -2288,12 +2409,15 @@ static void convert_function_body_to_CodeBlob(FunctionPtr fun_ref, FunctionBodyC }); } - if (fun_ref->name == "onInternalMessage") { + if (fun_ref->is_entrypoint() && fun_ref->is_onInternalMessage()) { handle_onInternalMessage_codegen_start(fun_ref, rvect_import, *blob, fun_ref->ident_anchor); } process_block_statement(v_body, *blob); - append_implicit_return_statement(*blob); + // append empty "return" to the end of the function; if it's not reachable, it will be dropped + // (IR cfg reachability may differ from FlowContext in case of "never" types, so there may be situations, + // when IR will consider this "return" reachable and leave it, but actually execution will never reach it) + gen_return_from_cur_fun(*blob, fun_ref->ident_anchor, {}); blob->close_blk(v_body); code_body->set_code(blob); @@ -2331,7 +2455,8 @@ static void convert_asm_body_to_AsmOp(FunctionPtr fun_ref, FunctionBodyAsm* asm_ class UpdateArgRetOrderConsideringStackWidth final { public: static bool should_visit_function(FunctionPtr fun_ref) { - return !fun_ref->is_generic_function() && (!fun_ref->ret_order.empty() || !fun_ref->arg_order.empty()); + return fun_ref->is_asm_function() && !fun_ref->is_generic_function() && + (!fun_ref->ret_order.empty() || !fun_ref->arg_order.empty()); } static void start_visiting_function(FunctionPtr fun_ref, V v_function) { @@ -2345,7 +2470,7 @@ class UpdateArgRetOrderConsideringStackWidth final { // do not allow `mutate` combined with non-primitives arg_order shuffling: impractical but hard to follow if (has_arg_width_not_1 && total_arg_mutate_width) { - err("width not 1 and mutate").fire(fun_ref->ident_anchor, fun_ref); + err("width not 1 and mutate").fire(fun_ref); } // example: `fun f(a: int, b: (int, (int, int)), c: int)` with `asm (b a c)` @@ -2385,7 +2510,7 @@ class UpdateArgRetOrderConsideringStackWidth final { class ConvertASTToLegacyOpVisitor final { public: static bool should_visit_function(FunctionPtr fun_ref) { - return !fun_ref->is_generic_function(); + return !fun_ref->is_generic_function() && (fun_ref->is_code_function() || fun_ref->is_asm_function()); } static void start_visiting_function(FunctionPtr fun_ref, V) { diff --git a/tolk/pipe-check-const-expr.cpp b/tolk/pipe-check-const-expr.cpp index 21857a1454..3bce274e2a 100644 --- a/tolk/pipe-check-const-expr.cpp +++ b/tolk/pipe-check-const-expr.cpp @@ -29,6 +29,12 @@ namespace tolk { +static Error err_duplicate_enum_value(const td::RefInt256& dup_val, EnumDefPtr enum_ref, EnumMemberPtr prev_member_ref) { + std::string val_str = dup_val->to_dec_string(); + return err("duplicate value `{}` in enum `{}`", val_str, enum_ref) + .with_secondary(prev_member_ref, "this member also equals to {}", val_str); +} + class ConstantExpressionsChecker final : public ASTVisitorFunctionBody { void visit(V v) override { @@ -74,7 +80,7 @@ void pipeline_check_constant_expressions() { } } // and for default values of parameters - for (FunctionPtr fun_ref : get_all_not_builtin_functions()) { + for (FunctionPtr fun_ref : get_all_functions()) { for (int i = 0; i < fun_ref->get_num_params(); ++i) { LocalVarPtr param_ref = &fun_ref->get_param(i); if (param_ref->has_default_value() && !fun_ref->is_generic_function()) { @@ -86,8 +92,14 @@ void pipeline_check_constant_expressions() { // assign `enum` members values (either auto-compute sequentially or use manual initializers) for (EnumDefPtr enum_ref : get_all_declared_enums()) { std::vector values = calculate_enum_members_with_values(enum_ref); - for (EnumMemberPtr member_ref : enum_ref->members) { - member_ref->mutate()->assign_computed_value(values[member_ref->member_idx]); + for (size_t i = 0; i < enum_ref->members.size(); ++i) { + td::RefInt256 cur_val = values[i]; + for (size_t j = 0; j < i; ++j) { + if (td::cmp(cur_val, enum_ref->members[j]->computed_value) == 0) { + err_duplicate_enum_value(cur_val, enum_ref, enum_ref->members[j]).collect(enum_ref->members[i]); + } + } + enum_ref->members[i]->mutate()->assign_computed_value(std::move(cur_val)); } } diff --git a/tolk/pipe-check-inferred-types.cpp b/tolk/pipe-check-inferred-types.cpp index c96db65ca3..d2b98f7a3c 100644 --- a/tolk/pipe-check-inferred-types.cpp +++ b/tolk/pipe-check-inferred-types.cpp @@ -25,13 +25,28 @@ namespace tolk { static bool expect_integer(AnyExprV v_inferred); -static std::string expression_as_string(AnyExprV v) { - if (auto v_ref = v->try_as()) { - if (v_ref->sym->try_as() || v_ref->sym->try_as()) { - return "variable `" + static_cast(v_ref->get_identifier()->name) + "`"; +struct TypePatternVariantMatch { + int variant_idx; + bool is_subtype_violated; + TypePtr variant_type; +}; + +// given `type MyUnion = IntAlias | slice`, we can use `v is IntAlias` and `v is int` (narrowed subtype) +// but for `int | slice`, we can NOT use `v is IntAlias` (subtype extension is violated) +static TypePatternVariantMatch calculate_type_pattern_variant_match(TypePtr subject_type, TypePtr pattern_type) { + if (const TypeDataUnion* subject_union = subject_type->unwrap_alias()->try_as()) { + int variant_idx = subject_union->get_variant_equal_to(pattern_type); // it's runtime equality + if (variant_idx == -1) { + return {-1, false, nullptr}; } + TypePtr variant_type = subject_union->variants[variant_idx]; + return {variant_idx, !SubtypeDistance::calc_between(variant_type, pattern_type).is_applicable(), variant_type}; } - return "expression"; + + if (!subject_type->equal_to(pattern_type)) { + return {-1, false, nullptr}; + } + return {0, !SubtypeDistance::calc_between(subject_type, pattern_type).is_applicable(), subject_type}; } // make a general error on type mismatch; for example, "can not assign `cell` to `slice`"; @@ -99,15 +114,6 @@ static Error err_not_bool_in_unary_not(AnyExprV unary_expr) { return err_cannot_apply_operator("!", unary_expr); } -GNU_ATTRIBUTE_NOINLINE -static void warning_condition_always_true_or_false(FunctionPtr cur_f, SrcRange keyword_range, AnyExprV cond, const char* operator_name) { - bool no_warning = cond->kind == ast_bool_const || cond->kind == ast_int_const; - if (no_warning) { // allow `while(true)` without a warning - return; - } - err("condition of {} is always {}", operator_name, cond->is_always_true).warning(keyword_range, cur_f); -} - // given fun `f` and a call `f(a,b,c)`, check that argument count is expected; // (parameters may have default values, so it's not as trivial as to compare params and args size) static void check_arguments_count_at_fun_call(FunctionPtr cur_f, V v, FunctionPtr called_f, AnyExprV self_obj) { @@ -128,10 +134,26 @@ static void check_arguments_count_at_fun_call(FunctionPtr cur_f, Vdoes_accept_self() && self_obj) { // static method `Point.create(...)` called as `p.create()` err("method `{}` can not be called via dot\n(it's a static method, it does not accept `self`)", called_f).collect(v->get_callee(), cur_f); } else if (n_max_params < n_arguments) { - err("too many arguments in call to `{}`, expected {}, have {}", called_f, n_max_params - delta_self, n_arguments - delta_self).collect(v->get_arg_list(), cur_f); + err("too many arguments in call to `{}`, expected {}, have {}", called_f, n_max_params - delta_self, n_arguments - delta_self) + .with_secondary(called_f, "function declared here") + .collect(v->get_arg(n_max_params - delta_self), cur_f); } else if (n_arguments < n_min_params) { - err("too few arguments in call to `{}`, expected {}, have {}", called_f, n_min_params - delta_self, n_arguments - delta_self).collect(v->get_arg_list(), cur_f); + err("too few arguments in call to `{}`, expected {}, have {}", called_f, n_min_params - delta_self, n_arguments - delta_self) + .with_secondary(called_f, "function declared here") + .collect(v->get_arg_list(), cur_f); + } +} + +// mutate writeback uses equal_to(), plus alias identity when both sides are aliases +static bool mutate_types_compatible(TypePtr from, TypePtr to) { + if (!from->equal_to(to)) { + return false; + } + if (from->try_as() && to->try_as()) { + // only when both aliases: `tuple.push` is okay for `array.push`, even though distance == 1 + return SubtypeDistance::calc_between(from, to).get_distance() == 0; } + return true; } // given `f(x: mutate int?)` and a call `f(expr)`, check that `int?` is assignable to expr_type @@ -149,14 +171,14 @@ static void check_function_argument_mutate_back(FunctionPtr cur_f, TypePtr arg_t } // here, in checking mutations, we will emit an error if this back-assignment is incompatible; - // we don't allow passing `int` to mutate `coins` and similar: not can_rhs_be_assigned(), but equal_to() - bool ok = arg_type_orig->equal_to(param_type); + // we don't allow passing `int` to mutate `coins` and similar: not can_rhs_be_assigned(), but mutate_types_compatible() + bool ok = mutate_types_compatible(arg_type_orig, param_type); if (!ok) { // the only exception, if we originally have `var x: int|builder`, and `x` is smart-cast to `builder`, // we allow calling method for `builder`; we also don't allow intersection between unions if (const TypeDataUnion* orig_union = arg_type_orig->unwrap_alias()->try_as()) { TypePtr only_t = orig_union->calculate_exact_variant_to_fit_rhs(param_type); - ok = only_t != nullptr && only_t->equal_to(param_type) && arg_type_before_mutate->equal_to(param_type); + ok = only_t != nullptr && mutate_types_compatible(only_t, param_type) && mutate_types_compatible(arg_type_before_mutate, param_type); } } @@ -172,9 +194,8 @@ static void check_function_argument_mutate_back(FunctionPtr cur_f, TypePtr arg_t // make an error on `var n = null` // technically it's correct, type of `n` is TypeDataNullLiteral, but it's not what the user wanted // so, it's better to see an error on assignment, that later, on `n` usage and types mismatch -// (most common is situation above, but generally, `var (x,n) = xn` where xn is a tensor with 2-nd always-null, can be) -static Error err_assign_always_null_to_variable(LocalVarPtr assigned_var, bool is_assigned_null_literal) { - return err("can not infer type of `{}`, it's always null\nspecify its type with `{}: `{}", assigned_var, assigned_var, (is_assigned_null_literal ? " or use `null as `" : "")); +static Error err_assign_null_literal_to_variable(LocalVarPtr assigned_var) { + return err("can not infer type of `{}`, it's always null\n""hint: specify its type with `{}: ` or use `null as `", assigned_var, assigned_var); } // handle __expect_type(expr, "type") call @@ -184,7 +205,7 @@ static void handle_possible_compiler_internal_call(FunctionPtr cur_f, Vfun_maybe; tolk_assert(fun_ref && fun_ref->is_builtin()); - // prohibit calling built-ins `__dict.XXX`, `builder.__storeVarInt`, etc. + // prohibit calling compiler-only built-ins like `__dict.XXX` and `__storeVarInt` if (!fun_ref->name.starts_with("__expect")) { err("internal compiler functions are not allowed to be called").fire(v, cur_f); } @@ -262,16 +283,16 @@ static bool check_eq_neq_operator(TypePtr lhs_type, TypePtr rhs_type, bool& not_ // given `fun Some.packToBuilder`, check that it's declared correctly static void check_declared_packToBuilder(FunctionPtr f_pack) { - bool declared_correctly = f_pack->does_accept_self() && !f_pack->does_mutate_self() + bool declared_correctly = f_pack->does_accept_self() && !f_pack->does_mutate_self() && !f_pack->does_return_self() && f_pack->get_num_params() == 2 && f_pack->has_mutate_params() && f_pack->get_param(1).declared_type == TypeDataBuilder::create() && f_pack->inferred_return_type->equal_to(TypeDataVoid::create()); if (!declared_correctly) { - err("method `{}` is declared incorrectly\n""hint: it must accept 2 parameters and return nothing:\n""> fun {}(self, mutate b: builder)", f_pack, f_pack).collect(f_pack->ident_anchor, f_pack); + err("method `{}` is declared incorrectly\n""hint: it must accept 2 parameters and return nothing:\n""> fun {}(self, mutate b: builder)", f_pack, f_pack).collect(f_pack); } bool is_receiver_ok = f_pack->receiver_type->try_as() || f_pack->receiver_type->try_as() || f_pack->receiver_type->try_as(); if (!is_receiver_ok) { - err("this method can not be declared for type `{}`\n""hint: custom pack/unpack can be declared only for type aliases and structures", f_pack->receiver_type).collect(f_pack->ident_anchor, f_pack); + err("this method can not be declared for type `{}`\n""hint: custom pack/unpack can be declared only for type aliases and structures", f_pack->receiver_type).collect(f_pack); } } @@ -282,11 +303,11 @@ static void check_declared_unpackFromSlice(FunctionPtr f_unpack) { && f_unpack->get_param(0).declared_type == TypeDataSlice::create() && f_unpack->inferred_return_type->equal_to(f_unpack->receiver_type); if (!declared_correctly) { - err("method `{}` is declared incorrectly\n""hint: it must accept 1 parameter and return an object:\n""> fun {}(mutate s: slice): {}", f_unpack, f_unpack, f_unpack->receiver_type).collect(f_unpack->ident_anchor, f_unpack); + err("method `{}` is declared incorrectly\n""hint: it must accept 1 parameter and return an object:\n""> fun {}(mutate s: slice): {}", f_unpack, f_unpack, f_unpack->receiver_type).collect(f_unpack); } bool is_receiver_ok = f_unpack->receiver_type->try_as() || f_unpack->receiver_type->try_as() || f_unpack->receiver_type->try_as(); if (!is_receiver_ok) { - err("this method can not be declared for type `{}`\n""hint: custom pack/unpack can be declared only for type aliases and structures", f_unpack->receiver_type).collect(f_unpack->ident_anchor, f_unpack); + err("this method can not be declared for type `{}`\n""hint: custom pack/unpack can be declared only for type aliases and structures", f_unpack->receiver_type).collect(f_unpack); } } @@ -405,7 +426,7 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { std::string hint = ""; const TypeDataUnion* to_union = cast_to->unwrap_alias()->try_as(); if (to_union && to_union->or_null && cast_from->can_be_casted_with_as_operator(to_union->or_null)) { - hint = "\n""use an intermediate cast: `xxx as " + to_union->or_null->as_human_readable() + " as " + cast_to->as_human_readable() + "`"; + hint = "\n""hint: use an intermediate cast: `xxx as " + to_union->or_null->as_human_readable() + " as " + cast_to->as_human_readable() + "`"; } err("type `{}` can not be cast to `{}`{}", cast_from, cast_to, hint).collect(v, cur_f); } @@ -416,18 +437,19 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { TypePtr rhs_type = v->type_node->resolved_type; if (rhs_type->unwrap_alias()->try_as()) { // `v is T1 | T2` / `v is T?` is disallowed - err("union types are not allowed, use concrete types in `is`").collect(v, cur_f); + err("union types are not allowed, use concrete types in `is`").collect(v->type_node, cur_f); return; } - if ((v->is_always_true && !v->is_negated) || (v->is_always_false && v->is_negated)) { - err("{} is always `{}`, this condition is always {}", expression_as_string(v->get_expr()), rhs_type, v->is_always_true).warning(v, cur_f); + TypePtr expr_type = v->get_expr()->inferred_type; + TypePatternVariantMatch match = calculate_type_pattern_variant_match(expr_type, rhs_type); + if (match.is_subtype_violated) { + err("wrong pattern matching: use `{}` instead of `{}`", match.variant_type, rhs_type).collect(v, cur_f); } + if ((v->is_always_false && !v->is_negated) || (v->is_always_true && v->is_negated)) { if (v->get_expr()->inferred_type == TypeDataUnknown::create()) { err("operator `is` does not work for `unknown`, it works for union types only").collect(v, cur_f); - } else { - err("{} of type `{}` can never be `{}`, this condition is always {}", expression_as_string(v->get_expr()), v->get_expr()->inferred_type, rhs_type, v->is_always_true).warning(v, cur_f); } } } @@ -439,7 +461,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { // operator `!` used for always-null (proven by smart casts, for example), it's an error err("operator `!` used for always null expression").collect(v, cur_f); } - // if operator `!` used for non-nullable, probably a warning should be printed } void visit(V v) override { @@ -486,7 +507,9 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { V arg_i = v->get_arg(i); TypePtr param_type = param_i.declared_type; if (!param_type->can_rhs_be_assigned(arg_i->inferred_type)) { - err_type_mismatch("can not pass {src} to {dst}", arg_i->inferred_type, param_type).collect(arg_i, cur_f); + err_type_mismatch("can not pass {src} to {dst}", arg_i->inferred_type, param_type) + .with_secondary(¶m_i, "parameter declared here") + .collect(arg_i, cur_f); } if (param_i.is_mutate_parameter()) { check_function_argument_mutate_back(cur_f, arg_i->inferred_type, arg_i->get_expr(), false); @@ -526,8 +549,8 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { err_type_mismatch("can not assign {src} to variable of type {dst}", rhs_type, declared_type).collect(err_loc, cur_f); } } else { - if (rhs_type == TypeDataNullLiteral::create()) { - err_assign_always_null_to_variable(lhs_var->var_ref->try_as(), corresponding_maybe_rhs && corresponding_maybe_rhs->kind == ast_null_keyword).collect(err_loc, cur_f); + if (rhs_type == TypeDataNullLiteral::create() && corresponding_maybe_rhs && corresponding_maybe_rhs->kind == ast_null_keyword) { + err_assign_null_literal_to_variable(lhs_var->var_ref->try_as()).collect(err_loc, cur_f); } } return; @@ -549,7 +572,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { for (int i = 0; i < lhs_tensor->size(); ++i) { process_assignment_lhs(lhs_tensor->get_item(i), rhs_type_tensor->items[i], rhs_tensor_maybe ? rhs_tensor_maybe->get_item(i) : nullptr); } - return; } // `[v1, v2] = rhs` / `var [v1, v2] = rhs` (rhs may be `[1,2]` or `shapedTupleVar`) @@ -559,19 +581,27 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { err("can not assign `{}` to `[...]`", rhs_type).collect(err_loc, cur_f); return; } - // i-th component compatibility will be checked automatically below + V rhs_shaped_maybe = corresponding_maybe_rhs ? corresponding_maybe_rhs->try_as() : nullptr; + for (int i = 0; i < lhs_shaped->size(); ++i) { + process_assignment_lhs(lhs_shaped->get_item(i), rhs_type_shaped->items[i], rhs_shaped_maybe ? rhs_shaped_maybe->get_item(i) : nullptr); + } } // here is `v = rhs` (just assignment, not `var v = rhs`) / `a.0 = rhs` / `getObj(z=f()).0 = rhs` etc. // types were already inferred, so just check their compatibility // for strange lhs like `f() = rhs` type checking will pass, but will fail lvalue check later if (!lhs->inferred_type->can_rhs_be_assigned(rhs_type)) { - if (lhs->try_as()) { - err_type_mismatch("can not assign {src} to variable of type {dst}", rhs_type, lhs->inferred_type).collect(err_loc, cur_f); - } else if (lhs->try_as()) { - err_type_mismatch("can not assign {src} to field of type {dst}", rhs_type, lhs->inferred_type).collect(err_loc, cur_f); + if (auto as_ref = lhs->try_as()) { + err_type_mismatch("can not assign {src} to variable of type {dst}", rhs_type, lhs->inferred_type) + .with_secondary(as_ref->sym, "variable declared here") + .collect(err_loc, cur_f); + } else if (auto as_dot = lhs->try_as(); as_dot && as_dot->is_target_struct_field()) { + err_type_mismatch("can not assign {src} to field of type {dst}", rhs_type, lhs->inferred_type) + .with_secondary(std::get(as_dot->target), "field declared here") + .collect(err_loc, cur_f); } else { - err_type_mismatch("can not assign {src} to {dst}", rhs_type, lhs->inferred_type).collect(err_loc, cur_f); + err_type_mismatch("can not assign {src} to {dst}", rhs_type, lhs->inferred_type) + .collect(err_loc, cur_f); } } } @@ -588,7 +618,9 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { TypePtr expr_type = v->get_return_value()->inferred_type; if (!cur_f->inferred_return_type->can_rhs_be_assigned(expr_type)) { - err_type_mismatch("can not convert type {src} to return type {dst}", expr_type, cur_f->inferred_return_type).collect(v->get_return_value(), cur_f); + err_type_mismatch("can not convert type {src} to return type {dst}", expr_type, cur_f->inferred_return_type) + .with_secondary(cur_f->return_type_node, "return type declared here") + .collect(v->get_return_value(), cur_f); } } @@ -615,20 +647,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (!expect_boolean(cond)) { err_not_bool_as_condition("if", cond).collect(cond, cur_f); } - - if (cond->is_always_true || cond->is_always_false) { - warning_condition_always_true_or_false(cur_f, cond->range, cond, "ternary operator"); - return; - } - - AnyExprV when_true = v->get_when_true(); - AnyExprV when_false = v->get_when_false(); - if (!v->inferred_type->can_rhs_be_assigned(when_true->inferred_type)) { - err_type_mismatch("can not convert type {src} to ternary result type {dst}", when_true->inferred_type, v->inferred_type).collect(when_true, cur_f); - } - if (!v->inferred_type->can_rhs_be_assigned(when_false->inferred_type)) { - err_type_mismatch("can not convert type {src} to ternary result type {dst}", when_false->inferred_type, v->inferred_type).collect(when_false, cur_f); - } } void visit(V v) override { @@ -659,7 +677,7 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { // for `map`, every item must be `[k,v]`, e.g. `map [ [1,true], [2,false] ]` auto ith_square = ith_v->try_as(); if (!ith_square || ith_square->size() != 2) { - err("invalid `[...]` constructor for `map`: each item must be `[key, value]`\n""example:\n""> var m: map = [ [1,true], [2,false] ];").collect(ith_v, cur_f); + err("invalid `[...]` constructor for `map`: each item must be `[key, value]`\n""hint: for example:\n""> var m: map = [ [1,true], [2,false] ];").collect(ith_v, cur_f); } else { if (!h_map->TKey->can_rhs_be_assigned(ith_square->get_item(0)->inferred_type)) { err_type_mismatch("invalid `[...]` constructor: can not convert {src} to {dst}", ith_square->get_item(0)->inferred_type, h_map->TKey).collect(ith_square->get_item(0), cur_f); @@ -703,15 +721,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { const TypeDataEnum* subject_enum = subject_type->unwrap_alias()->try_as(); const TypeDataUnion* subject_union = subject_type->unwrap_alias()->try_as(); - if (!v->is_statement()) { - for (int i = 0; i < v->get_arms_count(); ++i) { - AnyExprV arm_body = v->get_arm(i)->get_body(); - if (!v->inferred_type->can_rhs_be_assigned(arm_body->inferred_type)) { - err_type_mismatch("can not convert type {src} to match result type {dst}", arm_body->inferred_type, v->inferred_type).collect(arm_body, cur_f); - } - } - } - std::vector covered_variants; // union variant indexes; for non-union, the only matching type is 0 std::vector covered_enum; // for `match` over an enum, we want it to be exhaustive @@ -731,8 +740,11 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (lhs_type->unwrap_alias()->try_as()) { err("wrong pattern matching: union types are not allowed, use concrete types in `match`").collect(v_arm->get_pattern_expr(), cur_f); } - int variant_idx = subject_union ? subject_union->get_variant_idx(lhs_type) : (subject_type->equal_to(lhs_type) ? 0 : -1); - if (variant_idx == -1) { + TypePatternVariantMatch match = calculate_type_pattern_variant_match(subject_type, lhs_type); + int variant_idx = match.is_subtype_violated ? -1 : match.variant_idx; + if (match.is_subtype_violated) { + err("wrong pattern matching: use `{}` instead of `{}`", match.variant_type, lhs_type).collect(v_arm->get_pattern_expr(), cur_f); + } else if (variant_idx == -1) { err("wrong pattern matching: `{}` is not a variant of `{}`", lhs_type, subject_type).collect(v_arm->get_pattern_expr(), cur_f); } bool is_duplicated = variant_idx != -1 && std::find(covered_variants.begin(), covered_variants.end(), variant_idx) != covered_variants.end(); @@ -839,7 +851,9 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { parent::visit(v->get_init_val()); if (!v->field_ref->declared_type->can_rhs_be_assigned(v->get_init_val()->inferred_type)) { - err_type_mismatch("can not assign {src} to field of type {dst}", v->get_init_val()->inferred_type, v->field_ref->declared_type).collect(v->get_init_val(), cur_f); + err_type_mismatch("can not assign {src} to field of type {dst}", v->get_init_val()->inferred_type, v->field_ref->declared_type) + .with_secondary(v->field_ref, "field declared here") + .collect(v->get_init_val(), cur_f); } } @@ -850,10 +864,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (!expect_boolean(cond)) { err_not_bool_as_condition("if", cond).collect(cond, cur_f); } - - if (cond->is_always_true || cond->is_always_false) { - warning_condition_always_true_or_false(cur_f, v->keyword_range(), cond, "`if`"); - } } void visit(V v) override { @@ -872,10 +882,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (!expect_boolean(cond)) { err_not_bool_as_condition("while", cond).collect(cond, cur_f); } - - if (cond->is_always_true || cond->is_always_false) { - warning_condition_always_true_or_false(cur_f, v->keyword_range(), cond, "`while`"); - } } void visit(V v) override { @@ -885,10 +891,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (!expect_boolean(cond)) { err_not_bool_as_condition("while", cond).collect(cond, cur_f); } - - if (cond->is_always_true || cond->is_always_false) { - warning_condition_always_true_or_false(cur_f, v->keyword_range(), cond, "`do while`"); - } } void visit(V v) override { @@ -912,22 +914,6 @@ class CheckInferredTypesVisitor final : public ASTVisitorFunctionBody { if (!expect_thrown_code(v->get_thrown_code()->inferred_type)) { err("thrown excNo of `assert` must be an integer, got `{}`", v->get_thrown_code()->inferred_type).collect(v->get_thrown_code(), cur_f); } - - if (cond->is_always_true || cond->is_always_false) { - warning_condition_always_true_or_false(cur_f, v->keyword_range(), cond, "`assert`"); - } - } - - void visit(V v) override { - parent::visit(v); - - if (v->first_unreachable) { - // it's essential to print "unreachable code" warning AFTER type checking - // (printing it while inferring might be a false positive if types are incorrect, due to smart casts for example) - // a more correct approach would be to access cfg here somehow, but since cfg is now available only while inferring, - // a special v->first_unreachable was set specifically for this warning (again, which is correct if types match) - err("unreachable code").warning(v->first_unreachable, cur_f); - } } public: diff --git a/tolk/pipe-check-on-message.cpp b/tolk/pipe-check-on-message.cpp new file mode 100644 index 0000000000..285c0a67ce --- /dev/null +++ b/tolk/pipe-check-on-message.cpp @@ -0,0 +1,121 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . +*/ +#include "ast.h" +#include "ast-visitor.h" +#include "type-system.h" + +/* + * This pipe validates bodies of message entrypoints: + * > fun onInternalMessage(in: InMessage) + * > fun onBouncedMessage(in: InMessageBounced) + * > fun onExternalMessage([slice]) + * + * This is validation-only, no transformation is done. + * `in.field` is represented as special IR at lowering. + */ + +namespace tolk { + +static void validate_onBouncedMessage(FunctionPtr f) { + if (f->inferred_return_type != TypeDataVoid::create() && f->inferred_return_type != TypeDataNever::create()) { + err("`onBouncedMessage` should return `void`").fire(f); + } + if (f->get_num_params() != 1) { + err("`onBouncedMessage` should have one parameter `InMessageBounced`").fire(f); + } + const auto* t_struct = f->get_param(0).declared_type->try_as(); + if (!t_struct || t_struct->struct_ref->name != "InMessageBounced") { + err("`onBouncedMessage` should have one parameter `InMessageBounced`").fire(f); + } +} + +static void validate_onExternalMessage(FunctionPtr f) { + bool no_param_or_slice = f->get_num_params() == 0 || + (f->get_num_params() == 1 && f->get_param(0).declared_type == TypeDataSlice::create()); + if (!no_param_or_slice) { + err("`onExternalMessage` should have one parameter `slice`").fire(f); + } +} + +class CheckOnMessageVisitor final : public ASTVisitorFunctionBody { + LocalVarPtr param_ref = nullptr; // `in` for `fun onInternalMessage(in: InMessage)` + + void visit(V v) override { + // don't allow `var v = in` or passing `in` to another function (only `in.someField` is allowed) + if (v->sym == param_ref) { + err("using `{}` as an object is prohibited, because `InMessage` is a built-in struct, its fields are mapped to TVM instructions\n""hint: use `{}.senderAddress` and other fields directly", param_ref, param_ref).fire(v, cur_f); + } + parent::visit(v); + } + + void visit(V v) override { + if (v->get_obj()->kind == ast_reference && v->get_obj()->as()->sym == param_ref && v->is_target_struct_field()) { + // `body` / `bouncedBody` may be used as lvalue; other fields are read-only + if (v->is_lvalue && v->get_field_name() != "body" && v->get_field_name() != "bouncedBody") { + err("modifying an immutable variable\n""hint: fields of InMessage can be used for reading only").fire(v, cur_f); + } + // do not visit the child `ast_reference`: `in.senderAddress` is valid + return; + } + + parent::visit(v); + } + + void visit(V v) override { + for (LocalVarPtr captured_var_ref : v->captured_vars) { + if (captured_var_ref == param_ref) { + err("capturing `InMessage` in a lambda is prohibited").fire(v, cur_f); + } + } + parent::visit(v); + } + +public: + bool should_visit_function(FunctionPtr fun_ref) override { + if (!fun_ref->is_entrypoint()) { // quick false + return false; + } + return fun_ref->is_onInternalMessage() || fun_ref->is_onExternalMessage() || fun_ref->is_onBouncedMessage(); + } + + void on_enter_function(V v_function) override { + param_ref = nullptr; + if (cur_f->is_onExternalMessage()) { + validate_onExternalMessage(cur_f); + return; + } + if (cur_f->is_onBouncedMessage()) { + validate_onBouncedMessage(cur_f); + } + param_ref = &cur_f->parameters[0]; + } + + void on_exit_function(V v_function) override { + if (cur_f->is_onInternalMessage() || cur_f->is_onBouncedMessage()) { + // replace `in:InMessage` with `in.body:slice`, preserving parameters[0] pointer for ast_reference::sym + tolk_assert(cur_f->get_num_params() == 1); + cur_f->mutate()->parameters[0] = LocalVarData("in.body", cur_f->ident_anchor, TypeDataSlice::create(), nullptr, 0, 0); + } + } +}; + +void pipeline_check_onInternalMessage() { + CheckOnMessageVisitor visitor; + visit_ast_of_all_functions(visitor); +} + +} // namespace tolk diff --git a/tolk/pipe-check-private-fields.cpp b/tolk/pipe-check-private-fields.cpp index 88647b31c1..22686fe233 100644 --- a/tolk/pipe-check-private-fields.cpp +++ b/tolk/pipe-check-private-fields.cpp @@ -22,7 +22,8 @@ namespace tolk { static Error err_private_field_used_outside_method(StructPtr struct_ref, StructFieldPtr field_ref) { - return err("field `{}.{}` is private", struct_ref, field_ref); + return err("field `{}.{}` is private", struct_ref, field_ref) + .with_secondary(field_ref, "field declared here"); } static bool is_private_field_usage_allowed(FunctionPtr cur_f, StructPtr struct_ref) { diff --git a/tolk/pipe-check-pure-impure.cpp b/tolk/pipe-check-pure-impure.cpp deleted file mode 100644 index ed154caab6..0000000000 --- a/tolk/pipe-check-pure-impure.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - This file is part of TON Blockchain source code. - - TON Blockchain is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - TON Blockchain is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with TON Blockchain. If not, see . -*/ -#include "ast.h" -#include "ast-visitor.h" -#include "compilation-errors.h" - -/* - * This pipe checks for impure operations inside pure functions. - * It happens after type inferring (after methods binding) since it operates fun_ref of calls. - * It does not check for globals modification, it's done in another pipe (see a comment below). - */ - -namespace tolk { - -static Error err_impure_operation_inside_pure_function() { - return err("an impure operation in a pure function"); -} - -class CheckImpureOperationsInPureFunctionVisitor final : public ASTVisitorFunctionBody { - - void visit(V v) override { - // `globalVar = rhs` is also denied in pure functions, but it's analyzed not here, - // it's checked in `pipe-check-rvalue-lvalue.cpp` to detect `(globVar!).0 = rhs` and other - // (it's the same as detecting modification of immutable variables) - parent::visit(v); - } - - void visit(V v) override { - // v is `globalF(args)` / `globalF(args)` / `obj.method(args)` / `local_var(args)` / `getF()(args)` - // `local_var(args)` is always impure, no considerations about what's there at runtime - if (!v->fun_maybe || !v->fun_maybe->is_marked_as_pure()) { - err_impure_operation_inside_pure_function().collect(v, cur_f); - } - - parent::visit(v); - } - - void visit(V v) override { - err_impure_operation_inside_pure_function().collect(v, cur_f); - } - - void visit(V v) override { - err_impure_operation_inside_pure_function().collect(v, cur_f); - } - - void visit(V v) override { - err_impure_operation_inside_pure_function().collect(v, cur_f); - } - -public: - bool should_visit_function(FunctionPtr fun_ref) override { - return fun_ref->is_code_function() && !fun_ref->is_generic_function() && fun_ref->is_marked_as_pure(); - } -}; - -void pipeline_check_pure_impure_operations() { - CheckImpureOperationsInPureFunctionVisitor visitor; - visit_ast_of_all_functions(visitor); -} - -} // namespace tolk diff --git a/tolk/pipe-check-rvalue-lvalue.cpp b/tolk/pipe-check-rvalue-lvalue.cpp index a511e0104f..a106e69682 100644 --- a/tolk/pipe-check-rvalue-lvalue.cpp +++ b/tolk/pipe-check-rvalue-lvalue.cpp @@ -36,14 +36,17 @@ namespace tolk { static Error err_modifying_immutable_variable(LocalVarPtr var_ref) { if (var_ref->param_idx == 0 && var_ref->name == "self") { - return err("modifying `self`, which is immutable by default; probably, you want to declare `mutate self`"); + return err("modifying `self`, which is immutable by default") + .with_secondary(var_ref, "probably, you want to declare `mutate self`"); } else { - return err("modifying immutable variable `{}`\n""hint: it's declared `val {}`, keyword 'val' means 'immutable'\n""hint: declare `var {}` to allow modifications", var_ref, var_ref, var_ref); + return err("modifying immutable variable `{}`\n""hint: it's declared `val {}`, keyword 'val' means 'immutable'", var_ref, var_ref) + .with_secondary(var_ref, "declared as `val` here; use `var` to allow modifications"); } } static Error err_modifying_readonly_field(StructPtr struct_ref, StructFieldPtr field_ref) { - return err("modifying readonly field `{}.{}`", struct_ref, field_ref); + return err("modifying readonly field `{}.{}`", struct_ref, field_ref) + .with_secondary(field_ref, "field declared here"); } // validate a function used as rvalue, like `var cb = f` @@ -58,6 +61,9 @@ static void validate_function_used_as_noncall(FunctionPtr cur_f, AnyExprV v, Fun if (fun_ref->does_return_self()) { err("saving `{}` into a variable is impossible, since it returns `self` and should be used as a method", fun_ref).collect(v, cur_f); } + if (fun_ref->is_builtin() && fun_ref->name.find("__") != std::string::npos) { + err("internal compiler functions are not allowed to be called").collect(v, cur_f); + } } class CheckRValueLvalueVisitor final : public ASTVisitorFunctionBody { @@ -74,14 +80,7 @@ class CheckRValueLvalueVisitor final : public ASTVisitorFunctionBody { } else if (GlobalConstPtr const_ref = sym->try_as()) { // deny `SOME_CONST = rhs` / `mutate CONST_TENSOR.0` / etc. - err("modifying immutable constant `{}`", const_ref->name).collect(range, cur_f); - - } else if (GlobalVarPtr glob_ref = sym->try_as()) { - // fire on `global = rhs` in a @pure function: it's easier to do this check here, - // because it's very similar to checking immutable variables, especially `(global!).field = rhs` - if (cur_f->is_marked_as_pure()) { - err("modifying a global `{}` in a pure function", glob_ref->name).collect(range, cur_f); - } + err("modifying immutable constant `{}`", const_ref).collect(range, cur_f); } else if (sym->try_as() || sym->try_as()) { // `Point.create = f` or `Enum.value = v` diff --git a/tolk/pipe-check-serialized-fields.cpp b/tolk/pipe-check-serialized-fields.cpp index 98cc2127d9..15331a03ea 100644 --- a/tolk/pipe-check-serialized-fields.cpp +++ b/tolk/pipe-check-serialized-fields.cpp @@ -29,7 +29,8 @@ namespace tolk { // make an error on overflow 1023 bits or 4 cells static Error err_theoretical_overflow_1023(StructPtr struct_ref, PackSize size) { bool exceeds_in_bits = size.max_bits > 1023; - return err("struct `{}` can exceed {} {} in serialization (estimated size: {}..{} {})\n\n" + return err("struct `{}` can exceed {} {} in serialization (estimated size: {}..{} {})\n" + "hint:\n" "1) either suppress it by adding an annotation:\n" "> @overflow1023_policy(\"suppress\")\n" "> struct {} {\n" @@ -40,7 +41,7 @@ static Error err_theoretical_overflow_1023(StructPtr struct_ref, PackSize size) "> struct {} {\n" "> ...\n" "> more: Cell;\n" - "> }\n", + "> }", struct_ref, exceeds_in_bits ? 1023 : 4, exceeds_in_bits ? "bits" : "refs", @@ -137,7 +138,7 @@ static bool check_enum_colon_type_to_be_intN(EnumDefPtr enum_ref, AnyTypeV colon if (const TypeDataIntN* t_intN = colon_type_node->resolved_type->try_as(); t_intN && !t_intN->is_variadic) { for (EnumMemberPtr member_ref : enum_ref->members) { if (!member_ref->computed_value->fits_bits(t_intN->n_bits, !t_intN->is_unsigned)) { - err("member `{}` = {} does not fit into `{}`", member_ref->name, member_ref->computed_value->to_dec_string(), t_intN).collect(enum_ref->ident_anchor); + err("member `{}` = {} does not fit into `{}`", member_ref, member_ref->computed_value->to_dec_string(), t_intN).collect(enum_ref); colon_valid = false; } } @@ -173,7 +174,7 @@ class CheckSerializedFieldsAndTypesVisitor final : public ASTVisitorFunctionBody PackSize size = estimate_serialization_size(t_struct); if ((size.max_bits > 1023 || size.max_refs > 4) && !size.is_unpredictable_infinity()) { if (struct_ref->overflow1023_policy == StructData::Overflow1023Policy::not_specified) { - err_theoretical_overflow_1023(struct_ref, size).collect(struct_ref->ident_anchor); + err_theoretical_overflow_1023(struct_ref, size).collect(struct_ref); } } // don't check Cell fields for overflow of T: it would be checked on load() or other interaction with T diff --git a/tolk/pipe-collect-abi.cpp b/tolk/pipe-collect-abi.cpp index fac43b0cec..a361eb0bfd 100644 --- a/tolk/pipe-collect-abi.cpp +++ b/tolk/pipe-collect-abi.cpp @@ -78,7 +78,7 @@ class CollectAbiFromBodyVisitor final : public ASTVisitorFunctionBody { if (called_f && called_f->is_builtin() && called_f->is_instantiation_of_generic_function()) { std::string_view f_name = called_f->base_fun_ref->name; - if (f_name == "map.mustGet" && v->get_num_args() > 1) { + if (f_name == "map.mustGet" && v->get_num_args() > 1) { on_assert_throw(v->get_arg(1)->get_expr()); } else if (f_name == "createMessage" && collect_outgoing_messages) { abi->register_outgoing_message(called_f->substitutedTs->typeT_at(0)); @@ -108,7 +108,7 @@ class CollectAbiFromBodyVisitor final : public ASTVisitorFunctionBody { , collect_thrown_errors(collect_thrown_errors) {} bool should_visit_function(FunctionPtr fun_ref) override { - return fun_ref->is_code_function() && fun_ref->is_really_used(); + return fun_ref->is_really_used() && (fun_ref->is_code_function() || fun_ref->is_prototype_only()); } void on_exit_function(V v_function) override { diff --git a/tolk/pipe-detect-inline-in-place.cpp b/tolk/pipe-detect-inline-in-place.cpp index 996d4c82eb..0276bac011 100644 --- a/tolk/pipe-detect-inline-in-place.cpp +++ b/tolk/pipe-detect-inline-in-place.cpp @@ -17,11 +17,15 @@ #include "ast.h" #include "ast-visitor.h" #include "compilation-errors.h" +#include "inline-return-analysis.h" +#include "pack-unpack-api.h" +#include #include +#include #include /* - * This pipe detects whether each function can be inlined in-place or not. + * This pipe detects whether each function should be inlined in-place or not. * Outcome: call `fun_ref->assign_inline_mode_in_place()` for "lightweight" or "called only once" functions, * and they will be inlined in-place while converting AST to IR (to Ops), and won't be generated to Fift. * @@ -31,40 +35,30 @@ * - if a function is tiny, inline it always * - if a function is called only once, inline it (only there, obviously) * - if a function is marked `@inline` (intended by the user), inline it in place (if possible) - * - see `should_auto_inline_if_not_prevented()` + * - see `should_auto_inline_if_not_annotated()` * - * What can prevent a function from inlining? Several reasons: + * What can prevent a function from inlining? For example: * - it's recursive * - it's used as non-call (a reference to it is taken) - * - see `is_inlining_prevented_even_if_annotated()` + * - it has non-primitive control flow with return statements (e.g. `return` from a loop) + * - all reasons are in `build_inlining_plan_for_function()`, both this pipe and lowering ask it * - * About `@inline` annotation. It means "user intention", so the compiler tries to inline it in-place - * without considering AST metrics. But anyway, something may prevent inlining (middle returns, for example). - * In this case, the desired flag is just not set; inline_mode remains inlineViaFif, we'll generate `PROCINLINE`. + * The `@inline` annotation means "user intention", so the compiler tries to inline it in-place + * without considering AST metrics. If any reason prevents inlining, an error is shown. + * So, `@inline` is either inlined or fired, it's not silently skipped if impossible. * * Besides inline detection, this pipe populates `fun_ref->n_times_called` (while building call graph). * It's used in Fift output inside comments. + * + * The same call graph is reused to assign `requires_callxargs`: a function needs CALLXARGS if it + * contains `try/catch`, or inlines in-place another function that does. */ namespace tolk { -static bool is_called_implicitly_by_compiler(FunctionPtr f) { - if (f->name == "onBouncedMessage") { - return true; - } - if (f->is_packToBuilder()) { - return f->does_accept_self() && !f->does_mutate_self() && f->get_num_params() == 2 && f->has_mutate_params(); - } - if (f->is_unpackFromSlice()) { - return !f->does_accept_self() && f->get_num_params() == 1 && f->has_mutate_params(); - } - return false; -} - // when traversing a function, collect some AST metrics used to detect whether it's lightweight struct StateWhileTraversingFunction { FunctionPtr fun_ref; - bool has_returns_in_the_middle = false; int n_statements = 0; int n_function_calls = 0; int n_binary_operators = 0; @@ -80,14 +74,25 @@ struct StateWhileTraversingFunction { + n_control_flow * 10 + n_globals * 5 + (max_block_depth - 1) * 10; } - bool is_inlining_prevented_even_if_annotated() const { - // even if user specified `@inline`, we can't do anything about recursions, for example; - // in this case, in-place inlining won't happen, we'll generate `PROCINLINE` to Fift - bool is_inside_recursion = fun_ref->n_times_called >= 9999; - return has_returns_in_the_middle || is_inside_recursion || fun_ref->is_used_as_noncall() || !fun_ref->is_code_function(); + // if a user specified `@inline`, check that it's possible; if not, show an error why + void try_inline_or_collect_error() const { + InlineReturnPlan inlining_plan = build_inlining_plan_for_function(fun_ref); + if (!inlining_plan.ok()) { + Error diagnostic = err("function `{}` can't be inlined\n""hint: `@inline` is impossible, {}", fun_ref, inlining_plan.cant_inline_because); + if (inlining_plan.cant_inline_at.is_defined()) { + diagnostic.with_secondary(inlining_plan.cant_inline_at, "this prevents inlining"); + } + diagnostic.collect(fun_ref); + } } - bool should_auto_inline_if_not_prevented() const { + // if no annotation specified, detect whether to auto-inline a function + bool should_auto_inline_if_not_annotated() const { + // a function can not be inlined (for example, it's recursive or contains `return` in a loop) + if (!build_inlining_plan_for_function(fun_ref).ok()) { + return false; + } + // if a function is called only once, inline it regardless of its size // (to prevent this, `@inline_ref` can be used, for example) if (fun_ref->n_times_called == 1) { @@ -182,29 +187,24 @@ class DetectIfToInlineFunctionInPlaceVisitor final : public ASTVisitorFunctionBo parent::visit(v); } - void visit(V v) override { - // detect if `return` the last return statement in a function's body - // (currently in-place inlining for functions with returns in the middle is not supported) - auto body_block = cur_state.fun_ref->ast_root->as()->get_body()->as(); - bool is_last_statement = body_block->get_item(body_block->size() - 1) == v; - cur_state.has_returns_in_the_middle |= !is_last_statement; - parent::visit(v); - } - public: bool should_visit_function(FunctionPtr fun_ref) override { - // unsupported or no-sense cases - if (fun_ref->is_builtin() || fun_ref->is_asm_function() || fun_ref->is_generic_function() || - fun_ref->has_tvm_method_id() || !fun_ref->arg_order.empty() || !fun_ref->ret_order.empty() || - fun_ref->is_used_as_noncall()) { + if (!fun_ref->is_code_function() || fun_ref->is_generic_function()) { return false; } - // disabled by the user - if (fun_ref->inline_mode == FunctionInlineMode::noInline || fun_ref->inline_mode == FunctionInlineMode::inlineRef) { + // has `@inline` annotation: we should check it can be inlined actually + if (fun_ref->inline_mode == FunctionInlineMode::inlineInPlace) { + return true; + } + // has other annotations, e.g. `@inline_ref` or `@noinline` + if (fun_ref->inline_mode != FunctionInlineMode::notAnnotated) { return false; } - // okay, start auto-detection; - // for functions marked `@inline` (inlineViaFif), probably we'll change to inlineInPlace + // okay, we need to auto-detect whether to inline this function; filter out obviously false + if (fun_ref->has_tvm_method_id() || fun_ref->is_used_as_noncall() || fun_ref->is_lambda()) { + return false; + } + // start auto-detection return true; } @@ -213,20 +213,9 @@ class DetectIfToInlineFunctionInPlaceVisitor final : public ASTVisitorFunctionBo } void on_exit_function(V v_function) override { - bool prevented_anyway = cur_state.is_inlining_prevented_even_if_annotated(); - bool will_inline = false; - if (cur_f->inline_mode == FunctionInlineMode::inlineViaFif) { - // if a function is marked `@inline`, so the user requested in to be inlined; - // if it's possible, do it; otherwise, leave it as `PROCINLINE` to Fift - will_inline = !prevented_anyway; - } else { - // a function is not marked `@inline` / `@inline_ref` / etc., so automatically decide - will_inline = !prevented_anyway && cur_state.should_auto_inline_if_not_prevented(); - } - - // okay, this function will be inlined, mark the flag - bool is_called = cur_f->n_times_called || is_called_implicitly_by_compiler(cur_f); - if (will_inline && is_called) { + if (cur_f->inline_mode == FunctionInlineMode::inlineInPlace) { + cur_state.try_inline_or_collect_error(); + } else if (cur_state.should_auto_inline_if_not_annotated()) { cur_f->mutate()->assign_inline_mode_in_place(); } } @@ -235,23 +224,46 @@ class DetectIfToInlineFunctionInPlaceVisitor final : public ASTVisitorFunctionBo // this visitor (called once for a function): // 1) fills call_graph[cur_f] (all function calls from cur_f) // 2) increments n_times_called +// 3) records functions that contain `try/catch` // as a result of applying it to every function, we get a full call graph and how many times each function was called; // we'll use this call graph to detect recursive components (functions within recursions can not be inlined) +// and to propagate `requires_callxargs` through in-place inline edges class CallGraphBuilderVisitor final : public ASTVisitorFunctionBody { + void register_call(FunctionPtr called_f) { + tolk_assert(!called_f->is_generic_function()); + if (called_f->is_code_function()) { + call_graph[cur_f].emplace_back(called_f); + } + called_f->mutate()->n_times_called++; + } + void visit(V v) override { if (FunctionPtr called_f = v->fun_maybe) { - if (called_f->is_code_function()) { - call_graph[cur_f].emplace_back(called_f); + register_call(called_f); + + // calling `SomeStruct.toCell()` implicitly calls custom `packToBuilder()` serializers for nested fields; + // include them in call graph to detect recursive serializers and try/catch within them + if (called_f->is_compile_time_special_gen() && called_f->is_instantiation_of_generic_function()) { + std::vector implicit_pack_unpack; + collect_recursive_pack_unpack_when_f_called(called_f, nullptr, &implicit_pack_unpack); + for (FunctionPtr implicit_f : implicit_pack_unpack) { + register_call(implicit_f); + } } - called_f->mutate()->n_times_called++; } parent::visit(v); } + void visit(V v) override { + functions_with_try_catch.insert(cur_f); + parent::visit(v); + } + public: // populated while visiting: maps [ fun_ref -> list of functions it calls ] std::unordered_map> call_graph; + std::unordered_set functions_with_try_catch; bool should_visit_function(FunctionPtr fun_ref) override { // don't include asm functions, we don't need them in calculations @@ -263,18 +275,13 @@ class CallGraphBuilderVisitor final : public ASTVisitorFunctionBody { } }; -static void detect_recursive_functions() { - // 1) build call_graph (and calculate n_times_called also) - CallGraphBuilderVisitor visitor; - visit_ast_of_all_functions(visitor); - std::unordered_map> call_graph = std::move(visitor.call_graph); - - // 2) using call_graph, detect cycles (the smallest, non-optimized algorithm, okay for our needs) +static void detect_recursive_functions(const std::unordered_map>& call_graph) { + // using call_graph, detect cycles (the smallest, non-optimized algorithm, okay for our needs) for (const auto& it : call_graph) { FunctionPtr f_start_from = it.first; std::unordered_set visited; std::function is_recursive_dfs = [&](FunctionPtr cur) -> bool { - for (FunctionPtr f_called : call_graph[cur]) { + for (FunctionPtr f_called : call_graph.at(cur)) { if (f_called == f_start_from) return true; if (!visited.insert(f_called).second) @@ -286,7 +293,31 @@ static void detect_recursive_functions() { }; if (!it.second.empty() && is_recursive_dfs(f_start_from)) { f_start_from->mutate()->n_times_called = 9999; // means "recursive" + if (f_start_from->is_packToBuilder() || f_start_from->is_unpackFromSlice()) { + err("function `{}` is recursive and leads to infinite serialization", f_start_from).collect(f_start_from); + } + } + } +} + +static void detect_requires_callxargs(const std::unordered_map>& call_graph, + const std::unordered_set& functions_with_try_catch) { + std::unordered_set requires_callxargs = functions_with_try_catch; + bool changed; + do { + changed = false; + for (const auto& [caller, callees] : call_graph) { + if (!requires_callxargs.contains(caller)) { + bool inlines_callxargs = std::any_of(callees.begin(), callees.end(), [&](FunctionPtr callee) { + return callee->is_inlined_in_place() && requires_callxargs.contains(callee); + }); + changed |= inlines_callxargs && requires_callxargs.insert(caller).second; + } } + } while (changed); + + for (FunctionPtr fun_ref : requires_callxargs) { + fun_ref->mutate()->assign_requires_callxargs(); } } @@ -312,11 +343,19 @@ class CheckExpectInlineAssertionsVisitor final : public ASTVisitorFunctionBody { }; void pipeline_detect_inline_in_place() { - detect_recursive_functions(); + // 1) we need call graph to detect recursive functions, BEFORE calculating inline + CallGraphBuilderVisitor graph_visitor; + visit_ast_of_all_functions(graph_visitor); + detect_recursive_functions(graph_visitor.call_graph); + + // 2) not auto-detect inline and check `@inline` annotations DetectIfToInlineFunctionInPlaceVisitor visitor; visit_ast_of_all_functions(visitor); CheckExpectInlineAssertionsVisitor checker; visit_ast_of_all_functions(checker); + + // 3) assign callxargs by THE SAME call graph, strictly AFTER calculating inline + detect_requires_callxargs(graph_visitor.call_graph, graph_visitor.functions_with_try_catch); } } // namespace tolk diff --git a/tolk/pipe-find-unused-symbols.cpp b/tolk/pipe-find-unused-symbols.cpp index fd8f13a51f..4ae2256bca 100644 --- a/tolk/pipe-find-unused-symbols.cpp +++ b/tolk/pipe-find-unused-symbols.cpp @@ -35,12 +35,12 @@ namespace tolk { static void mark_function_used(FunctionPtr fun_ref) { - if (!fun_ref->is_code_function() || fun_ref->is_really_used()) { // already handled + if (fun_ref->is_really_used()) { // already handled return; } - fun_ref->mutate()->assign_is_really_used(); - if (!fun_ref->is_inlined_in_place()) { + + if (fun_ref->is_code_function() && !fun_ref->is_inlined_in_place()) { std::get(fun_ref->body)->code->ops.mark_function_used_dfs(); } } diff --git a/tolk/pipe-generate-fif-output.cpp b/tolk/pipe-generate-fif-output.cpp index 6e102c7ee3..9594210302 100644 --- a/tolk/pipe-generate-fif-output.cpp +++ b/tolk/pipe-generate-fif-output.cpp @@ -32,6 +32,17 @@ namespace tolk { +GNU_ATTRIBUTE_NOINLINE +static void fire_on_method_id_collision(FunctionPtr f1, FunctionPtr f2) { + Error diagnostic = err("method_id collision: `{}` and `{}` produce the same method_id={}", f2, f1, f1->tvm_method_id); + if (f1->is_entrypoint() && f2->is_entrypoint()) { + diagnostic = err("both `main` and `onInternalMessage` are not allowed"); + } else if (f1->is_contract_getter() && f2->is_contract_getter()) { + diagnostic = err("GET methods hash collision: `{}` and `{}` produce the same method_id={}\n""hint: rename one of these functions", f2, f1, f1->tvm_method_id); + } + diagnostic.with_secondary(f2, "collides with this function").fire(f1); +} + void FunctionBodyCode::set_code(CodeBlob* code) { this->code = code; } @@ -183,9 +194,7 @@ static void output_asm_code_for_fun(std::ostream& os, FunctionPtr fun_ref, std:: const std::vector& var_names = std::get(fun_ref->body)->code->vars; const char* modifier = "PROC"; - if (fun_ref->inline_mode == FunctionInlineMode::inlineViaFif) { - modifier = "PROCINLINE"; - } else if (fun_ref->inline_mode == FunctionInlineMode::inlineRef) { + if (fun_ref->inline_mode == FunctionInlineMode::inlineRef) { modifier = "PROCREF"; } if (print_line_comments) { @@ -305,6 +314,7 @@ static void generate_output_func(std::ostream& os, FunctionPtr fun_ref) { code->print(std::cerr, 6); } } + code->materialize_immediate_returns(); code->compute_used_code_vars(); code->fwd_analyze(); code->mark_noreturn(); @@ -315,18 +325,14 @@ static void generate_output_func(std::ostream& os, FunctionPtr fun_ref) { std::cerr << "\n---------- resulting code for " << fun_ref->name << " -------------\n"; } int mode = 0; - if (fun_ref->inline_mode == FunctionInlineMode::inlineViaFif && code->ops.is_noreturn()) { - mode |= Stack::_InlineFunc; - } - if (fun_ref->inline_mode == FunctionInlineMode::inlineViaFif || fun_ref->inline_mode == FunctionInlineMode::inlineRef) { - mode |= Stack::_InlineAny; + tolk_assert(fun_ref->inline_mode != FunctionInlineMode::inlineInPlace); + if (fun_ref->inline_mode == FunctionInlineMode::inlineRef) { + mode |= Stack::_InlineRef; } try { std::vector asm_code = code->generate_asm_code(mode); - if (G_settings.optimization_level >= 2) { - asm_code = optimize_asm_code(std::move(asm_code)); - } + asm_code = optimize_asm_code(std::move(asm_code)); output_asm_code_for_fun( os, fun_ref, @@ -339,7 +345,7 @@ static void generate_output_func(std::ostream& os, FunctionPtr fun_ref) { err("generated TVM stack is too deep while compiling function `{}`.\n" "TVM can not store more than 255 elements on the stack.\n" "hint: try splitting very wide tensors/structs, storing parts in cells/tuples", - fun_ref).fire(fun_ref->ident_anchor, fun_ref); + fun_ref).fire(fun_ref); } if (G_settings.verbosity >= 2) { @@ -366,18 +372,13 @@ void pipeline_generate_fif_output(std::ostream& os) { bool has_fun_main = false; bool has_onInternalMessage = false; int n_inlined_in_place = 0; - std::vector all_contract_getters; + std::vector all_methods_with_id; for (FunctionPtr fun_ref : G.all_functions) { if (fun_ref->is_asm_function() || !fun_ref->does_need_codegen()) { n_inlined_in_place += fun_ref->is_inlined_in_place() && fun_ref->is_really_used(); continue; } - if (fun_ref->is_entrypoint()) { - has_fun_main |= fun_ref->name == "main"; - has_onInternalMessage |= fun_ref->name == "onInternalMessage"; - } - os << " "; if (fun_ref->has_tvm_method_id()) { os << fun_ref->tvm_method_id << " DECLMETHOD " << CodeBlob::fift_name(fun_ref) << "\n"; @@ -385,13 +386,17 @@ void pipeline_generate_fif_output(std::ostream& os) { os << "DECLPROC " << CodeBlob::fift_name(fun_ref) << "\n"; } - if (fun_ref->is_contract_getter()) { - for (FunctionPtr other : all_contract_getters) { + // check for collisions of method_id (must be unique: it's a key in a routing dict) + if (fun_ref->has_tvm_method_id()) { + has_fun_main |= fun_ref->name == "main"; + has_onInternalMessage |= fun_ref->name == "onInternalMessage"; + + for (FunctionPtr other : all_methods_with_id) { if (other->tvm_method_id == fun_ref->tvm_method_id) { - err("GET methods hash collision: `{}` and `{}` produce the same method_id={}. Consider renaming one of these functions.", other, fun_ref, fun_ref->tvm_method_id).fire(fun_ref->ident_anchor); + fire_on_method_id_collision(fun_ref, other); } } - all_contract_getters.push_back(fun_ref); + all_methods_with_id.push_back(fun_ref); } } @@ -402,19 +407,17 @@ void pipeline_generate_fif_output(std::ostream& os) { throw Fatal("the contract has no entrypoint; forgot `fun onInternalMessage(...)`?"); } } - if (has_fun_main && has_onInternalMessage) { - throw Fatal("both `main` and `onInternalMessage` are not allowed"); - } if (n_inlined_in_place) { os << " // " << n_inlined_in_place << " functions inlined in-place:" << "\n"; for (FunctionPtr fun_ref : G.all_functions) { - if (fun_ref->is_inlined_in_place()) { + if (fun_ref->is_inlined_in_place() && fun_ref->is_really_used()) { os << " // - " << fun_ref->name << " (" << fun_ref->n_times_called << (fun_ref->n_times_called == 1 ? " call" : " calls") << ")\n"; } } } + int n_used_globals = 0; for (GlobalVarPtr var_ref : G.all_global_vars) { if (!var_ref->is_really_used()) { if (G_settings.verbosity >= 2) { @@ -423,6 +426,10 @@ void pipeline_generate_fif_output(std::ostream& os) { continue; } + // Asm.fif short GETGLOB/SETGLOB accept only slots 1..31 + if (++n_used_globals > 31) { + err("too many global variables (more than 31)").fire(var_ref); + } os << " " << "DECLGLOBVAR " << CodeBlob::fift_name(var_ref) << "\n"; } diff --git a/tolk/pipe-infer-types-and-calls.cpp b/tolk/pipe-infer-types-and-calls.cpp index abf8f9545a..194d1f6ceb 100644 --- a/tolk/pipe-infer-types-and-calls.cpp +++ b/tolk/pipe-infer-types-and-calls.cpp @@ -119,8 +119,9 @@ static Error err_calling_asm_function_with_non1_stack_width_arg(FunctionPtr fun_ } // make an error on using lateinit variable before definite assignment -static Error err_using_lateinit_variable_uninitialized(std::string_view name) { - return err("using variable `{}` before it's definitely assigned", name); +static Error err_using_lateinit_variable_uninitialized(LocalVarPtr var_ref) { + return err("using variable `{}` before it's definitely assigned", var_ref) + .with_secondary(var_ref, "variable declared here"); } // make an error when `obj.f()`, method `f` not found, try to locate a method for another type @@ -145,7 +146,7 @@ static Error err_method_or_field_not_found(TypePtr receiver_type, std::string_vi return err("method `{}` not found for type `{}`\n(but it exists for {} {})", field_name, receiver_type, other.size() == 1 ? "type" : "types", candidate_receivers); } if (const Symbol* sym = lookup_global_symbol(field_name); sym && sym->try_as()) { - return err("method `{}` not found, but there is a global function named `{}`\n(a function should be called `foo(arg)`, not `arg.foo()`)", field_name, field_name); + return err("method `{}` not found, but there is a global function named `{}`\n""hint: a function should be called `foo(arg)`, not `arg.foo()`", field_name, field_name); } return err("method `{}` not found", field_name); } @@ -240,32 +241,19 @@ static TypePtr pick_exact_type_if_generics_omitted(TypePtr expr_type, TypePtr cm return cmp_type; } -// given `p.create` (called_receiver = Point, called_name = "create") -// look up a corresponding method (it may be `Point.create` / `Point?.create` / `T.create`) -static std::pair choose_only_method_to_call(FunctionPtr cur_f, SrcRange range, TypePtr called_receiver, std::string_view called_name) { - std::vector candidates = resolve_methods_for_call(called_receiver, called_name, true); - if (candidates.size() == 1) { - return {candidates[0].method_ref, candidates[0].substitutedTs}; - } - if (candidates.empty()) { // return nullptr, the caller side decides how to react on this - return {nullptr, GenericsSubstitutions(nullptr)}; - } - - std::ostringstream msg; - msg << "call to method `" << called_name << "` for type `" << called_receiver->as_human_readable() << "` is ambiguous\n"; +// make an error "call to method is ambiguous, valid candidates: formatted list" +GNU_ATTRIBUTE_NOINLINE +static Error err_ambiguous_receiver(TypePtr receiver_type, std::string_view method_name, const std::vector& candidates) { + Error diagnostic = err("call to method `{}` for type `{}` is ambiguous", method_name, receiver_type); for (const MethodCallCandidate& candidate : candidates) { FunctionPtr method_ref = candidate.method_ref; - msg << "candidate function: `" << method_ref->as_human_readable() << "`"; if (method_ref->is_generic_function()) { - msg << " with " << candidate.substitutedTs.as_human_readable(false); - } - if (method_ref->ident_anchor) { - msg << " (declared at " << method_ref->ident_anchor->range.stringify_start_location(false) << ")\n"; - } else if (method_ref->is_builtin()) { - msg << " (builtin)\n"; + diagnostic.with_secondary(method_ref, "candidate function: `{}` with {}", method_ref, candidate.substitutedTs.as_human_readable(false)); + } else { + diagnostic.with_secondary(method_ref, "candidate function: `{}`", method_ref); } } - err("{}", msg.str()).fire(range, cur_f); + return diagnostic; } static void check_no_unexpected_type_arguments(FunctionPtr cur_f, V v_instantiationTs) { @@ -274,6 +262,18 @@ static void check_no_unexpected_type_arguments(FunctionPtr cur_f, V return_statements; + std::vector> return_statements; + std::vector loop_stack; GNU_ATTRIBUTE_ALWAYS_INLINE static void assign_inferred_type(AnyExprV dst, AnyExprV src) { @@ -330,6 +331,10 @@ class InferTypesAndCallsAndFieldsVisitor final { return process_while_statement(v->as(), std::move(flow)); case ast_do_while_statement: return process_do_while_statement(v->as(), std::move(flow)); + case ast_break_statement: + return process_break_statement(v->as(), std::move(flow)); + case ast_continue_statement: + return process_continue_statement(v->as(), std::move(flow)); case ast_throw_statement: return process_throw_statement(v->as(), std::move(flow)); case ast_assert_statement: @@ -668,11 +673,14 @@ class InferTypesAndCallsAndFieldsVisitor final { assign_inferred_type(v, TypeDataBool::create()); if (!used_as_condition) { FlowContext out_flow = FlowContext::merge_flow(std::move(after_lhs.false_flow), std::move(after_rhs.out_flow)); + out_flow.reanchor_to(after_lhs.out_flow); return ExprFlow(std::move(out_flow), false); } - FlowContext out_flow = FlowContext::merge_flow(std::move(after_lhs.out_flow), std::move(after_rhs.out_flow)); + FlowContext out_flow = FlowContext::merge_flow(after_lhs.out_flow.clone(), std::move(after_rhs.out_flow)); FlowContext true_flow = std::move(after_rhs.true_flow); FlowContext false_flow = FlowContext::merge_flow(std::move(after_lhs.false_flow), std::move(after_rhs.false_flow)); + out_flow.reanchor_to(after_lhs.out_flow); + false_flow.reanchor_to(after_lhs.out_flow); return ExprFlow(std::move(out_flow), std::move(true_flow), std::move(false_flow)); } case tok_logical_or: { @@ -681,11 +689,14 @@ class InferTypesAndCallsAndFieldsVisitor final { assign_inferred_type(v, TypeDataBool::create()); if (!used_as_condition) { FlowContext out_flow = FlowContext::merge_flow(std::move(after_lhs.true_flow), std::move(after_rhs.out_flow)); + out_flow.reanchor_to(after_lhs.out_flow); return ExprFlow(std::move(out_flow), false); } - FlowContext out_flow = FlowContext::merge_flow(std::move(after_lhs.out_flow), std::move(after_rhs.out_flow)); + FlowContext out_flow = FlowContext::merge_flow(after_lhs.out_flow.clone(), std::move(after_rhs.out_flow)); FlowContext true_flow = FlowContext::merge_flow(std::move(after_lhs.true_flow), std::move(after_rhs.true_flow)); FlowContext false_flow = std::move(after_rhs.false_flow); + out_flow.reanchor_to(after_lhs.out_flow); + true_flow.reanchor_to(after_lhs.out_flow); return ExprFlow(std::move(out_flow), std::move(true_flow), std::move(false_flow)); } // others are mathematical: + * ... @@ -715,28 +726,26 @@ class InferTypesAndCallsAndFieldsVisitor final { ExprFlow after_true = infer_any_expr(v->get_when_true(), std::move(after_cond.true_flow), used_as_condition, hint); ExprFlow after_false = infer_any_expr(v->get_when_false(), std::move(after_cond.false_flow), used_as_condition, hint); - if (v->get_cond()->is_always_true) { - assign_inferred_type(v, v->get_when_true()); - return after_true; - } - if (v->get_cond()->is_always_false) { - assign_inferred_type(v, v->get_when_false()); - return after_false; - } - + // infer lca(lhs,rhs) regardless of whether condition is_always_true or not (not to deal with unreachable at lowering) TypeInferringUnifyStrategy branches_unifier(hint); branches_unifier.unify_with(v->get_when_true()->inferred_type); branches_unifier.unify_with(v->get_when_false()->inferred_type); if (branches_unifier.became_union_without_hint()) { // `... ? intVar : sliceVar` results in `int | slice`, probably it's not what the user expected // but do NOT show an error for `var v: T = ternary` (T is hint); it will be checked by type checker later - err("types of ternary branches are incompatible: `{}` and `{}`\n""hint: maybe, you should use ` as ` to make them identical", v->get_when_true()->inferred_type, v->get_when_false()->inferred_type).fire(v, cur_f); + err("types of ternary branches are incompatible: `{}` and `{}`\n""hint: maybe, you should use ` as ` to make them identical", v->get_when_true()->inferred_type, v->get_when_false()->inferred_type) + .with_secondary(v->get_when_true(), "this operand is `{}`", v->get_when_true()->inferred_type) + .with_secondary(v->get_when_false(), "this operand is `{}`", v->get_when_false()->inferred_type) + .collect(v, cur_f); } assign_inferred_type(v, branches_unifier.get_result()); FlowContext out_flow = FlowContext::merge_flow(std::move(after_true.out_flow), std::move(after_false.out_flow)); FlowContext true_flow = FlowContext::merge_flow(std::move(after_true.true_flow), std::move(after_false.true_flow)); FlowContext false_flow = FlowContext::merge_flow(std::move(after_true.false_flow), std::move(after_false.false_flow)); + out_flow.reanchor_to(after_cond.out_flow); + true_flow.reanchor_to(after_cond.out_flow); + false_flow.reanchor_to(after_cond.out_flow); return ExprFlow(std::move(out_flow), std::move(true_flow), std::move(false_flow)); } @@ -746,6 +755,8 @@ class InferTypesAndCallsAndFieldsVisitor final { TypePtr lhs_type = v->get_lhs()->inferred_type; TypePtr without_null_type = calculate_type_subtract_rhs_type(lhs_type, TypeDataNullLiteral::create()); + SinkExpression lhs_s_expr = extract_sink_expression_from_vertex(v->get_lhs()); + FlowContext flow_before_branching = flow.clone(); FlowContext rhs_flow = flow.clone(); if (lhs_type == TypeDataNullLiteral::create()) { // `null ?? rhs` — lhs is always null, rhs is always executed, the non-null branch is unreachable @@ -755,9 +766,9 @@ class InferTypesAndCallsAndFieldsVisitor final { rhs_flow.mark_unreachable(UnreachableKind::CantHappen); } else { // regular situation: in the lhs branch, lhs was non-null; calculate rhs branch with lhs=null - if (SinkExpression s_expr = extract_sink_expression_from_vertex(v->get_lhs())) { - flow.register_known_type(s_expr, without_null_type); - rhs_flow.register_known_type(s_expr, TypeDataNullLiteral::create()); + if (lhs_s_expr) { + flow.register_known_type(lhs_s_expr, without_null_type); + rhs_flow.register_known_type(lhs_s_expr, TypeDataNullLiteral::create()); } } rhs_flow = infer_any_expr(v->get_rhs(), std::move(rhs_flow), false, hint).out_flow; @@ -776,12 +787,16 @@ class InferTypesAndCallsAndFieldsVisitor final { if (branches_unifier.became_union_without_hint()) { // `nullableSlice ?? 0` results in `slice | int`, probably it's not what the user expected // but do NOT show an error for `var v: T = ...` (T is hint); it will be checked by type checker later - err("type of operator `??` is `{}`; probably, it's not what you expected\n""assign it to a variable `var v: = ... ?? ...` manually", branches_unifier.get_result()).fire(v, cur_f); + err("type of operator `??` is `{}`; probably, it's not what you expected\n""hint: assign it to a variable `var v: = ... ?? ...` manually", branches_unifier.get_result()) + .with_secondary(v->get_lhs(), "this operand is `{}`", lhs_type) + .with_secondary(v->get_rhs(), "this operand is `{}`", v->get_rhs()->inferred_type) + .collect(v, cur_f); } assign_inferred_type(v, branches_unifier.get_result()); } flow = FlowContext::merge_flow(std::move(flow), std::move(rhs_flow)); + flow.reanchor_to(flow_before_branching); return ExprFlow(std::move(flow), used_as_condition); } @@ -820,15 +835,22 @@ class InferTypesAndCallsAndFieldsVisitor final { return after_expr; } + SinkExpression s_expr = extract_sink_expression_from_vertex(v->get_expr()); FlowContext true_flow = after_expr.out_flow.clone(); FlowContext false_flow = after_expr.out_flow.clone(); - if (SinkExpression s_expr = extract_sink_expression_from_vertex(v->get_expr())) { + if (s_expr) { if (v->is_always_true) { false_flow.mark_unreachable(UnreachableKind::CantHappen); false_flow.register_known_type(s_expr, TypeDataNever::create()); + if (!v->is_negated) { + true_flow.register_known_type(s_expr, rhs_type); + } } else if (v->is_always_false) { true_flow.mark_unreachable(UnreachableKind::CantHappen); true_flow.register_known_type(s_expr, TypeDataNever::create()); + if (v->is_negated) { + false_flow.register_known_type(s_expr, rhs_type); + } } else if (!v->is_negated) { true_flow.register_known_type(s_expr, rhs_type); false_flow.register_known_type(s_expr, non_rhs_type); @@ -890,12 +912,11 @@ class InferTypesAndCallsAndFieldsVisitor final { } std::vector type_arguments; - type_arguments.reserve(genericTs->size()); + type_arguments.reserve(instantiationT_list->size()); for (int i = 0; i < instantiationT_list->size(); ++i) { type_arguments.push_back(instantiationT_list->get_item(i)->type_node->resolved_type); } - genericTs->append_defaults(type_arguments); - + // omitted tail arguments stay nullptr; check_and_instantiate_generic_function applies defaults return type_arguments; } @@ -905,11 +926,19 @@ class InferTypesAndCallsAndFieldsVisitor final { // example: was `var cb = t.first;` (used as reference, as non-call), instantiate `tuple.first` // returns fun_ref to instantiated function FunctionPtr check_and_instantiate_generic_function(SrcRange range, FunctionPtr fun_ref, GenericsSubstitutions&& substitutedTs) const { + // `` fills T that deduction left nullptr; anything still missing is an error + substitutedTs.rewrite_missing_with_defaults(); + for (int i = 0; i < substitutedTs.size(); ++i) { + if (substitutedTs.typeT_at(i) == nullptr) { + GenericSubstitutionsDeducing(fun_ref).err_can_not_deduce(substitutedTs.nameT_at(i)).fire(range, cur_f); + } + } + // T for asm function must be a TVM primitive (width 1), otherwise, asm would act incorrectly - if (fun_ref->is_asm_function() || fun_ref->is_builtin()) { + if (fun_ref->is_asm_function()) { for (int i = 0; i < substitutedTs.size(); ++i) { if (substitutedTs.typeT_at(i)->get_width_on_stack() != 1 && !is_allowed_asm_generic_function_with_non1_width_T(fun_ref, substitutedTs, i)) { - err_calling_asm_function_with_non1_stack_width_arg(fun_ref, substitutedTs, i).fire(range, cur_f); + err_calling_asm_function_with_non1_stack_width_arg(fun_ref, substitutedTs, i).collect(range, cur_f); } } } @@ -923,13 +952,23 @@ class InferTypesAndCallsAndFieldsVisitor final { // - either a standalone: `local_var` / `SOME_CONST` / `globalF` / `genericFn` // - or inside a call: `globalF()` / `genericFn()` / `genericFn()` / `local_var()` + if (v->sym == nullptr) { + // nullptr may be left after `pipe-resolve-identifiers` only for compiler intrinsics like `__expect_type` + // (ordinary functions and variables already have sym assigned) + const Symbol* sym = lookup_global_symbol(v->get_name()); + if (!sym || !sym->try_as()) { + err("undefined symbol `{}`", v->get_name()).fire(v->get_identifier(), cur_f); + } + v->mutate()->assign_sym(sym); + } + if (LocalVarPtr var_ref = v->sym->try_as()) { - tolk_assert(flow.smart_cast_exists(SinkExpression(var_ref))); // all local vars are presented in flow - TypePtr declared_or_smart_casted = flow.smart_cast_or_original(SinkExpression(var_ref), var_ref->declared_type); - assign_inferred_type(v, declared_or_smart_casted); + TypePtr local_type = flow.smart_cast_or(SinkExpression(var_ref), nullptr); + tolk_assert(local_type); // all local vars are presented in flow + assign_inferred_type(v, local_type); bool used_as_write = v->is_lvalue && !v->is_rvalue; - if (var_ref->is_lateinit() && declared_or_smart_casted == TypeDataNotInferred::create() && !used_as_write) { - err_using_lateinit_variable_uninitialized(v->get_name()).fire(v, cur_f); + if (var_ref->is_lateinit() && local_type == TypeDataNotInferred::create() && !used_as_write) { + err_using_lateinit_variable_uninitialized(var_ref).fire(v, cur_f); } // it might be `local_var()` also, don't fill out_f_called, we have no fun_ref, it's a call of arbitrary expression @@ -974,6 +1013,9 @@ class InferTypesAndCallsAndFieldsVisitor final { if (fun_ref->is_entrypoint()) { err("can not get reference to this function, it's a special entrypoint").fire(v, cur_f); } + if (fun_ref->is_prototype_only()) { + err("can not get reference to this function, it's a get method prototype").fire(v, cur_f); + } fun_ref->mutate()->assign_is_used_as_noncall(); get_or_infer_return_type(fun_ref); assign_inferred_type(v, fun_ref->inferred_full_type); @@ -1038,10 +1080,15 @@ class InferTypesAndCallsAndFieldsVisitor final { err_cannot_deduce_genericT(r_structT).fire(v->get_obj(), cur_f); } } - std::tie(fun_ref, substitutedTs) = choose_only_method_to_call(cur_f, v_ident->range, receiver_type, field_name); - if (!fun_ref) { + auto candidates = resolve_methods_for_call(receiver_type, field_name, true); + if (candidates.empty()) { err_method_or_field_not_found(receiver_type, field_name, out_f_called != nullptr, true).fire(v_ident, cur_f); } + if (candidates.size() > 1) { + err_ambiguous_receiver(receiver_type, field_name, candidates).fire(v_ident, cur_f); + } + fun_ref = candidates.front().method_ref; + substitutedTs = candidates.front().substitutedTs; } } // handle other (most, actually) cases: `.field` / `.method` @@ -1059,7 +1106,7 @@ class InferTypesAndCallsAndFieldsVisitor final { v->mutate()->assign_target(field_ref); TypePtr inferred_type = field_ref->declared_type; if (SinkExpression s_expr = extract_sink_expression_from_vertex(v)) { - inferred_type = flow.smart_cast_or_original(s_expr, inferred_type); + inferred_type = flow.smart_cast_or(s_expr, inferred_type); } check_no_unexpected_type_arguments(cur_f, v_instantiationTs); assign_inferred_type(v, inferred_type); @@ -1081,7 +1128,7 @@ class InferTypesAndCallsAndFieldsVisitor final { v->mutate()->assign_target(index_at); TypePtr inferred_type = t_tensor->items[index_at]; if (SinkExpression s_expr = extract_sink_expression_from_vertex(v)) { - inferred_type = flow.smart_cast_or_original(s_expr, inferred_type); + inferred_type = flow.smart_cast_or(s_expr, inferred_type); } assign_inferred_type(v, inferred_type); return ExprFlow(std::move(flow), used_as_condition); @@ -1093,7 +1140,7 @@ class InferTypesAndCallsAndFieldsVisitor final { v->mutate()->assign_target(index_at); TypePtr inferred_type = t_shaped->items[index_at]; if (SinkExpression s_expr = extract_sink_expression_from_vertex(v)) { - inferred_type = flow.smart_cast_or_original(s_expr, inferred_type); + inferred_type = flow.smart_cast_or(s_expr, inferred_type); } assign_inferred_type(v, inferred_type); return ExprFlow(std::move(flow), used_as_condition); @@ -1103,7 +1150,19 @@ class InferTypesAndCallsAndFieldsVisitor final { // check for method (`t.size` / `user.getId`); even `i.0()` can be here if `fun int.0(self)` exists // for `T.copy` / `Container.create`, substitution for T is also returned if (!fun_ref) { - std::tie(fun_ref, substitutedTs) = choose_only_method_to_call(cur_f, dot_obj->range, dot_obj->inferred_type, field_name); + auto candidates = resolve_methods_for_call(dot_obj->inferred_type, field_name, true); + if (candidates.empty()) { + // for example, obj.field is `cell` due to smart cast, but originally it's `dict`, try `dict.method` + TypePtr declared_type = calc_declared_type_before_smart_cast(dot_obj); + candidates = resolve_methods_for_call(declared_type, field_name, true); + } + if (candidates.size() > 1) { + err_ambiguous_receiver(dot_obj->inferred_type, field_name, candidates).fire(v_ident, cur_f); + } + if (candidates.size() == 1) { + fun_ref = candidates.front().method_ref; + substitutedTs = candidates.front().substitutedTs; + } } // not a field, not a method — fire an error @@ -1198,6 +1257,9 @@ class InferTypesAndCallsAndFieldsVisitor final { if (fun_ref->is_entrypoint()) { err("{} is a special entrypoint, it can not be called as a regular function", fun_ref).fire(v->get_callee(), cur_f); } + if (fun_ref->is_prototype_only()) { + err("get method `{}` is a prototype and can not be called", fun_ref).fire(v->get_callee(), cur_f); + } // so, we have a call `f(args)` or `obj.f(args)`, f is fun_ref (function / method) (code / asm / builtin) // we're going to iterate over passed arguments, and (if generic) infer substitutedTs @@ -1256,20 +1318,9 @@ class InferTypesAndCallsAndFieldsVisitor final { // same for generic methods `t.tupleAt`, need to achieve `t.tupleAt` if (fun_ref->is_generic_function()) { - // if `f(args)` was called, Ts were inferred; check that all of them are known - std::string_view nameT_unknown = deducingTs.get_first_not_deduced_nameT(); - if (!nameT_unknown.empty() && hint && !hint->has_genericT_inside() && fun_ref->declared_return_type) { - // example: `t.tupleFirst()`, T doesn't depend on arguments, but is determined by return type - // if used like `var x: int = t.tupleFirst()` / `t.tupleFirst() as int` / etc., use hint + // `t.tupleFirst()` — T does not depend on arguments, but `var x: int = t.tupleFirst()` provides it + if (!deducingTs.get_first_not_deduced_nameT().empty() && hint && !hint->has_genericT_inside() && fun_ref->declared_return_type) { deducingTs.auto_deduce_from_argument(cur_f, v->get_callee()->range, fun_ref->declared_return_type, hint); - nameT_unknown = deducingTs.get_first_not_deduced_nameT(); - } - if (!nameT_unknown.empty()) { - deducingTs.apply_defaults_from_declaration(); - nameT_unknown = deducingTs.get_first_not_deduced_nameT(); - } - if (!nameT_unknown.empty()) { - deducingTs.err_can_not_deduce(nameT_unknown).fire(v->get_callee(), cur_f); } fun_ref = check_and_instantiate_generic_function(v->get_callee()->range, fun_ref, deducingTs.flush()); } @@ -1286,17 +1337,12 @@ class InferTypesAndCallsAndFieldsVisitor final { // calling `SomeStruct.toCell()` implicitly calls custom `packToBuilder()` serializers for nested fields, // which may be generic and need to be instantiated here - if (fun_ref->is_builtin() && fun_ref->is_instantiation_of_generic_function()) { - TypePtr serialized_type = nullptr; - bool is_pack = true; - if (is_serialization_builtin_function(fun_ref, &serialized_type, &is_pack)) { - // use the traversing function that collects all nested types recursively (fields, tensor components, etc.) - std::vector un_pack_candidates; - check_struct_can_be_packed_or_unpacked(serialized_type, is_pack, nullptr, &un_pack_candidates); - for (MethodCallCandidate c : un_pack_candidates) { - if (c.is_generic() && c.substitutedTs.typeT_at(c.substitutedTs.size() - 1)) { - instantiate_generic_function(c.method_ref, std::move(c.substitutedTs)); - } + if (fun_ref->is_compile_time_special_gen() && fun_ref->is_instantiation_of_generic_function()) { + std::vector un_pack_candidates; + collect_recursive_pack_unpack_when_f_called(fun_ref, &un_pack_candidates, nullptr); + for (MethodCallCandidate c : un_pack_candidates) { + if (c.is_generic()) { + check_and_instantiate_generic_function(v->get_callee()->range, c.method_ref, std::move(c.substitutedTs)); } } } @@ -1355,7 +1401,7 @@ class InferTypesAndCallsAndFieldsVisitor final { types_list.emplace_back(item->inferred_type); } if (types_list.size() >= 64) { - err("too big tuple (64 or more elements)").fire(v, cur_f); + err("too big tuple (64 or more elements)").collect(v, cur_f); } if (v->type_node) { @@ -1387,7 +1433,7 @@ class InferTypesAndCallsAndFieldsVisitor final { unifier.unify_with(types_list[i]); } if (unifier.became_union_without_hint()) { - err("type of `[...]` is `array<{}>`; probably, it's not what you expected\n""specify the array's type manually\n""example:\n""> var x = array [...]", unifier.get_result()).fire(v, cur_f); + err("type of `[...]` is `array<{}>`; probably, it's not what you expected\n""hint: specify the array's type manually, for example:\n""> var x = array [...]", unifier.get_result()).collect(v, cur_f); } assign_inferred_type(v, TypeDataArray::create(unifier.get_result())); } @@ -1415,7 +1461,7 @@ class InferTypesAndCallsAndFieldsVisitor final { auto v_arm = v->get_arm(i); FlowContext arm_flow = infer_any_expr(v_arm->get_pattern_expr(), arms_entry_facts.clone(), false, nullptr).out_flow; if (v_arm->pattern_kind == MatchArmKind::exact_type) { - TypePtr exact_type = pick_exact_type_if_generics_omitted(v->get_subject()->inferred_type, v_arm->pattern_type_node->resolved_type); + TypePtr exact_type = pick_exact_type_if_generics_omitted(subject_type, v_arm->pattern_type_node->resolved_type); if (!exact_type) { // `match (v) { Wrapper => ... }` but can't detect T for `Wrapper` err_cannot_deduce_genericT(v_arm->pattern_type_node->resolved_type).fire(v_arm->pattern_type_node, cur_f); } @@ -1445,7 +1491,7 @@ class InferTypesAndCallsAndFieldsVisitor final { || has_type_arm // all possible types must be covered, checked in a later pipe || subject_type->unwrap_alias()->try_as(); // all enum members must be covered // special case: `match (boolVar) { true => false => }` - if (subject_type == TypeDataBool::create() && v->get_arms_count() == 2 && has_expr_arm) { + if (subject_type->unwrap_alias() == TypeDataBool::create() && v->get_arms_count() == 2 && has_expr_arm) { auto arm0 = v->get_arm(0)->get_pattern_expr()->try_as(); auto arm1 = v->get_arm(1)->get_pattern_expr()->try_as(); is_exhaustive |= arm0 && arm1 && arm0->bool_val != arm1->bool_val; @@ -1466,11 +1512,17 @@ class InferTypesAndCallsAndFieldsVisitor final { } if (branches_unifier.became_union_without_hint()) { // same as in ternary: `match (...) { t1 => someSlice, t2 => someInt }` is `int|slice`, probably unexpected - err("type of `match` was inferred as `{}`; probably, it's not what you expected\nassign it to a variable `var v: = match (...) { ... }` manually", branches_unifier.get_result()).fire(v->keyword_range(), cur_f); + Error diagnostic = err("type of `match` was inferred as `{}`; probably, it's not what you expected\n""hint: assign it to a variable `var v: = match (...) { ... }` manually", branches_unifier.get_result()); + for (int i = 0; i < v->get_arms_count(); ++i) { + auto ith_arm = v->get_arm(i); + diagnostic.with_secondary(ith_arm->get_pattern_expr(), "this branch is `{}`", ith_arm->get_body()->inferred_type); + } + diagnostic.collect(v->keyword_range(), cur_f); } assign_inferred_type(v, branches_unifier.get_result()); } + match_out_flow.reanchor_to(flow); return ExprFlow(std::move(match_out_flow), used_as_condition); } @@ -1505,7 +1557,7 @@ class InferTypesAndCallsAndFieldsVisitor final { } } if (!struct_ref || struct_ref->is_instantiation_of_LispListT()) { - err("can not detect struct name\nuse either `var v: StructName = { ... }` or `var v = StructName { ... }`").fire(v, cur_f); + err("can not detect struct name\n""hint: use `var v = StructName { ... }`").fire(v, cur_f); } // so, we have struct_ref, so we can check field names and infer values @@ -1518,7 +1570,9 @@ class InferTypesAndCallsAndFieldsVisitor final { std::string_view field_name = field_i->get_field_name(); StructFieldPtr field_ref = struct_ref->find_field(field_name); if (!field_ref) { - err("field `{}` not found in struct `{}`", field_name, struct_ref).fire(field_i->get_field_identifier(), cur_f); + err("field `{}` not found in struct `{}`", field_name, struct_ref) + .with_secondary(struct_ref, "struct declared here") + .fire(field_i->get_field_identifier(), cur_f); } field_i->mutate()->assign_field_ref(field_ref); @@ -1562,7 +1616,9 @@ class InferTypesAndCallsAndFieldsVisitor final { if (!(occurred_mask & (1ULL << field_ref->field_idx))) { bool allow_missing = field_ref->has_default_value() || field_ref->declared_type == TypeDataVoid::create(); if (!allow_missing) { - err("field `{}` missed in initialization of struct `{}`", field_ref, struct_ref).fire(SrcRange::empty_at_end(v->range), cur_f); + err("field `{}` missed in initialization of struct `{}`", field_ref, struct_ref) + .with_secondary(field_ref, "field declared here") + .collect(v, cur_f); } } } @@ -1602,9 +1658,10 @@ class InferTypesAndCallsAndFieldsVisitor final { std::vector full_params_types; full_params_types.reserve(v->captured_vars.size() + params_types.size()); for (LocalVarPtr captured_var_ref : v->captured_vars) { - TypePtr captured_type = flow.smart_cast_or_original(SinkExpression(captured_var_ref), captured_var_ref->declared_type); + TypePtr captured_type = flow.smart_cast_or(SinkExpression(captured_var_ref), nullptr); + tolk_assert(captured_type != nullptr); if (captured_var_ref->is_lateinit() && captured_type == TypeDataNotInferred::create()) { - err_using_lateinit_variable_uninitialized(captured_var_ref->name).fire(v, cur_f); + err_using_lateinit_variable_uninitialized(captured_var_ref).fire(v, cur_f); } full_params_types.push_back(captured_type); } @@ -1637,13 +1694,7 @@ class InferTypesAndCallsAndFieldsVisitor final { } FlowContext process_block_statement(V v, FlowContext&& flow) { - // we'll print a warning if after some statement, control flow became unreachable - // (but don't print a warning if it's already unreachable, for example we're inside always-false if) - bool initially_unreachable = flow.is_unreachable(); for (AnyV item : v->get_items()) { - if (flow.is_unreachable() && !initially_unreachable && !v->first_unreachable && item->kind != ast_empty_statement) { - v->mutate()->assign_first_unreachable(item); // a warning will be printed later, after type checking - } flow = process_any_statement(item, std::move(flow)); } return flow; @@ -1661,7 +1712,7 @@ class InferTypesAndCallsAndFieldsVisitor final { flow.mark_unreachable(UnreachableKind::ReturnStatement); if (!cur_f->declared_return_type) { - return_statements.push_back(v->get_return_value()); // for future unification + return_statements.push_back(v); // for future unification } return flow; } @@ -1673,19 +1724,26 @@ class InferTypesAndCallsAndFieldsVisitor final { FlowContext true_flow = process_any_statement(v->get_if_body(), std::move(after_cond.true_flow)); FlowContext false_flow = process_any_statement(v->get_else_body(), std::move(after_cond.false_flow)); - return FlowContext::merge_flow(std::move(true_flow), std::move(false_flow)); + FlowContext out_flow = FlowContext::merge_flow(std::move(true_flow), std::move(false_flow)); + out_flow.reanchor_to(after_cond.out_flow); + return out_flow; } FlowContext process_repeat_statement(V v, FlowContext&& flow) { // in `repeat` (as opposed to `while`), a condition is not boolean, it's a number flow = infer_any_expr(v->get_cond(), std::move(flow), false).out_flow; FlowContext loop_entry_facts = flow.clone(); + loop_stack.emplace_back(); // infer until loop-entry facts reach a fixed point while (true) { + loop_stack.back().reset_in_fixpoint(loop_entry_facts); FlowContext body_out = process_any_statement(v->get_body(), flow.clone()); - FlowContext next_flow = FlowContext::merge_flow(loop_entry_facts.clone(), std::move(body_out)); + FlowContext back_edge = FlowContext::merge_flow(std::move(body_out), loop_stack.back().continue_flow.clone()); + FlowContext next_flow = FlowContext::merge_flow(loop_entry_facts.clone(), std::move(back_edge)); if (next_flow.equivalent_to(flow)) { - return next_flow; + FlowContext exit_flow = FlowContext::merge_flow(std::move(next_flow), std::move(loop_stack.back().break_flow)); + loop_stack.pop_back(); + return exit_flow; } flow = std::move(next_flow); } @@ -1693,15 +1751,19 @@ class InferTypesAndCallsAndFieldsVisitor final { FlowContext process_while_statement(V v, FlowContext&& flow) { // infer until loop-entry facts reach a fixed point - // also remember, we don't have a `break` statement, that's why when loop exits, condition became false FlowContext loop_entry_facts = flow.clone(); + loop_stack.emplace_back(); while (true) { + loop_stack.back().reset_in_fixpoint(loop_entry_facts); ExprFlow after_cond = infer_any_expr(v->get_cond(), flow.clone(), true); FlowContext body_out = process_any_statement(v->get_body(), std::move(after_cond.true_flow)); - FlowContext next_flow = FlowContext::merge_flow(loop_entry_facts.clone(), std::move(body_out)); + FlowContext back_edge = FlowContext::merge_flow(std::move(body_out), loop_stack.back().continue_flow.clone()); + FlowContext next_flow = FlowContext::merge_flow(loop_entry_facts.clone(), std::move(back_edge)); if (next_flow.equivalent_to(flow)) { v->get_cond()->mutate()->assign_always_true_or_false(after_cond.get_always_true_false_state()); - return std::move(after_cond.false_flow); + FlowContext exit_flow = FlowContext::merge_flow(std::move(after_cond.false_flow), std::move(loop_stack.back().break_flow)); + loop_stack.pop_back(); + return exit_flow; } flow = std::move(next_flow); } @@ -1710,18 +1772,43 @@ class InferTypesAndCallsAndFieldsVisitor final { FlowContext process_do_while_statement(V v, FlowContext&& flow) { // infer until loop-entry facts reach a fixed point FlowContext loop_entry_facts = flow.clone(); + loop_stack.emplace_back(); while (true) { + loop_stack.back().reset_in_fixpoint(loop_entry_facts); FlowContext body_out = process_any_statement(v->get_body(), flow.clone()); - ExprFlow after_cond = infer_any_expr(v->get_cond(), std::move(body_out), true); + FlowContext cond_input = FlowContext::merge_flow(std::move(body_out), loop_stack.back().continue_flow.clone()); + ExprFlow after_cond = infer_any_expr(v->get_cond(), std::move(cond_input), true); FlowContext next_flow = FlowContext::merge_flow(loop_entry_facts.clone(), std::move(after_cond.true_flow)); if (next_flow.equivalent_to(flow)) { v->get_cond()->mutate()->assign_always_true_or_false(after_cond.get_always_true_false_state()); - return std::move(after_cond.false_flow); + FlowContext exit_flow = FlowContext::merge_flow(std::move(after_cond.false_flow), std::move(loop_stack.back().break_flow)); + loop_stack.pop_back(); + return exit_flow; } flow = std::move(next_flow); } } + FlowContext process_break_statement(V, FlowContext&& flow) { + if (!loop_stack.empty()) { + // when empty, a later loop checker pipe will fire "break used outside a loop" + LoopFlowFrame& loop = loop_stack.back(); + loop.break_flow = FlowContext::merge_flow(std::move(loop.break_flow), flow.clone()); + } + flow.mark_unreachable(UnreachableKind::BreakStatement); + return flow; + } + + FlowContext process_continue_statement(V, FlowContext&& flow) { + if (!loop_stack.empty()) { + // when empty, a later loop checker pipe will fire "continue used outside a loop" + LoopFlowFrame& loop = loop_stack.back(); + loop.continue_flow = FlowContext::merge_flow(std::move(loop.continue_flow), flow.clone()); + } + flow.mark_unreachable(UnreachableKind::ContinueStatement); + return flow; + } + FlowContext process_throw_statement(V v, FlowContext&& flow) { flow = infer_any_expr(v->get_thrown_code(), std::move(flow), false).out_flow; flow = infer_any_expr(v->get_thrown_arg(), std::move(flow), false).out_flow; @@ -1755,7 +1842,7 @@ class InferTypesAndCallsAndFieldsVisitor final { // `arg` is a curious thing, it can be any TVM primitive, so assign unknown to it // hence, using `fInt(arg)` (int from parameter is a target type) or `arg as slice` works well // it's not truly correct, because `arg as (int,int)` also compiles, but can never happen, but let it be user responsibility - FlowContext catch_flow = std::move(before_try); + FlowContext catch_flow = before_try.clone(); tolk_assert(v->get_catch_expr()->size() == 2); std::vector types_list = {TypeDataInt::create(), TypeDataUnknown::create()}; catch_flow = process_catch_variable(v->get_catch_expr()->get_item(0), types_list[0], std::move(catch_flow)); @@ -1763,7 +1850,9 @@ class InferTypesAndCallsAndFieldsVisitor final { assign_inferred_type(v->get_catch_expr(), TypeDataTensor::create(std::move(types_list))); FlowContext catch_end = process_any_statement(v->get_catch_body(), std::move(catch_flow)); - return FlowContext::merge_flow(std::move(try_end), std::move(catch_end)); + FlowContext out_flow = FlowContext::merge_flow(std::move(try_end), std::move(catch_end)); + out_flow.reanchor_to(before_try); + return out_flow; } FlowContext process_expression_statement(AnyExprV v, FlowContext&& flow) { @@ -1782,8 +1871,23 @@ class InferTypesAndCallsAndFieldsVisitor final { assign_inferred_type(fun_ref, inferred_return_type, TypeDataFunCallable::create(std::move(params_types), inferred_return_type)); } + void start_visiting_parameter_defaults(FunctionPtr fun_ref) { + FlowContext params_flow; + for (int i = 0; i < fun_ref->get_num_params(); ++i) { + LocalVarPtr param_ref = &fun_ref->get_param(i); + if (param_ref->has_default_value()) { + params_flow = infer_any_expr(param_ref->default_value, std::move(params_flow), false, param_ref->declared_type).out_flow; + } + params_flow.register_known_type(SinkExpression(param_ref), param_ref->declared_type); + } + } + void start_visiting_function(FunctionPtr fun_ref, V v_function) { + return_statements.clear(); TypePtr inferred_return_type = fun_ref->declared_return_type; + if (fun_ref->is_prototype_only()) { + tolk_assert(fun_ref->declared_return_type); // checked at lexer + } if (fun_ref->is_code_function()) { FlowContext body_start; for (const LocalVarData& param : fun_ref->parameters) { @@ -1806,13 +1910,16 @@ class InferTypesAndCallsAndFieldsVisitor final { if (fun_ref->does_return_self()) { return_unifier.unify_with(fun_ref->parameters[0].declared_type); } - bool has_void_returns = false; - bool has_non_void_returns = false; - for (AnyExprV return_expr : return_statements) { - TypePtr cur_type = return_expr->inferred_type; // `return expr` - type of expr; `return` - void + V last_void_return = nullptr; + V last_non_void_return = nullptr; + for (V v_return : return_statements) { + TypePtr cur_type = v_return->get_return_value()->inferred_type; // `return expr` - type of expr; `return` - void return_unifier.unify_with(cur_type); - has_void_returns |= cur_type == TypeDataVoid::create(); - has_non_void_returns |= cur_type != TypeDataVoid::create(); + if (cur_type == TypeDataVoid::create()) { + last_void_return = v_return; + } else { + last_non_void_return = v_return; + } } inferred_return_type = return_unifier.get_result(); if (inferred_return_type == nullptr) { // if no return statements at all @@ -1820,18 +1927,20 @@ class InferTypesAndCallsAndFieldsVisitor final { } if (!body_end.is_unreachable() && inferred_return_type != TypeDataVoid::create()) { - err("missing return").fire(SrcRange::empty_at_end(v_function->range), fun_ref); - } - if (has_void_returns && has_non_void_returns) { - for (AnyExprV return_expr : return_statements) { - if (return_expr->inferred_type == TypeDataVoid::create()) { - err("mixing void and non-void returns in function `{}`", fun_ref).fire(return_expr, fun_ref); - } - } + err("missing return").collect(SrcRange::empty_at_end(v_function->range), fun_ref); } - if (return_unifier.became_union_without_hint()) { + if (last_void_return && last_non_void_return) { + err("mixing void and non-void returns") + .with_secondary(last_non_void_return->keyword_range(), "this return has a value") + .collect(last_void_return->keyword_range(), fun_ref); + } else if (return_unifier.became_union_without_hint()) { // `return intVar` + `return sliceVar` results in `int | slice`, probably unexpected - err("function `{}` calculated return type is `{}`; probably, it's not what you expected\ndeclare `fun (...): ` manually", fun_ref, inferred_return_type).fire(v_function->get_identifier(), fun_ref); + Error diagnostic = err("function `{}` calculated return type is `{}`; probably, it's not what you expected\n""hint: declare `fun (...): ` manually", fun_ref, inferred_return_type); + for (V v_return : return_statements) { + TypePtr cur_type = v_return->get_return_value()->inferred_type; + diagnostic.with_secondary(v_return->keyword_range(), "this return is `{}`", cur_type); + } + diagnostic.fire(v_function->get_identifier(), fun_ref); } } @@ -1841,14 +1950,7 @@ class InferTypesAndCallsAndFieldsVisitor final { } // visit default values of parameters; to correctly track symbols in `fun f(a: int, b: int = a)`, use flow context - FlowContext params_flow; - for (int i = 0; i < fun_ref->get_num_params(); ++i) { - LocalVarPtr param_ref = &fun_ref->get_param(i); - if (param_ref->has_default_value()) { - params_flow = infer_any_expr(param_ref->default_value, std::move(params_flow), false, param_ref->declared_type).out_flow; - } - params_flow.register_known_type(SinkExpression(param_ref), param_ref->declared_type); - } + start_visiting_parameter_defaults(fun_ref); assign_fun_full_type(fun_ref, inferred_return_type); fun_ref->mutate()->assign_is_type_inferring_done(); @@ -1876,19 +1978,6 @@ class InferTypesAndCallsAndFieldsVisitor final { } }; -class LaunchInferTypesAndMethodsOnce final { -public: - static bool should_visit_function(FunctionPtr fun_ref) { - // since inferring can be requested on demand, prevent second execution from a regular pipeline launcher - return !fun_ref->is_type_inferring_done() && !fun_ref->is_generic_function(); - } - - static void start_visiting_function(FunctionPtr fun_ref, V v_function) { - InferTypesAndCallsAndFieldsVisitor visitor; - visitor.start_visiting_function(fun_ref, v_function); - } -}; - // infer return type "on demand" // example: `fun f() { return g(); } fun g() { ... }` // when analyzing `f()`, we need to infer what fun_ref=g returns @@ -1906,7 +1995,7 @@ static void infer_and_save_return_type_of_function(FunctionPtr fun_ref) { // prevent recursion of untyped functions, like `fun f() { return g(); } fun g() { return f(); }` bool contains = std::find(called_stack.begin(), called_stack.end(), fun_ref) != called_stack.end(); if (contains) { - err("could not infer return type of `{}`, because it appears in a recursive call chain\ndeclare `fun (...): ` manually", fun_ref).fire(fun_ref->ident_anchor, fun_ref); + err("could not infer return type of `{}`, because it appears in a recursive call chain\n""hint: declare `fun (...): ` manually", fun_ref).fire(fun_ref); } // dig into g's body; it's safe, since the compiler is single-threaded @@ -1928,7 +2017,7 @@ static void infer_and_save_type_of_constant(GlobalConstPtr const_ref) { // prevent recursion like `const a = b; const b = a` bool contains = std::find(called_stack.begin(), called_stack.end(), const_ref) != called_stack.end(); if (contains) { - err("const `{}` appears, directly or indirectly, in its own initializer", const_ref).fire(const_ref->ident_anchor); + err("const `{}` appears, directly or indirectly, in its own initializer", const_ref).fire(const_ref); } called_stack.push_back(const_ref); @@ -1940,19 +2029,29 @@ static void infer_and_save_type_of_constant(GlobalConstPtr const_ref) { } void pipeline_infer_types_and_calls_and_fields() { - // loop over user-defined functions - LaunchInferTypesAndMethodsOnce launcher; - visit_ast_of_all_functions(launcher); + // infer every function in registration order; compiler-only built-ins like `__expect_type` have no AST, + // while stdlib `builtin` functions have, they also need parameter defaults inferred from the declaration + InferTypesAndCallsAndFieldsVisitor visitor; + const std::vector& all = get_all_functions(); + for (size_t i = 0; i < all.size(); ++i) { // NOLINT(*-loop-convert) + FunctionPtr fun_ref = all[i]; // generic instantiations can be appended while inferring + // inferring could already be done while processing another function on demand + if (fun_ref->is_type_inferring_done()) { + continue; + } - // assign inferred_type to built-in functions like __throw() - for (FunctionPtr fun_ref : get_all_builtin_functions()) { - if (LaunchInferTypesAndMethodsOnce::should_visit_function(fun_ref)) { - infer_and_save_return_type_of_function(fun_ref); - } + if (fun_ref->is_generic_function()) { + if (fun_ref->is_builtin() && fun_ref->ast_root) { + visitor.start_visiting_parameter_defaults(fun_ref); + } + } else if (fun_ref->ast_root) { + visitor.start_visiting_function(fun_ref, fun_ref->ast_root->as()); + } else { + infer_and_save_return_type_of_function(fun_ref); + } } // analyze constants that weren't referenced by any function - InferTypesAndCallsAndFieldsVisitor visitor; for (GlobalConstPtr const_ref : get_all_declared_constants()) { if (!const_ref->inferred_type) { visitor.start_visiting_constant(const_ref); diff --git a/tolk/pipe-lazy-load-insertions.cpp b/tolk/pipe-lazy-load-insertions.cpp index fe6db7db75..8ac68468a3 100644 --- a/tolk/pipe-lazy-load-insertions.cpp +++ b/tolk/pipe-lazy-load-insertions.cpp @@ -16,18 +16,17 @@ */ #include "lazy-helpers.h" #include "ast.h" -#include "ast-aux-data.h" #include "ast-visitor.h" -#include "ast-replacer.h" #include "compilation-errors.h" #include "type-system.h" #include "smart-casts-cfg.h" #include "pack-unpack-api.h" +#include "inline-return-analysis.h" namespace tolk { /* - * This pipe finds `lazy` operators and inserts loading points to load only required fields just before being used. + * This pipe finds `lazy` operators and builds a LazyLoadPlan: which fields to load just before which statement. * It happens after type inferring/checking. While inferring, `lazy expr` was inferred just as expr. * There is no dedicated `Lazy` type in the type system. All the magic of laziness is calculated here. * @@ -117,7 +116,7 @@ namespace tolk { * * Some highlights and considerations: * - `lazy A.fromSlice(s)` does NOT read from slice immediately; instead, it saves the slice pointer and reads on demand - * (at "loading points" inserted by the compiler in the current pipe). + * (at "loading points" recorded in LazyLoadPlan by the current pipe). * - Options can be passed: `lazy A.fromSlice(s, {...})`, but `assertEndAfterReading` is ignored, it doesn't make sense * (because fields are read later, only required ones, and the last is preloaded rather than loaded). * There is a special method `a.forceLoadLazyObject()`, can be used inside `match` to load the variant fully. @@ -162,12 +161,13 @@ static bool does_function_satisfy_for_lazy_operator(FunctionPtr fun_ref, bool al std::string_view f_name = fun_ref->base_fun_ref->name; return f_name == "T.fromSlice" || f_name == "T.fromCell" || f_name == "Cell.load"; } - // allow `lazy loadData()`, where loadData() is a simple wrapper like - // `fun loadData() { return SomeStruct.fromCell(contract.getData()) }` + // allow `lazy loadData()`, where loadData() ends with `return SomeStruct.fromCell(...)` + // and has no other returns if (allow_wrapper && fun_ref->is_code_function() && fun_ref->get_num_params() == 0) { auto f_body = fun_ref->ast_root->as()->get_body()->as(); - if (f_body->size() == 1) { - if (auto f_returns = f_body->get_item(0)->try_as(); f_returns && f_returns->has_return_value()) { + if (!f_body->empty()) { + auto f_returns = f_body->get_item(f_body->size() - 1)->try_as(); + if (f_returns && f_returns->has_return_value() && find_first_return(f_body) == f_returns) { if (auto f_returns_call = f_returns->get_return_value()->try_as()) { return does_function_satisfy_for_lazy_operator(f_returns_call->fun_maybe, false) && fun_ref->inferred_return_type->equal_to(f_returns_call->inferred_type); @@ -449,7 +449,7 @@ struct ExprUsagesWhileCollecting { if (const TypeDataBitsN* last_bitsN = future_fields.back().field_type->try_as()) { PackSize cur_size = estimate_serialization_size(field_type); if (cur_size.min_bits == cur_size.max_bits && cur_size.max_refs == 0 && !cur_size.skipping_is_dangerous) { - TypePtr total_bitsN = TypeDataBitsN::create(last_bitsN->n_width + cur_size.max_bits, true); + TypePtr total_bitsN = TypeDataBitsN::create(last_bitsN->n_bits + cur_size.max_bits); future_fields.back().field_type = total_bitsN; continue; } @@ -460,7 +460,7 @@ struct ExprUsagesWhileCollecting { TypePtr skip_type = field_type; PackSize skip_size = estimate_serialization_size(field_type); if (skip_size.min_bits == skip_size.max_bits && skip_size.max_refs == 0 && !skip_size.skipping_is_dangerous) { - skip_type = TypeDataBitsN::create(skip_size.max_bits, true); + skip_type = TypeDataBitsN::create(skip_size.max_bits); } future_fields.emplace_back(LazyStructLoadInfo::SkipField, "`gap`", skip_type); } @@ -526,13 +526,13 @@ struct ExprUsagesWhileCollecting { }; // After collecting all vars/fields/variants usages, we should store, where exactly (in AST) which fields to load. -// Every insertion point is represented as this class, it's transformed to an AST auxiliary vertex by a replacer. +// Every insertion point is finalized into LazyLoadPlan by a top-down walk (first candidate wins). struct OneLoadingInsertionPoint { std::vector all_stmts_where_used; TypePtr union_variant; StructFieldPtr field_ref; LazyStructLoadInfo load_info; - mutable bool was_inserted_to_ast = false; + mutable bool was_added_to_plan = false; OneLoadingInsertionPoint(std::vector&& all_stmts_where_used, TypePtr union_variant, StructFieldPtr field_ref, LazyStructLoadInfo&& load_info) : all_stmts_where_used(std::move(all_stmts_where_used)) @@ -540,8 +540,8 @@ struct OneLoadingInsertionPoint { , field_ref(field_ref) , load_info(std::move(load_info)) {} - void mark_inserted_to_ast() const { - was_inserted_to_ast = true; + void mark_added_to_plan() const { + was_added_to_plan = true; } bool is_mentioned_in_stmt(AnyV stmt) const { @@ -556,7 +556,7 @@ struct LazyVarInFunction { V created_by_lazy_op; V v_lazy_match_var_itself = nullptr; // lazy `match` for the variable itself V v_lazy_match_last_field = nullptr; // lazy `match` for the last field of a struct - std::vector load_points; // a set of points where AST should be updated + std::vector load_points; // candidate statements; later one anchor is chosen per point // convert already calculated usages of "st" variable and all its fields to a final immutable representation LazyVarInFunction(FunctionPtr cur_f, LocalVarPtr var_ref, V created_by_lazy_op, ExprUsagesWhileCollecting&& var_usages) @@ -574,6 +574,7 @@ struct LazyVarInFunction { err("`lazy` will not work here, because variable `{}` is used both for lazy matching and in a non-lazy manner", var_ref).fire(created_by_lazy_op->keyword_range(), cur_f); } v_lazy_match_var_itself = var_usages.used_as_match_subj.front(); + v_lazy_match_var_itself->mutate()->assign_is_lazy_match(); load_points.reserve(var_usages.variants.size()); for (ExprUsagesWhileCollecting& variant_usages : var_usages.variants) { LazyStructLoadInfo load_info = variant_usages.calculate_hidden_struct(true); @@ -604,14 +605,15 @@ struct LazyVarInFunction { StructFieldPtr field_ref = t_struct->struct_ref->fields.back(); const ExprUsagesWhileCollecting& last_field_usages = var_usages.fields.back(); v_lazy_match_last_field = last_field_usages.used_as_match_subj.front(); - // inside `match` over a field, loading locations were not detected: insert "load all fields" into every arm + v_lazy_match_last_field->mutate()->assign_is_lazy_match(); + // inside `match` over a field, loading locations were not detected: schedule "load all fields" into every arm for (int i = 0; i < v_lazy_match_last_field->get_arms_count(); ++i) { if (auto v_arm = v_lazy_match_last_field->get_arm(i); v_arm->pattern_kind == MatchArmKind::exact_type) { TypePtr union_variant = v_arm->pattern_type_node->resolved_type; auto v_arm_body = v_arm->get_body()->get_block_statement(); if (!v_arm_body->empty()) { const TypeDataUnion* t_union = field_ref->declared_type->unwrap_alias()->try_as(); - int variant_idx = t_union->get_variant_idx(union_variant); + int variant_idx = t_union->get_variant_equal_to(union_variant); LazyStructLoadInfo load_all = last_field_usages.variants[variant_idx].generate_hidden_struct_load_all(true); load_points.emplace_back(std::vector{v_arm_body->get_item(0)}, union_variant, field_ref, std::move(load_all)); } @@ -695,7 +697,7 @@ class CollectUsagesInStatementVisitor final : public ASTVisitorFunctionBody { if (receiver_type_matches_lazy_expr && can_method_be_inlined_preserving_lazy(fun_ref)) { auto v_body_block = fun_ref->ast_root->try_as()->get_body()->try_as(); ExprUsagesWhileCollecting inner_usages = collect_expr_usages_in_block(lazy_expr->name_str + "(=self)", SinkExpression(&fun_ref->parameters[0]), lazy_expr->expr_type, v_body_block); - inner_usages.treat_match_like_read(); // nested lazy match in inlined functions doesn't work, it's not wrapped into aux vertex + inner_usages.treat_match_like_read(); // nested lazy match in inlined functions doesn't work lazy_expr->merge_with_sub_block(inner_usages); if (dot_obj->kind == ast_assign) { parent::visit(v->get_callee()); @@ -738,7 +740,7 @@ class CollectUsagesInStatementVisitor final : public ASTVisitorFunctionBody { auto v_arm = v->get_arm(i); if (v_arm->pattern_kind == MatchArmKind::exact_type) { TypePtr exact_type = v_arm->pattern_type_node->resolved_type; - int variant_idx = expr_as_union ? expr_as_union->get_variant_idx(exact_type) : 0; // match over non-union is ok + int variant_idx = expr_as_union ? expr_as_union->get_variant_equal_to(exact_type) : 0; // match over non-union is ok tolk_assert(variant_idx != -1); // in case of aliases, it may point to another StructPtr (but still equal_to) exact_type = expr_as_union ? expr_as_union->variants[variant_idx] : lazy_expr->expr_type; auto v_block = v_arm->get_body()->get_block_statement(); @@ -789,7 +791,7 @@ class CollectUsagesInStatementVisitor final : public ASTVisitorFunctionBody { // It takes care of nested try/catch, etc. // Ideally, it should calculate the only "lca" AST vertex of all usages, but it's not as easy as it seems. // Instead, `lazy_expr->needed_above_stmt` contains all statements where expr is "mentioned" (and needs to be loaded before). -// And later, traversing top-down, the first occurrence is taken, inserting an AST aux vertex right before it. +// And later, traversing top-down, the first occurrence is taken as the anchor statement in LazyLoadPlan. class CollectUsagesInBlockBottomUp { ExprUsagesWhileCollecting* lazy_expr; SinkExpression s_expr; @@ -908,6 +910,7 @@ class CollectAllLazyObjectsAndFieldsVisitor final : public ASTVisitorFunctionBod var_ref, var_ref->declared_type, rhs_lazy->inferred_type).fire(rhs_lazy->keyword_range(), cur_f); } ExprUsagesWhileCollecting var_usages = collect_expr_usages_in_block(var_ref->name, SinkExpression(var_ref), var_ref->declared_type, parent_block); + rhs_lazy->mutate()->assign_dest_var_ref(var_ref); LazyVarInFunction lazy_var(cur_f, var_ref, rhs_lazy, std::move(var_usages)); functions_with_lazy_vars[cur_f].emplace_back(std::move(lazy_var)); } @@ -916,6 +919,18 @@ class CollectAllLazyObjectsAndFieldsVisitor final : public ASTVisitorFunctionBod parent::visit(v); } + // `match (val x = lazy ...)` as an expression is unsafe (statement form is fine) + void visit(V v) override { + if (!v->is_statement()) { + if (auto v_assign = v->get_subject()->try_as()) { + if (auto rhs_lazy = v_assign->get_rhs()->try_as()) { + err("incorrect `lazy` operator usage, it's not directly assigned to a variable\n""hint: use `lazy` like this:\n> var st = lazy MyStorage.fromSlice(...)").fire(rhs_lazy->keyword_range(), cur_f); + } + } + } + parent::visit(v); + } + // check that `lazy` operator used in a correct pattern with a correct expression void visit(V v) override { for (const LazyVarInFunction& lazy_var : functions_with_lazy_vars[cur_f]) { @@ -936,63 +951,41 @@ class CollectAllLazyObjectsAndFieldsVisitor final : public ASTVisitorFunctionBod }; // Step 2: -// After visiting all functions and finding all lazy variables, this replacer updates AST, -// inserting (already calculated) load vertices. They are auxiliary vertices holding special data. -// They are handled later when transforming AST to Ops. -class LazyLoadInsertionsReplacer final : public ASTReplacerInFunctionBody { +// After visiting all functions and finding all lazy variables, walk AST top-down and record +// the first suitable statement for each load point. AST structure is not modified. +class BuildLazyLoadPlanVisitor final : public ASTVisitorFunctionBody { + LazyLoadPlan* plan = nullptr; - // `var st = lazy expr` -> save "st" (it will be used in codegen to assert "st.x" that "x" is loaded) - AnyExprV replace(V v) override { + void add_loads_before_to_plan(AnyV stmt) const { for (const LazyVarInFunction& lazy_var : functions_with_lazy_vars[cur_f]) { - if (lazy_var.created_by_lazy_op == v) { - v->mutate()->assign_dest_var_ref(lazy_var.var_ref); - return parent::replace(v); + for (const OneLoadingInsertionPoint& ins : lazy_var.load_points) { + if (!ins.was_added_to_plan && ins.is_mentioned_in_stmt(stmt)) { + plan->loads_before_statement[stmt].emplace_back(lazy_var.var_ref, ins.union_variant, ins.field_ref, ins.load_info); + ins.mark_added_to_plan(); + } } } - - tolk_assert(false); // all `lazy` operators where detected and handled } - // `{ ... }` -> `{ ... load ... }` - AnyV replace(V v) override { - std::vector new_children; // since we don't have "parent_node" and "next_child" in AST, - new_children.reserve(v->size()); // traverse every block statement and insert "load" in the middle - + void visit(V v) override { for (AnyV stmt : v->get_items()) { - for (const LazyVarInFunction& lazy_var : functions_with_lazy_vars[cur_f]) { - for (const OneLoadingInsertionPoint& ins : lazy_var.load_points) { - if (!ins.was_inserted_to_ast && ins.is_mentioned_in_stmt(stmt)) { - ASTAuxData* aux_data = new AuxData_LazyObjectLoadFields(lazy_var.var_ref, ins.union_variant, ins.field_ref, ins.load_info); - new_children.push_back(createV(createV(stmt->range), aux_data, TypeDataVoid::create())); - ins.mark_inserted_to_ast(); - } - } - } - new_children.push_back(parent::replace(stmt)); + add_loads_before_to_plan(stmt); + parent::visit(stmt); } + } - v->mutate()->assign_new_children(std::move(new_children)); - return v; + void on_enter_function(V) override { + plan = new LazyLoadPlan(); } - // `match (lazy_obj)` / `match (lazy_obj.field)` -> wrap with aux - AnyExprV replace(V v) override { + void on_exit_function(V) override { for (const LazyVarInFunction& lazy_var : functions_with_lazy_vars[cur_f]) { - bool is_lazy_match_for_union = lazy_var.v_lazy_match_var_itself == v; - if (is_lazy_match_for_union) { - ASTAuxData* aux_data = new AuxData_LazyMatchForUnion(lazy_var.var_ref, nullptr); - return createV(parent::replace(v), aux_data, v->inferred_type); - } - - bool is_lazy_match_for_last_field = lazy_var.v_lazy_match_last_field == v; - if (is_lazy_match_for_last_field) { - StructPtr struct_ref = lazy_var.var_ref->declared_type->unwrap_alias()->try_as()->struct_ref; - ASTAuxData* aux_data = new AuxData_LazyMatchForUnion(lazy_var.var_ref, struct_ref->fields.back()); - return createV(parent::replace(v), aux_data, v->inferred_type); + for (const OneLoadingInsertionPoint& ins : lazy_var.load_points) { + tolk_assert(ins.all_stmts_where_used.empty() || ins.was_added_to_plan); } } - - return parent::replace(v); + cur_f->mutate()->assign_lazy_load_plan(plan); + plan = nullptr; } public: @@ -1002,18 +995,18 @@ class LazyLoadInsertionsReplacer final : public ASTReplacerInFunctionBody { }; // Step 3: -// After modifying AST (inserting loads, lazy match, etc.), +// After the plan is built (and dest_var_ref / is_lazy_match decorations are set), // check __expect_lazy() calls, used in compiler tests as assertions. class CheckExpectLazyAssertionsVisitor final : public ASTVisitorFunctionBody { - static std::string stringify_lazy_load_above_stmt(const AuxData_LazyObjectLoadFields* aux_load) { + static std::string stringify_lazy_load_action(const LazyLoadAction& action) { static const char* action_to_str[] = {"load", "skip", "lazy match", "save immutable"}; - const LazyStructLoadInfo& load_info = aux_load->load_info; + const LazyStructLoadInfo& load_info = action.load_info; StructPtr struct_ref = load_info.hidden_struct; std::string_view last_action; - std::string result = "[" + aux_load->var_ref->name + "] "; + std::string result = "[" + action.var_ref->name + "] "; for (int i = 0; i < struct_ref->get_num_fields(); ++i) { std::string field_name = struct_ref->get_field(i)->name; if (field_name == "`gap`") { @@ -1022,13 +1015,13 @@ class CheckExpectLazyAssertionsVisitor final : public ASTVisitorFunctionBody { if (field_name == "`tail`") { field_name = "(tail)"; } - std::string_view action = action_to_str[load_info.ith_field_action[i]]; - if (action != last_action) { + std::string_view action_str = action_to_str[load_info.ith_field_action[i]]; + if (action_str != last_action) { if (result[result.size() - 2] != ']') { result += ", "; } - result += action; - last_action = action; + result += action_str; + last_action = action_str; } result += " "; result += field_name; @@ -1047,12 +1040,13 @@ class CheckExpectLazyAssertionsVisitor final : public ASTVisitorFunctionBody { tolk_assert(i + 1 < v->size() && v_expected_str && "invalid __expect_lazy"); AnyV next_stmt = v->get_item(i + 1); std::string actual; - if (auto next_aux = next_stmt->try_as()) { - if (const auto* aux_load = dynamic_cast(next_aux->aux_data)) { - actual = stringify_lazy_load_above_stmt(aux_load); - } - if (const auto* aux_match = dynamic_cast(next_aux->aux_data)) { - actual = "[" + aux_match->var_ref->name + "] " + "lazy match"; + for (const LazyLoadAction& action : cur_f->lazy_load_plan->loads_before(next_stmt)) { + actual += stringify_lazy_load_action(action); + } + if (actual.empty()) { + if (const auto* lazy_match = next_stmt->try_as(); lazy_match && lazy_match->is_lazy_match) { + SinkExpression s_expr = extract_sink_expression_from_vertex(lazy_match->get_subject()); + actual = "[" + s_expr.var_ref->name + "] " + "lazy match"; } } @@ -1075,8 +1069,8 @@ void pipeline_lazy_load_insertions() { functions_with_lazy_vars.clear(); CollectAllLazyObjectsAndFieldsVisitor collector; visit_ast_of_all_functions(collector); - LazyLoadInsertionsReplacer replacer; - replace_ast_of_all_functions(replacer); + BuildLazyLoadPlanVisitor planner; + visit_ast_of_all_functions(planner); CheckExpectLazyAssertionsVisitor checker; visit_ast_of_all_functions(checker); functions_with_lazy_vars.clear(); diff --git a/tolk/pipe-loop-break-continue.cpp b/tolk/pipe-loop-break-continue.cpp new file mode 100644 index 0000000000..fa36baf66e --- /dev/null +++ b/tolk/pipe-loop-break-continue.cpp @@ -0,0 +1,122 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#include "ast-visitor.h" +#include "compilation-errors.h" +#include "loop-control-analysis.h" + +/* + * This pipe checks break/continue placement and extra restrictions LoopContinuePlan plan does not cover: + * - break/continue outside a loop + * - inside try/catch + * - `return` in a loop that already uses `break` (*BRK occupies c1) + * + * It also asks `build_continue_plan_for_loop()` whether structural `continue` can be compiled. + */ + +namespace tolk { + +class LoopBreakContinueValidator final : public ASTVisitorFunctionBody { + struct LoopFrame { + AnyV break_stmt = nullptr; + AnyV return_stmt = nullptr; + }; + + std::vector loop_frames; + int inside_try_catch = 0; // we are in `try` or `catch` body, deny break/continue + int inside_loop_condition = 0; // we are `while(HERE)`, deny break/continue/return + + void visit(V v) override { + if (inside_loop_condition || loop_frames.empty()) { + err("`break` used outside a loop").collect(v, cur_f); + } else if (inside_try_catch) { + err("`break` is not allowed inside `try/catch`").collect(v, cur_f); + } else { + loop_frames.back().break_stmt = v; + } + } + + void visit(V v) override { + if (inside_loop_condition || loop_frames.empty()) { + err("`continue` used outside a loop").collect(v, cur_f); + } else if (inside_try_catch) { + err("`continue` is not allowed inside `try/catch`").collect(v, cur_f); + } + } + + void visit(V v) override { + if (inside_loop_condition) { + err("`return` is not allowed in a loop condition").collect(v, cur_f); + } else { + for (LoopFrame& frame : loop_frames) { + frame.return_stmt = v; + } + } + parent::visit(v); + } + + void visit(V v) override { + inside_try_catch++; + parent::visit(v); + inside_try_catch--; + } + + void analyze_loop(SrcRange keyword_range, AnyV condition, V body) { + inside_loop_condition++; + parent::visit(condition); + inside_loop_condition--; + + loop_frames.push_back(LoopFrame{}); + parent::visit(body); + const LoopFrame& frame = loop_frames.back(); + if (frame.break_stmt && frame.return_stmt) { + err("`return` is not allowed inside a loop that uses `break`") + .with_secondary(frame.break_stmt, "`break` is used here") + .collect(frame.return_stmt, cur_f); + } + LoopContinuePlan plan = build_continue_plan_for_loop(body); + if (!plan.ok()) { + err("can not compile `continue`, {}", plan.cant_continue_because) + .with_secondary(plan.cant_continue_at, "this prevents compiling `continue`") + .collect(keyword_range, cur_f); + } + loop_frames.pop_back(); + } + + void visit(V v) override { + analyze_loop(v->keyword_range(), v->get_cond(), v->get_body()); + } + + void visit(V v) override { + analyze_loop(v->keyword_range(), v->get_cond(), v->get_body()); + } + + void visit(V v) override { + analyze_loop(v->keyword_range(), v->get_cond(), v->get_body()); + } + +public: + bool should_visit_function(FunctionPtr fun_ref) override { + return fun_ref->is_code_function() && !fun_ref->is_generic_function(); + } +}; + +void pipeline_check_loop_break_continue() { + LoopBreakContinueValidator visitor; + visit_ast_of_all_functions(visitor); +} + +} // namespace tolk diff --git a/tolk/pipe-refine-lvalue-for-mutate.cpp b/tolk/pipe-refine-lvalue-for-mutate.cpp index d7746911ed..ba982703a2 100644 --- a/tolk/pipe-refine-lvalue-for-mutate.cpp +++ b/tolk/pipe-refine-lvalue-for-mutate.cpp @@ -36,19 +36,15 @@ namespace tolk { static Error err_invalid_mutate_arg_passed(FunctionPtr fun_ref, const LocalVarData& p_sym, bool arg_passed_as_mutate, AnyV arg_expr) { std::string arg_str(arg_expr->kind == ast_reference ? arg_expr->as()->get_name() : "obj"); - std::string param_name(p_sym.name); - - // built-in functions don't have parameter names, let it be `slice` / `builder` / etc. - if (param_name.empty()) { - param_name = p_sym.declared_type->as_human_readable(); - } if (p_sym.is_mutate_parameter() && !arg_passed_as_mutate) { // called `mutating_function(arg)`; suggest: `mutate arg` - return err("function `{}` mutates parameter `{}`\nyou need to specify `mutate` when passing an argument, like `mutate {}`", fun_ref, param_name, arg_str); + return err("function `{}` mutates parameter `{}`\n""hint: specify `mutate` when passing an argument, like `mutate {}`", fun_ref, p_sym.name, arg_str) + .with_secondary(&p_sym, "parameter declared here"); } else { // called `usual_function(mutate arg)` - return err("incorrect `mutate`, since `{}` does not mutate parameter `{}`", fun_ref, param_name); + return err("incorrect `mutate`, since `{}` does not mutate parameter `{}`", fun_ref, p_sym.name) + .with_secondary(&p_sym, "parameter declared here"); } } diff --git a/tolk/pipe-register-symbols.cpp b/tolk/pipe-register-symbols.cpp index 215a8e8a50..740e4b6773 100644 --- a/tolk/pipe-register-symbols.cpp +++ b/tolk/pipe-register-symbols.cpp @@ -18,6 +18,7 @@ #include "src-file.h" #include "compilation-errors.h" #include "compiler-state.h" +#include "compiler-settings.h" #include "generics-helpers.h" #include "pack-unpack-serializers.h" #include "contract-directive.h" @@ -145,7 +146,9 @@ static EnumDefPtr register_enum(V v) { for (EnumMemberPtr prev : members) { if (prev->name == member_name) { - err("redeclaration of member `{}`", member_name).fire(v_member); + err("redeclaration of member `{}`", member_name) + .with_secondary(prev, "previous declaration is here") + .fire(v_ident); } } members.emplace_back(new EnumMemberData(std::move(member_name), v_ident, i, v_member->init_value, DocCommentLines(v_member->doc_lines))); @@ -172,7 +175,9 @@ static StructPtr register_struct(V v, StructPtr base_str for (StructFieldPtr prev : fields) { if (prev->name == field_name) { - err("redeclaration of field `{}`", field_name).fire(v_field); + err("redeclaration of field `{}`", field_name) + .with_secondary(prev, "previous declaration is here") + .fire(v_ident); } } fields.emplace_back(new StructFieldData(std::move(field_name), v_ident, i, v_field->is_private, v_field->is_readonly, v_field->type_node, v_field->abi_type_node, v_field->default_value, DocCommentLines(v_field->doc_lines))); @@ -239,13 +244,16 @@ static LocalVarData register_parameter(V v, int idx) { } static FunctionPtr register_function(V v, FunctionPtr base_fun_ref = nullptr, std::string override_name = {}, const GenericsSubstitutions* substitutedTs = nullptr) { - if (v->is_builtin_function()) { - return nullptr; - } - V v_ident = v->get_identifier(); std::string_view f_identifier = v_ident->name; // function or method name + // `builtin` is a compiler/stdlib contract, not a way for user code to declare new intrinsics + if (v->is_builtin_function() && !v->range.get_src_file()->is_stdlib_file) { + return nullptr; + } + // note that `builtin` from stdlib pass the pipeline, their names/params are registered, + // and later (after all symbols are registered) their implementation is redefined, see `setup_legacy_builtins` + std::vector parameters; int n_mutate_params = 0; parameters.reserve(v->get_num_params()); @@ -265,7 +273,16 @@ static FunctionPtr register_function(V v, FunctionPtr } const GenericsDeclaration* genericTs = nullptr; // at registering it's null; will be assigned after types resolving - FunctionBody f_body = v->get_body()->kind == ast_block_statement ? static_cast(new FunctionBodyCode) : static_cast(new FunctionBodyAsm); + FunctionBody f_body = v->is_builtin_function() + ? static_cast(new FunctionBodyBuiltinStub) + : v->is_code_function() + ? static_cast(new FunctionBodyCode) + : v->is_get_prototype() + ? static_cast(new FunctionBodyPrototype) + : static_cast(new FunctionBodyAsm); + if (v->is_get_prototype() && !G_settings.allow_empty_get_fun) { + err("empty `get fun` is allowed only with --allow-empty-get-fun\n""hint: provide a function body `{ ... }`, or pass this flag for ABI-only prototypes").fire(v_ident); + } FunctionData* f_sym = new FunctionData(std::move(name), v_ident, std::move(method_name), v->receiver_type_node, v->return_type_node, std::move(parameters), 0, v->inline_mode, genericTs, substitutedTs, DocCommentLines(v->doc_lines), f_body, v); f_sym->base_fun_ref = base_fun_ref; // for `f`, here is `f`; for a lambda, a containing function @@ -325,17 +342,21 @@ static void iterate_through_file_symbols(SrcFilePtr file, FileSymbolsRegistratio } tolk_assert(file && file->ast); + bool is_imported = !ctx.import_stack.empty(); + ctx.import_stack.push_back(file); + bool should_register_symbols = ctx.registered_files.insert(file).second; + // first pass: detect `contract XXX { ... }` anywhere in a file, before processing all declarations for (AnyV v : file->ast->as()->get_toplevel_declarations()) { - if (v->kind == ast_contract_directive && !file->has_contract_directive()) { - file->mutate()->assign_contract_directive(parse_contract_directive(v)); - break; + if (v->kind == ast_contract_directive && should_register_symbols) { + auto v_contract = v->as(); + if (file->has_contract_directive()) { + err("a file can have only one `contract` directive").fire(v_contract->get_identifier()); + } + file->mutate()->assign_contract_directive(parse_contract_directive(v_contract)); } } - bool is_imported = !ctx.import_stack.empty(); - ctx.import_stack.push_back(file); - bool should_register_symbols = ctx.registered_files.insert(file).second; SrcFilePtr nearest_contract_file = ctx.find_nearest_contract_file(); std::vector> skipped_get_fun; @@ -366,7 +387,10 @@ static void iterate_through_file_symbols(SrcFilePtr file, FileSymbolsRegistratio // we do not allow `import "something"` having `get fun` inside; // we force the convention: all getters to be visible at a glance, in the contract file if (nearest_contract_file && file != nearest_contract_file) { - err("all contract entrypoints must be placed in the contract file `{}`\n""hint: keep `onInternalMessage` and `get fun` just below `contract`, not in other files", nearest_contract_file->extract_short_name()).fire(v_fun->get_identifier()); + auto v_contract = nearest_contract_file->contract_directive->v_contract->as(); + err("all contract entrypoints must be placed in the contract file `{}`\n""hint: keep `onInternalMessage` and `get fun` just below `contract`, not in other files", nearest_contract_file->extract_short_name()) + .with_secondary(v_contract->keyword_range(), "`contract` is here") + .fire(v_fun->get_identifier()); } } break; diff --git a/tolk/pipe-resolve-identifiers.cpp b/tolk/pipe-resolve-identifiers.cpp index a52ceb065b..7ccc1e11c0 100644 --- a/tolk/pipe-resolve-identifiers.cpp +++ b/tolk/pipe-resolve-identifiers.cpp @@ -22,11 +22,11 @@ /* * This pipe resolves identifiers (local variables, globals, constants, etc.) in all functions bodies. - * It happens before type inferring, but after all global symbols are registered. - * It means, that for any symbol `x` we can look up whether it's a global name or not. + * It happens before type inferring, but after all global symbols from `*.tolk` files are registered. + * Compiler intrinsics like `__expect_type` are registered later, so `v->sym` remains nullptr until type inferring. * * Example: `var x = 10; x = 20;` both `x` point to one LocalVarData. - * Example: `x = 20` undefined symbol `x` is also here (unless it's a global) + * Example: `x = 20` unknown symbol `x` remains unresolved and will be checked again during type inferring. * Variables scoping and redeclaration are also here. * Note, that `x` is stored as `ast_reference (ast_identifier "x")`. More formally, "references" are resolved. * "Reference" in AST, besides the identifier, stores optional generics instantiation. `x` is grammar-valid. @@ -38,8 +38,8 @@ * as well as `tuplePush` to be instantiated, and fun_ref to point at that exact instantiations. * * As a result of this step, - * * every V::sym is filled, pointing either to a local var/parameter, or to a global symbol - * (exceptional for function calls and methods, their references are bound later) + * * every resolvable V::sym is filled, pointing either to a local var/parameter, or to a global symbol; + * unresolved references are retried during type inferring, after compiler-only functions are registered */ namespace tolk { @@ -126,9 +126,11 @@ struct NameAndScopeResolver { } uint64_t key = key_hash(v_sym->name); - const auto& [_, inserted] = scopes.rbegin()->emplace(key, v_sym); + const auto& [it, inserted] = scopes.rbegin()->emplace(key, v_sym); if (!inserted) { - err("redeclaration of local variable `{}`", v_sym).fire(v_sym->ident_anchor); + err("redeclaration of local variable `{}`", v_sym) + .with_secondary(it->second, "previous declaration is here") + .fire(v_sym); } } }; @@ -182,7 +184,11 @@ class AssignSymInsideFunctionVisitor final : public ASTVisitorFunctionBody { void visit(V v) override { const Symbol* sym = current_scope.lookup_symbol(v->get_name()); if (!sym) { - err_undefined_symbol(v->get_identifier()).fire(v->get_identifier(), cur_f); + // compiler intrinsics like `__expect_type` are registered later, leave `v->sym = nullptr` until type inferring + if (v->get_name() == "self" || find_skipped_imported_getter(v->get_name())) { + err_undefined_symbol(v->get_identifier()).fire(v->get_identifier(), cur_f); + } + return; } LocalVarPtr var_ref = sym->try_as(); @@ -203,7 +209,8 @@ class AssignSymInsideFunctionVisitor final : public ASTVisitorFunctionBody { // for global functions, global vars and constants, `import` must exist if (!var_ref) { - bool allow_no_import = sym->is_builtin() || sym->ident_anchor->range.is_file_id_same_or_stdlib_common(v->range); + bool allow_no_import = sym->ident_anchor == nullptr || + sym->ident_anchor->range.is_file_id_same_or_stdlib_common(v->range); if (!allow_no_import) { sym->check_import_exists_when_used_from(cur_f, v); } @@ -360,7 +367,7 @@ void pipeline_resolve_identifiers_and_assign_symbols() { for (AnyV v : file->ast->as()->get_toplevel_declarations()) { if (auto v_fun = v->try_as()) { // v_fun->fun_ref may be nullptr if it's `get fun` implicitly imported and ignored because of `contract` - if (v_fun->fun_ref && visitor.should_visit_function(v_fun->fun_ref)) { + if (v_fun->fun_ref && (visitor.should_visit_function(v_fun->fun_ref) || v_fun->fun_ref->is_builtin())) { visitor.start_visiting_function(v_fun->fun_ref, v_fun); } diff --git a/tolk/pipe-resolve-types.cpp b/tolk/pipe-resolve-types.cpp index 331715146f..9a447222de 100644 --- a/tolk/pipe-resolve-types.cpp +++ b/tolk/pipe-resolve-types.cpp @@ -26,8 +26,6 @@ namespace tolk { -void patch_builtins_after_stdlib_loaded(); - /* * This pipe transforms AST of types into TypePtr. * It happens after all global symbols were registered, and all local references were bound. @@ -90,14 +88,14 @@ static TypePtr parse_intN_uintN(std::string_view strN, bool is_unsigned) { return TypeDataIntN::create(n, is_unsigned, false); } -static TypePtr parse_bytesN_bitsN(std::string_view strN, bool is_bits) { +static TypePtr parse_bitsN(std::string_view strN) { int n; auto result = std::from_chars(strN.data(), strN.data() + strN.size(), n); bool parsed = result.ec == std::errc() && result.ptr == strN.data() + strN.size(); if (!parsed || n <= 0 || n > 1023) { - return nullptr; // `bytes9999`, maybe it's user-defined alias, let it be unresolved + return nullptr; // `bits9999`, maybe it's user-defined alias, let it be unresolved } - return TypeDataBitsN::create(n, is_bits); + return TypeDataBitsN::create(n); } static TypePtr try_parse_predefined_type(std::string_view str) { @@ -153,15 +151,10 @@ static TypePtr try_parse_predefined_type(std::string_view str) { } } if (str.starts_with("bits")) { - if (TypePtr bitsN = parse_bytesN_bitsN(str.substr(4), true)) { + if (TypePtr bitsN = parse_bitsN(str.substr(4))) { return bitsN; } } - if (str.starts_with("bytes")) { - if (TypePtr bytesN = parse_bytesN_bitsN(str.substr(5), false)) { - return bytesN; - } - } return nullptr; } @@ -199,7 +192,8 @@ class TypeNodesVisitorResolver { if (const Symbol* sym = lookup_global_symbol(text)) { if (TypePtr custom_type = try_resolve_user_defined_type(cur_f, v->range, sym, allow_without_type_arguments)) { bool already_resolved = v->resolved_type != nullptr; - bool allow_no_import = sym->is_builtin() || sym->ident_anchor->range.is_file_id_same_or_stdlib_common(v->range); + bool allow_no_import = sym->ident_anchor == nullptr || + sym->ident_anchor->range.is_file_id_same_or_stdlib_common(v->range); if (!allow_no_import && !already_resolved) { sym->check_import_exists_when_used_from(cur_f, v); } @@ -278,7 +272,7 @@ class TypeNodesVisitorResolver { if (!struct_ref->genericTs) { bool contains = std::find(called_stack.begin(), called_stack.end(), struct_ref) != called_stack.end(); if (contains) { - err("type `{}` circularly references itself", struct_ref).fire(struct_ref->ident_anchor); + err("type `{}` circularly references itself", struct_ref).fire(struct_ref); } called_stack.push_back(struct_ref); RecursionGuard guard([&] { @@ -443,7 +437,7 @@ class TypeNodesVisitorResolver { // prevent recursion like `type A = B; type B = A` (we can't create TypeDataAlias without a resolved underlying type) if (!inserted) { if (!it_visited->second) { - err("type `{}` circularly references itself", alias_ref).fire(alias_ref->ident_anchor); + err("type `{}` circularly references itself", alias_ref).fire(alias_ref); } return; } @@ -537,8 +531,6 @@ class ResolveTypesInsideFunctionVisitor final : public ASTVisitorFunctionBody { } void visit(V v) override { - tolk_assert(v->sym != nullptr); - // for `f` / `f`, resolve "MyAlias" and "T" // (for function call `f()`, this v (ast_reference `f`) is callee) if (auto v_instantiationTs = v->get_instantiationTs()) { @@ -636,7 +628,7 @@ class ResolveTypesInsideFunctionVisitor final : public ASTVisitorFunctionBody { public: bool should_visit_function(FunctionPtr fun_ref) override { - return !fun_ref->is_builtin(); + return true; } void on_enter_function(V v) override { @@ -774,7 +766,7 @@ class InfiniteStructSizeDetector { bool contains = std::find(called_stack.begin(), called_stack.end(), struct_ref) != called_stack.end(); if (contains) { - err("struct `{}` size is infinity due to recursive fields", struct_ref).fire(struct_ref->ident_anchor); + err("struct `{}` size is infinity due to recursive fields", struct_ref).fire(struct_ref); } // Some nominal references intentionally defer struct fields until a later top-level pass. @@ -853,8 +845,6 @@ void pipeline_resolve_types_and_aliases() { } InfiniteStructSizeDetector::detect_and_fire_if_any_struct_is_infinite(); - - patch_builtins_after_stdlib_loaded(); } void pipeline_resolve_types_and_aliases(FunctionPtr fun_ref) { diff --git a/tolk/pipe-transform-on-message.cpp b/tolk/pipe-transform-on-message.cpp deleted file mode 100644 index c40552a957..0000000000 --- a/tolk/pipe-transform-on-message.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - This file is part of TON Blockchain source code. - - TON Blockchain is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - TON Blockchain is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with TON Blockchain. If not, see . -*/ -#include "ast.h" -#include "ast-aux-data.h" -#include "ast-replacer.h" -#include "type-system.h" - -/* - * This pipe analyzes the body of - * > fun onInternalMessage(in: InMessage) - * and transforms accesses to `in.xxx` fields. - * - * This function is transformed to - * > fun onInternalMessage(in.body) - * so, - * - accessing `in.body` with directly reference the parameter on a stack. - * - accessing `in.senderAddress` will emit `INMSG_SRC` TVM instruction. - */ - -namespace tolk { - -// detect `onInternalMessage` with a single parameter -// (there are other valid forms accepting raw slices, they are not transformed) -static bool is_onInternalMessage(FunctionPtr fun_ref) { - if (fun_ref->is_entrypoint() && (fun_ref->name == "main" || fun_ref->name == "onInternalMessage")) { - if (fun_ref->get_num_params() == 1) { - const auto* t_param = fun_ref->get_param(0).declared_type->try_as(); - return t_param && t_param->struct_ref->name == "InMessage"; - } - } - return false; -} - -// `onBouncedMessage` is only one, it's automatically embedded into `onInternalMessage` if exists -static bool is_onBouncedMessage(FunctionPtr fun_ref) { - return fun_ref->is_entrypoint() && fun_ref->name == "onBouncedMessage"; -} - -// `onExternalMessage` is only one, we'll validate its prototype -static bool is_onExternalMessage(FunctionPtr fun_ref) { - return fun_ref->is_entrypoint() && fun_ref->name == "onExternalMessage"; -} - - -class TransformOnInternalMessageReplacer final : public ASTReplacerInFunctionBody { - LocalVarPtr param_ref = nullptr; // `in` for `fun onInternalMessage(in: InMessage)` - std::string orig_in_name; // "in" (saved, since `param_ref` is a dangling pointer) - - static void validate_onBouncedMessage(FunctionPtr f) { - if (f->inferred_return_type != TypeDataVoid::create() && f->inferred_return_type != TypeDataNever::create()) { - err("`onBouncedMessage` should return `void`").fire(f->ident_anchor, f); - } - if (f->get_num_params() != 1) { - err("`onBouncedMessage` should have one parameter `InMessageBounced`").fire(f->ident_anchor, f); - } - const auto* t_struct = f->get_param(0).declared_type->try_as(); - if (!t_struct || t_struct->struct_ref->name != "InMessageBounced") { - err("`onBouncedMessage` should have one parameter `InMessageBounced`").fire(f->ident_anchor, f); - } - } - - static void validate_onExternalMessage(FunctionPtr f) { - bool no_param_or_slice = f->get_num_params() == 0 || - (f->get_num_params() == 1 && f->get_param(0).declared_type == TypeDataSlice::create()); - if (!no_param_or_slice) { - err("`onExternalMessage` should have one parameter `slice`").fire(f->ident_anchor, f); - } - } - - AnyExprV replace(V v) override { - // don't allow `var v = in` or passing `in` to another function (only `in.someField` is allowed) - if (v->sym == param_ref) { - err("using `{}` as an object is prohibited, because `InMessage` is a built-in struct, its fields are mapped to TVM instructions\n""hint: use `{}.senderAddress` and other fields directly", orig_in_name, orig_in_name).fire(v, cur_f); - } - return parent::replace(v); - } - - AnyExprV replace(V v) override { - if (v->get_obj()->kind == ast_reference && v->get_obj()->as()->sym == param_ref && v->is_target_struct_field()) { - // replace `in.body` with a reference to a parameter, `slice` on a stack - if (v->get_field_name() == "body" || v->get_field_name() == "bouncedBody") { - auto v_body_name = createV(v->range, "in.body"); - auto v_body_param = createV(v->range, v_body_name, nullptr); - v_body_param->mutate()->assign_sym(&cur_f->parameters[0]); - v_body_param->mutate()->assign_inferred_type(TypeDataSlice::create()); - return v_body_param; - } - - // don't allow to modify `in.valueCoins` and other - if (v->is_lvalue) { - err("modifying an immutable variable\n""hint: fields of InMessage can be used for reading only").fire(v, cur_f); - } - - // replace `in.senderAddress` / `in.valueCoins` with an aux vertex - ASTAuxData* aux_getField = new AuxData_OnInternalMessage_getField(cur_f, v->get_field_name()); - return createV(v, aux_getField, v->inferred_type); - } - - return parent::replace(v); - } - - AnyExprV replace(V v) override { - for (LocalVarPtr captured_var_ref : v->captured_vars) { - if (captured_var_ref == param_ref) { - err("capturing `InMessage` in a lambda is prohibited").fire(v, cur_f); - } - } - return parent::replace(v); - } - -public: - bool should_visit_function(FunctionPtr fun_ref) override { - return is_onInternalMessage(fun_ref) || is_onBouncedMessage(fun_ref) || is_onExternalMessage(fun_ref); - } - - void on_enter_function(V v_function) override { - if (cur_f->name == "onExternalMessage") { - validate_onExternalMessage(cur_f); - return; - } - if (cur_f->name == "onBouncedMessage") { - validate_onBouncedMessage(cur_f); - } - param_ref = &cur_f->parameters[0]; // it's `in`, we'll replace references to it - orig_in_name = param_ref->name; - - cur_f->mutate()->parameters = { - LocalVarData("in.body", cur_f->ident_anchor, TypeDataSlice::create(), nullptr, 0, 0), - }; - } -}; - -void pipeline_transform_onInternalMessage() { - TransformOnInternalMessageReplacer replacer; - replace_ast_of_all_functions(replacer); -} - -} // namespace tolk diff --git a/tolk/pipeline.h b/tolk/pipeline.h index b341b92194..9416522c91 100644 --- a/tolk/pipeline.h +++ b/tolk/pipeline.h @@ -39,16 +39,16 @@ void pipeline_calculate_rvalue_lvalue(); void pipeline_infer_types_and_calls_and_fields(); void pipeline_check_inferred_types(); void pipeline_refine_lvalue_for_mutate_arguments(); +void pipeline_check_loop_break_continue(); void pipeline_check_rvalue_lvalue(); void pipeline_check_private_fields_usage(); -void pipeline_check_pure_impure_operations(); void pipeline_check_constant_expressions(); void pipeline_mini_borrow_checker_for_mutate(); void pipeline_optimize_boolean_expressions(); void pipeline_detect_inline_in_place(); void pipeline_check_serialized_fields(); void pipeline_lazy_load_insertions(); -void pipeline_transform_onInternalMessage(); +void pipeline_check_onInternalMessage(); void pipeline_convert_ast_to_legacy_Expr_Op(); void pipeline_find_unused_symbols(); diff --git a/tolk/pretty-print.cpp b/tolk/pretty-print.cpp index ec031c1248..bd6becd10e 100644 --- a/tolk/pretty-print.cpp +++ b/tolk/pretty-print.cpp @@ -144,6 +144,7 @@ std::string debug_print(const Op* op) { case Op::_UnTuple: case Op::_Return: case Op::_Import: + case Op::_BreakFromLoop: os << " "; for (size_t i = 0; i < op->left.size(); ++i) { if (i) @@ -167,8 +168,8 @@ std::string debug_print(const Op* op) { os << " |disabled"; if (op->flags & Op::_NoReturn) os << " |noret"; - if (op->flags & Op::_Impure) - os << " |impure"; + if (op->flags & Op::_KeepEvenIfUnused) + os << " |keep"; return os.str(); } diff --git a/tolk/send-message-api.cpp b/tolk/send-message-api.cpp index 2355239880..7f662b1192 100644 --- a/tolk/send-message-api.cpp +++ b/tolk/send-message-api.cpp @@ -20,7 +20,7 @@ namespace tolk { -std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* debug_desc); +std::vector pre_compile_is_type(CodeBlob& code, TypePtr expr_type, TypePtr cmp_type, const std::vector& expr_ir_idx, AnyV origin, const char* purpose); std::vector transition_to_target_type(std::vector&& rvect, CodeBlob& code, TypePtr original_type, TypePtr target_type, AnyV origin); static bool is_type_UnsafeBodyNoRef_T(TypePtr bodyT) { diff --git a/tolk/smart-casts-cfg.cpp b/tolk/smart-casts-cfg.cpp index 58b1518c62..478d4c18ed 100644 --- a/tolk/smart-casts-cfg.cpp +++ b/tolk/smart-casts-cfg.cpp @@ -86,8 +86,8 @@ * // but `t` still has type `(int?, ...)`, not `(int, ...)`; only `t.0` is refined * * In the future, not only smart casts, but other data-flow analysis can be implemented. - * 1) detect signs: `if (x > 0) { ... if (x < 0)` to warn always false - * 2) detect always true/false: `if (x) { return; } ... if (!x)` to warn always true + * 1) detect signs: `if (x > 0) { ... if (x < 0)` is always false + * 2) detect always true/false: `if (x) { return; } ... if (!x)` is always true * These potential improvements are SignState and BoolState. Now they are NOT IMPLEMENTED, though declared. * Their purpose is to show, that data flow is not only about smart casts, but eventually for other facts also. * (though it's not obvious whether they should be analyzed at AST level or at IR level, like constants now) @@ -102,16 +102,17 @@ std::string SinkExpression::to_string() const { while (cur_path != 0) { result += "."; bool formatted = false; + int index_at = static_cast(cur_path & 0xFF) - 1; try { if (const TypeDataStruct* t_struct = cur_type->try_as()) { - StructFieldPtr field_ref = t_struct->struct_ref->get_field((cur_path & 0xFF) - 1); + StructFieldPtr field_ref = t_struct->struct_ref->get_field(index_at); result += field_ref->name; cur_type = field_ref->declared_type; formatted = true; } } catch (...) {} if (!formatted) { - result += std::to_string((cur_path & 0xFF) - 1); + result += std::to_string(index_at); } cur_path >>= 8; } @@ -130,14 +131,14 @@ bool SinkExpression::is_child_of(SinkExpression rhs) const { } SinkExpression SinkExpression::get_child_s_expr(int field_idx) const { - uint64_t new_index_path = index_path; // if we have c.1 (index_path = 2) and construct c.1.N, calc (N<<8 + 2) + SinkExpression child = *this; // if we have c.1 (index_path = 2) and construct c.1.N, calc (N<<8 + 2) for (int empty_byte = 0; empty_byte < 8; ++empty_byte) { - if ((index_path & (static_cast(0xFF) << (empty_byte*8))) == 0) { - new_index_path += (static_cast(field_idx) + 1) << (empty_byte*8); + if ((child.index_path & (static_cast(0xFF) << (empty_byte*8))) == 0) { + child.index_path += (static_cast(field_idx) + 1) << (empty_byte*8); break; } } - return SinkExpression(var_ref, new_index_path); + return child; } static std::string to_string(SignState s) { @@ -166,32 +167,52 @@ static AnyExprV unwrap_not_null_operator(AnyExprV expr) { // example: `cond ? beginCell() : null`; lca(`builder`,`null`) = `builder?` // 3) when two data flows rejoin // example: `if (tensorVar != null) ... else ...` rejoin `(int,int)` and `null` into `(int,int)?` -// when lca can't be calculated (example: `(int,int)` and `(int,int,int)`), nullptr is returned -static TypePtr calculate_type_lca(TypePtr a, TypePtr b, bool* became_union = nullptr) { - if (a->equal_to(b)) { - return a; - } +enum class TypeLcaStatus { + NotAUnion, + BecameUnion, + FailedUnion, +}; + +struct TypeLcaResult { + TypePtr type; + TypeLcaStatus status = TypeLcaStatus::NotAUnion; +}; + +// when making a union, we allow `int | int | slice` (flatten it to `int | slice`), +// but disallow `int | IntAlias | slice`: also flatten, but `IntAlias` is an invalid duplicate +static TypeLcaResult calculate_union_lca(TypePtr a, TypePtr b) { + std::vector invalid_duplicates; + TypePtr result = TypeDataUnion::create(std::vector{a, b}, &invalid_duplicates); + if (!invalid_duplicates.empty()) { + return {result, TypeLcaStatus::FailedUnion}; + } + if (!a->equal_to(result) && !b->equal_to(result)) { + return {result, TypeLcaStatus::BecameUnion}; // result is TypeDataUnion + } + return {result, TypeLcaStatus::NotAUnion}; // result is not TypeDataUnion +} +static TypeLcaResult calculate_type_lca(TypePtr a, TypePtr b) { if (a == TypeDataNotInferred::create() || b == TypeDataNotInferred::create()) { - return TypeDataNotInferred::create(); + return {TypeDataNotInferred::create()}; } if (a == TypeDataUnknown::create() || b == TypeDataUnknown::create()) { - return TypeDataUnknown::create(); + return {TypeDataUnknown::create()}; } if (a == TypeDataNever::create()) { - return b; + return {b}; } if (b == TypeDataNever::create()) { - return a; + return {a}; } if (a == TypeDataNullLiteral::create()) { - return TypeDataUnion::create_nullable(b); + return {TypeDataUnion::create_nullable(b)}; } if (b == TypeDataNullLiteral::create()) { - return TypeDataUnion::create_nullable(a); + return {TypeDataUnion::create_nullable(a)}; } const auto* tensor1 = a->try_as(); @@ -201,22 +222,24 @@ static TypePtr calculate_type_lca(TypePtr a, TypePtr b, bool* became_union = nul types_lca.reserve(tensor1->size()); bool ith_became_union = false; for (int i = 0; i < tensor1->size(); ++i) { - TypePtr next = calculate_type_lca(tensor1->items[i], tensor2->items[i], &ith_became_union); - if (next == nullptr) { - return nullptr; - } - types_lca.push_back(next); + TypeLcaResult next = calculate_type_lca(tensor1->items[i], tensor2->items[i]); + ith_became_union |= next.status != TypeLcaStatus::NotAUnion; + types_lca.push_back(next.type); } if (!ith_became_union) { - return TypeDataTensor::create(std::move(types_lca)); + return {TypeDataTensor::create(std::move(types_lca))}; } + // for `(T,U)` and `(T,V)` return not `(T,U|V)` but `(T,U)|(T,V)`, this is correct for rejoin + return calculate_union_lca(a, b); } - TypePtr resulting_union = TypeDataUnion::create(std::vector{a, b}); - if (became_union != nullptr && !a->equal_to(resulting_union) && !b->equal_to(resulting_union)) { - *became_union = true; + const auto* alias1 = a->try_as(); + const auto* alias2 = b->try_as(); + if (alias1 && alias2 && alias1->alias_ref == alias2->alias_ref) { + return {alias1}; } - return resulting_union; + + return calculate_union_lca(a, b); } // merge (unify) of two sign states: what sign do we definitely have @@ -259,6 +282,39 @@ BoolState calculate_bool_lca(BoolState a, BoolState b) { return transformations[static_cast(a)][static_cast(b)]; } +// given an indexable type, calculate its child type at index +// example: `Point` at index_at=1 is `int` (field `y`) +static TypePtr get_child_type(TypePtr parent_type, int index_at) { + parent_type = parent_type->unwrap_alias(); + if (const auto* t_union = parent_type->try_as(); t_union && t_union->or_null) { + parent_type = t_union->or_null->unwrap_alias(); + } + + if (const auto* t_struct = parent_type->try_as()) { + return t_struct->struct_ref->get_field(index_at)->declared_type; + } + if (const auto* t_tensor = parent_type->try_as()) { + return t_tensor->items[index_at]; + } + if (const auto* t_shaped = parent_type->try_as()) { + return t_shaped->items[index_at]; + } + return nullptr; +} + +// given a SinkExpression, calculate its original (declared) type without smart casts +// example: s_expr = `somePoint.y`, return `int` +static TypePtr get_declared_type(SinkExpression s_expr) { + TypePtr cur_type = s_expr.var_ref->declared_type; + uint64_t remaining_path = s_expr.index_path; + while (remaining_path != 0 && cur_type != nullptr) { + int index_at = static_cast(remaining_path & 0xFF) - 1; + cur_type = get_child_type(cur_type, index_at); + remaining_path >>= 8; + } + return cur_type; +} + // example for a ternary operator: `var v = cond ? someSlice : someCell` (no hint) will give a compilation error, // but `var v: HINT = ` is okay, if hint is valid; // for instance, `var v: int = cond ? someInt32 : someInt64` is ok: no unification @@ -292,11 +348,9 @@ void TypeInferringUnifyStrategy::unify_with(TypePtr next) { return; } - bool became_union = false; - TypePtr combined = calculate_type_lca(unified_result, next, &became_union); - different_types_became_union |= became_union; - - unified_result = combined; + TypeLcaResult combined = calculate_type_lca(unified_result, next); + lca_needs_hint |= combined.status != TypeLcaStatus::NotAUnion; + unified_result = combined.type; } // invalidate knowledge about sub-fields of a variable or its field @@ -313,20 +367,30 @@ void FlowContext::invalidate_all_subfields(LocalVarPtr var_ref, uint64_t parent_ } } -// get the resulting type of variable or struct field -TypePtr FlowContext::smart_cast_or_original(SinkExpression s_expr, TypePtr originally_declared_type) const { - auto it = known_facts.find(s_expr); - if (it == known_facts.end()) { - return originally_declared_type; - } +// get the current type of SinkExpression by known_facts; for example, after +// > if (myDict == null) { return } +// `myDict` will be `cell`, not `dict` +TypePtr FlowContext::get_effective_type(SinkExpression s_expr) const { + uint64_t remaining_path = s_expr.index_path; + s_expr = SinkExpression(s_expr.var_ref); // will iterate `obj`, `obj.field`, `obj.field.0` + + TypePtr cur_type = smart_cast_or(s_expr, s_expr.var_ref->declared_type); - TypePtr smart_casted = it->second.expr_type; - if (smart_casted->equal_to(originally_declared_type)) { - // given `var a: dict`, after merging control flow branches, restore `a: dict` instead of `a: cell?` - // (same for struct fields and other sink expressions) - return originally_declared_type; + while (remaining_path != 0) { + int index_at = static_cast(remaining_path & 0xFF) - 1; + s_expr = s_expr.get_child_s_expr(index_at); + if (TypePtr smart_casted = smart_cast_or(s_expr, nullptr)) { + cur_type = smart_casted; + } else { + cur_type = get_child_type(cur_type, index_at); + if (cur_type == nullptr) { + return nullptr; // example: `m: Msg1|Msg2`, so `m.field` is undefined + } + } + + remaining_path >>= 8; } - return smart_casted; + return cur_type; } // update current type of `local_var` / `tensorVar.0` / `obj.field` @@ -350,6 +414,44 @@ void FlowContext::register_known_type(SinkExpression s_expr, TypePtr assigned_ty known_facts[s_expr] = FactsAboutExpr(assigned_type, SignState::Unknown, BoolState::Unknown); } +// after control-flow branches rejoin, restore the exact source-level type from the entry flow: +// > fun demo(d: dict) { +// > if (d != null) {} +// > // here merge_flow joins to `d: cell?` +// > // and reanchor_to restores `d: dict`, because equal_to +void FlowContext::reanchor_to(const FlowContext& flow_before_branching) { + for (auto it = known_facts.begin(); it != known_facts.end();) { + const SinkExpression& s_expr = it->first; + FactsAboutExpr& facts = it->second; + + // step 1: reanchor to parent flow considering all intermediate smart casts (that's why it's not smart_cast_or) + TypePtr type_before = flow_before_branching.get_effective_type(s_expr); + if (type_before != nullptr && facts.expr_type->equal_to(type_before)) { + auto it_before = flow_before_branching.known_facts.find(s_expr); + if (it_before != flow_before_branching.known_facts.end()) { + facts.expr_type = it_before->second.expr_type; + ++it; + } else { + it = known_facts.erase(it); + } + continue; + } + + // step 2: if it became identical to original var/field type, restore it + TypePtr declared_type = get_declared_type(s_expr); + if (declared_type != nullptr && facts.expr_type->equal_to(declared_type)) { + if (s_expr.index_path == 0) { + facts.expr_type = declared_type; // local vars are always stored in flow + ++it; + } else { + it = known_facts.erase(it); + } + continue; + } + ++it; + } +} + // mark control flow unreachable / interrupted void FlowContext::mark_unreachable(UnreachableKind reason) { unreachable = true; @@ -404,7 +506,7 @@ FlowContext FlowContext::merge_flow(FlowContext&& c1, FlowContext&& c2) { if (auto it2 = c2.known_facts.find(s_expr); it2 != c2.known_facts.end()) { const FactsAboutExpr& i2 = it2->second; unified.emplace(s_expr, i1 == i2 ? i1 : FactsAboutExpr( - calculate_type_lca(i1.expr_type, i2.expr_type), + calculate_type_lca(i1.expr_type, i2.expr_type).type, calculate_sign_lca(i1.sign_state, i2.sign_state), calculate_bool_lca(i1.bool_state, i2.bool_state) )); @@ -492,11 +594,11 @@ SinkExpression extract_sink_expression_from_vertex(AnyExprV v) { } if (index_path && depth < 8) { // `(x = rhs).field` is the same sink as `x.field` if (SinkExpression inner = extract_sink_expression_from_vertex(cur_dot->get_obj())) { - int inner_n_bits = 0; - for (uint64_t tmp = inner.index_path; tmp; tmp >>= 8) { - inner_n_bits += 8; + while (index_path != 0) { + inner = inner.get_child_s_expr(static_cast(index_path & 0xFF) - 1); + index_path >>= 8; } - return SinkExpression(inner.var_ref, (index_path << inner_n_bits) | inner.index_path); + return inner; } } } diff --git a/tolk/smart-casts-cfg.h b/tolk/smart-casts-cfg.h index 449832798d..c695cc21ca 100644 --- a/tolk/smart-casts-cfg.h +++ b/tolk/smart-casts-cfg.h @@ -36,14 +36,14 @@ namespace tolk { class TypeInferringUnifyStrategy { TypePtr dest_hint = nullptr; TypePtr unified_result = nullptr; - bool different_types_became_union = false; + bool lca_needs_hint = false; public: explicit TypeInferringUnifyStrategy(TypePtr hint); void unify_with(TypePtr next); TypePtr get_result() const { return unified_result; } - bool became_union_without_hint() const { return different_types_became_union && dest_hint == nullptr; } + bool became_union_without_hint() const { return lca_needs_hint && dest_hint == nullptr; } }; /* @@ -55,8 +55,8 @@ class TypeInferringUnifyStrategy { * Note, that globals are NOT sink: don't encourage to use a global twice, it costs gas, better assign it to a local. */ struct SinkExpression { - LocalVarPtr const var_ref; // smart casts and data flow applies only to locals - const uint64_t index_path; // 0 for just `v`; for `v.N` it's (N+1), for `v.N.M` it's (N+1) + (M+1)<<8, etc. + LocalVarPtr var_ref; // smart casts and data flow applies only to locals + uint64_t index_path; // 0 for just `v`; for `v.N` it's (N+1), for `v.N.M` it's (N+1) + (M+1)<<8, etc. SinkExpression() : var_ref(nullptr), index_path(0) {} @@ -66,7 +66,7 @@ struct SinkExpression { : var_ref(var_ref), index_path(index_path) {} SinkExpression(const SinkExpression&) = default; - SinkExpression& operator=(const SinkExpression&) = delete; + SinkExpression& operator=(const SinkExpression&) = default; bool operator==(const SinkExpression& rhs) const { return var_ref == rhs.var_ref && index_path == rhs.index_path; } bool operator<(const SinkExpression& rhs) const { return var_ref == rhs.var_ref ? index_path < rhs.index_path : var_ref < rhs.var_ref; } @@ -86,6 +86,8 @@ enum class UnreachableKind { ThrowStatement, ReturnStatement, CallNeverReturnFunction, + BreakStatement, + ContinueStatement, }; // SignState is "definitely positive", etc. @@ -114,9 +116,9 @@ enum class BoolState { // example: after `x = 2;`, x is `int`, sign is Positive, bool is AlwaysTrue // example: inside `if (x != null && x > 0)`, x is `int`, sign is Positive (in else, no definite knowledge) // remember, that indices/fields are also expressions, `t.1 = 2` or `u.id = 2` also store such facts -// WARNING! Detecting data-flow facts about sign state and bool state is NOT IMPLEMENTED +// NOTE: detecting data-flow facts about sign state and bool state is NOT IMPLEMENTED // (e.g. `if (x > 0)` / `if (!t.1)` is NOT analysed, therefore not updated, always Unknown now) -// it's a potential improvement for the future, for example `if (x > 0) { ... if (x < 0)` to warn always false +// it's a potential improvement for the future, for example `if (x > 0) { ... if (x < 0)` is always false // their purpose for now is to show, that data flow is not only about smart casts, but eventually for other facts also struct FactsAboutExpr { TypePtr expr_type; // originally declared type or smart cast (Unknown if no info) @@ -144,6 +146,7 @@ class FlowContext { : known_facts(std::move(known_facts)), unreachable(unreachable) {} void invalidate_all_subfields(LocalVarPtr var_ref, uint64_t parent_path, uint64_t parent_mask); + TypePtr get_effective_type(SinkExpression s_expr) const; friend std::ostream& operator<<(std::ostream& os, const FlowContext& flow); @@ -162,10 +165,13 @@ class FlowContext { bool is_unreachable() const { return unreachable; } bool equivalent_to(const FlowContext& another) const; - bool smart_cast_exists(SinkExpression s_expr) const { return known_facts.find(s_expr) != known_facts.end(); } - TypePtr smart_cast_or_original(SinkExpression s_expr, TypePtr originally_declared_type) const; + TypePtr smart_cast_or(SinkExpression s_expr, TypePtr fallback) const { + auto it = known_facts.find(s_expr); + return it == known_facts.end() ? fallback : it->second.expr_type; + } void register_known_type(SinkExpression s_expr, TypePtr assigned_type); + void reanchor_to(const FlowContext& flow_before_branching); void mark_unreachable(UnreachableKind reason); static FlowContext merge_flow(FlowContext&& c1, FlowContext&& c2); diff --git a/tolk/source-maps.cpp b/tolk/source-maps.cpp index 764e928451..157974eba3 100644 --- a/tolk/source-maps.cpp +++ b/tolk/source-maps.cpp @@ -75,15 +75,6 @@ static SrcRange get_function_body_end(FunctionPtr fun_ref) { return SrcRange::span_at_end(fun_ref->ast_root->range, 1); } -static void to_json(JsonPrettyOutput& json, SrcRange range) { - SrcRange::DecodedRange r = range.decode_offsets(); - json << '[' - << r.file_id << ',' << ' ' - << r.start_line_no << ',' << r.start_char_no << ',' << ' ' - << r.end_line_no << ',' << r.end_char_no - << ']'; -} - static void to_json(JsonPrettyOutput& out, const std::vector& ir_idx_arr) { out << '['; bool first = true; @@ -236,8 +227,9 @@ void DebugMarksCollecting::to_pretty_json(std::ostream& os, const SymbolTypesCol } else if (const DebugMarkScopeStart* m_scope = std::get_if(&mark)) { json.key_value("kind", "scope_start"); json.key_value("range", m_scope->range); - } else if (std::get_if(&mark)) { + } else if (const DebugMarkScopeEnd* m_end = std::get_if(&mark)) { json.key_value("kind", "scope_end"); + json.key_value("range", m_end->range); } else if (const DebugMarkSmartCast* m_sc = std::get_if(&mark)) { json.key_value("kind", "smart_cast"); json.key_value("var_name", m_sc->local_ref->name); diff --git a/tolk/source-maps.h b/tolk/source-maps.h index 6f270f3cf7..9686c59025 100644 --- a/tolk/source-maps.h +++ b/tolk/source-maps.h @@ -64,6 +64,7 @@ struct DebugMarkScopeStart { }; struct DebugMarkScopeEnd { + SrcRange range; // same range as the matching DebugMarkScopeStart }; struct DebugMarkSmartCast { diff --git a/tolk/src-file.cpp b/tolk/src-file.cpp index 2e353a2764..ab75423a7f 100644 --- a/tolk/src-file.cpp +++ b/tolk/src-file.cpp @@ -18,6 +18,8 @@ #include "compilation-errors.h" #include "compiler-state.h" #include "compiler-settings.h" +#include +#include #include #include #include @@ -77,12 +79,25 @@ SrcFile* AllRegisteredSrcFiles::get_next_unparsed_file() { return const_cast(all_src_files[++last_parsed_file_id]); } -void SrcFile::assign_contract_directive(ContractDirective* contract_directive) { - this->contract_directive = contract_directive; +SrcFile::SrcFile(int file_id, bool is_stdlib_file, std::string realpath, std::string&& file_text) + : file_id(file_id) + , is_stdlib_file(is_stdlib_file) + , realpath(std::move(realpath)) + , text(std::move(file_text)) + , text_len(static_cast(text.size())) + , contract_directive(nullptr) { + // build a sorted index of line starts once; `convert_offset` uses upper_bound on it + line_offsets.reserve(text_len / 40 + 2); // rough estimate: ~40 chars per line + line_offsets.push_back(0); + for (int i = 0; i < text_len; ++i) { + if (text[i] == '\n') { + line_offsets.push_back(i + 1); + } + } } -bool SrcFile::is_offset_valid(int offset) const { - return offset >= 0 && offset <= static_cast(text.size()); +void SrcFile::assign_contract_directive(ContractDirective* contract_directive) { + this->contract_directive = contract_directive; } SrcFile::SrcPosition SrcFile::convert_offset(int offset) const { @@ -90,30 +105,18 @@ SrcFile::SrcPosition SrcFile::convert_offset(int offset) const { return SrcPosition{-1, -1, offset, "invalid offset"}; } - // currently, converting offset to line number is O(N): just read file contents char by char and detect lines - // since original Tolk src lines are now printed into Fift output, this is invoked for every asm instruction - // but anyway, it consumes a small amount of time relative to other work of the compiler - // in the future, it can be optimized by making lines index aside just std::string_view text - int line_idx = 0; - int char_idx = 0; - int line_offset = 0; - for (int i = 0; i < offset; ++i) { - char c = text[i]; - if (c == '\n') { - line_idx++; - char_idx = 0; - line_offset = i + 1; - } else { - char_idx++; - } - } - - size_t line_len = text.size() - line_offset; - for (int i = line_offset; i < static_cast(text.size()); ++i) { - if (text[i] == '\n') { - line_len = i - line_offset; - break; - } + // line_offsets[i] = start offset of line i; find the last start <= offset + auto it = std::upper_bound(line_offsets.begin(), line_offsets.end(), offset); + --it; + int line_idx = static_cast(it - line_offsets.begin()); + int line_offset = *it; + int char_idx = offset - line_offset; + + int line_len; + if (line_idx + 1 < static_cast(line_offsets.size())) { + line_len = line_offsets[line_idx + 1] - line_offset - 1; // exclude trailing '\n' + } else { + line_len = text_len - line_offset; } std::string_view line_str(text.data() + line_offset, line_len); @@ -181,7 +184,42 @@ SrcRange::DecodedRange SrcRange::decode_offsets() const { }; } -void SrcRange::output_underlined(std::ostream& os) const { +static bool contains_only_spaces(std::string_view s) { + for (char c : s) { + if (!std::isspace(static_cast(c))) { + return false; + } + } + return true; +} + +// True when the underlined range is essentially the whole statement on its line, +// e.g. `return x;` or ` break; // comment` +// Then we'll also output a line above, for better context. +static bool occupies_almost_whole_line(const SrcFile::SrcPosition& start, const SrcFile::SrcPosition& end) { + if (start.line_no != end.line_no) { + return false; + } + + // before: only spaces + std::string_view before = start.line_str.substr(0, start.char_no - 1); + if (!contains_only_spaces(before)) { + return false; + } + + // after: spaces, semicolon, optional comment + std::string_view after = start.line_str.substr(end.char_no - 1); + size_t i = 0; + while (i < after.size() && (std::isspace(static_cast(after[i])) || after[i] == ';')) { + ++i; + } + if (i >= after.size()) { + return true; + } + return i + 1 < after.size() && after[i] == '/' && after[i + 1] == '/'; +} + +void SrcRange::output_underlined(std::ostream& os, const char* ansi_gutter, const char* ansi_underline, const char* ansi_reset) const { SrcFilePtr src_file = get_src_file(); if (!src_file || !src_file->is_offset_valid(end_offset) || !is_valid()) { return; @@ -189,30 +227,39 @@ void SrcRange::output_underlined(std::ostream& os) const { SrcFile::SrcPosition start = src_file->convert_offset(start_offset); SrcFile::SrcPosition end = src_file->convert_offset(end_offset); - os << std::right << std::setw(4) << start.line_no << " | " << start.line_str << "\n"; - os << " " << " | "; + os << ansi_gutter << " " << " | " << ansi_reset << std::endl; + + if (occupies_almost_whole_line(start, end) && start.line_no > 1) { + SrcFile::SrcPosition prev = src_file->convert_offset(src_file->line_offsets[start.line_no - 2]); + if (!contains_only_spaces(prev.line_str)) { + os << ansi_gutter << " " << " | " << ansi_reset << prev.line_str << "\n"; + } + } + + os << ansi_gutter << std::right << std::setw(4) << start.line_no << " | " << ansi_reset << start.line_str << "\n"; + os << ansi_gutter << " " << " | " << ansi_reset; for (int i = 1; i < start.char_no; ++i) { os << ' '; } - int end_char_no_first_line = start.line_no == end.line_no ? end.char_no : static_cast(start.line_str.size()); - for (int i = start.char_no; i < end_char_no_first_line; ++i) { + int end_char_no_first_line = start.line_no == end.line_no ? end.char_no - 1 : static_cast(start.line_str.size()); + os << ansi_underline; + for (int i = start.char_no; i <= end_char_no_first_line; ++i) { os << '^'; } - os << "\n"; + os << ansi_reset << "\n"; if (end.line_no > start.line_no + 1) { - os << " ..." << " ..."; - os << "\n"; + os << ansi_gutter << " ..." << " ..." << ansi_reset << "\n"; } if (end.line_no > start.line_no) { - os << std::right << std::setw(4) << end.line_no << " | " << end.line_str << "\n"; - os << " " << " | "; + os << ansi_gutter << std::right << std::setw(4) << end.line_no << " | " << ansi_reset << end.line_str << "\n"; + os << ansi_gutter << " " << " | " << ansi_reset << ansi_underline; bool was_non_space = false; for (int i = 1; i < end.char_no; ++i) { was_non_space |= !std::isspace(end.line_str[i - 1]); os << (was_non_space ? '^' : ' '); } - os << "\n"; + os << ansi_reset << "\n"; } } diff --git a/tolk/src-file.h b/tolk/src-file.h index 65fd89dfa7..acede2103c 100644 --- a/tolk/src-file.h +++ b/tolk/src-file.h @@ -41,21 +41,20 @@ struct SrcFile { bool is_stdlib_file; // is a part of Tolk distribution, imported via "@stdlib/..." std::string realpath; // what "realpath" returned to locate (either abs path or what tolk-js returns) std::string text; // file contents loaded into memory, every Token::str_val points inside it + int text_len; // length (size) of text + std::vector line_offsets; // offset of each line start in text; built once on load for convert_offset AnyV ast = nullptr; // when a file has been parsed, its ast_tolk_file is kept here std::vector imports; // to check strictness (can't use a symbol without importing its file) ContractDirective* contract_directive; - SrcFile(int file_id, bool is_stdlib_file, std::string realpath, std::string&& text) - : file_id(file_id) - , is_stdlib_file(is_stdlib_file) - , realpath(std::move(realpath)) - , text(std::move(text)) - , contract_directive(nullptr) { } + SrcFile(int file_id, bool is_stdlib_file, std::string realpath, std::string&& file_text); SrcFile(const SrcFile& other) = delete; SrcFile &operator=(const SrcFile&) = delete; - bool is_offset_valid(int offset) const; + bool is_offset_valid(int offset) const { + return offset >= 0 && offset <= text_len; + } SrcPosition convert_offset(int offset) const; SrcFile* mutate() const { return const_cast(this); } @@ -153,7 +152,7 @@ class SrcRange { SrcFilePtr get_src_file() const; std::string stringify_start_location(bool output_char_no) const; - void output_underlined(std::ostream& os) const; + void output_underlined(std::ostream& os, const char* ansi_gutter, const char* ansi_underline, const char* ansi_reset) const; struct DecodedRange { diff --git a/tolk/symtable.cpp b/tolk/symtable.cpp index 7831b78b8c..171b39af3b 100644 --- a/tolk/symtable.cpp +++ b/tolk/symtable.cpp @@ -32,7 +32,9 @@ void Symbol::check_import_exists_when_used_from(FunctionPtr cur_f, AnyV usage) c has_import |= import.imported_file == declared_in; } if (!has_import) { - err("Using a non-imported symbol `{}`\n""hint: forgot to import \"{}\"?", name, declared_in->extract_short_name()).collect(usage, cur_f); + err("Using a non-imported symbol `{}`\n""hint: forgot to import \"{}\"?", name, declared_in->extract_short_name()) + .with_secondary(this, "declared here") + .collect(usage, cur_f); } } @@ -70,11 +72,43 @@ LocalVarPtr FunctionData::find_param(std::string_view name) const { return nullptr; } +// detect `fun onInternalMessage(in: InMessage)` +// we allow also FunC-style `onInternalMessage(msgCell: cell, msgBody: slice)`, then return false +bool FunctionData::is_onInternalMessage() const { + if (get_num_params() == 1 && name == "onInternalMessage") { + const TypeDataStruct* t_param = parameters[0].declared_type->try_as(); + if (t_param && t_param->struct_ref->name == "InMessage") { + return true; + } + // after transformation, `in:InMessage` becomes `in.body:slice` + if (parameters[0].declared_type == TypeDataSlice::create() && parameters[0].name == "in.body") { + return true; + } + } + // otherwise, we will output onInternalMessage without any transformations, `in.senderAddress` etc. are disallowed + return false; +} + +// detect `fun onExternalMessage()` (may accept `slice` parameter, may not) +bool FunctionData::is_onExternalMessage() const { + return name == "onExternalMessage"; +} + +// detect `fun onBouncedMessage(in: InMessageBounced)` +bool FunctionData::is_onBouncedMessage() const { + return name == "onBouncedMessage"; + // don't check signature, because unlike onInternalMessage, onBouncedMessage must be declared the only way +} + bool FunctionData::does_need_codegen() const { // when a function is declared, but not referenced from code in any way, don't generate its body if (!is_really_used()) { return false; } + // it's declared as `get fun smth(): T;` without body, for ABI only + if (is_prototype_only()) { + return false; + } // functions with asm body don't need code generation // (even if used as non-call: `var a = beginCell;` inserts TVM continuation inline) if (is_asm_function() || is_builtin()) { @@ -132,6 +166,10 @@ void FunctionData::assign_is_type_inferring_done() { this->flags |= flagTypeInferringDone; } +void FunctionData::assign_requires_callxargs() { + this->flags |= flagRequiresCallxargs; +} + void FunctionData::assign_is_really_used() { this->flags |= flagReallyUsed; } @@ -144,6 +182,10 @@ void FunctionData::assign_arg_order(std::vector&& arg_order) { this->arg_order = std::move(arg_order); } +void FunctionData::assign_lazy_load_plan(const LazyLoadPlan* lazy_load_plan) { + this->lazy_load_plan = lazy_load_plan; +} + void GlobalVarData::assign_resolved_type(TypePtr declared_type) { this->declared_type = declared_type; } @@ -266,20 +308,15 @@ EnumMemberPtr EnumDefData::find_member(std::string_view member_name) const { } static Error err_redefinition_of_symbol(const Symbol* previous) { - if (previous->is_builtin()) { - return err("redefinition of built-in symbol"); - } - if (previous->ident_anchor->range.get_src_file()->is_stdlib_file) { - return err("redefinition of a symbol from stdlib"); - } - return err("redefinition of symbol, previous was at: {}", previous->ident_anchor->range.stringify_start_location(false)); + return err("redefinition of symbol `{}`", previous->name) + .with_secondary(previous, "previous definition is here"); } void GlobalSymbolTable::add_global_symbol(const Symbol* sym) { auto key = key_hash(sym->name); auto [it, inserted] = entries.emplace(key, sym); if (!inserted) { - err_redefinition_of_symbol(it->second).fire(sym->ident_anchor); + err_redefinition_of_symbol(it->second).fire(sym); } } diff --git a/tolk/symtable.h b/tolk/symtable.h index ed4fc329d1..734806df29 100644 --- a/tolk/symtable.h +++ b/tolk/symtable.h @@ -27,7 +27,7 @@ namespace tolk { struct Symbol { std::string name; - AnyV ident_anchor; // "identifier node", e.g. for `struct Demo { ... }` will be `Demo`; nullptr for builtin + AnyV ident_anchor; // "identifier node", e.g. for `struct Demo { ... }` will be `Demo`; nullptr for compiler-only builtins Symbol(std::string name, AnyV ident_anchor) : name(std::move(name)) @@ -44,7 +44,6 @@ struct Symbol { return dynamic_cast(this); } - bool is_builtin() const { return ident_anchor == nullptr; } void check_import_exists_when_used_from(FunctionPtr cur_f, AnyV usage) const; }; @@ -99,6 +98,8 @@ struct LocalVarData final : Symbol { struct FunctionBodyCode; struct FunctionBodyAsm; +struct FunctionBodyPrototype; +struct FunctionBodyBuiltinStub {}; struct FunctionBodyBuiltinAsmOp; struct FunctionBodyBuiltinGenerateOps; struct GenericsDeclaration; @@ -106,6 +107,8 @@ struct GenericsDeclaration; typedef std::variant< FunctionBodyCode*, FunctionBodyAsm*, + FunctionBodyPrototype*, + FunctionBodyBuiltinStub*, FunctionBodyBuiltinAsmOp*, FunctionBodyBuiltinGenerateOps* > FunctionBody; @@ -117,7 +120,7 @@ struct FunctionData final : Symbol { flagIsLambda = 2, // it's an anonymous function (instantiated from a function expression, a lambda) flagTypeInferringDone = 4, // type inferring step of function's body (all AST nodes assigning v->inferred_type) is done flagUsedAsNonCall = 8, // used not only as `f()`, but as a 1-st class function (assigned to var, pushed to tuple, etc.) - flagMarkedAsPure = 16, // declared as `pure`, can't call impure and access globals, unused invocations are optimized out + flagRemovableIfUnused = 16, // asm `@pure` or built-in intrinsic; unused compiler-generated calls may be optimized out flagImplicitReturn = 32, // control flow reaches end of function, so it needs implicit return at the end flagContractGetter = 64, // was declared via `get func(): T`, tvm_method_id is auto-assigned flagIsEntrypoint = 128, // it's `main` / `onExternalMessage` / etc. @@ -126,8 +129,8 @@ struct FunctionData final : Symbol { flagReturnsSelf = 1024, // return type is `self` (returns the mutated 1st argument), calls can be chainable flagReallyUsed = 2048, // calculated via dfs from used functions; declared but unused functions are not codegenerated flagCompileTimeVal = 4096, // calculated only at compile-time for constant arguments: `grams("0.05")`, `"str".crc32()`, and others - flagAllowAnyWidthT = 16384, // for built-in generic functions that is not restricted to be 1-slot type flagManualOnBounce = 32768, // for onInternalMessage, don't insert "if (isBounced) return" + flagRequiresCallxargs = 65536, // the lowered body contains try/catch and needs a private c2 }; int tvm_method_id = EMPTY_TVM_METHOD_ID; @@ -151,7 +154,8 @@ struct FunctionData final : Symbol { DocCommentLines doc_lines; FunctionPtr base_fun_ref = nullptr; // for `f`, here is `f`; for a lambda, a containing function FunctionBody body; - AnyV ast_root; // V for user-defined (not builtin) + AnyV ast_root; // V for functions declared in source; nullptr for compiler-only builtins + const LazyLoadPlan* lazy_load_plan = nullptr; // when has `lazy` vars; see pipe-lazy-load-insertions.cpp FunctionData(std::string name, AnyV ident_anchor, std::string method_name, AnyTypeV receiver_type_node, AnyTypeV return_type_node, std::vector parameters, int initial_flags, FunctionInlineMode inline_mode, const GenericsDeclaration* genericTs, const GenericsSubstitutions* substitutedTs, DocCommentLines doc_lines, FunctionBody body, AnyV ast_root) : Symbol(std::move(name), ident_anchor) @@ -199,6 +203,7 @@ struct FunctionData final : Symbol { bool is_code_function() const { return std::holds_alternative(body); } bool is_asm_function() const { return std::holds_alternative(body); } + bool is_prototype_only() const { return std::holds_alternative(body); } bool is_method() const { return !method_name.empty(); } bool is_static_method() const { return is_method() && !does_accept_self(); } @@ -208,11 +213,16 @@ struct FunctionData final : Symbol { bool is_generic_function() const { return genericTs != nullptr; } bool is_instantiation_of_generic_function() const { return substitutedTs != nullptr; } bool is_lambda() const { return flags & flagIsLambda; } + bool is_builtin() const { + return std::holds_alternative(body) || + std::holds_alternative(body) || + std::holds_alternative(body); + } bool is_inlined_in_place() const { return inline_mode == FunctionInlineMode::inlineInPlace; } bool is_type_inferring_done() const { return flags & flagTypeInferringDone; } bool is_used_as_noncall() const { return flags & flagUsedAsNonCall; } - bool is_marked_as_pure() const { return flags & flagMarkedAsPure; } + bool is_removable_if_unused() const { return flags & flagRemovableIfUnused; } bool is_implicit_return() const { return flags & flagImplicitReturn; } bool is_contract_getter() const { return flags & flagContractGetter; } bool has_tvm_method_id() const { return tvm_method_id != EMPTY_TVM_METHOD_ID; } @@ -224,8 +234,12 @@ struct FunctionData final : Symbol { bool is_really_used() const { return flags & flagReallyUsed; } bool is_compile_time_const_val() const { return flags & flagCompileTimeVal; } bool is_compile_time_special_gen() const { return std::holds_alternative(body); } - bool is_variadic_width_T_allowed() const { return flags & flagAllowAnyWidthT; } bool is_manual_on_bounce() const { return flags & flagManualOnBounce; } + bool requires_callxargs() const { return flags & flagRequiresCallxargs; } + + bool is_onInternalMessage() const; + bool is_onExternalMessage() const; + bool is_onBouncedMessage() const; bool does_need_codegen() const; @@ -237,9 +251,11 @@ struct FunctionData final : Symbol { void assign_is_used_as_noncall(); void assign_is_implicit_return(); void assign_is_type_inferring_done(); + void assign_requires_callxargs(); void assign_is_really_used(); void assign_inline_mode_in_place(); void assign_arg_order(std::vector&& arg_order); + void assign_lazy_load_plan(const LazyLoadPlan* lazy_load_plan); }; struct GlobalVarData final : Symbol { diff --git a/tolk/tail-routing.h b/tolk/tail-routing.h new file mode 100644 index 0000000000..d89192a095 --- /dev/null +++ b/tolk/tail-routing.h @@ -0,0 +1,30 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . +*/ +#pragma once + +#include "fwd-declarations.h" + +namespace tolk { + +// Where a FallthroughTail is routed inside an `if`/`match`. +// Shared by LoopContinuePlan and InlineReturnPlan; the plans themselves stay separate. +struct TailRoutingNode { + AnyV pivot = nullptr; // the `if` or `match` statement itself + AnyV goes_to = nullptr; // its branch: a block of `if`, or an arm of `match`; nullptr = implicit `else` of `match` +}; + +} // namespace tolk diff --git a/tolk/tolk-main.cpp b/tolk/tolk-main.cpp index f0d20f9578..3cb292180a 100644 --- a/tolk/tolk-main.cpp +++ b/tolk/tolk-main.cpp @@ -27,6 +27,7 @@ #include "compiler-state.h" #include "compiler-settings.h" #include "td/utils/port/path.h" +#include #include #include #include @@ -34,7 +35,8 @@ #include #elif TD_WINDOWS #include -#else // linux +#endif +#if !TD_WINDOWS #include #endif #include "git.h" @@ -51,13 +53,14 @@ enum LongOnlyOptions { OPT_NO_SYMBOL_TYPES, OPT_EMIT_DEBUG_MARKS, OPT_JSON_ERRORS, + OPT_COLOR, OPT_CHECK_ONLY, OPT_ALLOW_NO_ENTRYPOINT, + OPT_ALLOW_EMPTY_GET_FUN, }; static struct option long_options[] = { {"output", required_argument, nullptr, 'o'}, - {"opt-level", required_argument, nullptr, 'O'}, {"path-mapping", required_argument, nullptr, OPT_PATH_MAPPING}, {"no-stack-comments", no_argument, nullptr, OPT_NO_STACK_COMMENTS}, {"no-line-comments", no_argument, nullptr, OPT_NO_LINE_COMMENTS}, @@ -66,8 +69,10 @@ static struct option long_options[] = { {"no-symbol-types", no_argument, nullptr, OPT_NO_SYMBOL_TYPES}, {"emit-debug-marks", no_argument, nullptr, OPT_EMIT_DEBUG_MARKS}, {"json-errors", no_argument, nullptr, OPT_JSON_ERRORS}, + {"color", required_argument, nullptr, OPT_COLOR}, {"check-only", no_argument, nullptr, OPT_CHECK_ONLY}, {"allow-no-entrypoint", no_argument, nullptr, OPT_ALLOW_NO_ENTRYPOINT}, + {"allow-empty-get-fun", no_argument, nullptr, OPT_ALLOW_EMPTY_GET_FUN}, {"verbose", no_argument, nullptr, 'e'}, {"version", no_argument, nullptr, 'V'}, {"help", no_argument, nullptr, 'h'}, @@ -81,8 +86,6 @@ void usage(const char* progname) { "-o, --output \n" "\tWrite generated code into specified .fif file instead of stdout\n" "\tOther artifacts use the same basename: for 'out.fif', also emit 'out.abi.json', etc.\n" - "-O, --opt-level \n" - "\tSet optimization level (2 by default)\n" "--path-mapping \n" "\tRegister @name -> path mapping (e.g. @mylib=/path/to/lib)\n" "--no-stack-comments\n" @@ -95,10 +98,14 @@ void usage(const char* progname) { "\tOutput debug marks JSON artifact and debug marks to Fift code\n" "--json-errors\n" "\tShow compilation errors in JSON (not human-readable) format\n" + "--color \n" + "\tControl colors in human-readable compilation errors\n" "--check-only\n" "\tCheck sources for errors without generating code (for IDE in background)\n" "--allow-no-entrypoint\n" "\tDo not require main/onInternalMessage (e.g. to compile only get-methods)\n" + "--allow-empty-get-fun\n" + "\tAllow `get fun` without a body (ABI-only prototypes, no Fift code)\n" "-e, --verbose\n" "\tIncrease verbosity level (extra output into stderr)\n" "-v, --version\n" @@ -266,6 +273,24 @@ td::Result fs_read_callback(CompilerSettings::FsReadCallbackKind ki static_cast(callback_payload); } +static bool auto_detect_colorize() { +#if TD_WINDOWS + return false; +#else + // may be disabled by any non-empty env, e.g. NO_COLOR=yes|1|true + if (const char* no_color = getenv("NO_COLOR"); no_color && *no_color) { + return false; + } + + if (!::isatty(STDERR_FILENO)) { + return false; + } + + const char* term = getenv("TERM"); + return term && *term && std::strcmp(term, "dumb") != 0; +#endif +} + GNU_ATTRIBUTE_NOINLINE static void compilation_failed_output_errors(const std::vector& errors) { constexpr int JSON_ERROR_LIMIT = 50; @@ -296,7 +321,7 @@ static void compilation_failed_output_errors(const std::vector } static void compilation_failed_with_fatal(const std::string& message) { - // no location, no pretty header, no json output, just "fatal", something unexpected happened + // no location, no pretty header, no colorize, no json output, just "fatal", something unexpected happened std::cerr << "fatal: " << message << std::endl; } @@ -308,14 +333,12 @@ static void compilation_succeed_after_output_done() { int main(int argc, char* const argv[]) { int i; - while ((i = getopt_long(argc, argv, "o:O:evVh", long_options, nullptr)) != -1) { + const char* color_mode = "auto"; + while ((i = getopt_long(argc, argv, "o:evVh", long_options, nullptr)) != -1) { switch (i) { case 'o': G_settings.output_filename = optarg; break; - case 'O': - G_settings.optimization_level = std::max(0, atoi(optarg)); - break; case OPT_PATH_MAPPING: if (!G_settings.parse_path_mapping_cmd_arg(optarg)) { return 2; // the error was printed to std::cerr @@ -342,12 +365,22 @@ int main(int argc, char* const argv[]) { case OPT_JSON_ERRORS: G_settings.show_errors_as_json = true; break; + case OPT_COLOR: + if (strcmp(optarg, "auto") && strcmp(optarg, "always") && strcmp(optarg, "never")) { + std::cerr << "invalid --color value: expected auto|always|never" << std::endl; + return 2; + } + color_mode = optarg; + break; case OPT_CHECK_ONLY: G_settings.check_only_no_output = true; break; case OPT_ALLOW_NO_ENTRYPOINT: G_settings.allow_no_entrypoint = true; break; + case OPT_ALLOW_EMPTY_GET_FUN: + G_settings.allow_empty_get_fun = true; + break; case 'e': G_settings.verbosity++; break; @@ -397,6 +430,14 @@ int main(int argc, char* const argv[]) { return 2; } + if (!G_settings.show_errors_as_json) { // no colorize for json errors + if (strcmp(color_mode, "always") == 0) { + G_settings.colorize_errors = true; + } else if (strcmp(color_mode, "auto") == 0) { + G_settings.colorize_errors = auto_detect_colorize(); + } + } + TolkCompilationResult result = tolk_proceed(argv[optind]); if (!result.fatal_msg.empty()) { compilation_failed_with_fatal(result.fatal_msg); diff --git a/tolk/tolk-version.h b/tolk/tolk-version.h index 0e20d8d073..ca21245e03 100644 --- a/tolk/tolk-version.h +++ b/tolk/tolk-version.h @@ -18,6 +18,6 @@ namespace tolk { -constexpr const char* TOLK_VERSION = "1.4.2"; +constexpr const char* TOLK_VERSION = "1.5.0"; } // namespace tolk diff --git a/tolk/tolk-wasm.cpp b/tolk/tolk-wasm.cpp index 14753c0b36..cea7e12c81 100644 --- a/tolk/tolk-wasm.cpp +++ b/tolk/tolk-wasm.cpp @@ -69,27 +69,29 @@ static td::Result compile_internal(char *config_json) { TRY_RESULT(input_json, td::json_decode(td::MutableSlice(config_json))) td::JsonObject& config = input_json.get_object(); - TRY_RESULT(opt_level, config.get_optional_int_field("optimizationLevel", 2)); TRY_RESULT(stack_comments, config.get_optional_bool_field("withStackComments", false)); TRY_RESULT(src_line_comments, config.get_optional_bool_field("withSrcLineComments", false)); TRY_RESULT(emit_symbol_types, config.get_optional_bool_field("withSymbolTypes", true)); TRY_RESULT(emit_debug_marks, config.get_optional_bool_field("withDebugMarks", false)); TRY_RESULT(entrypoint_filename, config.get_required_string_field("entrypointFileName")); TRY_RESULT(show_errors_as_json, config.get_optional_bool_field("jsonErrors", false)); + TRY_RESULT(colorize_errors, config.get_optional_bool_field("colorizeErrors", false)); TRY_RESULT(check_only_no_output, config.get_optional_bool_field("checkOnly", false)); TRY_RESULT(allow_no_entrypoint, config.get_optional_bool_field("allowNoEntrypoint", false)); + TRY_RESULT(allow_empty_get_fun, config.get_optional_bool_field("allowEmptyGetFun", false)); // note that `pathMappings` are handled on a client-side (in tolk-js) only G_settings.verbosity = 0; - G_settings.optimization_level = std::max(0, opt_level); G_settings.stack_layout_comments = stack_comments; G_settings.tolk_src_as_line_comments = src_line_comments; G_settings.show_errors_as_json = show_errors_as_json; + G_settings.colorize_errors = colorize_errors && !show_errors_as_json; G_settings.check_only_no_output = check_only_no_output; G_settings.emit_contract_abi = true; G_settings.emit_symbol_types = emit_symbol_types; G_settings.emit_debug_marks = emit_symbol_types && emit_debug_marks; G_settings.allow_no_entrypoint = allow_no_entrypoint; + G_settings.allow_empty_get_fun = allow_empty_get_fun; std::ostringstream errs; std::streambuf* old_err = std::cerr.rdbuf(errs.rdbuf()); diff --git a/tolk/tolk.cpp b/tolk/tolk.cpp index 879a5f6d9f..939ccfc416 100644 --- a/tolk/tolk.cpp +++ b/tolk/tolk.cpp @@ -38,7 +38,7 @@ thread_local CompilerSettings G_settings; thread_local CompilerState G; // prototypes of functions initializing/resetting global state and pointers -void define_builtins(); +void attach_builtins_implementation(); void type_system_init(); void lexer_init(); void clear_computed_constants_cache(); @@ -56,7 +56,6 @@ TolkCompilationResult tolk_proceed(const std::string &entrypoint_filename) { // reset per-compilation mutable state to allow successive compilation within each thread G = CompilerState{}; clear_computed_constants_cache(); - define_builtins(); // add built-in functions into G.symtable G.symbol_types_pool.seed_primitive_types(); // enable error collecting for check stages (multiple errors can be reported): @@ -70,13 +69,14 @@ TolkCompilationResult tolk_proceed(const std::string &entrypoint_filename) { pipeline_register_global_symbols(); pipeline_resolve_identifiers_and_assign_symbols(); pipeline_resolve_types_and_aliases(); + attach_builtins_implementation(); pipeline_calculate_rvalue_lvalue(); pipeline_infer_types_and_calls_and_fields(); pipeline_check_inferred_types(); pipeline_refine_lvalue_for_mutate_arguments(); + pipeline_check_loop_break_continue(); pipeline_check_rvalue_lvalue(); pipeline_check_private_fields_usage(); - pipeline_check_pure_impure_operations(); pipeline_check_constant_expressions(); pipeline_mini_borrow_checker_for_mutate(); pipeline_optimize_boolean_expressions(); @@ -94,19 +94,11 @@ TolkCompilationResult tolk_proceed(const std::string &entrypoint_filename) { .marks_json = "", }; } - // output warnings to console, if any collected - if (!G_settings.show_errors_as_json) { - for (const ThrownParseError& err : error_collector.flush()) { - if (err.is_warning) { - err.output_to_console(std::cerr); - } - } - } G.error_collector = nullptr; // the following pipes can't operate if any previous errors exist pipeline_lazy_load_insertions(); - pipeline_transform_onInternalMessage(); + pipeline_check_onInternalMessage(); // for IDE in background: all checks passed, skip codegen if (G_settings.check_only_no_output) { diff --git a/tolk/tolk.h b/tolk/tolk.h index 2458ac0edf..cfaef05d0e 100644 --- a/tolk/tolk.h +++ b/tolk/tolk.h @@ -32,6 +32,8 @@ namespace tolk { +struct InlineReturnPlan; +struct LoopContinuePlan; /* * @@ -270,6 +272,7 @@ struct OpList { VarDescrList fwd_analyze(VarDescrList values) const; bool mark_noreturn(); bool prune_unreachable(); + bool has_reachable_direct_break() const; bool optimize_conditional_branches(); void mark_function_used_dfs() const; void show(std::ostream& os, const std::vector& vars, const std::string& indent, int mode = 0) const; @@ -294,13 +297,14 @@ struct Op { _Until, _Repeat, _Again, + _BreakFromLoop, _TryCatch, _SliceConst, _SnakeStringConst, _DebugMark, }; OpKind cl; - enum { _Disabled = 1, _NoReturn = 2, _Impure = 4, _ArgOrderAlreadyEqualsAsm = 8 }; + enum { _Disabled = 1, _NoReturn = 2, _KeepEvenIfUnused = 4, _ArgOrderAlreadyEqualsAsm = 8 }; int flags; FunctionPtr f_sym = nullptr; GlobalVarPtr g_sym = nullptr; @@ -330,8 +334,8 @@ struct Op { bool set_noreturn() { flags |= _NoReturn; return true; } bool set_noreturn(bool flag); - bool impure() const { return flags & _Impure; } - void set_impure_flag(); + bool keep_even_if_unused() const { return flags & _KeepEvenIfUnused; } + void set_keep_flag(bool flag); bool arg_order_already_equals_asm() const { return flags & _ArgOrderAlreadyEqualsAsm; } void set_arg_order_already_equals_asm_flag(); @@ -378,6 +382,9 @@ struct FunctionBodyCode { void set_code(CodeBlob* code); }; +struct FunctionBodyPrototype { +}; + /* * * GENERATE CODE @@ -856,7 +863,7 @@ struct Stack { AsmOpList& o; std::vector& unique_constants; const std::vector& named_vars; - enum { _DisableOut = 128, _InsideLet = 256, _InlineFunc = 512, _InlineAny = 1024, _NeedRetAlt = 2048 }; + enum { _DisableOut = 128, _InsideLet = 256, _InlineRef = 1024, _NeedRetAlt = 2048 }; int mode; Stack(AsmOpList& _o, std::vector& constants, const std::vector& named_vars, int _mode) : o(_o), unique_constants(constants), named_vars(named_vars), mode(_mode) { @@ -900,6 +907,8 @@ struct Stack { void assign_var(var_idx_t new_idx, var_idx_t old_idx); void do_copy_var(var_idx_t new_idx, var_idx_t old_idx); void enforce_state(const StackLayoutVars& req_stack); + void assume_state(const StackLayoutVars& req_stack); + void assume_state(Stack&& other); void rearrange_top(const StackLayoutVars& top, std::vector last); void rearrange_top(var_idx_t top_var_idx, bool last); void merge_const(const Stack& req_stack); @@ -955,19 +964,31 @@ struct LazyVarRefAtCodegen { : var_ref(var_ref), var_state(var_state) {} }; +// a function being inlined right now; nullptr while lowering a real function body +struct InliningFrameLowering { + FunctionPtr f_inlined; // which function is being inlined + AnyV call_origin; // the call site it was expanded at + const InlineReturnPlan* plan; // branching at if/else/match to carry FallthroughTail + std::vector rvect_out; // `return x` writes here +}; + +// a loop being lowered right now; nullptr outside any loop +struct LoopFrameLowering { + const LoopContinuePlan* plan; // branching at if/else/match to carry FallthroughTail + AnyV body; // loop body block; its range is the MARK_SCOPE_END target +}; + struct CodeBlob { int var_cnt, in_var_cnt; FunctionPtr fun_ref; std::vector vars; std::vector lazy_variables; std::vector ever_smart_casted; - std::vector* inline_rvect_out = nullptr; - AnyV inline_return_stmt_out = nullptr; - bool inlining_before_immediate_return = false; + const InliningFrameLowering* inlining = nullptr; + const LoopFrameLowering* current_loop = nullptr; OpList ops; OpList* cur_ops; std::stack cur_ops_stack; - bool require_callxargs = false; explicit CodeBlob(FunctionPtr fun_ref) : var_cnt(0), in_var_cnt(0), fun_ref(fun_ref), cur_ops(&ops) { } @@ -975,21 +996,22 @@ struct CodeBlob { cur_ops->push_back(std::make_unique(origin, Op::_Nop)); } void add_call(AnyV origin, std::vector ret, std::vector args, FunctionPtr called_f, - bool arg_order_already_equals_asm = false) { + bool force_keep = false, bool arg_order_already_equals_asm = false) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_Call, std::move(ret))); op.right = std::move(args); op.f_sym = called_f; - if (!called_f->is_marked_as_pure()) op.set_impure_flag(); + if (force_keep || !called_f->is_removable_if_unused()) op.set_keep_flag(true); if (arg_order_already_equals_asm) op.set_arg_order_already_equals_asm_flag(); } void add_indirect_invoke(AnyV origin, std::vector ret, std::vector args) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_CallInd, std::move(ret))); op.right = std::move(args); - op.set_impure_flag(); + op.set_keep_flag(true); } - void add_let(AnyV origin, std::vector dst, std::vector src) { + void add_let(AnyV origin, std::vector dst, std::vector src, bool force_keep = false) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_Let, std::move(dst))); op.right = std::move(src); + if (force_keep) op.set_keep_flag(true); } void add_int_const(AnyV origin, std::vector dst, td::RefInt256 value) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_IntConst, std::move(dst))); @@ -1015,21 +1037,18 @@ struct CodeBlob { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_SetGlob)); op.right = std::move(src); op.g_sym = g; - op.set_impure_flag(); op.debug_mark = DebugMarkSetGlob{g, op.right}; } void add_setcontargs(AnyV origin, std::vector dst, std::vector src) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_SetContArgs, std::move(dst))); op.right = std::move(src); } - void add_import_fun_params(AnyV origin, std::vector ir_params, FunctionPtr f_entered, DebugMarkInfo mark_enter_fun) { + void add_import_fun_params(AnyV origin, std::vector ir_params, DebugMarkInfo mark_enter_fun) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_Import, std::move(ir_params))); - op.f_sym = f_entered; op.debug_mark = std::move(mark_enter_fun); } void add_return(AnyV origin, std::vector ir_return, FunctionPtr f_return_from) { Op& op = cur_ops->push_back(std::make_unique(origin, Op::_Return, std::move(ir_return))); - op.f_sym = f_return_from; op.debug_mark = create_mark_leave_fun(f_return_from, origin, op.left); } void add_to_tuple(AnyV origin, std::vector dst, std::vector src) { @@ -1056,6 +1075,9 @@ struct CodeBlob { Op& add_repeat_loop(AnyV origin, std::vector count) { return cur_ops->push_back(std::make_unique(origin, Op::_Repeat, std::move(count))); } + void add_break_from_loop(AnyV origin) { + cur_ops->push_back(std::make_unique(origin, Op::_BreakFromLoop)); + } Op& add_try_catch(AnyV origin) { return cur_ops->push_back(std::make_unique(origin, Op::_TryCatch)); } @@ -1096,6 +1118,7 @@ struct CodeBlob { void prune_unreachable_code(); void fwd_analyze(); void mark_noreturn(); + void materialize_immediate_returns(); bool optimize_conditional_branches(); std::vector generate_asm_code(int mode) const; diff --git a/tolk/type-export-json.cpp b/tolk/type-export-json.cpp index 2e072d3dc1..531f7da581 100644 --- a/tolk/type-export-json.cpp +++ b/tolk/type-export-json.cpp @@ -233,7 +233,7 @@ void TypeDataCoins::as_abi_json(std::string& out, JsonTypeExporter& registry) co void TypeDataBitsN::as_abi_json(std::string& out, JsonTypeExporter& registry) const { out += R"({"kind":"bitsN","n":)"; - out += std::to_string(is_bits ? n_width : n_width * 8); + out += std::to_string(n_bits); out += '}'; } @@ -304,7 +304,7 @@ void TypeDataUnknown::as_abi_json(std::string& out, JsonTypeExporter& registry) } void TypeDataNotInferred::as_abi_json(std::string& out, JsonTypeExporter& registry) const { - tolk_assert(false); + out += R"({"kind":"unknown"})"; } void TypeDataNever::as_abi_json(std::string& out, JsonTypeExporter& registry) const { @@ -531,6 +531,15 @@ static void to_json(JsonPrettyOutput& json, const CustomPackUnpackF& f) { json.end_object(); } +void to_json(JsonPrettyOutput& json, SrcRange range) { + SrcRange::DecodedRange r = range.decode_offsets(); + json << '[' + << r.file_id << ',' << ' ' + << r.start_line_no << ',' << r.start_char_no << ',' << ' ' + << r.end_line_no << ',' << r.end_char_no + << ']'; +} + void to_json(JsonPrettyOutput& json, const JsonTypeExporter::ConstValJson& v) { const ConstValExpression& expr = v.value; json.start_object(); @@ -618,15 +627,6 @@ static std::string get_type_description(TypePtr ty) { return {}; } -static void to_json(JsonPrettyOutput& json, SrcRange range) { - SrcRange::DecodedRange r = range.decode_offsets(); - json << '[' - << r.file_id << ',' << ' ' - << r.start_line_no << ',' << r.start_char_no << ',' << ' ' - << r.end_line_no << ',' << r.end_char_no - << ']'; -} - // Writes all type-related top-level arrays in canonical order: unique_types, generic instantiations, and declarations. // Both out.abi.json and out.symbolTypes.json need exactly the same set of arrays (differ only in EmitOptions). void JsonTypeExporter::emit_unique_ty_and_declarations_json(JsonPrettyOutput& json, const EmitOptions& opts) const { diff --git a/tolk/type-export-json.h b/tolk/type-export-json.h index 79b1bab569..470236bb01 100644 --- a/tolk/type-export-json.h +++ b/tolk/type-export-json.h @@ -92,5 +92,6 @@ std::string get_abi_description(const DocCommentLines& doc_lines); // shared to_json overloads used by ABI/symbol-types emitters via ADL void to_json(JsonPrettyOutput& json, const JsonTypeExporter::ConstValJson& v); +void to_json(JsonPrettyOutput& json, SrcRange range); } // namespace tolk diff --git a/tolk/type-system.cpp b/tolk/type-system.cpp index 623c497451..f8de81a0a6 100644 --- a/tolk/type-system.cpp +++ b/tolk/type-system.cpp @@ -118,28 +118,6 @@ bool TypeData::is_cell_or_CellT() const { return this == TypeDataCell::create(); } -// having `type UserId = int` and `type OwnerId = int` (when their underlying types are equal), -// make `UserId` and `OwnerId` NOT equal and NOT assignable (although they'll have the same type_id); -// it allows overloading methods for these types independently, e.g. -// > type BalanceList = dict -// > type AssetList = dict -// > fun BalanceList.validate(self) -// > fun AssetList.validate(self) -static bool are_two_equal_type_aliases_different(const TypeDataAlias* t1, const TypeDataAlias* t2) { - if (t1->alias_ref == t2->alias_ref) { - return false; - } - if (t1->alias_ref->is_instantiation_of_generic_alias() && t2->alias_ref->is_instantiation_of_generic_alias()) { - return t1->alias_ref->base_alias_ref != t2->alias_ref->base_alias_ref - || !t1->alias_ref->substitutedTs->equal_to(t2->alias_ref->substitutedTs); - } - // handle `type MInt2 = MInt1`, as well as `type BalanceList = dict`, then they are equal - const TypeDataAlias* t_und1 = t1->underlying_type->try_as(); - const TypeDataAlias* t_und2 = t2->underlying_type->try_as(); - bool one_aliases_another = (t_und1 && t_und1->alias_ref == t2->alias_ref) - || (t_und2 && t1->alias_ref == t_und2->alias_ref); - return !one_aliases_another; -} // -------------------------------------------- // create() @@ -213,8 +191,8 @@ TypePtr TypeDataIntN::create(int n_bits, bool is_unsigned, bool is_variadic) { return new TypeDataIntN(n_bits, is_unsigned, is_variadic); } -TypePtr TypeDataBitsN::create(int n_width, bool is_bits) { - return new TypeDataBitsN(n_width, is_bits); +TypePtr TypeDataBitsN::create(int n_bits) { + return new TypeDataBitsN(n_bits); } TypePtr TypeDataUnion::create(std::vector&& variants, std::vector* out_invalid_duplicates) { @@ -500,8 +478,7 @@ std::string TypeDataIntN::as_human_readable() const { } std::string TypeDataBitsN::as_human_readable() const { - std::string s_bits = is_bits ? "bits" : "bytes"; - return s_bits + std::to_string(n_width); + return "bits" + std::to_string(n_bits); } std::string TypeDataUnion::as_human_readable() const { @@ -608,13 +585,6 @@ TypePtr TypeDataMapKV::replace_children_custom(const ReplacerCallbackT& callback // bool TypeDataAlias::can_rhs_be_assigned(TypePtr rhs) const { - if (const TypeDataAlias* rhs_alias = rhs->try_as()) { - // having `type UserId = int` and `type OwnerId = int`, make them NOT assignable without `as` - // (although they both have the same type_id) - if (underlying_type->equal_to(rhs_alias->underlying_type)) { - return !are_two_equal_type_aliases_different(this, rhs_alias); - } - } return underlying_type->can_rhs_be_assigned(rhs); } @@ -836,8 +806,8 @@ bool TypeDataIntN::can_rhs_be_assigned(TypePtr rhs) const { bool TypeDataBitsN::can_rhs_be_assigned(TypePtr rhs) const { if (const TypeDataBitsN* rhs_bitsN = rhs->try_as()) { // `slice` is NOT assignable to bitsN without `as` - // `bytes32` is NOT assignable to `bytes256` and even to `bits256` without `as` - return n_width == rhs_bitsN->n_width && is_bits == rhs_bitsN->is_bits; + // `bits32` is NOT assignable to `bits256` without `as` + return n_bits == rhs_bitsN->n_bits; } if (const TypeDataAlias* rhs_alias = rhs->try_as()) { return can_rhs_be_assigned(rhs_alias->underlying_type); @@ -961,7 +931,7 @@ bool TypeDataCell::can_be_casted_with_as_operator(TypePtr cast_to) const { } bool TypeDataSlice::can_be_casted_with_as_operator(TypePtr cast_to) const { - if (cast_to->try_as()) { // `slice` to `bytes32` / `slice` to `bits8` + if (cast_to->try_as()) { // `slice` to `bits32` return true; } if (cast_to->try_as()) { // `slice` to `address` @@ -1162,13 +1132,13 @@ bool TypeDataIntN::can_be_casted_with_as_operator(TypePtr cast_to) const { } bool TypeDataBitsN::can_be_casted_with_as_operator(TypePtr cast_to) const { - if (cast_to->try_as()) { // `bytes256` as `bytes512`, `bits1` as `bytes8` + if (cast_to->try_as()) { // `bits512` as `bits512` return true; } - if (cast_to->try_as()) { // `bytes267` as `address` + if (cast_to->try_as()) { // `bits267` as `address` return true; } - if (const TypeDataUnion* to_union = cast_to->try_as()) { // `bytes8` as `slice?` + if (const TypeDataUnion* to_union = cast_to->try_as()) { // `bits8` as `slice?` return to_union->calculate_exact_variant_to_fit_rhs(this); } if (const TypeDataAlias* to_alias = cast_to->try_as()) { @@ -1308,20 +1278,13 @@ bool TypeDataVoid::can_hold_tvm_null_instead() const { // -------------------------------------------- // equal_to() // -// comparing types for equality (when implementation differs from a default "compare pointers"); -// two types are EQUAL is a much more strict property than "assignable"; +// comparing types for runtime equality (when implementation differs from a default "compare pointers"); +// two types are EQUAL is a more strict property than "assignable", but aliases are erased; // a union type can hold only non-equal types; for instance, having `type MyInt = int`, a union `int | MyInt` == `int`; -// searching for a compatible method for a receiver is also based on equal_to() as first priority +// searching for a compatible method for a receiver is handled separately via SubtypeDistance::calc_between() // bool TypeDataAlias::equal_to(TypePtr rhs) const { - if (const TypeDataAlias* rhs_alias = rhs->try_as()) { - // given `type UserId = int` and `type OwnerId = int`, treat them as NOT equal (they are also not assignable); - // (but nevertheless, they will have the same type_id, and `UserId | OwnerId` is not a valid union) - if (underlying_type->equal_to(rhs_alias->underlying_type)) { - return !are_two_equal_type_aliases_different(this, rhs_alias); - } - } return underlying_type->equal_to(rhs); } @@ -1442,7 +1405,7 @@ bool TypeDataIntN::equal_to(TypePtr rhs) const { bool TypeDataBitsN::equal_to(TypePtr rhs) const { if (const TypeDataBitsN* rhs_bitsN = rhs->try_as()) { - return n_width == rhs_bitsN->n_width && is_bits == rhs_bitsN->is_bits; + return n_bits == rhs_bitsN->n_bits; } if (const TypeDataAlias* rhs_alias = rhs->try_as()) { return equal_to(rhs_alias->underlying_type); @@ -1506,7 +1469,7 @@ bool TypeDataUnion::has_all_variants_of(const TypeDataUnion* rhs_type) const { return true; } -int TypeDataUnion::get_variant_idx(TypePtr lookup_variant) const { +int TypeDataUnion::get_variant_equal_to(TypePtr lookup_variant) const { for (int i = 0; i < size(); ++i) { if (variants[i]->equal_to(lookup_variant)) { return i; @@ -1537,4 +1500,113 @@ TypePtr TypeDataUnion::calculate_exact_variant_to_fit_rhs(TypePtr rhs_type) cons return first_covering; } +// -------------------------------------------- +// SubtypeDistance +// + +// from `array` collect [T], from `Pair` collect [A,B], and so on; +// collected children from two types are compared for subtype distance, see below +static std::vector collect_subtype_children(TypePtr t) { + if (const TypeDataArray* t_array = t->try_as()) { + return {t_array->innerT}; + } + if (const TypeDataShapedTuple* t_shaped = t->try_as()) { + return t_shaped->items; + } + if (const TypeDataTensor* t_tensor = t->try_as()) { + return t_tensor->items; + } + if (const TypeDataMapKV* t_map = t->try_as()) { + return {t_map->TKey, t_map->TValue}; + } + if (const TypeDataGenericTypeWithTs* t_Ts = t->try_as()) { + return t_Ts->type_arguments; + } + if (const TypeDataFunCallable* t_callable = t->try_as()) { + std::vector children = t_callable->params_types; + children.push_back(t_callable->return_type); + return children; + } + if (const TypeDataStruct* t_struct = t->try_as(); t_struct && t_struct->struct_ref->is_instantiation_of_generic_struct()) { + const GenericsSubstitutions* substitutedTs = t_struct->struct_ref->substitutedTs; + std::vector children; + children.reserve(substitutedTs->size()); + for (int i = 0; i < substitutedTs->size(); ++i) { + children.push_back(substitutedTs->typeT_at(i)); + } + return children; + } + return {}; // int / bool / enum / genericT / ... — no children, so distance 0 once they're equal_to +} + +// A type alias makes a structural "subtype" for its underlying_type: +// > type IntAlias = int // distance 1 from `int` +// > type DeeperInt = IntAlias // distance 2 from `int`, distance 1 from `IntAlias` +// The relation is structural: +// - `Wrapper` is a subtype of `Wrapper` (distance 1) +// - but `Wrapper` is NOT a subtype of `Wrapper` (incomparable) +// What for: +// - `fun int.method` can be called both for provided `int` and `IntAlias` +// - `fun MyInt.method` can NOT be called with `int` (e.g., `42.method()` is "method not found") +SubtypeDistance SubtypeDistance::calc_between(TypePtr provided, TypePtr receiver) { + // `builder` and `slice` are incomparable, neither is a subtype of another + if (!provided->equal_to(receiver)) { + return incomparable(); + } + + // aliases form subtypes, check them first + if (const TypeDataAlias* provided_alias = provided->try_as()) { + if (const TypeDataAlias* receiver_alias = receiver->try_as()) { + // `IntAlias` and `IntAlias` + if (provided_alias->alias_ref == receiver_alias->alias_ref) { + return ok(0); + } + // `GenericAlias

` and `GenericAlias`, compare P and R + if (provided_alias->alias_ref->is_instantiation_of_generic_alias() && + receiver_alias->alias_ref->is_instantiation_of_generic_alias() && + provided_alias->alias_ref->base_alias_ref == receiver_alias->alias_ref->base_alias_ref) { + const GenericsSubstitutions* providedTs = provided_alias->alias_ref->substitutedTs; + const GenericsSubstitutions* receiverTs = receiver_alias->alias_ref->substitutedTs; + SubtypeDistance sum = ok(0); + for (int i = 0; i < providedTs->size(); ++i) { + sum = sum + calc_between(providedTs->typeT_at(i), receiverTs->typeT_at(i)); + } + return sum; + } + } + // `DeeperInt` and `int`, calc `IntAlias` and `int` + 1 + return calc_between(provided_alias->underlying_type, receiver) + ok(1); + } + + // `int` and `IntAlias`, distance is -1 formally, but for our purposes it's incomparable + if (receiver->try_as()) { + return incomparable(); + } + + // `slice | IntAlias` and `slice | int` (may be reordered, but still runtime equal), sum 0 + 1 + if (const TypeDataUnion* provided_union = provided->try_as()) { + const TypeDataUnion* receiver_union = receiver->try_as(); + SubtypeDistance sum = ok(0); + for (TypePtr receiver_variant : receiver_union->variants) { + int provided_idx = provided_union->get_variant_equal_to(receiver_variant); + tolk_assert(provided_idx >= 0); + sum = sum + calc_between(provided_union->variants[provided_idx], receiver_variant); + } + return sum; + } + + // okay, provided and receiver have equal shapes: + // - `array` and `array` => collect [MyInt] and [int] + // - `map` and `map` => collect [A,B] and [C,D] + // - `int8` and `int8` => collect [] and [] + std::vector provided_children = collect_subtype_children(provided); + std::vector receiver_children = collect_subtype_children(receiver); + tolk_assert(provided_children.size() == receiver_children.size()); + SubtypeDistance sum = ok(0); + for (int i = 0; i < static_cast(provided_children.size()); ++i) { + sum = sum + calc_between(provided_children[i], receiver_children[i]); + } + return sum; +} + } // namespace tolk diff --git a/tolk/type-system.h b/tolk/type-system.h index d4ac278599..bd8831147c 100644 --- a/tolk/type-system.h +++ b/tolk/type-system.h @@ -117,6 +117,9 @@ class TypeData { return true; } + // equal_to is RUNTIME equality (aliases erased); for instance, `int` equal_to `IntAlias` and vice versa; + // but some compiler features (overload resolution, pattern matching) treat aliases-as-subtypes: + // for example, `fun IntAlias.method` can't be called with `int`; see `SubtypeDistance` virtual bool equal_to(TypePtr rhs) const { return this == rhs->unwrap_alias(); } @@ -129,7 +132,7 @@ class TypeData { }; /* - * `type AliasName = underlying_type` is an alias, which is fully interchangeable with its original type. + * `type AliasName = underlying_type` is a subtype of its underlying type. * It never occurs at runtime: at IR generation it's erased, replaced by an underlying type. * But until IR generation, aliases exists, and `var t: MyTensor2 = (1,2)` is alias "MyTensor", not tensor (int,int). * That's why lots of code comparing types use `type->unwrap_alias()` or `try_as`. @@ -611,22 +614,20 @@ class TypeDataCoins final : public TypeData { }; /* - * `bits512`, `bytes8`, `bits9` are TypeDataBitsN. At TVM level, it's just slice. - * The purpose of bitsN is to be used in struct fields, describing the way of serialization (n bytes / n bits). + * `bits512`, `bits9` are TypeDataBitsN. At TVM level, it's just slice. + * The purpose of bitsN is to be used in struct fields, describing the way of serialization. * In this essence, bitsN is very similar to intN. * Note that unlike intN automatically cast to/from int, bitsN does NOT auto cast to slice (without `as`). */ class TypeDataBitsN final : public TypeData { - TypeDataBitsN(int n_width, bool is_bits) + explicit TypeDataBitsN(int n_bits) : TypeData(0) - , n_width(n_width) - , is_bits(is_bits) {} + , n_bits(n_bits) {} public: - const int n_width; // either in bits, or in bytes - const bool is_bits; + const int n_bits; - static TypePtr create(int n_width, bool is_bits); + static TypePtr create(int n_bits); int get_type_id() const override; std::string as_human_readable() const override; @@ -687,7 +688,7 @@ class TypeDataUnion final : public TypeData { TypePtr calculate_exact_variant_to_fit_rhs(TypePtr rhs_type) const; bool has_variant_equal_to(TypePtr rhs_type) const; bool has_all_variants_of(const TypeDataUnion* rhs_type) const; - int get_variant_idx(TypePtr lookup_variant) const; + int get_variant_equal_to(TypePtr lookup_variant) const; int get_width_on_stack() const override; int get_type_id() const override; @@ -821,5 +822,38 @@ class TypeDataVoid final : public TypeData { bool can_hold_tvm_null_instead() const override; }; +/* + * `SubtypeDistance` is a metric between a type alias and its underlying type: + * > type IntAlias = int // distance 1 from int + * > type DeeperInt = IntAlias // distance 2 from int, 1 from IntAlias + * Relation is structural: d(array,array) = 2. + * Identical types have distance 0 between: d(int,int) = 0, d(cell,cell) = 0. + * Incomparable types don't have a valid value: d(int,slice) = d(int8,int16) = incomparable. + */ +class SubtypeDistance { + int distance; + + explicit SubtypeDistance(int distance): distance(distance) {} + +public: + bool is_applicable() const { return distance >= 0; } + int get_distance() const { return distance; } + + SubtypeDistance operator+(SubtypeDistance rhs) const { + if (!is_applicable() || !rhs.is_applicable()) { + return incomparable(); + } + return ok(distance + rhs.distance); + } + + static SubtypeDistance ok(int distance) { + return SubtypeDistance(distance); + } + static SubtypeDistance incomparable() { + return SubtypeDistance{-1}; + } + + static SubtypeDistance calc_between(TypePtr provided, TypePtr receiver); +}; } // namespace tolk