scope function calls and storage management
play

Scope, Function Calls and Storage Management Friday Oct 17 No - PDF document

CS 242 Revised class schedule Scope, Function Calls and Storage Management Friday Oct 17 No lecture; discussion section as usual Friday Oct 24 No section John Mitchell Monday Oct 27 Review section during class meeting


  1. CS 242 Revised class schedule Scope, Function Calls and Storage Management � Friday Oct 17 • No lecture; discussion section as usual � Friday Oct 24 • No section John Mitchell � Monday Oct 27 • Review section during class meeting time, Gates B01 � Wednesday Oct 29 • No lecture • Evening exam: 7PM, Gates B01 and B03 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 block x = y+ 2; � 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 � Blocks in common languages Registers Code Data • C { … } • Algol begin … end Stack • ML let … in … end � Two forms of blocks • I n-line blocks Program • Blocks associated with functions or procedures Counter � Topic: block-based memory management, Heap Environment Pointer access to local variables, parameters,global vars 1

  2. Interested in Memory Mgmt Only Some basic concepts � Registers, Code segment, Program counter � Scope • Ignore registers • Region of program text where declaration is visible � Lifetime • Details of instruction set will not matter � 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 � Activation record � Control link Control link • Data structure stored on run -time stack • pointer to previous record Local variables • Contains space for local variables on stack Intermediate results � Example � Push record on stack: • Set new control link to Push record with space for x, y Control link { int x= 0; point to old env ptr Set values of x, y int y= x+ 1; Local variables Push record for inner block • Set env ptr to new record { int z= (x+ y)* (x-y); Intermediate results Set value of z � Pop record off stack }; Pop record for inner block • Follow control link of } ; Pop record for outer block Environment current record to reset Pointer environment pointer May need space for variables and intermediate results like (x+ y), (x-y ) Example Scoping rules � Global and local variables { int x= 0; 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 } ; � Static scope z -1 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 Pop record for outer block Pointer These are same until we consider function calls. 2

  3. 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 Parameters < proc body> < function body> • Address in activation end; } ; record of calling block to Local variables � Activation record must include space for receive return address 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 Environment • location to put fact(n) Return-result addr Pointer � Parameter Local variables n 2 fact(n-1) • set to value of n by calling Intermediate results fact(1) sequence 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 Function return next slide → be ptr into code segment Function return Topics for first-order functions � Parameter passing fact(3) fact(3) 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 See this yourself: write factorial and run under debugger else n * fact(n-1) fact(n-1) 3

  4. ML imperative features (review) Parameter passing � General terminology: L-values and R-values � Pass-by-reference • Assignment y := x+ 3 • Caller places L-value (address) – Identifier on left refers to location, called its L-value of actual parameter in activation record – Identifier on right refers to contents, called R-value • Function can assign to variable that is passed � ML reference cells and assignment � Pass-by-value • Different types for location and contents • Caller places R-value (contents) x : int non-assignable integer value y : int ref location whose contents must be integer of actual parameter in activation record !y the contents • Function cannot change value of caller’s variable ref x expression creating new cell initialized to x • Reduces aliasing (alias: two names refer to same loc) • ML form of assignment 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 Example Access to global variables pseudo-code Standard ML � Two possible scoping conventions fun f (x : int ref) = e f r - y • Static scope: refer to closest enclosing block b s - ( x := !x+ 1; !x ); s a p y = ref 0 : int ref; • Dynamic scope: most recent activation record on stack f(y) + !y; � Example function f (x) = { x := x+ 1; return x }; var y : int = 0; outer block x 1 int x= 1; print f(y)+ y; function g(z) = x+ z; y 3 fun f (z : int ) = f(3) pass-by-value function f(y) = let x = ref z in x 4 x := !x+ 1; !x { int x = y+1; end; return g(y* x) } ; g(12) z 12 y = ref 0 : int ref; f(3); f(!y) + !y; Which x is used for expression x+ z ? Activation record for static scope Complex nesting structure � Control link function m(…) { Control link int x= 1; • Link to activation record of … int x= 1; Access link previous (calling) block function n( … ){ function g(z) = x+ z; � Access link Return address function g(z) = x+ z; function f(y) = Simplify to … • Link to activation record of { int x = y+ 1; Return result addr { … closest enclosing block in return g(y* x) } ; Parameters function f(y) { f(3); program text Local variables int x = y+1; � Difference return g(y* x) } ; Intermediate results • Control link depends on … dynamic behavior of prog Simplified code has same block nesting, f(3); … } Environment if we follow convention that each • Access link depends on Pointer … n( … ) … } static form of program text declaration begins a new block. … m(…) 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend