
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Ā© Harry H. Porter, 2006
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Static Allocation
Ā© Harry H. Porter, 2006
Stack Allocation
Each variable is ā local ā to some routine
Invoke a routine?
Allocate storage for its variables
(and initialize it?)
Return?
Pop frame
(Variables are destroyed)
Consider one routine (e.g., āquicksortā)
Many activations, many frames
! Many copies of each local variable
Local variables:
Each invocation has its own set of variables
The ācurrently activeā invocation
Its variables will be in the frame on top of stack.
Every reference to a local variable...
will access data in the top frame
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
References to a local variable in the currently active routine...
%sp
%fp
StackTop
*(StackTop + offset X
) ld [%fp-48],%l
Ā© Harry H. Porter, 2006
Laying Out the Frame
Each local (and temp) variable has a size
āCā int: 4 bytes,
double: 8 bytes, ....
āPCATā all variables: 4 bytes
Each local and temp variable needs an ā offset ā
for each procedure (or block) do
offset = 0;
for each local and temp variable do
assign this variable to current offset
offset = offset + this variableās length
endFor
endFor
Weāll treat āmainā body as just another routine.
It will have a frame
Global variables
Treat identically to local variables for procedures!
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Example
Ā© Harry H. Porter, 2006
Example
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Example
Re-order!
Ā© Harry H. Porter, 2006
The Dynamic Link
When we return, we need to be able to go back to previous frame.
During a call:
(Allocate new frame)
in Dynamic Link field
During a return:
the old StackTop
To access local variables:
[StackTop + offset X
]
To access variables in our callerās frame:
[[StackTop + offset DynamicLink
] + offset X
]
What do we need from the callerās frame?
ā¢ā¢ā¢
StackTop
used to access
non-local variables
(to be discussed later)
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
SPARC
Much of the Activation Record is cached in registers!!!
Dynamic Link
Stored in registers (%sp, %fp)
Return Address
Stored in registers (%i7, %o7)
Arguments
Some in registers %i0 ... %i
Rest in callerās frame
Returned Value
32-bits: in register (%i0, %o0)
Machine Status
64 bytes
Architecture dependent & often not needed
ā¢ā¢ā¢
%sp
%fp
hidden register
hidden register
hidden register
Ā© Harry H. Porter, 2006
The āCalling Sequenceā
The āReturn Sequenceā
Flexibility as to who...
**_- !caller
...does what...
**_- calling sequence
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Callerās Code: Calleeās Code:
...
...
...
AAA
AAA
AAA
DDD
DDD
DDD
...
...
...
BBB
BBB
BBB
...
...
...
CCC
CCC
CCC
...
...
...
call foo
...
...
...
foo: ...
...
...
...
ret
Calling Sequence
Return Sequence
Ā© Harry H. Porter, 2006
Parameter Passing in SPARC
Caller
āfooā
Callee
ābarā
fp+
fp+
...
Arguments to ābarā
at known offset in
the callerās frame
fp
sp
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for Expression Evaluation
Source Code:
x := y + (2 * z);
Target Code:
push y
push 2
push z
mult
add
pop x
Ā© Harry H. Porter, 2006
Using a Stack for Expression Evaluation
Source Code:
x := y + (2 * z);
Target Code:
push y
push 2
push z
mult
add
pop x
Frame, with
local variables
Used for expression
evaluation
x:
y:
z:
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Fooās Frame
Calling Sequence:
Push args onto the stack
Save FP
TOP = TOP + FrameSize
Return Sequence:
Move return value to where arg 1 was
Restore TOP, FP
Pop stack top into...
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Fooās Frame
Result
Calling Sequence:
Push args onto the stack
Save FP
TOP = TOP + FrameSize
Return Sequence:
Move return value to where arg 1 was
Restore TOP, FP
Pop stack top into...
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
Barās Frame
Waiting
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x:
y: 30
z: 40
i: 50
Ā© Harry H. Porter, 2006
Using a Stack for
Argument Evaluation and Parameter Passing
Source Code:
x := y + 2 * (bar(7,i+1));
Target Code:
push y
push 2
push 7
push i
push 1
add
call bar
mult
add
pop x
Fooās Frame
x: 36
y: 30
z: 40
i: 50
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Variable-Length Local Variables
Goal:
Allow a routine to have variable-length data
(i.e., dynamically-sized arrays) as local data in frame
Ā© Harry H. Porter, 2006
Variable-Length Local Variables
Goal:
Allow a routine to have variable-length data
(i.e., dynamically-sized arrays) as local data in frame
Option 1:
Allocate the variable on the heap
Work with pointers to the data
PCAT: Hide the pointers from the programmer
Programmer codes:
a[i]
Compiler produces code like this:
(a + 4i)
Auto free the data when the routine returns?
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Variable-Length Local Variables
Goal:
Allow a routine to have variable-length data
(i.e., dynamically-sized arrays) as local data in frame
Option 1:
Allocate the variable on the heap
Work with pointers to the data
PCAT: Hide the pointers from the programmer
Programmer codes:
a[i]
Compiler produces code like this:
(a + 4i)
Auto free the data when the routine returns?
Option 2:
Create the variable on the stack, dynamically
Effectively: Enlarge the frame as necessary
Still need to work with pointers