Leanpub Header

Skip to main content

Build a Compiler in C++, Backwards

Start with twelve bytes of machine code. Finish with a compiler that draws what gcc draws.

Build a Compiler in C++, Backwards
This book is 100% completeLast updated on 2026-10-03

Build a compiler from the machine up. Start with twelve bytes of machine code you encode by hand, and finish with a compiler whose ray tracer draws exactly what gcc's build of it draws.

Minimum price

$33.00

$33.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Most compiler books start with a lexer, and nothing runs for chapters. This one starts with twelve bytes of machine code.

In the first hour you encode an x86-64 program by hand, wrap it in an ELF executable by hand, and run it. No assembler and no linker touches it. Then you climb, one layer per unit, until you have a complete compiler for a language called Slate: an assembler, relocatable object files the system linker accepts, a machine IR, two register allocators, SSA, an optimiser, a lexer, a parser, a type checker, a one-command driver, and DWARF line tables that let `gdb` step through Slate source one line at a time.

The order is the point. Because the back end comes first, every stage you write later has a real machine underneath it, and when the front end arrives in Unit 13 it already has somewhere to send its output.


This book was created through a process that combines careful human planning, content direction, and advanced AI technology, followed by thorough refinement and review to ensure a high-quality final work.

**Nothing is taken on trust.** The byte counts, addresses, sizes and exit statuses in the book come from running the code, and a five-layer verification suite stands behind them: hand-written encodings assembled independently by `nasm`, nearly every transcript re-executed, every "break it and watch it fail" experiment actually performed, every listing compared byte for byte with the file it came from, and random-program oracles that compile what nobody wrote. The repository's fault log lists 171 real mistakes found while the book was written, and what caught each one.

The last unit is the proof. A 270-line ray tracer is written twice, in Slate and in C. Your compiler builds one, `gcc` builds the other, and forty-one scenes must come out identical: the same picture to the byte, and the floating-point numbers behind every pixel the same to the bit.

What you get

- 19 units in five parts, more than 400 pages laid out for the screen

- More than 130 code listings, each with its real file path and line numbers, and more than 200 terminal transcripts

- About 22,000 lines of C++23 you can build, run and break, with the verification suite that checks them

- Linux on x86-64 and the usual toolchain (g++ 13+, nasm, binutils, gdb); no framework, no package manager, no dependencies

Bundle

Bundles that include this book

Author

About the Author

Hatem M.

Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.

His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.

Contents

Table of Contents

Part I. From Bytes to Object Files
  1. Your First Program, By Hand
  2. The Assembler
  3. Object Files and Relocation
  4. Machine IR
  5. The Calling Convention
Part II. Registers
  1. Linear Scan
  2. Graph Colouring
  3. The Second Register File
Part III. What the Compiler Knows
  1. What the Compiler Knows
  2. One Definition Each
  3. Collecting
  4. The Moves That Survive
Part IV. The Front End
  1. Reading What People Write
  2. Which Tokens Belong Together
  3. What the Tree Means
  4. Where the Two Halves Meet
Part V. A Compiler You Can Use
  1. One Command
  2. Which Line
  3. A Picture

Also: an introduction, "How this book verifies itself", and a glossary.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub