Compiler Fun

This past spring I took my college’s compilers course which was an absolute blast. During the course, my partner and I wrote a compiler in OCaml that compiles a custom functional language straight down to x64 assembly. After the course finished, I spent a bit of time working on adding some more features to my compiler. My current feature set is:

  1. Basic integer arithmetic (+, -, *, /, %)
  2. Strings and various string operations (concatenation, char_at, etc)
  3. Mutable tuples including tuple unpacking
  4. Defining and calling functions
  5. Higher ordered functions
  6. Automatic garbage collection
  7. Defining, throwing, and catching exceptions
  8. Runtime type errors

I’m certainly not going to claim that it is a useful or novel language in any way, but it was a lot of fun to make. Working through implementing it made me far more comfortable with assembly. If you’re curious, I set up a simple godbolt.org-like playground that shows off some sample programs and the generated assembly. It also makes it possible to view which lines of assembly correspond to which expressions in my language. Check it out here:

http://daviddworken.com/compiler

Along the way, I also wrote a simple fuzzer for it that generates mostly valid programs and runs them through the compiler and an interpreter and compares their outputs. This caught a bunch of different bugs that otherwise I may have never found (ie some tricky memory alignment bugs).