r/Compilers • • Oct 17 '26

Mucc: a self hosting C compiler in one static binary, with its own assembler, linker and musl (x86-64 and ARM64 Linux)

Mucc is a small C compiler for Linux that ships as a single 8.5 MB static file. The preprocessor, assembler, linker, archiver, headers and C library (musl, like zig cc does) are all built in, so you can drop it into an empty container, an old distro or a Raspberry Pi and build C with nothing else installed: no gcc, no binutils, no system headers. 

Programs it builds are static, so they run on any x86-64 Linux. There's a native ARM64 build too, and it can cross-compile between the two.

The whole project actually started as a fork of Rui Ueyama's chibicc. I felt more could be done on this design. So now Mucc adds its own assembler and linker, an ARM64 backend, register allocation, the bundled musl, DWARF debug info, C23 support and a reproducible bootstrap chain.

What’s actually working so far 

- C17 by default, C23 with `-std=c23`, plus the GNU extensions real code uses

- Self-hosting: each release is built by the one before it, and a Mucc-built Mucc produces byte-identical output

- Real projects build and pass their own test suites: SQLite, Lua, zlib, jq, Redis and Tcl with the bundled musl; CPython and Git with the system glibc

- Same ABI as gcc, so objects from either link together

- Compiles its own 33k lines in 0.46 s (gcc -O0: 1.22 s, gcc -O2: 5.35 s)

What’s currently not working, it does no optimization beyond register allocation and constant folding, so programs run about as fast as gcc -O0. No C++, Linux only. For performance-critical code, use gcc or clang.

If you've got C code it chokes on, or a use for it I haven't thought of, I'd like to hear it.

GitHub: https://github.com/Dom-Yan/Mucc

Website: https://dom-yan.github.io/Mucc/

Thanks to Rui Ueyama for chibicc, and to Fabrice Bellard, whose TinyCC showed how much a small compiler can do.

33 Upvotes

10 comments sorted by

4

u/kostya27 Oct 17 '26

how this is better/worse than tcc?

7

u/dom_aley Oct 17 '26

Good question. So to be honest, tcc is better in terms of compile speed, size, and platform support (Windows, macOS, BSDs, RISC-V). Where Mucc is different and better imo, is that it's one static binary with its own assembler, linker, headers and musl built in, so you can drop it on an empty container or a Raspberry Pi with nothing installed and build C. tcc still needs the system’s libc and headers. It also does C17 by default and C23 with -std=c23. SQLite, Lua and Redis build with it and pass their test suites, and CPython and Git do too with --libc=system. So for the fastest tiny compiler tcc wins and is better. now if you want a whole C toolchain in one file with zero dependencies, Mucc is better.

2

u/No_Pianist1870 Oct 17 '26

Why can't musl support dlopen ?

2

u/dom_aley Oct 17 '26

musl does support dlopen, but only in dynamically linked programs, through its own dynamic linker. Mucc bundles musl as a static library and its linker only makes static executables, and in a static musl program dlopen is a stub that just returns “Dynamic loading not supported”. Supporting it would mean shipping musl’s dynamic linker and making Mucc’s linker produce dynamic executables. Then programs would need ld-musl on whatever machine they run on, which defeats the “runs on any Linux” point. If you need dlopen, --libc=system uses glibc and the system linker, and it works there. :)

2

u/Serious_Pin_1040 Oct 17 '26

I really like it. Three questions:

1, Is it single pass? 2, Performance being 50% of gcc with -O2, do you know what you could do to improve that? 3, Does it handle linker scripts?

1

u/dom_aley Oct 17 '26

I'll go ahead and answer this in three parts :

  1. No. It keeps chibicc’s design as it tokenizes the whole file, preprocesses, builds an AST for the whole file, generates assembly text, and then its built-in assembler turns that into machine code. tcc is the single-pass one.
  2. Most of the gap is codegen. The only optimizations are constant folding and keeping a function’s 5 most-used int/pointer locals in callee-saved registers. For something like s += a[i] * 3 + 1, it loads 3 and 1 into registers instead of using immediates, computes &a[i] with a multiply instead of an addressing mode, and sign extends ints it doesn’t need to. The cheap wins are immediates and addressing modes, a peephole pass, using more registers, and inlining small static functions. Past that you need a real IR, and that’s a much bigger project (im a solo dev). Now some of the gap is also the bundled musl with --libc=system the code is about 1.9x slower than gcc -O2 instead of 2.7x.
  3. Partly. The built-in linker reads the simple GROUP(...)/INPUT(...) scripts that some libc.so/libm.a files are. For real -T scripts with SECTIONS it hands the link to the system ld. Right now that only works with --libc=system; with the bundled musl it fails, which is a bug I’m fixing. I have a incoming patch I hope addresses most of the speed issues and performance.

1

u/Serious_Pin_1040 Oct 17 '26

Thank you for your reply. I will star and continue watching your progress.

1

u/[deleted] Oct 17 '26

That is quite an impressive spec, even with those limitations. How long did it take?

Binary Size I like self-contained products like this! But 8.5MB is quite hefty: is most of that those bundled libraries?

Compilation Speed (This is my special interest!) You mention a build-time for one project of 100Klps, which was only 50% faster than Clang-O0.

Is that the self-compiled version, or optimised via another compiler? Either way, it doesn't strike me as fast. On my machine (which benchmarks at 1700 compared to your 2700). a fast C compiler can manage 500-1000Klps.

So what is causing it to be slow? (For clang and gcc, those are just massive, complex products.)

1

u/dom_aley Oct 17 '26

Thanks! I started from Rui Ueyama’s chibicc, so not completely from scratch but I decided to finally make it a git project about 1 month ago and started the whole thing about 5 months ago.

Now for Size: yes, almost all of it is bundled files. The compiler itself is about 0.75 MB. Linux kernel headers are about 3.5 MB (1,000+ files), musl’s libc.a is about 3.1 MB, and musl’s headers are about 0.5 MB. They’re stored uncompressed. xz gets them down to about 1.1 MB, so compressing them would bring the binary to around 2 MB, Which I plan on doing very soon in this upcoming patch.

On Speed: Yeah, fair point. The 100K lines/s is the release binary, which is compiled by mucc itself (no optimizer) and linked with musl, the slowest way to build it. Here’s sqlite3.c (239K lines, -c, best of 5, 2.8 GHz Xeon VM):

  • tcc: 0.11 s, about 2.2M lines/s
  • mucc built by gcc -O2: 0.68 s, about 350K lines/s
  • mucc release (self-built): 1.85 s, about 130K lines/s
  • clang -O0: 2.24 s
  • gcc -O0: 5.50 s

So in this sense fast means fast next to gcc and clang, not tcc. In a profile, about 27% of the time is lexing, 9% preprocessing, 19% parsing, 16% generating assembly text, and 29% assembling that text back into machine code. Tokens are heap-allocated linked lists, there’s a full AST, and the assembly text round trip alone is about 45% of the work. tcc goes straight from tokens to machine code. Emitting machine code directly is the biggest win I could make, and better codegen would speed up the self-built binary too, which again I do plan on addressing very soon :). Most of these problems I have encountered as well in my own testing and plan on fixing them because I do use this Compiler often.