| author | Christian Urban <urbanc@in.tum.de> | 
| Wed, 20 Nov 2019 17:10:03 +0000 | |
| changeset 695 | a936b1717b1b | 
| parent 692 | 96fa4a73ab48 | 
| child 705 | a5fa8ab52fe0 | 
| permissions | -rw-r--r-- | 
| 601 | 1 | % !TEX program = xelatex | 
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 2 | \documentclass{article}
 | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 3 | \usepackage{../style}
 | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 4 | \usepackage{../langs}
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 5 | \usepackage{../grammar}
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 6 | \usepackage{../graphics}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 7 | |
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 8 | |
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 9 | \begin{document}
 | 
| 668 | 10 | \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019}
 | 
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 11 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 12 | \section*{Handout 7 (Compilation)}
 | 
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 13 | |
| 690 | 14 | |
| 15 | ||
| 668 | 16 | The purpose of a compiler is to transform a program a human can read and | 
| 17 | write into code the machine can run as fast as possible. The fastest | |
| 18 | code would be machine code the CPU can run directly, but it is often | |
| 690 | 19 | good enough for improving the speed of a program to target a | 
| 668 | 20 | virtual machine. This produces not the fastest possible code, but code | 
| 690 | 21 | that is often pretty fast. This way of producing code has the advantage that | 
| 668 | 22 | the virtual machine takes care of things a compiler would normally need | 
| 690 | 23 | to take care of (like explicit memory management). | 
| 452 
0b707b614dac
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 24 | |
| 
0b707b614dac
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 25 | As a first example in this module we will implement a compiler for the | 
| 
0b707b614dac
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 26 | very simple While-language. It will generate code for the Java Virtual | 
| 690 | 27 | Machine (JVM). Unfortunately the Java ecosystem does not come with an | 
| 28 | assembler which would be handy for our compiler-endeavour (unlike | |
| 29 | Microsoft's Common Language Infrastructure for the .Net platform which | |
| 30 | has an assembler out-of-the-box). As a substitute we use in this module | |
| 31 | the 3rd-party programs Jasmin and Krakatau | |
| 32 | ||
| 33 | \begin{itemize}
 | |
| 34 |   \item \url{http://jasmin.sourceforge.net}
 | |
| 35 |   \item \url{https://github.com/Storyyeller/Krakatau}
 | |
| 36 | \end{itemize}
 | |
| 37 | ||
| 38 | \noindent | |
| 39 | The first is a Java program and the second a program written in Python. | |
| 40 | Each of them allow us to generate \emph{assembly} files that are still
 | |
| 41 | readable by humans, as opposed to class-files which are pretty much just | |
| 42 | (horrible) zeros and ones. Jasmin (respectively Krakatau) will then take | |
| 43 | an assembly file as input and generate the corresponding class file for | |
| 44 | us. | |
| 45 | ||
| 46 | Good about the JVM is that it is a stack-based virtual machine, a fact | |
| 47 | which will make it easy to generate code for arithmetic expressions. For | |
| 48 | example when compiling the expression $1 + 2$ we need to generate the | |
| 49 | following three instructions | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 50 | |
| 668 | 51 | \begin{lstlisting}[language=JVMIS,numbers=none]
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 52 | ldc 1 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 53 | ldc 2 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 54 | iadd | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 55 | \end{lstlisting}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 56 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 57 | \noindent The first instruction loads the constant $1$ onto | 
| 668 | 58 | the stack, the next one loads $2$, the third instruction adds both | 
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 59 | numbers together replacing the top two elements of the stack | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 60 | with the result $3$. For simplicity, we will throughout | 
| 692 | 61 | consider only integer numbers. Therefore we can | 
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 62 | use the JVM instructions \code{iadd}, \code{isub},
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 63 | \code{imul}, \code{idiv} and so on. The \code{i} stands for
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 64 | integer instructions in the JVM (alternatives are \code{d} for
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 65 | doubles, \code{l} for longs and \code{f} for floats).
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 66 | |
| 600 | 67 | Recall our grammar for arithmetic expressions (\meta{E} is the
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 68 | starting symbol): | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 69 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 70 | |
| 601 | 71 | \begin{plstx}[rhs style=, margin=3cm]
 | 
| 72 | : \meta{E} ::= \meta{T} $+$ \meta{E}
 | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 73 |          | \meta{T} $-$ \meta{E}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 74 |          | \meta{T}\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 75 | : \meta{T} ::= \meta{F} $*$ \meta{T}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 76 |           | \meta{F} $\backslash$ \meta{T}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 77 |           | \meta{F}\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 78 | : \meta{F} ::= ( \meta{E} )
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 79 |           | \meta{Id}
 | 
| 601 | 80 |           | \meta{Num}\\
 | 
| 81 | \end{plstx}
 | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 82 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 83 | |
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 84 | \noindent where \meta{Id} stands for variables and \meta{Num}
 | 
| 668 | 85 | for numbers. For the moment let us omit variables from arithmetic | 
| 86 | expressions. Our parser will take this grammar and given an input | |
| 87 | produce abstract syntax trees. For example we will obtain for the | |
| 88 | expression $1 + ((2 * 3) + (4 - 3))$ the following tree. | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 89 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 90 | \begin{center}
 | 
| 601 | 91 | \begin{tikzpicture}
 | 
| 92 | \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]] | |
| 93 | \end{tikzpicture}
 | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 94 | \end{center}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 95 | |
| 668 | 96 | \noindent To generate JVM code for this expression, we need to | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 97 | traverse this tree in post-order fashion and emit code for | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 98 | each node---this traversal in post-order fashion will produce | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 99 | code for a stack-machine (what the JVM is). Doing so for the | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 100 | tree above generates the instructions | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 101 | |
| 668 | 102 | \begin{lstlisting}[language=JVMIS,numbers=none]
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 103 | ldc 1 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 104 | ldc 2 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 105 | ldc 3 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 106 | imul | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 107 | ldc 4 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 108 | ldc 3 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 109 | isub | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 110 | iadd | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 111 | iadd | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 112 | \end{lstlisting}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 113 | |
| 668 | 114 | \noindent If we ``run'' these instructions, the result $8$ will be on | 
| 115 | top of the stack (I leave this to you to verify; the meaning of each | |
| 116 | instruction should be clear). The result being on the top of the stack | |
| 690 | 117 | will be an important convention we always observe in our compiler. Note, | 
| 118 | that a different bracketing of the expression, for example $(1 + (2 * | |
| 119 | 3)) + (4 - 3)$, produces a different abstract syntax tree and thus also | |
| 120 | a different list of instructions. Generating code in this | |
| 668 | 121 | post-order-traversal fashion is rather easy to implement: it can be done | 
| 122 | with the following recursive \textit{compile}-function, which takes the
 | |
| 123 | abstract syntax tree as argument: | |
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 124 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 125 | \begin{center}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 126 | \begin{tabular}{lcl}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 127 | $\textit{compile}(n)$ & $\dn$ & $\pcode{ldc}\; n$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 128 | $\textit{compile}(a_1 + a_2)$ & $\dn$ &
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 129 | $\textit{compile}(a_1) \;@\;\textit{compile}(a_2)\;@\; \pcode{iadd}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 130 | $\textit{compile}(a_1 - a_2)$ & $\dn$ & 
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 131 | $\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{isub}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 132 | $\textit{compile}(a_1 * a_2)$ & $\dn$ & 
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 133 | $\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{imul}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 134 | $\textit{compile}(a_1 \backslash a_2)$ & $\dn$ & 
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 135 | $\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{idiv}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 136 | \end{tabular}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 137 | \end{center}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 138 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 139 | However, our arithmetic expressions can also contain | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 140 | variables. We will represent them as \emph{local variables} in
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 141 | the JVM. Essentially, local variables are an array or pointers | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 142 | to memory cells, containing in our case only integers. Looking | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 143 | up a variable can be done with the instruction | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 144 | |
| 668 | 145 | \begin{lstlisting}[language=JVMIS,mathescape,numbers=none]
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 146 | iload $index$ | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 147 | \end{lstlisting}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 148 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 149 | \noindent | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 150 | which places the content of the local variable $index$ onto | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 151 | the stack. Storing the top of the stack into a local variable | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 152 | can be done by the instruction | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 153 | |
| 668 | 154 | \begin{lstlisting}[language=JVMIS,mathescape,numbers=none]
 | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 155 | istore $index$ | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 156 | \end{lstlisting}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 157 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 158 | \noindent Note that this also pops off the top of the stack. | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 159 | One problem we have to overcome, however, is that local | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 160 | variables are addressed, not by identifiers, but by numbers | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 161 | (starting from $0$). Therefore our compiler needs to maintain | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 162 | a kind of environment where variables are associated to | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 163 | numbers. This association needs to be unique: if we muddle up | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 164 | the numbers, then we essentially confuse variables and the | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 165 | consequence will usually be an erroneous result. Our extended | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 166 | \textit{compile}-function for arithmetic expressions will
 | 
| 692 | 167 | therefore take two arguments: the abstract syntax tree and an | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 168 | environment, $E$, that maps identifiers to index-numbers. | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 169 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 170 | \begin{center}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 171 | \begin{tabular}{lcl}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 172 | $\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 173 | $\textit{compile}(a_1 + a_2, E)$ & $\dn$ & 
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 174 | $\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 175 | $\textit{compile}(a_1 - a_2, E)$ & $\dn$ &
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 176 | $\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 177 | $\textit{compile}(a_1 * a_2, E)$ & $\dn$ &
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 178 | $\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 179 | $\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$ & 
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 180 | $\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 181 | $\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 182 | \end{tabular}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 183 | \end{center}
 | 
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 184 | |
| 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 185 | \noindent In the last line we generate the code for variables | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 186 | where $E(x)$ stands for looking up the environment to which | 
| 690 | 187 | index the variable $x$ maps to. This is similar to an interpreter, | 
| 188 | which also needs an environment: the difference is that the | |
| 189 | interpreter maintains a mapping from variables to current values (what is the | |
| 190 | currently the value of a variable), while compilers need a mapping | |
| 191 | from variables to memory locations (where can I find the current | |
| 192 | value for the variable in memory). | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 193 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 194 | There is a similar \textit{compile}-function for boolean
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 195 | expressions, but it includes a ``trick'' to do with | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 196 | \pcode{if}- and \pcode{while}-statements. To explain the issue
 | 
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 197 | let us first describe the compilation of statements of the | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 198 | While-language. The clause for \pcode{skip} is trivial, since
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 199 | we do not have to generate any instruction | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 200 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 201 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 202 | \begin{tabular}{lcl}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 203 | $\textit{compile}(\pcode{skip}, E)$ & $\dn$ & $([], E)$\\
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 204 | \end{tabular}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 205 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 206 | |
| 668 | 207 | \noindent whereby $[]$ is the empty list of instructions. Note that | 
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 208 | the \textit{compile}-function for statements returns a pair, a
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 209 | list of instructions (in this case the empty list) and an | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 210 | environment for variables. The reason for the environment is | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 211 | that assignments in the While-language might change the | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 212 | environment---clearly if a variable is used for the first | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 213 | time, we need to allocate a new index and if it has been used | 
| 690 | 214 | before, then we need to be able to retrieve the associated index. | 
| 215 | This is reflected in the clause for compiling assignments, say | |
| 216 | $\textit{x := a}$:
 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 217 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 218 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 219 | \begin{tabular}{lcl}
 | 
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 220 | $\textit{compile}(x := a, E)$ & $\dn$ & 
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 221 | $(\textit{compile}(a, E) \;@\;\pcode{istore}\;index, E')$
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 222 | \end{tabular}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 223 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 224 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 225 | \noindent We first generate code for the right-hand side of | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 226 | the assignment and then add an \pcode{istore}-instruction at
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 227 | the end. By convention the result of the arithmetic expression | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 228 | $a$ will be on top of the stack. After the \pcode{istore}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 229 | instruction, the result will be stored in the index | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 230 | corresponding to the variable $x$. If the variable $x$ has | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 231 | been used before in the program, we just need to look up what | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 232 | the index is and return the environment unchanged (that is in | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 233 | this case $E' = E$). However, if this is the first encounter | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 234 | of the variable $x$ in the program, then we have to augment | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 235 | the environment and assign $x$ with the largest index in $E$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 236 | plus one (that is $E' = E(x \mapsto largest\_index + 1)$). | 
| 692 | 237 | To sum up, for the assignment $x := x + 1$ we generate the | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 238 | following code | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 239 | |
| 668 | 240 | \begin{lstlisting}[language=JVMIS,mathescape,numbers=none]
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 241 | iload $n_x$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 242 | ldc 1 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 243 | iadd | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 244 | istore $n_x$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 245 | \end{lstlisting}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 246 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 247 | \noindent | 
| 692 | 248 | where $n_x$ is the index (or pointer to the memory) for the variable | 
| 249 | $x$. The code for looking-up the index for the variable is as follow: | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 250 | |
| 668 | 251 | \begin{center}
 | 
| 252 | \begin{tabular}{lcl}
 | |
| 690 | 253 | $index \;=\; E\textit{.getOrElse}(x, |E|)$
 | 
| 668 | 254 | \end{tabular}
 | 
| 255 | \end{center}
 | |
| 256 | ||
| 257 | \noindent | |
| 258 | In case the environment $E$ contains an index for $x$, we return it. | |
| 259 | Otherwise we ``create'' a new index by returning the size $|E|$ of the | |
| 260 | environment (that will be an index that is guaranteed to be not used | |
| 692 | 261 | yet). In all this we take advantage of the JVM which provides us with | 
| 262 | a potentially limitless supply of places where we can store values | |
| 263 | of variables. | |
| 668 | 264 | |
| 692 | 265 | A bit more complicated is the generation of code for | 
| 266 | \pcode{if}-statements, say
 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 267 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 268 | \begin{lstlisting}[mathescape,language={},numbers=none]
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 269 | if $b$ then $cs_1$ else $cs_2$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 270 | \end{lstlisting}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 271 | |
| 692 | 272 | \noindent where $b$ is a boolean expression and where both $cs_{1/2}$
 | 
| 273 | are the statements for each of the \pcode{if}-branches. Lets assume we
 | |
| 274 | already generated code for $b$ and $cs_{1/2}$. Then in the true-case the
 | |
| 275 | control-flow of the program needs to behave as | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 276 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 277 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 278 | \begin{tikzpicture}[node distance=2mm and 4mm,
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 279 |  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 280 |  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 281 |  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 282 | \node (A1) [point] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 283 | \node (b) [block, right=of A1] {code of $b$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 284 | \node (A2) [point, right=of b] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 285 | \node (cs1) [block, right=of A2] {code of $cs_1$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 286 | \node (A3) [point, right=of cs1] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 287 | \node (cs2) [block, right=of A3] {code of $cs_2$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 288 | \node (A4) [point, right=of cs2] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 289 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 290 | \draw (A1) edge [->, black, line width=1mm] (b); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 291 | \draw (b) edge [->, black, line width=1mm] (cs1); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 292 | \draw (cs1) edge [->, black, line width=1mm] (A3); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 293 | \draw (A3) edge [->, black, skip loop] (A4); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 294 | \node [below=of cs2] {\raisebox{-5mm}{\small{}jump}};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 295 | \end{tikzpicture}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 296 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 297 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 298 | \noindent where we start with running the code for $b$; since | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 299 | we are in the true case we continue with running the code for | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 300 | $cs_1$. After this however, we must not run the code for | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 301 | $cs_2$, but always jump after the last instruction of $cs_2$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 302 | (the code for the \pcode{else}-branch). Note that this jump is
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 303 | unconditional, meaning we always have to jump to the end of | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 304 | $cs_2$. The corresponding instruction of the JVM is | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 305 | \pcode{goto}. In case $b$ turns out to be false we need the
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 306 | control-flow | 
| 370 
a65767fe5d71
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
369diff
changeset | 307 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 308 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 309 | \begin{tikzpicture}[node distance=2mm and 4mm,
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 310 |  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 311 |  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 312 |  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 313 | \node (A1) [point] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 314 | \node (b) [block, right=of A1] {code of $b$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 315 | \node (A2) [point, right=of b] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 316 | \node (cs1) [block, right=of A2] {code of $cs_1$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 317 | \node (A3) [point, right=of cs1] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 318 | \node (cs2) [block, right=of A3] {code of $cs_2$};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 319 | \node (A4) [point, right=of cs2] {};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 320 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 321 | \draw (A1) edge [->, black, line width=1mm] (b); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 322 | \draw (b) edge [->, black, line width=1mm] (A2); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 323 | \draw (A2) edge [skip loop] (A3); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 324 | \draw (A3) edge [->, black, line width=1mm] (cs2); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 325 | \draw (cs2) edge [->,black, line width=1mm] (A4); | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 326 | \node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}};
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 327 | \end{tikzpicture}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 328 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 329 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 330 | \noindent where we now need a conditional jump (if the | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 331 | if-condition is false) from the end of the code for the | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 332 | boolean to the beginning of the instructions $cs_2$. Once we | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 333 | are finished with running $cs_2$ we can continue with whatever | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 334 | code comes after the if-statement. | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 335 | |
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 336 | The \pcode{goto} and the conditional jumps need addresses to
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 337 | where the jump should go. Since we are generating assembly | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 338 | code for the JVM, we do not actually have to give (numeric) | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 339 | addresses, but can just attach (symbolic) labels to our code. | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 340 | These labels specify a target for a jump. Therefore the labels | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 341 | need to be unique, as otherwise it would be ambiguous where a | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 342 | jump should go to. A label, say \pcode{L}, is attached to code
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 343 | like | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 344 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 345 | \begin{lstlisting}[mathescape,numbers=none]
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 346 | L: | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 347 | $instr_1$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 348 | $instr_2$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 349 | $\vdots$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 350 | \end{lstlisting}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 351 | |
| 692 | 352 | \noindent where a label is indicated by a colon. The task of the | 
| 353 | assmbler (in our case Jasmin or Krakatau) is to resolve the labels | |
| 354 | to actual addresses, for example jump 10 instructions forward, | |
| 355 | or 20 instructions backwards. | |
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 356 | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 357 | Recall the ``trick'' with compiling boolean expressions: the | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 358 | \textit{compile}-function for boolean expressions takes three
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 359 | arguments: an abstract syntax tree, an environment for | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 360 | variable indices and also the label, $lab$, to where an conditional | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 361 | jump needs to go. The clause for the expression $a_1 = a_2$, | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 362 | for example, is as follows: | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 363 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 364 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 365 | \begin{tabular}{lcl}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 366 | $\textit{compile}(a_1 = a_2, E, lab)$ & $\dn$\\ 
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 367 | \multicolumn{3}{l}{$\qquad\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{if_icmpne}\;lab$}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 368 | \end{tabular}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 369 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 370 | |
| 376 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 371 | \noindent where we are first generating code for the | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 372 | subexpressions $a_1$ and $a_2$. This will mean after running | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 373 | the corresponding code there will be two integers on top of | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 374 | the stack. If they are equal, we do not have to do anything | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 375 | (except for popping them off from the stack) and just continue | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 376 | with the next instructions (see control-flow of ifs above). | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 377 | However if they are \emph{not} equal, then we need to
 | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 378 | (conditionally) jump to the label $lab$. This can be done with | 
| 
af65ffff9cdd
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
375diff
changeset | 379 | the instruction | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 380 | |
| 692 | 381 | \begin{lstlisting}[mathescape,numbers=none,language=JVMIS]
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 382 | if_icmpne $lab$ | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 383 | \end{lstlisting}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 384 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 385 | \noindent Other jump instructions for boolean operators are | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 386 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 387 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 388 | \begin{tabular}{l@{\hspace{10mm}}c@{\hspace{10mm}}l}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 389 | $\not=$ & $\Rightarrow$ & \pcode{if_icmpeq}\\
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 390 | $<$ & $\Rightarrow$ & \pcode{if_icmpge}\\
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 391 | $\le$ & $\Rightarrow$ & \pcode{if_icmpgt}\\
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 392 | \end{tabular}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 393 | \end{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 394 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 395 | \noindent and so on. I leave it to you to extend the | 
| 692 | 396 | \textit{compile}-function for the other boolean expressions. Note that
 | 
| 397 | we need to jump whenever the boolean is \emph{not} true, which means we
 | |
| 398 | have to ``negate'' the jump condition---equals becomes not-equal, less | |
| 399 | becomes greater-or-equal. If you do not like this design (it can be the | |
| 400 | source of some nasty, hard-to-detect errors), you can also change the | |
| 401 | layout of the code and first give the code for the else-branch and then | |
| 402 | for the if-branch. However in the case of while-loops this | |
| 403 | ``upside-down-inside-out'' way of generating code still seems the most | |
| 404 | convenient. | |
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 405 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 406 | We are now ready to give the compile function for | 
| 601 | 407 | if-statements---remember this function returns for statements a | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 408 | pair consisting of the code and an environment: | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 409 | |
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 410 | \begin{center}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 411 | \begin{tabular}{lcl}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 412 | $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 413 | \multicolumn{3}{l}{$\qquad L_\textit{ifelse}\;$ (fresh label)}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 414 | \multicolumn{3}{l}{$\qquad L_\textit{ifend}\;$ (fresh label)}\\
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 415 | \multicolumn{3}{l}{$\qquad (is_1, E') = \textit{compile}(cs_1, E)$}\\
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 416 | \multicolumn{3}{l}{$\qquad (is_2, E'') = \textit{compile}(cs_2, E')$}\\
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 417 | \multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, L_\textit{ifelse})$}\\
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 418 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 419 | \multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;L_\textit{ifend}$}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 420 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifelse}:$}\\
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 421 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 422 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifend}:, E'')$}\\
 | 
| 372 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 423 | \end{tabular}
 | 
| 
d6af4b1239de
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
370diff
changeset | 424 | \end{center}
 | 
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 425 | |
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 426 | \noindent In the first two lines we generate two fresh labels | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 427 | for the jump addresses (just before the else-branch and just | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 428 | after). In the next two lines we generate the instructions for | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 429 | the two branches, $is_1$ and $is_2$. The final code will | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 430 | be first the code for $b$ (including the label | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 431 | just-before-the-else-branch), then the \pcode{goto} for after
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 432 | the else-branch, the label $L_\textit{ifesle}$, followed by
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 433 | the instructions for the else-branch, followed by the | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 434 | after-the-else-branch label. Consider for example the | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 435 | if-statement: | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 436 | |
| 690 | 437 | \begin{lstlisting}[mathescape,numbers=none,language=While]
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 438 | if 1 = 1 then x := 2 else y := 3 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 439 | \end{lstlisting}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 440 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 441 | \noindent | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 442 | The generated code is as follows: | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 443 | |
| 690 | 444 | \begin{lstlisting}[language=JVMIS,mathescape,numbers=left]
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 445 | ldc 1 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 446 | ldc 1 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 447 |    if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 448 | ldc 2 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 449 | istore 0 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 450 |    goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 451 | L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 452 | ldc 3 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 453 | istore 1 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 454 | L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 455 | \end{lstlisting}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 456 | |
| 601 | 457 | \begin{tikzpicture}[remember picture,overlay]
 | 
| 458 |   \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) 
 | |
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 459 | -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 460 |   \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) 
 | 
| 601 | 461 | -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); | 
| 462 | \end{tikzpicture}
 | |
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 463 | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 464 | \noindent The first three lines correspond to the the boolean | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 465 | expression $1 = 1$. The jump for when this boolean expression | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 466 | is false is in Line~3. Lines 4-6 corresponds to the if-branch; | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 467 | the else-branch is in Lines 8 and 9. Note carefully how the | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 468 | environment $E$ is threaded through the recursive calls of | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 469 | \textit{compile}. The function receives an environment $E$,
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 470 | but it might extend it when compiling the if-branch, yielding | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 471 | $E'$. This happens for example in the if-statement above | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 472 | whenever the variable \code{x} has not been used before.
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 473 | Similarly with the environment $E''$ for the second call to | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 474 | \textit{compile}. $E''$ is also the environment that needs to
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 475 | be returned as part of the answer. | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 476 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 477 | The compilation of the while-loops, say | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 478 | \pcode{while} $b$ \pcode{do} $cs$, is very similar. In case
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 479 | the condition is true and we need to do another iteration, | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 480 | and the control-flow needs to be as follows | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 481 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 482 | \begin{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 483 | \begin{tikzpicture}[node distance=2mm and 4mm,
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 484 |  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 485 |  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 486 |  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 487 | \node (A0) [point, left=of A1] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 488 | \node (A1) [point] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 489 | \node (b) [block, right=of A1] {code of $b$};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 490 | \node (A2) [point, right=of b] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 491 | \node (cs1) [block, right=of A2] {code of $cs$};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 492 | \node (A3) [point, right=of cs1] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 493 | \node (A4) [point, right=of A3] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 494 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 495 | \draw (A0) edge [->, black, line width=1mm] (b); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 496 | \draw (b) edge [->, black, line width=1mm] (cs1); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 497 | \draw (cs1) edge [->, black, line width=1mm] (A3); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 498 | \draw (A3) edge [->,skip loop] (A1); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 499 | \end{tikzpicture}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 500 | \end{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 501 | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 502 | \noindent Whereas if the condition is \emph{not} true, we
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 503 | need to jump out of the loop, which gives the following | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 504 | control flow. | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 505 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 506 | \begin{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 507 | \begin{tikzpicture}[node distance=2mm and 4mm,
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 508 |  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 509 |  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 510 |  skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 511 | \node (A0) [point, left=of A1] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 512 | \node (A1) [point] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 513 | \node (b) [block, right=of A1] {code of $b$};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 514 | \node (A2) [point, right=of b] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 515 | \node (cs1) [block, right=of A2] {code of $cs$};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 516 | \node (A3) [point, right=of cs1] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 517 | \node (A4) [point, right=of A3] {};
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 518 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 519 | \draw (A0) edge [->, black, line width=1mm] (b); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 520 | \draw (b) edge [->, black, line width=1mm] (A2); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 521 | \draw (A2) edge [skip loop] (A3); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 522 | \draw (A3) edge [->, black, line width=1mm] (A4); | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 523 | \end{tikzpicture}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 524 | \end{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 525 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 526 | \noindent Again we can use the \textit{compile}-function for
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 527 | boolean expressions to insert the appropriate jump to the | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 528 | end of the loop (label $L_{wend}$ below).
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 529 | |
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 530 | \begin{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 531 | \begin{tabular}{lcl}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 532 | $\textit{compile}(\pcode{while}\; b\; \pcode{do} \;cs, E)$ & $\dn$\\ 
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 533 | \multicolumn{3}{l}{$\qquad L_{wbegin}\;$ (fresh label)}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 534 | \multicolumn{3}{l}{$\qquad L_{wend}\;$ (fresh label)}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 535 | \multicolumn{3}{l}{$\qquad (is, E') = \textit{compile}(cs_1, E)$}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 536 | \multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 537 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;\textit{compile}(b, E, L_{wend})$}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 538 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 539 | \multicolumn{3}{l}{$\qquad\phantom{(}@\; \text{goto}\;L_{wbegin}$}\\
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 540 | \multicolumn{3}{l}{$\qquad\phantom{(}@\;L_{wend}:, E')$}\\
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 541 | \end{tabular}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 542 | \end{center}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 543 | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 544 | \noindent I let you go through how this clause works. As an example | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 545 | you can consider the while-loop | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 546 | |
| 690 | 547 | \begin{lstlisting}[mathescape,numbers=none,language=While]
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 548 | while x <= 10 do x := x + 1 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 549 | \end{lstlisting}
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 550 | |
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 551 | \noindent yielding the following code | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 552 | |
| 690 | 553 | \begin{lstlisting}[language=JVMIS,mathescape,numbers=left]
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 554 | L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 555 | iload 0 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 556 | ldc 10 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 557 |    if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 558 | iload 0 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 559 | ldc 1 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 560 | iadd | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 561 | istore 0 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 562 |    goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 563 | L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 564 | \end{lstlisting}
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 565 | |
| 601 | 566 | \begin{tikzpicture}[remember picture,overlay]
 | 
| 567 |   \draw[->,very thick] (LA) edge [->,to path={-- ++(10mm,0mm) 
 | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 568 | -- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east); | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 569 |   \draw[->,very thick] (LC) edge [->,to path={-- ++(10mm,0mm) 
 | 
| 601 | 570 | -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east); | 
| 571 | \end{tikzpicture}
 | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 572 | |
| 690 | 573 | \noindent | 
| 574 | I leave it to you to read the code and follow its controlflow. | |
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 575 | |
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 576 | Next we need to consider the statement \pcode{write x}, which
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 577 | can be used to print out the content of a variable. For this | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 578 | we need to use a Java library function. In order to avoid | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 579 | having to generate a lot of code for each | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 580 | \pcode{write}-command, we use a separate helper-method and
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 581 | just call this method with an argument (which needs to be | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 582 | placed onto the stack). The code of the helper-method is as | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 583 | follows. | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 584 | |
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 585 | |
| 690 | 586 | \begin{lstlisting}[language=JVMIS,numbers=left]
 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 587 | .method public static write(I)V | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 588 | .limit locals 1 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 589 | .limit stack 2 | 
| 373 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 590 | getstatic java/lang/System/out Ljava/io/PrintStream; | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 591 | iload 0 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 592 | invokevirtual java/io/PrintStream/println(I)V | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 593 | return | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 594 | .end method | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 595 | \end{lstlisting}
 | 
| 
b018234c9126
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
372diff
changeset | 596 | |
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 597 | \noindent The first line marks the beginning of the method, | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 598 | called \pcode{write}. It takes a single integer argument
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 599 | indicated by the \pcode{(I)} and returns no result, indicated
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 600 | by the \pcode{V}. Since the method has only one argument, we
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 601 | only need a single local variable (Line~2) and a stack with | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 602 | two cells will be sufficient (Line 3). Line 4 instructs the | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 603 | JVM to get the value of the field \pcode{out} of the class
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 604 | \pcode{java/lang/System}. It expects the value to be of type
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 605 | \pcode{java/io/PrintStream}. A reference to this value will be
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 606 | placed on the stack. Line~5 copies the integer we want to | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 607 | print out onto the stack. In the next line we call the method | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 608 | \pcode{println} (from the class \pcode{java/io/PrintStream}).
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 609 | We want to print out an integer and do not expect anything | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 610 | back (that is why the type annotation is \pcode{(I)V}). The
 | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 611 | \pcode{return}-instruction in the next line changes the
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 612 | control-flow back to the place from where \pcode{write} was
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 613 | called. This method needs to be part of a header that is | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 614 | included in any code we generate. The helper-method | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 615 | \pcode{write} can be invoked with the two instructions
 | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 616 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 617 | \begin{lstlisting}[mathescape,language=JVMIS]
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 618 | iload $E(x)$ | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 619 | invokestatic XXX/XXX/write(I)V | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 620 | \end{lstlisting}
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 621 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 622 | \noindent where we first place the variable to be printed on | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 623 | top of the stack and then call \pcode{write}. The \pcode{XXX}
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 624 | need to be replaced by an appropriate class name (this will be | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 625 | explained shortly). | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 626 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 627 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 628 | \begin{figure}[t]
 | 
| 690 | 629 | \begin{lstlisting}[mathescape,language=JVMIS,numbers=left]
 | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 630 | .class public XXX.XXX | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 631 | .super java/lang/Object | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 632 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 633 | .method public <init>()V | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 634 | aload_0 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 635 | invokenonvirtual java/lang/Object/<init>()V | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 636 | return | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 637 | .end method | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 638 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 639 | .method public static main([Ljava/lang/String;)V | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 640 | .limit locals 200 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 641 | .limit stack 200 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 642 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 643 |       $\textit{\ldots{}here comes the compiled code\ldots}$
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 644 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 645 | return | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 646 | .end method | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 647 | \end{lstlisting}
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 648 | \caption{Boilerplate code needed for running generated code.\label{boiler}}
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 649 | \end{figure}
 | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 650 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 651 | |
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 652 | By generating code for a While-program, we end up with a list | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 653 | of (JVM assembly) instructions. Unfortunately, there is a bit | 
| 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 654 | more boilerplate code needed before these instructions can be | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 655 | run. The complete code is shown in Figure~\ref{boiler}. This
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 656 | boilerplate code is very specific to the JVM. If we target any | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 657 | other virtual machine or a machine language, then we would | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 658 | need to change this code. Lines 4 to 8 in Figure~\ref{boiler}
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 659 | contain a method for object creation in the JVM; this method | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 660 | is called \emph{before} the \pcode{main}-method in Lines 10 to
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 661 | 17. Interesting are the Lines 11 and 12 where we hardwire that | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 662 | the stack of our programs will never be larger than 200 and | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 663 | that the maximum number of variables is also 200. This seem to | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 664 | be conservative default values that allow is to run some | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 665 | simple While-programs. In a real compiler, we would of course | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 666 | need to work harder and find out appropriate values for the | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 667 | stack and local variables. | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 668 | |
| 375 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 669 | To sum up, in Figure~\ref{test} is the complete code generated
 | 
| 601 | 670 | for the slightly nonsensical program | 
| 375 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 671 | |
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 672 | \begin{lstlisting}[mathescape,language=While]
 | 
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 673 | x := 1 + 2; | 
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 674 | write x | 
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 675 | \end{lstlisting}
 | 
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 676 | |
| 692 | 677 | \noindent I let you read the code and make sure the code behaves as | 
| 678 | expected. Having this code at our disposal, we need the assembler to | |
| 679 | translate the generated code into JVM bytecode (a class file). This | |
| 680 | bytecode is then understood by the JVM and can be run by just invoking | |
| 681 | the \pcode{java}-program.
 | |
| 375 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 682 | |
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 683 | |
| 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 684 | \begin{figure}[p]
 | 
| 383 
a6a6bf32fade
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
377diff
changeset | 685 | \lstinputlisting[language=JVMIS]{../progs/test-small.j}
 | 
| 377 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 686 | \caption{Generated code for a test program. This code can be 
 | 
| 
a052a83f562e
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
376diff
changeset | 687 | processed by an Java assembler producing a class-file, which | 
| 394 
2f9fe225ecc8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
383diff
changeset | 688 | can be run by the {\tt{}java}-program.\label{test}}
 | 
| 375 
bf36664a3196
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
374diff
changeset | 689 | \end{figure}
 | 
| 374 
0e25fb72d339
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
373diff
changeset | 690 | |
| 690 | 691 | \subsection*{Arrays}
 | 
| 692 | ||
| 691 | 693 | Maybe a useful addition to the While-language would be arrays. This | 
| 694 | would let us generate more interesting While-programs by translating | |
| 695 | BF*** programs into equivalent While-code. So in this section lets have | |
| 696 | a look at how we can support the following three constructions | |
| 690 | 697 | |
| 698 | \begin{lstlisting}[mathescape,language=While]
 | |
| 699 | new arr[15000] | |
| 700 | x := 3 + arr[3 + y] | |
| 701 | arr[42 * n] := ... | |
| 702 | \end{lstlisting}
 | |
| 703 | ||
| 704 | \noindent | |
| 691 | 705 | The first construct is for creating new arrays, in this instance the | 
| 706 | name of the array is \pcode{arr} and it can hold 15000 integers. The
 | |
| 707 | second is for referencing an array cell inside an arithmetic | |
| 708 | expression---we need to be able to look up the contents of an array at | |
| 692 | 709 | an index determined by an arithmetic expression. Similarly in the line | 
| 710 | below, we need to be able to update the content of an array at an | |
| 711 | calculated index. | |
| 691 | 712 | |
| 713 | For creating a new array we can generate the following three JVM | |
| 714 | instructions: | |
| 690 | 715 | |
| 716 | \begin{lstlisting}[mathescape,language=JVMIS]
 | |
| 717 | ldc number | |
| 718 | newarray int | |
| 719 | astore loc_var | |
| 720 | \end{lstlisting}
 | |
| 721 | ||
| 722 | \noindent | |
| 691 | 723 | First we need to put the dimension of the array onto the stack. The next | 
| 724 | instruction creates the array. With the last we can store the array as a | |
| 725 | local variable (like the ``simple'' variables from the previous | |
| 692 | 726 | section). The use of a local variable for each array allows us to have | 
| 727 | multiple arrays in a While-program. For looking up an element in an | |
| 728 | array we can use the following JVM code | |
| 690 | 729 | |
| 730 | \begin{lstlisting}[mathescape,language=JVMIS]
 | |
| 731 | aload loc_var | |
| 732 | index_aexp | |
| 733 | iaload | |
| 734 | \end{lstlisting}
 | |
| 735 | ||
| 736 | \noindent | |
| 691 | 737 | The first instruction loads the ``pointer'' to the array onto the stack. | 
| 738 | Then we have some instructions corresponding to the index where we want | |
| 739 | to look up the array. The idea is that these instructions will leave a | |
| 740 | concrete number on the stack, which will be the index into the array we | |
| 741 | need. Finally we need to tell the JVM to load the corresponding element | |
| 742 | onto the stack. Updating an array at an index with a value is as | |
| 743 | follows. | |
| 744 | ||
| 745 | \begin{lstlisting}[mathescape,language=JVMIS]
 | |
| 746 | aload loc_var | |
| 747 | index_aexp | |
| 748 | value_aexp | |
| 749 | iastore | |
| 750 | \end{lstlisting}
 | |
| 751 | ||
| 752 | \noindent | |
| 753 | Again the first instruction loads the ``pointer'' to the array onto the | |
| 754 | stack. Then we have some instructions corresponding to the index where | |
| 692 | 755 | we want to update the array. After that come the instructions for with | 
| 756 | what value we want to update the array. The last line contains the | |
| 757 | instruction for updating the array. | |
| 691 | 758 | |
| 692 | 759 | Next we need to modify our grammar rules for our While-language: it | 
| 760 | seems best to extend the rule for factors in arithmetic expressions with | |
| 761 | a rule for looking up an array. | |
| 691 | 762 | |
| 763 | \begin{plstx}[rhs style=, margin=3cm]
 | |
| 764 | : \meta{E} ::= \meta{T} $+$ \meta{E}
 | |
| 765 |          | \meta{T} $-$ \meta{E}
 | |
| 766 |          | \meta{T}\\
 | |
| 767 | : \meta{T} ::= \meta{F} $*$ \meta{T}
 | |
| 768 |           | \meta{F} $\backslash$ \meta{T}
 | |
| 769 |           | \meta{F}\\
 | |
| 770 | : \meta{F} ::= ( \meta{E} )
 | |
| 771 |           | $\underbrace{\meta{Id}\,[\,\meta{E}\,]}_{new}$
 | |
| 772 |           | \meta{Id}
 | |
| 773 |           | \meta{Num}\\
 | |
| 774 | \end{plstx}
 | |
| 775 | ||
| 776 | \noindent | |
| 777 | There is no problem with left-recursion as the \meta{E} is ``protected''
 | |
| 692 | 778 | by an identifier and the brackets. There are two new rules for statements, | 
| 779 | one for creating an array and one for array assignment: | |
| 691 | 780 | |
| 781 | \begin{plstx}[rhs style=, margin=2cm, one per line]
 | |
| 782 | : \meta{Stmt} ::=  \ldots
 | |
| 783 |               | \texttt{new}\; \meta{Id}\,[\,\meta{Num}\,] 
 | |
| 784 |               | \meta{Id}\,[\,\meta{E}\,]\,:=\,\meta{E}\\
 | |
| 785 | \end{plstx}
 | |
| 690 | 786 | |
| 692 | 787 | With this in place we can turn back to the idea of creating While | 
| 788 | programs by translating BF programs. This is a relatively easy task | |
| 789 | because BF only has eight instructions (we will actually only have seven | |
| 790 | because we can omit the read-in instruction from BF). But also translating | |
| 791 | BF-loops is going to be easy since they straightforwardly can be | |
| 792 | represented by While-loops. The Scala code for the translation is | |
| 793 | as follows: | |
| 794 | ||
| 795 | \begin{lstlisting}[language=Scala,numbers=left]
 | |
| 796 | def instr(c: Char) : String = c match {
 | |
| 797 | case '>' => "ptr := ptr + 1;" | |
| 798 | case '<' => "ptr := ptr - 1;" | |
| 799 | case '+' => "field[ptr] := field[ptr] + 1;" | |
| 800 | case '-' => "field[ptr] := field[ptr] - 1;" | |
| 801 | case '.' => "x := field[ptr]; write x;" | |
| 802 |   case '['  => "while (field[ptr] != 0) do {"
 | |
| 803 | case ']' => "skip};" | |
| 804 | case _ => "" | |
| 805 | } | |
| 806 | \end{lstlisting}
 | |
| 807 | ||
| 808 | \noindent | |
| 809 | The idea behind the translation is that BF-programs operate on an array, | |
| 810 | called \texttt{field}. The BP-memory pointer into this array is
 | |
| 811 | represented as the variable \texttt{ptr}. The BF-instructions \code{>}
 | |
| 812 | and \code{<} increase, respectively decrease, \texttt{ptr}. The
 | |
| 813 | instructions \code{+} and \code{-} update a cell in \texttt{field}. In
 | |
| 814 | Line 6 we need to first assign a field-cell to an auxiliary variable | |
| 815 | since we have not changed our write functions in order to cope with | |
| 816 | writing out any array-content directly. Lines 7 and 8 are for | |
| 817 | translating BF-loops. Line 8 is interesting in the sense that we need to | |
| 818 | generate a \code{skip} instruction just before finishing with the 
 | |
| 819 | closing \code{"\}"}. The reason is that we are rather pedantic about
 | |
| 820 | semicolons in our While-grammar: the last command cannot have a | |
| 821 | semicolon---adding a \code{skip} works around this snag. Putting
 | |
| 822 | all this together is we can generate While-programs with more than | |
| 823 | 400 instructions and then run the compiled JVM code for such programs. | |
| 824 | ||
| 825 | ||
| 327 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 826 | \end{document}
 | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 827 | |
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 828 | %%% Local Variables: | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 829 | %%% mode: latex | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 830 | %%% TeX-master: t | 
| 
9470cd124667
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 831 | %%% End: |