Library

To make best use of the code space of the AVR we do not compile a Forth kernel with all the standard Forth words, and then compile our application. Instead we use a Library. All the Forth definitions are compiled into the LIBRARY vocabulary and executed by the compiler as they are found in the input stream. The definitions in the Library are special but the words L: and L; allow you to create your own functions to extend these definitions.

Library words are required to perform two differing functions depending on their use. If a word is encountered when compiling a : definition in the Target, it is made into what is called a Forward reference, if it has not been previously compiled. When the compiler reaches the end of the current definition it tests the TARGET VOCABULARY for any such references. These are then looked up in the LIBRARY VOCABULARY to see if they exist. If so the Library definition is executed. This will generate the Target code necessary and resolve the references. If, however, the Library definition contains other Library words their execution must create a forward reference, for now, that will be resolved later. This entire process continues until no more unresolved references exist in the TARGET VOCABULARY that have equivalents in the Library . As Library words may contain other Library words it is necessary that no forward references exist in the Library. The compiler will give an exception to any word it finds that has not been previously defined.

You may add to the Library both assembler and Forth definitions. These are defined as follows:

High Level:

L: <name> word1 word2 word3 EXIT L;

Code:

L: <name> M[ ADIW TOS , # 2

  INC TOSL

  PUSHT

  RET ]M L;

In the High Level definition, word1,2 and 3 must exist in the Library prior to compiling this new definition. M[ and ]M must start and end any code fragments.

Note: High Level code must end with the EXIT and Code Routines end with RET or a JMP to code ending in RET.

Also: If you wish to use a Forth word from the Nucleus like, BRANCH, ?BRANCH, PAUSE, STOP, STATUS, BASE, PTR or PAD, these must be preceded by [TARGET] in the Library definition. This is because they do not have a Library version. Also any fixed value must be followed by LITERAL or DLITERAL to give the correct compilation.

 

Contents