r/Compilers • u/Big-Rub9545 • 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.
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.
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.
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/
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.
5
u/phischu 24d ago
As usual Compiling to Assembly from Scratch!
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/suhcoR 24d ago
Have a look at Wirth's compiler book: http://www.projectoberon.net/wirth/CompilerConstruction/CompilerConstruction.pdf
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
1
u/Flimsy-Low-1326 22d ago
SCIP show the procedure in Chapter 5.
-6
u/WillingPirate3009 25d ago
Cfbr