\documentclass{article}
\usepackage{graphs}
\usepackage{fullpage}
\usepackage{array}

\title{Assignment 1\\ Instruction Selection and Register Allocation}
\author{15-411: Compiler Design\\
Rob Arnold (rdarnold@andrew) and Eugene Marinelli (emarinel@andrew)}
\date{Due: Tuesday, September 9, 2008 (1:30 pm)}

\begin{document}
\maketitle

\noindent\textbf{Reminder:} Assignments are individual assignments,
not done in pairs.  The work must be all your own.

You may hand in a handwritten solution or a printout of a typeset
solution at the beginning of lecture on Tuesday, September 9.  Please
read the late policy for written assignments on the course web page.
If you decide not to typeset your answers, make sure the text and
pictures are legible and clear.

In this assignment you will generate machine code to evaluate a simple
arithmetic expression, carrying out some non-trivial compilation steps.
The expression is
\begin{verbatim}
  { return (7 + 3 * (8 - 2)) - 4 * (9 + 5); }
\end{verbatim}

\section*{Problem 1 (20 points)}

\begin{enumerate}
\item [(a)] Construct an abstract syntax tree (AST) corresponding to the
  expression.  For example, the AST for the expression
\begin{verbatim}
  { return 1 + 2; }
\end{verbatim}
  would be represented as\footnote{The \LaTeX{} graph library used to
    generate these trees can be found at
    \texttt{http://www.cs.umu.se/\~{}drewes/graphs/}. The library
    apparently doesn't work with \texttt{pdflatex}.  You can work
    around this using \texttt{latex} and \texttt{dvipdf}, for
    example.}:

  \begin{center}
    \begin{graph}(2,2)
      \textnode{RETURN}(1,2){RETURN}
      \textnode{PLUS}(1,1){$+$}
      \textnode{ONE}(0,0){CONST(1)}
      \textnode{TWO}(2,0){CONST(2)}
      \edge{RETURN}{PLUS}
      \edge{PLUS}{ONE}
      \edge{PLUS}{TWO}
    \end{graph}
  \end{center}

\item [(b)] Translate the AST from problem 1(a) into linear
  three-address form by applying maximal munch using the tiles in
  the table below.  Temporaries should be called \texttt{t}0,
  \texttt{t}1, ..., \texttt{t}$n$.

  \begin{center}
    \begin{tabular}{|m{1in}|m{1in}|}
      \hline
      Tile & IR \\
      \hline
      \hline
      \begin{graph}(2,2)
        \textnode{CONST}(1,1){CONST($n$)}
      \end{graph}
      & \texttt{t$i$ <- $n$;}\\
      \hline
      \begin{graph}(2,2)
        \textnode{RETURN}(1,1){RETURN}
        \textnode{NIL}(1,0){}
        \edge{RETURN}{NIL}
      \end{graph}
      & \texttt{return t$i$;}\\
      \hline
      \begin{graph}(2,2)
        \textnode{OP}(1,1){+}
        \textnode{NIL1}(0,0){}
        \textnode{NIL2}(2,0){}
        \edge{OP}{NIL1}
        \edge{OP}{NIL2}
      \end{graph}
      & \texttt{t$i$ <- t$j$ + t$k$;}\\
      \hline
      \begin{graph}(2,2)
        \textnode{OP}(1,1){-}
        \textnode{NIL1}(0,0){}
        \textnode{NIL2}(2,0){}
        \edge{OP}{NIL1}
        \edge{OP}{NIL2}
      \end{graph}
      & \texttt{t$i$ <- t$j$ - t$k$;}\\
      \hline
      \begin{graph}(2,2)
        \textnode{OP}(1,1){*}
        \textnode{NIL1}(0,0){}
        \textnode{NIL2}(2,0){}
        \edge{OP}{NIL1}
        \edge{OP}{NIL2}
      \end{graph}
      & \texttt{t$i$ <- t$j$ * t$k$;}\\
      \hline
    \end{tabular}
  \end{center}

    For example, the AST from part (a) would be translated to
\begin{verbatim}
  t0 <- 1;
  t1 <- 2;
  t2 <- t0 + t1;
  return t2;
\end{verbatim}

\end{enumerate}

\section*{Problem 2 (20 points)}

\begin{enumerate}
\item[(a)] Compute the live variables after each statement in the
  program generated in problem 1b.

\item[(b)] Construct the interference graph.
Is it chordal\footnote{Refer to the lecture notes for Lecture 3 at \texttt{http://www.cs.cmu.edu/\~{}fp/courses/15411-f08/lectures/03-regalloc.pdf}.}?

\item[(c)] Reorder the statements so that the program gives the same
  result, but there is less interference (i.e., fewer registers are
  needed), or state that the instruction order is already optimal in
  this sense.  If it is already optimal, you only need to show
  one answer each for parts (d) and (e).

\item[(d)] Construct elimination orders using maximal cardinality
  search for each of the two interference graphs.  If the algorithm
  has a choice, list the lowest-numbered temporary first.

\item[(e)] Use greedy coloring to allocate registers for both the
  original program and the reordered program.  Label the registers
  \texttt{r}0, \texttt{r}1, ..., \texttt{r}$n$.

\end{enumerate}

\section*{Problem 3 (20 points)}

\begin{enumerate}
\item[(a)] Translate the program from the AST in problem 1(a) into an
  ABI-conformant\footnote{Refer to
    \texttt{http://www.x86-64.org/documentation/abi.pdf}.} x86-64
  assembly routine, selecting instructions and allocating the
  registers using an algorithm of your choosing.  Describe briefly
  which algorithm you used.  Use the general-purpose x86-64
  registers\footnote{Refer to page 7 of
    \tt{http://www.cs.cmu.edu/\~{}fp/courses/15411-f08/misc/asm64-handout.pdf}.}
  and beware the calling conventions.  Assume that all arithmetic is
  done on signed 64-bit integers.  It should be possible to assemble
  your functions using the GNU assembler.  The function should be
  callable using the following C prototype:
\begin{verbatim}
extern long ass1a();
\end{verbatim}

\item[(b)] Handwrite assembly code using only \texttt{mov},
  \texttt{addq}, \texttt{mulq}, \texttt{subq}, and \texttt{ret}
  instructions which uses as few instructions as possible and carries
  out the required operations.  You should not use any laws of modular
  arithmetic such as commutativity, associativity, or distributivity
  to optimize the computation, and you should not use constant
  folding, that is, all operations should be performed at runtime.  It
  should be callable as
\begin{verbatim}
extern long ass1b();
\end{verbatim}

\item[(c)] How many instructions appear in the systematically
  constructed assembly code, and how many instructions does your
  hand-crafted code have?  Explain why you believe your code
  to be optimal, under the given constraints.  Do you believe
  your code also uses the minimal number of necessary registers?
  Explain why or why not.

\end{enumerate}

\end{document}
