COMS W4115
Programming Languages and Translators
Homework Assignment #4
Submit solutions electronically on
    by 2:40pm, April 24, 2013



  1. Consider the arithmetic expression u * (v - w) + x / y and a register machine with instructions of the form
         LD reg, src
         ST dst, reg
         OP reg1, reg2, reg3      // the registers need not be distinct
    1. Draw an abstract syntax tree for the expression and label the nodes with Ershov numbers.
    2. Generate machine code for the expression on a two-register machine minimizing the number of spills.

  3. Consider the following sequence of three-address code:
       x = 0
       i = 0
    L: t1 = i * 4
       t2 = a[t1]
       t3 = i * 4
       t4 = b[t3]
       t5 = t2 * t4
       x = x + t5
       i = i + 1
       if i < n goto L
    1. Draw a flow graph for this three-address code.
    2. Optimize this code by eliminating common subexpressions, performing reduction in strength on induction variables, and eliminating all the induction variables that you can. State what transformations you are using at each optimization step.

  5. Consider the lambda-calculus expression (λu. (λx. u) u) ((λy. y) (λw. (λv.v) w) ).
    1. Draw a parse tree for this expression.
    2. Identify all redexes in this expression.
    3. Evaluate this expression using normal order evaluation.
    4. Evaluate this expression using applicative order evaluation.

  6. Evaluate the lambda-calculus expression (λx.(λy.(x(λx.xy))))y. What order of evaluation did you use? Explain all the steps you used in the evaluation.