Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Similar presentations


Presentation on theme: "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."— Presentation transcript:

1 Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

2 Runtime Organization (Chapter 6) 2 Heap Storage Allocation Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

3 Runtime Organization (Chapter 6) 3 Heap Storage Allocation Recap: We have seen 2 storage allocation models so far: static allocation global variables exist “forever” stack allocation local variables and arguments for procedures and functions lifetime is from when procedure called until procedure returns Next: Heap allocation. Memory allocation is explicitly under the programmer’s control. (C: malloc, C ++ / Java / Pascal: new operation) Example: (Java) int[ ] nums = new int[sizeExpression];

4 Runtime Organization (Chapter 6) 4 Heap Storage Allocation Explicit versus Implicit Deallocation Examples: Implicit: Java, Scheme Explicit: Pascal, C, C ++ To free heap memory a specific operation must be called. Pascal ==> dispose C ==> free C ++ ==> delete In explicit memory management, the program must explicitly call an operation to release memory back to the memory management system. In implicit memory management, heap memory is reclaimed automatically by a “garbage collector”.

5 Runtime Organization (Chapter 6) 5 Heap Storage Allocation Memory managers (both implicit as well as explicit) have been studied extensively by researchers. => Algorithms for fast allocation / deallocation of memory and low memory overhead for memory management administration. => There are many complicated, sophisticated, and interesting algorithms. We could dedicate an entire course to this topic alone! However, we only have time to look at memory management superficially. We will look at the kinds of algorithms/issues associated with explicit and implicit memory management.

6 Runtime Organization (Chapter 6) 6 Where to put the heap? The heap is an area of memory which is dynamically allocated. Like a stack, it may grow and shrink during runtime. (Unlike a stack, a heap is not LIFO => more complicated to manage.) In a typical programming language implementation, we will have both heap-allocated and stack-allocated memory that co-exist. Q: How do we allocate memory for both? A simple approach is to divide the available memory at the start of the program into two areas: stack and heap. Another question then arises: - How do we decide what portion to allocate for stack vs. heap? - Issue: if either one of the areas is full, then even though memory is available (in the other area) we will get out-of-memory errors. Q: Isn’t there a better way?

7 Runtime Organization (Chapter 6) 7 Where to put the heap? Q: Isn’t there a better way? A: Yes, there is an often used “trick”: let both stack and heap share the same memory area, but grow towards each other from opposite ends! ST SB HB HT Stack memory area Heap memory area Stack grows downward Heap can expand upward

8 Runtime Organization (Chapter 6) 8 How to keep track of free memory? Stack is LIFO allocation => ST moves up/down. Everything above ST is in use/allocated. Below ST is free memory. This part is easy! Heap is not LIFO, how to manage free space in the “middle” of the heap? HB HT Allocated ST SB Free Mixture of Allocated and Free space How to reuse?

9 Runtime Organization (Chapter 6) 9 How to keep track of free memory? How to manage free space in the “middle” of the heap? HB HT => Keep track of free blocks in a data structure: the “free list”. For example use a linked list pointing to the free blocks. Free Next freelist Free Next A freelist! Good idea! But where do we find the memory to store this data structure? A freelist! Good idea! But where do we find the memory to store this data structure?

10 Runtime Organization (Chapter 6) 10 How to keep track of free memory? HB HT Q: Where do we find the memory to store a freelist data structure? A: Since the free blocks are not used for anything by the program => memory manager can use them for storing the freelist itself. HF free block size next free

11 Runtime Organization (Chapter 6) 11 Simple Explicit Memory Manager Algorithm We next consider a memory manager that is intended for a programming language with explicit deallocation of memory. Q: What operations does the memory management library provide? Pointer malloc(int size); Asks memory manager to find and allocate a block of specified size. Returns a pointer to beginning of that block. void free(Pointer toFree, int size); Called when a block toFree is released by the program and returned to the memory manager.

12 Runtime Organization (Chapter 6) 12 Simple Explicit Memory Manager Algorithm Pointer malloc(int size) { block = search the freelist until a block of at least size is found. if (block is found) and (block.size == size) delete block from free list. return pointer to start of block. else if (block found) and (block.size > size) delete block from free list. insert remainder of block into free list. return pointer to start of block. else // no block found try to expand heap by size. if (expansion was successful) then return HT; else generate out-of-memory error. }

13 Runtime Organization (Chapter 6) 13 Simple Explicit Memory Manager Algorithm Pointer free(Pointer toFree, int size) { insert block(toFree,size) into the freelist } Pointer free(Pointer toFree, int size) { toFree[0] = size; // size at offset 0 toFree[1] = HF; // next pointer at offset 1 HF = toFree; } This algorithm is rather simplistic and has some rather big problems. Q: What are those problems? How could we fix them? A bit more detail:

14 Runtime Organization (Chapter 6) 14 Fragmentation One of the biggest problems with the simplistic algorithm is memory fragmentation. blocks are split to create smaller blocks to fit the requested size. blocks are never merged. => Blocks will continually get smaller and smaller. Better algorithm merges released blocks with neighboring blocks that are free. => less fragmentation Q1: Analyze impact on the performance (time) for allocating and releasing blocks. Q2: Does this solve the fragmentation problem completely?

15 Runtime Organization (Chapter 6) 15 Heap Compaction To fight fragmentation, some memory management algorithms perform “heap compaction” once in a while. HB HT HF a b c HB HT a b d d c beforeafter Q: Can compaction be done for all programming language implementations?

16 Runtime Organization (Chapter 6) 16 Complication with Explicit Memory Allocation “Problems” with explicit memory allocation algorithms: Two common, nasty kinds of bugs in programs using explicit memory management are Memory leaks (garbage) Dangling pointers Q1: What are memory leaks and dangling pointers? Q2: Is it true that there can be no memory leaks in a garbage- collected language? Q3: Is it true that there can be no dangling pointers in a garbage- collected language?

17 Runtime Organization (Chapter 6) 17 Automatic Storage Deallocation (Garbage Collection) Everybody probably knows what a garbage collector is. But here are two statements to make you think about what a garbage collector really does. 1. A garbage collector provides the “illusion of infinite memory”. 2.A garbage collector predicts the future: it runs when “almost” about to run out of memory. Next we consider one of the most often used garbage collection algorithms (if time permits).

18 Runtime Organization (Chapter 6) 18 Mark and Sweep Garbage Collection HT e c a HB SB ST b d before gc HT e c a SB ST b d HB mark as free phase X X X X X

19 Runtime Organization (Chapter 6) 19 Mark and Sweep Garbage Collection HT e c a SB ST b d HB mark as free phase X X X X X HT e c a SB ST b d HB X X X X X X X X mark reachable SB ST HB HT e b d X X X X X X collect free

20 Runtime Organization (Chapter 6) 20 Mark and Sweep Garbage Collection Algorithm pseudo code: void garbageCollect( ) { mark all heap variables as free for each frame in the stack scan(frame) for each heapvar that is still marked as free add heapvar to freelist } void scan(region) { for each pointer p in region if p points to region marked as free then mark region at p as reachable scan(region at p ) } Q: This algorithm is recursive. What do you think about that?

21 Runtime Organization (Chapter 6) 21 Object Orientation Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

22 Runtime Organization (Chapter 6) 22 Runtime Organization for OO Languages What is this about? How to represent/implement object-oriented constructs such as objects, classes, methods, instance variables, and method calls. Some definitions and relationships for these concepts: An object is a group of instance variables to which a group of instance methods is attached. An instance variable is a named component of a particular object. An instance method is a named operation that is attached to a particular object and that is able to access that object’s instance variables. A class is a family of objects with similar instance variables and identical methods.

23 Runtime Organization (Chapter 6) 23 Runtime Organization for OO Languages Objects are a lot like records and instance variables are a lot like fields. => The representation of objects is similar to that of a record. Methods are a lot like procedures/functions. => Implementation of methods is similar to routines. But there are differences: Objects have methods as well as instance variables, whereas records only have fields. The methods have to somehow know what object they are associated with (so that methods can access the object’s instance variables).

24 Runtime Organization (Chapter 6) 24 Example: Representation of a simple Java object Example 1: a simple Java object (no inheritance) class Point { int x, y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area( ) { return 0.0; } public float dist(Point other) {... } } class Point { int x, y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area( ) { return 0.0; } public float dist(Point other) {... } } (1) (2) (3) (4)

25 Runtime Organization (Chapter 6) 25 Example: Representation of a simple Java object Example: a simple Java object (no inheritance) Point class Point move area dist constructor(1) method(2) method(3) method(4) Point p = new Point(2,3); Point q = new Point(0,0); pqpq class xyxy 2323 xyxy 0000 new allocates an object in the heap

26 Runtime Organization (Chapter 6) 26 Inheritance Example 2: Points and other “shapes” abstract class Shape { int x, y; // “origin” of the shape public Shape(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public abstract float area( ); public float dist(Shape other) {... } } abstract class Shape { int x, y; // “origin” of the shape public Shape(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public abstract float area( ); public float dist(Shape other) {... } } (S1) (S2) (S3)

27 Runtime Organization (Chapter 6) 27 class Point extends Shape { public Point(int x, int y) { super(x,y); } public float area( ) { return 0.0; } } class Point extends Shape { public Point(int x, int y) { super(x,y); } public float area( ) { return 0.0; } } Inheritance Example 2: Points and other “shapes” (P1) (P2)

28 Runtime Organization (Chapter 6) 28 Inheritance Example 2: Points and other “shapes” class Circle extends Shape { int r; public Circle(int x, int y, int r) { super(x,y); this.r = r; } public int radius( ) { return r; } public float area( ) { return 3.14 * r * r; } class Circle extends Shape { int r; public Circle(int x, int y, int r) { super(x,y); this.r = r; } public int radius( ) { return r; } public float area( ) { return 3.14 * r * r; } (C1) (C3) (C2)

29 Runtime Organization (Chapter 6) 29 Inheritance Shape class Shape move area dist constru(S1) method(S2) method(S3) Circle class Circle move area dist constru(C1) method(S2) method(C3) method(S3) radius method(C2) Inherited from shape Point class Point move area dist constru(P1) method(S2) method(S3) method(P2) Note the similar layout of each class object. Q: Why is that important? Q: Why don’t we need a pointer to the super-class in a class object?

30 Runtime Organization (Chapter 6) 30 Inheritance Shape[ ] s = new Shape[2]; s[0] = new Point(2,3); s[1] = new Circle(4,5,6); s class xyrxyr 456456 xyxy 2323 Point class Circle class Note the similar layout between Point and Circle objects! s[0] s[1] s[0].x =...; s[1].y =...; float sumAreas = s[0].area( ) +s[1].area( );


Download ppt "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."

Similar presentations


Ads by Google