Midterm II CS164, Spring 2006

Similar documents
COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The Activation Record (AR)

Midterm I (Solutions) CS164, Spring 2002

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

Compilers and computer architecture: A realistic compiler to MIPS

CS64 Week 5 Lecture 1. Kyle Dewey

CA Compiler Construction

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS143 Final Fall 2009

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

CS143 Final Spring 2016

Midterm I - Solution CS164, Spring 2014

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

CS61C : Machine Structures

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Code Generation. Lecture 19

CSCI Compiler Design

CSCI Compiler Design

Computer Architecture I Midterm I

CS61C : Machine Structures

CS143 - Written Assignment 4 Reference Solutions

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Code Generation. Lecture 12

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CS 61c: Great Ideas in Computer Architecture

CSE Lecture In Class Example Handout

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

CS4215 Programming Language Implementation

Functions and Procedures

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

CS61c MIDTERM EXAM: 3/17/99

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CS153: Compilers Lecture 8: Compiling Calls

CS 6353 Compiler Construction Project Assignments

ECS 142 Project: Code generation hints

ECE260: Fundamentals of Computer Engineering

MIPS function continued

CS164: Midterm I. Fall 2003

UCB CS61C : Machine Structures

CSE Lecture In Class Example Handout

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

ECE 15B Computer Organization Spring 2010

Course Administration

LAB C Translating Utility Classes

Lecture 7: Procedures and Program Execution Preview

Lecture 7: Procedures

Lecture 9: Procedures & Functions. CS 540 George Mason University

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

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

Written Assignment 5 Due??

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Review of Activation Frames. FP of caller Y X Return value A B C

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

G Programming Languages - Fall 2012

University of California, Berkeley College of Engineering

Structure of a Compiler. We looked at each stage in turn. Exceptions. Language Design and Implementation Issues

CS 61C, Midterm #2, Spring 1997 CS 61C. Spring 1997 Midterm #2. Professor Brian Harvey

System Software Assignment 1 Runtime Support for Procedures

CS 415 Midterm Exam Spring SOLUTION

ECE 30 Introduction to Computer Engineering

CS61C : Machine Structures

ECE550 PRACTICE Midterm

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

CS 351 Exam 2 Mon. 11/2/2015

CSE P 501 Exam 8/5/04

We will study the MIPS assembly language as an exemplar of the concept.

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

CS61C : Machine Structures

CS 110 Computer Architecture MIPS Instruction Formats

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CSE 401 Final Exam. December 16, 2010

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

MIPS Functions and Instruction Formats

COE608: Computer Organization and Architecture

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Lecture 2: SML Basics

Chapter 2. Instructions:

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

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

CS 536 Midterm Exam Spring 2013

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Operational Semantics of Cool

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

ECE 30 Introduction to Computer Engineering

Transcription:

Midterm II CS164, Spring 2006 April 11, 2006 Please read all instructions (including these) carefully. Write your name, login, SID, and circle the section time. There are 10 pages in this exam and 4 questions, each with multiple parts. Some questions span multiple pages. All questions have some easy parts and some hard parts. If you get stuck on a question move on and come back to it later. You have 1 hour and 20 minutes to work on the exam. The exam is closed book, but you may refer to your two pages of handwritten 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. We might deduct points if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit. LOGIN: NAME: SID: Circle the time of your section: Fri 10:00 Fri 11:00 Fri 2:00 Problem Max points Points 1 10 2 30 3 40 4 20 TOTAL 100 1

1 Liveness (10 points) Consider the following program fragment. d a := 1 a,d c := 3 a,c,d if a >= 2 goto L2 a,c,d b := c + 1 a,b,d c := b + 2 a,d,c L1: b := d a,d,c a if a >= d goto L2 return a c L2: a := c + 2 a,c, d := c + 1 a,c,d goto L1 (a) Draw horizontal lines to separate the basic blocks. (b) In the boxes to the left of each instruction write the set of live variables on entry to that instruction. (c) Cross with an X the dead instructions. Explain below how do you recognize a dead assignment. Only b := d is dead 2

2 Global Analysis (30 points) The goal of this exercise is to design a fancier version of global constant propagation discussed in lecture. As in lecture, for each integer variable x we try to find if x is guaranteed to be a constant at a given program point. We write C in (x, s) = n when this is the case right before statement s and the constant is n. The difference from the version discussed in lecture is that now we propagate the information properly across assignments when the right hand side is known to be a constant and across arithmetic operations when both operands are known to be constants. (a) How can you use the results of this analysis to optimize the instruction if x 5 goto L? If x is guaranteed to be a constant 5, then we can replace this instruction with goto L. If x is guaranteed to be a constant < 5, then we can eliminate the instruction. (b) Fill-in the empty boxes with the values computed by the analysis for the variables x and y at each program point in the corresponding CFG. Each box is associated with a point on the nearest edge. (c) Consider an instruction s with two predecessors p 1 and p 2. Write the formulas for C in (x, s). No need to consider the # value here. c if C out (x, p 1 ) = C out (x, p 2 ) = c, Solution : C in (x, s) = if C out (x, p 1 ) C out (x, p 2 ), if C out (x, p 1 ) = or C out (x, p 2 ) = 3

(d) Consider an instruction s of the form x := y + z. Write the formula for C out (x, s). No need to consider the # value here. { c + d if C in (y, s) = c and C in (z, s) = d and c and d, Solution : C out (x, s) = if C in (y, s) = or C in (z, s) = (e) Consider the instruction s of the form if x == n goto L. Write the formulas for C out (x, s) for the case when the branch is taken. { # if C in (x, s) is #, Solution : C out (x, s) = n otherwise (f) Is this analysis a forward or a backward analysis? Forward, because we mark an instruction based on something that occurs in the past. (g) Show a small CFG such that at the end it is true that x has value 0, yet the global analysis is not able to discover that fact. Your program may not contain loops or function calls. Explain your answer. Consider a simple CFG with one basic block as follows: x = 0 x = x + y x = x - y 4

Observe that x is guaranteed to be zero at the end of this block, because x starts out as zero, and then you add y, and then you subtract y. So x = 0 + y y = 0. However, when global analysis sees this, it doesn t know what value y has, so it assigns y =. Then it sees x = x + y, and since y =, that means after this instruction, x =. So in the end, x =, according to global analysis. Many other solutions are possible. 5

3 Runtime Organization and Code Generation (40 points) Suppose we extended the Cool syntax with nested function definitions. We add a new form of expressions similar to let but defining functions: fun f(x 1 : T 1,..., x n : T n ) : T n+1 {e 1 } in e 2 binds the identifier f to a new function with body e 1 that takes parameters x 1,..., x n with types T 1,..., T n, returns a value of type T n+1, and can only be called from within e 1 or e 2. Moreover, the body e 1 and the expression e 2 can refer to variables declared in enclosing lets, as well as function and method parameters of the enclosing functions, as well as f itself. Both variable and function names are statically scoped. For example, consider the following method exp in class Math that computes x y. class Math { f(x : Int) : Int {... }; exp(x : Int, y : Int) : Int { let a : Int <- 1 in fun f(i : Int) : Int { fun geta() : Int { a + i } in if i = 0 then geta() else { a <- a * x; f(i-1); } fi } in f(y) }; }; The x in the body of the f refers to the parameter x of exp, and the a in the body of geta refers to the local variable introduced by the let in exp. Similarly the i in the body of geta refers to the argument of f. Also, notice that the calls to f refer to the f declared in exp, not the method f. 6

3.1 Type Checking To type check fun, we do not need to modify the typing judgment, nor introduce any additional forms of types. Recall that the Cool typing judgment is of the form O, M, C e : T, where M is a map such that M(D, f) = (T 1,..., T n, T n+1 ), whenever class D supports a method named f with n arguments of types T 1,...,T n, and result type T n+1. You will use the same map M to carry information about the nested functions that are in scope. We ignore SELF TYPE in this problem. (b) Write the remaining hypotheses and complete the conclusion of the typing rule for function invocation: M(C, f) = (T 1,..., T n, T n+1 ) Solution : i {1... n} O, M, C e i : T i T i T i O, M, C f(e 1,..., e n ) : T n+1 (c) Give a sound typing rule for fun (one that allows as many safe programs as possible). If you need to introduce new notation, explain it below the typing rule. Solution : O = O[T 1 /x 1 ]... [T n /x n ] M = M[(T 1,..., T n, T n+1 )/(C, f)] O, M, C e 1 : T T T n+1 O, M, C e 2 : T O, M, C fun f(x 1 : T 1,..., x n : T n ) : T n+1 {e 1 } in e 2 : T 7

3.2 Code Generation The difficulty in code generation is that nested functions may need to use variables declared in enclosing functions. Consider the previous example. The function f not only needs access to its own activation record (for variable i) but also the activation record for exp (for variables x and a). Similarly geta needs access to the activation frame of the enclosing f (for i) and of the enclosing exp (for a). One way to implement this feature is to use a different type of activation record for nested functions (as opposed to the activation record used for methods). This new activation record contains an extra entry known as a static link, stored in between the saved frame pointer and the first argument. The static link is a pointer to the active activation record of the nearest statically enclosing function/method. The first three activation records for a method call to exp(5,2) are given below (one for exp(5,2), one for the function call to f(2), and one for f(1)). In the diagram, we have noted with an arrow that the static links for f(2) and f(1) point to the begining of the activation frame for exp(5,2). (d) Complete the stack of activation records at the time of the call to geta() for exp(5,2) (i.e., having three calls to f). Include the activation record for geta(). Then, draw arrows to show where the static links and saved frame pointers point. A diagram containing both the AR for f(0) on top of the AR for f(1) with a static link to the bottom of the AR for exp(5,2) and the AR for geta() on top of the AR for f(0) with a static link to the bottom of the AR for f(0). f(1) f(2) exp(5,2) return address saved caller s $fp static link i = 1 return address saved caller s $fp static link i = 2 return address saved caller s $fp a x = 5 y = 2 Higher addresses 8

(e) Generate MIPS code to store the result of the assignment a <- a * x in function f. Assume that the code for the multiplication has already been generated with the result in $a0. (Hint: refer to the diagram given on the previous page. You can use temporaries to generate code.) Math.exp f:...# code to compute a * x leaving the result in $a0 # load the static link in a temporary (say $t1) using $fp # store $a0 in a using $t1... (f) Complete the generated MIPS code for geta(). Return the result in register $a0. The function prologue and epilogue has been provided for you. (Hint: use your completion of the above diagram.) Math.exp f geta: addiu $sp $sp -8 # room for the FP and RA sw $fp 8($sp) sw $ra 4($sp) addiu $fp $sp 4 # load the static link to f(0) in $a0 using $fp # load i using $a0 # load the static link to exp(5,2) in $a0 using $a0 # load a using $a0 # add a and i and store the result in $a0 lw $ra 0($fp) lw $fp 4($fp) addiu $sp $sp 12 # pop the static link as well jr $ra 9

(g) Complete the generated MIPS code for the call to geta() from f. Math.exp f:... # push static link ($fp) jal Math.exp f geta... (h) Complete the generated MIPS code for the call to f(i - 1) from f. Assume that the code for the subtraction has already been generated with the result in $a0. Math.exp f:...# code to compute i - 1 in $a0 # push $a0 # load static link in $a0 using $fp # push $a0 jal Math.exp f... (i) How many loads are required for reading a variable in a nested function? The number of enclosing functions plus one. (j) Describe an alternative compilation strategy that reduces the number of loads required for a variable access. Store a static link to each enclosing function in the AR. 10

4 Short Answers (20 points) (a) Recall that in Cool (and many other languages), objects are stored in the heap. Explain why can t the objects be stored in the activation frame. Objects must be able to outlive the current invocation (b) Give an example of statically typed language and one of a dynamically typed language. Statically typed: Cool Dynamically typed: Scheme (c) What is a limitation of a dynamically typed language? Slow run-time checks, bugs are found very late. (d) What is a limitation of a statically typed language? Some valid programs are rejected by the type checker. (e) Consider a data-flow analysis that marks all invocations of new that create objects whose field f is eventually referenced in the rest of the execution. Would this be a forward or a backward analysis? Backward (f) What is the most general condition under which T 2 T 3 T 2, where is the least upper bound operator on Cool types? T 3 T 2 11