Calling TVM from Fift
Fift has the wordsrunvmcode, runvmdict, and runvm to invoke TVM code. All these words require that the code be provided in a slice. The code arguments can be prepared in the Fift stack, which is passed in its entirety to a fresh instance of the TVM stack. After executing the TVM code, the resulting TVM stack and the exit code are passed back to the Fift stack, so that it can be examined later using Fift words.
Word runvmcode consumes the slice s at the top of the Fift stack and invokes a new instance of TVM with the current continuation cc initialized with the code in s. Then, runvmcode initializes the TVM stack with the contents of the Fift stack. When TVM terminates, its resulting stack is used as the new Fift stack, with the exit code x pushed at its top. If x is non-zero, indicating that TVM has been terminated by an unhandled exception, the next stack entry from the top contains the parameter a of this exception, and x is the exception code. Additionally, when x is non-zero, all other entries below a in the Fift stack are removed.
Word runvmdict is very similar to runvmcode, but runvmdict also initializes the c3 TVM register with the code in slice s, and pushes a zero into the initial TVM stack before the TVM execution begins. This zero at the top of the TVM stack is called “the selector”, and tells which subroutine in slice s should be executed. In a typical application, slice s consists of several subroutines together with subroutine election code that uses the top-of-stack integer to select the subroutine to execute. The selector equal to zero corresponds to the main() subroutine in a large TVM program.
Word runvm is very similar to runvmdict, but runvm also initializes the persistent storage register c4. Word runvm expects the Fift stack to have the form s c, where s is the slice containing the code to execute and c the cell that will initialize the c4 register. After initializing the TVM stack, and TVM registers c3, c4, word runvm proceeds as runvmdict. When the TVM finishes execution, the Fift stack will have at its top most elements x c0, where c0 is the final cell of c4, and x is the exit code. If x is non-zero, indicating that TVM has been terminated by an unhandled exception, the next stack entry below x contains the parameter a of this exception. Additionally, when x is non-zero, all other entries below a in the Fift stack are removed.
For example, one can create an instance of TVM running simple code as follows:
2 3 9 x{1221}, where slice x{1221} is the topmost element. The slice x{1221} contains 16 data bits and no references. By consulting the TVM instructions table, it can be seen that x{12} is the code of the TVM instruction XCHG s1 s2, and that x{21} is the code of the TVM instruction OVER. Hence, x{1221} encodes the TVM instructions XCHG s1 s2 OVER. When word runvmcode executes, it transforms x{1221} into a TVM continuation, initializes the TVM stack to 2 3 9, where 9 is the top element. The Fift console then shows the following while runvmcode executes the TVM:
runvmcode finishes execution, it copies the contents of the TVM stack back into the Fift stack and pushes the TVM exit code. This means that, at the end, the Fift stack contains 3 2 9 2 0, where 0 is the top element, representing the exit code of the TVM, which in this case signals TVM successful execution.
If an unhandled exception is generated during the TVM execution, the code of this exception is returned as the exit code:
0 6, where 6 is the TVM exit code and 0 is the exception parameter. The numbers 3 2 9 are dropped from the Fift stack, because an exception occurred.
Simple TVM programs may be represented by Slice literals with the aid of the x{...} construct, as in the above examples. More sophisticated programs are usually created with the aid of the Fift assembler as explained in the next sections.
Fift assembler basics
The Fift assembler transforms human-readable mnemonics of TVM instructions into their binary representation. For instance, one could write<{ s1 s2 XCHG OVER }>s instead of x{1221}, as in the example of the previous section. This section explains the basics of notation like <{ s1 s2 XCHG OVER }>s.
Importing the assembler
The Fift assembler is located in fileAsm.fif in the Fift library directory. It is loaded by putting the phrase "Asm.fif" include at the very beginning of a program that needs to use the Fift assembler. File Asm.fif is resolved using the path provided in the -I command-line argument of the Fift interpreter.
The rest of the page assumes that all code snippets have included the
Asm.fif file using the the phrase "Asm.fif" include at the top.XCHG s1,s2 is represented in the Fift assembler as s1 s2 XCHG.
Assembler code blocks
Assembler code is opened by a special opening word, such as<{, which pushes an empty Builder at the top of the stack and loads the namespace for assembler words. After this, assembler words can be invoked to serialize the corresponding TVM instruction into the builder. For example, the word OVER will write the bits 00100001 into the builder, which correspond to the binary code for the OVER TVM instruction. Finally, the assembler code is terminated by a closing word, such as }>, }>s, or }>c.
Word }> leaves the builder at the top of the stack, but it also closes the assembler namespace, as long as it is the most external closing brace. This means that words representing TVM instructions, such as OVER, can only be used inside assembler code blocks like <{ }>. Word }>s does the same as }>, but it also transforms the builder at the top of the stack into a slice. Word }>c does the same as }>, but it also transforms the builder at the top of the stack into a cell.
TVM Stack registers
Fift uses special assembler words to represent stack registers while writing down assembler code. Wordss0 up to s15 represent the first 16 TVM stack registers, where register s0 represents the top of the TVM stack. Words s0 up to s15 push a cell into the Fift stack, encoding the corresponding TVM stack register.
Certain assembler words expect cells encoding TVM stack registers; for example, XCHG expects a stack of the form b c c', where b is the builder being constructed, and c, c' are two cells encoding TVM stack registers. For instance, s0 s1 XCHG first pushes to the Fift stack a cell encoding the TVM stack register s0, and a cell encoding the next-from-top s1. Then, word XCHG serializes into the builder the TVM binary code for exchanging TVM stack registers s0 and s1.
To refer to TVM stack registers below s15, Fift provides the more general word s(), which expects the TVM stack register index 0 <= i <= 255 to be at the top of the Fift stack. Word s() consumes integer i and pushes a cell to the Fift stack encoding the TVM stack register s_i. For instance, 40 s() 20 s() XCHG first pushes to the Fift stack a cell encoding the TVM stack register s40, and a cell encoding the TVM stack register s20. Then, word XCHG serializes into the builder the TVM binary code for exchanging TVM stack registers s40 and s20.
Basic examples with integers
The wordPUSHINT, or equivalently INT, consumes the integer x at the top of the Fift stack and serializes into the builder the TVM binary code for pushing integer x into the TVM stack. For instance, here is the trace for <{ 239 17 * INT }>s,
239 * 17 = 4063 happens in the Fift stack, not during TVM runtime. If the intention is to carry out the computation in the TVM, the assembler block should be written as <{ 239 INT 17 INT MUL }>s. Here is the trace:
TVM continuations
TVM continuations can be assembled using the wordPUSHCONT. This word consumes the builder at the top of the Fift stack. Such builder should contain the code for the continuation, which can be assembled using a nested <{ ... }> construct.
For example, the following assembler block builds code that will add numbers 3 and 5 when executed in the TVM. The code uses the nested <{ 5 INT ADD }> to assemble the continuation that “adds 5 to the number at the top of the stack”. The nested <{ 5 INT ADD }> then gets serialized as a continuation with the word PUSHCONT:
CONT:<{ ... }>, which is equivalent to <{ ... }> PUSHCONT. For instance, the above example can also be assembled as:
TVM conditionals and loops
TheIF word assembles the TVM instruction IF, which when executed in the TVM expects a TVM stack of the form i c, where i is an integer, and c is a continuation at the top of the stack. The instruction IF consumes both i and c. Then, it checks if i is zero or not. If i is non-zero, IF executes c; otherwise, IF does nothing. The IF instruction is roughly equivalent to the statement IF i THEN { c } in a high level programming language.
For instance,
5 0. The first number is what remained as result in the TVM stack. Integer 0 is the TVM exit code for a successful execution.
A more concise, but equivalent way, to write the previous example is with the use of IF:<{:
}>IF:
IFNOT is similar to IF, but it executes the continuation only if i is zero. The IFNOT instruction is roughly equivalent to the statement IF (NOT i) THEN { c } in a high level programming language. Word IFNOT also admits the constructs IFNOT:<{ and }>IFNOT.
Word IFELSE assembles the TVM instruction IFELSE, which when executed in the TVM expects a TVM stack of the form i c c', where i is an integer, and c, c' are continuations with c' at the top of the stack. The instruction IFELSE consumes i, c, and c'. Then, it checks if i is zero or not. If i is non-zero, IFELSE executes c; otherwise, IFELSE executes c'. The IFELSE instruction is roughly equivalent to the statement IF i THEN { c } ELSE { c' } in a high level programming language.
For instance,
5 0.
A more concise, but equivalent way, to write the previous example is with the use of IF:<{ ... }>ELSE<{ ... }>:
REPEAT executes a continuation a fixed number of times. Word REPEAT assembles the TVM instruction REPEAT, which when executed in the TVM expects a TVM stack of the form i c, where i is an integer and c is a continuation at the top of the stack. The instruction REPEAT consumes both i and c. Then, it executes c exactly i times. The REPEAT instruction is roughly equivalent to the statement REPEAT (i) { c } in a high level programming language.
For instance, the following increments the initial 0, two times:
runvmcode executes, it produces the following output, with added comments to clarify the steps:
REPEAT:<{:
}>REPEAT:
WHILE executes a continuation while a condition remains true. Word WHILE assembles the TVM instruction WHILE, which when executed in the TVM expects a TVM stack of the form c c', where c, c' are continuations with c' at the top of the stack. The instruction WHILE consumes c, and c'. Then, it executes c and pops an integer i from the stack. If i is non-zero, WHILE executes c' and repeats the process by executing c and popping an integer again. If i is zero, WHILE stops execution. The WHILE instruction is roughly equivalent to the statement WHILE { c } DO { c' } in a high level programming language.
For instance, the following increments the initial 0 while it is less than 2:
runvmcode executes, it produces the following output, with added comments to clarify the steps:
WHILE:<{ ... }>DO<{ ... }>:
UNTIL executes a continuation until a condition becomes true. Word UNTIL assembles the TVM instruction UNTIL, which when executed in the TVM expects a TVM stack of the form c, where c is a continuation at the top of the stack. The instruction UNTIL consumes c. Then, it executes c and pops an integer i from the stack. If i is zero, UNTIL repeats the process by executing c and popping an integer again. However, if i is non-zero, UNTIL stops execution.
For instance, the following increments the initial 0 until it is bigger than 2:
runvmcode executes, it produces the following output, with added comments to clarify the steps:
UNTIL:<{:
}>UNTIL:
Larger programs and subroutines
Larger TVM programs, such as TON Blockchain smart contracts, typically consist of several mutually recursive subroutines, with one or several of them selected as top-level subroutines (calledmain() or recv_internal()). The execution starts from one of the top-level subroutines, which is free to call any of the other defined subroutines, which in turn can call whatever other subroutines they need.
Such TVM programs are implemented by means of a selector function, which accepts an extra integer argument in the TVM stack; this integer selects the actual subroutine to be invoked. Before execution, the code of this selector function is loaded both into special register c3 and into the current continuation cc. The selector of the main function is pushed into the initial stack, and the TVM execution is started. Afterwards, a subroutine can be invoked by means of a suitable TVM instruction, such as CALLDICT n, where n is the integer selector of the subroutine to be called.
The Fift assembler offers several words facilitating the implementation of such large TVM programs. In particular, subroutines can be defined separately and assigned symbolic names, instead of numeric selectors, which can be used to call them afterwards. The Fift assembler automatically creates a selector function from these separate subroutines and returns it as the top-level assembly result.
Here is an example of a program consisting of several subroutines. This program computes the complex number (5 + i)^4 + (239 - i) in the main subroutine, where a complex number a + bi is represented in the stack as the two numbers a b:
- A TVM program is opened by
PROGRAM{and closed by either}END>c, which returns the assembled program as a cell, or}END>s, which returns a slice. - A new subroutine identifier is declared by means of the phrase
NEWPROC <NAME>. This declaration assigns the next positive integer as a selector for the subroutine, and stores this integer into the constant<NAME>. For instance, the declarationsNEWPROC addandNEWPROC mulin the example defineaddandmulas integer constants equal to1and2, respectively. This means thataddandmulcan be used anywhere as integer constants, i.e., they will push the integer selector for such subroutine. - Some subroutines identifiers are pre-declared and do not need to be declared again with
NEWPROC. For instance,mainis a subroutine identifier always bound to integer selector0. - Other predefined subroutine selectors such as
recv_internal(equal to0) orrecv_external(equal to-1), useful for implementing TON Blockchain smart contracts, can be declared by means of constants, instead ofNEWPROC. For example,-1 constant recv_external. - A subroutine can be defined either with the aid of the word
PROC, which takes the integer selector of the subroutine and the slice containing the code for this subroutine from the Fift stack, or with the aid ofPROC:<{ ... }>, convenient for defining larger subroutines. In the example,main,add, andmulare defined usingPROC:<{ ... }>. - The
CALLDICTTVM instruction is assembled with the wordCALL, which takes the integer selector of the subroutine from the top of the Fift stack. For example,add CALLwill push the integer selector foradd, which is1, and then assemble the instructionCALLDICT 1. - The current implementation of the Fift assembler collects all subroutines into a dictionary with 14-bit signed integer keys. Therefore, all subroutine selectors must be in the range
-2^(13) ... 2^(13) - 1. - If a subroutine with an unknown selector is called during runtime, an exception with code
11is thrown. - The Fift assembler checks that all subroutine identifiers declared by
NEWPROCare actually defined byPROCorPROC:<{before the end of the program. It also checks that a subroutine is not redefined.
runvmdict, which initializes the TVM registers c3 and cc with the program and also pushes integer 0 to the top of the TVM stack, which is the selector for the main subroutine. This means that the first subroutine that TVM will execute is main. Word runvmdict produces the following TVM trace in the interpreter output, where comments were added to help identify the subroutine calls and control flow:
715 479 0. The first two numbers represent the complex number 715 + 479i which is the result of (5+i)^4 + (239-i). Integer 0 is the TVM exit code for a successful execution.