Question

What is Self-compilation (a.k.a. Meta-compilation) in Forth? How it works and why is it useful? Does it have any practical usage and is it still used in modern systems?

Was it helpful?

Solution

A metaprogram is one that takes another program as input. Every compiler is a metaprogram, in this sense.

A metacompiler is a compiler that takes a compiler is input, usually itself. It is closely related to the idea of a bootstrapping compiler. Another related concept is homoiconicity, which means that the program structure is similar in form to the data the program operates on.

The archetypal languages to consider are Lisp and Forth. In Lisp everything is a list; there is a core of operations including the ability to define new operations; and a large Lisp system can be bootstrapped from a small core.

Forth deals with stacks and words. It has a small set of core words that perform basic operations, including the ability to define new words; and a large Forth application is bootstrapped from that small core.

The key advantage in both cases is this ability to bootstrap a new system by writing a tiny (4K or less) core in assembly language on some processor, and then using that to compile the rest.

Forth is alive and well and still used in some embedded systems, but the main advantages have been overtaken by faster processors with more memory and the use of standard higher level languages. But if you really want to write code for a processor too small to see with the naked eye, keep Forth in mind!

Licensed under: CC-BY-SA with attribution
scroll top