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 done in the example of previous section.
The Fift assembler is located in file Asm.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 Fift assembler. File Asm.fif is resolved using the path provided in the -I command-line argument of the Fift interpreter.
The Fift assembler inherits from Fift its postfix operation notation, i.e., the arguments or parameters are written before the corresponding instructions. For instance, the TVM assembler instruction represented as XCHG s1,s2 is represented in the Fift assembler as s1 s2 XCHG.
Fift assembler code is usually opened by a special opening word, such as <{, and terminated by a closing word, such as }> or }>s. For instance,
XCHG s1,s2 and OVER, and returns the result as a Slice (because }>s is used). The resulting Slice is displayed by csr., yielding
x{12} is indeed the (codepage zero) code of the TVM instruction XCHG s1,s2, and that x{21} is the code of the TVM instruction OVER (not to be confused with Fift primitive over).
In the future, we will assume that the Fift assembler is already loaded and omit the phrase "Asm.fif" include from our examples.
The Fift assembler uses the Fift stack in a straightforward fashion, using the top several stack entries to hold a Builder with the code being assembled,
and the arguments to TVM instructions. For example:
| Word | Stack | Description |
|---|---|---|
<{ | ( – b) | begins a portion of Fift assembler code by pushing an empty Builder into the Fift stack (and potentially switching the namespace to the one containing all Fift assembler-specific words). Approximately equivalent to <b. |
}> | (b – b') | terminates a portion of Fift assembler code and returns the assembled portion as a Builder (and potentially recovers the original namespace). Approximately equivalent to nop in most situations. |
}>c | (b – c) | terminates a portion of Fift assembler code and returns the assembled portion as a Cell (and potentially recovers the original namespace). Approximately equivalent to b>. |
}>s | (b – s) | terminates a portion of Fift assembler code similarly to }>, but returns the assembled portion as a Slice. Equivalent to }>c <s. |
OVER | (b – b') | assembles the code of the TVM instruction OVER by appending it to the Builder at the top of the stack. Approximately equivalent to x{21} s,. |
s1 | ( – s) | pushes a special Slice used by the Fift assembler to represent the “stack register” s1 of TVM. |
s0... s15 | ( – s) | words similar to s1, but pushing the Slice representing other “stack registers” of TVM. Notice that s16... s255 must be accessed using the word s(). |
s() | (x – s) | takes an Integer argument 0 ≤ x ≤ 255 and returns a special Slice used by the Fift assembler to represent “stack register” s(x). |
XCHG | (b s s0 – b0) | takes two special Slices representing two “stack registers” s(i) and s(j) from the stack, and appends to Builder b the code for the TVM instruction XCHG s(i),s(j). |
OVER defined by the Fift assembler has a completely different effect from Fift primitive over.
The actual action of OVER and other Fift assembler words is somewhat more complicated than that of x{21} s,. If the new instruction code does not fit into the Builder b (i.e., if b would contain more than 1023 data bits after adding the new instruction code), then this and all subsequent instructions are assembled into a new Builder ˜b, and the old Builder b is augmented by a reference to the Cell obtained from ˜b once the generation of ˜b is finished. In this way long stretches of TVM code are automatically split into chains of valid Cells containing at most 1023 bits each. Because TVM interprets a lonely cell reference at the end of a continuation as an implicit JMPREF, this partitioning of TVM code into cells has almost no effect on the execution.
Pushing integer constants
The TVM instructionPUSHINT x, pushing an Integer constant x when invoked, can be assembled with the aid of Fift assembler words INT or PUSHINT:
| Word | Stack | Description |
|---|---|---|
PUSHINT | (b x – b0) | assembles TVM instruction PUSHINT x into a Builder. |
INT | (b x – b0) | equivalent to PUSHINT. |
PUSHINT is an Integer value taken from the Fift stack and is not necessarily a literal. For instance, <{ 239 17 * INT }>s is a valid way to assemble a PUSHINT 4063 instruction, because 239·17 = 4063. Notice that the multiplication is performed by Fift during assemble time, not during the TVM runtime. The latter computation might be performed by means of <{ 239 INT 17 INT MUL }>s:
PUSHINT x instruction depending on its argument x.
Some TVM instructions (such as PUSHINT) accept immediate arguments. These arguments are usually passed to the Fift word assembling the corresponding instruction in the Fift stack. Integer immediate arguments are usually represented by Integer’s, cells by Cell’s, continuations by Builder’s and Cell’s, and cell slices by Slice’s. For instance, 17 ADDCONST assembles TVM instruction ADDCONST 17, and x{ABCD_} PUSHSLICE assembles PUSHSLICE xABCD_:
ADDCONST x is defined only for −128 ≤ x < 128, but the Fift assembler accepts 239 ADDCONST:
"ADDCONST 239" has been tacitly replaced by PUSHINT 239 and ADD. This feature is convenient when the immediate argument to ADDCONST is itself a result of a Fift computation, and it is difficult to estimate whether it will always fit into the required range.
In some cases, there are several versions of the same TVM instructions, one accepting an immediate argument and another without any arguments. For instance, there are both LSHIFT n and LSHIFT instructions. In the Fift assembler, such variants are assigned distinct mnemonics. In particular, LSHIFT n is represented by n LSHIFT#, and LSHIFT is represented by itself.
TVM continuations
When an immediate argument is a continuation, it is convenient to create the corresponding Builder in the Fift stack by means of a nested<{ ... }> construct. For instance, TVM assembler instructions
"x{710192A70AE4} ok".
Incidentally, a better way of implementing the above loop is by means of REPEATEND:
"x{7101E7A70A}" and output "10000000" after seven iterations of the loop.
Notice that several TVM instructions that store a continuation in a separate cell reference (such as JMPREF) accept their argument in a Cell, not in a Builder. In such situations, the <{ ... }>c construct can be used to produce this immediate argument.
TVM Control flow: loops and conditionals
Almost all TVM control flow instructions—such asIF, IFNOT, IFRET, IFNOTRET, IFELSE, WHILE, WHILEEND, REPEAT, REPEATEND, UNTIL, and UNTILEND — can be assembled similarly to REPEAT and REPEATEND in the examples of 7.5 when applied to literal continuations. For instance, TVM assembler code
3n + 1 or n/2 depending on whether its argument n is odd or even, can be assembled and applied to n = 7 by
"x{2071B093A703A4DCAB00}".
Fift assembler words that can be used to produce such “high-level” conditionals and loops include IF:<{, IFNOT:<{, IFJMP:<{, }>ELSE<{, }>ELSE:,
}>IF, REPEAT:<{, UNTIL:<{, WHILE:<{, }>DO<{, }>DO:, AGAIN:<{, }>AGAIN, }>REPEAT, and }>UNTIL. Their complete list can be found in the source file Asm.fif. For instance, an UNTIL loop can be created by UNTIL:<{ ... }> or <{ ... }>UNTIL, and a WHILE loop by WHILE:<{ ... }>DO<{ ... }>.
If we choose to keep a conditional branch in a separate cell, we can use the <{ ... }>c construct along with instructions such as IFJMPREF: