Modern Compilers
Modern Compilers
Compilers have not changed a great deal
since the days of Backus. They still consist
of two main components:
The front-end reads in the program in the
source languages, makes sense of it, and
stores it in an internal representation…
And the back-end, which converts the internal
representation into the target language,
perhaps with optimizations. The target
language used is typically an assembly
language, but it is often easier to use a more
established, higher-level language.
Source Language
Structure
of a ?
Compiler
Target Language
Source Language
Front End
Structure
of a Intermediate Code
Compiler
Back End
Target Language
Source Language
Lexical Analyzer
Syntax Analyzer Front
End
Semantic Analyzer
Int. Code Generator
Structure
of a Intermediate Code
Compiler
Back End
Target Language
Source Language
Lexical Analyzer
Syntax Analyzer Front
End
Semantic Analyzer
Structure Int. Code Generator
of a
Intermediate Code
Compiler
Code Optimizer Back
Target Code Generator End
Target Language
Lexical Analysis
In a compiler linear analysis is called lexical
analysis or scanning . for example in lexical
analysis the characters in the assignment
statement.
Position =initial+Rate*60
Lexical Analysis (cont’d)
Would be grouped into the following tokens.
1) The identification position
2) The assignment symbol:=
3) The identifier initial
4) The plus sign
5) The identifier rate
6) The multiplication sign
7) The number 60
Lexical Analysis (cont’d)
The blanks separating the characters of
these tokens would normally be eliminated
during lexical analysis.
Syntax Analysis
Hierarchical analysis is called parsing or
syntax analysis. It involves grouping the token
of the source program into grammatical
phrases that are used by the compiler to
synthesize the output. Usually the
grammatical phrases of the source program
are represented by a parse tree.
Semantic Analysis
The semantic analysis phase checks the
source programme for semantic errors and
gathers type information for the subsequent
code generation phase. it uses the
hierarchical structure determined by the
syntax analysis phase to identify the
operators and operands of expressions and
statements.
Semantic Analysis (cont’d)
An important component of semantic analysis
is type checking hence the compiler checks
that each operator has operands that are
permitted by the source language
specification. For example many
programming language definitions require a
compiler to report an error every time a real
number is used to index an array
Intermediate Code Generation
After syntax and semantic analysis some
compilers generate an explicit intermediate
representation of the source program this
intermediate representation should have two
important properties it should be easy to
produce and easy to translate into the target
program .
Intermediate Code Generation (cont’d)
We consider an intermediate form called Three
address code which is like the assembly
language for a machine in which every memory
location can act like a register. “These address
code consists of a sequence of instructions
each of which has at most three operands. The
source programme in three address code is:
Intermediate Code Generation (cont’d)
Temp1 = Into real (60)
Temp2 =Id3 * Temp1
Temp3 = Id2+ Temp2
Id1 = Temp3
Intermediate Code Generation (cont’d)
This intermediate form has several properties ;
Each three address instruction has at most one
operator in addition to the assignment thus when
generating these instructions the compiler has to
decide on the order in which operations are to be
done; the multiplication precedes the addition in
source programme
Intermediate Code Generation (cont’d)
The compiler must generate a temporary
name to hold the value computed by each
instruction.
Some “Three address” instruction have fewer
than three operands e.g. the first and last
instruction.
Code Optimization
The code optimization function phase attempts to
improve the intermediate code so that faster running
machine code will result.
Some optimizations are trivial (of very little
importance) for example a natural algorithm
generates the intermediate code using an instruction
for each operator in the tree representation after
semantic analysis even though there is a better way
to perform the same calculation using two
instructions
Code Optimization (cont’d)
Temp 1 = id3 *60.0
Id1 = id2 + temp 1
Code Generation
The final phase of the compiler is the
generation of target code constructing
normally of relocatable m/c code or
assembly code memory locations are
selected for each variables used by the
program. A crucial aspect is the assignment
of variables to registers.
Code Generation (cont’d)
For example using registers 1 and 2 the
translation of the code might become
MOVF id 3, R2
MULF # 60.0,R2
MOVF id2,R1
ADDF R2,R1
MOVF R1, ID 1
Code Generation (cont’d)
The first and the 2nd operands of each
instruction specify a source and destination
respectively . The F in each instruction tells
us that instruction deal with floating point
numbers.
Source Language Example Compilation
Lexical Analyzer Source Code:
Position = Initial + Rate * 60
Syntax Analyzer
Semantic Analyzer
Int. Code Generator
Intermediate Code
Code Optimizer
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer Source Code:
Position = Initial + Rate * 60
Int. Code Generator Lexical Analysis:
ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60)
Intermediate Code
Code Optimizer
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Source Code:
Semantic Analyzer Position = Initial + Rate * 60
Int. Code Generator Lexical Analysis:
ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60)
Intermediate Code Syntax Analysis:
ASSIGN
Code Optimizer ID(1) ADD
Target Code Generator ID(2) MULT
ID(3) INT(60)
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer Syntax Analysis:
ASSIGN
Semantic Analyzer
ID(1) ADD
Int. Code Generator ID(2) MULT
ID(3) INT(60)
Intermediate Code Sematic Analysis:
ASSIGN
Code Optimizer ID(1) ADD
Target Code Generator ID(2) MULT
ID(3) int2real
Target Language
INT(60)
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Sematic Analysis:
ASSIGN
Semantic Analyzer
ID(1) ADD
Int. Code Generator
ID(2) MULT
Intermediate Code ID(3) int2real
INT(60)
Code Optimizer
Intermediate Code:
Target Code Generator temp1 = int2real(60)
temp2 = id3 * temp1
temp3 = id2 + temp2
Target Language id1 = temp3
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Intermediate Code:
Semantic Analyzer temp1 = int2real(60)
temp2 = id3 * temp1
Int. Code Generator temp3 = id2 + temp2
id1 = temp3
Intermediate Code Optimized Code (step 0):
temp1 = int2real(60)
temp2 = id3 * temp1
Code Optimizer temp3 = id2 + temp2
id1 = temp3
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Intermediate Code:
Semantic Analyzer temp1 = int2real(60)
temp2 = id3 * temp1
Int. Code Generator temp3 = id2 + temp2
id1 = temp3
Intermediate Code Optimized Code (step 1):
temp1 = 60.0
temp2 = id3 * temp1
Code Optimizer temp3 = id2 + temp2
id1 = temp3
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Intermediate Code:
Semantic Analyzer temp1 = int2real(60)
temp2 = id3 * temp1
Int. Code Generator temp3 = id2 + temp2
id1 = temp3
Intermediate Code Optimized Code (step 2):
temp2 = id3 * 60.0
Code Optimizer temp3 = id2 + temp2
id1 = temp3
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer Intermediate Code:
temp1 = int2real(60)
temp2 = id3 * temp1
Int. Code Generator temp3 = id2 + temp2
id1 = temp3
Intermediate Code Optimized Code (step 3):
Code Optimizer temp2 = id3 * 60.0
id1 = id2 + temp2
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer Intermediate Code:
temp1 = int2real(60)
temp2 = id3 * temp1
Int. Code Generator temp3 = id2 + temp2
id1 = temp3
Intermediate Code Optimized Code:
Code Optimizer temp1 = id3 * 60.0
id1 = id2 + temp1
Target Code Generator
Target Language
Source Language Example Compilation
Lexical Analyzer
Syntax Analyzer
Intermediate Code:
Semantic Analyzer temp1 = int2real(60)
temp2 = id3 * temp1
temp3 = id2 + temp2
Int. Code Generator id1 = temp3
Optimized Code:
Intermediate Code
temp1 = id3 * 60.0
id1 = id2 + temp1
Code Optimizer
Target Code:
MOVF id3, R2
Target Code Generator MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
Target Language MOVF R1, id1
THANKS