Writing a C Compiler Cover

Writing a C Compiler

Build a Real Programming Language from Scratch
by Nora Sandler
July 2024, 792 pp.
ISBN-13: 
9781718500426

Download Chapter 4: Logical and Relational Operators

Look Inside!

Writing a C Compiler back cover

Writing a C Compiler pages 60-61Writing a C Compiler pages 224-225Writing a C Compiler pages 584-585

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.

Author Bio 

Nora Sandler is a software engineer based in Seattle. She holds a BS in computer science from the University of Chicago, where she researched the implementation of parallel programming languages. More recently, she’s worked on domain-specific languages at an endpoint security company. You can find her blog on pranks, compilers, and other computer science topics at https://norasandler.com

Table of contents 

Acknowledgments
Introduction
Part I: The Basics
Chapter 1: A Minimal Compiler
Chapter 2: Unary Operators
Chapter 3: Binary Operators
Chapter 4: Logical and Relational Operators
Chapter 5: Local Variables
Chapter 6: If Statements and Conditional Expressions
Chapter 7: Compound Statements
Chapter 8: Loops
Chapter 9: Functions
Chapter 10: File-scope Variables and Storage-class
Specifiers
Part II: Types Beyond Int
Chapter 11: Long Integers
Chapter 12: Unsigned Integers
Chapter 13: Floating-Point Numbers
Chapter 14: Pointers
Chapter 15: Arrays and Pointer Arithmetic
Chapter 16: Characters and Strings
Chapter 17: Supporting Dynamic Memory Allocation
Chapter 18: Structures
Part III: Optimizations
Chapter 19: Optimizing Tacky Programs
Chapter 20: Register Allocation
Next Steps
Appendix A: Debugging Assembly Code with GDB or LLDB
Appendix B: Assembly Generation and Code Emission Tables
References

View the Copyright page
View the detailed Table of Contents
View the Index

Reviews 

"Have you written C code and really wondered how this turns into code that runs on a processor? Nora's book will not only help you see how that works but will help you to understand how to do these sorts of translations for your own assets, in language and code accessible to people without a PhD in compilers."
—Lars Bergstrom, Director of Engineering at Google

"This book is a great introduction to the topic of compilers for programming languages. It is extremely thorough, with many suggestions for further reading, while being quite enjoyable to read — a 'soup-to-nuts' treatment of compiler writing for C-like languages."
—John Reppy, Professor of Computer Science, University of Chicago

"It is refreshing to read a book on compiler design with a focus on implementing a practical real-world language, rather than working through dry academic exercises . . . practical, fun, and exciting to read if you are sick of reading textbooks on the subject."
—Rick Battagline, author of The Art of WebAssembly

Extra Stuff 

View the book's resource page for companion code, errata, updates, and other resources.

Check out Nora Sandler's interview with Raincode Labs.