CS143 Final Fall 2009

Similar documents
CS143 Final Spring 2016

Midterm II CS164, Spring 2006

CS143 Midterm Spring 2014

CS164: Midterm I. Fall 2003

CS 3360 Design and Implementation of Programming Languages. Exam 1

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

CS143 Midterm Fall 2008

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

Midterm I - Solution CS164, Spring 2014

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

Final Examination. Winter Problem Points Score. Total 180

Programming Project 4: COOL Code Generation

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS4215 Programming Language Implementation

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

CS143 Midterm Sample Solution Fall 2010

CSE-304 Compiler Design

Operational Semantics. One-Slide Summary. Lecture Outline

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

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

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

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Final Exam CS164, Fall 2007 Dec 18, 2007

Midterm I (Solutions) CS164, Spring 2002

CS164: Midterm Exam 2

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

CSE 5317 Midterm Examination 4 March Solutions

EINDHOVEN UNIVERSITY OF TECHNOLOGY

CS 2505 Computer Organization I

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

This book is licensed under a Creative Commons Attribution 3.0 License

Final Examination May 5, 2005

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Solution printed. Do not start the test until instructed to do so! CS 2504 Intro Computer Organization Test 2 Spring 2006.

COS 226 Algorithms and Data Structures Fall Midterm

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

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

CS164: Final Exam. Fall 2003

Final Examination. Preliminaries. Compilers (CS F) Due: 11 a.m., Friday, 20 December 2002

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

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

MIDTERM EXAMINATION - CS130 - Spring 2005

CSE 247 Data Structures and Algorithms Fall Exam I

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

CS140 Operating Systems and Systems Programming Final Exam

Questions Total Points Score

CS 2504 Intro Computer Organization Test 1

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Midterm CSE 21 Fall 2012

Do not start the test until instructed to do so!

Prelim One Solution. CS211 Fall Name. NetID

CS 2506 Computer Organization II

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

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

14.1 Encoding for different models of computation

cs173: Programming Languages Final Exam

CS140 Operating Systems and Systems Programming Midterm Exam

CSE351 Winter 2016, Final Examination March 16, 2016

CSE 413 Final Exam. June 7, 2011

Structure of Programming Languages Lecture 10

Computer Science Foundation Exam

6.821 Programming Languages Handout Fall 2002 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

Problem Score Max Score 1 Syntax directed translation & type

COMPUTER SCIENCE TRIPOS

CS 415 Midterm Exam Spring 2002

CIS 341 Final Examination 3 May 2011

CS 406/534 Compiler Construction Putting It All Together

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

Operational Semantics

Operational Semantics of Cool

1 Lexical Considerations

Compilers and computer architecture: A realistic compiler to MIPS

CS558 Programming Languages Winter 2013 Lecture 8

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Lexical Considerations

Operational Semantics of Cool

Semantic Analysis. Lecture 9. February 7, 2018

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

COP5621 Exam 4 - Spring 2005

Short Notes of CS201

Project Compiler. CS031 TA Help Session November 28, 2011

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CS 415 Midterm Exam Fall 2003

CS 134 Midterm Fall 2006

CS558 Programming Languages

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

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

CIS 341 Final Examination 4 May 2017

Computer Science Foundation Exam

cs173: Programming Languages

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

What goes inside when you declare a variable?

Name: CIS 341 Final Examination 10 December 2008

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Transcription:

CS143 Final Fall 2009 Please read all instructions (including these) carefully. There are 4 questions on the exam, all with multiple parts. You have 2 hours to work on the exam. The exam is closed book, but you may refer to your four sheets of prepared notes. Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper. 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 20 2 40 3 40 4 20 TOTAL 1

1. Memory Management (20 points) (5 points) You are using an application written in a language with automatic memory management, and you notice that the application stops responding to your input for a few seconds every once in a while. You suspect the garbage collector to be the source of this delay. From the list of automatic memory management strategies below, place a checkmark next to the strategies that could cause this behavior and justify your answer for each choice below. Mark-and-sweep Generational stop-and-copy Reference counting (5 points) Your colleague designs and implements a garbage collection technique she calls Ref- Count++ which extends reference counting to detect unreachable cyclic data structures. She is very proud of it and claims that programs managed by Ref-Count++ cannot have memory leaks. Assuming Ref-Count++ works as advertised, do you agree with her that programs managed by Ref-Count++ do not have memory leaks? Why or why not? 2

(5 points) Your friend wants to mitigate the runtime overhead of automatic memory management by combining static and dynamic techniques. In particular, he would like his compiler to automatically insert as many delete calls as possible and have only the remaining objects collected at run-time by the garbage collector. After reading over your CS143 lecture notes, he decides liveness analysis is a great match for this problem. Specifically, he proposes to insert a call delete v as soon as variable v stops being live. Is this a correct memory management strategy? Explain your reasoning. (5 points) A colleague of yours who is an avid C++ advocate says that you can avoid memory leaks in C++ by allocating all objects on the stack, which you cannot do in Java. He also proposes that you can rewrite your Java program in C++ using only stack allocation and thereby avoid any memory leaks. In particular, he proposes that you replace any Java statement Foo f = new Foo() by the C++ code Foo f; Foo* f = & f, which makes f point to a stack-allocated Foo object instead of a heap allocated one. Is this a correct transformation? Justify your answer. 3

2. Optimization and Dataflow Analysis (40 points) (5 points) After profiling the programs generated by your Cool compiler, you notice that dynamic dispatch takes up a large chunk of running time. What is the minimum number of extra memory reads introduced by dynamic dispatch over static dispatch in Cool, assuming that dispatch tables are global and shared between different instances of a class? Justify your answer. (5 points) You realize that you can replace some of the dynamic dispatches by a static dispatch just by examining the inheritance tree of the entire Cool program. Given a method call o.m(...) where o s static type is C, when can you safely replace this call with o@c.m(...) just by examining the inheritance tree and the set of methods defined in each class? Your project partner insists more can be done to address this performance bottleneck. In particular, she wants to design a dataflow analysis to detect which calls o.m(...) can be replaced by o@c.m(...). For example, in the following program: { if... then o <- new A else { x <- new A; o <- x } fi; o.m() } it is safe to replace the dispatch call by o@a.m(). On the other hand, if we replaced one new A by new B for any type B distinct from A we could not do this transformation. The rest of this question walks you through designing such an analysis. To keep the problem simple, you may assume that methods take no arguments and dispatch expressions are always variables. (2 points) List the dataflow values used by your analysis and their interpretation. 4

(3 points) Draw the dataflow lattice (i.e., the ordering between these dataflow values). (2 points) Is your analysis forwards (i.e., information pushed from inputs to outputs) or backwards (i.e., from outputs to inputs)? Explain. (3 points) For each program point, describe how you initialize the dataflow value associated with each program variable. 5

(15 points) Let E in (s, v) denote the dataflow value of variable v before executing statement s, and E out (s, v) denote the dataflow value of variable v after executing s. For each of the following statements s, give an equation defining the value of E out (s, v). v <- v2 v <- new C v.m() (5 points) Using the results of your dataflow analysis, explain how you determine which dynamic dispatches can safely be replaced with static dispatches. 6

3. Semantic Analysis, Runtime Organization, and Code Generation (40 points) A two-stack machine has an alphabet of symbols and two stacks for storing symbols. The instructions of a two-stack machine are as follows: L: symbols{ X1, X2,...} push1 X push2 X pop1 pop2 beq X L There is always exactly one symbols{x1,x2,...} instruction and it is always the first instruction of a program; this declares the set of symbols used in the program. The instruction push1 X pushes the symbol X on the first stack. The instruction pop1 pops one symbol off of the first stack. The push2 X and pop2 instructions work analogously with the second stack. The instruction beq X L tests the most recently popped symbol (from either stack); if that symbol is an X, the program jumps to label L and otherwise it falls through to the next instruction. An instruction L: does nothing except define a label in the program that can be the target of a branch. There is a reserved symbol BOS (for bottom of stack ) that always exists but may not be declared and can only be referred to in branch instructions. If a stack is empty then a pop instruction returns BOS. Also, if a beq X L instruction is executed before any symbol has been popped from a stack, the branch to L is taken only if X is the symbol BOS. As an aside, a two stack machine is equivalent to a Turing machine it can encode any computable function. A one stack machine is strictly less powerful. (8 points) List any semantic checks that are needed to ensure that programs are well-formed. (Note there are no types in this language, and so no type checking. We are talking about other kinds of semantic checks.) 7

(12 points) Describe the run-time organization of data you would use for this language. Include descriptions of which registers would be used for what purposes and any data/code regions you would use and how they would be managed. In both this and the next question, full credit solutions should be efficient in addition to being correct. 8

(20 points) Write a code generator for this language. You may use any reasonable and clear pseudoassembly language notation. 9

4. Various (20 points) Array languages emphasize operations on entire vectors, rather than on individual data elements. Examples of such languages include APL, FP, FL, and more recently Map-Reduce and Hadoop. Consider the following grammar: E -> zero(n) inc sum map(e) E o E Expressions in this language are functions. Note that there are no variable names; everything is a function. The function inc takes one integer argument and produces an integer result (the argument plus one). The other functions map vectors of integers to vectors of integers. For any vector argument, zero(n) returns a vector of length N of all 0 s. The function sum takes an argument vector of integers and the result is another vector of integers where the ith element of the output vector is the sum of the first i elements of the input vector. The function map(e) is particularly interesting: here E is a function mapping integers to integers, and map(e) is the function that produces a new vector by applying E to each element of the input vector. The function f o g is function composition. Consider the program sum o sum o map(inc) o zero(4) Running this program on input <0> (the input vector doesn t matter) the output is < 1, 3, 6, 10>, which is computed as follows: (sum o sum o map(inc) o zero(4))(<0>) = sum(sum(map(inc)(<0,0,0,0>))) = sum(sum(<1,1,1,1>)) = sum(<1,2,3,4>) = <1,3,6,10> (5 points) Is the grammar above ambiguous? Why or why not? 10

(5 points) Give an operational semantics rule for function composition f o g. For full credit, your rule should not only be correct but also be as simple as possible. (5 points) An important transformation that compilers for array languages can perform is map(f) o map(g) = map(f o g) Given one reason that a compiler might choose to replace the left-hand side by the right-hand side in a program. (5 points) Referring again to the transformation in the previous subproblem, now assume that the language includes exceptions. When a function raises an exception execution halts and the result is that exception (there is no facility for catching exceptions). Prove that if f and g can raise different exceptions, the transformation is incorrect. 11