Skip to content

A just-in-time-compiling forth system using libfirm.

License

Notifications You must be signed in to change notification settings

anse1/firmforth

Repository files navigation

firmforth

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.

Design

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.

firmforth.png

Usage

A couple of standard forth words are written in forth itself in the file 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

Goals

  • [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()

Benchmark

The table below lists times[s] required for compiling and executing the word fibonacci and queens in examples.fifo on my machine for various forth systems.

benchmarkfirmforth 95ab9b7gforth-fast 0.7.2
40 fibonacci0.82 (+0.05 compilation)3.2
16 queens25.1 (+0.28 compilation)43.9

References

Authors

Andreas Seltenreich <seltenreich@gmx.de>

About

A just-in-time-compiling forth system using libfirm.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published