
NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS Name Binding and Binding Time Name binding is the associaon of objects (data and/or code) with names (iden1fiers) Shape S = new Shape(); The binding of a program element to a par1cular property is the choice of the property from a set of possible proper1es binding and binding 1mes are the proper1es of program elements that are determined by the defini1on of the language or its implementaon The 1me during program formulaon or processing when this choice is made is the binding +me There are many classes of bindings in programming languages as well as many different binding 1mes Binding Time Binding mes: Run 1me (execu1on 1me): On entry to a subprogram or block Binding of formal to actual parameters Binding of formal parameters to storage locaons At arbitrary points during execu1on binding of variables to values Dynamic binding Binding Time Compile 1me (Stac Time) Declaraons (programmer ac1on) Variable names variable types program statement structure Compiler ac1on Relave locaon of data objects Linker ac1ons Relave locaon of different object modules Binding Time Binding Time Binding Time o The sum operator (+) At compilaon 1me (depending on the type of the operands because of overloading) o If x is declared integer + means one thing o if x is declared real means something else o + can also be overloaded by the programmer. o Example (C++): it is possible to specify that + operates on strings: string operator +(string& a, string& b) { return a.append(b); } Binding Time Shape s= new Shape(); s.getArea(); // The compiler can resolve this method call statically. Binding Time public void MakeSomeFoo(object a) { // Things happen... ((Shape) a).getArea(); // You won't know if this works until runtime!} Binding Time: discussion • Many of the most important and subtle differences between programming languages involve differences in binding 1me • The trade off is between stac analysis, efficient execu1on and flexibility • The language comes with a type system. The compiler assigns a type expression to parts of the source program. The compiler checks that the type usage in the program conforms to the type system for the language. • When efficiency is a consideraon (Fortran, C) languages are designed so that as many bindings as possible are performed during translaon • Where flexibility is the prime determiner, bindings are delayed un1l execu1on 1me so that they may be made data dependent Dynamic Dispatch " ! Dynamic dispatch allows the code executed when a message is sent to an object (o.m(x)) to be determined by run-1me values. " ! interface Shape { ... void draw() { ... } } class Circle extends Shape { ... void draw() { ... } } class Square extends Shape { ... void draw() { ... } } ... Shape s = ...; //could be a circle a square, or something else. s.draw(); " ! Invoking s.draw() could run the code for any of the methods shown in the program (or for any other class that extends Shape). " ! Java, all methods (except for stac methods) are dispatched dynamically. In " ! C++, only virtual members are dispatched dynamically. " ! Note that dynamic dispatch is not the same as overloading, which is usually resolved using the stac types of the arguments to the func1on being called. Objects life1me Program execution time time Program execution Creation of an object Object lifetime Object Creation of a binding Binding Binding lifetime lifetime Destruction of a binding Dangling reference if these two times are Destruction of an object interchanged Dangling References Int * p = new int; Int * q = new int; // things happen on p and q delete p; //Other things happens Use(q) Dangling references #include<stdio.h> int *call(); void main(){ int*ptr; ptr=call(); fflush(stdin); printf("%d",*ptr); } int * call(){ int x=25; ++x; return &x; } Storage Management ! Programming languages provide three storage allocaon mechanisms o! Stac Absolute address retained ! troughout program’s execu1on o! Stack Dynamic allocaon with ! calls&returns o! Heap Allocated and de-allocated at arbitrary 1me ! Stac Allocaon ! Global variables ! Constants o! manifest, declared (parameter variables in Fortran) or iden1fied by the compiler ! Variables iden1fied as const in C can be a func1on of non constants and therefore cannot be stacally allocated ! Constant tables generated by the compiler for debugging and other purposes Stac Allocaon ! In the absence of recursion, all variables can be stacally allocated ! Also, can be stacally allocated: o! Arguments and return values (or their addresses). Allocaon can be in processor registers rather than in memory o! Temporaries o! Bookkeeping informaon !return address !saved registers !debugging informaon Stac Allocaon Stack-based Allocaon ! Needed when language permits recursion ! Useful in languages without recursion because it can save space ! Each subrou1ne invocaon creates a frame or ac1vaon record o! arguments o! return address o! local variables o! temporaries o! bookkeeping informaon ! Stack maintained by o! calling sequence o! prologue o! epilogue Stack-based Allocaon (Cont.) Una funzione ricorsiva int Func ( /* in */ int a, /* in */ int b ) { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a , b - 1 ) ) ; return result; } 23 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL ? original call result ? at instruction 100 b 2 a 5 pushes on this record Return Address 100 for Func(5,2) 24 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL ? call in Func(5,2) code result ? at instruction 50 b 1 pushes on this record a 5 for Func(5,1) Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 record for Func(5,2) a 5 Return Address 100 25 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL ? call in Func(5,1) code result ? at instruction 50 b 0 pushes on this record a 5 for Func(5,0) Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 record for Func(5,1) Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 record for Func(5,2) Return Address 100 26 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL 0 result 0 b 0 record for Func(5,0) a 5 Return Address 50 is popped first with its FCTVAL FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 record for Func(5,1) Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 record for Func(5,2) Return Address 100 27 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 record for Func(5,1) Return Address 50 is popped next with its FCTVAL FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 record for Func(5,2) Return Address 100 28 Run-Time Stack Ac1vaon Records x = Func(5, 2);// original call at instruction 100 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5 record for Func(5,2) Return Address 100 is popped last with its FCTVAL 29 Heap-based Allocaon ! Region of storage in which blocks of memory can be allocated and de-allocated at arbitrary 1mes ! Because they are not allocated in the stack, the life1me of objects allocated in the heap is not confined to the subrou1ne where they are created o! They can be assigned to parameters (or to components of objects accessed via pointers by parameters) o! They can be returned as value of the subrou1ne/func1on/ method Find the errors in this code Heap-based Allocaon ! Several strategies to manage space in the heap ! Fragmentaon o! Internal fragmenta+on when space allocated is larger than needed o! External fragmenta+on when allocated blocks are scaered through the heap. Total space available might be more than requested, but no block has the needed size Heap-based Allocaon ! One approach to maintain the free memory space is to use a free list ! Two strategies to find a block for a give request o! First fit: use first block in the list large enough to sasfy the request o! Best fit: search the list to find the smallest block that sasfy the request ! The free list could be organized as an array of free lists where each list in the array contain blocks of the same size Cells and Liveness " ! Cell = data item in the heap o! Cells are “pointed to” by pointers held in registers, stack, global/stac memory, or in other heap cells " ! Roots: registers, stack locaons, global/stac variables " ! A cell is live if its address is held in a root or held by another live cell in the heap Garbage " ! Garbage is a block of heap memory that cannot be accessed by the program o! An allocated block of heap memory does not have a reference to it (cell is no longer “live”) " ! Garbage collec1on (GC) - automac management of dynamically allocated storage o! Reclaim unused heap blocks for later use by program slide 35 Example of Garbage class node { int value; p = new node(); q = new node(); node next; q = p; } delete p; node p, q; slide 36 The Perfect Garbage Collector " ! No visible impact on program execu1on " ! Works with any program and its data structures o! For example, handles cyclic data structures " ! Collects garbage (and only garbage) cells quickly o! Incremental; can meet real-1me constraints " ! Has excellent spaal locality of reference o! No excessive paging, no negave cache effects " ! Manages the heap efficiently o! Always sasfies an allocaon request and does not fragment slide 37 Reference Coun1ng: Example Heap space root set 1 2 1 1 1 1 2 1 slide 38 Reference Coun1ng: Cycles Heap space Memory leak root set 1 1 1 1 1 1 2 1 slide 39 Mark-Sweep Example (1) Heap space
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages44 Page
-
File Size-