# HG changeset patch # User Christian Urban # Date 1447732928 0 # Node ID b018234c9126e33a272e4e1c865e2a2b93294b9c # Parent d6af4b1239de9c5d8945006f52ed299542b93c7b updated diff -r d6af4b1239de -r b018234c9126 coursework/cw04.pdf Binary file coursework/cw04.pdf has changed diff -r d6af4b1239de -r b018234c9126 coursework/cw04.tex --- a/coursework/cw04.tex Tue Nov 17 01:58:50 2015 +0000 +++ b/coursework/cw04.tex Tue Nov 17 04:02:08 2015 +0000 @@ -238,11 +238,10 @@ \begin{minipage}{12cm} \begin{lstlisting}[language=JVMIS, numbers=none] .method public static write(I)V - .limit locals 5 - .limit stack 5 - iload 0 + .limit locals 1 + .limit stack 2 getstatic java/lang/System/out Ljava/io/PrintStream; - swap + iload 0 invokevirtual java/io/PrintStream/println(I)V return .end method diff -r d6af4b1239de -r b018234c9126 handouts/ho07.pdf Binary file handouts/ho07.pdf has changed diff -r d6af4b1239de -r b018234c9126 handouts/ho07.tex --- a/handouts/ho07.tex Tue Nov 17 01:58:50 2015 +0000 +++ b/handouts/ho07.tex Tue Nov 17 04:02:08 2015 +0000 @@ -359,19 +359,153 @@ \begin{center} \begin{tabular}{lcl} $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ -\multicolumn{3}{l}{$\qquad l_\textit{ifelse}\;$ (fresh label)}\\ -\multicolumn{3}{l}{$\qquad l_\textit{ifend}\;$ (fresh label)}\\ +\multicolumn{3}{l}{$\qquad L_\textit{ifelse}\;$ (fresh label)}\\ +\multicolumn{3}{l}{$\qquad L_\textit{ifend}\;$ (fresh label)}\\ \multicolumn{3}{l}{$\qquad (is_1, E') = \textit{compile}(cs_1, E)$}\\ \multicolumn{3}{l}{$\qquad (is_2, E'') = \textit{compile}(cs_2, E')$}\\ -\multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, l_\textit{ifelse})$}\\ +\multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, L_\textit{ifelse})$}\\ \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\ -\multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;l_\textit{ifend}$}\\ -\multicolumn{3}{l}{$\qquad\phantom{(}@\;l_\textit{ifelse}:$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;L_\textit{ifend}$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifelse}:$}\\ \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\ -\multicolumn{3}{l}{$\qquad\phantom{(}@\;l_\textit{ifend}:, E'')$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifend}:, E'')$}\\ \end{tabular} \end{center} +\noindent In the first two lines we generate two fresh labels +for the jump addresses (just before the else-branch and just +after). In the next two lines we generate the instructions for +the two branches, $is_1$ and $is_2$. The final code will +be first the code for $b$ (including the label +just-before-the-else-branch), then the \pcode{goto} for after +the else-branch, the label $L_\textit{ifesle}$, followed by +the instructions for the else-branch, followed by the +after-the-else-branch label. Consider for example the +if-statement: + +\begin{lstlisting}[mathescape,numbers=none,language={}] +if 1 = 1 then x := 2 else y := 3 +\end{lstlisting} + +\noindent +The generated code is as follows: + +\begin{lstlisting}[mathescape,language={}] + ldc 1 + ldc 2 + if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$ + ldc 2 + istore 0 + goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$ +L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$ + ldc 3 + istore 1 +L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$ +\end{lstlisting} + +\begin{tikzpicture}[remember picture,overlay] \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) + -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east); + \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) + -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); \end{tikzpicture} + +\noindent The first three lines correspond to the the boolean +expression $1 = 1$. The jump for when this boolean expression +is false is in Line~3. Lines 4-6 corresponds to the if-branch; +the else-branch is in Lines 8 and 9. Note carefully how the +environment $E$ is threaded through the calls of +\textit{compile}. The function receives an environment $E$, +but it might extend it when compiling the if-branch, yielding +$E'$. This happens for example in the if-statement above +whenever the variable \code{x} has not been used before. +Similarly with the environment $E''$ for the second call +to \textit{compile}. $E''$ is also the environment that need +to be returned. + +The compilation of the while-loops, say +\pcode{while} $b$ \pcode{do} $cs$, is very similar. In case +the condition is true and we need to do another iteration, the +control-flow needs to be as follows + +\begin{center} +\begin{tikzpicture}[node distance=2mm and 4mm, + block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, + point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, + skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] +\node (A0) [point, left=of A1] {}; +\node (A1) [point] {}; +\node (b) [block, right=of A1] {code of $b$}; +\node (A2) [point, right=of b] {}; +\node (cs1) [block, right=of A2] {code of $cs$}; +\node (A3) [point, right=of cs1] {}; +\node (A4) [point, right=of A3] {}; + +\draw (A0) edge [->, black, line width=1mm] (b); +\draw (b) edge [->, black, line width=1mm] (cs1); +\draw (cs1) edge [->, black, line width=1mm] (A3); +\draw (A3) edge [->,skip loop] (A1); +\end{tikzpicture} +\end{center} + +\noindent While if the condition is \emph{not} true we +need to jump out of the loop, which gives the following +control flow. + +\begin{center} +\begin{tikzpicture}[node distance=2mm and 4mm, + block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, + point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, + skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] +\node (A0) [point, left=of A1] {}; +\node (A1) [point] {}; +\node (b) [block, right=of A1] {code of $b$}; +\node (A2) [point, right=of b] {}; +\node (cs1) [block, right=of A2] {code of $cs$}; +\node (A3) [point, right=of cs1] {}; +\node (A4) [point, right=of A3] {}; + +\draw (A0) edge [->, black, line width=1mm] (b); +\draw (b) edge [->, black, line width=1mm] (A2); +\draw (A2) edge [skip loop] (A3); +\draw (A3) edge [->, black, line width=1mm] (A4); +\end{tikzpicture} +\end{center} + +\noindent Again we can use the \textit{compile}-function for +boolean expressions to insert the appropriate jump to the +end of the loop (label $L_{wend}$). + +\begin{center} +\begin{tabular}{lcl} +$\textit{compile}(\pcode{while}\; b\; \pcode{do} \;cs, E)$ & $\dn$\\ +\multicolumn{3}{l}{$\qquad L_{wbegin}\;$ (fresh label)}\\ +\multicolumn{3}{l}{$\qquad L_{wend}\;$ (fresh label)}\\ +\multicolumn{3}{l}{$\qquad (is, E') = \textit{compile}(cs_1, E)$}\\ +\multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\;\textit{compile}(b, E, L_{wend})$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\; \text{goto}\;l_{wbegin}$}\\ +\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_{wend}:, E')$}\\ +\end{tabular} +\end{center} + +Next we need to consider the statement \pcode{write x}, which +can be used to write out the content of a variable. For this +we need to use a Java library function. In order to avoid +having to generate a lot of code for each +\pcode{write}-command, we use a separate method and just call +this method with an appropriate stack. + +\begin{lstlisting}[language=JVMIS] +.method public static write(I)V + .limit locals 5 + .limit stack 5 + getstatic java/lang/System/out Ljava/io/PrintStream; + iload 0 + invokevirtual java/io/PrintStream/println(I)V + return +.end method +\end{lstlisting} + \end{document} %%% Local Variables: diff -r d6af4b1239de -r b018234c9126 progs/compile.scala --- a/progs/compile.scala Tue Nov 17 01:58:50 2015 +0000 +++ b/progs/compile.scala Tue Nov 17 04:02:08 2015 +0000 @@ -39,11 +39,10 @@ .end method .method public static write(I)V - .limit locals 5 - .limit stack 5 - iload 0 + .limit locals 1 + .limit stack 2 getstatic java/lang/System/out Ljava/io/PrintStream; - swap + iload 0 invokevirtual java/io/PrintStream/println(I)V return .end method diff -r d6af4b1239de -r b018234c9126 progs/fib.j --- a/progs/fib.j Tue Nov 17 01:58:50 2015 +0000 +++ b/progs/fib.j Tue Nov 17 04:02:08 2015 +0000 @@ -9,50 +9,48 @@ .end method .method public static write(I)V - .limit locals 5 - .limit stack 5 - iload 0 + .limit locals 1 + .limit stack 2 getstatic java/lang/System/out Ljava/io/PrintStream; - swap + iload 0 invokevirtual java/io/PrintStream/println(I)V return .end method .method public static read()I - .limit locals 10 - .limit stack 10 + .limit locals 10 + .limit stack 10 - ldc 0 - istore 1 ; this will hold our final integer + ldc 0 + istore 1 ; this will hold our final integer Label1: - getstatic java/lang/System/in Ljava/io/InputStream; - invokevirtual java/io/InputStream/read()I - istore 2 - iload 2 - ldc 10 ; the newline delimiter - isub - ifeq Label2 - iload 2 - ldc 32 ; the space delimiter - isub - ifeq Label2 + getstatic java/lang/System/in Ljava/io/InputStream; + invokevirtual java/io/InputStream/read()I + istore 2 + iload 2 + ldc 10 ; the newline delimiter + isub + ifeq Label2 + iload 2 + ldc 32 ; the space delimiter + isub + ifeq Label2 - iload 2 - ldc 48 ; we have our digit in ASCII, have to subtract it from 48 - isub - ldc 10 - iload 1 - imul - iadd - istore 1 - goto Label1 + iload 2 + ldc 48 ; we have our digit in ASCII, have to subtract it from 48 + isub + ldc 10 + iload 1 + imul + iadd + istore 1 + goto Label1 Label2: - ;when we come here we have our integer computed in Local Variable 1 - iload 1 - ireturn + ;when we come here we have our integer computed in local variable 1 + iload 1 + ireturn .end method - .method public static main([Ljava/lang/String;)V .limit locals 200 .limit stack 200 @@ -87,7 +85,7 @@ Loop_end_1: -iload 2 +iload 1 invokestatic fib/fib/write(I)V diff -r d6af4b1239de -r b018234c9126 slides/slides07.tex --- a/slides/slides07.tex Tue Nov 17 01:58:50 2015 +0000 +++ b/slides/slides07.tex Tue Nov 17 04:02:08 2015 +0000 @@ -581,9 +581,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\mode{ \begin{frame}[t] -\frametitle{\begin{tabular}{c}Compiling Whiles\end{tabular}} +\frametitle{Compiling Whiles} {\Large\bl{$\text{while}\;b\;\text{do}\;cs$}}\bigskip\bigskip @@ -615,14 +614,12 @@ \end{tikzpicture} \end{center} - -\end{frame}} +\end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\mode{ \begin{frame}[t] -\frametitle{\begin{tabular}{c}Compiling Whiles\end{tabular}} +\frametitle{Compiling Whiles} {\Large\bl{while $b$ do $cs$}} @@ -640,7 +637,7 @@ \end{tabular}} \end{center} -\end{frame}} +\end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%