My Own C Compiler

This page written March 25, 2003

Not strictly my own, as it was a two-person project, done together with a CS student named Peter Mielke.

In 1990, in fourth year university, we Computer Engineering students were permitted to take fourth year Computer Science elective courses. Perennial favourites were the trains course (real-time control of an electric train layout) and the compiler course (link to current version). I took the latter.

The format was very simple: You could come to the lectures if you wanted, and they would teach you interesting stuff, but your final mark was mostly determined by the self-hosting C compiler that you would write. In those days, the favourite Computer Science type of computer was still the DEC VAX, which had the most elaborate instruction set imaginable (one instruction had 7 arguments), having been designed to be programmed by hand in assembly language, unlike modern machines. Very easy to generate code for. Some groups elected to generate code for the Motorola 68000 instead; this was because they had 68000 machines at home (Atari ST or Amiga).

Our compute environment was DEC MicroVAX II machines shared among a number of people each, typically running at load average 5 (i.e. at any time there were 5 tasks competing for the CPU). Doing a full recompile of a project of this size (on the order of 8000 lines of C) took so long that people went for a pizza and beer at the nearby student pub while waiting for it to finish!

We engineering students had a little-used lab full of brand-new Sun Sparc I workstations, which were about the most powerful micros then in existence. With such a machine all to oneself, it took all of one minute to rebuild an 8000 line project. We simply copied the result of our test compiles over to a VAX to actually run them.

I lived and breathed this compiler project for most of the semester. This was about the most excited I've ever been about writing a piece of software, and since I've gotten progressively less "nerdy" as I get older, it still stands out as my most memorable programming project ever. I remember there was an exceptionally warm couple of days that winter, the windows in the lab were open and the warm air was blowing in, and I was so deep in my coding thoughts that I forgot it was winter and thought I heard people outside playing frisbee on the lawn or something.

The compiler became self-hosting (i.e. able to compile a working version of itself) with a comfortable few days of margin before the deadline. Only a week earlier, it had just gained the ability to compile quoted text strings and had yet to get array indexing (implemented as three lines of code re-using the stuff from &, . and *). Things came together very fast once all the infrastructure (parser, type checker, code generator fundamentals) were in place.

My partner in crime Peter knew how to use the troff document formatting system, the same one used to typeset the Bell Labs Unix documents, which to me are about the most beautiful technical publications of all time. I gave him input in plain ASCII, he worked his magic and out came the nicest looking document I've ever had a hand in producing.

A comment on the report, when we got it back, was that our project showed a very good understanding of the VAX architecture! Actually, all we had to go on was a pocket reference guide to its instruction set, and a lot of study of actual VAX assembly language resulting from test compiles with the existing gcc compiler. Compared to the 6502, 68000, 8096, Z80 and 8031 assembly languages I was already familar with, the VAX was a piece of cake.

I just unearthed the whole kit & kaboodle from an old archive disk (nostalgia attack) and tried running the documentation through the groff system that comes with my Linux setup. It wasn't quite compatible; I had to change the main chapter headings to level 1 numbered headings, and Peter's illustrations (made with pic) don't look quite as good. Still looks pretty good.

Here is the documentation in PDF format, and rendered directly to HTML (not nearly as pretty, but all the information is there).

And of course, here is all the code, documentation source and test suite to download and try out if anyone likes to. I'm sure Peter will forgive me for this (part of the code is his). No specific licence, we retain the copyright of course by default, but if you want to use any of this code (I can't imagine why) just ask and GPL or whatever will be granted as appropriate!

Back to my Projects Page