How Compilers Work: From Source Code to Machine Code

Matt George

Fractional CTO · Founder of Injuria.ai

•7 min read•

You write total = price * quantity and a processor eventually performs a multiply and stores a result in memory. Between those two things is a surprising amount of work, and the program that does it is a compiler.[7] Its design is a clean pipeline: a series of stages that each take the program one step further from human text and one step closer to machine instructions.[8] Knowing the stages makes error messages less mysterious and performance behavior less like luck.

The pipeline at a glance

A compiler does not translate source to machine code in one leap. It passes the program through a sequence of representations, each simpler and more explicit than the last. The front end figures out what your code means, the middle improves it, and the back end turns it into instructions for a specific processor. Splitting the job this way is what lets compilers support many languages and many processors without rewriting everything each time.[9]

Lexing: text into tokens

The first stage, the lexer, reads your source as a raw stream of characters and groups it into tokens, the smallest meaningful pieces.

source:  total = price * 2
tokens:  IDENT(total)  EQUALS  IDENT(price)  STAR  NUMBER(2)

Under the hood this is a job for regular expressions and the finite automata they compile to, which is why a lexer is fast and simple. One common claim needs a caveat, though: whitespace is not always thrown away. In C or JavaScript it is noise, but in Python indentation is significant, so the lexer emits tokens for it to mark where blocks begin and end.[3] Many modern lexers also keep comments rather than discard them, so tools and documentation generators can see them.

Parsing: tokens into a tree

The parser takes that flat list of tokens and builds structure from it, following the grammar of the language, which is formally a context-free grammar implemented with an algorithm like recursive descent or a generated LL or LR parser. It often first builds a concrete syntax tree that mirrors the grammar exactly, then simplifies that into an abstract syntax tree, or AST, which drops the noise and keeps the structure.

        =
       / \
   total   *
          / \
      price   2

The tree makes precedence and grouping explicit: it knows the multiplication happens before the assignment. This is also the stage that catches syntax errors. A detail worth appreciating here is error recovery. A naive parser stops at the first mistake, but good compilers deliberately resynchronize and keep going so they can report several errors in one run, which is the difference between fixing ten typos in one pass and fixing them one maddening compile at a time.

Semantic analysis: does it actually make sense

A program can be grammatically correct and still be nonsense, like adding a number to a function or using a variable that was never declared. Semantic analysis checks meaning: it resolves names to the things they refer to, tracks scopes so it knows which x you mean, and checks types. This is where most helpful errors come from. "Undefined variable," "type mismatch," and "cannot call a value that is not a function" are semantic errors. The code parsed fine, it just does not add up.

Intermediate representation: a simpler middle language

Rather than translate the checked AST straight to one processor's instructions, most compilers lower it into an intermediate representation, an IR, a simpler and more uniform language independent of both the source and the target.[1] This middle layer is one of the best ideas in compiler design: optimizations written against the IR work for any source language that compiles to it, and a new processor can be supported without touching the front end. LLVM is the best-known example, which is why languages like Rust and Swift could reuse a mature optimizer and backend instead of building their own.[6] Most serious IRs use a form called static single assignment, where every variable is assigned exactly once, a constraint that sounds academic but is what makes modern optimizations tractable to reason about.[2]

Optimization: making it faster and smaller

With the program in IR form, the compiler improves it without changing what it does.[10] Constant folding computes known expressions ahead of time, so 2 * 60 becomes 120 at compile time. Inlining pastes a small function's body into its caller to avoid the call overhead.[11] A pair of terms often gets blurred and is worth separating: eliminating an assignment whose result is never read is dead-store elimination, while removing code that can never run is unreachable code elimination; dead-code elimination removes code whose results are never used. Both help, and they run as passes over the IR. It is also worth knowing that optimization happens in two places: machine-independent passes on the IR, and machine-dependent passes during code generation, like instruction scheduling and peephole fixes tuned to a specific chip.

Code generation, and the two steps everyone forgets

The back end turns the optimized IR into instructions for a processor family such as x86 or ARM, selecting the actual machine instructions and handling register allocation, deciding which values live in the processor's small set of fast registers and which spill to slower memory.[4] But the pipeline does not end at machine code, and this is the part introductions skip. The compiler typically emits assembly or object files, not a runnable program. An assembler turns assembly into object code, and then a linker combines those object files, resolves references between them and to libraries, and produces the final executable. Plenty of real-world build failures ("undefined reference to...") are linker errors, not compiler errors, and they are baffling until you know this last step exists.

Interpreters and JITs, by contrast

Not every language takes the whole path to a standalone binary. An interpreter walks the AST or a bytecode and executes it directly, step by step, which makes the edit-and-run loop fast and the program portable at some cost in speed. A just-in-time compiler blends the approaches, and the common description, "it interprets first, then compiles hot paths," is only one flavor. That tiered model is what the JavaScript engines in your browser and the Java Virtual Machine use, watching which code runs often and promoting it to optimized machine code while the program runs.[5] Other JITs skip interpretation entirely and compile everything to unoptimized code on first run, optimizing later. Either way, it is how a language once dismissed as slow can approach native speed on the code that matters.

Why knowing the stages pays off

Every language you use runs some version of this pipeline, whether fully ahead of time, on the fly, or a mix. The payoff of knowing it is not trivia, it is diagnosis. A cryptic "unexpected token" is the parser, a "type mismatch" is semantic analysis, an "undefined reference" is the linker, and a program that got three times faster with a flag flipped is the optimizer. A compiler stops being a black box the moment you can point at which stage an error or a speedup came from, and nearly every confusing thing it does becomes a specific, locatable step in a line you can now picture end to end.

Sources (11)
  1. LLVM Language Reference Manual
  2. Static single-assignment form (Wikipedia)
  3. Python Language Reference: Lexical analysis
  4. GCC online documentation
  5. V8 JavaScript engine documentation
  6. Rust Compiler Development Guide
  7. William M. Waite, Gerhard Goos, Compiler Construction (Springer Science & Business Media, 2012)
  8. Keith D. Cooper, Linda Torczon, Engineering a Compiler (Morgan Kaufmann, 2022)
  9. Dick Grune, Kees van Reeuwijk, Henri E. Bal, Modern Compiler Design (Springer Science & Business Media, 2012)
  10. Stefan Jähnichen, Compiler Construction (Springer, 2004)
  11. Torben Ægidius Mogensen, Introduction to Compiler Design (Springer, 2017)

Recommended reading