Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Similar documents
Scope, Functions, and Storage Management

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

INF3110 Programming Languages Runtime Organization part II

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Procedure and Object- Oriented Abstraction

Concepts Introduced in Chapter 7

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Topic 7: Activation Records

Control in Sequential Languages

Today's Topics. CISC 458 Winter J.R. Cordy

Implementing Subprograms

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Lecture08: Scope and Lexical Address

CS 345. Functions. Vitaly Shmatikov. slide 1

Names, Bindings, Scopes

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

G Programming Languages - Fall 2012

Weeks 6&7: Procedures and Parameter Passing

CS 415 Midterm Exam Spring 2002

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

Recap: Functions as first-class values

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Variables and Bindings

DO NOT OPEN UNTIL INSTRUCTED

G Programming Languages - Fall 2012

CS 345. Exceptions. Vitaly Shmatikov. slide 1

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Topic IV. Block-structured procedural languages Algol and Pascal. References:

CS558 Programming Languages. Winter 2013 Lecture 3

INF3110 Programming Languages Runtime Organization part II

Intermediate Representations & Symbol Tables

The Procedure Abstraction

Some Advanced ML Features

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CA Compiler Construction

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments - 2

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Run-time Environments

Run-time Environments

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

NOTE: Answer ANY FOUR of the following 6 sections:

Types, Type Inference and Unification

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Begin at the beginning

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Subprograms, Subroutines, and Functions

Qualifying Exam in Programming Languages and Compilers

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

The Procedure Abstraction Part I: Basics

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Compilers and computer architecture: A realistic compiler to MIPS

CS 314 Principles of Programming Languages. Lecture 13

The PCAT Programming Language Reference Manual

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

CSC 2400: Computer Systems. Using the Stack for Function Calls

The SPL Programming Language Reference Manual

Run-time Environments - 3

CS558 Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Midterm II CS164, Spring 2006

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Types and Type Inference

Implementing Subprograms

Run-Time Environments

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Systems I. Machine-Level Programming V: Procedures

CS558 Programming Languages

Compiler Construction

Names, Scope, and Bindings

CS 314 Principles of Programming Languages

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CS 242. Fundamentals. Reading: See last slide

System Software Assignment 1 Runtime Support for Procedures

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Fundamentals of Programming Session 13

Compiler Construction

To figure this out we need a more precise understanding of how ML works

CSE 307: Principles of Programming Languages

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Lectures Basics in Procedural Programming: Machinery

Compilers and Code Optimization EDOARDO FUSELLA

Programmiersprachen (Programming Languages)

Programming Languages Third Edition. Chapter 7 Basic Semantics

Parsing Scheme (+ (* 2 3) 1) * 1

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Transcription:

CS 242 2007 Scope, Function Calls and Storage Management John Mitchell Announcements Homework Returned in class Wed; afterwards in 4 th Floor Gates Cabinet 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 Homework graders Please send email cs242@cs if you would like to try this Free pizza or Chinese food, plus chance to talk with us Hourly pay for eligible students Midterm exam Wed 10/24, 7 9PM, Room TBA See web site for calendar Topics Block structured languages and stack storage In line Blocks activation records storage for local, global variables First order functions parameter passing tail recursion and iteration Higher order functions deviations from stack discipline language expressiveness => implementation complexity Block Structured Languages Nested blocks, local variables Example { int x = 2; { int y = 3; inner block x = y+2; outer block new variables declared in nested blocks local l variable global variable Storage management Enter block: allocate space for variables Eit bl k ll b d ll t d Examples Simplified Machine Model Blocks in common languages C, JavaScript * { Algol begin end ML let in end Two forms of blocks In line blocks Blocks associated with functions or procedures Topic: block based memory management, to local variables, parameters, global variables Registers Program Counter Data Stack Heap * JavaScript functions provide blocks 1

Interested in Memory Mgmt Only Registers, segment, Program Ignore registers Details of instruction set will not matter Data Segment Stack contains data related to block entry/exit Heap contains data of varying lifetime pointer points to current stack position Block entry: add new activation record to stack Block exit: remove most recent activation record Some basic concepts Scope Region of program text where declaration is visible Lifetime Period of time when location is allocated to program { int x = ; { int y = ; { int x = ;. ; ; ; 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. In line Blocks Activation record Data structure stored on run time stack Contains space for local variables 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 May need space for variables and intermediate results like (x+y), (x-y) Activation record for in line block pointer to previous record on stack Push record on stack: Set new link to point to old env ptr Set env ptr to new record Pop record off stack Follow link of current record to reset environment pointer Can be optimized away, but assume not for purpose of discussion. Example Scoping rules { 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 x 0 y 1 z -1 x+y 1 x-y -1 Global and local variables x, y are local to outer block z is local to inner bock x, y are global to inner block { int x=0; int y=x+1; { int z=(x+y)*(x-y); Static scope global refers to declaration in closest enclosing block Dynamic scope global refers to most recent activation record These are same until we consider function calls. ; ; 2

Functions and procedures Activation record for function Syntax of procedures (Algol) and functions (C) procedure P (<pars>) <type> function f(<pars>) begin { <local vars> <local vars> <proc body> <function body> end; Activation record must include space for parameters return address ue (an intermediate result) location to put return value on function exit Return address Parameters Return address Location of code to execute on function return Return result address Address in activation record of calling block to receive return address Parameters Locations to contain data from calling block Example Function call Return address Parameters Function fact(n) = if n<= 1 then 1 else n * fact(n 1) ess location to put fact(n) Parameter set to value of n by calling sequence Intermediate result locations to contain value of fact(n 1) fact(k) n fact(n) = if n<= 1 then 1 else n * Return address omitted; would be ptr into code segment k fact(3) fact(2) fact(1) n 3 n 2 n 1 Function return next slide Function return Topics for first order functions fact(3) fact(2) fact(1) n 3 n 2 1 n 1 fact(3) n 2 fact(2) n 2 1 fact(n) = if n<= 1 then 1 else n * 3 Parameter passing use ML reference cells to describe pass by value, passby reference Access to global variables globalvariables are contained in an activation record higher up the stack Tail recursion an optimization for certain recursive functions See this yourself: write factorial and run under debugger 3

ML imperative features General 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 ML reference cells and assignment Different types for location and contents x : int non assignable integer value y : int ref location whose contents must be integer!y the contents of cell y ref x expression creating new cell initialized to x 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 ML examples Create cell and change contents val x = ref Bob ; x := Bill ; Create cell and increment val y = ref 0; y :=!y + 1; While loop vali= ref 0; while!i < 10 do i:=!i +1;!i; x y Bob Bill 01 Parameter passing Pass by reference Caller places L value (address) of actual parameter in activation record Function can assign to variable that is passed Pass by value Caller places R value (contents) of actual parameter in activation record Function cannot change value of caller s variable Reduces aliasing (alias: two names refer to same loc) pseudo-code function f (x) = { x = x+1; return x; var y = 0; print (f(y)+y); Example Standard ML fun f (x : int ref) = ( x :=!x+1;!x ); y = ref 0 : int ref; f(y) +!y; fun f (z : int) = let x = ref z in x :=!x+1;!x end; y = ref 0 : int ref; f(!y) +!y; Access to global variables Activation record for static scope Two possible scoping conventions Static scope: refer to closest enclosing block Dynamic scope: most recent activation record on stack Example var x=1; function g(z) { return x+z; function f(y) { var x = y+1; return g(y*x); f(3); outer block f(3) g(12) z 12 Which x is used for expression x+z? Access link Return address Parameters Link to activation record of previous (calling) block Access link Link to activation record of closest enclosing block in program text Difference depends on dynamic behavior of prog Access link depends on static form of program text 4

Complex nesting structure Static scope with links 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 var x=1; function g(z) { return x+z; function f(y) { var x = y+1; return g(y*x); f(3); Simplified code has same block nesting, if we follow convention that each declaration begins a new block. outer block var x=1; function g(z) = { return x+z; function f(y) = { var x = y+1; return g(y*x); f(3); Use 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 f(3) g(12) link link g link link f link link link link z 12 Tail recursion (first order case) Example Calculate least power of 2 greater than y Function g makes a tail call to function f if Return value of function f is ue of g Example tail call not a tail call fun g(x) = if x>0 then f(x) else f(x)*2 Optimization Can pop activation record on a tail call Especially useful for recursive tail call next activation record has exactly same form f(1,3) fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; x 2 Optimization Set return value address to that of caller Question Can we do the same with link? Optimization avoid return to caller Tail recursion elimination Tail recursion and iteration f(1,3) f(2,3) f(4,3) f(1,3) f(2,3) f(4,3) x 2 x 2 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3); Optimization pop followed by push = reuse activation record in place Conclusion Tail recursive function equiv to iterative loop fun f(x,y) = if x>y then x else f(2*x, y); f(1,y); initial value test loop body function g(y) { var x = 1; while (!x>y) x = 2*x; return x; 5

Higher Order Functions Language features Functions passed as arguments Functions that return functions from nested blocks Need to maintain environment of function Simpler case Function passed as argument Need pointer to activation record higher up in stack More complicated second case Function returned as result of function call Need to keep activation record of returning function Pass function as argument val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; { var x = 4; { function f(y) {return x*y; { function g(h) { var x = 7; return h(3) + x; There are two declarations of x Which one is used for each occurrence of x? Static Scope for Function Argument Static Scope for Function Argument val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f) h(3) follow link f g h x 7 x * y How is link for h(3) set? local var for f for g { var x = 4; { function f(y) {return x*y; { function g(h) { int x=7; return h(3) + x; g(f) h(3) follow link f g h x 7 x * y How is link for h(3) set? local var for f for g Result of function call Closures Function value is pair closure = env, code When a function represented by a closure is called, Allocate activation record for call (as always) Set the link in the activation record using the environment pointer from the closure 6

Function Argument and Closures Function Argument and Closures val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f) h(3) Run-time stack with links f g h x 7 link set from closure for f for g Run-time stack with links { var x = 4; { function f(y){return x*y; { function g(h) { for f int x=7; f return h(3)+x; g g(f) for g h x 7 h(3) link set from closure Summary: Function Arguments Use closure to maintain a pointer to the static environment of a function body When called, set link from closure All 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 Language feature Functions that return new functions Need to maintain environment of function Example fun compose(f,g) = (fn x => g(f x)); Function 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_ (init : int) = let val count = ref init fun (inc:int) = (count :=!count + inc;!count) in end; val c = mk_(1); c(2) + c(2); Function to make returns a closure How is correct value of count determined in c(2)? Example: Return fctn with private state {int int mk_ (int init) { int count = init; int (int inc) { return count += inc; return int int c = mk_(1); print c(2) + c(2); Function to make returns a closure How is correct value of count determined in call c(2)? 7

Function Results and Closures Function Results and Closures fun mk_ (init : int) = let val count = ref init fun (inc:int) = (count :=!count + inc;!count) in end end; val c = mk_(1); c(2) + c(2); mk_c c for mk_ {int int mk_ (int init) { int count = init; int (int inc) { return count+=inc; int int c = mk_(1); mk_c print c(2) + c(2); c for mk_ mk_(1) init 1 count 13 mk_(1) init 1 count 13 c(2) Call changes cell value from 1 to 3 inc 2 for c(2) Call changes cell value from 1 to 3 inc 2 for Closures in Web programming Useful 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); pointer lets the button s click handler find the message to display Summary: Return Function Results Use closure to maintain static environment May need to keep activation records after return Stack (lifo) order fails! Possible 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 Block structured lang uses stack of activ records Activation records contain parameters, local vars, Also pointers to enclosing scope Several different parameter passing mechanisms Tail calls may be optimized Function 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 8