Programming
Languages
Compiler phases
Prof. Xin Yuan
Overview
Compiler phases
10/2/2013
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate (machine-independent) code generation
Intermediate code optimization
Target (machine-dependent) code generation
Target code optimization
COP4020 Spring 2013
2
Source program with macros
A typical compilation process
Preprocessor
Source program
Compiler
Target assembly program
Try g++ with –v, -E, -S flags on linprog.
assembler
Relocatable machine code
linker
Absolute machine code
10/2/2013
COP4020 Spring 2013
3
What is a compiler?
A program that reads a program written in one language
(source language) and translates it into an equivalent program in another language (target language).
Two components
Understand the program (make sure it is correct)
Rewrite the program in the target language.
Traditionally, the source language is a high level language and the target language is a low level language (machine code). Source program compiler
Target program Error message
10/2/2013
COP4020 Spring 2013
4
Compilation Phases and Passes
Compilation of a program proceeds through a fixed series of phases
Each phase use an (intermediate) form of the program produced by an earlier phase
Subsequent phases operate on lower-level code representations
Each phase may consist of a number of passes over the program representation
10/2/2013
Pascal, FORTRAN, C languages designed for one-pass compilation, which explains the need for function prototypes
Single-pass compilers need less memory to operate
Java and ADA are multi-pass
COP4020 Spring 2013
5
Compiler Front- and Back-end
Abstract syntax tree or other intermediate form
Source program (character stream)
Scanner
(lexical analysis)
MachineIndependent Code