Browse Source

updates to explicate

Jeremy Siek 3 years ago
parent
commit
90c2363936
1 changed files with 41 additions and 32 deletions
  1. 41 32
      book.tex

+ 41 - 32
book.tex

@@ -7288,14 +7288,12 @@ above example would be as follows.
 \begin{center}
 \begin{center}
 \begin{minipage}{0.96\textwidth}
 \begin{minipage}{0.96\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
-    ...
     cmpq $1, x
     cmpq $1, x
     setl %al
     setl %al
     movzbq %al, tmp
     movzbq %al, tmp
     cmpq $1, tmp
     cmpq $1, tmp
     je then_branch_1
     je then_branch_1
     jmp else_branch_1
     jmp else_branch_1
-    ...
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 \end{center}
 \end{center}
@@ -7309,11 +7307,9 @@ For example, we want to generate the following code for the inner
 \begin{center}
 \begin{center}
 \begin{minipage}{0.96\textwidth}
 \begin{minipage}{0.96\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
-    ...
     cmpq $1, x
     cmpq $1, x
     je then_branch_1
     je then_branch_1
     jmp else_branch_1
     jmp else_branch_1
-    ...
 \end{lstlisting}
 \end{lstlisting}
 \end{minipage}
 \end{minipage}
 \end{center}
 \end{center}
@@ -7393,7 +7389,7 @@ respectively.  In particular, we start \code{block40} with the
 comparison \racket{\code{(eq? x 0)}}\python{\code{x == 0}}
 comparison \racket{\code{(eq? x 0)}}\python{\code{x == 0}}
 and then branch to \code{block38} or
 and then branch to \code{block38} or
 \code{block39}, the two branches of the outer \key{if}, i.e.,
 \code{block39}, the two branches of the outer \key{if}, i.e.,
-\code{\code{(+ y 2)}}\python{\code{y + 2}} and
+\racket{\code{(+ y 2)}}\python{\code{y + 2}} and
 \racket{\code{(+ y 10)}}\python{\code{y + 10}}.
 \racket{\code{(+ y 10)}}\python{\code{y + 10}}.
 The story for \code{block41} is similar.
 The story for \code{block41} is similar.
 
 
@@ -7522,54 +7518,65 @@ following paragraphs we discuss specific cases in the
 We recommend implementing \code{explicate\_control} using four
 We recommend implementing \code{explicate\_control} using four
 auxiliary functions which we discuss in the following paragraphs.
 auxiliary functions which we discuss in the following paragraphs.
 \begin{description}
 \begin{description}
-\item[\code{explicate\_exp}] generates code for expressions in all
-  other contexts.
-\item[\code{explicate\_assign}] generates code for exprssions
-  that appear on the right-hand side of an assignment.
-\item[\code{explicate\_pred}] generates code for expressions
-  that appear in the condition of an \code{if}.
+\item[\code{explicate\_effect}] generates code for expressions as
+  statements, so their result is ignored and only their side-effects
+  matter.
+\item[\code{explicate\_assign}] generates code for expressions
+  on the right-hand side of an assignment.
+\item[\code{explicate\_pred}] generates code for an \code{if}
+  expression or statement by analyzing the condition expression.
 \item[\code{explicate\_stmt}] generates code for statements.
 \item[\code{explicate\_stmt}] generates code for statements.
 \end{description}
 \end{description}
 These four functions should incrementally build up the dictionary of
 These four functions should incrementally build up the dictionary of
 basic blocks. The following auxiliary function can be used to create a
 basic blocks. The following auxiliary function can be used to create a
 new basic block from a list of statements. It returns a \code{goto}
 new basic block from a list of statements. It returns a \code{goto}
 statement that jumps to the new basic block.
 statement that jumps to the new basic block.
+\begin{center}
+\begin{minipage}{\textwidth}
 \begin{lstlisting}
 \begin{lstlisting}
 def create_block(stmts, basic_blocks):
 def create_block(stmts, basic_blocks):
     label = label_name(generate_name('block'))
     label = label_name(generate_name('block'))
     basic_blocks[label] = stmts
     basic_blocks[label] = stmts
     return Goto(label)
     return Goto(label)
 \end{lstlisting}
 \end{lstlisting}
+\end{minipage}
+\end{center}
 Figure~\ref{fig:explicate-control-Rif} provides a skeleton for the
 Figure~\ref{fig:explicate-control-Rif} provides a skeleton for the
 \code{explicate\_control} pass.
 \code{explicate\_control} pass.
 
 
-The \code{explicate\_exp} function has three parameters: 1) the
+The \code{explicate\_effect} function has three parameters: 1) the
 expression to be compiled, 2) the already-compiled code for this
 expression to be compiled, 2) the already-compiled code for this
-expression's \emph{continuation}, that is, the code that should
-execute after this expression, and 3) the dictionary of generated
-basic blocks. The output of \code{explicate\_exp} is a list of
-\LangCIf{} statements.
-%
-The only kind of expression in \LangIf{} that is not in \LangCIf{} is
-the \code{if} expression, so that must be translated into something
-else.  The two branches should be translated using
-\code{explicate\_exp} and the condition expression should be
-translated using \code{explicate\_pred}.  All other expressions can be
-translated into a statment using \code{Expr}.
+expression's \emph{continuation}, that is, the list of statements that
+should execute after this expression, and 3) the dictionary of
+generated basic blocks. The output of \code{explicate\_effect} is a
+list of \LangCIf{} statements.
+%
+Let's consider a few of the cases for the expression to be compiled.
+If the expression to be compiled is a constant, then it can be
+discarded because it has no side effects. If it's a \CREAD{}, then
+that's a side-effect and should be preserved. So it should be
+translated into a statment using the \code{Expr} AST class. If the
+expression to be compiled is an \code{if} expression, we translate the
+two branches using \code{explicate\_effect} and then translate the
+condition expression using \code{explicate\_pred}, which generates
+code for the entire \code{if}.
 
 
 The \code{explicate\_assign} function has four parameters: 1) the
 The \code{explicate\_assign} function has four parameters: 1) the
 right-hand-side of the assignment, 2) the left-hand-side of the
 right-hand-side of the assignment, 2) the left-hand-side of the
 assignment (the variable), 3) the continuation, and 4) the dictionary
 assignment (the variable), 3) the continuation, and 4) the dictionary
-of basic blocks. When the right-hand-side is an \code{if} expression,
-there is some work to there. The two branches should be translated
-using \code{explicate\_assign} and the condition expression should be
+of basic blocks. The output of \code{explicate\_assign} is a list of
+\LangCIf{} statements.
+
+When the right-hand-side is an \code{if} expression, there is some
+work to do. In particular, the two branches should be translated using
+\code{explicate\_assign} and the condition expression should be
 translated using \code{explicate\_pred}.  Otherwise we can simply
 translated using \code{explicate\_pred}.  Otherwise we can simply
 generate an assignment statement with the given left and right-hand
 generate an assignment statement with the given left and right-hand
 sides.
 sides.
 
 
 \begin{figure}[tbp]
 \begin{figure}[tbp]
 \begin{lstlisting}
 \begin{lstlisting}
-def explicate_exp(e, cont, basic_blocks):
+def explicate_effect(e, cont, basic_blocks):
     match e:
     match e:
         case IfExp(test, body, orelse):
         case IfExp(test, body, orelse):
             ...
             ...
@@ -7607,7 +7614,7 @@ def explicate_stmt(s, cont, basic_blocks):
         case Assign([lhs], rhs):
         case Assign([lhs], rhs):
             return explicate_assign(rhs, lhs, cont, basic_blocks)
             return explicate_assign(rhs, lhs, cont, basic_blocks)
         case Expr(value):
         case Expr(value):
-            return explicate_exp(value, cont, basic_blocks)
+            return explicate_effect(value, cont, basic_blocks)
         case If(test, body, orelse):
         case If(test, body, orelse):
             ...
             ...
 
 
@@ -7655,9 +7662,10 @@ def explicate_control(p):
   two branches of the enclosing \key{if}.}
   two branches of the enclosing \key{if}.}
 %
 %
 \python{The \code{explicate\_pred} function has four parameters: 1)
 \python{The \code{explicate\_pred} function has four parameters: 1)
-  the condition expession, 2) the generated code for the ``then''
-  branch, 3) the generated code for the ``else'' branch, and 4) the
-  dictionary of basic blocks.}
+  the condition expession, 2) the generated statements for the
+  ``then'' branch, 3) the generated statements for the ``else''
+  branch, and 4) the dictionary of basic blocks. The output of
+  \code{explicate\_pred} is a list of \LangCIf{} statements.}
 %
 %
 Consider the case for comparison operators. We translate the
 Consider the case for comparison operators. We translate the
 comparison to an \code{if} statement whose branches are \code{goto}
 comparison to an \code{if} statement whose branches are \code{goto}
@@ -7670,7 +7678,7 @@ either the \code{thn} or \code{els} branch depending on whether the
 constant is \TRUE{} or \FALSE{}. This case demonstrates that we
 constant is \TRUE{} or \FALSE{}. This case demonstrates that we
 sometimes discard the \code{thn} or \code{els} blocks that are input
 sometimes discard the \code{thn} or \code{els} blocks that are input
 to \code{explicate\_pred}.
 to \code{explicate\_pred}.
-%
+
 The case for \key{if} expressions in \code{explicate\_pred} is
 The case for \key{if} expressions in \code{explicate\_pred} is
 particularly illuminating because it deals with the challenges we
 particularly illuminating because it deals with the challenges we
 discussed above regarding nested \key{if} expressions
 discussed above regarding nested \key{if} expressions
@@ -7813,6 +7821,7 @@ Add the following entry to the list of \code{passes} in
 
 
 \end{exercise}
 \end{exercise}
 
 
+\clearpage
 
 
 \section{Select Instructions}
 \section{Select Instructions}
 \label{sec:select-Rif}
 \label{sec:select-Rif}