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

Similar documents
Functional Programming - 2. Higher Order Functions

Concepts Introduced in Chapter 7

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

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

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Chapter 10. Implementing Subprograms

NOTE: Answer ANY FOUR of the following 6 sections:

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

G Programming Languages - Fall 2012

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

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lecture 7: Binding Time and Storage

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

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

Scope, Functions, and Storage Management

G Programming Languages - Fall 2012

Special Topics: Programming Languages

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

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Chapter 10. Implementing Subprograms ISBN

Implementing Subprograms

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

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

Run Time Environments

Runtime Environments I. Basilio B. Fraguela

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

CS 314 Principles of Programming Languages

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Project 2: Scheme Interpreter

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Vectors in Scheme. Data Structures in Scheme

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

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Chapter 5. Names, Bindings, and Scopes

Types II. Hwansoo Han

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Chapter 5 Names, Binding, Type Checking and Scopes

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

CS 314 Principles of Programming Languages

Memory Management and Run-Time Systems

CS 314 Principles of Programming Languages

Lecture08: Scope and Lexical Address

CS 314 Principles of Programming Languages. Lecture 13

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Functional Programming. Pure Functional Programming

CIT Week13 Lecture

CA Compiler Construction

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

6. Names, Scopes, and Bindings

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

CSE 504: Compiler Design. Runtime Environments

Run-time Environments

LECTURE 14. Names, Scopes, and Bindings: Scopes

Run-time Environments

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

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

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

Run-Time Environments

Run-Time Data Structures

Implementing Subprograms

Programming Languages: Lecture 12

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Lecture 9: Procedures & Functions. CS 540 George Mason University

Heap storage. Dynamic allocation and stacks are generally incompatible.

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

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

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

MIDTERM EXAM (Solutions)

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Chapter 3:: Names, Scopes, and Bindings

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

CSE 307: Principles of Programming Languages

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Fall 2003

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

THEORY OF COMPILATION

Runtime Support for Algol-Like Languages Comp 412

CMSC 331 Final Exam Section 0201 December 18, 2000

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

Procedure and Object- Oriented Abstraction

Lexical Considerations

Programming Languages

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

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

low and not larger than high. 18 points

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

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

Weeks 6&7: Procedures and Parameter Passing

Names and Abstractions: What s in a Name?

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

Run-time Environments - 2

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

Run-Time Environments

Programming Languages

Transcription:

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1

Review Functions as first-class objects What can you do with an integer? Use as an argument to a function Return as value of a function Write a constant What can you do with a function? Use as an argument to a function Return as value of a function Write a constant higher order functions CS 314, LS,BR,LTM: Scope and Memory 2

map Higher order function used to apply another function to every element of a list Takes 2 arguments a function f and a list lst and builds a new list by applying the function to every element of the (argument) list (map abs (-1 2-3 -4)) returns (1 2 3 4) (map (lambda (x) (+ 1 x)) (1 2 3)) returns (2 3 4) CS 314, LS,BR,LTM: Scope and Memory 3

Using map Define atomcnt3 which uses map to calculate the number of atoms in a list. atomcnt3 creates a list of the count of atoms in every sublist and apply of + calculates the sublist sum. (define (atomcnt3 s) (cond ((atom? s) 1) (else (apply + (map atomcnt3 s))))) (atomcnt3 (1 2 3)) returns 3 (atomcnt3 ((a b) d)) returns 3 (atomcnt3 (1 ((2) 3) (((3) (2) 1)))) returns 6 How does this function work? CS 314, LS,BR,LTM: Scope and Memory 4

apply apply is a built-in function whose first argument f is a function and whose second argument ll is a list of arguments for that function evaluation of apply applies f to ll (apply + (1 2 3)) returns 6 (apply zero? 2) returns #f (apply (lambda (n) (+ 1 n)) (3)) returns 4 The power of apply is that it lets your program build an S- expression to evaluate during execution, and then lets it be evaluated. CS 314, LS,BR,LTM: Scope and Memory 5

A Common Recursive Pattern (define (sum lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))))) (define (prod lst) (if (null? lst) 1 (* (car lst) (sum (cdr lst))))) CS 314, LS,BR,LTM: Scope and Memory 6

foldr Higher order function that takes a binary, associative operation and uses it to roll-up a list (define (foldr op lst id) (if (null? lst) id (op (car lst) (foldr op (cdr lst) id)) )) (foldr + (10 20 30) 0) yields (+ 10 (foldr + (20 30) 0) ) (+ 10 (+ 20 (foldr + (30) 0) )) (+ 10 (+ 20 (+ 30 (foldr + () 0)))) (+ 10 (+ 20 (+ 30 0))) yields 60 CS 314, LS,BR,LTM: Scope and Memory 7

Reverse (define (reverse lst) (if (null? lst) () (append (reverse (cdr lst)) (list (car lst))))) (define (reverse lst) (foldr () lst (lambda (e l) (append l (list e))))) CS 314, LS,BR,LTM: Scope and Memory 8

The Power of Higher Order Can compose higher order functions to form compact powerful functions (define (sumfn f lst) (foldr + (map f lst) 0)) sumfn takes a function f and a list lst sum applies f to each element of the list and then sums the results (sumfn (lambda (x) (* 2 x)) (1 2 3)) yields 12 (sumfn square (2 3)) yields 13 CS 314, LS,BR,LTM: Scope and Memory 9

Scope The scope of a variable binding is the region of the program in which you can refer to that binding CS 314, LS,BR,LTM: Scope and Memory 10

Scope in Scheme (let* ((x 2)(y 3)(x 4)) (+ x y)) (let ((x 2)) (let ((y 3)) (let ((x 4)) (+ x y)))) What result? R 5? R 7? CS 314, LS,BR,LTM: Scope and Memory 11

Scope in Scheme ((lambda (x) ((lambda (y) ((lambda (x) (+ x y)) 4)) 3)) 2) What result? R 5? R 7? binding occurrences bound occurrences CS 314, LS,BR,LTM: Scope and Memory 12

Scope in Scheme (let ((x 2)) (let ((f (lambda (y) (+ x y)))) (let ((x 4)) (+ x (f 3))))) What result? R 9? R 11? R error message? CS 314, LS,BR,LTM: Scope and Memory 13

Scope in Scheme Consider: (lambda (y) (+ x y)) binding occurrence bound occurrence free occurrence Where does x get its value? From the outermost let, where x = 2? From the innermost let, where x = 4? CS 314, LS,BR,LTM: Scope and Memory 14

Dynamic Scope A variable has dynamic scope if: it can be referred to anywhere in the program, and at the time of reference, it gets whatever binding is currently in effect. (let ((x 2)) (let ((f (lambda (y) (+ x y)))) (let ((x 4)) (+ x (f 3))))) Under dynamic scope rules R 11 CS 314, LS,BR,LTM: Scope and Memory 15

Lexical Scope A variable has lexical scope if: it can only be referred to within the textual construct in which it was created, and it gets its binding from that context. (let ((x 2)) (let ((f (lambda (y) (+ x y)))) (let ((x 4)) (+ x (f 3))))) Under lexical scope rules R 9 CS 314, LS,BR,LTM: Scope and Memory 16

Closures Variables in Scheme are lexically scoped! In addition, their bindings persist for as long as the possibility of reference remains. > (define (adder x) (lambda (y) (+ x y))) > ((adder 3) 5) 8 > (map (adder 5) '(1 3 5 7 9)) (6 8 10 12 14) CS 314, LS,BR,LTM: Scope and Memory 17

Closures A closure is a function plus a binding environment in which it is to be evaluated. (adder 3) returns a closure in which x R 3 (adder 5) returns a closure in which x R 5 These are, in effect, different functions! When a closure is applied to its argument(s), the binding environment provides values for variables in the function definition. Thus ((adder 3) 5) is, in effect, equivalent to: (let ((x 3)) ((lambda (y) (+ x y)) 5)) CS 314, LS,BR,LTM: Scope and Memory 18

Scope in Pascal (Sethi( Sethi) CS 314, LS,BR,LTM: Scope and Memory 19

Scope in C (Sethi( Sethi) CS 314, LS,BR,LTM: Scope and Memory 20

Block-Structured Languages program Main { a,b,c: integer; procedure P { c: integer; procedure S { c,d: integer;...; @5;...}...; @4;...}...; @2;...} a: integer; procedure Q {...; @6;...}...; @3;...}...; @1;...} Examples: Algol, Pascal, Ada,... C has nested blocks, but not nested procedure declarations CS 314, LS,BR,LTM: Scope and Memory 21

Visibility of Identifiers program Main { a,b,c: integer; procedure P { c: integer; procedure S { c,d: integer;...; @5;...}...; @4;...}...; @2;...} a: integer; procedure Q {...; @6;...}...; @3;...}...; @1;...} Which identifiers are visible at location @1? @2? @3? @4? @5? @6? What is the Rule? CS 314, LS,BR,LTM: Scope and Memory 22

Visibility of Procedures program Main { a,b,c: integer; procedure P { c: integer; procedure S { c,d: integer;...; @5;...}...; @4;...}...; @2;...} a: integer; procedure Q {...; @6;...}...; @3;...}...; @1;...} Procedure Declaration Tree R S P Main R Q Can a procedure call itself? its children? its ancestors? its ancestors siblings? CS 314, LS,BR,LTM: Scope and Memory 23

Visibility of Procedures program Main { a,b,c: integer; procedure P { c: integer; procedure S { c,d: integer;...; @5;...}...; @4;...}...; @2;...} a: integer; procedure Q {...; @6;...}...; @3;...}...; @1;...} Which procedures are visible/callable at location @1? @2? @3? @4? @5? @6? What is the Rule? CS 314, LS,BR,LTM: Scope and Memory 24

Binding Times Static Bindings: Compile Time Link Time Load Time Dynamic Bindings: Run Time Lifetime or Extent of a Binding: When is a binding initiated? When is a binding terminated? CS 314, LS,BR,LTM: Scope and Memory 25

Memory Allocation Classical Layout: C O D E S T A T I C S T A C K free memory H E A P high Address Space low CS 314, LS,BR,LTM: Scope and Memory 26

Runtime Stack Used to manage block-structured storage, as well as procedure calls and returns. Stack contains frames of all blocks (or procedures) which have been entered but not yet exited. Frame contains: All information necessary to update stack and resume execution when a block (or procedure) is exited. Addresses of all local variables (and all parameters), encoded as an offset from the base address of the frame. CS 314, LS,BR,LTM: Scope and Memory 27

Heap Used for dynamically created objects whose lifetime may not coincide with procedure calls and returns. Allows allocation and deallocation of indeterminate sized blocks of memory during execution. Allocation and deallocation can be: Explicit (e.g., in Pascal, C, C++) Problems: dangling references, memory leaks,... Implicit (e.g., in Java, Scheme, Prolog) Requires: automatic garbage collection CS 314, LS,BR,LTM: Scope and Memory 28

Implementing Lexical Scope Use stack to manage nesting Stack frame includes: space for parameters, local variables, return value saved state of caller (registers, return address, etc.) control link (or dynamic link): return pointer to stack frame of caller access link (or static link): to access nonlocal variables caller s frame control link access link saved state of caller parameters, X =... local vars, C =... function result Stack Frame CS 314, LS,BR,LTM: Scope and Memory 29

Implementing Lexical Scope Prologue push new stack frame, reset CurrentFramePointer initialize fixed length fields evaluate argument expressions and place in parameters (if language uses call-by-value, e.g., Scheme, C, Java) Execute code in the called procedure or function Epilogue restore caller s registers pop stack, and reset CurrentFramePointer CS 314, LS,BR,LTM: Scope and Memory 30

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R Current Frame CS 314, LS,BR,LTM: Scope and Memory 31

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S <P> Access Link Static Link Control Link Dynamic Link CS 314, LS,BR,LTM: Scope and Memory 32

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S <P> <R> x: par a CS 314, LS,BR,LTM: Scope and Memory 33

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S <P> CS 314, LS,BR,LTM: Scope and Memory 34

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S c R <P> <S> CS 314, LS,BR,LTM: Scope and Memory 35

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S c R <P> <S> <R> x: par CS 314, LS,BR,LTM: Scope and Memory 36

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S c R <P> <S> CS 314, LS,BR,LTM: Scope and Memory 37

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R c S <P> CS 314, LS,BR,LTM: Scope and Memory 38

program Main { a,b,c: integer; procedure P { c: integer; procedure S { c: integer; print(a,b,c,x);...}...; R(c);...}...; R(2); S;...} a: integer;...; print(a,b,c,x);...}...; P;...} <Main> a b c P R CS 314, LS,BR,LTM: Scope and Memory 39

Implementing Lexical Scope Dynamic chain: sequence of control links Where a procedure appears on the dynamic chain depends on the calling context. Static chain: sequence of access links Where a procedure appears on the static chain depends only on the lexical nesting of declarations, independent of the calling context. We can determine at compile time from the program text the nesting depth n of every procedure. Use this to implement displays [Sethi, pp. 196-198]. CS 314, LS,BR,LTM: Scope and Memory 40

Displays Display: An array of pointers to the currently active frames on the stack that correspond to lexically enclosing blocks of the currently executing procedure display[j]= pointer to some frame of nesting depth j To encode the address of a variable: Determine statically (i.e., at compile time) the block whose binding is in effect, and its nesting depth j Variable is stored at address display[j]+offset Manage display in procedure prologue and epilogue CS 314, LS,BR,LTM: Scope and Memory 41

program Main { a 1,b 1,c 1 : integer; procedure P 1 { c 2 : integer; procedure S 2 { d 3 : integer; procedure R 3 (x 4 : integer) { c 2 =a 1 +c 2 +d 3 +x 4 ;...}...; R 3 (b 1 );...}...; R 1 (2); S 2 ;...} procedure R 1 (x 2 : integer) { a 2,c 2 : integer;...; print(a 2,b 1,c 2,x 2 );...}...; P 1 ;...} At compile time, variables can be labelled with their nesting depth. What does display look like when S is called? CS 314, LS,BR,LTM: Scope and Memory 42

1 2 3 Display Main proc P proc S dtop = 3 b 1 is at display[1] + 2 c 2 is at display[2] + 1 d 3 is at display[3] + 1 R 1 is at display[1] + 5 R 3 is at display[3] + 2 <Main> a b c P R c S d R <P> <S> CS 314, LS,BR,LTM: Scope and Memory 43

Implementing Displays Display is managed as a stack itself. Prologue: Push frame onto stack. Save old value of display[]. Update display[] and adjust dtop, if needed. Epilogue: Pop frame from stack. Restore display[] and adjust dtop, if needed. Two versions: How much information is saved in each frame? CS 314, LS,BR,LTM: Scope and Memory 44

program Main { a: integer; procedure P { @2 @3 b: integer; b=a; Q;...} procedure Q { b: integer; b=a;...} a=10; P;} @1 1 Main dtop = 1 1 2 Main dtop = 2 1 2 Main dtop = 2 @1 @2 @3 <Main> a == 10 <Main> a == 10 <P> b <Main> a == 10 <P> b NonRecursive Example <Q> b CS 314, LS,BR,LTM: Scope and Memory 45

program Main { a: integer; procedure P { c: integer; procedure S { @1 b: integer; P;...} S; 1 2 3 Main proc P proc S dtop = 3 @1 <Main> a <P> c <S> b...} P;} Recursive Example CS 314, LS,BR,LTM: Scope and Memory 46

program Main { a: integer; procedure P { @2 P;} c: integer; procedure S { b: integer; P;...} S;...} 1 2 3 Main proc P proc S dtop = 2 @2 <Main> a <P> c <S> b <P> c Recursive Example CS 314, LS,BR,LTM: Scope and Memory 47

Garbage Collection Automatic heap memory management (e.g., in Java, Scheme, Prolog,...) Goal: Reclaim memory that is no longer accessible. Methods: Reference Counting Mark and Sweep Stop and Copy Generational CS 314, LS,BR,LTM: Scope and Memory 48