handouts/ho07.tex
changeset 373 b018234c9126
parent 372 d6af4b1239de
child 374 0e25fb72d339
equal deleted inserted replaced
372:d6af4b1239de 373:b018234c9126
   361 pair consisting of the code and an environment:
   361 pair consisting of the code and an environment:
   362 
   362 
   363 \begin{center}
   363 \begin{center}
   364 \begin{tabular}{lcl}
   364 \begin{tabular}{lcl}
   365 $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
   365 $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
   366 \multicolumn{3}{l}{$\qquad l_\textit{ifelse}\;$ (fresh label)}\\
   366 \multicolumn{3}{l}{$\qquad L_\textit{ifelse}\;$ (fresh label)}\\
   367 \multicolumn{3}{l}{$\qquad l_\textit{ifend}\;$ (fresh label)}\\
   367 \multicolumn{3}{l}{$\qquad L_\textit{ifend}\;$ (fresh label)}\\
   368 \multicolumn{3}{l}{$\qquad (is_1, E') = \textit{compile}(cs_1, E)$}\\
   368 \multicolumn{3}{l}{$\qquad (is_1, E') = \textit{compile}(cs_1, E)$}\\
   369 \multicolumn{3}{l}{$\qquad (is_2, E'') = \textit{compile}(cs_2, E')$}\\
   369 \multicolumn{3}{l}{$\qquad (is_2, E'') = \textit{compile}(cs_2, E')$}\\
   370 \multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, l_\textit{ifelse})$}\\
   370 \multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, L_\textit{ifelse})$}\\
   371 \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\
   371 \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\
   372 \multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;l_\textit{ifend}$}\\
   372 \multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;L_\textit{ifend}$}\\
   373 \multicolumn{3}{l}{$\qquad\phantom{(}@\;l_\textit{ifelse}:$}\\
   373 \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifelse}:$}\\
   374 \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\
   374 \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\
   375 \multicolumn{3}{l}{$\qquad\phantom{(}@\;l_\textit{ifend}:, E'')$}\\
   375 \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifend}:, E'')$}\\
   376 \end{tabular}
   376 \end{tabular}
   377 \end{center}
   377 \end{center}
       
   378 
       
   379 \noindent In the first two lines we generate two fresh labels
       
   380 for the jump addresses (just before the else-branch and just
       
   381 after). In the next two lines we generate the instructions for
       
   382 the two branches, $is_1$ and $is_2$. The final code will
       
   383 be first the code for $b$ (including the label 
       
   384 just-before-the-else-branch), then the \pcode{goto} for after
       
   385 the else-branch, the label $L_\textit{ifesle}$, followed by
       
   386 the instructions for the else-branch, followed by the 
       
   387 after-the-else-branch label. Consider for example the 
       
   388 if-statement:
       
   389 
       
   390 \begin{lstlisting}[mathescape,numbers=none,language={}]
       
   391 if 1 = 1 then x := 2 else y := 3
       
   392 \end{lstlisting}
       
   393 
       
   394 \noindent 
       
   395 The generated code is as follows:
       
   396 
       
   397 \begin{lstlisting}[mathescape,language={}]
       
   398    ldc 1
       
   399    ldc 2
       
   400    if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$
       
   401    ldc 2
       
   402    istore 0
       
   403    goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$
       
   404 L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$
       
   405    ldc 3
       
   406    istore 1
       
   407 L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$
       
   408 \end{lstlisting}
       
   409 
       
   410 \begin{tikzpicture}[remember picture,overlay]
       
   411   \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) 
       
   412            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east);
       
   413   \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) 
       
   414            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east);
       
   415 \end{tikzpicture}
       
   416 
       
   417 \noindent The first three lines correspond to the the boolean 
       
   418 expression $1 = 1$. The jump for when this boolean expression
       
   419 is false is in Line~3. Lines 4-6 corresponds to the if-branch; 
       
   420 the else-branch is in Lines 8 and 9. Note carefully how the
       
   421 environment $E$ is threaded through the calls of 
       
   422 \textit{compile}. The function receives an environment $E$, 
       
   423 but it might extend it when compiling the if-branch, yielding 
       
   424 $E'$. This happens for example in the if-statement above 
       
   425 whenever the variable \code{x} has not been used before. 
       
   426 Similarly with the environment $E''$ for the second call
       
   427 to \textit{compile}. $E''$ is also the environment that need
       
   428 to be returned.
       
   429 
       
   430 The compilation of the while-loops, say 
       
   431 \pcode{while} $b$ \pcode{do} $cs$, is very similar. In case
       
   432 the condition is true and we need to do another iteration, the 
       
   433 control-flow needs to be as follows
       
   434 
       
   435 \begin{center}
       
   436 \begin{tikzpicture}[node distance=2mm and 4mm,
       
   437  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
       
   438  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
       
   439  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
       
   440 \node (A0) [point, left=of A1] {};
       
   441 \node (A1) [point] {};
       
   442 \node (b) [block, right=of A1] {code of $b$};
       
   443 \node (A2) [point, right=of b] {};
       
   444 \node (cs1) [block, right=of A2] {code of $cs$};
       
   445 \node (A3) [point, right=of cs1] {};
       
   446 \node (A4) [point, right=of A3] {};
       
   447 
       
   448 \draw (A0) edge [->, black, line width=1mm] (b);
       
   449 \draw (b) edge [->, black, line width=1mm] (cs1);
       
   450 \draw (cs1) edge [->, black, line width=1mm] (A3);
       
   451 \draw (A3) edge [->,skip loop] (A1);
       
   452 \end{tikzpicture}
       
   453 \end{center}
       
   454 
       
   455 \noindent While if the condition is \emph{not} true we
       
   456 need to jump out of the loop, which gives the following
       
   457 control flow.
       
   458 
       
   459 \begin{center}
       
   460 \begin{tikzpicture}[node distance=2mm and 4mm,
       
   461  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
       
   462  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
       
   463  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
       
   464 \node (A0) [point, left=of A1] {};
       
   465 \node (A1) [point] {};
       
   466 \node (b) [block, right=of A1] {code of $b$};
       
   467 \node (A2) [point, right=of b] {};
       
   468 \node (cs1) [block, right=of A2] {code of $cs$};
       
   469 \node (A3) [point, right=of cs1] {};
       
   470 \node (A4) [point, right=of A3] {};
       
   471 
       
   472 \draw (A0) edge [->, black, line width=1mm] (b);
       
   473 \draw (b) edge [->, black, line width=1mm] (A2);
       
   474 \draw (A2) edge [skip loop] (A3);
       
   475 \draw (A3) edge [->, black, line width=1mm] (A4);
       
   476 \end{tikzpicture}
       
   477 \end{center}
       
   478 
       
   479 \noindent Again we can use the \textit{compile}-function for
       
   480 boolean expressions to insert the appropriate jump to the
       
   481 end of the loop (label $L_{wend}$).
       
   482 
       
   483 \begin{center}
       
   484 \begin{tabular}{lcl}
       
   485 $\textit{compile}(\pcode{while}\; b\; \pcode{do} \;cs, E)$ & $\dn$\\ 
       
   486 \multicolumn{3}{l}{$\qquad L_{wbegin}\;$ (fresh label)}\\
       
   487 \multicolumn{3}{l}{$\qquad L_{wend}\;$ (fresh label)}\\
       
   488 \multicolumn{3}{l}{$\qquad (is, E') = \textit{compile}(cs_1, E)$}\\
       
   489 \multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\
       
   490 \multicolumn{3}{l}{$\qquad\phantom{(}@\;\textit{compile}(b, E, L_{wend})$}\\
       
   491 \multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\
       
   492 \multicolumn{3}{l}{$\qquad\phantom{(}@\; \text{goto}\;l_{wbegin}$}\\
       
   493 \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_{wend}:, E')$}\\
       
   494 \end{tabular}
       
   495 \end{center}
       
   496 
       
   497 Next we need to consider the statement \pcode{write x}, which 
       
   498 can be used to write out the content of a variable. For this 
       
   499 we need to use a Java library function. In order to avoid
       
   500 having to generate a lot of code for each 
       
   501 \pcode{write}-command, we use a separate method and just call
       
   502 this method with an appropriate stack.
       
   503 
       
   504 \begin{lstlisting}[language=JVMIS]
       
   505 .method public static write(I)V 
       
   506     .limit locals 5 
       
   507     .limit stack 5  
       
   508     getstatic java/lang/System/out Ljava/io/PrintStream; 
       
   509     iload 0
       
   510     invokevirtual java/io/PrintStream/println(I)V 
       
   511     return 
       
   512 .end method
       
   513 \end{lstlisting}
   378 
   514 
   379 \end{document}
   515 \end{document}
   380 
   516 
   381 %%% Local Variables: 
   517 %%% Local Variables: 
   382 %%% mode: latex  
   518 %%% mode: latex