handouts/ho09.tex
changeset 917 89e05a230d2d
parent 913 eef6a56c185a
equal deleted inserted replaced
916:10f834eb0a9e 917:89e05a230d2d
   783 \end{lstlisting}
   783 \end{lstlisting}
   784 \caption{Abstract syntax trees for the Fun-language and the K-language.\label{absfun}}
   784 \caption{Abstract syntax trees for the Fun-language and the K-language.\label{absfun}}
   785 \end{figure}
   785 \end{figure}
   786 
   786 
   787 
   787 
       
   788 \section*{Alternatives to CPS}
       
   789 
       
   790 While I appreciate that this handout is already pretty long, this
       
   791 section is for students who think the CPS-translation is too much of
       
   792 voodoo programming---there is a perhaps simpler alternative. This
       
   793 alternative is along the lines: if you cannot bridge the gap in
       
   794 a single step, do it in two simpler steps. Let's look at the
       
   795 simple expression $1 + (2 + 3)$. The CPS-translation correctly
       
   796 generates the expression
       
   797 
       
   798 \begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}]
       
   799 let tmp0 = add 2 3 in   
       
   800 let tmp1 = add 1 tmp0 in 
       
   801   return tmp1 
       
   802 \end{lstlisting}
       
   803 
       
   804 \noindent
       
   805 where $(2 + 3)$ is pulled out and calculated first. The problem is that it
       
   806 requires a bit of magic. But with the ability to give a separate variable
       
   807 to each indivifual computation, we could do the following: The expression
       
   808 $1 + (2 + 3)$ is a tree like this
       
   809 
       
   810 \begin{center}
       
   811 \begin{tikzpicture}[line width=1mm]
       
   812   \node {root} [grow'=up]
       
   813     child {node {1}}
       
   814     child {node[circle,draw] {+}
       
   815       child {node {2}}
       
   816       child {node {3}}
       
   817     };
       
   818 \end{tikzpicture}
       
   819 \end{center}
       
   820 
       
   821 \noindent
       
   822 and we could perform a completely standard recursive traversal of the
       
   823 tree: each inner node gets a new variable and assignment.  
       
   824 
       
   825 
   788 
   826 
   789 \noindent
   827 \noindent
   790 \end{document}
   828 \end{document}
   791 
   829 
   792 
   830