badc is a small cross-platform optimizing C compiler, and a
compiler-as-library, that emits native binaries for five targets from any host.
It carries its own linker, DWARF emitter, inline-asm encoder, in-process JIT,
and SSA interpreter.
badcused to be bad when the project started out, and the name stuck.There is some compiler-building jargon in this document here and there. You can safely skip it, and jump to the usage section right away.
For the true compiler heads there is the
--dump-ssaoption which prints each function's SSA IR plus the register allocator's per-value placement to stderr before lowering.
demos/ wires each project below as a smoke test;
demos/README.md says what each exercises.
- Language interpreters:
Python3.14 on all five targets,Lua,quickjs,TCL. - Systems software:
sqlite3,curl,qemu: over a thousand units per target, self-linked, and bothqemu-system-aarch64andqemu-system-x86_64boot Linux through UEFI firmware to a shell under TCG. - Toolchains:
chibicc,tinycc, and thenasm/yasmassemblers, each run against its own test suite. - Firmware and kernels:
edk2: badc compiles the full UEFI firmware from edk2 source into a bootable OVMF / AAVMF image, and the CI boots run under it.efi_hellois a single-source EFI application, its PE subsystem selected by#pragma subsystem.kernelis a freestanding preemptive multitasking kernel on both architectures, timer interrupts and context switches included ,Linuxis the Linux kernel7.1.10. - Cryptography and compression:
TweetNaCl,Monocypher,BearSSL,miniz,bzip2. - Graphics, math, and the rest:
stb,raylibwith a Lode Runner game,kissfft, theGUIdemos (one windowed program per OS family, each cross-compiled to every target), theWDMWindows kernel driver, the NT native binaries (nt_hello, anIMAGE_SUBSYSTEM_NATIVEPE that runs underntdllalone, andnt_loader, which spawns one throughNtCreateUserProcess), and the cooperative-concurrency libraries (libmill,libdill,coroutines), whose context switches run through inline asm. - Editors, terminal tools:
uemacs, MicroEMACS built from Linus Torvalds' tree,picocom, a serial terminal emulator (driven between two pseudo-terminals when testing),screen, GNU Screen (run as both halves of its own client/server pair when testing), andvim(when testing is driven in ex mode and from keystrokes at a pseudo-terminal).
It started as a Rust port of Robert Swierczek's C compiler in four functions,
c4, and diverged enough to call the dialect
c5. Hence the c5 module and the C5Error type. c4.c ships as a test
fixture and self-hosts:
badc -O -o c4 tests/fixtures/c/c4.c # compile c4 to a native binary
./c4 hello.c # which then runs hello.cAnd you can really crank the fun up with something like
badc -O --jit tests/fixtures/c/c4.c tests/fixtures/c/c4.c tests/fixtures/c/c4.c tests/fixtures/c/c4.cto run it quadro-nested :)
It has since grown from a stack IR through a 3-operand IR to SSA with an optimizing backend, without taking on the pass count of a titan toolchain.
- What
badccan do -- a short survey ofbadcfeatures. - Getting started -- install, first run, flags, debugging, C as a script.
- Native compilation -- targets, multiple
translation units, the linker, headers and bindings,
#pragma-driven build flags, the JIT, optimizations. - Diagnostics -- the catalogue: every warning and
error with its code and name, and the
-Woptions and pragmas that select them. - Standard conformance -- implementation-defined choices, divergences from C99, and the C11 / C23 / POSIX / GCC / MSVC extensions implemented.
- The interpreter --
--interpand the pointer-tracking safety net. - Library and
no_std-- using badc from Rust. - Testing -- the suites, the fixtures, the snapshots, CI.
- Tools -- the core walker and the assembler-surface probe.
This is a personal educational/research project, it has not been sponsored or suggested by anyone, i.e. it is a product of my own volition. That said, in no event I'll be responsible for how you use this project or what happens due to that. See LICENSE for the exact terms.