r/Compilers • • 25d ago

Anything like Crafting Interpreters?

I’m looking to see if there are any books (or other sources, like articles/pages) similar to Crafting Interpreters but for compilers instead, i.e., instructing you clearly on how to implement each stage of a basic compiler.

It doesn’t need to be as clear or direct by giving you every line of code or such, but clear and instructional such that someone can follow along. Would also be very helpful if (assuming it includes code) it uses a modern or popular language (so I don’t have to dissect ML or Forth code).

I’ve come across some books that advertise this but in reality compile to bytecode with a VM (so still an interpreter in a sense), but I’m looking for books focused on actual native code compilers (at least down to the IR stage).

Edit: clarification.

95 Upvotes

24 comments sorted by

6

u/Bergergi 25d ago edited 25d ago

I've seen a lot of DIY type compiler books released over the past few years. Have you looked at:

Sandler, Writing a C compiler

Ball, Writing an Interpeter/Compiler in Go

(I've not read them, but they look on the mark).

I used Cooper&Torczon:"Engineering a Compiler" and Appel:"Modern Compiler implementation in C" in combination when I first got into this stuff long ago. They gave me what I needed, and so I can recommend that pairing, but books like Nystrom, Sandler, Ball, probably has a lighter how-to feel.

6

u/Big-Rub9545 25d ago

Sandler's is great but seems to skip over some of the actual steps needed to implement certain parts (e.g., the first chapter basically needs you to write a whole functioning compiler, albeit a minimal one).

Ball's, from what I've seen, seems to fit my comment at the end, i.e., not actually a native machine code compiler (bytecode instead).

3

u/SalubriousInsoucianc 24d ago edited 10d ago

Sandler is what the OP needs. It introduces AoT asm gen in project form.

Unlike Nystrom, it starts with a trivial end-to-end compiler then adds small features (instead of writing a lexer, then parser, …). I find that a faster, more rewarding way to hack compilers.

OP, why do you want a book that walks you through every line of lexing and parsing again? I can’t think of a book that you want, but u/Bergergi has good recommendations for the subject matter.

1

u/solgul 24d ago

The ball books are great.

2

u/seg_lol 24d ago

If you are writing a compiler, learning to read ML will get you very far.

Use the Futumura, be the interpreter that compiles.

Deegen: A JIT-Capable VM Generator for Dynamic Languages https://arxiv.org/abs/2411.11469

Partial Evaluation, Whole-Program Compilation https://arxiv.org/abs/2411.10559v1

then read the copy and patch papers

Copy-and-Patch Compilation: A fast compilation algorithm for high-level languages and bytecode https://arxiv.org/abs/2011.13127

Do a little more research yourself before asking a low effort question.

4

u/Big-Rub9545 24d ago

Good lord the pretentiousness.

21

u/FloweyTheFlower420 25d ago

https://github.com/SeaOfNodes/Simple

a fairly simple optimizing compiler with a cool IR

14

u/alphaglosined 25d ago

Compiler Design in C - Holub, now freely available as a pdf.

9

u/raymyers 24d ago

Highly recommend Essentials of Compilation, an incremental approach in Racket (or Python)

This uses the nanopass architecture and develops and end-to-end compiler that gradually gets more features. It made a bunch of ideas finally click for me.

https://mitpress.mit.edu/9780262047760/essentials-of-compilation/

4

u/mpahrens 24d ago

Seconding this, it's really good and worked well for the course I taught.

5

u/ventZl 24d ago

> I’ve come across some books that advertise this but in reality compile to bytecode with a VM (so still an interpreter in a sense)

No, whether the output is a bytecode for VM, or native binary, from compiler design point of view it doesn't matter. Compiler structure is basically the same, just the instruction set compiler emits is different.

With modern (30 years old or younger) CPUs, even the platform itself optimizing execution under the hood holds both for JIT VMs and real-world CPUs. OOO CPUs reorder instructions similarly to what JIT can do, to execute code faster.

2

u/Inconstant_Moo 24d ago

There is one important difference which is that a single bytecode instruction FOO 42 can mean "Call the .foo method on the object you'll find in vm.FooList[42], which we baked at compile time", and so you're compiling the hard parts into objects and closures where the bytecode instruction is just the magic word that makes it go brrr. Someone working on a VM needs to be taught how to think like this because if all you've known is machine code, you'll probably produce something way too much like machine code at first, and not avail yourself of the VM's magicks.


I see that I've been organized enough to group these things together in my VM declaration.

// These are things the index of which can be an operand.
	Tokens           []*token.Token
	LambdaFactories  []*LambdaFactory
	SnippetFactories []*SnippetFactory
	GoFns            []GoFn
	Evaluators       []func(string) values.Value // One per compiler, to implement `eval`.
	// As sometimes can this; it is indexed by the numbers of concrete types.
	ConcreteTypeInfo []TypeInformation
// This contains the information necessary to call the tests of a given compiler.
	Tests            [][]TestInfo
	Labels           []string // Array from the number of a field label to its name.
	ValidationErrors []*ValidationError
	Tracking         []TrackingData // Data needed by the 'trak' opcode to produce the live tracking data.
	AbstractTypes    []AbstractTypeInfo

1

u/Normal-Reaction5316 24d ago

Agreed. FWIW, my most capable toy compiler to date compiles to an intermediate representation similar to (and heavily inspired by) .NET byte code. The compiler includes a VM for it, which means the compiler can actually run the code of pure functions at compile time. But the byte code is then subsequently compiled further into native WINTEL32 machine code.

4

u/asindarov 24d ago

https://link.springer.com/book/10.1007/978-3-642-17548-0 [Compiler Design: Analysis and Transformation] [0]

https://dl.acm.org/doi/book/10.5555/2502810 [Compiler Design: Code Generation and Machine-level Optimization] [1]

you might want to read these books, I enjoyed reading "Compiler Design: Analysis and Transformation" while taking compiler construction course. I have not read the [1] but I think it is also equally interesting.

3

u/il_dude 25d ago

There's Appel's book on Modern Compiler Implementation but the best edition is in ML. It has chapters on code generation and optimization, but not very detailed to IR level. I'm not aware of modern books detailing optimizations aside from Muchnick, which as far as I know contains lots of errors but is a good reference.

3

u/dobryak 25d ago

Programming Languages: Application and Interpretation

3

u/Big_Tadpole7174 24d ago

This tutorial helped me a lot back in the day. https://compilers.iecc.com/crenshaw/

3

u/Polyscone 24d ago

Not a book but I have one playlist on YT from 2025 where I go through a basic compiler compiling all the way down to IR and x64 machine code on Windows here https://youtube.com/playlist?list=PLfwYKZej86PpEFsy6HQB9WQIlIGmfOy_s&si=4RP5d9zAdM5ixX7Q

Then another playlist that I'm slowly working through in more depth from the bottom up here https://youtube.com/playlist?list=PLfwYKZej86Po9QdiPknTLbtWyeUD7rKlP&si=4h4dSRXkVDV0Kpvh

Maybe they can help you get the information you want if you don't mind sitting through videos

1

u/Necromancer5211 25d ago

Crafting interpreters has second part that is building compiler in C. Not sure if you have seen it. Its in the same book

0

u/Big-Rub9545 25d ago

My last comment in the post was to exclude sources like that (compiles to bytecode with a VM).

1

u/Flimsy-Low-1326 22d ago

SCIP show the procedure in Chapter 5.