GitHub|Download|Report a bug Mucc Home|Documentation»How Mucc Works

How Mucc Works

How mucc is put together: the compiler pipeline, the single binary, and how mucc builds itself. The code itself is the final reference; each file starts with what it does, and src/mucc.h has a guide to where common changes go.

The Pipeline

The compiler is a pipeline, one stage per file in src/:

StageFileDoes
1token.csource text to tokens
2preprocess.cmacros, #include, #if
3parser.c (with init.c, expr.c, builtin.c, complex.c, warn.c), type.ctokens to a typed syntax tree
4cgen.c, cgen_arm64.csyntax tree to x86-64 or ARM64 assembly
5asm.cassembly to an ELF object
6link.cobjects and archives to a static executable

Around them:

Each file is split into sections by //---------- Name --- lines; grep -n '^//-------' src/*.c lists them all.

Code Generation

The code generators walk the syntax tree and write assembly text. Values are computed into one register (%rax, or x0 on ARM64), with the stack for temporaries, and improved where it pays:

The Single Binary

The released binary, build/mucc, is mucc built by mucc and linked statically against musl, with its own headers (include/), musl's headers and library, and Linux's headers inside it. It reads them from memory, as files under the path <mucc>/, so it needs nothing next to it.

What's Inside the Binary

To see it for a build of your own, make build/mucc prints how much the headers and the library pack to, and size build/mucc the rest.

How the Files Are Packed

mucc -pack compresses them when the binary is built. The format is deflate's, simplified:

Each file is packed by itself, so mucc unpacks only those it reads, the first time it reads them. libc.a is packed a member at a time, and a link unpacks only the members it takes, so linking is as fast as with the library unpacked. The packer is plain C, built by mucc: the toolchain needs no compression tools.

Updating musl or Linux's Headers

Both live as released, in thirdparty/. Replace them with a newer release and rebuild: the packer learns its models and dictionaries from whatever files are there, so nothing is tuned to one version. test/single.sh checks every packed file unpacks to the one that went in.

How Mucc Builds Itself

Mucc is fully self-hosted: no other compiler or tool is needed to build it.

  1. Stage 1: make CC=mucc builds ./mucc with an installed mucc (the last release), or with gcc.
  2. The C library: make libc builds musl with ./mucc.
  3. The single binary: make build/mucc compiles mucc again with ./mucc, links it against that musl, and packs the headers and library into it.

make test-bootstrap proves it: it builds all of this with build/mucc alone, in a copy of the tree, with gcc, as, ld, ar and the rest replaced by commands that fail, and checks the result is build/mucc again, byte for byte. CI runs it on x86-64 and ARM64. Each release is built this way by the one before it, which is why src/ may only use C the latest release compiles.

ARM64

cgen_arm64.c makes ARM64 code, and asm.c and link.c handle ARM64 objects and relocations as well as x86-64's. ARM64 has no instructions for 128-bit long double arithmetic, so runtime/arm64.c provides the routines that gcc's libgcc would, in the bundled musl's libmucc.a.