CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
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.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Compiler Summary Mooly Sagiv html://
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.
CS 153: Concepts of Compiler Design October 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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.
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.
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
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.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CPS 506 Comparative Programming Languages Syntax Specification.
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 October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 21 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 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 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.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Design issues for Object-Oriented Languages
CS 153: Concepts of Compiler Design October 5 Class Meeting
Dynamic Memory Allocation
Compilers.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design November 29 Class Meeting
Binding Times Binding is an association between two things Examples:
Dynamic Memory.
CMPE 152: Compiler Design March 7 Class Meeting
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 2  A grammar defines a language.  Grammar The set of all the BNF rules (or syntax diagrams)  Language The set of all the legal strings of tokens... … according to the grammar  Legal string of tokens A syntactically correct statement _ Grammars and Languages

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 3 Grammars and Languages, cont’d  A statement is in the language (it’s syntactically correct) if it can be derived by the grammar.  Each grammar rule “produces” a token string in the language.  The sequence of productions required to arrive at a syntactically correct token string is the derivation of the string. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 4 Grammars and Languages  Example: A very simplified expression grammar:  What strings (expressions) can we derive from this grammar? _ ::= | | ( ) ::= 0|1|2|3|4|5|6|7|8|9 ::= + | *

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 5 Derivations and Productions  Is (1 + 2)*3 valid in our expression language?  Yes! The expression is valid. ::= 1  ( )*3 ::=  ( + 2 )*3 ::= +  ( + 2 )*3 ::= 2  ( 2 )*3 ::=  ( )*3 ::=  ( )*3 ::= ( )  ( )*3 ::= *  *3 ::= 3  3 ::=  ::=  GRAMMAR RULEPRODUCTION ::= | | ( ) ::= 0|1|2|3|4|5|6|7|8|9 ::= + | *

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 6 Extended BNF (EBNF) { } Surround items to be repeated zero or more times. [ ] ( )? Surround optional items.  Extended BNF (EBNF) adds meta-symbols { } and [ ]

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 7 Extended BNF: Repetition (One or More)  BNF: ::= |  EBNF: ::= { }

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 8 Extended BNF: Optional Items  BNF: ::= |.  EBNF: or: ::= [. ] ::= (. )?

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 9 JavaCC Compiler-Compiler  Feed JavaCC the grammar for a source language and it will automatically generate a scanner and a parser written in Java. Specify the source language tokens with regular expressions  JavaCC generates a scanner for the source language. Specify the source language syntax rules with Extended BNF  JavaCC generates a parser for the source language.

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 10 JavaCC Regular Expressions  Literals  Character classes  Character ranges _ Token name Token string

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 11 JavaCC Regular Expressions, cont’d  Alternates  Negation  Repetition _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 12 JavaCC Regular Expressions, cont’d  Quantifiers _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 13 JavaCC Parser Specification EBNF : JavaCC: TOKEN : { ){4}> | ){3}> | } void PhoneNumber() : {} { "-" "-" } Token specifications Production rule Java statements can go in here! ::= 0|1|2|3|4|5|6|7|8|9 ::= ::= - - Demo Terminal Literal Terminal Nonterminal

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 14 Static Scoping  Most modern languages use 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.  Static scoping in Java: What value is returned by calling function g() ? int x = 0; int f() { return x ; } int g() { int x = 1; return f(); } What is the binding of this x with static scoping?

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 15 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. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 16 Dynamic Scoping, cont’d  Hypothetical dynamic scoping in Java: What value is returned by calling function g() ? Call chain: main  g  f How would you implement runtime dynamic scoping? int x = 0; int f() { return x ; } int g() { int x = 1; return f(); } What is the binding of this x with dynamic scoping?

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 17 Runtime Memory Management  In the Scheme, local data is kept on the runtime stack. RUNTIME STACK AR: main x AR: lambda a 2 2 (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) (define x 2) (define y 3) (proc x y) y 3 b 3 AR: let sum 5

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 18 Runtime Memory Management, cont’d  All memory for the parameters and variables declared locally by a routine is allocated in the routine’s activation record.  The memory is later automatically deallocated when the activation record is popped off the stack.  What about dynamically allocated data? Memory for dynamically allocated data is kept in the “heap”. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 19 Runtime Memory Management  Divide runtime memory into four partitions. 1. Static memory executable code statically-allocated data 2. Runtime stack activation records that contain locally-scoped data 3. Heap dynamically-allocated data 4. Free memory Executable code Statically-allocated data Runtime stack Heap Top of stackHeap limit Free memory Avoid: Heap-stack collision (Out of memory error)

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 20 The Java Virtual Machine (JVM) Architecture

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 21 Java Command-Line Arguments  java -Xms -Xmx ms : initial heap size mx : maximum heap size : size in megabytes ( m | M ) or gigabytes ( g | G ) Example: java -Xms512M -Xmx2G HelloWorld

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 22 Runtime Heap Management  Handled by language-specific runtime routines.  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.

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 23 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. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 24 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. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 25 Runtime Heap Management, cont’d  What’s the optimal 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?

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 26 Garbage Collection  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

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 27 Garbage Collection, cont’d  Automatic garbage collection is great for programmers, because they don’t have to think about it. But it can slow runtime performance unpredictably. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 28 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.

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 29 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. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 30 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? _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 31 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. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 32 Generational Garbage Collection, cont’d  Therefore, 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.

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 33 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.

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 34 Aggressive Heap Management, cont’d  Tradeoffs GC operations slow program performance. But paging to disk can be orders of magnitude slower. _

SJSU Dept. of Computer Science Spring 2014: April 16 CS 152: Programming Language Paradigms © R. Mak 35 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? _