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/:
| Stage | File | Does |
|---|---|---|
| 1 | token.c | source text to tokens |
| 2 | preprocess.c | macros, #include, #if |
| 3 | parser.c (with init.c, expr.c, builtin.c, complex.c, warn.c), type.c | tokens to a typed syntax tree |
| 4 | cgen.c, cgen_arm64.c | syntax tree to x86-64 or ARM64 assembly |
| 5 | asm.c | assembly to an ELF object |
| 6 | link.c | objects and archives to a static executable |
Around them:
main.cis the driver: it parses the command line and runs the stages, and holds the table of files built into the binary.ar.cismucc -ar, the archiver.pack.cismucc -pack, which compresses the single binary's files, and the code that unpacks them.diag.cprints errors and warnings for every stage.strings.c,hashmap.candunicode.care support code.mucc.hdeclares what they share.
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:
- local variables whose address is never taken live in callee-saved registers;
- integer expressions of constants are folded, division included;
- constants, local variables, and members or elements at constant offsets are used as instruction operands in place;
- a call's arguments that are constants or variables are loaded straight into their registers;
- comparisons jump on the CPU's flags directly.
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¶
- The compiler itself, code and data (with the musl functions it uses): about a third of the binary.
- Linux's, musl's and mucc's headers, compressed to about a fifth of their size.
- musl's
libc.aand startup files, compressed to about a quarter of theirs. - The list of those files, with their names.
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:
- LZ77: a file is literal bytes and copies of earlier bytes, found by an optimal parse (the cheapest coding of each prefix, in bits).
- Shared Huffman codes: the codes aren't in each file but shared by all the files of a kind: one model for the headers, one for the library. Most headers are too small to carry their own.
- A dictionary for each model: bytes a file is packed as if it came after, so its copies can come from there too. For the headers it is the lines most of them share (license comments, include guards); for the library, the stretches of code and tables most members share.
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.
- Stage 1:
make CC=muccbuilds./muccwith an installed mucc (the last release), or with gcc. - The C library:
make libcbuilds musl with./mucc. - The single binary:
make build/mucccompiles 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.