ruby compiler series: annotated git history

May 13th, 2009

I've been reading along with Vidar Hokstad's rather excellent Writing a compiler in Ruby bottom up. It's a 20 part (so far) series documenting his effort to hack together a Ruby hosted compiler that in the end will compile a language similar to Ruby into x86 assembly.

Compilers are complicated beasts that take a lot of planning to build. Now I'm not saying Vidar didn't do all the planning, but what makes this series especially palatable is the fact that he's writing it literally bottom up, through what you might call evidence based hacking. That is, compile the very simplest thing you can (starting with an empty ELF binary), and then see what gcc produces. From there on, add print "Hello World" and see how the code changes and so forth, adding new constructs. This means you can read along even if you don't know any assembly (like yours truly) and take it in small steps without first having to absorb the complexity of a whole compiler.

It's a great learning opportunity, seeing as how each step is a working compiler one iteration up. You can read along Vidar's blog with the git diff side by side and see how the assembly is changing. To make this a bit clearer I've forked his repo and annotated the early commits with tags (where they were missing) and made sure the customary make run / make clean work as expected. I've also added some commit messages that tell you exactly what the iteration achieves at each particular step, so you can browse the history and figure out say "where do I look to see how to do a while construct".

I've annotated the first 15 steps (the rest were already tagged):

compiler-in-ruby

Given how git works, where every commit is hashed from the sum total of the previous ones, the only way I could do this is by rewriting the git history, which is not ideal. So Vidar's commit objects won't match mine, but all I've done is cherry pick them off his branch and add some annotations. It's all still there. :)

:: random entries in this category ::