Upload
hacong
View
216
Download
0
Embed Size (px)
Citation preview
Code Generation
Frédéric Haziza <[email protected]>
Department of Computer Systems
Uppsala University
Spring 2008
Operating SystemsProcess Management
Memory Management
Storage Management
CompilersCompiling process &Lexical analysis
Parsing
Semantic &Code generation
Lexicalanalysis
Syntaxanalysis
Semanticsanalysis
Analysis
Machineindepen-dant codegeneration
Optimizationof machine
inde-pendant
code
StorageAllocation
Machinecode
generation
Optimizationof machine
code
Synthesis
Intermediate code Machine code Optimizations
Outline
1 Intermediate code
2 Machine code
3 Optimizations
4 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Good IR
easy to translate from AST
easy to translate to assembly/machine code
easy to optimize
easy to retarget
6 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Well-known examples
1 Three-address code
2 P-code (for Pascal)
3 Bytecode (for Java)
7 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
P-Code
Stack-based intermediate code, for Pascal.
Instruction format: F P Q
F is a function
P, Q may be absent
P used to specify a static block level
Q: offset within a frame or immediate operand (ie constant)
Compile-time address are (static level, offset)
8 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Instructions
Instructions with no parameter operate on the stack’s top• AND, DIF, NGI, FLT, FLO, INN
One or two-address instruction to load/store a value on thetop of the stack
• LDCI, LODI, LDA, STRI
Jump instructions:• UJP L7: unconditional jump• FJP L8: jump to L8 if top of the stack is false
Labels
9 OSKomp’08 | Code Generation
if (expression) statement1 else statement2
Code to put the value of expression on top of the stack
FJP L1
Code to implement statement1UJP L2
L1
Code to implement statement2L2
while (expression) statement
L1
Code to put the value of expression on top of the stack
FJP L2
Code to implement statement
UJP L1
L2
Intermediate code Machine code Optimizations
Java Run-Time system
Execution engine (executes bytecode instructions)
Memory manager (manages heap in which all objects and arrays are stored)
Error and Exception manager (used to catch runtime failures in a planned and
systematic manner)
Threads interface (handles concurrency)
Class Loader (loads, links and initializes classes)
Security manager deals with attempts to run ’hostile’ programs
11 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Principal types of bytecode instructions
Stack manipulation
Performing arithmetic
Handling objects and arrays
Control flow
Method invocation
Handling exceptions and concurrency
12 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Manipulating the stack
Instructions Meanings
iconst_4 load the integer constant 4 on to the stack
iload_4 load the value of local variable number 4 on to the stack
pop discard top value of the stack
dup duplicate top item on the stack
swap interchange top two values of the stack
istore_4 store the value on top of the stack in the local variable number 4
13 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Arithmetic and Array
Instructions Meanings
iadd add the two integers on the top of the stack
fadd add the two floats on the top of the stack
fmul multiply the two floats on the top of the stack
Instructions Meanings
iaload puts the value of an array element on top of the stack,
assuming the array reference and the index of the array are already on the stack
14 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Conditional and unconditional Branches
Instructions Meanings
ifeq L1 jump to L1 if the integer value on top of the stack is zero
if_icmpne L1 jump to L1 if two integer values on top of the stack are not equal
goto L1 jump to L1
15 OSKomp’08 | Code Generation
if (expression) statement1 else statement2
Bytecode to put the value of expression on top of the stack
ifeq L1
Bytecode to implement statement1goto L2
L1
Bytecode to implement statement2L2
while (expression) statement
L1
Bytecode to put the value of expression on top of the stack
ifeq L2
Bytecode to implement statement
goto L1
L2
Intermediate code Machine code Optimizations
Considerations
CISC complex instruction set computer
* Wide range of addressing mode* Small number of registers (<16)* Many special purpose registers* 2-address instructions A + B → A* Variable length instructions* Instructions with side-effect
* Different exec time for instr
RISC reduced instruction set computer
* Simple addressing mode (withregisters)* Many registers (>32)* All registers are ’general purpose’* 3-address instructions r3 = r1 + r2
* Fixed length instructions (32 bits)* No side-effect, one result per instr
* Similar exec time for instr
Instruction selectionRegister allocation
18 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Example
t1 = a + bt2 = c + dt3 = t1 ∗ t2
Must keep t1 and t2 until t3 is evaluated
19 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Example – 2
a ∗ b + c ∗ d + e ∗ f
t1 = a ∗ bt2 = c ∗ dt3 = t1 + t2t4 = e ∗ ft5 = t3 + t4
Temporaries Register
t1 1t2 2t3 3t4 1t5 2
Do we really need distinct registers?
20 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
1) n = 02) sum2 = 03) sum3 = 04) t1 = n < 10 : L1
5) t2 = not t16) if t2 goto L2
7) n = n + 18) m = n ∗ n9) sum2 = sum2 + m10) t3 = m ∗ n11) sum3 = sum3 + t312) goto L1
13) L2
n = 0;sum2 = 0;sum3 = 0;while (n<10){n = n+1;m = n*n;sum2 = sum2 + m;sum3 = sum3 + m*n;
}
Variable Live Registern 1..12 1sum2 2..12 2sum3 3..12 3t1 4..5 4m 8..10 4t2 5..6 4t3 10..11 4
21 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Liveness analysis
Equations of the form:
1 inn = usen ∪ (outn\defn)
2 outn =⋃
s∈succ(n) ins
where
usen: set of all variables whose values are used in statement noutn: set of all variables that are live on leaving statement ndefn: set of all variables that are defined in statement n
inn: set of all variables that are live on reaching statement n
22 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Typical local optimizations
Constant folding
Strength reduction
Elimination of unnecessary instructions
24 OSKomp’08 | Code Generation
Intermediate code Machine code Optimizations
Typical global optimizations
Analysis of control and data flow:
Dead code elimination
Common subexpression elimination
Loop optimizations
25 OSKomp’08 | Code Generation