CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
1 Storage Allocation Operating System Hebrew University Spring 2007.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run-Time Storage Organization
Run time vs. Compile time
The Concept of Variables
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
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.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
COP4020 Programming Languages
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Memory Management Chapter 7.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Binding Time and Storage Allocation (Section ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
COMP3190: Principle of Programming Languages
CSC 8505 Compiler Construction Runtime Environments.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Memory Management -Memory allocation -Garbage collection.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
Names, Scope, and Bindings Programming Languages and Paradigms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Object Lifetime and Pointers
Storage Allocation Mechanisms
Data Types In Text: Chapter 6.
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Run-time organization
Topic 3-b Run-Time Environment
Binding Times Binding is an association between two things Examples:
Operating System Chapter 7. Memory Management
Programming Languages
Name Binding and Object Lifetimes
Run Time Environments 薛智文
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9

22/69 Binding Time Static vs. Dynamic: −static binding time - corresponds to bindings made before run time −dynamic binding time - corresponds to bindings made at run time Clearly static binding time is a coarse term that can mean many different times (language design, program writing, compilation, etc) −also called early binding Dynamic is also a coarse term, generally referring to binding times such as when variable values are bound to variables −also called late binding

33/69 Binding Time Early binding Late binding −associated with greater efficiency −compiled languages tend to have early binding times −the compiler analyzes the syntax and semantics of global variable declarations only once, decides on a data layout in memory, generates efficient code to access them −associated with greater flexibility −interpreted languages tend to have late binding times −the interpretor analyzes the declarations every time the program runs −bindings are not "frozen" at compile time, they can change during execution

44/69 Object Lifetime and Binding Lifetime Distinguish between names and objects they refer Identify several key events: −creation of objects −creation of bindings −references to names (variables, subroutines, types) −temporary deactivation / reactivation of bindings −destruction of bindings −destruction of objects −allocation −declaration −use of variable in expression, function call −entering a procedure / returning from a procedure (for global variables hidden by local ones) −returning from a procedure (for local variables), end of program (globals) −deallocation

55/69 Object Lifetime and Binding Lifetime Lifetime - the time interval between creation and destruction Both objects and bindings have their own, possibly distinct lifetimes If an object outlives its (only) binding it's garbage If a binding outlives its object it’s a dangling reference p = new int ; p = NULL ; p = new int ; r = p ; delete r ;

66/69 Storage Management Lifetime of objects is determined by allocation and deallocation Allocation - getting ("reserving") a memory cell from some pool of available cells (the "free space") Deallocation - placing a memory cell back in the pool Storage allocation mechanisms: −Static −Dynamic −stack −heap −explicit −implicit

77/69 Static Allocation Static allocation −Each object is given an address before execution begins and retains that address throughout execution −Examples - program code, C global variables and static variables, all Fortran 77 variables, explicit constants (including character strings), tables for debugging What to do with local variables (need to have a separate instance in each subroutine invocation)? How can then Fortran 77 allocate all variables statically? −Dynamic allocation on the stack −Fortran 77 does not allow recursion - for each subroutine, no more than one invocation is active at any time. Each subroutine can thus allocate statically its own "reserved" space for local variables

88/69 Static Allocation Static alocation for subroutines when no recursion is allowed (Fortran 77): Advantages: Disadvantages: −efficiency (direct addressing) −history-sensitive local variables −lack of flexibility (no recursion!)

99/69 Dynamic Stack Allocation Stack-based allocation −Objects are allocated (on a stack), and bindings are made when entering a subroutine −They are destroyed when returning from subroutine −Corresponds to last-in, first-out order −Examples - arguments, local variables, return value, return address, temporaries Advantages: Disadvantages: −Allows recursion −Reuses space −Run-time overhead of allocation and deallocation on stack −Local variables cannot be history sensitive −Inefficient references (indirect addressing)

1010/69 Dynamic Stack Allocation Stack pointer (sp) - register that points to the first unused entry on the stack Frame pointer (fp) - register that points to a known location within the active frame −Objects within frame can be accessed at a predefined offset from fp Frame (a.k.a. activation record) - an entry on the stack −when a subroutine is invoked - push a frame on the stack −when a subroutine ends - pop a frame from the stack

1111/69 Dynamic Stack Allocation Maintenance of the stack is the responsibility of: −calling sequence - code executed by caller immediately before and after the call −subroutine prologue and epilogue - code executed by subroutine at its beginning / end Which is more efficient? −The macro (#define) is generally more efficient, as it does not have the overhead of stack manipulation #define max(x,y) x>y?x:yint max (int x, int y) { return x>y?x:y ; } OR

1212/69 Dynamic Heap Allocation Explicit heap-based allocation −Allocated and deallocated by explicit directives at arbitrary times, specified by the programmer −Take effect during execution −Examples - dynamic objects in C (via malloc and free), or C++ (via new and delete) Advantage: −Provides on-demand dynamic storage management Disadvantage: −Requires an expensive and complex storage management algorithm

1313/69 Dynamic Heap Allocation Implicit heap-based allocation −Allocation and deallocation are implicit (transparent for the programmer) −Example: Advantage: Disadvantage: −Flexibility, ease of use −Possible inefficiency −if the programmer knows that there will be N elements in a list, it would be better to explicitly allocate space for them all at once −allocation of list structures in Scheme: (define x (cons 'a '(b c)))

1414/69 Dynamic Heap Allocation Allocation is made in a memory region called heap - no connection with the heap data structure Principal concerns in heap management are speed and space Space issues: −Internal fragmentation −when allocating a block larger than required to hold a given object −the extra space in the block is unused −External fragmentation −when allocated blocks are scattered through the heap, making the free space extremely fragmented −there may be a lot of free space, but no piece is large enough for some future request

1515/69 Dynamic Heap Allocation External fragmentation: −Shaded blocks – in use −Clear blocks – free

1616/69 Dynamic Heap Allocation Dealing with external fragmentation −cannot totally avoid it −ability of the heap to satisfy requests may degrade over time The solution: Why is this difficult? −compact the heap by moving already allocated blocks −need to find all pointers that refer to the moved blocks, and update their values

1717/69 Dynamic Heap Allocation Implementation −Maintain a single linked list of heap blocks that are not currently used (the free list) Strategies: −First fit – select the first block in the list that is large enough to satisfy the allocation request −Best fit – select the smallest block in the list that is large enough to satisfy the allocation request First fit Best fit −Faster, tends to produce internal fragmentation −Slower (searches the entire list), less internal fragmentation

1818/69 Dynamic Heap Allocation Using a single linked list makes the allocation time linear in the number of free blocks To reduce it to constant time: Implementation: −separate lists for blocks of different sizes Strategies: −Buddy system −Fibonacci heap

1919/69 Dynamic Heap Allocation Buddy system: −Block sizes are powers of 2 −Allocation: −a request for a block of size 2 k comes in −if a block of size 2 k is available, take it −if not, split a block of size 2 k+1 in two halves (2 k each), use half for allocation, and place the other in the 2 k free list −Deallocation: −merge the block with its "buddy" (the other half) if it is free Fibonacci system – similar, but uses Fibonacci numbers instead of powers of 2 free list 2k2k 2 k+1

2020/69 Announcements Readings −Chapter 3, up to Homework −HW 3 out – due on October 6 −Submission −at the beginning of class −with a title page: Name, Class, Assignment #, Date −everything stapled together −preferably typed