How to Write a Self-Hosted Go Compiler from Scratch
summary
Two handwritten Go compilers and a C-to-Go port produce working self-hosting compilers for a Go subset using GNU assembler and linker; the custom compiler outputs a 14 KB hello binary that runs in ~5 ms versus the official 1+ MB one at ~12 ms. Workflows use first- and second-generation bootstrapping, about six months to self-host for MiniGo, and a later BabyGo rebuild achieves self-hosting with <5,000 LOC while omitting GC, goroutines, maps and interfaces or implementing them trivially.
AI-written summary · about
chapters (24)
- 0:00Intro
- 0:50Today's Goal
- 1:45Architecture of my compilers
- 2:17Architecture of the official Go compiler
- 2:38minigo & babygo
- 10:23Encounter with 8cc
- 13:02Learn C and Go at the same time
- 13:50Tried writing a Go compiler
- 15:38Writing a Go compiler in Go: the hard parts
- 16:52minigo: Struggles in the last half
- 19:25Implementation of "map"
- 19:41Implementation of "interface"
- 20:29Funny bug: break
- 22:11Tried reading the official Go compiler
- 22:44Official compiler: size of slice
- 22:58Official compiler: variable names for slice
- 23:11Tried submitting a patch
- 24:22Started writing another Go compiler
- 24:39babygo: First commit
- 25:40Stack machine (chibicc style)
- 25:59babygo: stack machine (chibicc-like)
- 27:04babygo: Handwritten syscall
- 28:06babygo: Order of implementation
- 30:16Conclusion








