Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

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

2 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak Online Course Evaluations  Evaluation period closes Tuesday, Dec. 8.  If you don’t fill out the online SOTES by Dec. 8, you will have a 3-week delay in the release of your grades. 2

3 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 3 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  proc2 AR: main1 AR: proc2 AR: proc3 AR: func3 AR: func2 AR: proc2 RUNTIME DISPLAY 1 2 3

4 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak int x = 0; int f() { return x ; } int g() { int x = 1; return f(); } 4 Static Scoping in Java What is the binding of this x with static scoping?  What value is returned by calling function g() ?

5 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 5 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.

6 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak int x = 0; int f() { return x ; } int g() { int x = 1; return f(); } 6 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? What is the binding of this x with dynamic scoping?

7 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 7 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  proc2 AR: main1 AR: proc2 AR: proc3 AR: func3 AR: func2 AR: proc2 RUNTIME DISPLAY 1 2 3 The memory is later automatically deallocated when the activation record is popped off the stack

8 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 8 Runtime Memory Management, cont’d  What about dynamically allocated data?  Memory for dynamically allocated data is kept in the “heap”.

9 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 9 Runtime Memory Management, cont’d  Runtime memory can be divided 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 such as Java objects 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)

10 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 10 Recall the JVM Architecture...

11 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 11 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

12 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 12 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.

13 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 13 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.

14 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 14 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.

15 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 15 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?

16 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 16 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

17 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 17 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?

18 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 18 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.

19 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 19 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.

20 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 20 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?

21 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 21 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.

22 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 22 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.

23 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 23 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.

24 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 24 Aggressive Heap Management: Tradeoff  GC operations slow program performance.  But paging to disk can be orders of magnitude slower.

25 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 25 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?

26 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 26 Compiler Projects  Your.jjt file and a zip file of your src directory. Name the zip file after your team name.  A written report (5-10 pp.) that includes: A high-level description of the design of the compiler. UML diagrams of the major classes are acceptable. The grammar for your source language, either as syntax diagrams or in BNF (use JJDoc!). Code templates that show the Jasmin code your compiler generates for some key constructs of the source language.

27 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 27 Compiler Projects  Sample source programs to compile and execute on the JVM.  Text files of the output from executing your source programs.  Due Wednesday, December 9

28 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 28 Postmortem Reports  Due Wednesday, December 9 at 11:59 PM A few paragraphs.  Word document or just an email message Individual and private.  What did you learn in this class?  What were your accomplishments on your project team?  How well did each of your teammates do?

29 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 29 Final Exam  Wednesday, December 16 7:15-9:30 AM in DH 450  It will be similar to the midterm. Covers the entire semester.

30 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 30 Course Review  Lectures and PowerPoint slide sets  Reading assignments  Homework assignments  Compiler project

31 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 31 Course Review  Good understanding of compiler concepts Front end: parser, scanner, and tokens Intermediate tier: symbol table and parse trees Back end: interpreter and code generator The JavaCC compiler-compiler  Basic understanding of Pascal

32 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 32 Course Review  What is the overall architecture of a compiler or an interpreter? What are the source language-independent and -dependent parts? What are the target machine-independent and -dependent parts?  How can we manage the size and complexity of a compiler or an interpreter during its development?

33 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 33 Course Review  What are the main characteristics of a top-down recursive-descent parser?  Of a bottom-up parser?  What is the basic control flow through an interpreter as a source program is read, translated, and executed?  Through a compiler for code generation?

34 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 34 Course Review  How do the various components work with each other? parser  scanner scanner  source program parser  symbol table parser  parse tree executor code generator symbol table parse tree 

35 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 35 Course Review  What information is kept in a symbol table? When is a symbol table created? How is this information structured? How is this information accessed?  What information is kept in a parse tree? When is a parse tree created? How is this information structured? How is this information accessed?

36 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 36 Course Review  What is the purpose of the symbol table stack runtime stack runtime display operand stack parse stack

37 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 37 Course Review  Define or explain syntax and semantics syntax diagrams and BNF syntax error handling runtime error handling type checking

38 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 38 Course Review  Deterministic finite automaton (DFA) start state accepting state transitions state transition table table-driven DFA scanner

39 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 39 Course Review  What information is kept in an activation record or stack frame? How is this information initialized? What happens during a procedure or function call?  How to pass parameters by value by reference ... with an interpreter vs. with generated object code.

40 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 40 Course Review  The Java Virtual Machine (JVM) architecture  Runtime stack  Stack frame operand stack local variables array program counter

41 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 41 Course Review  The Jasmin assembly language instructions explicit operands operands on the stack standard and “short cut” type descriptors

42 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 42 Course Review  Jasmin assembler directives:.class.super.limit.field.var.method.line.end

43 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 43 Course Review  Basic concepts of the JavaCC compiler-compiler  Tokens specification with regular expressions SKIP tokens  Production rules specification with extended BNF (EBNF) choice conflict left recursion lookahead

44 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 44 Course Review  Error handling and recovery  Basic concepts of JJTree preprocessor to JavaCC generated tree nodes visitor design pattern

45 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 45 Course Review  Code generation and code templates expressions assignment statements conditional statements looping statements arrays and records

46 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 46 Course Review  Compiling procedures and functions fields and local variables call and return passing parameters

47 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 47 Course Review  Total compiler integration JJTree + JavaCC + symbol table + code generation tree structures  Multipass compilers type checking pass with the visitor pattern optimization pass code generation pass with the visitor pattern

48 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 48 Course Review  Integrating Jasmin routines with Java routines Pascal runtime library  Instruction selection  Instruction scheduling  Register allocation spilling values live variables

49 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 49 Course Review  Optimization for performance constant folding constant propagation strength reduction dead code elimination loop unrolling common subexpression elimination

50 Computer Science Dept. Fall 2014: November 26 CS 153: Concepts of Compiler Design © R. Mak 50 Course Review Was this course “deep” enough?


Download ppt "CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google