7. Code Generation Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010. 2. D. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, 2000. 3. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986. (2 nd Ed. 2006) 1
120
Embed
7. Code Generation Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
7. Code Generation
Chih-Hung Wang
Compilers
References1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010.2. D. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, 2000.3. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986. (2nd Ed. 2006)1
2
Overview
3
InterpretationAn interpreter is a program that consider
the nodes of the AST in the correct order and performs the actions prescribed for those nodes by the semantics of the language.
Two varietiesRecursiveIterative
4
InterpretationRecursive interpretation
operates directly on the AST [attribute grammar]
simple to writethorough error checksvery slow: 1000x speed of compiled code
Iterative interpretationoperates on intermediate code
good error checkingslow: 100x speed of compiled code
5
Recursive Interpretation
6
Self-identifying datamust handle user-defined data typesvalue = pointer to type descriptor +
array of subvalues
example: complex number
re: 3.0
im: 4.0
7
Complex number representation
8
Iterative interpretation
Operates on threaded AST
Active node pointer
Flat loop over a case statement
IF
condition THEN ELSE
FI
9
Sketch of the main loop
10
Example for demo compiler
11
Code GenerationCompilation produces object code from the
intermediate code tree through a process called code generation
Tree rewritingReplace nodes and subtrees of the AST by
target code segmentsProduce a linear sequence of instructions
from the rewritten AST
12
Example of code generationa:=(b[4*c+d]*2)+9;
13
Machine instructionsLoad_Addr M[Ri], C, Rd
Loads the address of the Ri-th element of the array at M into Rd, where the size of the elements of M is C bytes
Load_Byte (M+Ro)[Ri], C, RdLoads the byte contents of the Ri-th element
of the array at M plus offset Ro into Rd, where the other parameters have the same meanings as above
14
Two sample instructions with their ASTs
15
Code generationMain issues:Code selection – which template?Register allocation – too few!Instruction ordering
Optimal code generation is NP-completeConsider small parts of the ASTSimplify target machineUse conventions
16
Object code sequence Load_Byte (b+Rd)[Rc], 4, RtLoad_Addr 9[Rt], 2, Ra
17
Trivial code generation
18
Code for (7*(1+5))
19
Partial evaluation
20
New Code
21
Simple code generationConsider one AST node at a time
Two simplistic target machinesPure register machinePure stack machine
BP
SP
stack
frame
vars
22
Pure stack machineInstructions
23
Example of p:=p+5Push_Local #pPush_Const 5Add_Top2Store_Local #p
24
Pure register machineInstructions
25
Example of p:=p+5Load_Mem p, R1Load_Const 5, R2Add_Reg R2, R1Store_Reg R1, p
26
Simple code generation for a stack machineThe AST for b*b – 4 *(a*c)
Heuristic ordering algorithmTo delay the issues of register allocation,
use pseudo-registers during the linearization
•Select ladder sequence S without more than one incoming dependencies
•Introduce temporary (pseudo-) registers for non-leaf operands, which become additional roots
•Generate code for S, using R1 as the ladder register
•Remove S from the graph
•Repeat step 1 through 4 until the entire data dependency graph has been consumed and rewritten to code
69
Example of linearization
X1
70
The code for y, *, +Load_Reg X1, R1Add_Const 1, R1Multi_Mem d, R1Store_Reg R1, y
71
Remove the ladder sequence y, *, +
72
The code for x, +, +, *Load_Reg X1, R1Mult_Reg X1, R1Add_Mem b, R1Add_Mem c, R1Store_Reg R1, x
73
The Last stepLoad_Mem a, R1Add_Const 1, R1Load_Reg R1, X1
74
The results of code generation
75
Exercise
Generate code for the following dependency graph
*
2
*
+
+
x
-
+
y
*
a
*
b
76
Answers
*
2
*
+
+
x
-
+
y
*
a
*
b
Load_Reg R2, R1
Add_Reg R3, R1
Add_Reg, R4, R1
Store_Mem R1, x
1) ladder: x, +, +
Load_Reg R2, R1
Sub_Reg R3, R1
Add_Reg, R4, R1
Store_Mem R1, y
2) ladder: y, +, -
R2R3
R4
Load_Const 2, R1
Mul_Reg Ra, R1
Mul_Reg, Rb, R1
Load_Reg R1, R3
3) ladder: R3, *, *
Load_Reg Ra, R1
Mul_Reg Ra, R1
Load_Reg R1, R2
4) ladder: R2, *
Load_Reg Rb, R1
Mul_Reg Rb, R1
Load_Reg R1, R4
5) ladder: R4, *
77
Register allocation for the linearized codeMap the pseudo-registers to memory
locations or real registers
gcc compiler
78
Code optimization in the presence of pointersPointers cause two different problems
for the dependency grapha=x * y;*p = 3;b = x * y;
a=*p * y;b = 3;c = *p * q;
x * y is not a common subexpression if p happens to point to x or y
*p * q is not a common subexpression if p happens to point to b
79
Example (1/4)Assignment under a pointer
80
Example (2/4)
Data dependency graph with an assignment under a pointer
81
Example (3/4)
Cleaned-up graph
82
Example (4/4)
Target code
*x:=R1
83
BURS code generationIn practice, machines often have a great
variety of instructions, simple ones and complicated ones, and better code can be generated if all available instructions are utilized.
Machines often have several hundred different machine instructions, often each with ten or more addressing modes, and it would be very advantages if code generators for such machines could be derived from a concise machine description rather than written by hand.
Code generation using cost-conscious next-state table
103
Register allocation by graph coloringProcedure-wide register allocationOnly live variables require register storage
Two variables(values) interfere when their live ranges overlap
dataflow analysis: a variable is live at node N if the value it holds is used on some path further down the control-flow graph; otherwise it is dead
104
A program segment for live analysis
105
Live range of the variables
106
Graph coloringNP complete problem
Heuristic: color easy nodes lastFind node N with lowest degreeRemove N from the graphColor the simplified graph Set color of N to the first color that is
not used by any of N’s neighbors
107
Coloring process
3 registers
108
Preprocessing the intermediate codePreprocessing of expressions