Compiling C, explained

Compiling is not one translation. Each compiler has several stages, and each one throws away something the last one needed. Type C below, play any stage to watch it do its work one step at a time, then run what comes out.

Source

A subset of C: int and char, locals, arithmetic, if, while, for, functions that can call each other, and pointers and arrays —&x, *p, a[i]. Edit it and all seven stages recompile.

Examples

Preprocess

Text in, different text out. The compiler proper never sees what you wrote.

inthe characters you typedoutpreprocessed text

STEP 1 / 1

Where this step points in your source

What it produces

Scan

Characters become tokens. Spelling stops mattering; categories start.

inpreprocessed textouta token list

STEP 1 / 1

Where this step points in your source

What it produces

Parse

The flat list becomes a tree. Precedence is a shape, not an order.

ina token listouta syntax tree

STEP 1 / 1

Where this step points in your source

What it produces

Analyse

Names get homes and types get checked. None of this is in your source.

ina syntax treeouta symbol table and types

STEP 1 / 1

Where this step points in your source

What it produces

Lower to IR

The tree becomes a list. Control flow becomes jumps.

ina syntax tree + a symbol table and typesoutthree-address IR

STEP 1 / 1

Where this step points in your source

What it produces

Allocate registers

More values than registers. Which of them can share one is a graph-colouring problem.

inthree-address IRouta register assignment

STEP 1 / 1

Where this step points in your source

What it produces

Emit assembly

Instructions a processor could run — and a lot of moving data around.

inthree-address IR + a register assignment + a symbol table and typesoutx86-64 assembly

STEP 1 / 1

Where this step points in your source

What it produces

Run it

Not a rewrite. This executes the IR from stage five, one instruction at a time.

inthree-address IRouta return value, and whatever it wrote to memory

STEP 1 / 1

Where this step points in your source

What it produces

What is left out

A real compiler, but a small one. Being clear about the edges matters more than pretending otherwise.

×1