YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

1

CS 153: Concepts of Compiler DesignDecember 7 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2015Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

2

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.

Page 3: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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 determinedat 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: proc3

AR: func3

AR: func2

AR: proc2

RUNTIMEDISPLAY

1

2

3

Page 4: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

4

int x = 0; int f() { return x ; } int g() { int x = 1; return f(); }

Static Scoping in Java

What is the bindingof this x with static scoping?

What value is returned by calling function g()?

Page 5: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 6: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

6

int x = 0; int f() { return x ; } int g() { int x = 1; return f(); }

Hypothetical Dynamic Scoping in Java

What value is returned by calling function g()?

Call chain: main g f

How would you implementruntime dynamic scoping?

What is the bindingof this x withdynamic scoping?

Page 7: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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: proc3

AR: func3

AR: func2

AR: proc2

RUNTIMEDISPLAY

1

2

3

The memory is later automatically deallocated when the activation record is popped off the stack

Page 8: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 9: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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 stack

Heap limit

Free memory

Avoid: Heap-stack collision(Out of memory error)

Page 10: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

10

Recall the JVM Architecture ...

Page 11: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

11

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

Page 12: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 13: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 14: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 15: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 16: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 17: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 18: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 19: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 20: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 21: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 22: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 23: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 24: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 25: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 26: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 27: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 28: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 29: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 30: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 31: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 32: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 33: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 34: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

executorcode generator

symbol tableparse tree

Page 35: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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?

Page 36: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 37: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 38: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 39: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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.

Page 40: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 41: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 42: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 43: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 44: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 45: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 46: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 47: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 48: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 49: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

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

Page 50: CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

Computer Science Dept.Fall 2014: November 26

CS 153: Concepts of Compiler Design© R. Mak

50

Course Review

Was this course “deep” enough?


Related Documents