next up previous contents index
Next: Repackaging Up: Other Threading Techniques Previous: Indirect Threading   Contents   Index


Trampolines

Another approach to implementing Dreams in an indirect threaded Forth interpreter makes use of the concept of trampolines. Trampolines are short fragments of machine code that are created as needed and inserted into the otherwise normal sequence of machine code that supports a high level language function.

In an indirect threaded Forth system, the instruction pointer (IP) points to the execution token (ET). This execution token, in turn, points to the code field address (cfa). Typically, the IP is used to fetch the ET, and then the IP is incremented. The ET is used to fetch the machine instruction address to execute the Forth word associated with the ET. This machine code is typically the same for all words of the same kind, such as colon definitions.

If the behavior of a word's name is to be changed -- if it is to be re-bound -- the re-binding mechanism must fetch the cfa of the word, given its execution token. If this cfa does not point to a trampoline (which is determined by comparing to a skeleton trampoline), then a trampoline is allocated on the dictionary and the old cfa is pushed on the active bindings stack. The new trampoline is filled in to cause the newly bound action to occur instead of the old behavior. If the word already had a trampoline associated with it, then the trampoline's old machine instruction address would be pushed (just like a cfa) on the active bindings stack, and the new address would be stored in its place.

This technique has the advantage of not requiring modification to the Forth inner address interpreter. It also allows rebinding to different types of words, such as binding a variable name to the behavior of a colon definition. Its disadvantage is that once a word has been rebound, it will never be quite as fast, since the overhead of bouncing off of the trampoline will always be there once it has been inserted.

This overhead also exists in the unreclaimable storage associated with each trampoline. FORGET must never be used after any trampolines have been allocated, since a trampoline could be forgotten while its associated word was still in the dictionary. If such a word were to be executed, it would jump off to a trampoline that was no longer there. Great would be its fall!


next up previous contents index
Next: Repackaging Up: Other Threading Techniques Previous: Indirect Threading   Contents   Index
Robert J. Brown
1999-09-26