2k-line mundane C compiler: a self-hosting toy C compiler that's so mundane, your puny C compiler can compile me
(Actually, currently it's closer to 1.5k lines)
C compiler self-host struct%
- compile a subset of C into x64 asm System-V ABI
- the source code of the compiler must be correctly compilable by the compiler itself
- the source code of the compiler should be compilable by many other toy C compilers
The language implemented by this compiler is intended as a subset of C99 - C17. Hence,
for(int i = 0; i < 5; i++) { }
is supportedint foo();
means that it does not make assumptions about the parameter types, whileint foo(void);
means that there should be no parameters
Run
make
to build 2kmcc
; then run
cat foo.c | xargs -0 -I XX ./2kmcc XX > foo.s
gcc -o foo foo.s -static
to make an executable out of foo.c
.
- self-hosted in 1500 lines
- See
test_*.py
to get an idea on what the compiler can and cannot handle - Try
make test
to see what it can handle; trymake embarrass
for what it can't - Try
make test_2ndgen
andmake test_3rdgen
to see that it self-hosts
The following was what I originally planned:
C compiler self-host struct%
- compile a subset of C into x64 asm System-V ABI
- the source code of the compiler must be correctly compilable by the compiler itself
- the source code of the compiler should be compilable by many other toy C compilers
- Must self-host
- Must be legible enough
- codegolf techniques not permitted if it severely hurts legibility
- it should be such that it is easy to add a new feature
- Should be short. I ambitiously aim for less than 2k lines
The subset of C that this compiler can compile must be such that it is not painfully unfun to write a realistic program in.
In particular, it must support:
- multidimensional arrays
struct
- string literals with
\n
@hiromi-mi's hanando-fukui v1.1.1 is astonishingly short while satisfying the conditions that I put up, with 2203 lines of main.c
and 169 lines of main.h
. Therefore, I know that this goal is attainable in 2.5k lines; that's why I aim for sub 2k.
- Rui Ueyama (@rui314) for writing the compilerbook
- @spinachpasta for actually typing my experimental design up to step12
- @hiromi-mi for inspiring me to work on this project