14-1 14 Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,

Slides:



Advertisements
Similar presentations
Memory Management Chapter FourteenModern Programming Languages, 2nd ed.1.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Memory Management Chapter  Memory management: the process of binding values to (logical) memory locations.  The memory accessible to a program.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
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.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
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.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
COP4020 Programming Languages
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.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
ISBN Chapter 10 Implementing Subprograms.
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.
3-1 © 2004, D.A. Watt, University of Glasgow 3 Variables and Storage  A simple storage model.  Simple and composite variables.  Copy semantics vs reference.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Activation Records (in Tiger) CS 471 October 24, 2007.
9-1 9 Variables and lifetime  Variables and storage  Simple vs composite variables  Lifetime: global, local, heap variables  Pointers  Commands 
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
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.
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
ISBN Chapter 10 Implementing Subprograms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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.
CSC 533: Programming Languages Spring 2016
Storage Allocation Mechanisms
CSC 533: Programming Languages Spring 2015
Dynamic Memory Allocation
CS 153: Concepts of Compiler Design November 28 Class Meeting
Implementing Subprograms
Topic 3-b Run-Time Environment
Runtime Environments What is in the memory?.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CSC 533: Programming Languages Spring 2019
Run-time environments
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt, University of Glasgow

14-2 Data representation  Assumptions: –The PL is statically-typed. –The compiler decides the size and layout of each type. –All variables of the same type have the same size.  Here consider representation of: –primitive types –cartesian products –arrays –objects.

14-3 Representation of primitive types  Representation of each primitive type may be language-defined or implementation-defined.  Typically 8, 16, 32, or 64 bits.  BOOL: or  CHAR: , …, (if 8-bit)  INT: 16-bit or 32-bit or 64-bit twos-complement.  FLOAT: 32-bit or 64-bit IEEE floating-point.

14-4 Representation of arrays (1)  Represent an array by juxtaposing its components.  Represention of arrays of type {0, 1, 2, …} → T: component 0 base component 1 component 2 … components of the same type, with the same size

14-5 Representation of arrays (2)  The offset of array component i (relative to the array’s base address) is linearly related to i: offset of component i=(size of type T) × i known to the compiler unknown  Since i is unknown, the offset of component i must be calculated at run-time.

14-6 Example: representation of C arrays  C type definition: typedef int[] Arr;  Possible representation of values of type Arr : Assume size of INT is 4 bytes (offset of component i is 4i bytes) component 0 component 1 component 2 …

14-7 Representation of cartesian products (1)  Represent a tuple (or record or struct) by juxtaposing its components.  Representation of tuples of type T 1  T 2    T n : components of different types, with different sizes component n component 2 component 1 … base component 3

14-8 Representation of cartesian products (2)  The compiler knows the offset of each tuple component (relative to the tuple’s base address): offset of component 1=0 offset of component 2=size of type T 1 offset of component 3=size of type T 1 + size of type T 2 … offset of component n=size of type T 1 + size of type T size of type T n–1 all sizes known to the compiler

14-9 Example: representation of C structs  C struct type definition: struct Str { float f; int n; char c; };  Possible representation of structs of type Str : c (offset 6) n (offset 4) f (offset 0) Assume sizes: FLOAT 4 bytes INT 2 bytes CHAR 1 byte

14-10 Representation of objects (1)  Recall: Objects are tagged tuples.  Represent an object by juxtaposing its components (instance variables) with a class tag.

14-11 Representation of objects (2)  Consider class C with components (instance variables) of types T 1, , T n.  Representing objects of class C:  The compiler knows the offset of each component (relative to the object’s base address). component of type T n component of type T 1 … base class tag C components of different types, with different sizes

14-12 Representation of objects (3)  Now consider class C' (a subclass of C) with additional instance variables of types T' 1, , T' m.  Representation of objects of classes C and C': component of type T n component of type T 1 … class tag CC'C' component of type T' m component of type T' 1  Each component has a known offset in objects of a given class C and all subclasses of C.

14-13 Example: representation of Java objects (1)  Java class declarations: class Shape { int x, y; … } class Circle extends Shape { int r; … } class Box extends Shape { int w, h; boolean rd; // true if corners are rounded … }

14-14 Example: representation of Java objects (2)  Representing objects of above classes (simplified): x y Shape x y r Circle x y w h rd Box class tag

14-15 Storage organization  Each variable occupies storage space throughout its lifetime. That storage space must be: –allocated at the start of the variable’s lifetime –deallocated at the end of the variable’s lifetime.  Assumptions: –The PL is statically typed, so every variable’s type is known to the compiler. –All variables of the same type occupy the same amount of storage space.

14-16 Storage for global and local variables (1)  Recall: A global variable’s lifetime is the program’s entire run-time.  For global variables, the compiler allocates fixed storage space.  Recall: A local variable’s lifetime is an activation of the block in which the variable is declared. The lifetimes of local variables are nested.  For local variables, the compiler allocates storage space on a stack.

14-17 Storage for global and local variables (2)  At any given time, the stack contains one or more activation frames: –The frame at the base of the stack contains the global variables. –For each active procedure P, there is a frame containing P’s local variables.  A frame for procedure P is: –pushed on to the stack when P is called –popped off the stack when P returns. An active procedure is one that has been called but not yet returned.

14-18 Storage for global and local variables (3)  The compiler fixes the size and layout of each frame.  The offset of each global/local variable (relative to the base of the frame) is known to the compiler.

14-19 Example: storage for global and local variables in SVM (1)  SVM data store when the main program has called P, and P has called Q: frame for Q frame for P global frame P’s locals return address dynamic link Q’s locals return address dynamic link fp sp globals free  sp (stack pointer) points to the first free cell above the top of the stack.  fp (frame pointer) points to the first cell of the topmost frame.

14-20 Example: storage for global and local variables in SVM (2)  Effect of calls and returns: call Pcall Qreturn sp globals fp free fp sp P’s locals globals free P’s locals Q’s locals fp sp globals free P’s locals fp sp globals free

14-21 Storage for heap variables (1)  Recall: A heap variable’s lifetime starts when the heap variable is created and ends when it is destroyed or becomes unreachable. The lifetimes of heap variables follow no pattern.  Heap variables occupy a storage region called the heap. At any given time, the heap contains all currently-live heap variables, interspersed with free space. –When a new heap variable is to be created, some free space is allocated to it. –When a heap variable is to be destroyed, its allocated space reverts to being free.

14-22 Storage for heap variables (2)  The heap manager (part of the PL’s run-time system) keeps track of free space within the heap –usually by means of a free-list: a linked list of free areas of various sizes.  The heap manager provides: –a routine to create a heap variable (called by the PL’s allocator) –a routine to destroy a heap variable (called by the PL’s deallocator, if any).

14-23 Example: storage for heap variables  Effect of allocations and deallocations: d c b a e free d c b a e d c b a e d c a e stack heap d c b a allocate ed.succ=adeallocate bc.right=e

14-24 Garbage collection  If the PL has no deallocator, the heap manager must be able to find and destroy any unreachable heap variables automatically. This is done by a garbage collector.  A garbage collector must visit all reachable heap variables. This is inevitably time-consuming.  A mark-sweep garbage collector is the simplest. It first marks all reachable heap variables, then deallocates all unmarked heap variables.

14-25 Mark-sweep garbage collection algorithm  Mark-sweep algorithm: To mark all heap variables reachable from pointer p: 1Let heap variable v be the referent of p. 2If v is unmarked: 2.1Mark v. 2.2For each pointer q in v: 2.2.1Mark all heap variables reachable from q. To collect garbage: 1For each pointer p in a global/local variable: 1.1Mark all heap variables reachable from p. 2For each heap variable v: 2.1If v is unmarked, destroy v. 2.2Else, if v is marked, unmark v. depth-first traversal

14-26 Example: mark-sweep garbage collection  Effect of mark and sweep: marksweep stack heap d c b a e free d c b a e d c a e

14-27 Issues  Time complexity of mark-sweep garbage collection is O(n r + n h ) –n r = number of reachable heap variables –n h = total number of heap variables.  The heap tends to become fragmented: –There might be many small free areas, but none big enough to allocate a new large heap variable. –Partial solution: coalesce adjacent free areas in the heap. –Better solution: use a copying or generational garbage collector.

14-28 Copying garbage collector  A copying garbage collector maintains two separate heap spaces: –Initially, space 1 contains all heap variables; space 2 is spare. –Whenever the garbage collector reaches an unmarked heap variable v, it copies v from space 1 to space 2. –At the end of garbage collection, spaces 1 and 2 are swapped.  Pros and cons: +Heap variables can be consolidated when copied into space 2. –All pointers to a copied heap variable must be found and redirected from space 1 to space 2.

14-29 Generational garbage collector  A generational garbage collector maintains two (or more) separate heap spaces: –One space (the old generation) contains only long-lived heap variables; the other space (the young generation) contains shorter-lived heap variables. –The old generation is garbage-collected infrequently (since long-lived heap variables are rarely deallocated). –The young generation is garbage-collected frequently (since short-lived heap variables are often deallocated). –Heap variables that live long enough may be promoted from the young generation to the old generation.  Pro: +Garbage collection is more focussed.