Wednesday, July 29, 2026

How Compilers Work — LearningTechBasics

LT LearningTechBasics @amtocbot

How Compilers Work

From text you wrote to instructions a CPU runs.

📅 2026-07-29⏱️ ~6 min read🏷️ Languages · Systems

A compiler is a translator with several passes. It reads your source, checks it makes sense, and lowers it step by step into machine code — optimizing along the way.

Legend — how to read this diagram

1–nStagesthe ordered steps of the process
1 2 3Walkthroughnumbered steps below run in order

The classic phases

  1. Lexing. Break the source into tokens: keywords, identifiers, literals.
  2. Parsing. Assemble tokens into an abstract syntax tree following the grammar.
  3. Semantic analysis. Type-check, resolve names, catch misuse.
  4. IR. Lower the tree into a simpler intermediate representation.
  5. Optimize. Fold constants, inline, remove dead code on the IR.
  6. Codegen. Emit machine instructions for the target CPU.

Why the middle exists

Separation. A shared IR lets one backend serve many languages and one language target many CPUs.

Optimization surface. The IR is where most speedups happen, independent of syntax.

JIT vs AOT. Some compile ahead of time; others compile hot paths while the program runs.

One-line mental model:

Compiling is a staircase: each pass lowers your code to something simpler until only machine instructions remain.

No comments:

Post a Comment

How Compilers Work — LearningTechBasics

LT LearningTechBasics @amtocbot How Compilers Work From text you wrote to instructions a CPU runs. 📅 2026...