"Fast Code" Compilation

New with v3.00 is the facility to define words which are compiled partly or totally with inline machine code, rather than the more usual direct threaded code. This increases the size of code produced quite considerably, but is fully controllable and can increase the speed of critical code substantially.


Words available in fast compilation mode

Not all words can be compiled as inline machine code; particularly colon definitions. A large number of primitives can be, as well as all literals, constants, variables and values. Additionally all control structures except for DO...LOOP can be used (you can always replace this with another structure; FOR...STEP is usually the most suitable). Finally, it is also possible to use the phrases TO value and IS deferred_word, even though deferred words themselves cannot be fast-compiled.

It does not really matter that not all words can be compiled this way, as it is simple to direct the compiler to pause and resume fast compilation around a particular word or sequence of words; the only (fairly obvious) limitation is that all parts of a particular loop or control structure must be compiled the same way, even though code within them might not.

To determine the words available for fast code compilation, the following word is provided:

    FWORDS ( u1 u2 -- )

This lists all the fast primitives with code lengths between u2 and u1. Since the maximum code length for a fast primitive is 127 bytes, the following phrase will show all the fast primitives available:

    128 1 FWORDS

If you wish to compile only primitives up to a length of 10 bytes (say), the following phrase will list those available:

    11 1 FWORDS

Note that the words listed do not include constants, variables, values or control structures; these can all be compiled in fast mode as well.


Using fast compilation

Code that you wish to be compiled in "fast" mode is simply surrounded with curly brackets. For example, most of the following word will be compiled as inline machine code rather than threaded code:

    : TEST  { [CHAR] A  26 FOR  DUP } EMIT { 1+  STEP  DROP ;

This demonstrates a number of interesting points:

If you mistakenly try to fast compile an invalid word, exception -4095 will be generated.


Creating new fast primitives

If you are writing a new primitive, you can make it available for fast code compilation by using FCODE and FNEXT instead of the usual CODE and NEXT.

Some things to be aware of are:

Examples of fast primitives being defined are in the aregister.fth loadable file.


Notes on compilation words

If you are writing words which compile into colon definitions, you may need to take account of the current compilation state. If you use only the standard words LITERAL COMPILE, and POSTPONE there will be no problems. Otherwise you should check the new word FAST? which returns true if fast compilation mode is on. The best thing to do if this is the case is to THROW exception -4095.


More about CamelForth

Back to the Z88 home page