Skip to content
kromychPublic

About

Former bad C compiler. Now can cross-compile lots of complex stuff, look inside!

Topics

Resources

Contributing

Stars

9 stars

Watchers

0 watching

Forks

Repository files navigation

CI Release crates.io License: MIT OS Arch

If you wish to make an apple pie from scratch, you must first invent the universe.

-- Carl Sagan, Cosmos (1980), p. 218

badc is a slim cross-platform optimizing C17 compiler. There is extensive support for the older standards and good support for the newer ones, as well as support for the numerous GNU C, Clang, and MSVC extensions that real-world software requires.

From any host, badc builds native images for five targets: macOS on ARM64, Linux on ARM64 and x86-64, and Windows on ARM64 and x86-64. It has its own preprocessor, assembler, linker, archiver, and debug-info writers (DWARF and PDB), as well as an in-process JIT and an SSA interpreter. CI runs more than 80 jobs and more than 5,000 tests on every pull request and every push to master, and fuzzing jobs run daily.

badc ships as a single binary. The project can also be used as a Rust library.

  • badc was bad when the project started, and the name stuck.
  • --dump-ssa prints each function's SSA IR and the register allocator's placement of each value to stderr before lowering.
  • --dump-records prints the size, alignment and member offsets of each tagged structure and union, and of each typedef naming one, as laid out for --target.

Contents

Demos

demos/ builds each project below with badc and runs it as a smoke test. demos/README.md says what each one exercises.

  • Language interpreters: Python 3.15 on all five targets, Lua, QuickJS, Tcl.
  • Systems software: SQLite, curl, QEMU. QEMU is over a thousand units per target, linked by badc's own linker. The badc-built qemu-system-aarch64 and qemu-system-x86_64 boot Linux through UEFI firmware to a shell under TCG.
  • Toolchains: chibicc, TinyCC, and the nasm and yasm assemblers, each run against its own test suite.
  • Firmware and kernels:
    • edk2: badc builds the OVMF and AAVMF UEFI firmware from source, and the CI boots run under that firmware.
    • efi_hello: a single-source EFI application; #pragma subsystem selects its PE subsystem.
    • kernel: a freestanding preemptive multitasking kernel for both architectures, with timer interrupts and context switches.
    • Linux: the Linux 7.2.9 kernel; see the kernel page.
  • Cryptography and compression: TweetNaCl, Monocypher, BearSSL, miniz, bzip2, and OpenSSL with its make test.
  • Graphics and math: stb, raylib with a Lode Runner game, Nuklear, kissfft, and the GUI demos: one windowed program per OS family, each cross-compiled to every target. Nuklear's demo windows, rendered without a display, give the same frames under badc -O0, badc -O, and the host compiler.
  • Windows system code: the WDM kernel driver; SimpleVisor, an Intel VT-x hypervisor built as a native-subsystem driver; nt_hello, an IMAGE_SUBSYSTEM_NATIVE program that runs on ntdll alone; and nt_loader, which starts one through NtCreateUserProcess.
  • Coroutines: libmill, libdill, and coroutines, whose context switches run through inline asm.
  • Editors and terminal tools: uemacs, MicroEMACS from Linus Torvalds's tree; picocom, a serial terminal emulator, tested between two pseudo-terminals; GNU Screen, tested as both halves of its client/server pair; and Vim, tested in ex mode and with keystrokes at a pseudo-terminal.

Lineage

badc started as a Rust port of Robert Swierczek's c4, a C compiler in four functions that accepts only a subset of C. The dialect diverged enough to get its own name, c5, hence the c5 module and the C5Error type. It has since moved from a stack IR through a three-operand IR to SSA with an optimizing backend.

The venerable c4 runs itself. Here is how to run the hello program under c4, under c4, under c4 compiled by badc's JIT:

badc -O --jit tests/fixtures/c/c4.c tests/fixtures/c/c4.c tests/fixtures/c/c4.c \
    tests/fixtures/c/c4_selfhost_hello.c
Hello 123
exit(0) cycle = 11
exit(0) cycle = 37229
exit(0) cycle = 11152710

Documentation

  • What badc can do: a short survey of the features.
  • Getting started: install, first run, flags, debugging, C as a script.
  • Native compilation: targets, the instruction-set baseline (x86-64-v3, the Apple M1's ARMv8.4-A), multiple translation units, the linker, headers and bindings, #pragma build flags, the JIT, optimizations.
  • Diagnostics: every warning and error with its code and name, and the -W options and pragmas that select them.
  • Standard conformance: implementation-defined choices, divergences from C17, and the C23, POSIX, GCC, and MSVC extensions badc implements.
  • The Linux kernel: what badc compiles, links, and boots of the kernel, and how it is checked.
  • --interp and --jit: running C without writing an image, and the interpreter's pointer checks.
  • Library and no_std: using badc from Rust.
  • Testing: the suites, fixtures, snapshots, fuzzing, benchmarks, and CI.
  • Tools: the helper programs under tools/ and scripts/.
  • Performance: CI's benchmark results.
  • Contributing: what a pull request needs to be merged.

Disclaimer

This is a personal educational and research project. Nobody sponsored or suggested it. I am not responsible for how you use it or for what follows from that use. See LICENSE for the exact terms.

About

Former bad C compiler. Now can cross-compile lots of complex stuff, look inside!

Topics

Resources

Contributing

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages