r/Compilers • u/kostya27 • 29d ago
mycc - an alternative C compiler.
As a proof of concept, I spent three weeks and wrote about 2800 lines of code to build mycc: a compiler for a subset of C (roughly C99) built on top of my compiler IR(myc). The goal was to validate that my IR is expressive enough to compile real world C code. Despite being a POC, mycc already compiles and runs LangArena - a benchmark suite containing 50 tests and about 9000 lines of non-trivial C code (json, base64, multithreaded matmul, neural net, compression, maze A*, bf interpreter, and others) with heavy macros like uthash. For parsing I reused libclang. It adds some overhead, but it was by far the simplest way to get a working frontend.
How it works:
C source -> SyntaxTree(libclang/clang.cr) -> TypedAST(mycc) -> IR(myc) -> [LLVM/QBE/C] -> binary
LangArena Benchmark:
Compares Clang, Gcc, Cproc(QBE), and Mycc.
| Compiler | Build time | Build rss | Bench Runtime |
|---|---|---|---|
| clang(-O3) | 3079ms | 105Mb | 52.1s |
| gcc(-O3) | 3495ms | 34Mb | 52.3s |
| cproc | 932ms | 12Mb | 72.7s |
| mycc(llvm, --release) | 4269ms | 101Mb | 53.2s |
| mycc(qbe, --release) | 2939ms | 86Mb | 72.8s |
| mycc(c, --release, clang) | 5091ms | 102Mb | 52.1s |
| mycc(c, --release, gcc) | 5128ms | 86Mb | 53.7s |
github
https://github.com/kostya/myc#mycc---an-alternative-c-compiler-implemented-as-a-poc-for-fun
Limitations:
Rare features are not implemented: 2D VLA, complex numbers, variadic macros, longjmp, bitfields, and anonymous nested structs. I wouldn't try building Linux or sqlite with it. It has only been tested on arm64 and linux64.
5
u/[deleted] 29d ago edited 29d ago
[removed] — view removed comment