Home

Awesome

9cc C compiler

Note: 9cc is no longer an active project, and the successor is chibicc.

9cc is a successor of my 8cc C compiler. In this new project, I'm trying to write code that can be understood extremely easily while creating a compiler that generates reasonably efficient assembly.

9cc has more stages than 8cc. Here is an overview of the internals:

  1. Compiles an input string to abstract syntax trees.
  2. Runs a semantic analyzer on the trees to add a type to each tree node.
  3. Converts the trees to intermediate code (IR), which in some degree resembles x86-64 instructions but has an infinite number of registers.
  4. Maps an infinite number of registers to a finite number of registers.
  5. Generates x86-64 instructions from the IR.

There are a few important design choices that I made to keep the code as simple as I can get:

Overall, 9cc is still in its very early stage. I hope to continue improving it to the point where 9cc can compile real-world C programs such as Linux kernel. That is an ambitious goal, but I believe it's achievable, so stay tuned!