Professional Documents
Culture Documents
Produces output:
semantically equivalent target program
The back-end code generator of a compiler may generate different forms of code, depending on the requirements:
Absolute machine code (executable code) Relocatable machine code (object files for linker) Assembly language (facilitates debugging) Byte code forms for interpreters (e.g.
Instruction selection
choose appropriate target-machine instructions to implement the IR statements
Instruction ordering
decide in what order to schedule the execution of instructions
Design of all code generators involve the above three tasks Details of code generation are dependent on the specifics of IR, target language, and run-time system
Byte-addressable (word = 4 bytes) Has n general purpose registers R0, R1, , Rn1 Two-address instructions of the form op source, destination Op op-code Source, destination data fields
6
contents(c+contents(R))
N/A
1
1
Instruction Costs
Machine is a simple processor with fixed instruction costs Define the cost of instruction = 1 + cost(source-mode) + cost(destinationmode)
Examples
Instruction Operation MOV R0,R1 Store content(R0) into register R1 MOV R0,M Store content(R0) into memory location M MOV M,R0 Store content(M) into register R0 MOV 4(R0),M Store contents(4+contents(R0)) into M MOV *4(R0),M Store contents(contents(4+contents(R0))) into M MOV #1,R0 Store 1 into R0 ADD 4(R0),*12(R1) Add contents(4+contents(R0)) to value at location contents(12+contents(R1)) Cost 1 2 2 3 3 2
3
10
Instruction Selection
Instruction selection is important to obtain efficient code Suppose we translate three-address code
x:= y + z
to: MOV
a:=a+1
Better
Best
INC a Cost = 2
11
MOV *R1,*R0 ADD *R2,*R0 Assuming R1 and R2 contain values of b and c ADD R2,R1 MOV R1,a
12
to: MOV
Then, we translate
a:=b+c d:=a+e
to: MOV
ADD MOV MOV ADD MOV
Redundant
13
Register allocation to select the set of variables that will reside in registers at a point in the code Register assignment to pick the specific register that a variable will reside in
14
Example
t:=a*b t:=t+a t:=t/d
{ R1=t } MOV MUL ADD DIV MOV a,R1 b,R1 a,R1 d,R1 R1,t MOV MOV MUL ADD DIV MOV
a+b-(c+d)*e
reorder
t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3
16
Registers
Fast access, but only a few of them Address space not visible to programmer
Doesnt support pointer access!
Memory
Slow access, but large Supports pointers
Static (or Global) Variables that retain their value over the lifetime of the program Stack Variables that is only as long as the block within which they are defined (local) Heap Variables that are defined by calls to the system storage allocator
Memory Organization
Code
Static Data Stack Code and static data sizes determined by the compiler Stack and heap sizes vary at run-time Stack grows downward Heap grows upward
...
Heap
Class Problem
Specify whether each variable is stored in register or memory. For memory, which area of the memory? int a; float j; void foo(int b, double c) { int d; int h[10]; register char i = 5; int *m = new int[10]; }