AI Builds C Compiler from Scratch in 24 Minutes, Then Creates Super Mario Game
The author uses AI (Codex) to bootstrap a C compiler from a minimal seed binary, iteratively self-compiling it until stable, then writes a Super Mario-like game in the limited C subset and compiles it with the self-hosted compiler, demonstrating the full bootstrapping process in 24 minutes.
The Chicken-and-Egg Problem of Compilers
The article opens with the classic question: which came first, the C language or the C compiler? Historically, Dennis Ritchie evolved the B language into C while rewriting the compiler to emit PDP-11 machine code directly; the language and its compiler grew together rather than being designed upfront.
The Seed Compiler: A Hand-Crafted Binary
Codex produced a single binary named seed that runs on macOS (ARM64, Apple M4). It contains two parts:
A tiny virtual machine implemented in raw ARM64 machine instructions.
Hand-written VM bytecode that implements a minimal C compiler.
This seed compiler understands only a very restricted C dialect: functions, local variables, assignment, loops ( while), and branches ( if). It does not support operators; comparisons and arithmetic must be written as function calls, e.g., lt(i, 21) instead of i < 21 and add(i, 3) instead of i + 3. Codex explained this design avoids operator parsing in the seed.
The first compilation test used the following code:
int twice(int x) {
return add(x, x);
}
int main() {
int i;
i = 9;
while (lt(i, 21)) {
i = add(i, 3);
}
if (eq(i, 21)) {
return twice(i);
}
return 1;
}Bootstrapping the Compiler
Using the seed's limited syntax, Codex wrote a new compiler source file compiler.c (617 lines). This version added support for operators (e.g., 2 + 3 * 4), else, break, continue, arrays, and strings.
The bootstrapping proceeded in three rounds:
Round 1: The seed binary compiled compiler.c, producing the first self-hosted compiler.
Round 2: That first compiler recompiled compiler.c, producing a second version.
Round 3: The second compiler compiled compiler.c again, producing a third version.
A byte-for-byte comparison of the second and third compiler binaries showed they were identical, proving the compiler had reached a fixed point and no longer depended on the seed.
The author notes that this compiler still lacks struct, pointers, and other C99 features; tombkeeper's original experiment implemented full C99, but the prompt omitted that requirement.
Building a Game with the Self-Hosted Compiler
With a working compiler, Codex wrote approximately 900 lines of C code for a Super Mario–style platformer featuring three levels, patrolling enemies, coins, pipes, checkpoints, and a menu where every letter is drawn pixel-by-pixel. The game code uses only the syntax supported by the bootstrapped compiler (no structs, no pointers).
The self-hosted compiler then compiled this game source into a standalone executable that runs directly on macOS without any browser or external toolchain.
Reflections
The entire chain — seed binary → self-hosted compiler → compiled game — completed in 24 minutes without using any pre-existing compiler, assembler, or linker. The author emphasizes that the process can be continued to add more language features, eventually yielding a full modern C compiler. The experiment turns the abstract concept of bootstrapping into a tangible, reproducible demonstration accelerated by AI.
PS: A recent domestic model ran for one hour without success on the same task.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
