Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | raziejaz1089 |
View: | 224 times |
Download: | 0 times |
of 28
8/10/2019 Activation Records.ppt
1/28
UMBC
Activation RecordsModern Compiler Implementation in JavaAppel
Chapter 6
CMSC 432
Shon Vick
8/10/2019 Activation Records.ppt
2/28
UMBC
2
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) fcalls -> Many xs and ys
Runtime Stack
8/10/2019 Activation Records.ppt
3/28
8/10/2019 Activation Records.ppt
4/28
8/10/2019 Activation Records.ppt
5/28
UMBC
5
arg n..
arg 1stackpointer
framepointer
arg n..
arg 1stackpointer
framepointer
: frame sizeeither fixed
or varies =>Can bedeterminedvery late
Frame Pointer
g calls f(a1, a2, , an)
5
8/10/2019 Activation Records.ppt
6/28
UMBC
6
Registers
register : local vars, temporary results Can save load/store instructions
general purpose vs. special purposeregisters 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 dofine register save scheduling.
8/10/2019 Activation Records.ppt
7/28
UMBC
7
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 callinganotherfunction
Register windows
8/10/2019 Activation Records.ppt
8/28
UMBC
8
Parameter Passing (Contd)
argument passing in reg+ stack
Sometimes formalparameters are atconsecutive addresses :register save area bycallee
call-by-reference Code for dereferencing
formal parameter access
arg k..
arg 1
arg n..
arg k+1
registersavearea
framepointer
8/10/2019 Activation Records.ppt
9/28
UMBC
9
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)
8/10/2019 Activation Records.ppt
10/28
UMBC
10
Frame-resident Variables
Variables are written to memory only whennecessary Variable will be passed by reference or &
(address of) operator is applied Variable is accessed by a procedure nestedinside the current one
Value is too big to fit into a single register
Variable is an array Register holding variable is needed for specialpurpose (parameter passing)
Too many local variables (spilled into frame)
8/10/2019 Activation Records.ppt
11/28
UMBC
11
Escaped Variable
A variable escapes if it is passed byreference, its address is taken, or it
is accessed from a nested function.
Variables are bound to register or
memory in later phase in compiling.
8/10/2019 Activation Records.ppt
12/28
8/10/2019 Activation Records.ppt
13/28
UMBC
13
Lambda lifting
remove static links, only global routines
out-of-scope variables
referencing: pass additional pointers creating: heap 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
8/10/2019 Activation Records.ppt
14/28
8/10/2019 Activation Records.ppt
15/28
UMBC
15
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, AccessListt) {
head=h; tail=t;}
}
8/10/2019 Activation Records.ppt
16/28
UMBC
16
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);}
8/10/2019 Activation Records.ppt
17/28
8/10/2019 Activation Records.ppt
18/28
UMBC
18
Making new Frames
Hold information fo parameters & localvariables
Frame for function f with k formals
newFrame(f, l) where f: Label l:BoolList
Ex: a three-argument function named g with 1stargumentescaped (No parameters will be escapes in MiniJava.)
frame,newFrame(g,
new BoolList(true,
new BoolList(false,
newBoolList(false,null))))
8/10/2019 Activation Records.ppt
19/28
UMBC
19
Class Access
Describe formal & local vars in the frameor in registers
Abstract data type whose implementaion isvisible 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; }
8/10/2019 Activation Records.ppt
20/28
UMBC
20
Access and Allocate the Vars
InFrame(X) : a memory location at offsetX 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()
8/10/2019 Activation Records.ppt
21/28
UMBC
21
Representation of FrameDescriptions
Implementation of frame is an objectholding:
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 functionsmachine code is to begin
See Table 6.4 on page 129
8/10/2019 Activation Records.ppt
22/28
UMBC
22
Local Variables
To allocation a new local var in a frame f f.allocLocal(true) //allocate in Memory will return InFrame()access with an offset from FPex) 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
f ti f()
8/10/2019 Activation Records.ppt
23/28
Allocating LocalStorage in frame
with the SameName
function f() =
let var v1:= 6in
print(v1);
let var v2:= 7in print(v2);
end
print(v1);
let var v3:= 8
in print(v3);endprint(v1);
end
allocLocal()
allocLocal()
allocLocal()
v1v2v3
v1mightuse samespace
frame
pointer
stackpointer
8/10/2019 Activation Records.ppt
24/28
UMBC
24
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 avariable
integers and booleans are passed by value
object, including integer arrays, can be
represented as pointers that are passed byvalue
8/10/2019 Activation Records.ppt
25/28
UMBC
25
Calculating Escapes
FindEscape(): looks for escaping variablesand records this information in the escapefields 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
8/10/2019 Activation Records.ppt
26/28
UMBC
26
Temporaries and Labels
Temps are virtual registers May not be enough registers available to store
all temporaries in a register
Delay decision until later Label s are like labels in assembler, a
location of a machine language instruction Classes Temp and Label inpackage Temp Packages Frame and Temp provide machine
independent views of variables
8/10/2019 Activation Records.ppt
27/28
UMBC
27
Managing Static Links
Static Link management is somewhattedious?
MiniJava does not have nested functiondeclararions: thus Frameshould not knowanything about static links.
It will be handled in the Translation phase.
Static links may be passed to the callee bythe 1stformal parameter.
8/10/2019 Activation Records.ppt
28/28
UMBC
28
References
http://www.cs.rutgers.edu/~ryder/415/lectures/runtimeSyst3.pdf
http://pds.twi.tudelft.nl/~koen/compilerbouw/slides/week11.ppt
Modern Compiler Implementation in Java,
Appel , Cambridge University Press, Chapter 6
http://www.cs.rutgers.edu/~ryder/415/lectures/runtimeSyst3.pdfhttp://www.cs.rutgers.edu/~ryder/415/lectures/runtimeSyst3.pdfhttp://pds.twi.tudelft.nl/~koen/compilerbouw/slides/week11.ppthttp://pds.twi.tudelft.nl/~koen/compilerbouw/slides/week11.ppthttp://pds.twi.tudelft.nl/~koen/compilerbouw/slides/week11.ppthttp://pds.twi.tudelft.nl/~koen/compilerbouw/slides/week11.ppthttp://www.cs.rutgers.edu/~ryder/415/lectures/runtimeSyst3.pdfhttp://www.cs.rutgers.edu/~ryder/415/lectures/runtimeSyst3.pdf