Chapter 6 Activation Records. Local Variables, Instantiations int f(int x) { int y = x+x; if (y<10) return f(y); else return y-1; } Many (recursive) f.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Activation Records Mooly Sagiv html:// Chapter 6.3.
Runtime Environments Source language issues Storage organization
Activation Records Mooly Sagiv html:// Chapter 6.3.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Activation Records Mooly Sagiv html:// Chapter 6.3.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Routines and Control Flows Code Generation Professor Yihjia Tsai Tamkang University.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands.
Activation Records Chapter 6. 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.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Activation Records (in Tiger) CS 471 October 24, 2007.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Runtime storage and Activation Records
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
COMPILERS Activation Records
Activation Records Mooly Sagiv
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Runtime Environments What is in the memory?.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Chapter 6 Activation Records

Local Variables, Instantiations int f(int x) { int y = x+x; if (y<10) return f(y); else return y-1; } Many (recursive) f calls -> Many x ’ s and y ’ s  Runtime Stack

Higher-order Function In Pseudo-C int (*)() f(int x) { int g(int y) {return x+y;} return g; } int (*h)() = f(3); -> x=3 int (*j)() = f(4); -> x=4 int z = h(5) ; <- y = 5 but no x int w = j(7) ; <- no x nested function (where inner functions may use variables defined in the outer functions) + functions returned as results -> not in stack-mode C -> no nested function  runtime stack

Stack Frames incoming args localvariables return address tempssavedregisters arg n.. arg 1 Static link arg n.. arg 1 static link outgoing args current frame prev frame next frame stack pointer frame pointe r lower memory addresses higher addresses 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

arg n.. arg 1 stack pointe r frame pointer arg n.. arg 1 stack pointe r frame pointer : frame size either fixed or varies => Can be determined very late Frame Pointer g(…) calls f(a 1, a 2, ………, a n )

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)

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 To reduce memory traffic, need not to save “argument registers”, 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

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 k.. arg 1 arg n.. arg k+ 1 register save area frame pointer

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)

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)

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

Static Links prettyprint 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 prettyprintshowident show,,,,

Static Link activation record contains a static link (pointer) that points to outer scope int aap(int i) { int noot() {return i+7;} int mies(int i) {return i+noot();} return mies(2)- 3; } static link dynamic link return address parameter i aap static link dynamic link return address parameter i mies parameter i noot static link dynamic link return address

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?

Answers dynamic links static links A 2 B 2 B 1 B 0 C FP->static_link->static_link->param[#i]

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

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

Frames in MiniJava Package Frame Frame.java Access.java AccessList.java Package Temp 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()

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;} }

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); } Hold information for parameters & local variables allocated in this frame

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

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 1 st 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.)

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; }

Access and Allocate the Variables InFrame(X) : a memory location at offset X from the FP(frame pointer) InReg(t 84 ) : in register t 84 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()”

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

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 FP ex) two local variables in Sparcs => InFrame(-4), InFrame(-8) f.allocLocal(false) // allocate in register will return InReg() ex) on register-allocated vars => InReg(t 481 ) allocLocal(bool) Called when frame is create Called when nested block is entered

Allocating Local Storage in frame with the Same Name v function f() { var v 1 := 6 print(v 1 ); { var v 2 := 7 print(v 2 ); } print(v 1 ); { var v 3 := 8 print(v 3 ); } } allocLocal() v1v1 v2v2 v3v3 v 3 might use the same space of v 1 or v 2 frame pointer stack pointer

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

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

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 1 st formal parameter.