Escolar Documentos
Profissional Documentos
Cultura Documentos
3 April 2015
Phases of Compiler
Input Source Program
Lexical Analyzer
Syntax Analyzer
Symbol Table
Manager
Semantic Analyzer
Code Optimizer
Code Generator
Target Program
Error Handler
Code Generation
Input : Intermediate language program
such as sequence of quadruples or triples
and so on
Output: Object program such as machine
language form, assembly language
program or some other.
3 April 2015
the target machine has the instruction add one tostorage(AOS) then the statement A:=A+1 should
generate
AOS A
Rather than
LOAD A
ADD #1
STORE A
3 April 2015
3 April 2015
Machine Model
3 April 2015
source, destination
Machine Model
Addressing mode
r (register mode): register contains the operand
*r (indirect register): r contains the address of the
operand
X(r) (indexed mode): Value X is added to the contents
of r to produce the address of the operand.
*X(r) (indirect indexed mode) : Value X is added to
the contents of r to produce the address of the word
containing the address of the operand.
#X (immediate): The word following the instruction
contains the literal operand X.
3 April 2015
Machine Model
Op-Codes
3 April 2015
Machine Model
3 April 2015
Machine Model
MOV B, R0
ADD C, R0
MOV R0, A
Cost=6
MOV B, A
ADD C, A
Cost=6
Cost=2
10
3 April 2015
11
performed
Consult the address descriptor to get the location. If
B is not in reg generate MOV to copy in reg.
Generate the instruction OP C, L, C is the location,
update address descriptor, if L is reg. update register
descriptor
If B or C has no next use alter the register
descriptor.
3 April 2015
12
Statement
Code
generated
Register
descriptor
Address
descriptor
T: = A-B
MOV A,R0
SUB B,R0
R0 contains T T in R0
U: = A-C
MOV A,R1
SUB C, R1
R0 contains T T in R0
R1 contains U U in R1
V: = T+U
Add R1, R0
R0 contains V U in R1
R1 contains U V in R0
W: = V+U
ADD R1, R0
MOV R0, W
R0 contains
W
W in R0
13
3 April 2015
14
Conditional Statement
15
Register Allocation
16
3 April 2015
17
Usage count
Counts the saving for register allocation
3 April 2015
18
Usage count
bcdf
a:= b+c
d:= d-b
e:=a+f
acde
f:=a-d
B1
B2
cdef
b:= d+e
bcdef
Usage value for x=a is 4
acdf
b:= d+f
e:= a-e
B4
For x=a
a is live on exit from
B1and assigned a value
there but is not live on exit
from B2, B3 and B4
B3
For x=a
LIVE(a,B1)=2
USE(a,B1)=0, since a is
defined in B1 before any use.
USE(a,B2)=1, USE(a,B3)=1,
USE(a,B4)=0,
Thus 4 unit of costs can be saved by selecting a for one of the global registers
19
Usage count
Live
Use
B1 B2 B3 B4
B1 B2 B3 B4
1 0
0 0
1 0
1 0
3 April 2015
Total
20
MOV B, R1
MOV D, R2
Usage count
MOV R1, R0
ADD C, R0
SUB R1, R2
MOV R0, R3
ADD F, R3
MOV R3,E
MOV R0,R3
SUB R2, R3
MOV R3, F
MOV R2, R1
ADD F, R1
MOV R0,R3
SUB C, R3
MOV R3, E
B2
MOV R2, R1
ADD C, R1
3 April 2015
B1
MOV B, R1
MOV D, R2
B3
B4
MOV B, R1
MOV D, R2
21
Rearranging
T1:=A+B
T2:=C+D
T3=E-T2
T4:=T1-T3
MOV A, R0
ADD B, R0
MOV C, R1
ADD D, R1
MOV R0, T1
MOV E, R0
SUB R1, R0
MOV T1, R1
SUB R0, R1
MOV R1, T4
3 April 2015
22
Rearranging
Rearrange
T2:=C+D
T3=E-T2
T1:=A+B
T4:=T1-T3
MOV C, R0
ADD D, R0
MOV E, R1
SUB R0, R1
MOV A, R0
ADD B, R0
SUB R1, R0
MOV R0, T4
3 April 2015
23
THANK YOU
3 April 2015
24