The big picture

Compiler Phases

Follow a statement through scanning, parsing, type checking, three-address code, optimization, and code generation.

  • Lecture reference: Intro (cont’d): Compiler architecture · slides 3–11
  • Lecture reference: Intro (cont’d): Compiler structure with examples · slides 2–9
  • Lecture reference: Lexical Analysis · slide 3

Source

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.
Grammar
program -> stmt*
stmt    -> ID = expr ;
         | if cond then stmt [ else stmt ]
cond    -> expr relop expr        relop: == != < <= > >=
expr    -> term ( ( + | - ) term )*
term    -> factor ( ( * | / ) factor )*
factor  -> ID | NUM | FNUM | ( expr )

ID is a letter followed by letters or digits; NUM is an int literal (1), FNUM a float literal (2.3). An else belongs to the nearest if.

Declarations

The slide’s assumptions Lecture reference: Intro (cont’d): Compiler architecture · slide 4

7 Phases of a Compiler

Group the rows
Lecture reference: Intro (cont’d): Compiler architecture · slide 4

Seven phases; each one turns the output of the phase before it into a new form.

Seven phases of a compiler, with the output of each for the source above
GroupPhaseOutputSample
Programmer (source code producer)Source stringA= B1 +C;
1 Scanner (performs lexical analysis)Tokens and their attributes (lexemes)
  • 6 tokens. Whitespace separates lexemes and produces no token.
Open this token set in Scanner Rules
2 Parser (performs syntax analysis based on the grammar of the programming language)Abstract syntax tree
=A+B1C
3 Semantic analyzer (type checking, etc.)Annotated abstract syntax tree
=floatAfloat+floatint2fpfloatB1intCfloat const
  • int2fp inserted: B1 is int in the float operation B1 + C.
4 Intermediate code generator Three-address code, P-code, or RTL
Three-address code
oparg1arg2result
int2fpB1t1
+t1Ct2
:=t2A
  • 3 quads; temporaries t1 and t2.
5 Optimizer Three-address code, P-code, or RTL
Optimized three-address code
oparg1arg2result
int2fpB1t1
+t1#2.3A
  • C is the constant 2.3: C → #2.3.
  • t2 removed: + writes A directly instead of copying t2 to it.
6 Code generator Assembly code
  1. CVTLF B1,r2*
  2. MOVF #2.3,r1
  3. ADDF2 r1,r2
  4. MOVF r2,A
  • One quad at a time: temporaries live in r2, r3, …; r1 holds a loaded operand.
7 Peephole optimizer Assembly code
  1. CVTLF B1,r2
  2. ADDF2 #2.3,r2
  3. MOVF r2,A
  • MOVF #2.3,r1 ; ADDF2 r1,r2 → ADDF2 #2.3,r2 (r1 is not used afterwards).

* The slide assumes r2 already holds B1 as a float; CVTLF performs that conversion.

The C- Compiler runs these phases on a whole program.

Preprocessors, Compilers, Assemblers, and Linkers

Lecture reference: Intro (cont’d): Compiler architecture · slide 11
  1. Source Program w/preprocessor directives
  2. Preprocessor
  3. produces Source Program
  4. Compiler
  5. produces Target Assembly Program
  6. Assembler
  7. produces Relocatable Object Code
  8. Linker, which also takes Libraries and Relocatable Object Files
  9. Absolute Machine Code

Try, e.g.,

gcc -v Prog.c

With -v, gcc prints the command it runs for each step.

Lecture reference: Intro (cont’d): Compiler structure with examples · slide 17

Code generation produces assembly code (usually), which is then assembled into an object module. What next to obtain an executable object?

Show answer

Linking: the linker combines the relocatable object code with libraries and other relocatable object files into absolute machine code.