firmforth is a just-in-time-compiling forth-like system using libfirm. It is small and comprehensible and was written to evaluate JIT-compilation using libfirm.
Firmforth is built around a dictionary of functions that can be invoked for an effect on a stack of values or a side effect. It also sports a typical forth outer interpreter. In interpreter mode, it executes the words that are input. In compilation mode, its action depends on whether an entered word is flagged immediate. Immediate words - e.g. “if” - are still executed right-away and implement the compiler itself. Normal words end up as Call nodes in the intermediate language.
When the definition of a new word is complete, libfirm-provided optimizations are invoked, possibly inlining Calls of functions for which the intermediate representation is available. An assembly file is then generated by libfirm and assembled into a shared object. This is in turn loaded into the program using dlopen() and the word is added to the dictionary. The IR is also kept around for future inlining of the word.
A couple of standard forth words are written in forth itself in the
core.fifo. To get an interactive session with these words
defined, you could invoke firmforth like this:
make && cat core.fifo - | ./firmforth
Some of the forth words are currently implemented in C. In order to be able to inline them into new words for a major speedup, the intermediate representation for them can be made available by invoking cparser like this:
cparser --export-ir firmforth.c
- [X] Interactively compile forth words using graphs of Call nodes
- [X] Add control flow primitives
- [X] Keep IR of newly defined words around and inline them
- [X] Keep IR of statically defined words around and inline them
- [ ] Use libfirm’s binary emitter instead of dlopen()
The table below lists times[s] required for compiling and executing
examples.fifo on my machine for
various forth systems.
|benchmark||firmforth 95ab9b7||gforth-fast 0.7.2|
|40 fibonacci||0.82 (+0.05 compilation)||3.2|
|16 queens||25.1 (+0.28 compilation)||43.9|
Andreas Seltenreich <email@example.com>