COP4020 Programming Languages

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

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.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The Concept of Variables
Semantics of Calls and Returns
Memory Management Chapter 5.
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.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
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,
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
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
CS 326 Programming Languages, Concepts and Implementation
CS 363 – Chapter 3 What do we mean by names, scopes, bindings? Names
Dynamic Memory Allocation
Run-time organization
COP4020 Programming Languages
Topic 3-b Run-Time Environment
Binding Times Binding is an association between two things Examples:
Programming Languages
Name Binding and Object Lifetimes
Run Time Environments 薛智文
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan

Overview Object storage management Static allocation Stack allocation Heap allocation 4/21/2017 COP4020 Spring 2014 COP4020 Fall 2006

Object Lifetime and Storage Objects (program data and code) have to be stored in memory during their lifetime. In general there are three types of objects in a program The objects that are alive throughout the execution of a program E.g. global variables The objects that are alive within a routine E.g. local variables The objects whose lifetime can be dynamically changed. The objects that are managed by the ‘new/delete’ constructs. 4/21/2017 COP4020 Spring 2014

Object Lifetime and Storage The three types of objects correspond to three principal storage allocation mechanisms. Static objects have an absolute storage address that is retained throughout the execution of the program Global variables and data Subroutine code and class method code Stack objects are allocated in last-in first-out order, usually in conjunction with subroutine calls and returns Actual arguments passed by value to a subroutine Local variables of a subroutine Heap objects may be allocated and deallocated at arbitrary times, but require an expensive storage management algorithm Dynamically allocated data in C++. Java class instances are always stored on the heap 4/21/2017 COP4020 Spring 2014

Typical Program and Data Layout in Memory Program code is at the bottom of the memory region (code section) The code section is protected from run-time modification by the OS Static data objects are stored in the static region Stack grows downward Heap grows upward Upper addr stack heap Virtual memory address space static data code 0000 4/21/2017 COP4020 Spring 2014

Static Allocation Program code is statically allocated in most implementations of imperative languages Statically allocated variables are history sensitive Global variables keep state during entire program lifetime Static local variables in C/C++ functions keep state across function invocations Static data members are “shared” by objects and keep state during program lifetime Advantage of statically allocated object is the fast access due to absolute addressing of the object Can static allocation be used for local variables? Statically allocated local variables has only one copy of each variable. Cannot deal with the cases when multiple copies of a local varabile are alive!!! When does this happen? 4/21/2017 COP4020 Spring 2014

Static Allocation in Fortran 77 Fortran 77 has no recursion Global and local variables are statically allocated as decided by the compiler Global and local variables are referenced at absolute addresses Avoids overhead of creation and destruction of local objects for every subroutine call Each subroutine in the program has a subroutine frame that is statically allocated This subroutine frame stores all subroutine-relevant data that is needed to execute Temporary storage (e.g. for expression evaluation) Local variables Bookkeeping (e.g. saved CPU registers) Return address Subroutine arguments and returns Typical static subroutine frame layout 4/21/2017 COP4020 Spring 2014

Stack Allocation Each instance of a subroutine that is active has a subroutine frame (sometimes called activation record) on the run-time stack Compiler generates subroutine calling sequence to setup frame, call the routine, and to destroy the frame afterwards Method invocation works the same way, but in addition methods are typically dynamically bound Subroutine frame layouts vary between languages, implementations, and machine platforms 4/21/2017 COP4020 Spring 2014

Typical Stack-Allocated Subroutine Frame Lower addr Most modern processors have two registers: fp (frame pointer) and sp (stack pointer) to support efficient execution of subroutines in high level languages A frame pointer (fp) points to the frame of the currently active subroutine at run time Subroutine arguments, local variables, and return values are accessed by constant address offsets from the fp Temporary storage (e.g. for expression evaluation) Local variables Bookkeeping (e.g. saved CPU registers) Return address fp Subroutine arguments and returns Higher addr Typical subroutine frame layout 4/21/2017 COP4020 Spring 2014

Subroutine Frames on the Stack Stack growth sp Subroutine frames are pushed and popped onto/from the runtime stack The stack pointer (sp) points to the next available free space on the stack to push a new frame onto when a subroutine is called The frame pointer (fp) points to the frame of the currently active subroutine, which always the topmost frame on the stack The fp of the previous active frame is saved in the current frame and restored after the call In this example: M called A A called B B called A A Temporaries Local variables Bookkeeping fp Return address Arguments B Temporaries Local variables Bookkeeping Return address Arguments A Temporaries Local variables Bookkeeping Return address Arguments M Temporaries Local variables Bookkeeping Return address Higher addr Arguments 4/21/2017 COP4020 Spring 2014

Example Subroutine Frame Lower addr The size of the types of local variables and arguments determines the fp offset in a frame Example Pascal procedure: procedure P(a:integer, var b:real) (* a is passed by value   b is passed by reference,     = pointer to b's value *) var   foo:integer;(* 4 bytes *)   bar:real;   (* 8 bytes *)   p:^integer; (* 4 bytes *) begin   ... end Temporaries -36: foo (4 bytes) fp-32 -32: bar (8 bytes) -24: p (4 bytes) Bookkeeping (16 bytes) Return address to the caller of P (4 bytes) fp 0: a (4 bytes) fp+4 4: b (4 bytes) Higher addr 4/21/2017 COP4020 Spring 2014

Heap Allocation Heap is used to store objects who lifetime is dynamic Implicit heap allocation: Done automatically Java class instances are placed on the heap Scripting languages and functional languages make extensive use of the heap for storing objects Some procedural languages allow array declarations with run-time dependent array size Resizable character strings Explicit heap allocation: Statements and/or functions for allocation and deallocation Malloc/free, new/delete 4/21/2017 COP4020 Spring 2014

Heap allocation problem Heap is a large block of memory (say N bytes) Requests for memory of various sizes may arrive randomly: program runs ‘new’ Each request may ask for 1 to N bytes If a request of X bytes is granted, a continuous X bytes in the heap is allocated for the request. The memory will be used for a while and then return to the system (when the program runs ‘delete’ The problem: how to allocate memory so that as many request can be satisfied as possible. 4/21/2017 COP4020 Spring 2014

Heap allocation example 10KB memory to be managed r1 = req(1K); r2 = req (2K); r3 = req(4k); free(r2); free(r1); r4 = req(4k); How to do it makes a difference!! Internal fragment: unused memory within a block Asking for 100 bytes and get a 512 bytes block External fragment: unused memory between blocks Even when the total available memory is more than a request, the request cannot be satisfied as in the example. 4/21/2017 COP4020 Spring 2014

Heap Allocation Algorithms Heap allocation is performed by searching the heap for available free space For example, suppose we want to allocate a new object E of 20 bytes, where would it fit? Deletion of objects leaves free blocks in the heap that can be reused Two questions How to keep track of free blocks? How to find the right free block for each request? Object A Free Object B Object C Object D 30 bytes 8 bytes 10 bytes 24 bytes 20 bytes 4/21/2017 COP4020 Spring 2014

Heap Allocation Algorithms (cont’d) How to keep track of free blocks? Maintain a linked list of free heap blocks To satisfy a request (new), search the list to find one block whose size is equal to or larger than the requested size If the size is equal, remove the block from the free list If the size is larger, modify the size to (size – requested size). When an object is deleted (freed), the block of memory is returned to the heap. Insert a new block to the free list. If new block can be merged with its neighboring blocks to be a larger block, merge the blocks. 4/21/2017 COP4020 Spring 2014

Heap Allocation Algorithms (cont’d) How to pick which block to be used for each request? There can be many choices. First-fit: select the first block in the list that is large enough Best-fit: search the entire list for the smallest free block that is large enough to hold the object O(N) for both ‘new’ and ‘delete’operations 4/21/2017 COP4020 Spring 2014

Other Heap Allocation Algorithms Buddy system: use heap pools of standard sized blocks of size 2k If no free block is available for object of size between 2k-1+1 and 2k then find block of size 2k+1 and split it in half, adding the halves to the pool of free 2k blocks, etc. Fibonacci heap: use heap pools of standard size blocks according to Fibonacci numbers More complex but leads to slower internal fragmantation 4/21/2017 COP4020 Spring 2014

Garbage Collection Explicit manual deallocation errors are among the most expensive and hard to detect problems in real-world applications If an object is deallocated too soon, a reference to the object becomes a dangling reference If an object is never deallocated, the program leaks memory Automatic garbage collection removes all objects from the heap that are not accessible, i.e. are not referenced Used in Lisp, Scheme, Prolog, Ada, Java, Haskell Disadvantage is GC overhead, but GC algorithm efficiency has been improved Not always suitable for real-time processing 4/21/2017 COP4020 Spring 2014

Garbage Collection How does it work roughly? The language defines the lifetime of objects. The runtime keeps track of the number of references (bindings) to each object. +1 when a new reference is made, -1 when the reference is destroyed Can delete when the reference count is 0. Need to determine when a variable is alive or dead based on language specification. 4/21/2017 COP4020 Spring 2014

Storage Allocation Compared Static Stack Heap Ada N/A local variables and subroutine arguments of fixed size implicit: local variables of variable size; explicit: new (destruction with garbage collection or explicit with unchecked deallocation) C global variables; static local variables local variables and subroutine arguments explicit with malloc and free C++ Same as C, and static class members Same as C explicit with new and delete Java only local variables of primitive types implicit: all class instances (destruction with garbage collection) Fortran77 global variables (in common blocks), local variables, and subroutine arguments (implementation dependent); SAVE forces static allocation local variables and subroutine arguments (implementation dependent Pascal global variables (compiler dependent) global variables (compiler dependent), local variables, and subroutine arguments Explicit: new and dispose 4/21/2017 COP4020 Spring 2014

Summary Object storage management Static allocation Stack allocation What type of objects are allocated with static allocation? Why not static allocation for everything? Stack allocation What type of objects are allocated with stack allocation? Heap allocation What type of objects are allocation with heap allocation? Garbage collection 4/21/2017 COP4020 Spring 2014