5.2 Design of a simple Code Generator
5.2 Design of a simple Code Generator
Agenda
➢ Next Use
➢ Register and Address Descriptors
➢ Code Generator Algorithm
➢ Code sequences for pointer assignments
➢ Conditional Statements
Course Outcomes:
CO # Outcome K Level
Design and analyze Finite Automata and regular expression for any given Regular
CO1 K3
language
Understand and apply concepts of Context-Free Grammars, Push-Down Automata,
CO2 K3
and Turing machines to analyze computational problems and language ambiguities.
Demonstrate proficiency in Lexical Analysis, including defining tokens, recognizing
CO3 K2
tokens, and utilizing tools like Lex
Design efficient code generators and optimize code using techniques like peep-hole
CO5 K2
optimization, DAGs, and basic block optimization.
Next-Use
A three-address statement x:=y+z is said to define
x and to use y and z.
A name in a basic block is said to be live at a
given point if its value is used after that point in
the program.
Use of a name:
ifstatement i assigns a value to x ;
stmt j has x as an operand and control can flow from i
to j along a path with no intervening assignments to
we can say that j uses the value of x computed at i.
Next-Use
Next-use information is needed for dead-code
elimination and register assignment
Next-use is computed by a backward scan of a
basic block and performing the following actions on
statement
i: x := y op z
Add liveness/next-use info on x, y, and z to statement i
Set x to “not live” and “no next use”
Set y and z to “live” and the next uses of y and z to i
Design of a simple Code Generator
7 5 October 2024
Next-Use (Step 1)
i: a := b + c
Next-Use (Step 2)
Next-Use (Step 3)
Next-Use (Step 4)
live(a) = false nextuse(a) = none
live(b) = true nextuse(b) = i
live(c) = true nextuse(c) = i
live(t) = false nextuse(t) = none
i: a := b + c [ live(a) = true, live(b) = true, live(c) = false,
nextuse(a) = j, nextuse(b) = j, nextuse(c) = none ]
A Code Generator
Generates target code for a sequence of three-
address statements using next-use information
Uses new function getreg to assign registers to
variables
Computed results are kept in registers as long as
possible, which means until:
Result is needed in another computation
End of procedure call or end of block
Checks if operands to three-address code are
available in registers
Conditional Statements
Branch if value of register meets the required condition
Condition codes
if x < y goto z
CMP x, y
CJ< z
Example 1
x := y +z if x < 0 goto z
MOV y, R0
ADD z, R0
MOV R0, x
CJ< z
Example 2
References
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman,
“Compilers: Principles, Techniques and Tools”, Second Edition,
Pearson Education, 2009.
Steven S. Muchnick, “Advanced Compiler Design and
Implementation”, Morgan Kaufmann Publishers - Elsevier Science,
India, Indian Reprint 2003.
V. Raghavan, “Principles of Compiler Design”, Tata McGraw Hill
Education Publishers, 2010.
Allen I. Holub, “Compiler Design in C”, Prentice-Hall Software
Series, 1993.
Randy Allen, Ken Kennedy, “Optimizing Compilers for Modern
Architectures: A Dependence based Approach”, Morgan
Kaufmann Publishers, 2002.
Design of a simple Code Generator
25 5 October 2024