C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Implementation of Block Structured Languages.

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)
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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.
CS 536 Spring Run-time organization Lecture 19.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
CS 536 Spring Code generation I Lecture 20.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can.
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.
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.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
ISBN Chapter 10 Implementing Subprograms.
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Computer Science 210 Computer Organization
Implementing Subprograms
COMPILERS Activation Records
Implementing Subprograms
Run-time organization
Introduction to Compilers Tim Teitelbaum
Chapter 10: Implementing Subprograms Sangho Ha
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Implementation of Block Structured Languages

C SC 520 Principles of Programming Languages 2 Activations and Environment Aspects of Subroutines: Static vs Dynamic Static subroutine: code (``reentrant’’)  Exactly one subroutine Subroutine in execution: activation record, AR, activation  Many activations of same code possible State of a program in execution A collection of activations  In a stack or in a heap Contextual relationships among the activations  ``environment access’’ pointers & ``control’’ pointers Activation contents Fixed: program code (shared) Variable: activation  Instruction pointer (ip, ra) —also resumption address or return address  Control Pointer (dl) —also control link, dynamic link  Environment pointer (ep, fp) —also access link, frame pointer  Local environment (this activation)—fp  Nonlocal environment (other activations)—sl, static link

C SC 520 Principles of Programming Languages 3 Contour Diagram (Static) & RT Stack (Dynamic) aQRaQR bcdSTbcdST e b bcbc R(); R Q P S T Q(); T(); S(); Q(); Calling trace: P, R, Q, T, S, Q, T, S P R Q S Q T S T sl = static link to statically enclosing activation dl = dynamic link to caller at RT control environment Note sl and dl can be far apart Assume static binding

C SC 520 Principles of Programming Languages 4 Static Nesting Level ( snl ) and Distance ( sd ) aQRaQR bcdSTbcdST e b bcbc R Q P S T snl = 0 snl = 1 snl = 2 snl = 3 snl = 2 snl (name declaration) = # of contour lines surrounding declaration snl (name reference) = # contour lines surrounding reference e = b; a = b; Static Distance of a Symbol Occurrence sd (name occurrence) = # of contours crossed outward from occurrence to declaration = snl (name’s occurrence) - snl (that name’s decl.) a = b; snl (b) = 2 sd(b) = 0 snl (a) = 2 sd(a) = 1 snl (b) = 3 sd(b) = 0 snl (a) = 3 sd(a) = 2 snl (b) = 3 sd(b) = 1

C SC 520 Principles of Programming Languages 5 Symbol Table Computes snl Symbol table maps an occurrence of x to line # snl (declaration) Offset among declarations Each name x has an ``address’’: (line #, snl, offset) Scanner keeps track of Contour boundaries crossed (e.g. +1 for { & -1 for } ) Current name declarations in scope Scanner can therefore Identify declaration controlling a name occurrence Replace a name occurrence by pointer to symbol table line #

C SC 520 Principles of Programming Languages 6 Symbol Table (cont.) aQRaQR bcdSTbcdST e b bcbc R Q P S T e = b; a = b; # name snl offsettype &c … 1 P 00 int () 2 a 10 int 3 Q 11 int (int) 4 R 12 5 b 20 int 6 c 21 7 d 22 8 S 23 void (int) 9 T 24 int (int) 10 e 30 int 11 b 30 int 12 b 20 int 13 c 21 int Assume for simplicity all variables are int and occupy one address

C SC 520 Principles of Programming Languages 7 Activation Record Stack sl dl ra saved registers arguments fp sp temps stack pointer = next available location frame pointer = currently executing AR temps sl dl sl dl sl dl locals sp return address locals dynamic link static link higher addresses fp

C SC 520 Principles of Programming Languages 8 Activation Record Stack Model an AR by a struct (pretend all data are int for simplicity) struct AR { int arg[ n ]; int local[ m ]; AR* sl; AR* dl; CODE* ra; void* rx; —register save area... } Temps are pushed on top (``frame extension’’) during execution in the activation record and are abandoned on return Assume stack growth to higher addresses (in reality usually the other way)

C SC 520 Principles of Programming Languages 9 Registers Special purpose registers: ra, sp, fp General purpose registers divided into two classes Caller-saves: transient values unlikely to be needed across calls  Callee assumes nothing valuable in caller-saves set & can be used at will (“destroyed”)  Ex: temp values during expression evaluation in caller  Caller saves these during calling sequence and they are restored after subroutine return Callee-saves: used for local variables and indexes, etc.  Caller assumes these registers will not be destroyed by callee  Ex: register holding pointer during a list scan  Callee saves these in the prologue just after call, and restores in the epilogue just before return

C SC 520 Principles of Programming Languages 10 Compiler Code Generation What is generated at CT (to be executed at RT): Upon reference to a variable name x ? In caller before call to subroutine Q & after return to caller—the calling sequence? In callee before execution of body?  Prologue In callee before return to caller?  Epilogue Assume we are generating code inside body of a subroutine named P Compiler maintains a level counter during code generation: curr_level = current static nesting level of site where code is being generated (body of P )

C SC 520 Principles of Programming Languages 11 Access (Reference) to x inside P From symbol table compiler can compute: x  snl( x ), offset ( x ) P  snl( P ) curr_level = snl( P ) + 1 (level at which ref occurs) sd( x ) = curr_level - snl( x ) Generate code to compute l -value into lv : ap = activation record ptr ap = fp; for(i = 0; i sl ; lv = ap + offset (x); Use lv on LHS of assignment, *lv on RHS

C SC 520 Principles of Programming Languages 12 Call Q inside P Calling sequence for ``call Q’’ in source Assume arguments passed by value sp->arg[1] = value of argument 1 ; —transmit args... sp->arg[ n ] = value of argument n ; fp->ra= resume ; —set point to resume execution in caller sp->dl = fp; —set callee’s return link fp->ry = ry ; …; —save caller-saves registers ap = fp; —find AR of callee Q ’s declaration for(i = 0; i sl ; sp->sl = ap; —set callee’s static link fp = sp; —switch to new environment goto entrypoint (Q); —from symbol table, after Q is compiled resume: … note stack has not been pushed (callee’s responsibility)

C SC 520 Principles of Programming Languages 13 Prologue Code for Subroutine Q Code executed just after caller jumps to callee Note compiler knows size of AR for Q sp = sp + size( AR of Q ) ; —push stack frame for current activation fp->rx = rx; …; —save any callee-saves registers now sp points to next available stack location now fp points to subroutine frame base Push could be done by caller (caller knows name of Q at CT) But this will not work for closures (see below) where caller does not know name of callee at CT

C SC 520 Principles of Programming Languages 14 Epilogue code for Subroutine Q Code executed just before return to caller Note compiler knows size of AR for Q rx = fp->rx —restore any callee-saves registers sp = sp - size( AR of Q ) ; —pop stack frame for current activation fp = fp->dl; —make caller’s activation current one ry = fp->ry; … ; —restore caller-saves registers goto fp->ra; —resume execution in caller just after point of call now sp points to next available stack location now fp points to frame base of caller

C SC 520 Principles of Programming Languages 15 Display Method Linked list replaced by array! Replace traversal of static chain by a single memory reference— more efficient calculation of non-local environment references At CT, the maximum static nesting level is known; possible snl values are 1.. maxsnl The display is an array D of maxsnl elements D[i] = fp for that part of the environment that is in an AR at snl i D[1] D[2] D[3] D[4]... D[ maxsnl ] Executing at snl = 4 Equivalent static chain

C SC 520 Principles of Programming Languages 16 Access (Reference) to x inside P Generate code to compute l -value into lv : lv = *D[ snl (x)]+ offset (x) Use lv on LHS of assignment, *lv on RHS

C SC 520 Principles of Programming Languages 17 Call Q inside P inactive D[d] D[d+1]... D[u] P sp fp disp D[d] D[d+1]... D[u] P sp fp Q Inactive Before call Q Executing at snl = u in P Subr. Q defined at snl = d  u After call Q Executing at snl = d + 1 (body of Q one level deeper) D[d+1] overwritten to point to new AR

C SC 520 Principles of Programming Languages 18 Call Q inside P (cont.) Q defined at snl d  new AR executes at snl d+1  D[d+1] points to new AR for Q Old D[d+1] (dotted link) destroyed Saved in caller’s AR (since part of caller’s display) New AR field fp->disp Other elements D[i] where i  d + 2 left alone An AR deeper in the stack might need them upon return

C SC 520 Principles of Programming Languages 19 Call Q inside P (cont.) Calling sequence for ``call Q’’ in source Let u = snl (P) & d = snl (Q) Note fp == D[u] sp->arg[1] = value of argument 1 ; —transmit args... sp->arg[ n ] = value of argument n ; fp->ra= resume ; —set return point in caller sp->dl = fp; —set callee’s return link fp->ry = ry; …; —save caller-saves registers fp->disp = D[d+1]; —save caller’s display entry to reset on return D[d+1] = sp; —set display for callee; D[1..d] are shared fp = sp; —switch to callee environment goto entrypoint (Q); —from symbol table, after Q is compiled resume: …

C SC 520 Principles of Programming Languages 20 Prologue/Epilogue code for Subroutine Q Prologue same as before sp = sp + size( AR of Q ) ; —push stack frame for current activation fp->rx = rx;…; —save any callee-saves registers Epilogue restores caller’s display Let u = snl (Q) —this is known to compiler rx = fp->rx; …; —restore callee-save registers sp = sp - size(AR of Q ) ; —pop stack frame for current activation fp = fp->dl; —make caller’s activation current D[u] = fp->disp; —restore caller’s display ry = fp->ry; … ; —restore caller-saves registers goto fp->ra; —resume execution in caller just after point of call

C SC 520 Principles of Programming Languages 21 Costs: Static Chain vs Display Compare count of memory references Exclude argument transmission, reg. saves (common to both) Assume fp, sp held in registers Analyze calling sequence for static chain instruction# refs fp->ra= resume ; 1 sp->dl = fp; 1 sp->ry = ry; …; - ap = fp; 0 for(i = 0; i sl; sd (Q) sp->sl = ap; 1 fp = sp; 0 goto entrypoint (Q); 0 sd (Q)+3

C SC 520 Principles of Programming Languages 22 Costs (cont.) Comparison by # memory references: Need lots of sd (x)> 2 & sd (Q)> 2 to make worth it OperationStatic chainDisplay Access local l -value 11 Access non- local x l -value sd (x) 2 Call Q sd (Q) Q Prologue 00 Q Epilogue 35

C SC 520 Principles of Programming Languages 23 Funargs (Procedure/Function Arguments) PURPUR x y T R P P T Consider call U(T); (both U and T are visible in body of R) T is not visible to U  no T activation in the static chain of U  at the call T(2) in U, cannot locate definition environment of T ! How is the call F(2) implemented? Must work for any F actual What is passed to U in U(T)? F U F(2); U(P); U(T); void U(int F(int)); void P(int x); void R(); R(); void R(int y); function formal function actual P

C SC 520 Principles of Programming Languages 24 Funargs (cont.) Consider call F(2); Previous calling sequence cannot be used. Missing information shown in blue: sp->arg[1] = value of argument 1 ; … ; —transmit args fp->ra= resume ; —set return point in caller sp->dl = fp; —set callee’s return link fp->ry = ry ; …; —save caller-saves registers ap = fp; —find AR of callee F ’s declaration for(i = 0; i sl ; sp->sl = ap; —set callee’s static link fp = sp; —switch to new environment goto entrypoint (F); —from symbol table, after F is compiled resume: … Don’t know what F really is at CT and don’t know sd (F) and entrypoint (F)

C SC 520 Principles of Programming Languages 25 Calling a Formal: F(…); inside U(F) sd (F) is unknown at CT At RT, the actual functional argument need not even be in U ’s static chain  it is inaccessible from the current AR  environment of definition of each funarg must be passed to U as part of actual argument A funarg or closure is a pair (ip, ep) where: ip = entry address of the actual argument procedure ep = reference to most recent activation of definition environment of actual argument procedure

C SC 520 Principles of Programming Languages 26 Closure Implementation A closure is a pair of references: struct CL { CODE* ip; —instruction pointer (entrypoint) AR* ep; —environment pointer } Closure f is built in caller when a named procedure is passed as an actual f is copied to callee U as actual corresponding to formal F : effectively `` F = f ’’ When U calls F, the static link in the new activation is set by sp->sl = F.ep and the jump is by goto F.ip

C SC 520 Principles of Programming Languages 27 Call F inside U Calling sequence for ``call F’’ in source where F is a function formal sp->arg[1] = value of argument 1 ; —transmit args... sp->arg[ n ] = value of argument n ; fp->ra= resume ; —set return point to resume execution sp->dl = fp; —set callee’s return link fp->ry = ry ; …; —save caller-save registers sp->sl = F.ep; —set callee’s static link fp = sp; —switch to new environment goto F.ip; —entrypoint of code of actual resume: … ap = fp; —find AR of callee Q ’s declaration for(i = 0; i sl ;

C SC 520 Principles of Programming Languages 28 Constructing and Passing Closure Consider call U(T) in AR for R Case: actual proc T is visible, named proc & so is U sp->arg[ 1 ].ip = entrypoint (T); fp->ra= resume ; —set return point to resume execution sp->dl = fp; —set callee’s return link fp->ry = ry ; …; —save caller-save registers ap = fp; —find AR of argument T ’s declaration for(i = 0; i sl ; sp->arg[ 1 ].ep = ap; —environment of T set in callee ap = fp; for(i = 0; i sl ; sp->sl = ap; —set callee’s static link fp = sp; —switch to new environment goto entrypoint (U); —from symbol table resume: …

C SC 520 Principles of Programming Languages 29 Prologue/Epilogue Code Same as for ``named’’ calls, since code is generated once for each possible named actual such as T Information for allocation/deallocation known at CT for T

C SC 520 Principles of Programming Languages 30 Calls with Formal Procedures: Cases Let F, F’ name formal functional parameters and let U name a visible, actual proc Discuss implementation of calling sequences for each of: U(F); F(T); F(F’);

C SC 520 Principles of Programming Languages 31 Calls with Formal Procedures: F(T) Call to a formal proc with an actual visible named proc sp->arg[ 1 ].ip = entrypoint (T); ap = fp; —find AR of argument T ’s declaration for(i = 0; i sl ; sp->arg[ 1 ].ep = ap; —environment of T set in callee fp->ra= resume ; —set return point to resume execution sp->dl = fp; —set callee’s return link fp->ry = ry ; …; —save caller-save registers sp  sl = F.ep; —set callee’s static link fp = sp; —switch to new environment goto F.ip; —from closure of F resume: … ap = fp; for(i = 0; i sl ; sp->sl = ap; —set callee’s static link

C SC 520 Principles of Programming Languages 32 Challenge Can we implement functional parameters using the display? Where does F get its display? (No static chain to unravel given only a starting environment F.ep ) How is display restored upon return?

C SC 520 Principles of Programming Languages 33 Blocks Extend existing environment: { int x;... } Special case of subroutine: No parameters No name Called in one place—where defined Statically prior env. (surrounding block) == dynamically prior surrounding … void function B(); { float x,y;{ float x, y;x = z; y = 3;w = y; }} … blocksurrounding … B(); … block

C SC 520 Principles of Programming Languages 34 Block Activation/Deactivation A block is like a procedure, but Nameless (because called from only one place) Parameterless Defined at its point of invocation (inline text) Same static binding rules apply (static link == dynamic link) Why are references in body of B resolved correctly? Can remove need for new AR by allowing caller’s AR to grow and shrink sp->sl = fp; —set callee’s return link fp = sp; —switch to new environment sp = sp + size( AR of B ) ; —push stack frame for block activation entrypoint(B): …... Body of B... sp = sp - size( AR of B ) ; —pop stack frame for current activation fp = fp->sl; —reactivate containing block resume: …

C SC 520 Principles of Programming Languages 35 Exercise Show how to handle block entry/exit with using the display method... D[u] P sp fp disp... D[u] P sp fp Q Before block B entry Executing at snl = u in P After block B entry Executing at snl = u + 1 (body of B one level deeper) D[u+1] overwritten to point to new AR D[u+1] B

C SC 520 Principles of Programming Languages 36 Solution to Exercise: fp->disp = D[u+1]; —save caller’s display entry D[u+1] = sp; —set callee’s display fp = sp; —switch to new environment sp = sp + size( AR of B ) ; —push stack frame for block activation entrypoint(B): …... Body of B... sp = sp - size( AR of B ) ; —pop stack frame for current activation fp = D[u]; —reactivate containing block D[u+1] = fp->disp; —restore caller’s display resume: … fp = sp - size( AR of P ) ; —reactivate containing block optimization

C SC 520 Principles of Programming Languages 37 Non-local goto ’s sd (L) = snl ( use L) – snl ( def L) = snl (D )+1 – snl (L) ap = fp ; for(i = 0; i sl ; fp = ap; sp = fp + size( AR of A ) ; goto address (L); What if display is used? How restore environment of A? label L B C A B D C D { B(); L: print x; } {... goto L;... } C(); D(); A B C D A  sp fp sp fp

C SC 520 Principles of Programming Languages 38 Label Scope Rules Vary /* In C, labels have entire function as scope */ #include main() { int i = 3; int n = 10; printf("before forward jump i = %d\n", i); goto fore; back: printf("after back jump i = %d\n", i); if (n < 3) { int i = 7; int j = 13; fore: i = i + 1; printf("after forward jump i = %d\n", i); printf("after forward jump j = %d\n", j); goto back; } else { int i = 99; printf("after else i = %d\n", i); } printf("before return i = %d\n", i); }

C SC 520 Principles of Programming Languages 39 Label Scope Rules (cont.) opu> cc labels.c opu> a.out before forward jump i = 3 after forward jump i = 1 after forward jump j = 0 after back jump i = 3 after else i = 99 before return i = 3 opu>

C SC 520 Principles of Programming Languages 40 Returned Subroutines main() { int(int) makemult(int n) { int t(int x){ return n*x; }; return t; } int(int) f; int y; f = makemult(3); y = f(2); }

C SC 520 Principles of Programming Languages 41 Returned Subroutines (cont.) Before call to makemult(3) null pointer = At prologue of makemult(3) At return from makemult(3) ra dl sl f y ra dl sl f y ra dl sl n 3 main makemult epip r0 = x r1 = n r0 = mul r0 r1... r0 = makemult(3) f = r0... code (closure): typically in register  fp frame pointer =

C SC 520 Principles of Programming Languages 42 Returned Subroutines (cont.) After assignment f =... At prologue of call f(2) ra dl sl f y main ra dl sl n 3 makemult ra dl sl f y ra dl sl n 3 main makemult epip r0 = x r1 = n r0 = mul r0 r1 epip r0 = x r1 = n r0 = mul r0 r1 ra dl sl x 2 f Note static and dynamic links differ... r0 = makemult(3) f = r0 code r0 = f(2) fp

C SC 520 Principles of Programming Languages 43 Returned Subroutines (cont.) After assignment y = f(2) The AR for makemult(3) is never ``popped’’ while main() is active main activation refers to a function value f functional value f requires definition of n in makemult AR function f in environment can be called again many times  So lifetime of makemult AR is lifetime of main ARs now managed on a heap, along with closures ra dl sl f y 6 ra dl sl n 3 main makemult epip r0 = x r1 = n r0 = mul r0 r1 fp