Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | pascale-prince |
View: | 34 times |
Download: | 0 times |
Code Generation
Mooly Sagiv
html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html
Chapter 4.2
Basic Compiler Phases
Code generation issues
• Code selection
• Register allocation
• Instruction ordering
Simplifications
• Consider small parts of AST at time– One expression at the time
• Target machine simplifications– Ignore certain instructions
• Use simplifying conventions
Overall Structure
Outline
• Partial evaluation in the nutshell• Simple code generation for expressions (4.2.4)
– Pure stack machine
– Pure register machine
• Code generation of basic blocks (4.2.5)• Automatic generation of code generators (4.2.6)• Register Allocation by Graph Coloring (4.2.7)
Next w
eek
Partial Evaluation
• Partially interpret static parts in a program
• Generates an equivalent program
Partial EvaluatorProgram Program’
Input 1Input 2
Example
int pow(int n, int e)
{
if (e==0)
return 1;
else return n * pow(n, e-1);
}
e=4
int pow4(int n)
{
return n * n * n *n;
}
Example2
Bool match(string, regexp)
{
switch(regexp) {
….
}
}
regexp=a b*
Partial Evaluation Generalizes Compilation
Partial EvaluatorInterpreter Program
AST ProgramInput
But ….
Simple Code Generation
• Fixed translation for each node type
• Translates one expression at the time
• Local decisions only
• Works well for simple machine model– Stack machines (PDP 11, VAX)– Register machines (IBM 360/370)
• Can be applied to modern machines
Simple Stack Machine
BP
SP
Stack
Stack Machine Instructions
Example
p := p + 5Push_Local #p
Push_Const 5
Add_Top2
Store_Local #p
Simple Stack Machine
BP
SP
Push_Local #p
Push_Const 5
Add_Top2
Store_Local #p7 BP+5
Simple Stack Machine
BP
SPPush_Local #p
Push_Const 5
Add_Top2
Store_Local #p
7
BP+57
Simple Stack Machine
BP
SPPush_Local #p
Push_Const 5
Add_Top2
Store_Local #p
7
BP+57
5
Simple Stack Machine
BP
SP Push_Local #p
Push_Const 5
Add_Top2
Store_Local #p
12
BP+57
Simple Stack Machine
BP
SP
Push_Local #p
Push_Const 5
Add_Top2
Store_Local #pBP+512
Register Machine
• Fixed set of registers
• Load and store from/to memory
• Arithmetic operations on register only
Register Machine Instructions
Example
p := p + 5Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P
Simple Register Machine
Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P7 x770
memory
R1 R2
Simple Register Machine
Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P7 x770
memory
7
R1 R2
Simple Register Machine
Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P7 x770
memory
57
R1 R2
Simple Register Machine
Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P7 x770
memory
512
R1 R2
Simple Register Machine
Load_Mem p, R1
Load_Const 5, R2
Add_Reg R2, R1
Store_Reg R1, P12 x770
memory
512
R1 R2
Simple Code Generation for Stack Machine
• Tree rewritings
• Bottom up AST traversal
Abstract Syntax Trees for Stack Machine Instructions
Example
-
*
*
b b 4 *
a c
Push_Local #b Push_Local #b
Mult_Top2
Push_Constant 4
Push_Local #a Push_Local #c
Mult_Top2
Mult_Top2
Subt_Top2
Bottom-Up Code Generation