How Compilers Work
From text you wrote to instructions a CPU runs.
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
The classic phases
- Lexing. Break the source into tokens: keywords, identifiers, literals.
- Parsing. Assemble tokens into an abstract syntax tree following the grammar.
- Semantic analysis. Type-check, resolve names, catch misuse.
- IR. Lower the tree into a simpler intermediate representation.
- Optimize. Fold constants, inline, remove dead code on the IR.
- 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.
Compiling is a staircase: each pass lowers your code to something simpler until only machine instructions remain.
No comments:
Post a Comment