Chapter TwelveModern Programming Languages1 Memory Locations For Variables.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
ISBN Chapter 10 Implementing Subprograms.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Semantics of Calls and Returns
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
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
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.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Basic Semantics Associating meaning with language entities.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
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.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Memory Locations For Variables
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Scope, Visibility, and Lifetime
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 7: User-Defined Functions II
Scope, Function Calls and Storage Management
Implementing Subprograms
Implementing Subprograms
Presentation transcript:

Chapter TwelveModern Programming Languages1 Memory Locations For Variables

Chapter TwelveModern Programming Languages2 A Binding Question n Variables are bound (dynamically) to values n Those values must be stored somewhere n Therefore, variables must somehow be bound to memory locations n How?

Chapter TwelveModern Programming Languages3 Functional Meets Imperative Imperative languages expose the concept of memory locations: a := 0 – Store a zero in a ’s memory location Functional languages hide it: val a = 0 – Bind a to the value zero n But both need to connect variables to values represented in memory n So both face the same binding question

Chapter TwelveModern Programming Languages4 Outline n Activation records n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages5 Function Activations n The lifetime of one execution of a function, from call to corresponding return, is called an activation of the function n When each activation has its own binding of a variable to a memory locations, it is an activation-specific variable n (Also called dynamic or automatic)

Chapter TwelveModern Programming Languages6 Activation-Specific Variables n In most modern languages, activation- specific variables are the most common kind: fun days2ms days = let val hours = days * 24.0 val minutes = hours * 60.0 val seconds = minutes * 60.0 in seconds * end;

Chapter TwelveModern Programming Languages7 Block Activations n For block constructs that contain code, we can speak of an activation of the block n The lifetime of one execution of the block n A variable might be specific to an activation of a particular block within a function: fun fact n = if (n=0) then 1 else let val b = fact (n-1) in n*b end;

Chapter TwelveModern Programming Languages8 Other Lifetimes For Variables n Most imperative languages have a way to declare a variable that is bound to a single memory location for the entire runtime n Obvious binding solution: static allocation (classically, the loader allocates these) int count = 0; int nextcount() { count = count + 1; return count; }

Chapter TwelveModern Programming Languages9 Scope And Lifetime Differ n In most modern languages, variables with local scope have activation-specific lifetimes, at least by default n However, these two aspects can be separated, as in C: int nextcount() { static int count = 0; count = count + 1; return count; }

Chapter TwelveModern Programming Languages10 Other Lifetimes For Variables n Object-oriented languages use variables whose lifetimes are associated with object lifetimes n Some languages have variables whose values are persistent: they last across multiple executions of the program n Today, we will focus on activation-specific variables

Chapter TwelveModern Programming Languages11 Activation Records n Language implementations usually allocate all the activation-specific variables of a function together as an activation record n The activation record also contains other activation-specific data, such as – Return address: where to go in the program when this activation returns – Link to caller’s activation record: more about this in a moment

Chapter TwelveModern Programming Languages12 Block Activation Records n When a block is entered, space must be found for the local variables of that block n Various possibilities: – Preallocate in the containing function’s activation record – Extend the function’s activation record when the block is entered (and revert when exited) – Allocate separate block activation records n Our illustrations will show the first option

Chapter TwelveModern Programming Languages13 Outline n Activation-specific variables n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages14 Static Allocation n The simplest approach: allocate one activation record for every function, statically n Older dialects of Fortran and Cobol used this system n Simple and fast

Chapter TwelveModern Programming Languages15 Example FUNCTION AVG (ARR, N) DIMENSION ARR(N) SUM = 0.0 DO 100 I = 1, N SUM = SUM + ARR(I) 100 CONTINUE AVG = SUM / FLOAT(N) RETURN END

Chapter TwelveModern Programming Languages16 Drawback n Each function has one activation record n There can be only one activation alive at a time n Modern languages (including modern dialects of Cobol and Fortran) do not obey this restriction: – Recursion – Multithreading

Chapter TwelveModern Programming Languages17 Outline n Activation-specific variables n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages18 Stacks Of Activation Records n To support recursion, we need to allocate a new activation record for each activation n Dynamic allocation: activation record allocated when function is called n For many languages, like C, it can be deallocated when the function returns n A stack of activation records: stack frames pushed on call, popped on return

Chapter TwelveModern Programming Languages19 Current Activation Record n Before, static: location of activation record was determined before runtime n Now, dynamic: location of the current activation record is not known until runtime n A function must know how to find the address of its current activation record n Often, a machine register is reserved to hold this

Chapter TwelveModern Programming Languages20 C Example int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } We are evaluating fact(3). This shows the contents of memory just before the recursive call that creates a second activation.

Chapter TwelveModern Programming Languages21 int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } This shows the contents of memory just before the third activation.

Chapter TwelveModern Programming Languages22 int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } This shows the contents of memory just before the third activation returns.

Chapter TwelveModern Programming Languages23 int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The second activation is about to return.

Chapter TwelveModern Programming Languages24 int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The first activation is about to return with the result fact(3) = 6.

Chapter TwelveModern Programming Languages25 ML Example fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; We are evaluating halve [1,2,3,4]. This shows the contents of memory just before the recursive call that creates a second activation.

Chapter TwelveModern Programming Languages26 fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; This shows the contents of memory just before the third activation.

Chapter TwelveModern Programming Languages27 fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; This shows the contents of memory just before the third activation returns.

Chapter TwelveModern Programming Languages28 fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; The second activation is about to return.

Chapter TwelveModern Programming Languages29 fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a::b::cs) = let val (x, y) = halve cs in (a::x, b::y) end; The first activation is about to return with the result halve [1,2,3,4] = ([1,3],[2,4])

Chapter TwelveModern Programming Languages30 Outline n Activation-specific variables n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages31 Nesting Functions n What we just saw is adequate for many languages, including C n But not for languages that allow this trick: – Function definitions can be nested inside other function definitions – Inner functions can refer to local variables of the outer functions (under the usual block scoping rule) n Like ML, Ada, Pascal, etc.

Chapter TwelveModern Programming Languages32 Example fun quicksort nil = nil | quicksort (pivot::rest) = let fun split(nil) = (nil,nil) | split(x::xs) = let val (below, above) = split(xs) in if x<pivot then (x::below, above) else (below, x::above) end; val (below, above) = split(rest) in quicksort quicksort above end;

Chapter TwelveModern Programming Languages33 The Problem How can an activation of the inner function ( split ) find the activation record of the outer function ( quicksort )? n It isn’t necessarily the previous activation record, since the caller of the inner function may be another inner function Or it may call itself recursively, as split does…

Chapter TwelveModern Programming Languages34

Chapter TwelveModern Programming Languages35 Nesting Link n An inner function needs to be able to find the address of the most recent activation for the outer function n We can keep this nesting link in the activation record…

Chapter TwelveModern Programming Languages36

Chapter TwelveModern Programming Languages37 Setting The Nesting Link n Easy if there is only one level of nesting: – Calling outer function: set to null – Calling from outer to inner: set nesting link same as caller’s activation record – Calling from inner to inner: set nesting link same as caller’s nesting link n More complicated if there are multiple levels of nesting…

Chapter TwelveModern Programming Languages38 Multiple Levels Of Nesting References at the same level ( f1 to v1, f2 to v2, f3 to v3 ) use current activation record n References n nesting levels away chain back through n nesting links

Chapter TwelveModern Programming Languages39 Other Solutions n The problem: references from inner functions to variables in outer ones – Nesting links in activation records: as shown – Displays: nesting links not in the activation records, but collected in a single static array – Lambda lifting: problem references replaced by references to new, hidden parameters

Chapter TwelveModern Programming Languages40 Outline n Activation-specific variables n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages41 Functions As Parameters n When you pass a function as a parameter, what really gets passed? n Code must be part of it: source code, compiled code, pointer to code, or implementation in some other form n For some languages, something more is required…

Chapter TwelveModern Programming Languages42 Example This function adds x to each element of theList Notice: addXToAll calls map, map calls addX, and addX refers to a variable x in addXToAll ’s activation record fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end;

Chapter TwelveModern Programming Languages43 Nesting Links Again When map calls addX, what nesting link will addX be given? – Not map ’s activation record: addX is not nested inside map – Not map ’s nesting link: map is not nested inside anything To make this work, the parameter addX passed to map must include the nesting link to use when addX is called

Chapter TwelveModern Programming Languages44 Not Just For Parameters n Many languages allow functions to be passed as parameters n Functional languages allow many more kinds of operations on function-values: – passed as parameters, returned from functions, constructed by expressions, etc. n Function-values include both code to call, and nesting link to use when calling it

Chapter TwelveModern Programming Languages45 Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; This shows the contents of memory just before the call to map. The variable addX is bound to a function-value including code and nesting link.

Chapter TwelveModern Programming Languages46 Outline n Activation-specific variables n Static allocation of activation records n Stacks of activation records n Handling nested function definitions n Functions as parameters n Long-lived activation records

Chapter TwelveModern Programming Languages47 One More Complication n What happens if a function value is used after the function that created it has returned? fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end;

Chapter TwelveModern Programming Languages48 fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; This shows the contents of memory just before funToAddX returns.

Chapter TwelveModern Programming Languages49 fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; After funToAddX returns, f is the bound to the new function-value.

Chapter TwelveModern Programming Languages50 The Problem When test calls f, the function will use its nesting link to access x n That is a link to an activation record for an activation that is finished n This will fail if the language system deallocated that activation record when the function returned

Chapter TwelveModern Programming Languages51 The Solution n For ML, and other languages that have this problem, activation records cannot always be allocated and deallocated in stack order n Even when a function returns, there may be links to its activation record that will be used; it can’t be deallocated it is unreachable n Garbage collection: chapter 14, coming soon!

Chapter TwelveModern Programming Languages52 Conclusion n The more sophisticated the language, the harder it is to bind activation-specific variables to memory locations – Static allocation: works for languages that permit only one activation at a time (like early dialects of Fortran and Cobol) – Simple stack allocation: works for languages that do not allow nested functions (like C)

Chapter TwelveModern Programming Languages53 Conclusion, Continued – Nesting links (or some such trick): required for languages that allow nested functions (like ML, Ada and Pascal); function values must include both code and nesting link – Some languages (like ML) permit references to activation records for activations that are finished; so activation records cannot be deallocated on return