CMPE 152: Compiler Design May 2 Class Meeting

Slides:



Advertisements
Similar presentations
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
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 Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Run time vs. Compile time
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
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.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Lecture 10 : Introduction to Java Virtual Machine
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
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.
COMP3190: Principle of Programming Languages
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 160 and CMPE/SE 131 Software Engineering May 12 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Memory Management in Java Mr. Gerb Computer Science 4.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Storage Allocation Mechanisms
Garbage Collection What is garbage and how can we deal with it?
Processes and threads.
CS 326 Programming Languages, Concepts and Implementation
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
Dynamic Memory Allocation
Compilers.
Run-time organization
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE Database Systems Exercise #1 Solutions
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
Closure Representations in Higher-Order Programming Languages
Topic 3-b Run-Time Environment
CMPE 135: Object-Oriented Analysis and Design December 6 Class Meeting
CMPE 152: Compiler Design December 6 Class Meeting
CMPE 152: Compiler Design November 29 Class Meeting
Binding Times Binding is an association between two things Examples:
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Operating System Chapter 7. Memory Management
CMPE 152: Compiler Design March 7 Class Meeting
Run Time Environments 薛智文
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CS 144 Advanced C++ Programming April 25 Class Meeting
Automating Memory Management
CS 144 Advanced C++ Programming April 30 Class Meeting
Run-time environments
CMPE 152: Compiler Design April 30 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

CMPE 152: Compiler Design May 2 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Unofficial Field Trip Computer History Museum in Mt. View http://www.computerhistory.org/ Provide your own transportation to the museum. Saturday, May 4, 11:30 – closing time Special free admission. We will meet in the lobby. No backpacks. Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation. Do a self-guided tour of the Revolution exhibit.

Extra Credit! A team can give a short oral presentation to the rest of the class. Show off your language. Do a demo compilation and run of a sample program. About 15 minutes per presentation. Let me know if you’re interested. Add up to 50 points to the total assignment score of each team member. All team members should be present for the demo.

Extra Credit! cont’d Submit a note into Canvas: Assignments | Miscellaneous | Presentation date if your team wants to present. Choose either Tuesday, May 7 or Thursday, May 9 to present.

Static Scoping Pascal uses static (lexical) scoping. Scopes are determined at compile time by how the routines are nested in the source program. At runtime, variables are bound to their values as determined by the lexical scopes. The runtime display helps do the bindings. PROGRAM main1 FUNCTION func2 FUNCTION func3 PROCEDURE proc2 PROCEDURE proc3 RUNTIME STACK main1  proc2  proc3  func2  func3 AR: main1 AR: proc2 AR: proc3 AR: func3 AR: func2 RUNTIME DISPLAY 1 2 3

Static Scoping in C++ What value is returned by calling function g()? What is the binding of this x with static scoping? int x = 0; int f() { return x ; } int g() { int x = 1; return f(); }

Dynamic Scoping With dynamic scoping, runtime binding of variables to their values is determined by the call chain. To determine a variable’s binding, search the call chain backwards starting with the currently active routine. The variable is bound to the value of the first declared variable found with the same name.

Hypothetical Dynamic Scoping in C++ What value is returned by calling function g()? Call chain: main  g  f What is the binding of this x with dynamic scoping? int x = 0; int f() { return x ; } int g() { int x = 1; return f(); } How would you implement runtime dynamic scoping?

Runtime Memory Management In the “Pascal Virtual Machine”, all local data is kept on the runtime stack. All memory for the parameters and variables declared locally by a routine is allocated in the routine’s activation record (stack frame). PROGRAM main1 FUNCTION func2 FUNCTION func3 PROCEDURE proc2 PROCEDURE proc3 RUNTIME STACK main1  proc2  proc3  func2  func3 AR: main1 AR: proc2 AR: proc3 AR: func3 AR: func2 RUNTIME DISPLAY 1 2 3 The memory is later automatically deallocated when the activation record is popped off the stack

Runtime Memory Management, cont’d What about dynamically allocated data? Memory for dynamically allocated data is kept in the “heap”.

Runtime Memory Management, cont’d Runtime memory can be divided into four partitions: Static memory executable code statically-allocated data Runtime stack activation records that contain locally-scoped data Heap dynamically-allocated data such as Java objects Free memory Heap Heap limit Free memory Top of stack Runtime stack Statically-allocated data Executable code Avoid: Heap-stack collision (Out of memory error)

Recall the JVM Architecture ...

Runtime Heap Management Handled by language-specific runtime routines. Pascal, C, and C++ Call new/malloc to allocate memory. Call dispose/free to de-allocate. Java Call new to allocate memory. Automatic garbage collection. New C++ “smart pointers” do automatic deletes.

Runtime Heap Management, cont’d Keep track of all allocated blocks of memory and all free blocks. Free blocks are “holes” caused by freeing some of the dynamically allocated objects.

Runtime Heap Management, cont’d When a new block of memory needs to be allocated dynamically, where should you put it? You can allocate it at the end of the heap, and thereby expand the size of the heap. You can find a hole within the heap that’s big enough for the block.

Runtime Heap Management, cont’d What’s the optimal memory allocation strategy? Find the smallest possible hole that the block will fit in. Randomly pick any hole that’s big enough.

Runtime Heap Management, cont’d Should you periodically compact the heap to get rid of holes and thereby reduce the size of the heap? If allocated data moves due to compaction, how do you update references (pointers) to the data?

Garbage Collection Return a block of memory (“garbage”) to unallocated status … … when there are no longer any references to it. Various algorithms to locate garbage. reference counts mark and sweep stop and copy generational

Automatic Garbage Collection Automatic garbage collection is great for programmers, because you don’t have to think about it. But it can slow runtime performance unpredictably.

Garbage Collection: Reference Counts Include a counter with each block of allocated memory. Increment the counter each time a pointer is set to point to the block. Decrement the counter whenever a pointer to the block is set to null (or to point elsewhere). Deallocate the block when the counter reaches 0. Problem: Cyclic graphs The reference counts never become 0.

Garbage Collection: Mark and Sweep Make a pass over the heap to mark all allocated blocks of memory that are reachable via pointers. Various marking algorithms. Make a second pass to sweep (deallocate) blocks that are not marked.

Garbage Collection: Stop and Copy Partition the heap into two halves. Allocate memory only in one half at a time. When an allocation fails to find a sufficiently large block of free memory … Stop Copy all allocated blocks to the other half of the heap, thereby compacting it. Update all references to the allocated blocks. How do you update pointer values?

Generational Garbage Collection: Theory Most objects are short-lived. Long-lived object are likely to last until the program terminates.

Generational Garbage Collection: Practice Partition the heap into a new generation area and an old generation area. Allocate new objects in the new generation area. Keep track of how long an object lives. Once an object lives past a predetermined threshold of time, migrate it to the old generation area. The old generation area stays fairly compacted. The new generation area needs compacting infrequently.

Aggressive Heap Management Aggressive heap management means doing garbage collection frequently, even when it’s not necessary. There’s still adequate free space remaining in the heap area. Keep the heap as small as possible. Improve reference locality. Optimize the use of physical memory when multiple programs are running. Reduce virtual memory paging.

Aggressive Heap Management: Tradeoff GC operations slow program performance. But paging to disk can be orders of magnitude slower.

Garbage Collection Research Entire books have been written about garbage collection. It’s still an area with opportunities for research. You can become famous by inventing a better GC algorithm! Maybe some form of adaptive GC using machine learning?