This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on Runtime Environments: Page 1 of 32 More on Runtime Environments • How to efficiently implement procedure call and return in the presence of higher-order functions ? 1. what are higher-order functions ? 2. how to extend stack frames to support higher-order functions ? 3. efficiency issues (execution time, space usage) ? • How to efficiently support memory allocation and de-allocation ? 1. what are the data representations ? 2. what are the memory layout ? 3. explicit vs implicit memory de-allocation ? (malloc-free vs. garbage collection) C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on Runtime Environments: Page 2 of 32 Procedure Parameters (in Pascal) • Procedure parameters permit procedures to be invoked “out-of-scope”; • Question : how to get the correct environment when calling h inside b ? • Solution : must pass static link along with f as if it had been called at the point it was passed (line 11). 1 program main(input, output); 2 3 procedure b( function h(n : integer): integer); 4 var m : integer; 5 begin m := 6; writeln(h(2)) end ; 6 7 procedure c; 8 var m : integer; 9 function f(n: integer): integer; 10 begin f := m + n end ; 11 begin m := 0; b(f) end ; 12 begin c end . C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on Runtime Environments: Page 3 of 32 Restrictions in C & Pascal • C does not allow nested procedures --- names in C are either local to some procedure or are global and visible in all procedures. Procedures in C can be passed as arguments or returned as results. • Pascal (or Modula-2 , Modula-3 , Algol ) allows procedure declarations to be nested, but procedure parameters are of restricted use, and procedures cannot be returned as result. • Functional languages (e.g. ML, Haskell, Scheme, Lisp) support higher- order functions --- supporting both nested procedures and procedures passed as parameters or returned as results. supporting it is a big challenge to the compiler writers ! C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on Runtime Environments: Page 4 of 32 Traditional Stack Scheme program counter HEAP (dynamic data) STACK (activation records) STATIC (code and globals) Memory Layout code garbage collector parameters and returned values links and saved status temporaries and local data REGISTERS Activation Record C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on Runtime Environments: Page 5 of 32 Procedure Activations Nested Functions in ML val BIG = big(N) fun P(v,w,x,y) = let fun Q() = let val u = hd(v) fun R() = . . . P ( v , u , u , y ) . . ....
View Full Document
This document was uploaded on 02/20/2012.
- Fall '09