Vous êtes sur la page 1sur 28

UMBC

Activation Records
Modern Compiler Implementation in Java
Appel
Chapter 6

CMSC 432
Shon Vick

UMBC

Local Variables, Instantiations

Ex:
function f(x:int) : int =
let var y := x + x
in if y < 10 then f(y)
else y 1
end

Many (recursive) f calls -> Many xs and ys


Runtime Stack
2

UMBC

Higher-order Function

In Pseudo-C
int (*)() f(int x) {
int g(int y) {return
x+y;}
return g;
}

nested function
+
higher-order function
-> not in stack-mode
Pascal
-> no higher function

int (*h)() = f(3); -> x=3


int (*j)() = f(4); -> x=4

C -> no nested function

int z = h(5) ; <- no x

runtime stack

int w = j(7) ; <- no x

Local varialbes
Parameters
Return address
Temporaries
Register save area

Usually has standard


frame layout for several
languages
Depends on architecture

incoming
args

frame
pointer

arg n
.
.
arg 1
Static link

prev
frame

local
variables
return address
temps

lower memory addresses

Push/pop frames
Access variables in deeper
frames -> nonlocal
variables
Stack frame

higher addresses

Stack Frames

saved
registers

outgoing
args

current
frame

arg n
.
.
arg 1
static link

stack
pointer
next
frame
4

UMBC

Frame Pointer

frame
pointer

stack
pointer

g calls f(a1, a2, , an)


arg n
.
.
arg 1

frame
pointer

stack
pointer

arg n
.
.
arg 1

: frame size
either fixed
or varies =>
Can be
determined
very late
5
5

UMBC

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.
6

UMBC

Parameter Passing

passing with stack


passing some in registers and others in
stack
k=6 or 4
Need to save register when call another function

need not to save argument registers,


when
Leaf procedure
Interprocedural register allocation

Arguments become dead variables when calling another

function
Register windows

UMBC

Parameter Passing (Contd)

argument passing in reg


+ stack
Sometimes formal
parameters are at
consecutive addresses :
register save area by
callee
call-by-reference

frame
pointer

arg n
.
.
arg k+1
arg k
.
.
arg 1

register
save
area

Code for dereferencing


formal parameter access
8

UMBC

Return Address

g calls f : f returns

Need gs address (resume point) -> return


address

Can be saved

On stack
In special register
In special memory location

Hardware call instruction dependent


Usually in designated registers
Need to save (no-leaf proc)
No need to save (leaf proc)

UMBC

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)

10

UMBC

Escaped Variable

A variable escapes 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.

11

prettyprint
output
write
--output
show
n
ident
i,s
--output
--n
-- i,s

Static Links
Variable References
Static Links
Display
Lambda lifting
(passing all nonlocals
as parameters)

Procedure Calls
prettyprint

show
show
,
,

ident

UMBC

Lambda lifting

remove static links, only global routines


out-of-scope variables
referencing: pass additional pointers
creating: heap allocation
typedef int (*fptr)();

int aap(int *i) {return *i+7;}

fptr mies(int i) {
int aap()
{return i+7;}

fptr mies(int i) {
int *_i = malloc(sizeof(i));
*_i = i;

return aap;
}

return closure(aap,_i);
}

nested

lifted

13

UMBC

Frames in MiniJava

Package Frame

Access.java AccessList.java Frame.java

PackageTemp

Temp.java, TempList.java, Label.java,


LabelList.java

Package Util

BoolList.java

Package T (Mips, Sparcs)

T(Mips/Sparcs)Frame.java

Inframe(), InReg(), newFrame(), allocLocal()


14

UMBC

Package Frame

Abstraction of Actual Frames


package Frame;
import Temp.Templ 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;}
}

15

UMBC

Frame.java

public abstract class Frame {


public abstract Frame newFrame(Temp.Label name,
Util.BoolList formals);
public Temp.Label name;
public AccessList formals;
public abstract Access allocLocal(boolean escape);
public abstract Temp.Temp FP();
public abstract Temp.Temp RV();
/* ..other stuff, eventually */

//
//

public abstract int wordSize();


public abstract Tree.Exp externalCall(String func,
Tree.ExpList args);

}
16

UMBC

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


17

UMBC

Making new Frames

Hold information fo parameters & local


variables
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 (No parameters will be escapes in MiniJava.)
frame,newFrame(g,
new BoolList(true,
new BoolList(false,
new
BoolList(false,null))))

18

UMBC

Class Access

Describe formal & local vars in the frame


or in registers
Abstract data type whose implementaion 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; }
19

UMBC

Access and Allocate the Vars

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 calle :
shift of view
View shift must be handled by newFrame() 20

UMBC

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 functions
machine code is to begin
See Table 6.4 on page 129

21

Local Variables

UMBC

To allocation a new local var in a frame f

f.allocLocal(true) //allocate in Memory


will return InFrame() access with an offset from FP
ex) two local vars 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
22

Allocating Local
Storage in frame
with the Same
Name

function f() =
allocLocal()

allocLocal()

frame
pointer

v1 might

use same
space

stack
pointer

v1
v2
v3

let var v1 := 6
in
print(v1);
let var v2 := 7
in print(v2);
end
print(v1);

allocLocal()

let var v3 := 8
in print(v3);
end
print(v1);
end

UMBC

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

24

UMBC

Calculating Escapes

FindEscape(): looks for escaping variables


and records this information in the escape
fields of AST
Traverse the entire AST before semantic
analysis

When the variables are encounted,


Set 0 when first encountered
Set 1 when referenced at inner block!
when address is taken by &
when call-by-reference

25

UMBC

Temporaries and Labels

Temps are virtual registers

May not be enough registers available to store


all temporaries in a register
Delay decision until later

Labels are like labels in assembler, a


location of a machine language instruction
Classes Temp and Label in package Temp
Packages Frame and Temp provide machine
independent views of variables
26

UMBC

Managing Static Links

Static Link management is somewhat


tedious?
MiniJava does not have nested function
declararions: 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.
27

UMBC

References

http://www.cs.rutgers.edu/~ryder/41
5/lectures/runtimeSyst3.pdf
http://pds.twi.tudelft.nl/~koen/compi
lerbouw/slides/week11.ppt
Modern Compiler Implementation in Java,
Appel , Cambridge University Press, Chapter 6

28