Self Hosting C
I did it. It wasn’t easy, but I did it. My C Compiler can compile itself. Even though it still has holes in functionality and obvious bugs, It gives me a funny sense of pride that my compiler can now be used to improve itself. I consider it a significant milestone, and this post shares an overview of what was involved.
Lets look at the breakdown…
Timeline and Commits
~ 188 days of self directed work.
Approximately 380 commits, though probably more, because I discarded lots of work in local branches. I estimate a commit to be 20-30 minutes of work on average, so that translates to about 4 work weeks of hard work actually coding.
I worked whenever I could take a break, including evenings and lunch times. Sometimes I couldn’t sleep and would still be hacking away at 3 am. Once I woke up at 6 am, probably to fix a bug that was giving me nightmares.
Lines of code: 5902 lines of code currently, but will grow. My original goal was a complete toolchain in less than 15k lines of code, and I think I have still have room to spare for an optimizing backend and assemblers.
I think I have done a good job, though there is always room for improvement. I really wanted to make something anybody could understand. It is a matter of opinion, but look for yourself.
Compare my for loop parser:
To the equivalent clang for loop parser:
To the equivalent gcc parser:
To the equivalent Tiny C compiler parser:
I eventually reached a point where I felt like a failure, and that I couldn’t finish anything. Overcoming this can be a struggle, but in this case I challenged myself to not be a quitter. Whenever I hit a brick wall and wanted to give up, I told myself that this barrier would stop someone else, but it won’t stop me.
My motivation levels did drop at times, but I picked myself back up every time to reach this milestone.
My crazy ambition was to write the cleanest C compiler that could be used for an all C operating system like plan9 or Openbsd and there is still lots of work to do to reach that level of sophistication. Real OS support will require funding or many more dedicated code contributors.
For now though, this milestone is enough to feel like the project is a success.
This milestone really made me reflect and appreciate what some of the early programming language pioneers went through, the first self hosted languages really are something special.