mir9cc is tiny C Compiler by Rust.
This compiler can interpret the some basic syntax of C. (See the test directory for details.)
mir9cc has some stages, and here is an overview of the internals.
- Compiles an input program to AST.
- Runs a semantic analyzer on the trees to add a type to each tree node and to confirm The input program is semantically correct.
- 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.
Build.
$ make mir9cc
Run main test of ./test
.
$ make test
Run example program(nqueen).
$ ./target/debug/mir9cc examples/nqueen.c > tmp-nqueen.s
$ gcc -static -o tmp-nqueen tmp-nqueen.s
$ ./tmp-nqueen
Great thanks to 9cc.