handmade.network » Forums » A study of compilers
Dumitru Frunza
21 posts


#11054 A study of compilers
11 months, 4 weeks ago

Hey guys,

I'd like to share with you the results of my project to build a compiler from scratch. The main goal is to learn the internals of a compiler and to get a good understanding of the concepts involved, rather than to make a shippable product.

So I've been studying the famous "Dragon Book", 1st edition, for a while now, and I've gotten to a point where things start making sense :)
This is the first time since the beginning, when the compiler actually compiles something to "machine" code - and I'm excited!

Code on GitHub

The main focus at this point is around generating code for a stack machine. The source language (which I've named "hoc") is rudimentary : it supports only one type - the 32-bit integer; variables can be declared and initialized; procedures can be declared and called, but without arguments; arithmetic expressions can be formed and evaluated; no control flow yet.

The stack machine has been implemented too, along with it's instruction set. Normally, the "hoc" source gets translated to VM code that's then written to a file, and is interpreted by launching the VM program with the path to the file as argument.
A nice trick, is to bake the code into the VM executable itself, as a Win32 resource, and run that instead of the external file.

I'm a fan of Casey's approach to programming, so the compiler is written and architected in a style following that philosophy.

Take a look and tell me what you think!

Programming is hard, let's go shopping!
Ray Garner
44 posts
#11056 A study of compilers
11 months, 4 weeks ago

I think it would be cool if you try to implement something other than a stack style. If your gonna do it and take it that far maybe you should experiment and do something different.

Just to see what comes of it.
170 posts
2 projects

Father, husband, C programmer, and Linux apologist. Think before you code.

#11059 A study of compilers
11 months, 4 weeks ago

Must watch movie right here. Linked because of the book reference. Enjoy :-)

Build a man a fire, he'll be warm for a day.
Set a man on fire, he'll be warm for the rest of his life.