Compilers (CSC-362 98F)

Final Examination

This page may be found online at

Due: Noon, Thursday, December 17, 1998.

I will not accept your final exam unless you have turned in both the written and electronic evaluation forms!

There are a variety of questions on the exams, many with different point values. Answer all questions unless instructed to do otherwise. Note that point values do not necessarily correspond to length. For example, some ten point problems may take longer than the twenty point problem, or the twenty point problem may take significantly more than twice as long as a ten point problem.

This examination is open book, open notes, open mind, open computer, open web. Feel free to use any and all resources available to you except for other people. As always, you are expected to turn in your own work. If you find ideas in a book or on the web, be sure to cite them appropriately.

This is a take-home examination. It is likely to take you at least six hours. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by noon on Thursday, December 17th. No late exams will be accepted. I will make a solution key available at approximately noon on Thursday, December 17.

Because different students will be taking the exam at different times, you are not permitted to discuss the exam with anyone until after noon on December 17th. If you must say something about the exam, you are allowed to say ``This is definitely the hardest exam I have ever taken.'' You may also summarize these policies.

Answer all of your questions in a blue book or electronically. That is, you may write all of your answers on the computer and print them out at the end of class. If you write your answers in the blue book, you need not do the problems in order (but it would help me if you provided a table of contents). If you type your answers on the computer, please do them in order.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by highlighting your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you are taking the exam. If you feel that a question is badly worded or impossible to answer, note the problem and attempt to reword the problem in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445). Note that I've already taken the exam, so there should be few errors.

I will discuss questions you have on the exam on the last day of class (Friday, December 11). I will also be available much of the next week, although I will be out of town from noon on Sunday, December 13, to 5pm on Tuesday, December 15. For those of you with extremely last-minute questions, I will be available from 9:00 a.m. to noon on Thursday, December 17.

In some questions, you may be asked to write code. You should feel free to write pseudocode in any reasonable form (i.e., similar to any common language). Your pseudocode need not be "syntactically correct". However, it should be clear and should indicate to me that you would be able to write correct and working code (in some language) if you had sufficient time and inclination.

Problem 1: Shorter Questions [40 points]

Answer any four of the following questions. Indicate which ones you have answered. Most of these are relatively short questions (particularly as compared to my typical questions), especially if you have done some of the recommended extra work I've assigned during the semester.

If you choose to answer more than four, I will give you two points of extra credit for each correct answer to an extra problem (no more than two per problem), but you must indicate which ones I should grade for primary credit.

Problem 1.A: Optimizing DFAs

We've seen two strategies for optimizing deterministic finite automata:

Will these algorithms perform identically (i.e., will they give the same results on all DFAs)? Is one better? If so, how?

You may want to consider the following automaton.

States: q0, q1, q2, q3
Start state: q0
Final states: q1, q3
  q0,0 -> q1
  q0,1 -> q3
  q1,0 -> q2
  q1,1 -> q0
  q2,0 -> q3
  q2,1 -> q1
  q3,0 -> q0
  q3,1 -> q2

Problem 1.B: Cyclic Type Definitions

The Tiger language definition states that every cycle of type definitions must go through a record or array. But if the compiler forgets to check for this error, nothing terrible will happen. Explain why. If you believe that something terrible will happen, you can instead suggest what will happen and why it is terrible.

This problem is based on problem 5.3 in Appel (p. 124 of the Green book, p. 129 of the Red book).

Problem 1.C: Commutes

Recall that statement s and expression e commute if rearranging the order of evaluation of s and e does not affect the outcome of either operation. In chapter eight, Appel suggests some simple criteria that permit us to determine some of the cases in which s and e commute. Give at least two others.

Problem 1.D: Instruction Selection

Consider a machine with the following instruction

mult const1(src1), const2(src2), dst3

with the meaning

r3 <- M[r1 + const1] * M[r2 + const2]

On this machine, r0 is always 0 and M[1] always contains 1.

a. Draw all of the tree patterns that correspond to this instruction and its special cases.

b. Pick one of the bigger patterns and show how to write an if statement (in Java, C, or Tiger) that matches it. Make sure to show the IRT representation of the statement.

This problem is slightly modified from problem 9.2 of Appel (p. 220 of the Red book, p. 217 of the Green book).

Problem 1.E: Register Allocation

Find a graph that, for some K >= 3, is K colorable without spilling, but requires spilling in some invocation of the Optimistic Graph Coloring algorithm given in Section 11.1.

Note that you can interpret ``color'' and ``select a node to spill'' in any legal way you deem appropriate.

Problem 1.F: Student Presentations

Write a short question and answer based on another group's presentation. You will be graded on the quality of your question (which should be of comparable difficulty to the other short questions) as well as the quality of your answer. Note that I do not like simple memorization/definition questions.

Problem 2: Back End [20 points]

Consider the following program written as a series of IRT-like instructions in which the conditional branch has two destinations. Assume that readInt has been defined elsewhere and that it reads an integer and returns the integer read in register RV. Note that the program differs from IRT in a few subtle ways. For example, it is possible to jump to a computed address.

This program uses a more standard JUMP than Appel's (this jumps to a particular label, Appel uses an indexed Jump). If you'd prefer to use something closer to Appel's, an alternate version is available.

CALL printInt 10
CALL readInt
MOVE T(100) RV
CALL readInt
MOVE T(102) RV
CALL readInt
MOVE T(103) RV
CJUMP LT T(102) T(103) L(L52) L(L53)
CALL readInt
MOVE T(101) RV
MOVE T(200) MAXINT	# The maximum integer
MOVE T(201) MAXINT	# The maximum integer
MOVE T(300) T(100)
MOVE T(555) L(L201)
JUMP L(L300)
MOVE T(300) T(101)
MOVE T(555) L(L202)
JUMP L(L300)
MOVE T(300) T(102)
MOVE T(555) L(L203)
JUMP L(L300)
MOVE T(300) T(103)
MOVE T(555) L(L204)
JUMP L(L300)
JUMP L(L400)
CJUMP LT T(300) T(201) L(L301) L(L302)
MOVE T(201) T(300)
CJUMP LT T(201) T(200) L(L303) L(L302)
MOVE T(50) T(201)
MOVE T(201) T(200)
MOVE T(200) T(50)
JUMP T(555)	# This can only be L201, L202, L203, or L204
BINOP PLUS T(200) T(201)
BUILTIN printInt T(500)
CJUMP LT T(500) 10 L(L500) L(L501)

Problem 2.A: Basic Blocks

Identify the basic blocks in the program.

Problem 2.B: Organize Basic Blocks

Order the basic blocks so that (1) each CJUMP only uses the true label (and falls through to the false code) and (2) as many JUMPs as possible are eliminated.

Problem 2.C: Liveness Analysis

Compute the lifetime of each temporary used in the program. You may find it helpful to number the lines of the program to help with this, but your are not required to do so.

Problem 2.D: Register Allocation

Determine a mapping of temporaries to registers using only four registers You may need to spill some temporaries to memory, which will require updating the program. You can choose to use the formal algorithm, or you may use a more ad-hoc solution.

If you spill to memory, use memory locations 31, 32, 33, ....

Note that you can use the standard registers for storing values when they are not used for other purposes. However, they do count towards the four. In this program, the standard registers used are RV (the return value from functions, defined in any CALL), ACC (the accumulator, defined in any BINOP), and A0 (the argument to builtin printInt). For example, CALL printInt 10 defines RV and A0.

You need not concern yourself with the instruction counter, frame pointer, stack pointer, heap pointer, and return address. That is, you can not use them explicitly, but they don't count toward the four registers, either.

Warning! The JUMP R(555) can lead to four different labels. Hence, when you compute out for that instruction, you will need to use the in for all four labels.

Problem 2.E: Improvement

This part of the problem is optional! A correct answer will be worth two points of extra credit.

It is possible to change the program in such a way that it does not spill any temporaries to memory, and still uses only four registers. However, this requires a somewhat ad-hoc technique (at least given our current understanding). How might you use only four registers without spilling?

Hint: consider the relationships between T(100), T(101), T(102), and T(103) and how they are used in the program. Then, think of how all four relate to T(300).

Problem 2.F: Understanding

This part of the problem is optional! A correct answer will be worth two points of extra credit.

What does this program do?

Problem 3: Extending Tiger [40 points]

Suppose we decided to extend Tiger to include referencing (address-of) and dereferencing (contents-of) operations to Tiger. What changes would you have to make to the Tiger language and a typical Tiger compiler to support these new operations?

Your answer should be in a form that an appropriate person (e.g., one of your colleagues in this class) can understand and use as a ``checklist'' in updating the compiler. For each item, include not just what has to be updated (e.g., ``You will need to update the type checker'') but also how one might make the update, in general terms (e.g., ``Update transVar so that when it encounters a reference variable it ...'').

You should assume that the Tiger compiler being modified includes all of the components described in Chapters 1-12 of Appel, including the optional parts, such as an improved commutes operation.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Mon Dec 21 13:55:12 1998.

This page generated on Mon Jan 25 12:09:35 1999 by SiteWeaver.

Contact our webmaster at