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.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(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.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
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:
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
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
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.
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.
ISBN Chapter 10 Implementing Subprograms.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Basic Semantics Associating meaning with language entities.
Programming Languages and Paradigms Imperative Programming.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
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.
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.
Subprograms - implementation
7. Runtime Environments Zhang Zhizheng
Chapter SevenModern Programming Languages1 A Second Look At ML.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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.
ISBN Chapter 10 Implementing Subprograms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
Implementing Subprograms
Names and Attributes Names are a key programming language feature
ML Again ( Chapter 7) Patterns Local variable definitions
Memory Locations For Variables
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Scope, Function Calls and Storage Management
Presentation transcript:

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 Variables (ch 12)

10/16/2015IT 3272 Imperative  Functional n Imperative languages a := 0 – Store a zero in a ’s memory location n Functional languages val a = 0 – Bind a to the value zero a := 0; a := 1; val a = 0; val a = 1;

10/16/2015IT 3273 Function Activations n Activation of a function : The lifetime of one execution of the function, from call to corresponding return. most modern languages If each activation has its own binding for variables, the variables are called activation-specific variable (dynamic or automatic)

10/16/2015IT 3274 Block Activations n A variable might be specific to 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; Do we have it in C++/JAVA?

10/16/2015IT 3275 Other Lifetimes For Variables n We usually have a way to declare a variable that is bound to an independent memory (independent from any functions) n static allocation, the loader does the job of allocation int count = 0; int nextcount() { int inc = 1; count = count + inc; return count; }

10/16/2015IT 3276 Scope  Activation George Washington America  A.D

10/16/2015IT 3277 n In most modern languages, variables with local scope have activation-specific lifetimes, by default n some exception int nextcount() { static int count = 0; int inc = 1; count = count + inc; return count; } not activation-specific (lifetime) Scope  Activation George Washington America  A.D

10/16/2015IT 3278 there are other lifetimes for variables n In OOP, some variables’ lifetimes are associated with object lifetimes n Some variables may last across multiple executions of the program In addition to activation-specific variables

10/16/2015IT 3279 Activation Records Each function being executed has an activation record An activation record contains information about : 1.Activation-specific variables 2.Return address (or pointer to the current instructions) 3.Link to caller’s activation record

10/16/2015IT Block Activation Records n When a block is entered, space (memory) must be found for the local variables of that block n Possibile implementations: – 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

10/16/2015IT 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, but....(what’s the problem?)

10/16/2015IT Fortran 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 n Static activation record

10/16/2015IT Drawback n Each function has one activation record, thus There can be only one activation. Modern languages (including modern dialects Cobol and Fortran) do not obey this restriction for: 1.Recursion 2.Multithreading

10/16/2015IT Stacks Of Activation Records n To support recursion, we need to allocate a new activation record for each activation n Dynamic allocation: allocate  deallocate n A stack of activation records: stack frames push  pop

10/16/2015IT Current Activation Record the one for the function that is running n Static: location of activation record was determined before runtime n 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. A machine register is reserved to hold this

10/16/2015IT C Example int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } The evaluation of fact(3) before the 1 st recursive call, fact(2).... cout <<... << fact(3) << somewhere calling fact(3)

10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } before calling fact(1) After calling fact(2) calling fact(3) calling fact(2)

10/16/2015IT int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; } Before fact(1) returns calling fact(3) calling fact(2) calling fact(1)

10/16/2015IT 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. 12 2

10/16/2015IT 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.

10/16/2015IT 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; halve [1,2,3,4]

10/16/2015IT 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. halve [3,4]

10/16/2015IT 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. halve []

10/16/2015IT 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.

10/16/2015IT 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])

10/16/2015IT Nesting Functions – 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 ML, Ada, Pascal provide such feature; n C, Java don’t. (for good reasons)

10/16/2015IT Called by another inner function An easy solution: further trace down the previous activation record. Called by a recursive call. f(…) { int i = 3; } …… h(…) …… g(…) { … i … } h(…) { g(…) … } f(…) { int i = 3; … g(…) … } g(…) { … g(…)… … i … } What is i in function g ? The tricky part is that, the previous activation record may not help int i = 2;

10/16/2015IT Static Scoping vs Dynamic Scoping What is the vale of i in function g ? (suppose h  f  g) f(…) { int i = 3; } h(…) { int i = 2; …… f(…) …… } ……g(…)…… g(…) { … i … } dynamic scoping static scoping n Static Scoping  n Dynamic Scoping  i = 3

10/16/2015IT Static Scoping and Nesting Link For Static Scoping: n An inner function needs to find the address of the most recent activation of the outer function, not the caller (caller is for dynamic) n We can keep a nesting link in the activation record… f(…) { int i = 3; } h(…) { int i = 2; …… g(…) …… } ……g(…)…… g(…) { … i … } dynamic scoping static scoping

10/16/2015IT fun split nil = (nil,nil) | split [a] = ([a],nil) | split [a,b] = if (a <= b)then ([a],[b]) else ([b],[a]) | split (a::b::c) = let val (x,y) = split (a::c) in if (a < b) then (x, b::y) else (b::x, y) end; fun quicksort nil = nil | quicksort [a] = [a] | quicksort a = let val (x,y) = split a in end; Quick Sort in ML pivot

10/16/2015IT Better ML 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;

10/16/2015IT time Current Activation Record Register Can’t find pivot here QuickSort(…) Return Address Previous Activation Record QuickSort local variable: pivote, rest, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, …

10/16/2015IT time Current Activation Record QuickSort(…) Return Address Previous Activation Record QuickSort local variable: pivote, rest, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Split(…) Return Address Previous Activation Record QuickSort local variable: x, xs, … Nesting link null The rules to setup the nesting links: 1, 2,

10/16/2015IT How to Set Nesting Links n For one level of nesting: – 1. Calling from outer to outer: (non-nesting function, e.g. main calls quicksort)  set to null – 2. Calling from outer to inner: (e.g. quicksort calls split, where split is defined inside quicksort)  set nesting link same to caller’s activation record – 3. Calling from inner to inner: (e.g. split calls split)  set nesting link same to caller’s nesting link

10/16/2015IT Multiple Levels Of Nesting n References n nesting levels away chain back through n nesting links f(…) { int fi; } g(…) { int gi; } h(…) { int hi … hi, gi, fi }

10/16/2015IT n Common methods for referring to non-local variables in outer functions – Nesting links in activation records – Displays: no nesting links in the activation records, but collected in a single static array – Lambda lifting: passing all needed variables as parameters (i.e., as hidden parameters)

10/16/2015IT Functions As Parameters n What really gets passed? n Source code, compiled code, pointer to code, or implementation in some other form macro expandingOOP objectsC’s way functional languages

10/16/2015IT Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; addXToAll  (call) map, map  (call) addX, addX (refers) x In the previous example: quicksort  (call) split 5, [2,3,4]  [7,8,9] where to? Nesting link (in addXToAll ’s activation record) map’s activation record?

10/16/2015IT When map  addX, what nesting link will addX be given? – Not map ’s activation record: because addX is not nested inside map – Not map ’s nesting link: because map is not nested inside any other funcion Therefore, the parameter addX passed to map must include the nesting link to use when addX is called, i.e., the nesting link of addx should be prepared when addToAll tries to pass it as an argument to map

10/16/2015IT Example fun addXToAll (x,theList) = let fun addX y = y + x; in map addX theList end; Right before the call to map. The variable addX is bound to a function-value including code and nesting link. Current Activation Record y=>y+x Nesting link Parameter Return address Nesting link Previous activation record x theList addX

10/16/2015IT Not Just For 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

10/16/2015IT One More Complication n What happens if a function created is to be returned as a value? fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; return a function that add x

10/16/2015IT fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; Before funToAddX returns addx. y=>y+x Nesting link Parameter :3 Return address Nesting link: null Previous activation record x=3 addX Current Activation Record Parameter Return address Nesting link … f:?

10/16/2015IT fun funToAddX x = let fun addX y = y + x; in addX end; fun test = let val f = funToAddX 3; in f 5 end; y=>y+x Nesting link Parameter :3 Return address Nesting link: null Previous activation record x=3 addX Current Activation Record Parameter Return address Nesting link … f: After funToAddX returns, f is the bound to the new function-value. Any Problem ?

10/16/2015IT Problem -- This will fail if the language system deallocated that activation record when the function returned Solution:keep all activation records (ML’s way) New problem: Solution: waste too much memory Garbage collection New problem: ??