Docsity

Spring 2009 CMSC430 Midterm Solutions: Compiling & Optimizing, DAGs, Local Info, Data-Flow, Exams of Computer Science

Toggle sidebar
bg1
CMSC430 Spring 2009 Midterm 2 (Solutions)
Instructions
• You have until 4:45pm to complete the midterm.
• Feel free to ask questions on the midterm.
• One sentence answers are sufficient for the ``essay'' questions.
• Use only the following 3-address code and Java stack code instructions for answering
code generation questions.
1. (15 pts) Intermediate representations.
Consider the arithmetic expression:
a - (b - c)
a. Translate it into an AST
-
a -
b c
-
a -
b c
pf3
pf4
pf5
pf8

Partial preview of the text

Download Spring 2009 CMSC430 Midterm Solutions: Compiling & Optimizing, DAGs, Local Info, Data-Flow and more Exams Computer Science in PDF only on Docsity!

CMSC430 Spring 2009 Midterm 2 (Solutions)

Instructions

  • You have until 4:45pm to complete the midterm.
  • Feel free to ask questions on the midterm.
  • One sentence answers are sufficient for the ``essay'' questions.
  • Use only the following 3-address code and Java stack code instructions for answering code generation questions.
  1. (15 pts) Intermediate representations. Consider the arithmetic expression: a - (b - c) a. Translate it into an AST

a -

b c

a -

b c

b. Translate it into 3-address code load R1 b load R2 c sub R3 R1 R load R4 a sub R5 R4 R

c. Translate it into Java stack code iload index(a) iload index(b) iload index(c) isub isub

d. Name an advantage of using 3-address code instead of stack code. Greater flexibility in reordering/eliminating instructions.

  1. (16 pts) Code generation. You are generating code for a Java stack machine. You are given the following grammar attributes and helper functions:

a. A Ruby-style UPTO loop iterates (exp$_2$-exp$_1$+1) number of times. For example, 5 upto (2+6) do ... end would iterate 4 times. Write syntax-directed actions needed to generate code for a UPTO loop for the following grammar production. Note you can only use the Java instructions provided at the beginning of the exam. stmt  exp 1 UPTO exp 2 DO { stmtList } END {: stmt.code = ?? :}

exp := exp 1 NAND exp 2 {: Handle h1 = genInst( NOP ); // end of UPTO loop Handle h2 = genInst( NOP ); // beginning of UPTO loop

exp.code = append( exp 1 .code, genInst( ISTORE( index(1) ) ), exp 2 .code,

  1. (8 pts) Compiling and optimizing high-level languages Object-oriented programming languages such as C++ and Java use classes and inheritance to improve programmer productivity. Remember that inheritance allows objects of one class to be used in place of objects in a parent class.

a. How are objects and classes implemented in the run-time environment? Class records and object records. b. What compiler-generated code is need to support inheritance? Code to check class type via object tags. c. What is prefixing? Assigning object fields and method references to the same index for subclasses, so dynamic checks of class type will not be needed.

  1. (6 pts) Compiler optimizations

a. Give an example of a machine-independent optimization, and explain why it is machine-independent Eliminating redundancy, since executing fewer instructions usually improves performance b. Give an example of a machine-dependent optimization, and explain why it is machine-dependent Register allocation, since number of registers is machine dependent c. Why would a compiler writer decide not to implement a particular optimization? Insufficent improvement, or rarity of occurence

  1. (6 pts) Directed acyclic graphs Consider the following code. (1) a := b + c (2) b := b + c (3) c := b + c

a. Perform renaming for the code a 0 := b 0 + c 0 b 1 := b 0 + c 0 c 1 := b 1 + c 0

b. Build a DAG for the renamed code

b 0 c 0

c 1

a 0 , b 1

b 0 c 0

c 1

a 0 , b 1

c. How do DAGs support optimizations? By exposing common subexpressions

  1. (6 pts) Control flow analysis For the following problems, consider this code: a := b L1: b := c if (...) goto L c := d if (...) goto L L2: d := a

a. What are the basic blocks? B1 = { S1 } B2 = { S2, S3 } B3 = { S4, S5 } B4 = { S6 } b. What is the control flow graph

a = b

d = a

c = d

if (…) goto L

b = c

if (…) goto L

B

B

B

B

a = b

d = a

c = d

if (…) goto L

b = c

if (…) goto L

B

B

B

B

c. What is a reverse Postorder numbering of the basic blocks? B1, B2, B3, B4 or B1, B2, B4, B d. Why do compilers use basic blocks? To find regions of code with no control flow where local optimizations may be performed. To reduce the size of the control flow graph. To allow effects of multiple statements to be summarized together.

  1. (20 pts) Available expressions Consider the following control flow graph for available expressions:

a. Calculate GEN/KILL for each basic block

GEN KILL

B1 a+c, a+b a+b B2 a+c (^) 

B3 a+b a+c B4 a+c (^) 

b. Solve available expressions, showing IN/OUT for each pass

Init Pass1 Pass2 Pass B1 IN (^)     OUT (^)  a+c, a+b a+c, a+b a+c, a+b

B2 IN (^)   a+b a+b OUT (^)  a+c a+c, a+b a+c, a+b B3 IN (^)   a+c, a+b a+c, a+b

OUT (^)  a+b a+b a+b B4 IN (^)  a+b a+b a+b

OUT (^)  a+c, a+b a+c, a+b a+c, a+b

  1. (12 pts) Data-flow analysis frameworks

Recall that  is used in data-flow iterative analysis to combine information where paths merge.

a. Using properties of the operator, prove a  b and b  a imply a = b. a  b implies a  b = a, and b  a implies b  a = b. Since (^)  is commutative, a  b = b  a, so a = a  b = b  a = b, and a = b.

b. For reaching definitions, pick values for a, b, c for which a > b and b > c. a = { a, b, c }, b = {a, b}, c = {b}

c. For very busy expressions, pick values for a, b for which neither a  b or b  a are true? a = {a+b}, b = {a+c}

d. How is defined in terms of the operator?  a =

e. What is monotonicity and why is it important for iterative data-flow problems? Monotonicity means f(x  y)  f(x)  f(y), and is required for guaranteeing iterative data-flow problems will converge.