rui314/9cc logo

rui314/9cc

Free

A Small C Compiler

FreeFree tier
Type
Open Source

About rui314/9cc

9cc is a small C compiler written by Rui Ueyama, the successor to 8cc. The project prioritizes code simplicity and understandability, using a hand-written recursive descent parser, plain tools like Make and shell, and a brute-force approach to keep the codebase straightforward. It compiles C source code through multiple stages: parsing to abstract syntax trees, semantic analysis with type checking, conversion to an intermediate representation (IR) with an infinite number of registers, register allocation to map to finite registers, and final generation of x86-64 assembly. The compiler uses a no-memory-management policy (malloc without free) to simplify code and eliminate use-after-free bugs. Note: 9cc is no longer actively maintained; its successor is chibicc.

Key Features

Compiles C source code to x86-64 assembly
Multiple stages: parsing, semantic analysis, IR generation, register allocation, code generation
Hand-written recursive descent parser (no lex/yacc)
Intermediate representation (IR) with infinite registers
Brute-force simplicity and minimal memory management (malloc without free)
Loads entire input file into memory at once
Uses plain tools: Make, shell script
Open source under MIT license

Pros & Cons

Pros
  • Code is designed to be extremely easy to understand
  • Generates reasonably efficient x86-64 assembly
  • No memory management complexity eliminates use-after-free bugs
  • Hand-written parser avoids blackbox tools like lex/yacc
  • Uses simple, widely-known build tools (Make, shell)
  • MIT license allows free use and modification
Cons
  • No longer actively maintained; successor (chibicc) recommended
  • Still in early stage, may not support full C standard
  • Only targets x86-64 architecture
  • No memory deallocation can lead to high memory usage for large inputs
  • No official releases or version tags

Best For

Learning compiler construction and compiler internalsEducational projects for understanding parsing, type checking, and code generationBuilding a simple C compiler for x86-64 platformsStudying minimal and readable compiler implementations

FAQ

What is 9cc?
9cc is a small C compiler focused on simplicity and understandability, written by Rui Ueyama. It is no longer active; its successor is chibicc.
What are the main stages of compilation in 9cc?
The compiler first parses C code into abstract syntax trees, runs a semantic analyzer to add types, converts the trees to an intermediate representation (IR) with infinite registers, maps those registers to finite registers, and finally generates x86-64 instructions.
What is the license for 9cc?
9cc is released under the MIT license.
Is 9cc suitable for compiling real-world C programs?
At the time of writing, 9cc was still in its early stage. The author aimed to eventually compile programs like the Linux kernel, but the project has since been succeeded by chibicc.
What design choices make 9cc code easy to understand?
Key choices include a hand-written recursive descent parser, no memory management (malloc without free), using plain tools like Make and shell, brute-force simplicity over performance, and loading entire input files at once.