CS143 Final Spring 2016

Similar documents
CS143 Final Fall 2009

Midterm II CS164, Spring 2006

Final Examination. Winter Problem Points Score. Total 180

Solutions to Written Assignment 4

CS164: Midterm I. Fall 2003

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Spring 2017 CS 1110/1111 Exam 1

Operational Semantics. One-Slide Summary. Lecture Outline

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

CS143 Midterm Spring 2014

Operational Semantics of Cool

Compiler Construction

Heap Management. Heap Allocation

Structure of Programming Languages Lecture 10

CS 406/534 Compiler Construction Putting It All Together

Computer Science 320: Final Examination

Final Exam CS164, Fall 2007 Dec 18, 2007

CS 2505 Computer Organization I

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CSE351 Spring 2018, Final Exam June 6, 2018

cs173: Programming Languages

Compilers. Cool Semantics II. Alex Aiken

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

CS4215 Programming Language Implementation

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Operational Semantics of Cool

ESC101N: Fundamentals of Computing End-sem st semester

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CIS 341 Final Examination 3 May 2011

1 Lexical Considerations

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Run-time Environments

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

Run-time Environments

Midterm I - Solution CS164, Spring 2014

CSCE 206: Structured Programming in C++

Compiler Construction

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Programming Project 4: COOL Code Generation

CS61, Fall 2012 Section 2 Notes

CS143 Compilers - Written Assignment 4

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CS558 Programming Languages

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Homework I - Solution

Qualifying Exam Languages

CS558 Programming Languages

CS558 Programming Languages

Run-Time Environments/Garbage Collection

Project 5: Extensions to the MiniJava Compiler

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Names, Scope, and Bindings

CS3330 Exam 3 Spring 2015

CS143 Midterm Fall 2008

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

2. Reachability in garbage collection is just an approximation of garbage.

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Lexical Considerations

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Name: CIS 341 Final Examination 10 December 2008

Pierce Ch. 3, 8, 11, 15. Type Systems

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

COMP-520 GoLite Tutorial

CS 553 Compiler Construction Fall 2006 Project #4 Garbage Collection Due November 27, 2005

CS558 Programming Languages

CS143 - Written Assignment 4 Reference Solutions

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CS140 Operating Systems and Systems Programming Final Exam

MIDTERM EXAMINATION - CS130 - Spring 2003

Problem Score Max Score 1 Syntax directed translation & type

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Dynamic Storage Allocation

Programming Assignment IV Due Thursday, November 18th, 2010 at 11:59 PM

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CS107 Handout 43 Spring 2007 June 8 th, 2007 CS107 Final Exam

Habanero Extreme Scale Software Research Project

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

MODEL ANSWERS COMP36512, May 2016

Lexical Considerations

Written Assignment 5 Due??

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Lecture 13: Garbage Collection

Operational Semantics of Cool

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Transcription:

CS143 Final Spring 2016 Please read all instructions (including these) carefully. There are 5 questions on the exam, all with multiple parts. This exam is designed to take 2 hours, but you have the full 3 hours to complete the exam. The exam is open note. You may use laptops, phones and e-readers to read electronic notes, but not for computation or access to the internet for any reason. Please write your answers in the space provided on the exam, and clearly mark your solutions. Do not write on the back of exam pages or other pages. Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. You may get as few as 0 points for a question if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit. NAME: In accordance with both the letter and spirit of the Honor Code, I have neither given nor received assistance on this examination. SIGNATURE: Problem Max points Points 1 30 2 20 3 30 4 20 5 20 TOTAL 120 1

1. Language Design (30 points) Consider extending COOL with exceptions. There are two new expressions. The first throws an exception: throw e The expression e must evaluate to a string, and that string is propagated as an exception value. Except for the try-catch expression described below, for any other kind of expression e, if a subexpression of e throws an exception then e throws that same exception. This is described further below. Exception values can be caught using a try-catch statement: try e1 catch x in e2 In this statement, expression e1 is evaluated first. If it completes normally with a value v (that is, without throwing an exception), then the whole try-catch expression completes normally with value v. Otherwise, if e1 throws an uncaught exception, the variable x is bound to the exception string, and e2 is evaluated. As examples, the expression try 1 catch x in 2 evaluates to Int(1), and try (throw "a") catch x in x evaluates to the object String(1, a). To give operational semantics to exceptions, we need to give the operational semantics for the two new statements. However, we also need to define the exception propagation behavior for existing constructs in the language. We introduce Exc(m) (where m is a string object) as a new kind of value that expressions can evaluate to i.e., expressions can evaluate to objects (as usual) or exceptions. The behavior of exceptions is different from the behavior of objects. For example, consider an arithmetic operation e1+e2: If the first operand e1 evaluates to Exc(m), then the whole expression evaluates to Exc(m). If e1 completes normally with a value v and e2 evaluates to Exc(m), then the whole expression evaluates to Exc(m). Otherwise (i.e., both expressions evaluate normally), the usual rule from the COOL manual applies. Formally, the rules for the propagation of exceptions in addition expressions are: so, S 1, E e 1 Exc(m), S 2 so, S 1, E e 1 + e 2 Exc(m), S 2 so, S 1, E e 1 Int(i 1 ), S 2 so, S 2, E e 2 Exc(m), S 3 so, S 1, E e 1 + e 2 Exc(m), S 3 2

(a) Give the operational semantics for throw and try-catch expressions. Make sure you cover both exception propagation as well as regular executions. so, S 1, E e Exc(m), S 2 so, S 1, E throw e Exc(m), S 2 so, S 1, E e v, S 2 so, S 1, E throw e Exc(v), S 2 so, S 1, E e 1 v, S 2 so, S 1, E try e 1 catch x in e 2 v, S 2 so, S 1, E e 1 Exc(m), S 2 l = newloc(s 2 ) so, S 2 [m/l], E[l/x] e 2 v, S 3 so, S 1, E try e 1 catch x in e 2 v, S 3 so, S 1, E e 1 Exc(m), S 2 l = newloc(s 2 ) so, S 2 [m/l], E[l/x] e 2 Exc(m), S 3 so, S 1, E try e 1 catch x in e 2 Exc(m), S 3 3

(b) Give sound type-checking rules for throw and try-catch expressions. Hint: Make sure that your type-checking rules can correctly type-check the following program (you don t have to show the derivation): if 1 < 2 then throw "error" else 1 + 2 O, M, C e : String O, M, C throw e : T O, M, C e 1 : T 1 O[String/x], M, C e 2 : T 2 O, M, C try e 1 catch x in e 2 : lub(t 1, T 2 ) 4

2. Garbage Collection (20 points) Assume we are programming in a safe language like Java or python. Consider the following fragment of code: class Foo { Int i; } for (Int j = 0; j < 1000000000; j++) { // Allocate memory Foo data = new Foo; // Allocates a Foo object on the heap. // Generate random number data.i = random_number(); } // Print random number print("number is %d\n", data.i); (a) Which GC Algorithm (Reference Counting, Mark and Sweep, Stop and Copy) is best suited (gives the fastest start-to-finish execution times) for this code? Justify your choice. Stop-and-Copy, because the GC cost is proportional to the number of live objects, and there is only 1 live object in this example. A possible alternative explanation is that reference counting will be fastest because of improved locality. 5

(b) Assume the same program is run on a machine with sufficient physical memory to hold n Foo objects. What is the maximum number of objects that can be allocated on the heap at any single time by an implementation using a Mark and Sweep collector? Justify your answer. n; Mark and Sweep can use all available memory and will not trigger a collection until it is full. (c) What is the maximum number of objects that can be allocated on the heap at any single time by an implementation using a Reference Counting collector? Justify your answer. 1 or 2; each object becomes dead as soon as a new one is allocated. Depending on how the student interprets the order in which reference counting operations are done, there will be a most 1 or 2 objects allocated. (d) What is the maximum number of objects that can be allocated on the heap at any single time by an implementation using a Stop and Copy collector? Justify your answer. n/2; Stop and Copy can use only half of the heap to hold objects and will trigger a collection when that half is full. 6

3. Global Optimization & Register Allocation (30 points) (a) You are given the control-flow graph for a function which uses variables a to g, with a and b being the arguments and g the return value. The program statements have been removed from this graph, and replaced by empty boxes. Below/above every such box we give the set of variables that are live right after/before the corresponding statement. Fill in program statements in the boxes, so that the given live sets are consistent with the code. Program statements can take one of the following forms: x = 1, x = y or x = y + z (where x, y and z can be any one of the variables used by the function). If more than one statement would work in some position, pick the simplest one (prefer x = 1 over x = y, and x = y over x = y + z). {a,b} c = 1 {a,b,c} g = a {b,c,g} d = c {b,d,g} {b,d} e = d {b,e} f = b {e,f} {d,g} f = 1 {d,f,g} e = d + g {e,f} {e,f} g = e + f {g} 7

(b) Give the register interference graph for the above code, by filling in the adjacency matrix below (mark X on the cell at row v 1 and column v 2 iff there s an edge between nodes v 1 and v 2 ). You may also want to draw the graph to help with the next questions, but we won t grade your drawing. a b c d e f g a x x b x x x x c x d x x e x f x g (c) Perform register allocation for the above code, using the graph coloring algorithm presented in class. Fill in (and use!) the minimum number of registers k for which you can color the graph without spilling. If at any point there s a choice between multiple variables to push on the stack, pick the one which comes first alphabetically. Show the state of the stack right after you ve removed the last node from the graph: k = 3 top (pushed last) g f d b e c a bottom (pushed first) (d) Show your final coloring on the following table, by listing the variables assigned to each register. Use only the first k registers, where k is the number of colors you used. Register r 1 r 2 r 3 r 4 r 5 r 6 r 7 Variables g, e, a f, b d, c 8

4. Local Optimization (20 points) Consider three address programs that consist only of the following types of operations: x = y for variable x and variable or constant y x = y + z for variable x and variables or constants y and z. Answer each of the following questions. For each problem, state which variable(s) are live on exit. For full credit, your solution should be as short as possible. (a) Give a program where performing local optimizations in the order copy propagation, constant folding and then dead code elimination results in a final program with fewer instructions than performing first constant folding, copy propagation and then dead code elimination. (This question was removed from the exam) (b) Give a program for which the opposite is true: performing constant folding, copy propagation and then dead code elimination is better than performing copy propagation, constant folding and then dead code elimination. y = 1 + 1 x = y + z (x live out) Minimum program is of length 2. 9

(c) Give a program for which both of copy propagation and constant folding must be done at least twice each before performing dead code elimination to achieve the best results. x = 1 + 1 y = x + 1 z = y + 1 (z live out) Minimum program is of length 3. 10

5. Activation Records (20 points) Consider the following COOL class and method definitions: 1 class Main { 2 apple(a: Int, b: Int, c: Bool): Int { 3 if c then 4 if a - b = 0 then 5 0 6 else 7 a + b 8 fi 9 else 10 { 11 a <- a * b; 12 a + b; 13 } 14 fi 15 }; 16 17 banana(x: Int, y: Int): String { 18 if x < cherry(y) then 19 "Peel" 20 else 21 "Split" 22 fi 23 }; 24 25 cherry(z: Int): Int { 26 let p: Int <- 2 in { 27 apple(z, p, false); 28 } 29 }; 30 31 main(): Object { 32 let q: Int <- 3, r: Int <- 4 in { 33 banana(q, r); 34 } 35 }; 36 37 }; 11

A compiler with an unknown runtime management strategy is used to compile this code in particular, the strategy used may not be exactly one that you have seen before. A partial listing of the stack at a certain point in the execution of the compiled program is given below. There are three kinds of entries on the stack: a return address, a stack address, and a reference to an object in the heap (e.g., Int(3)). (a) Fill in the missing entries in the table. Address Contents 1000 OS Frame Pointer 1004 OS return address 1008 Int(3) 1012 Int(4) 1016 1000 1020 Int(3) 1024 Int(4) 1028 return address of banana(q,r) 1032 Int(3) 1036 1016 1040 Int(4) 1044 cherry(y) 1048 Int(2) 1052 1036 1056 Int(4) 1060 Int(2) 1064 Bool(false) 1068 return address of apple(z,p,false) (b) On the previous page, clearly indicate the latest possible point in the execution of the program that could have this stack contents. Just before the return of the call to apple. 12