Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Nora Sandler

Rating No ratings yet

A fun, hands-on guide to writing your own compiler for a real-world programming language.Compilers are at the heart of everything programmers do, yet even experienced developers find them intimidating. For those eager to truly grasp how compilers work, Writing a C Compiler dispels the mystery. This book guides you through a fun and engaging project where you’ll learn what it takes to compile a real-world programming language to actual assembly code. Writing a C Compiler will take you step by step through the process of building your own compiler for a significant subset of C—no prior experience with compiler construction or assembly code needed. Once you’ve built a working compiler for the simplest C program, you’ll add new features chapter by chapter. The algorithms in the book are all in pseudocode, so you can implement your compiler in whatever language you like. Along the way, you’ll explore key concepts like: - Lexing and parsing: Learn how to write a lexer and recursive descent parser that transform C code into an abstract syntax tree. - Program analysis: Discover how to analyze a program to understand its behavior and detect errors. - Code generation: Learn how to translate C language constructs like arithmetic operations, function calls, and control-flow statements into x64 assembly code. - Optimization techniques: Improve performance with methods like constant folding, dead store elimination, and register allocation. Compilers aren’t terrifying beasts—and with help from this hands-on, accessible guide, you might even turn them into your friends for life.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on project book that takes you from a bare-bones C compiler to a working x64 code generator, one feature at a time. Best for programmers who learn by building and want to demystify compilers without a formal course. 【Book Arc】 - **Opening (~0%–15%)**: Sets up the project and mental model — the four compiler passes, a minimal end-to-end pipeline, and the test suite you'll use to validate every later feature. - **Early (~15%–35%)**: Builds the front end and basic codegen for integer expressions — lexer, recursive-descent parser, precedence climbing, TACKY intermediate representation, and x64 assembly emission for unary, binary, logical, and relational operators. - **Middle (~35%–55%)**: Adds program structure — local variables, semantic analysis and variable resolution, if/conditional expressions, compound statements and scopes, loops, functions with calling conventions, and file-scope declarations. - **Late (~55%–80%)**: Expands the type system beyond int — long, unsigned, floating point, pointers, arrays and pointer arithmetic, characters and strings, dynamic memory allocation, and structures. - **Ending (~80%–100%)**: Turns a correct compiler into a faster one — optimizing TACKY programs, register allocation, plus appendices on debugging assembly with GDB/LLDB and code-emission tables. 【Key Takeaways】 - **Build incrementally, validate constantly** (Opening): Each chapter adds one feature on top of a working compiler, backed by a test suite — the core learning loop is implement, test, repeat. - **A clean multi-pass pipeline beats a monolith** (Early): Lexing, parsing, TACKY IR generation, and assembly generation are separated so each stage stays tractable and debuggable. - **Parsing precedence is a real design decision** (Early): The book contrasts grammar refactoring with precedence climbing, showing why the latter scales better for expression-heavy C. - **An intermediate representation pays off** (Middle): TACKY lets you handle control flow, short-circuiting, and later optimizations without tangling them into assembly generation. - **Semantic analysis is where C's rules live** (Middle): Variable resolution, scopes, type checking, and linkage are treated as distinct passes — essential for correctness beyond syntax. - **Calling conventions are the hardest practical hurdle** (Middle): Understanding the System V ABI and stack layout is what makes real function calls and libraries work. - **Types multiply complexity** (Late): Pointers, arrays, floats, and structs each force revisiting earlier passes — a lesson in how language features ripple through a compiler. - **Optimization comes last, deliberately** (Ending): Constant folding, dead store elimination, and register allocation are introduced only after correctness, mirroring real compiler development. 【Reading Tips】 - **Code along, don't just read**: The book is pseudocode-based by design; pick an implementation language early and keep the test suite green as you go. - **Deep-read Part I**: The minimal compiler and operator chapters establish patterns reused throughout — skimming here makes later parts painful. - **Treat functions/calling conventions as a checkpoint**: Slow down on the System V ABI section; it's the most common place readers get stuck. - **Skim optimization if time-limited**: Part III is valuable but separable — you can return after you have a correct compiler. - **Use the appendices actively**: The GDB/LLDB debugging guide is worth consulting the first time generated assembly misbehaves. 【Coverage Limits】 Excerpts are dominated by front matter and the table of contents; chapter-level detail on specific algorithms and worked examples is thin, so this guide maps structure and themes rather than deep technical content.
Page 1
S C R A T C H THE F INEST IN GEEK ENTERTA INMENT ™ nostarch.com ® ® W R IT IN G A C C O M P IL E R S A N D L E R Compilers are at the heart of everything pro...
View in text
Page 11
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . ....
View in text
Page 14
Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Assembly Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
View in text
Page 15
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Extra Credit: Compound Assignment, Increment, and Decrement . . . . . . . . . . . . . . . . 1...
View in text
Page 17
234 Assembly Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Generating Assembly for Variable Defi...
View in text
Page 18
. . . . . . . . . . . 302 Matching the End of a Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 The Parser . . . . . . . . . . ....
View in text
Page 19
Static Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 Initializing Scalar Variables with ZeroInit . . . . . . . . . ....
View in text
Page 20
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Instruction Fix-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
View in text
Tags
AI categories
Programming LanguageSoftware
ISBN: 1718500432
Publisher: No Starch Press
Publish Year: 2024
Language: English
Pages: 904
File Format: PDF
File Size: 10.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…