|
@@ -16,6 +16,7 @@
|
|
|
\usepackage{color}
|
|
|
%\usepackage{ifthen}
|
|
|
\usepackage{upquote}
|
|
|
+\usepackage[all]{xy}
|
|
|
|
|
|
|
|
|
\definecolor{lightgray}{gray}{1}
|
|
@@ -10620,21 +10621,22 @@ $\Rightarrow$
|
|
|
|
|
|
Recall that in the \code{explicate\_control} pass we define one helper
|
|
|
function for each kind of position in the program. For the \LangVar{}
|
|
|
-language of integers and variables we needed kinds of positions:
|
|
|
-assignment and tail. The \code{if} expressions of \LangIf{} introduced
|
|
|
-predicate positions. For \LangLoop{}, the \code{begin} expression introduces
|
|
|
-yet another kind of position: effect position. Except for the last
|
|
|
+language of integers and variables we needed assignment and tail
|
|
|
+positions. The \code{if} expressions of \LangIf{} introduced predicate
|
|
|
+positions. For \LangLoop{}, the \code{begin} expression introduces yet
|
|
|
+another kind of position: effect position. Except for the last
|
|
|
subexpression, the subexpressions inside a \code{begin} are evaluated
|
|
|
only for their effect. Their result values are discarded. We can
|
|
|
generate better code by taking this fact into account.
|
|
|
|
|
|
The output language of \code{explicate\_control} is \LangCLoop{}
|
|
|
(Figure~\ref{fig:c7-syntax}), which is nearly identical to
|
|
|
-\LangCIf{}. The only syntactic difference is that \code{read} may also
|
|
|
-appear as a statement. The most significant difference between the
|
|
|
-programs generated by \code{explicate\_control} in
|
|
|
-Chapter~\ref{ch:Lif} versus \code{explicate\_control} in this chapter
|
|
|
-is that the control-flow graphs of the later may contain cycles.
|
|
|
+\LangCIf{}. The only syntactic difference is the addition of \VOID{}
|
|
|
+and that \code{read} may appear as a statement. The most significant
|
|
|
+difference between the programs generated by \code{explicate\_control}
|
|
|
+in Chapter~\ref{ch:Lif} versus \code{explicate\_control} in this
|
|
|
+chapter is that the control-flow graphs of the later may contain
|
|
|
+cycles.
|
|
|
|
|
|
\begin{figure}[tp]
|
|
|
\fbox{
|
|
@@ -10657,21 +10659,33 @@ is that the control-flow graphs of the later may contain cycles.
|
|
|
\end{figure}
|
|
|
|
|
|
The new auxiliary function \code{explicate\_effect} takes an
|
|
|
-expression (in an effect position) and a continuation. The function
|
|
|
-returns a $\Tail$ that includes the generated code for the input
|
|
|
-expression followed by the continuation. If the expression is
|
|
|
-obviously pure, that is, never causes side effects, then the
|
|
|
-expression can be removed, so the result is just the continuation.
|
|
|
-%
|
|
|
-The $\WHILE{\itm{cnd}}{\itm{body}}$ expression is the most interesting
|
|
|
-case. First, you will need a fresh label $\itm{loop}$ for the top of
|
|
|
-the loop. Recursively process the \itm{body} (in effect position)
|
|
|
-with the a \code{goto} to $\itm{loop}$ as the continuation, producing
|
|
|
-\itm{body'}. Next, process the \itm{cnd} (in predicate position) with
|
|
|
+expression (in an effect position) and the code for its
|
|
|
+continuation. The function returns a $\Tail$ that includes the
|
|
|
+generated code for the input expression followed by the
|
|
|
+continuation. If the expression is obviously pure, that is, never
|
|
|
+causes side effects, then the expression can be removed, so the result
|
|
|
+is just the continuation.
|
|
|
+%
|
|
|
+The case for $\WHILE{\itm{cnd}}{\itm{body}}$ expressions is
|
|
|
+interesting; the generated code is depicted in the following diagram.
|
|
|
+\begin{center}
|
|
|
+ \begin{minipage}{0.3\textwidth}
|
|
|
+\xymatrix{
|
|
|
+ *+[F=]{\txt{\code{goto} \itm{loop}}} \ar[r]
|
|
|
+ & *+[F]{\txt{\itm{loop}: \\ \itm{cnd'}}} \ar[r]^{else} \ar[d]^{then}
|
|
|
+ & *+[F]{\txt{\itm{cont}}} \\
|
|
|
+ & *+[F]{\txt{\itm{body'} \\ \code{goto} \itm{loop}}} \ar@/^50pt/[u]
|
|
|
+}
|
|
|
+\end{minipage}
|
|
|
+\end{center}
|
|
|
+We start by creating a fresh label $\itm{loop}$ for the top of the
|
|
|
+loop. Next, recursively process the \itm{body} (in effect position)
|
|
|
+with a \code{goto} to $\itm{loop}$ as the continuation, producing
|
|
|
+\itm{body'}. Process the \itm{cnd} (in predicate position) with
|
|
|
\itm{body'} as the then-branch and the continuation block as the
|
|
|
-else-branch. The result should be added to the control-flow graph with
|
|
|
-the label \itm{loop}. The result for the whole \code{while} loop is a
|
|
|
-\code{goto} to the \itm{loop} label.
|
|
|
+else-branch. The result should be added to the dictionary of
|
|
|
+\code{basic-blocks} with the label \itm{loop}. The result for the
|
|
|
+whole \code{while} loop is a \code{goto} to the \itm{loop} label.
|
|
|
|
|
|
The auxiliary functions for tail, assignment, and predicate positions
|
|
|
need to be updated. The three new language forms, \code{while},
|
|
@@ -10767,7 +10781,8 @@ The \code{analyze\_dataflow} function has four parameters.
|
|
|
%\node (F1-2) at (9,0) {\large \LangLoopFunRef{}};
|
|
|
%\node (F1-3) at (6,0) {\large \LangLoopFunRef{}};
|
|
|
\node (F1-4) at (6,2) {\large \LangLoop{}};
|
|
|
-\node (F1-5) at (9,2) {\large \LangLoopANF{}};
|
|
|
+\node (F1-5) at (9,2) {\large \LangLoop{}};
|
|
|
+\node (F1-6) at (12,2) {\large \LangLoopANF{}};
|
|
|
\node (C3-2) at (3,0) {\large \racket{\LangCLoop{}}\python{\LangCIf{}}};
|
|
|
|
|
|
\node (x86-2) at (3,-2) {\large \LangXIfVar{}};
|
|
@@ -10795,8 +10810,10 @@ The \code{analyze\_dataflow} function has four parameters.
|
|
|
%% \path[->,bend right=15] (F1-3) edge [above] node
|
|
|
%% {\ttfamily\footnotesize expose-alloc.} (F1-4);
|
|
|
\path[->,bend left=15] (F1-4) edge [above] node
|
|
|
- {\ttfamily\footnotesize remove\_complex.} (F1-5);
|
|
|
-\path[->,bend left=15] (F1-5) edge [right] node
|
|
|
+ {\ttfamily\footnotesize uncover\_get!} (F1-5);
|
|
|
+\path[->,bend left=15] (F1-5) edge [above] node
|
|
|
+ {\ttfamily\footnotesize remove\_complex.} (F1-6);
|
|
|
+\path[->,bend left=15] (F1-6) edge [right] node
|
|
|
{\ttfamily\footnotesize explicate\_control} (C3-2);
|
|
|
\path[->,bend left=15] (C3-2) edge [left] node
|
|
|
{\ttfamily\footnotesize select\_instr.} (x86-2);
|