How to write a (tiny) Compiler in a weekend.


Gigahertz league processors, fast 128Mb memories and hard discs storing tens of Gigabytes have made a joke of what used to be heated optimisation and memory management issues. But there may still be some reason to take a last nostalgic look at what would have been an interesting project some time ago. For example, an old, inexpensive installation (which did not warrant a powerful processor) may need a single fast routine. A programmer may not be proficient in machine code in order to provide it.

Writing a 3 kilobyte compiler is bound to attract the wrath of fanatics in the structured programming community, and is unlikely to appeal to anybody (very reasonably) expecting every possible facility. Here is a link of less controversial recent sections. Take the link, too, if you have the widespread demand to be provided with several ways of getting at the same result. By now, you are probably well aware that this site is fervently minimalist!

Now that you have been warned, here are the main compiler shortcuts:
Compared to high- level languages (and processors!) offering 500+ commands, this looks terribly limited. However, the mini compiler is not intended to provide stand- alone programs, only a fast routine to an otherwise undemanding program, in terms of speed. It should be contrasted to having to write a section in machine code! Interfacing between the main program and fast routine is by the primitive method of directly accessing common memory.

Here are typical applications:
Here are the simple expressions possible:
The star operator directly accesses memory (is a pointer, in C- parlance.) Arithmetic operations also include subtraction, multiplication, integer division and remainder. Logical operations also feature And, Xor and Not. Relational operations also comprise less than, equal, and not equal. More complicated expressions have to be broken into simpler ones: (Think of this as a preamble to machine code programming!)

It would be a pity to severely restrict the language, in order to write a tiny, but giving fast executable code, compiler, only to waste time in IO calls: OS input- output routines have to cater for graphics, different devices, colours, character sets, screen resolutions, windows of different sizes at different locations, scrolling, compatibility and so on. Understandably, they are not particularly fast. If a limited range of options will only be requested, it is certainly possible to improve upon the speed.

Everything that is nice in organised programming has been eradicated- are the results any good? Well, a routine was compiled to capture and store samples on a slow board. The compiler executable could handle 50,000 samples per second, which is nowhere near the 165,000 samples achieved by handcrafted code, but a clear improvement upon the 400 samples provided by the high- level language interpreter: After all, an interpreter spends most of the time deciding what to do, not actually doing it.

Because of recent developments in technology, this section will not look in depth at why the differences between compiler code and custom code should be so. It is quite acceptable to trade efficiency for ease of programming, most of the time. However, here are the principal compiler drawbacks:
Strictly speaking, the 3 kb figure is only attained in a high- level language which tokenises keywords before storing them to disc/ memory, so a space is not needed after them. (Apart from loss of readability, the only ill- effect of this, is that a variable such as 'Forever' will clash with the keyword 'For'.)

1