Date post: | 19-Jan-2016 |
Category: |
Documents |
Upload: | ronald-cole |
View: | 215 times |
Download: | 0 times |
1
Chapter 6
Runtime storage andActivation Records
2
Outline Basic computer execution model run-time program layout activation record procedure linkage Frame in MiniJava
3
Basic Execution Model MIPS as an example
» Intro to mips assembly» Programmed Intro To mips assembly.
CPU» ALU Unit» Registers» Control Unit
Memory» program» data
Memory
Registers
Control
ALU
4
Arithmetic and Logic Unit Performs most of the data operations Has the form:
OP Rdest, Rsrc1, Rsrc2
Operations are:» Arithmetic operations (add, sub,
mulo [mult with overflow])» Logical operations (and, sll, srl)» Comparison operations (seq, sge,
slt [set to 1 if less than])
Memory
Registers
Control
ALU
5
Arithmetic and Logic Unit Many arithmetic operations can cause
an exception» overflow and underflow
Can operate on different data types» 8, 16, 32 bits» signed and unsigned arithmetic» Floating-point operations
(separate ALU)» Instructions to convert between
formats (cvt.s.d)
Memory
Registers
Control
ALU
6
Control Handles the instruction sequencing Executing instructions
» All instructions are in memory» Fetch the instruction pointed by the
PC and execute it» For general instructions, increment
the PC to point to the next location in memory
Memory
Registers ALU
Control
7
Control Unconditional Branches
» Fetch the next instruction from a different location
» Unconditional jump to a given addressj label
» Unconditional jump to an address in a register jr rsrc
» To handle procedure calls, do an unconditional jump, but save the next address in the current stream in a register jal label [jump and link] jalr rsrc
Memory
Registers ALU
Control
8
Control Conditional Branches
» Perform a test, if successful fetch instructions from a new address, otherwise fetch the next instruction
» Instructions are of the form: brelop Rsrc1, Rsrc2, label
» ‘relop’ is of the form: ‘’, ‘eq’, ‘ne’, ‘gt’, ‘ge’, ‘lt’, ‘le’
Memory
Registers ALU
Control
9
Control Control transfer in special (rare)
cases» traps and exceptions» Mechanism
– Save the next(or current) instruction location
– find the address to jump to (from an exception vector)
– jump to that location
Memory
Registers ALU
Control
10
Memory Flat Address Space
» composed of words» byte addressable
Need to store» Program» Local variables» Global variables and data» Stack» Heap
Memory
Registers ALU
Control
11
Memory
Memory
Registers ALU
Control
Stack
Generated Code
HeapObjects
Arrays
locals(parameters)
12
Registers Load/store architecture
» All operations are on register values» Need to bring data in-to/out-of registers
» la Rdest, address [load address]
» lw Rdest, address [load word]
» li, Rdest, imm [load imm]
» sw Rsrc, address [store word ]
» mv Rdest, Rsrc [ move ]
» address has the from value(R) Important for performance
» limited in number
ALU
Control
Memory
Registers
13
Other interactions Other operations
» Input/Output» Privilege / secure operations» Handling special hardware
– TLBs, Caches etc.
Mostly via system calls » hand-coded in assembly» compiler can treat them as a normal
function call
ALU
Control
Memory
Registers
14
Registers (of MIPS processors)
0 zero hard-wired to zero
1 at Reserved for asm
2 - 3 v0 - v1 expr. eval and return of results
4 - 7 a0 - a3 arguments 1 to 4
8-15 t0 - t7 caller saved temporary(save before call)
16 - 23 s0 - s724, 25 t8, t9 caller saved temporary
28 gp pointer to global area
29 sp stack pointer
30 fp frame pointer
31 ra return address
15
Typical program layout Start of the stack Heap management
» free lists starting location in
the text segment
Stack
Text segment
HeapObjectsArrays
locals(parameters)
0x7fffffff
0x400000
Reserved
static Data segment
16
Procedure call and Stack Frame
17
Activations An invocation of procedure P is an activation of P
The lifetime of an activation of P is» All the steps to execute P» Including all the steps in procedures that P
calls
18
Lifetimes of Variables The lifetime of a variable x is the portion of
execution in which x is defined
Note that» Lifetime is a dynamic (run-time) concept» Scope is a static concept
19
Activation Trees When P calls Q, then Q returns before P does
» LIFO Stack
Lifetimes of procedure activations are properly nested
Activation lifetimes can be depicted as a tree
20
ExampleClass Main {
void g() { return ; }
void f() { g() ; }
void m() { g(); f(); };
} m
fg
g
m enter
g enter
g return
f enter
g enter
g return
f return
m return
activation tree for m() Lifetime of invocations
21
Example 2
Class Main {
int g() { return 1; };
int f(int x){
if( x == 0) return g(); else return f(x - 1); }
int m(){ return f(3) }
}
What is the activation tree for this example?
22
Example 2m() enter
f(3) enter f(2) enter f(1) enter f(0) enter g()
enter g()
return f(0) return f(1) return f(2) returnf(3) return
m() return
m()
f(3)
f(2)
f(1)
g()
23
Notes The activation tree depends on run-time behavior
The activation tree may be different for every program input
Since activations are properly nested, a stack can track currently active procedures
24
ExampleClass Main {
g() { return; };
f() { g() };
m() { g(); f(); };
}m Stack
m
25
ExampleClass Main {
g() { return; }
f(): Int { g() }
m() { g(); f(); }
} m
g
Stack
m
g
26
ExampleClass Main {
g() { return }
f() { g() }
m() { g(); f(); }
}m
g f
Stack
m
f
27
ExampleClass Main {
g() { return; }
f() { g(); }
m(){ g(); f(); }
} m
fg
g
Stack
m
f
g
28
Revised Memory Layout
Low Address
High Address
Memory
Code
Stack
29
Activation Records On many machine the stack starts at high-
addresses and grows towards lower addresses
The information needed to manage one procedure activation is called an activation record (AR) or (stack) frame
If procedure F calls G, then G’s activation record contains a mix of info about F and G.
30
What is in G’s AR when F calls G?
F is “suspended” until G completes, at which point F resumes. G’s AR contains information needed to resume execution of F.
G’s AR may also contain:» Actual parameters to G (supplied by F)» G’s return value (needed by F)» Space for G’s local variables
31
The Contents of a Typical AR for G
Space for G’s return value Actual parameters Pointer to the previous activation record
» The control link; points to AR of caller of G Machine status prior to calling G
» Contents of registers & program counter» Local variables
Other temporary values
32
Example 2, Revisited
Class Main {
int g() { return 1; };
int f(int x) {if (x==0) return g();
else return f(x - 1); (**) };
void main() { f(3); (*)}
AR for f:result
argument
control link
return address
33
Stack After Two Calls to f
main result
3
(*)
f(3)
result
2
(**)
f(2)
Stack
fp for f(1)
fp for f(2)
34
Notes main has no argument or local variables and its
result is never used; its AR is uninteresting (*) and (**) are return addresses of the
invocations of f» The return address is where execution
resumes after a procedure call finishes
This is only one of many possible AR designs» Would also work for C, Pascal, FORTRAN, etc.
35
The Main Point
The compiler must determine, at compile-time, the layout of activation records and generate code
that correctly accesses locations in the activation record
Thus, the AR layout and the code generator must be designed together!
36
Heap Storage A value that outlives the procedure that creates it
cannot be kept in the AR
void foo(Foo f) { f.bar = new Bar(); }
The Bar object must survive deallocation of foo’s AR
Languages with dynamically allocated data use a heap to store dynamic data
37
Notes The code area contains object code
» For most languages, fixed size and read only The static area contains data (not code) with
fixed addresses (e.g., global data)» Fixed size, may be readable or writable
The stack contains an AR for each currently active procedure.» Each AR usually fixed size, contains locals
Heap contains all other data» In C, heap is managed by malloc and free
38
Stack Frames
incomingargs
locallocalvariablesvariables
return addressreturn address
tempstemps
saved saved registersregisters
arg narg n....
arg 1arg 1Static linkStatic link
arg narg n....
arg 1arg 1static linkstatic link
outgoingargs
currentframe
prevframe
nextframe
stackpointer
framepointer
low
er m
em
ory
ad
dre
sses
h
igh
er a
dd
ress
es
• Push/pop frames• Access variables in deeper
frames -> nonlocal variables• Stack frame
– Local variables– Parameters– Return address– Temporaries– Register save area
• Usually has “standard” frame layout for several languages
• Depends on architecture
39
arg narg n....
arg 1arg 1stackpointer
framepointer
arg narg n....
arg 1arg 1
stackpointer
framepointer
: frame sizeeither fixed or varies => Can be determined very late
Frame PointerFrame Pointer
g(…) calls f(a1, a2, ………, an)
40
Registers
register : local vars, temporary results…» Can save load/store instructions
general purpose vs. special purpose registers caller save vs. callee save register
» Ex: MIPS r16 - r23 are preserved across procedure calls (callee-save) r0 - r15 not preserved (caller-save)
If we do interprocedure analysis, we can do fine register save scheduling» If x is not needed after the call caller-save but not save» If x is need before and after the call callee-save» In general, register allocator (chapter 11)
41
Parameter Passing passing with stack (for machines designed in 1970s) passing some (k) in registers and others in stack
» k=6 or 4(mips: $a0~$a3)» Need to save register when call another function
To reduce memory traffic, need not save “argument registers” [into memory stack], when» Leaf procedure
– – procedure does not call other procedures
» Interprocedural register allocation – – analyze all functions
» Arguments become dead variables at the point where another function is called
» Register windows – - each function call allocates a fresh set of registers
42
Parameter Passing (cont’d)
argument passing in reg + stack
Sometimes formal parameters are at consecutive addresses: register save area by callee
call-by-reference» Code for dereferencing
formal parameter access» no dangling reference
int *f(int x) {return &x;}
void f(int &y);
arg karg k....
arg 1arg 1
arg narg n....
arg k+arg k+11
registersavearea
framepointer
43
Return Address g calls f : f returns
» Need g’s address (resume point) -> return address» Call instruction at address a
-> return to a+1 (the next instruction) Can be saved
» On stack» In special register» In special memory location
Hardware “call” instruction dependent» Usually in designated registers» Need to save (non-leaf proc)» No need to save (leaf proc)
44
Frame-resident Variables
Variables are written to memory only when necessary» Variable will be passed by reference or & (address
of) operator is applied» Variable is accessed by a procedure nested inside
the current one» Value is too big to fit into a single register» Variable is an array» Register holding variable is needed for special
purpose (parameter passing)» Too many local variables (“spilled” into frame)
45
Escaped Variable A variable “escape”s if
» it is passed by reference, » its address is taken, » or it is accessed from a nested function.
Variables are bound to register or memory in later phase in compiling.» declarations vs. uses
46
Static Linksprettyprint output
write --output
show
n
ident
i,s
--output
--n
-- i,s
Inner functions may use variables declared in outer functions
Variable References• Static Links• Lambda lifting (passing all nonlocals as arguments)• Display
Procedure Calls
prettyprint show ident
show,,
47
Static Link activation record contains a static
link (pointer) that points to outer scope
int a(int i) {
int c() {return i+7;}
int b(int i) {return i+c();}
return b(2)- 3;
}
static link
dynamic link
return address
parameter i
a
static link
dynamic link
return address
parameter i
b
parameter i
cstatic link
dynamic link
return address
48
Example Given the GNU C routine:
void A(int a) {
void B(int b) {
void C(void) {
printf(“C called, a = %d\n”, a);
}
if (b == 0) C() else B(b-1);
}
B(a);
}
a) draw the stack that results from the call A(2)b) how does C access the parameter (a) from A?
49
Answersdynamic
linksstatic
links
A 2
B 2
B 1
B 0
CFP->static_link->static_link->param[#i]
50
Lambda lifting outer-scope variables
» referencing: pass additional pointers
int f() {
int k = 5;
int g(int t) {
return k + t
}
return g(2);
}
nested
int g(int k, int t) { return k + t }
int f() {
int k = 5;
return g(k, 2);
}
lifted
51
Lambda lifting out-of-scope variables
» referencing: pass additional pointers» creating: heap (dynamic) allocation
typedef int (*fptr)();
fptr mies(int i) {
int aap() {return i+7;}
return aap;
}
nested
int aap(int *i) {return *i+7;}
fptr mies(int i) {
int *_i = malloc(sizeof(i));
*_i = i;
return closure(aap,_i);
}
lifted
52
Procedure linkages The linkage convention is the interface used for
performing procedure calls» on entry, establish p's environment» at a call, preserve p's environment» after a call, restore p’s environment» on exit, tear down p's environment» in between, handle addressability and lifetimes
Ensures each procedure inherits from caller a valid run-time environment and also restores one for its caller
53
Procedure LinkagesStandard procedure linkage
procedure p
prolog
epilog
pre-call
post-return
procedure q
prolog
epilog
Procedure has
• standard prolog
• standard epilog
Each call involves a
• pre-call sequence
• post-return sequence
54
Stack
return addressold frame pointer
Local variables
Calliee savedregisters
Stack temporaries
... argument 5argument 4
Dynamic area
Caller saved registers arguments
fp
sp
When calling a new procedure, caller:» push any t0-t9 that has a
live value on the stack» put arguments 1-4 on a0-
a3» push rest of the
arguments on the stack » do a jal or jalr
Pre-Call
55
return addressold frame pointer
return addressold frame pointer
Stack
Local variables
Callee savedregisters
Stack temporaries
... argument 5argument 4
Dynamic area
Local variables
Callee savedregisters
Caller saved registers arguments
Dynamic area
fp
sp
In a procedure call, the callee at the beginning:» push $fp on the stack» copy $sp+4 to $fp» push $ra on the stack» if any s0-s7 is used in the
procedure save it on the stack
» create space for local variables on the stack
» execute the callee...
Prolog
stack frame
56
return addressold frame pointer
Stack
Local variables
Callee savedregisters
Stack temporaries
... argument 5argument 4
Dynamic area
Caller saved registers arguments
In a procedure call, the callee at the end:
» put return values on v0,v1
» update $sp using $fp ($fp-8) - ...
» Pop the callee saved registers from stack
» restore $ra from stack
» restore $fp from stack
» execute jr ra and return to caller
fp
sp
Epilog
57
Stack On return from a procedure
call, the caller:» Update $sp to ignore
arguments» pop the caller saved
registers» Continue...
return addressold frame pointer
Local variables
Calliee savedregisters
Stack temporaries
... argument 5argument 4
Dynamic area
fp
sp
Post-call
58
Argument 5: bx (0)
Example Programclass auxmath {
int sum3d(int ax, int ay, int az, int bx, int by, int bz)
{int dx, dy, dz;if(ax > ay)
dx = ax - bx;else
dx = bx - ax; …
retrun dx + dy + dz;}
}
…int px, py, pz;px = 10; py = 20; pz = 30;auxmath am;am.sum3d(px, py, pz, 0, 1, -1);
return addressold frame pointer
Dynamic area
Caller saved registers
Argument 7: bz (-1)
fp
sp
Argument 6: by (1)
Local variable dx (??) Local variable dy (??) Local variable dz (??)
v0 ??v1 ??a0 thisa1 ax (10)a2 ay (20)a3 az (30)v0 ??v1 ??t0 ??t1 ??
59
Frames in MiniJava Package Frame
» Frame.java Access.java AccessList.java Package Temp
» Temp.java, TempList.java, Label.java, LabelList.java» Temp is used for temporary register» Label is used for storage location
Package Util» BoolList.java
Package T(Mips, Sparcs)» T(Mips/Sparcs) Frame.java
– Inframe(), InReg(), newFrame(), allocLocal()
60
Package Frame Abstraction of Actual Frames
package Frame;import Temp.Temp import Temp.Label;
Public abstract class Access{ … }public class AccessList { public Access head; public AccessList tail; public AccessList(Access h, AccessList t) { head=h; tail=t;}}
61
Frame.java
public abstract class Frame { public abstract Frame newFrame(Temp.Label name,
Util.BoolList formals); public Temp.Label name; //Function name public AccessList formals; //Parameters public abstract Access allocLocal(boolean escape);
public abstract Temp.Temp FP(); //Frame Pointer public abstract Temp.Temp RV(); //Return Value /* ..other stuff, eventually … */
// public abstract int wordSize(); //Size of Word// public abstract Tree.Exp externalCall(String func,
Tree.ExpList args);// public abstarct tree.Stm procEntryExit1(tree.Stm body );}
Hold information for parameters & local variables allocated in this frame
62
TFrame : specific to Target Machine
For T machine… package T; class Frame extends Frame.Frame { /* real definitions of Frame */ …. }
In machine independent part of compiler // in class Main.Main: Frame.Frame frame = new T.Frame(…);
» To hide the identity of the target machine
63
Making new Frames» Frame for function f with k formals
newFrame(f, l) where f : Label l: BoolList
Ex: a three-argument function named g with 1st argument escaped, i.e., needs to stay in memory
frame.newFrame(g,
new BoolList(true,
new BoolList(false,
new BoolList(false,null))))
(No parameters will be escapes in MiniJava.)
64
Class Access
Access formal & local variables in the frame or in registers
Abstract data type whose implementation is visible only inside the Frame module:
package T class InFrame extends Frame.Access { int offset; InFrame (int o) {offset = o; } } class InReg extends Frame.Access { Temp.Temp temp; InReg(Temp.Temp t) {temp = t; }
65
Access and Allocate the Variables InFrame(X) : a memory location at offset X from the
FP(frame pointer) InReg(t84) : in register t84
formals in Frame.java» A list of k “accesses” denoting locations where the
formal parameters will be kept at runtime , as seen from inside the callee
» May be seen differently by the caller and callee : “shift of view”
» View shift must be handled by “newFrame()”
66
Representation of Frame Descriptions
Implementation of frame is an object holding:» the location of all the formals» instructions required to implement the “view
shift”» the number of locals allocated so far» the “label” at which the function’s machine
code is to begin» See Table 6.4 on page 129
67
Local Variables
To allocate a new local variable in a frame f
» f.allocLocal(true) // allocate in memory (stack)
» will return InFrame() access with an offset from FPex) two local variables in Sparcs => InFrame(-4), InFrame(-8)
» f.allocLocal(false) // allocate in register» will return InReg()
ex) on register-allocated vars => InReg(t481)
allocLocal(bool) » Called when frame is create » Called when nested block is entered
68
Allocating Local Storage in frame
with the Same Name v
function f() {
var v1 := 6
print(v1);
{
var v2 := 7
print(v2);
}
print(v1);
{ var v3 := 8
print(v3);
} }
allocLocal()
allocLocal()
allocLocal()
v1
v2
v3
v3 mightuse the same space of v1 or v2
framepointer
stackpointer
69
Escape Variables No variables escape in MiniJava, because
» there is no nesting of classes and methods» it is not possible to take the address of a variable» integers and booleans are passed by value» object, including integer arrays, can be represented as
pointers that are passed by value
70
Temporaries and Labels
Temps are virtual registers» May not be enough registers available to store all
temporaries in a program» Delay decision until later
Labels are like labels in assembler, a location of a machine language instruction» processing the declaration m(…) new Temp.Label(“C”+”$”+”m”)
Classes Temp and Label in package Temp Packages Frame and Temp provide machine
independent views of variables
71
Managing Static Links Static Link management is somewhat tedious? MiniJava does not have nested function declarations:
» thus Frame should not know anything about static links.
It will be handled in the Translation phase. Static links may be passed to the callee by the 1st formal
parameter.