Title: Code Generation
1Code Generation
Token Stream
Parser
- Basics of code generation
- Intermediate code to object code
- The simple machine language from the textbook
Intermediate Code
Optimization
Object Code
Chapter 8
2Code Generator
Goal is to convert the intermediate code, with
the help of the symbol table, into object
code. Well convert to something like assembly
language. Well skip optimization for now, since
we can live without it!
Intermediate Code
Optimization
Object Code
Generated code after the optimizer is just like
generated it before.
3What we have to do
Convert intermediate instructions into
assembly/machine language instructions. Convert
all of those temporary variables into registers
or local memory locations. Create everything we
need to do to set up for procedures correctly.
t1 116 t2
ld r9, 116 ld r10, 24(SP) mul r11, r9, r10
Pretty much all machine specific
4Example
Heap 0 stack pointer 1 2 2 3 Code 0
a addi 0 3 t1 1 ()
t1 0 t1 2 t1
1 t2 3 addi 0 2
t3 4 () t2 0
t3 5 ret 0 0 0 6
main param 2 0 0 7
subi 0 1 0 8 call
0 0 0 9 addi 0
1 0 10 () 0 0
t1 11 addi 0 1 0 12
addi 0 2 t2 13
() t1 0 t2 14 ret
0 0 0
a x x 2 main a 3
5Example
a x x 2 main a 3
Heap 12 2 14 3 Code 0 a addi 0
3 t1 1 () t1 0
t1 2 t1 1 t2 3
addi 0 2 t3 4
() t2 0 t3 5 ret
0 0 0 6 main param 2
0 0 7 subi 0 1
0 8 call 0 0
0 9 addi 0 1 0 10
() 0 0 t1 11
addi 0 1 0 12 addi
0 2 t2 13 () t1
0 t2 14 ret 0 0
0
a SUB SP, SP, 4 ST 0(SP), R0 LD R0,
12(SP) MUL R0, R0, 12 ST 8(SP), R0 LD R0,
0(SP) ADD SP, SP, 4 BR 0(SP)
6The textbook assembly language
N general purpose registers R0-Rn-1. Well
assume 16, R0 R15 SP Stack pointer Load
operations LD reg, addr - destination is a
register Store operations ST addr, reg- source
is a register Computation OP reg, src, src
destination is a register ADD, SUB, MUL, DIV,
NEG, etc. Unconditional jump BR L Branch to
location L Conditional jumps B??? reg, L
Branch on condition BLTZ, BGTZ, BLEZ, BGEZ, BEQZ
all compare to zero
Note No call or ret instructions
7Addressing modes
- 12, x l-value address. The address is a
location in memory - a(r) a is a memory location, r is a register.
resultcontents(a contents(r)) - 100(r) r is a register. resultcontent(100
contents(r)) - 100(r) r is a register. resultcontent(content
(100 content(r))) - 12 Immediate value of 12
Examples add r1, r1, x Adds contents of
location x to r1, result to r1 ld r2, y(r3)
Loads contents of location x r3, result to
r2 ld r4, 16(sp) Loads content a location sp
16 ld r4, 100(sp) Content at location sp 16
is used as an address we load from st 20(r3),
r9 br 0(SP)
8What goes where
Stack Locals, return values, return address,
parameters, saved registers Heap Global
variables, constants
x
SP 32
Heap is accessed by address LD R12, b Stack is
relative to SP LD R13, 28(SP)
y
SP 28
Return value
SP 24
Return address
SP 20
t1
SP 16
t2
SP 12
t3
SP 8
r1
SP 4
Stack pointer
r2
9Collecting it up
TT
10Our intermediate code
11Some modifications
f0-f15 are floating point registers for
doubles addf, subf, divf, mulf, negf floating
point instructions r0-15 float,
f0-15double Additional instructions to make
some simple stuff work...
12Some specific examples
t1 116 t2
mul r11, 116, 24(sp)
13Procedure calls
x
SP 32
y
SP 28
Assume SP is pointing to the first local value
for the current procedure. Example 2
parameters X, Y, return address, three registers
to save, 2 local variables.
Return value
SP 24
Return address
SP 20
t1
SP 16
t2
SP 12
t3
SP 8
r1
SP 4
Stack pointer
r2
14Setting up for a call
x
y
Decrement SP by what we need to put on the stack
for the call. If no parameters, this would be 8
bytes.
Return value
Return address
t1
t2
t3
r1
sub sp, sp, 8 st 0(sp), here24 br
procedure add sp, sp, 8
Stack pointer
r2
return value
ret
New stack pointer
8 bytes (2 words)
15This may seem strange...
1024 sub(i) 1028 sp 1032 sp 1036 8 1040 st 1044 0
1048 sp 1052 1040 24 1064 1056 br 1060 procedu
re address 1064 add(i) 1068 sp 1072 sp 1076 8
sub sp, sp, 8 st 0(sp), here24 br
procedure add sp, sp, 8
16Setting up for a call
x
y
Decrement SP by what we need to put on the stack
for the call. If no parameters, this would be 8
bytes.
Return value
Return address
t1
t2
t3
r1
sub sp, sp, 8 st 0(sp), here24 br
procedure add sp, sp, 8
Stack pointer
r2
return value
ret
New stack pointer
8 bytes (2 words)
TT
17Procedures
x
Make room for local activation frame
y
sub sp, sp, 20 st 4(sp), r1 st 0(sp), r2 ... do
some work st r1, 24(sp) ld r2, 0(sp) ld r1,
4(sp) add sp, sp,20 br 0(sp)
Save registers
Return value
Return address
Stack pointer
t1
t2
Save return value
t3
Restore registers
r1
Restore stack pointer
r2
New stack pointer
Return!!!
TT
18Tasks
- For each procedure create the start and end code.
- Allocate registers to use for temporaries.
- Assign registers to temporaries.
- Allocate stack space for temporaries that dont
fit in registers. - Set up procedure calls.
- Convert intermediate code to least costly target
code.
Any issues relative to the intermediate code that
we should consider?
19Costs
It is useful to assign a cost to operations so we
can gauge our performance. Were not optimizing
yet, but want to choose the least costly option
when we have a choice. Well use the model
that an instruction costs 1 the number of
memory accesses (not register accesses).
20What we would like to do...
- Order our code so it makes the most efficient use
of registers. - Optimally allocate registers to the temporaries
so we have a minimum of temporaries in memory. - Choose instructions that have the minimum cost of
execution.
Isnt this nice every last one of these is
NP-hard!
21Instrumentation youll want to add to your three
address code.
- Where procedures begin (already in the symbol
table) - What temporaries are needed for each procedure
(we dont keep track of that right now, add to
symbol table) - When we are allocating parameters and return
addresses (some notation added to the code)
22Textbook Fun
LD, ST (pg 512) No constaints on arguments, but
says LD reg, mem and LD reg, reg are common. If
no constraints, what is the difference between LD
and ST? (pg 514) LD R2, 0(R) ST x, R2 (pg
523) ST 0(SP), 344 (pg 543) LD reg, mem ST
mem, reg, OP reg, reg, reg
23Register allocation
- The register allocation problem
- Rewrite the code to minimize compiler temporaries
- Assign the remaining temporaries to registers or
local variables
24Reducing Temporaries
- Re-use a temporary variable we would otherwise be
finished with. - gt Have to be able to determine we are finished
with it. - Directly use constants when possible rather than
saving as temporaries. - Use an interpreter to perform any math on
constants at compile time.
25Example
2 4 t1 t1 2 - 4 t1 5 t2 t2 t1
5 - t2 1 t3 t3 t2 1 (assume t1, t2 not
used in any later code)
t1 can be reused after t1 5 t2. t2 can be
reused after t2 1 t3 We can use t1 for t2.
We can use t2 for t3.
2 4 t1 t1 2 - 4 t1 5 t1 t1 t1
5 - t1 1 t1 t1 t1 1
26Basic Idea!
A temporary is dead at some point in the program
if it is not used later in the program. A
register or local used to hold a value can be
reused once the value becomes dead. t1 and t2
can use the same register or local if at any
point in the program, at most t1 or t2 is
live. A temporary value can be artificially
killed by spilling it to another value.
Mostly we are concerned with registers here
27Control flow graph
Nodes 1 or more 3 address instructions
a b c d -a e d f if e gt 3 goto B1
B0
b d e e e 1 goto B4
B2
f 2 e
B1
b f c if b gt 3 goto B0
B3
Edges branches from one instruction to another
28Computing liveness
Suppose b is utilized after this graph Example
Its the return value of the function.
a b c d -a e d f if e gt 3 goto B1
B0
b d e e e 1 goto B4
B2
f 2 e
B1
A value v is live going out of a node if there is
some path starting at the successor of the node
where v is used before it is redefined.
b f c if b gt 3 goto B0
B3
b must be live here
b
29Computing liveness
Suppose b is utilized after this graph Example
Its the return value of the function.
a b c d -a e d f if e gt 3 goto B1
B0
A value v is live going out of a node if there is
some path starting at the successor of the node
where v is used before it is redefined.
b d e e e 1 goto B4
B2
f 2 e
B1
a? b? c? d? e? f?
A value v is live going into a node if there is
some path starting at the node where v is used
before it is redefined.
b f c if b gt 3 goto B0
B3
b
30Computing liveness
Suppose b is utilized after this graph Example
Its the return value of the function.
a b c d -a e d f if e gt 3 goto B1
B0
ii
i
A value v is live going out of a node if there is
some path starting at the successor of the node
where v is used before it is redefined.
b d e e e 1 goto B4
B2
f 2 e
B1
c, f
c, f
A value v is live going into a node if there is
some path starting at the node where v is used
before it is redefined.
b f c if b gt 3 goto B0
B3
b
TT
31Computing liveness
Suppose b is utilized after this graph Example
Its the return value of the function.
a b c d -a e d f if e gt 3 goto B1
B0
A value v is live going out of a node if there is
some path starting at the successor of the node
where v is used before it is redefined.
c, d, e, f
c,e
b d e e e 1 goto B4
B2
f 2 e
B1
c, f
c, f
A value v is live going into a node if there is
some path starting at the node where v is used
before it is redefined.
b f c if b gt 3 goto B0
B3
b
32Computing liveness
Suppose b is utilized after this graph Example
Its the return value of the function.
b, c, f
a b c d -a e d f if e gt 3 goto B1
B0
A value v is live going out of a node if there is
some path starting at the successor of the node
where v is used before it is redefined.
c, d, e, f
c,e
b d e e e 1 goto B4
B2
f 2 e
B1
c, f
c, f
A value v is live going into a node if there is
some path starting at the node where v is used
before it is redefined.
b f c if b gt 3 goto B0
B3
b
33Computing liveness
We determine live between instructions as well.
b, c, f
a b c d -a e d f if e gt 3 goto B1
B0
a, c, f
c, d, f
c, d, e, f
c, d, e, f
c,e
b d e e e 1 goto B4
B2
c, e, f
f 2 e
B1
c, f
c, f
c, f
b f c if b gt 3 goto B0
B3
b
34Register Interference Graph
Two temporaries that are live simultaneously
cannot be allocated to the same register!
- Construct an undirected graph
- A node for each temporary
- An edge between any two that are live
simultaneously at any point in the program
Register Interference Graph (RIG)
Two temporaries can be allocated to the same
register if no edge connects them.
35Computing liveness
a
b, c, f
b
f
a b c d -a e d f if e gt 3 goto B1
B0
a, c, f
c, d, f
c
e
c, d, e, f
c, d, e, f
d
c,e
b d e e e 1 goto B4
B2
c, e, f
f 2 e
B1
c, f
c, f
c, f
b f c if b gt 3 goto B0
B3
b
TT
36Computing liveness
a
b, c, f
b
f
a b c d -a e d f if e gt 3 goto B1
B0
a, c, f
c, d, f
c
e
c, d, e, f
c, d, e, f
d
c,e
b d e e e 1 goto B4
B2
c, e, f
f 2 e
B1
c, f
c, f
b and c cannot be in the same register b and d
can be in the same register
c, f
b f c if b gt 3 goto B0
B3
b
37Computing liveness
a
b, c, f
b
f
a b c d -a e d f if e gt 3 goto B1
B0
a, c, f
c, d, f
c
e
c, d, e, f
c, d, e, f
d
c,e
b d e e e 1 goto B4
B2
c, e, f
f 2 e
B1
c, f
c, f
a, b a, d e, b e, a Any issues here?
c, f
b f c if b gt 3 goto B0
B3
b
38Computing liveness
a
b, c, f
b
f
a b c d -a e d f if e gt 3 goto B1
B0
a, c, f
c, d, f
c
e
c, d, e, f
c, d, e, f
d
c,e
b d e e e 1 goto B4
B2
c, e, f
f 2 e
B1
c, f
c, f
c, f
a, d can be in same register/location a, e can be
in same register/location d, e cannot!
b f c if b gt 3 goto B0
B3
b
39Graph Coloring
a
b
f
A coloring of a graph is an assignment of colors
to nodes such that nodes connected by an edge
have different colors. A graph is k-colorable if
it has a coloring with k colors.
c
e
d
What does this mean for our problem?
40Graph Coloring
a
b
f
A coloring of a graph is an assignment of colors
to nodes such that nodes connected by an edge
have different colors. A graph is k-colorable if
it has a coloring with k colors.
c
e
d
a, b, d can all be the same
color registers If we have k registers and the
graph is k-colorable, there is an assignment that
uses no more than k register.
Any other colorings possible in this case?
41Simplifying due to coloring
a
b
f
a b c d -a e d f if e gt 3 goto B1
B0
c
e
d
b d e e e 1 goto B4
B2
f 2 e
B1
b f c if b gt 3 goto B0
B3
42Replace all ds with a
a
b
f
a b c a -a e a f if e gt 3 goto B1
B0
c
e
d
b a e e e 1 goto B4
B2
f 2 e
B1
b f c if b gt 3 goto B0
B3
43Replace all es with b
a
b
f
a b c a -a b a f if e gt 3 goto B1
B0
c
e
d
b a b b b 1 goto B4
B2
f 2 b
B1
b f c if b gt 3 goto B0
B3
44Problem reduces to k-coloring!
- This problem is NP-hard! No efficient algorithms
exist. - A coloring may not exist for a given number of
registers. - So, well use heuristics.
45A simple heuristic algorithm
- Let k be the coloring we want.
- Phase I
- Pick a node t with fewer than k neighbors
- Push t onto a stack and remove from the RIG
- Repeat until the graph has one node
- Phase II
- Assign colors to nodes on the stack in stack
order - For each node, pick a color different from those
assigned to already colored neighbors.
46Graph Coloring Heuristic, k4
a
a
b
b
f
f
c
e
c
e
d
d
47Graph Coloring Heuristic, k4
Stack a, d
a
b
b
f
f
c
e
c
e
d
48Graph Coloring Heuristic, k4
Stack a, d, f, b
a
b
f
c
e
c
e
d
49Graph Coloring Heuristic, k4
Stack a, d, f, b, e, c
a
b
f
c
e
d
50Graph Coloring Heuristic, k4
Stack a, d, f, b, e Pop c Color it blue
a
b
f
c
e
d
51Graph Coloring Heuristic, k4
Stack a, d, f, b Pop e Color it anything
other than blue (orange?)
a
b
f
c
e
d
52Graph Coloring Heuristic, k4
Stack a, d, f Pop b Color it anything other
than blue (orange?)
a
b
f
c
e
d
53Graph Coloring Heuristic, k4
Stack a What can we color a?
a
b
f
c
e
d
54What if the heuristic fails?
This graph cannot be 3-colored The heuristic will
not be able to find a color for some node
Stack a, d What color to make d (assuming
only three colors)
a
b
f
c
e
d
55Spillage...
Pick some node to live in memory. Well keep
that temporary out of registers (memory becomes
another color basically) Dont pick whats on
the stack, though, pick something with lots of
edges (why?)
Stack a, d What color to make d (assuming
only three colors)
a
b
f
c
e
d
56Spillage...
Pick some node to live in memory. Well keep
that temporary out of registers (memory becomes
another color basically). This is called
spilling the node to memory. But, well need
to use a register to load it in temporarily later.
Stack a, d What color to make d (assuming
only three colors)
a
b
f
c
e
d
57Who to spill
- Options
- Temporaries with the most edges
- Temporaries used the least
- Things outside of loops
All of these heuristics are reasonable