rui314/9cc has 1,955 GitHub stars and 156 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-08 00:13 UTC. Last push 2020-11-28.
A Small C Compiler
Measured star growth
- in 24h
- 0
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Original9cc 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:
- Compiles an input string to abstract syntax trees.
- Runs a semantic analyzer on the trees to add a type to each tree node.
- Converts the trees to intermediate code (IR), which in some degree resembles x86-64 instructions but has an infinite number of registers.
- Maps an infinite number of registers to a finite number of registers.
- 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:
Like 8cc, no memory management is the memory management policy in 9cc. We allocate memory using malloc() but never call free(). I know that people find the policy odd, but this is actually a reasonable design choice for short-lived programs such as compilers. This policy greatly simplifies code and also eliminates use-after-free bugs entirely.
9cc's parser is a hand-written recursive descendent parser, so that the compiler doesn't have any blackbox such as lex/yacc.
I stick with plain old tools such as Make or shell script so that you don't need to learn about new stuff other than the compiler source code itself.
We use brute force if it makes code simpler. We don't try too hard to implement sophisticated data structures to make the compiler run faster. If the performance becomes a problem, we can fix it at that moment.
Entire contents are loaded into memory at once if it makes code simpler. We don't use character IO to read from an input file; instead, we read an entire file to a char array in a batch. Likewise, we tokenize a whole file in a batch rather than doing it concurrently with the parser.
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!
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
560,803 starspublic-apis/public-apis
A collective list of free APIs
497,033 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
441,184 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,148 stars