Writing a C Compiler Cover

Writing a C Compiler

Build a Real Programming Language from Scratch
by Nora Sandler
August 2024, 792 pp.
ISBN-13: 
9781718500426
Use coupon code PREORDER to get 25% off!

Download Chapter 4: Logical and Relational Operators

Compilers are at the heart of everything programmers do—and they’re cool as heck—but they have an intimidating reputation. Writing a C Compiler makes this fascinating topic fun and accessible. This book is a step-by-step guide to writing your own compiler for a large subset of C. Your compiler will generate assembly code for the ubiquitous x64 processor architecture, so you’ll learn what it takes to compile a real-world programming language to run on a real-world processor.

You’ll build a working compiler for the simplest possible C programs in Chapter 1, then add a new feature in each subsequent chapter. You won’t need any prior knowledge of compiler construction or assembly code, and the implementation is presented in pseudocode, so you can write your compiler in whatever language you like. 

You’ll learn how to:

  • Write a lexer and recursive descent parser to turn source code into an abstract syntax tree (AST)
  • Implement arithmetic operations, control-flow statements, and other C language constructs in x64 assembly
  • Use your system’s calling convention to call functions defined in external libraries
  • Perform several common optimizations, including constant folding and dead store elimination
  • Write a graph coloring register allocator to efficiently assign values to hardware registers

In Writing a C Compiler, you’ll tackle a fun, challenging project—and gain a deeper understanding of the code you write and the systems it runs on.

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. After several years as a penetration tester, she found her way back to compilers. Most recently, she worked on domain-specific languages at an endpoint security company. You can read her blog about 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

The chapters in red are included in this Early Access PDF.

Reviews 

"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 

Check out Nora Sandler's interview with Raincode Labs.