CS 153: Concepts of Compiler Design November 28 Class Meeting

Slides:



Advertisements
Similar presentations
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Advertisements

Garbage Collection What is garbage and how can we deal with it?
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
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.
CS 536 Spring Run-time organization Lecture 19.
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:
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.
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
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Basic Semantics Associating meaning with language entities.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
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 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 149: Operating Systems March 5 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
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.
Design issues for Object-Oriented Languages
Storage Allocation Mechanisms
Garbage Collection What is garbage and how can we deal with it?
CS 326 Programming Languages, Concepts and Implementation
CS 153: Concepts of Compiler Design October 5 Class Meeting
Dynamic Memory Allocation
Storage Management.
Compilers.
CS 153: Concepts of Compiler Design October 3 Class Meeting
Run-time organization
Concepts of programming languages
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
Dynamically Allocated Memory
CMPE 152: Compiler Design October 4 Class Meeting
Main Memory Background Swapping Contiguous Allocation Paging
CMPE 152: Compiler Design October 4 Class Meeting
Closure Representations in Higher-Order Programming Languages
Topic 3-b Run-Time Environment
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 152: Compiler Design March 7 Class Meeting
Run Time Environments 薛智文
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
CMPE 152: Compiler Design May 2 Class Meeting
SPL – PS2 C++ Memory Handling.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

CS 153: Concepts of Compiler Design November 28 Class Meeting Department of Computer Science San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Extra-Credit Oral Presentations Let me know if your team would like to do an oral presentation for extra credit. Tell about your language. Show some example programs. Describe its grammar Show syntax diagrams. What Jasmin code do you generate? Show some code diagrams. Demo: Compile, execute, and run some sample programs.

Extra-Credit Oral Presentations, cont’d Submit a note by Friday, Dec. 1 into Canvas: Assignments | Miscellaneous | Presentation if your team wants to present. Choose either Dec. 5 or 7 to present. 15-20 minutes. Can add up to 50 points to each team member’s total assignment score.

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 Java 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 Java 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. 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 ...

Java Command-Line Arguments java -Xms<size> -Xmx<size> ms: initial heap size mx: maximum heap size <size>: size in megabytes (m|M) or gigabytes (g|G) Example: java -Xms512M -Xmx2G HelloWorld

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. Set the initial and maximum heap size using the -Xms and -Xmx options. Automatic garbage collection.

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. 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. How?

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 end of the run of the program,

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?