CS 242 Revised class schedule Scope, Function Calls and Storage Management � Wed Oct 27 • No lecture; discussion section during class time • Midterm Exam 7-9PM � Friday Oct 29 John Mitchell • No discussion section Topics Block-Structured Languages � Block-structured languages and stack storage � Nested blocks, local variables � In-line Blocks • Example new variables declared in nested blocks { int x = 2; • activation records { int y = 3; • storage for local, global variables outer inner local variable x = y+2; block � First-order functions block } global variable • parameter passing } • tail recursion and iteration � Higher-order functions • Storage management • deviations from stack discipline – Enter block: allocate space for variables • language expressiveness => implementation complexity – Exits block: some or all space may be deallocated Examples Simplified Machine Model Registers Code Data � Blocks in common languages • C { … } • Algol begin … end Stack • ML let … in … end � Two forms of blocks • In-line blocks Program • Blocks associated with functions or procedures Counter Heap � Topic: block-based memory management, Environment Pointer access to local variables, parameters,global vars 1
Interested in Memory Mgmt Only Some basic concepts � Registers, Code segment, Program counter � Scope • Ignore registers • Region of program text where declaration is visible • Details of instruction set will not matter � Lifetime � Data Segment • Period of time when location is allocated to program • Stack contains data related to block entry/exit { int x = … ; • Inner declaration of x hides outer one. • Heap contains data of varying lifetime { int y = … ; • Called “hole in scope” • Environment pointer points to current stack position { int x = … ; • Lifetime of outer x includes time when – Block entry: add new activation record to stack …. inner block is executed – Block exit: remove most recent activation record }; • Lifetime ≠ scope }; • Lines indicate “contour model” of scope. }; In-line Blocks Activation record for in-line block � Control link � Activation record Control link • pointer to previous record • Data structure stored on run-time stack Local variables on stack • Contains space for local variables Intermediate results � Push record on stack: � Example • Set new control link to Control link Push record with space for x, y { int x=0; point to old env ptr Set values of x, y Local variables int y=x+1; • Set env ptr to new record Push record for inner block { int z=(x+y)*(x-y); Intermediate results � Pop record off stack Set value of z }; Pop record for inner block • Follow control link of Environment }; Pop record for outer block current record to reset Pointer environment pointer May need space for variables and intermediate results like (x+y), (x-y ) Can be optimized away, but assume not for purpose of discussion. Example Scoping rules { int x=0; � Global and local variables Control link int y=x+1; • x, y are local to outer block { int x=0; x 0 { int z=(x+y)*(x-y); • z is local to inner bock int y=x+1; y 1 }; • x, y are global to inner block { int z=(x+y)*(x-y); }; }; Control link }; z -1 � Static scope Push record with space for x, y Set values of x, y x+y 1 • global refers to declaration in closest enclosing block Push record for inner block x-y -1 � Dynamic scope Set value of z • global refers to most recent activation record Pop record for inner block Environment Pointer Pop record for outer block These are same until we consider function calls. 2
Functions and procedures Activation record for function � Syntax of procedures (Algol) and functions (C) � Return address Control link procedure P (<pars>) <type> function f(<pars>) • Location of code to Return address begin { execute on function return Return-result addr <local vars> <local vars> � Return-result address <proc body> <function body> Parameters • Address in activation end; }; record of calling block to Local variables receive return address � Activation record must include space for Intermediate results � Parameters • parameters • location to put return • Locations to contain data value on function exit • return address Environment from calling block • return value Pointer (an intermediate result) Example Function call fact(k) � Function fact(3) Control link Control link Control link Return-result addr fact(n) = if n<= 1 then 1 Return-result addr Return address n 3 n k else n * fact(n-1) fact(n-1) fact(n-1) Return result addr � Return result address fact(2) Control link Parameters • location to put fact(n) Environment Return-result addr Pointer � Parameter Local variables n 2 fact(n-1) • set to value of n by calling Intermediate results sequence fact(1) Control link fact(n) = if n<= 1 then 1 � Intermediate result Return-result addr else n * fact(n-1) Environment n 1 • locations to contain value Pointer fact(n-1) of fact(n-1) Return address omitted; would be ptr into code segment Function return next slide → Function return Topics for first-order functions fact(3) fact(3) � Parameter passing Control link Control link Return result addr Return result addr • use ML reference cells to describe pass-by-value, n 3 n 3 pass-by-reference fact(n-1) fact(n-1) 2 � Access to global variables fact(2) fact(2) Control link Control link • global variables are contained in an activation record Return result addr Return result addr higher “up” the stack n 2 n 2 � Tail recursion fact(n-1) 1 fact(n-1) 1 • an optimization for certain recursive functions fact(1) Control link Return result addr fact(n) = if n<= 1 then 1 n 1 else n * fact(n-1) fact(n-1) See this yourself: write factorial and run under debugger 3
ML imperative features ML examples � General terminology: L-values and R-values � Create cell and change contents • Assignment y := x+3 x val x = ref “Bob”; Bob Bill – Identifier on left refers to location, called its L-value x := “Bill”; – Identifier on right refers to contents, called R-value � Create cell and increment � ML reference cells and assignment y val y = ref 0; 0 1 • Different types for location and contents y := !y + 1; x : int non-assignable integer value � While loop y : int ref location whose contents must be integer !y the contents of cell y val i = ref 0; ref x expression creating new cell initialized to x while !i < 10 do i := !i +1; • ML form of assignment !i; y := x+3 place value of x+3 in location (cell) y y := !y + 3 add 3 to contents of y and store in location y Parameter passing Example pseudo-code Standard ML � Pass-by-reference pass-by-ref fun f (x : int ref) = • Caller places L-value (address) ( x := !x+1; !x ); of actual parameter in activation record y = ref 0 : int ref; • Function can assign to variable that is passed function f (x) = f(y) + !y; � Pass-by-value { x := x+1; return x }; var y : int = 0; • Caller places R-value (contents) print f(y)+y; of actual parameter in activation record fun f (z : int) = pass-by-value • Function cannot change value of caller’s variable let x = ref z in x := !x+1; !x • Reduces aliasing (alias: two names refer to same loc) end; y = ref 0 : int ref; f(!y) + !y; Access to global variables Activation record for static scope � Control link � Two possible scoping conventions Control link • Link to activation record of • Static scope: refer to closest enclosing block Access link previous (calling) block • Dynamic scope: most recent activation record on stack � Access link Return address � Example • Link to activation record of Return result addr outer block x 1 closest enclosing block in int x=1; Parameters program text function g(z) = x+z; f(3) y 3 Local variables function f(y) = � Difference x 4 { int x = y+1; Intermediate results • Control link depends on return g(y*x) }; dynamic behavior of prog g(12) z 12 Environment f(3); Pointer • Access link depends on Which x is used for expression x+z ? static form of program text 4
Recommend
More recommend