



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A chapter from the cs5810 spring 2008 course notes on code generation. It covers the basics of code generation, including the use of registers, register and address descriptors, and a code-generation algorithm. It also discusses managing register and address descriptors, an example of code generation, and peephole optimization.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Chapter 8 II
Chapter 8: Code Generation CS5810 Spring 2008 1
A Simple Code Generator
Chapter 8: Code Generation CS5810 Spring 2008 2
Register and Address Descriptors
Chapter 8: Code Generation CS5810 Spring 2008 3
Code‐generation Algorithm
Chapter 8: Code Generation CS5810 Spring 2008 5
Example t = a – b u = a – c v = t + u a = d d
Chapter 8: Code Generation CS5810 Spring 2008 6
d = v + u
Example
t = a ‐ b LD R1, a; LD R2, b; SUB R2, R1, R
a b c d
R1 R2 R3 a b c d t u v
R1 R2 R3 a b c d t u v
Chapter 8: Code Generation CS5810 Spring 2008 7
a t a,R1 b c d R
R1 R2 R3 a b c d t u v
u = a ‐ c LD R3, c; SUB R1, R1, R
u t c a b c,R3 d R2 R
R1 R2 R3 a b c d t u v
v = t + u ADD R3, R2, R
u t v a b c d R2 R1 R
R1 R2 R3 a b c d t u v
Chapter 8: Code Generation CS5810 Spring 2008 8
a = d LD R2, d
u a,d v R2 b c d,R2 R1 R
R1 R2 R3 a b c d t u v
d = v + u ADD R1, R3, R
d a v R2 b c R1 R
R1 R2 R3 a b c d t u v
Chapter 8: Code Generation CS5810 Spring 2008 9
ST a, R2; ST d, R
d a v a,R2 b c d,R1 R
R1 R2 R3 a b c d t u v
Function getReg
Chapter 8: Code Generation CS5810 Spring 2008 10
Peephole Optimization
Chapter 8: Code Generation CS5810 Spring 2008 11
LD a, R ST R0, a
Chapter 8: Code Generation CS5810 Spring 2008 12
Chapter 8: Code Generation CS5810 Spring 2008 19
Ershov Numbers
Chapter 8: Code Generation CS5810 Spring 2008 20
Ershov Numbers
t
t
b e
t
t
a
Chapter 8: Code Generation CS5810 Spring 2008 21
t1 = a – b t2 = c + d t3 = e * t t4 = t1 + t
c d
(a‐b)+e*(c+d)
Chapter 8: Code Generation CS5810 Spring 2008 22
Chapter 8: Code Generation CS5810 Spring 2008 23
Ershov Numbers
t
t b e
t
t
a
Chapter 8: Code Generation CS5810 Spring 2008 24
LD R3, d LD R2, c ADD R3, R2, R LD R2, e MUL R3, R2, R
c d
LD R2, b LD R1, a SUB R2, R1, R ADD R3, R2, R
Insufficient Supply of Registers
Chapter 8: Code Generation CS5810 Spring 2008 25
Insufficient Supply of Registers
t
t
b e
t
t
a
Chapter 8: Code Generation CS5810 Spring 2008 26
LD R2, d LD R1, c ADD R2, R1, R LD R1, e MUL R2, R1, R ST t3, R
c d LD R2, b LD R1, a SUB R2, R1, R LD R1, t ADD R2, R2, R