Scope, Function Calls and Storage Management John Mitchell CS 2422008.

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.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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:
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
Dr. Muhammed Al-Mulhem ICS Chapter 7 Scope, Functions, and Storage Management.
Runtime Environments Source language issues Storage organization
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
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.
Scope, Function Calls and Storage Management John Mitchell CS
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
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.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
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.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Slide 1 Vitaly Shmatikov CS 345 Imperative Programming.
Basic Semantics Associating meaning with language entities.
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
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.
COMP3190: Principle of Programming Languages
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.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Slide 1 WEEK 8 Imperative Programming Original by Vitaly Shmatikov.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Implementing Subprograms
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
ISBN Chapter 10 Implementing Subprograms.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
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?
Chapter 14 Functions.
Implementing Subprograms Chapter 10
Implementing Subprograms
Run-time organization
Run-Time Storage Organization
Understanding Program Address Space
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Scope, Function Calls and Storage Management
Where is all the knowledge we lost with information? T. S. Eliot
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Scope, Function Calls and Storage Management John Mitchell CS

Announcements uHomework First homework posted to web today Due next Wed 5PM, location to be announced on web site –SCPD students: attach routing slip, will be returned by courier Late policy - Three late days, but only one at a time –You may turn in by 5PM Thursday three times in the quarter uHomework graders Please send if you would like to try this –Free pizza or Chinese food, plus chance to talk with us –Hourly pay for eligible students uMidterm exam Wed 10/22, 7-9PM, Room TBA Final: Wed Dec 10, 12:15 PM (corrected on web site) See web site for calendar

Topics uBlock-structured languages and stack storage uIn-line Blocks activation records storage for local, global variables uFirst-order functions parameter passing tail recursion and iteration uHigher-order functions deviations from stack discipline language expressiveness => implementation complexity

Block-Structured Languages uNested blocks, local variables Example { int x = 2; { int y = 3; x = y+2; } Storage management –Enter block: allocate space for variables –Exits block: some or all space may be deallocated new variables declared in nested blocks inner block outer block local variable global variable

Examples uBlocks in common languages C, JavaScript * { … } Algol begin … end ML let … in … end uTwo forms of blocks In-line blocks Blocks associated with functions or procedures uTopic: block-based memory management, access to local variables, parameters, global variables * JavaScript functions provide blocks

Simplified Machine Model Registers Environment Pointer Program Counter DataCode Heap Stack

Interested in Memory Mgmt Only uRegisters, Code segment, Program counter Ignore registers Details of instruction set will not matter uData Segment Stack contains data related to block entry/exit Heap contains data of varying lifetime Environment pointer points to current stack position –Block entry: add new activation record to stack –Block exit: remove most recent activation record

Some basic concepts uScope Region of program text where declaration is visible uLifetime Period of time when location is allocated to program Inner declaration of x hides outer one. Called “hole in scope” Lifetime of outer x includes time when inner block is executed Lifetime  scope Lines indicate “contour model” of scope. { int x = … ; { int y = … ; { int x = … ; …. };

In-line Blocks uActivation record Data structure stored on run-time stack Contains space for local variables uExample May need space for variables and intermediate results like (x+y), (x-y ) { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block

Activation record for in-line block uControl link pointer to previous record on stack uPush record on stack: Set new control link to point to old env ptr Set env ptr to new record uPop record off stack Follow control link of current record to reset environment pointer Control link Local variables Intermediate results Control link Local variables Intermediate results Environment Pointer Can be optimized away, but assume not for purpose of discussion.

Example { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block Control link x y 0 1 x+y x-y Environment Pointer 1 Control link z

Scoping rules uGlobal and local variables { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; x, y are local to outer block z is local to inner bock x, y are global to inner block uStatic scope global refers to declaration in closest enclosing block uDynamic scope global refers to most recent activation record These are same until we consider function calls.

Functions and procedures uSyntax of procedures (Algol) and functions (C) procedure P ( ) function f( ) begin { end; } uActivation record must include space for parameters return address local variables, intermediate results return value (an intermediate result) location to put return value on function exit

Activation record for function uReturn address Location of code to execute on function return uReturn-result address Address in activation record of calling block to receive return address uParameters Locations to contain data from calling block Control link Local variables Intermediate results Environment Pointer Parameters Return address Return-result addr

Example uFunction fact(n) = if n<= 1 then 1 else n * fact(n-1) Return result address location to put fact(n) uParameter set to value of n by calling sequence uIntermediate result locations to contain value of fact(n-1) Control link Local variables Intermediate results Environment Pointer Parameters Return address Return result addr

Control link fact(n-1) n Return-result addr 3 fact(3) Function call Return address omitted; would be ptr into code segment Control link fact(n-1) n Return-result addr 2 fact(2) fact(n) = if n<= 1 then 1 else n * fact(n-1) Control link fact(n-1) n Return-result addr k fact(k) Environment Pointer Control link fact(n-1) n Return-result addr 1 fact(1) Function return next slide 

Function return Control link fact(n-1) n Return result addr 3 fact(3) Control link fact(n-1) n Return result addr 1 2 fact(2) Control link fact(n-1) n Return result addr 1 fact(1) fact(n) = if n<= 1 then 1 else n * fact(n-1) Control link fact(n-1) n Return result addr 2 3 fact(3) Control link fact(n-1) n Return result addr 1 2 fact(2)

Topics for first-order functions uParameter passing pass-by-value: copy value to new activation record pass-by-reference: copy ptr to new activation record uAccess to global variables global variables are contained in an activation record higher “up” the stack uTail recursion an optimization for certain recursive functions See this yourself: write factorial and run under debugger

Parameter passing uGeneral terminology: L-values and R-values Assignment y := x+3 –Identifier on left refers to location, called its L-value –Identifier on right refers to contents, called R-value uPass-by-reference Place L-value (address) in activation record Function can assign to variable that is passed uPass-by-value Place R-value (contents) in activation record Function cannot change value of caller’s variable Reduces aliasing (alias: two names refer to same loc)

Example function f (x) = { x = x+1; return x; } var y = 0; print (f(y)+y); pseudo-codeactivation records pass-by-ref pass-by-value f(y) y0 Control link x Return result addr  f(y) y0 Control link x Return result addr 0 f(y)

Access to global variables uTwo possible scoping conventions Static scope: refer to closest enclosing block Dynamic scope: most recent activation record on stack uExample var x=1; function g(z) { return x+z; } function f(y) { var x = y+1; return g(y*x); } f(3); x1 x4 y3 z12 outer block f(3) g(12) Which x is used for expression x+z ?

Activation record for static scope uControl link Link to activation record of previous (calling) block uAccess link Link to activation record of closest enclosing block in program text uDifference Control link depends on dynamic behavior of prog Access link depends on static form of program text Control link Local variables Intermediate results Environment Pointer Parameters Return address Return result addr Access link

Complex nesting structure var x=1; function g(z) { return x+z; } function f(y) { var x = y+1; return g(y*x); } f(3); function m(…) { var x=1; … function n( … ){ function g(z) { return x+z; } … { … function f(y) { var x = y+1; return g(y*x); } … f(3); … } … n( … ) …} … m(…) Simplify to Simplified code has same block nesting, if we follow convention that each declaration begins a new block.

Static scope with access links var x=1; function g(z) = { return x+z; } function f(y) = { var x = y+1; return g(y*x); } f(3); x1 x4 y3 z12 outer block f(3) g(12) control link access link g…f… control link access link control link access link control link Use access link to find global variable: –Access link is always set to frame of closest enclosing lexical block –For function body, this is block that contains function declaration

Tail recursion (first-order case) uFunction g makes a tail call to function f if Return value of function f is return value of g uExample fun g(x) = if x>0 then f(x) else f(x)*2 uOptimization Can pop activation record on a tail call Especially useful for recursive tail call –next activation record has exactly same form tail callnot a tail call

Example Calculate least power of 2 greater than y fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; control return val x1 y3 control return val x1 y3 control return val x2 y3 control return val x4 y3 f(1,3) Optimization Set return value address to that of caller Question Can we do the same with control link? Optimization avoid return to caller

Tail recursion elimination fun f(x,y) = if x>y then x else f(2*x, y); f(1,3); control return val x1 y3 f(4,3) Optimization pop followed by push = reuse activation record in place Conclusion Tail recursive function equiv to iterative loop control return val x2 y3 f(1,3) control return val x4 y3 f(2,3)

Tail recursion and iteration fun f(x,y) = if x>y then x else f(2*x, y); f(1,y); control return val x1 y3 f(4,3) control return val x2 y3 f(1,3) control return val x4 y3 f(2,3) function g(y) { var x = 1; while (!x>y) x = 2*x; return x; } initial value loop body test

Higher-Order Functions uLanguage features Functions passed as arguments Functions that return functions from nested blocks Need to maintain environment of function uSimpler case Function passed as argument Need pointer to activation record “higher up” in stack uMore complicated second case Function returned as result of function call Need to keep activation record of returning function

Pass function as argument int x = 4; fun f(y) = x*y; fun g(h) = let int x=7 in h(3) + x; g(f); There are two declarations of x Which one is used for each occurrence of x? { var x = 4; { function f(y) {return x*y}; { function g(h) { var x = 7; return h(3) + x; }; g(f); } } } Haskell Pseudo-JavaScript

Static Scope for Function Argument int x = 4; fun f(y) = x*y; fun g(h) = let int x=7 in h(3) + x; g(f); x4hy3fg Code for f Code for g g(f) h(3) x * y x7 follow access link local var How is access link for h(3) set?

Static Scope for Function Argument { var x = 4; { function f(y) {return x*y}; { function g(h) { int x=7; return h(3) + x; }; g(f); } } } x4hy3fg Code for f Code for g g(f) h(3) x * y x7 follow access link local var How is access link for h(3) set?

Result of function call

Closures uFunction value is pair closure =  env, code  uWhen a function represented by a closure is called, Allocate activation record for call (as always) Set the access link in the activation record using the environment pointer from the closure

Function Argument and Closures int x = 4; fun f(y) = x*y; fun g(h) = let int x=7 in h(3) + x; g(f); x4 access link set from closure Code for f f access Run-time stack with access links Code for g h(3) y3 access g(f) h access x7 g

{ var x = 4; { function f(y){return x*y}; { function g(h) { int x=7; return h(3)+x; }; g(f); }}} Function Argument and Closures x4 access link set from closure Code for f f access Run-time stack with access links Code for g h(3) y3 access g(f) h access x7 g

Summary: Function Arguments uUse closure to maintain a pointer to the static environment of a function body uWhen called, set access link from closure uAll access links point “up” in stack May jump past activ records to find global vars Still deallocate activ records using stack (lifo) order

Return Function as Result uLanguage feature Functions that return “new” functions Need to maintain environment of function uExample function compose(f,g) {return function(x) { return g(f (x)) }}; uFunction “created” dynamically expression with free variables values are determined at run time function value is closure =  env, code  code not compiled dynamically (in most languages)

Example: Return fctn with private state fun mk_counter (init : int) = let val count = ref init fun counter(inc:int) = (count := !count + inc; !count) in counter end; val c = mk_counter(1); c(2) + c(2); Function to “make counter” returns a closure How is correct value of count determined in c(2) ? ML

Example: Return fctn with private state function mk_counter (init) { var count = init; function counter(inc) {count=count+inc; return count}; return counter}; var c = mk_counter(1); c(2) + c(2); Function to “make counter” returns a closure How is correct value of count determined in call c(2) ? JS

Function Results and Closures fun mk_counter (init : int) = let val count = ref init fun counter(inc:int) = (count := !count + inc; !count) in counter end end; val c = mk_counter(1); c(2) + c(2); c access Code for counter Code for mk_counter c(2) access inc2 1 mk_counter(1) count init1 access counter mk_c Call changes cell value from 1 to 3 3 ML

Function Results and Closures c access Code for counter Code for mk_counter c(2) access inc2 mk_counter(1) count1 init1 access counter mk_c function mk_counter (init) { var count = init; function counter(inc) {count=count+inc; return count}; return counter}; var c = mk_counter(1); c(2) + c(2); JS 3

Closures in Web programming uUseful for event handlers in Web programming: function AppendButton(container, name, message) { var btn = document.createElement('button'); btn.innerHTML = name; btn.onclick = function (evt) { alert(message); } container.appendChild(btn); } uEnvironment pointer lets the button’s click handler find the message to display

Summary: Return Function Results uUse closure to maintain static environment uMay need to keep activation records after return Stack (lifo) order fails! uPossible “stack” implementation Forget about explicit deallocation Put activation records on heap Invoke garbage collector as needed Not as totally crazy as is sounds May only need to search reachable data

Summary of scope issues uBlock-structured lang uses stack of activ records Activation records contain parameters, local vars, … Also pointers to enclosing scope uSeveral different parameter passing mechanisms uTail calls may be optimized uFunction parameters/results require closures Closure environment pointer used on function call Stack deallocation may fail if function returned from call Closures not needed if functions not in nested blocks