next up previous contents index
Next: return and throw statements Up: Applying the simplify algorithm Previous: do statements   Contents   Index


for statements

The for statement is quite complicated. Consider

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb$for($$a$\verb$; $$b$\verb$; $$c$\verb$) $$S$\end{tabbing}\end{minipage}\end{displaymath}

where $a$ is either a single declaration or a (possibly empty) comma-separated list of statement expressions, $b$ is an expression, $c$ is a (possibly empty) comma-separated list of statement expressions, and $S$ is a statement.

Suppose $a$ is the local variable declaration:

\begin{displaymath}
\mbox{\it m} \verb$ $ T \verb$ $ V_0 \verb$ $ I_0 \verb$, $ \ldots \verb$, $
V_{n-1} \verb$ $ I_{n-1}
\end{displaymath}

where definitions are as in the section beginning page [*]. Apply the transformation described in that section to the statement ``$a$ ;''. If this transformation does not produce any changes, and $T$ is not a multiarray type, the transformed version of the for construct is:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb$for($$a...
...$$\mathcal{C}$\verb$) $$\mathcal{T}$\end{tabbing}\end{minipage}\end{displaymath}

where $\mathcal{B}$, $\mathcal{C}$ and $\mathcal{T}$ will be defined below. Otherwise, if the transformed version of ``$a$ ;'' is $\mathcal{A}$, we may take the transformed version of the for construct to be:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb${$ \\
...
...verb$) $$\mathcal{T}$ \\
\verb$}$
\end{tabbing}\end{minipage}\end{displaymath}

Suppose, alternatively, that $a$ is the possibly-empty list of expressions:

\begin{displaymath}
a_0 \verb$, $ \ldots \verb$, $ a_{n-1}
\end{displaymath}

where $n \ge 0$. Again, apply the simplify algorithm to each expression in this list, and assume now that $\mbox{\it INITS}_i$ and $a'_i$ are the results of applying the algorithm to $a_i$. If the simplify algorithm doesn't change any of the expressions, and none of the $a_i$ are multiarrays, the transformed version of the for construct is again:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb$for($$a...
...$$\mathcal{C}$\verb$) $$\mathcal{T}$\end{tabbing}\end{minipage}\end{displaymath}

Otherwise, we may take the transformed version of the for construct to be:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb${$ \\
...
...verb$) $$\mathcal{T}$ \\
\verb$}$
\end{tabbing}\end{minipage}\end{displaymath}

Now we need to define $\mathcal{B}$, $\mathcal{C}$ and $\mathcal{T}$. We also introduce the symbols $s_b$ and $s_c$ to stand for possibly-empty statements added at the start and end of the loop body.

Apply the simplify algorithm to $b$, and let $\mbox{\it INITS}$ and $b'$ be the results. If the algorithm doesn't change $b$, then

\begin{displaymath}
\mathcal{B} = b
\end{displaymath}

and the statement $s_b$ is empty. If the algorithm does change $b$, then

\begin{displaymath}
\mathcal{B} = \verb$true$
\end{displaymath}

and the statement $s_b$ is:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb${$ \\
...
...$$b'$\verb$)) break ;$ \\
\verb$}$
\end{tabbing}\end{minipage}\end{displaymath}

Assume $c$ is the possibly-empty list of expressions:

\begin{displaymath}
c_0 \verb$, $ \ldots \verb$, $ c_{p-1}
\end{displaymath}

Apply the simplify algorithm to all these expressions. If the algorithm doesn't change any $c_i$, and none of the $c_i$ is a multiarray, then

\begin{displaymath}
\mathcal{C} = c
\end{displaymath}

and the statement $s_c$ is empty. If it does change some $c_i$, or any $c_i$ is a multiarray, then $\mathcal{C}$ is empty and and we will take the statement $s_c$ to be:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb${$ \\
...
...
\>$c'_{p-1}$\verb$ ;$ \\
\verb$}$
\end{tabbing}\end{minipage}\end{displaymath}

where now $\mbox{\it INITS}_i$ and $c'_i$ are the results of applying the algorithm to $c_i$.

Finally, if $s_b$ and $s_c$ are both empty, the value of $\mathcal{T}$ is just $S'$, the pre-translated version of $S$. Otherwise the block $\mathcal{T}$ has the form

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\begin{tabbing}
\verb${$ \\
...
...\>$S'$ \\
\>\=$s_c$ \\
\verb$}$
\end{tabbing}\end{minipage}\end{displaymath}


next up previous contents index
Next: return and throw statements Up: Applying the simplify algorithm Previous: do statements   Contents   Index
Bryan Carpenter 2003-04-15