author | Christian Urban <urbanc@in.tum.de> |
Fri, 15 Nov 2019 14:58:16 +0000 | |
changeset 690 | 8d57433c7b5e |
parent 668 | 9ce78065f68d |
child 691 | 991849dfbcb1 |
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:
369
diff
changeset
|
5 |
\usepackage{../grammar} |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
6 |
\usepackage{../graphics} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
370
diff
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
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
24 |
|
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
25 |
As a first example in this module we will implement a compiler for the |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
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:
369
diff
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:
369
diff
changeset
|
52 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
53 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
54 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
55 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
56 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
375
diff
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:
375
diff
changeset
|
60 |
with the result $3$. For simplicity, we will throughout |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
61 |
consider only integer numbers and results. Therefore we can |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
62 |
use the JVM instructions \code{iadd}, \code{isub}, |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
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:
375
diff
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:
375
diff
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:
369
diff
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:
370
diff
changeset
|
68 |
starting symbol): |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
69 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
73 |
| \meta{T} $-$ \meta{E} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
74 |
| \meta{T}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
75 |
: \meta{T} ::= \meta{F} $*$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
76 |
| \meta{F} $\backslash$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
77 |
| \meta{F}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
78 |
: \meta{F} ::= ( \meta{E} ) |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
82 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
83 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
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:
369
diff
changeset
|
89 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
94 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
changeset
|
100 |
tree above generates the instructions |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
103 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
104 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
105 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
106 |
imul |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
107 |
ldc 4 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
108 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
109 |
isub |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
110 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
111 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
112 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
124 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
125 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
126 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
127 |
$\textit{compile}(n)$ & $\dn$ & $\pcode{ldc}\; n$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
128 |
$\textit{compile}(a_1 + a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
130 |
$\textit{compile}(a_1 - a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
132 |
$\textit{compile}(a_1 * a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
134 |
$\textit{compile}(a_1 \backslash a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
136 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
137 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
138 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
139 |
However, our arithmetic expressions can also contain |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
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:
370
diff
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:
370
diff
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:
369
diff
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:
369
diff
changeset
|
146 |
iload $index$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
147 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
148 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
149 |
\noindent |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
370
diff
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:
369
diff
changeset
|
152 |
can be done by the instruction |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
155 |
istore $index$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
156 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
157 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
changeset
|
166 |
\textit{compile}-function for arithmetic expressions will |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
167 |
therefore take two arguments: the abstract syntax tree and the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
369
diff
changeset
|
169 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
170 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
171 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
172 |
$\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
173 |
$\textit{compile}(a_1 + a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
175 |
$\textit{compile}(a_1 - a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
changeset
|
177 |
$\textit{compile}(a_1 * a_2, E)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
369
diff
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:
369
diff
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:
369
diff
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:
369
diff
changeset
|
182 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
183 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
184 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
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:
370
diff
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:
370
diff
changeset
|
193 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
370
diff
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:
370
diff
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:
375
diff
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:
370
diff
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:
370
diff
changeset
|
199 |
we do not have to generate any instruction |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
200 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
201 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
202 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
203 |
$\textit{compile}(\pcode{skip}, E)$ & $\dn$ & $([], E)$\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
204 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
205 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
375
diff
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:
375
diff
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:
375
diff
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:
375
diff
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:
375
diff
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:
375
diff
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:
370
diff
changeset
|
217 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
218 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
219 |
\begin{tabular}{lcl} |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
220 |
$\textit{compile}(x := a, E)$ & $\dn$ & |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
221 |
$(\textit{compile}(a, E) \;@\;\pcode{istore}\;index, E')$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
222 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
223 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
224 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
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:
370
diff
changeset
|
236 |
plus one (that is $E' = E(x \mapsto largest\_index + 1)$). |
690 | 237 |
This means for the assignment $x := x + 1$ we generate the |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
238 |
following code |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
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:
370
diff
changeset
|
241 |
iload $n_x$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
242 |
ldc 1 |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
243 |
iadd |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
244 |
istore $n_x$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
245 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
246 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
247 |
\noindent |
690 | 248 |
where $n_x$ is the index (or pointer to the memory) for the variable $x$ . The code for |
668 | 249 |
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:
370
diff
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 |
|
261 |
yet). |
|
262 |
||
690 | 263 |
A bit more complicated is the generation of code for \pcode{if}-statements, say |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
264 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
265 |
\begin{lstlisting}[mathescape,language={},numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
266 |
if $b$ then $cs_1$ else $cs_2$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
267 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
268 |
|
668 | 269 |
\noindent where $b$ is a boolean expression and the $cs_{1/2}$ are the |
270 |
statements for each of the \pcode{if}-branches. Lets assume we already |
|
271 |
generated code for $b$ and $cs_{1/2}$. Then in the true-case the |
|
272 |
control-flow of the program needs to be |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
273 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
274 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
275 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
276 |
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:
370
diff
changeset
|
277 |
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:
370
diff
changeset
|
278 |
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:
370
diff
changeset
|
279 |
\node (A1) [point] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
280 |
\node (b) [block, right=of A1] {code of $b$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
281 |
\node (A2) [point, right=of b] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
282 |
\node (cs1) [block, right=of A2] {code of $cs_1$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
283 |
\node (A3) [point, right=of cs1] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
284 |
\node (cs2) [block, right=of A3] {code of $cs_2$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
285 |
\node (A4) [point, right=of cs2] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
286 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
287 |
\draw (A1) edge [->, black, line width=1mm] (b); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
288 |
\draw (b) edge [->, black, line width=1mm] (cs1); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
289 |
\draw (cs1) edge [->, black, line width=1mm] (A3); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
290 |
\draw (A3) edge [->, black, skip loop] (A4); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
291 |
\node [below=of cs2] {\raisebox{-5mm}{\small{}jump}}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
292 |
\end{tikzpicture} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
293 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
294 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
295 |
\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:
370
diff
changeset
|
296 |
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:
370
diff
changeset
|
297 |
$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:
370
diff
changeset
|
298 |
$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:
370
diff
changeset
|
299 |
(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:
370
diff
changeset
|
300 |
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:
370
diff
changeset
|
301 |
$cs_2$. The corresponding instruction of the JVM is |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
302 |
\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:
370
diff
changeset
|
303 |
control-flow |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
304 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
305 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
306 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
307 |
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:
370
diff
changeset
|
308 |
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:
370
diff
changeset
|
309 |
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:
370
diff
changeset
|
310 |
\node (A1) [point] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
311 |
\node (b) [block, right=of A1] {code of $b$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
312 |
\node (A2) [point, right=of b] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
313 |
\node (cs1) [block, right=of A2] {code of $cs_1$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
314 |
\node (A3) [point, right=of cs1] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
315 |
\node (cs2) [block, right=of A3] {code of $cs_2$}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
316 |
\node (A4) [point, right=of cs2] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
317 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
318 |
\draw (A1) edge [->, black, line width=1mm] (b); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
319 |
\draw (b) edge [->, black, line width=1mm] (A2); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
320 |
\draw (A2) edge [skip loop] (A3); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
321 |
\draw (A3) edge [->, black, line width=1mm] (cs2); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
322 |
\draw (cs2) edge [->,black, line width=1mm] (A4); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
323 |
\node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
324 |
\end{tikzpicture} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
325 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
326 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
327 |
\noindent where we now need a conditional jump (if the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
328 |
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:
370
diff
changeset
|
329 |
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:
370
diff
changeset
|
330 |
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:
370
diff
changeset
|
331 |
code comes after the if-statement. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
332 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
333 |
The \pcode{goto} and the conditional jumps need addresses to |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
334 |
where the jump should go. Since we are generating assembly |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
335 |
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:
375
diff
changeset
|
336 |
addresses, but can just attach (symbolic) labels to our code. |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
337 |
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:
375
diff
changeset
|
338 |
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:
375
diff
changeset
|
339 |
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:
375
diff
changeset
|
340 |
like |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
341 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
342 |
\begin{lstlisting}[mathescape,numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
343 |
L: |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
344 |
$instr_1$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
345 |
$instr_2$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
346 |
$\vdots$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
347 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
348 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
349 |
\noindent where a label is indicated by a colon. |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
350 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
351 |
Recall the ``trick'' with compiling boolean expressions: the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
352 |
\textit{compile}-function for boolean expressions takes three |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
353 |
arguments: an abstract syntax tree, an environment for |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
354 |
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:
370
diff
changeset
|
355 |
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:
370
diff
changeset
|
356 |
for example, is as follows: |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
357 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
358 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
359 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
360 |
$\textit{compile}(a_1 = a_2, E, lab)$ & $\dn$\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
361 |
\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:
370
diff
changeset
|
362 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
363 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
364 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
365 |
\noindent where we are first generating code for the |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
366 |
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:
375
diff
changeset
|
367 |
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:
375
diff
changeset
|
368 |
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:
375
diff
changeset
|
369 |
(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:
375
diff
changeset
|
370 |
with the next instructions (see control-flow of ifs above). |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
371 |
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:
375
diff
changeset
|
372 |
(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:
375
diff
changeset
|
373 |
the instruction |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
374 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
375 |
\begin{lstlisting}[mathescape,numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
376 |
if_icmpne $lab$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
377 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
378 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
379 |
\noindent Other jump instructions for boolean operators are |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
380 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
381 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
382 |
\begin{tabular}{l@{\hspace{10mm}}c@{\hspace{10mm}}l} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
383 |
$\not=$ & $\Rightarrow$ & \pcode{if_icmpeq}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
384 |
$<$ & $\Rightarrow$ & \pcode{if_icmpge}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
385 |
$\le$ & $\Rightarrow$ & \pcode{if_icmpgt}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
386 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
387 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
388 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
389 |
\noindent and so on. I leave it to you to extend the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
390 |
\textit{compile}-function for the other boolean expressions. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
391 |
Note that we need to jump whenever the boolean is \emph{not} |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
392 |
true, which means we have to ``negate'' the jump |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
393 |
condition---equals becomes not-equal, less becomes |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
394 |
greater-or-equal. If you do not like this design (it can be |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
395 |
the source of some nasty, hard-to-detect errors), you can also |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
396 |
change the layout of the code and first give the code for the |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
397 |
else-branch and then for the if-branch. However in the case |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
398 |
of while-loops this way of generating code still seems |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
399 |
the most convenient. |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
400 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
401 |
We are now ready to give the compile function for |
601 | 402 |
if-statements---remember this function returns for statements a |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
403 |
pair consisting of the code and an environment: |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
404 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
405 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
406 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
407 |
$\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:
372
diff
changeset
|
408 |
\multicolumn{3}{l}{$\qquad L_\textit{ifelse}\;$ (fresh label)}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
409 |
\multicolumn{3}{l}{$\qquad L_\textit{ifend}\;$ (fresh label)}\\ |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
410 |
\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:
370
diff
changeset
|
411 |
\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:
372
diff
changeset
|
412 |
\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:
370
diff
changeset
|
413 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\ |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
414 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;L_\textit{ifend}$}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
415 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifelse}:$}\\ |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
416 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\ |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
417 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifend}:, E'')$}\\ |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
418 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
419 |
\end{center} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
420 |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
421 |
\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:
372
diff
changeset
|
422 |
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:
372
diff
changeset
|
423 |
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:
372
diff
changeset
|
424 |
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:
372
diff
changeset
|
425 |
be first the code for $b$ (including the label |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
426 |
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:
372
diff
changeset
|
427 |
the else-branch, the label $L_\textit{ifesle}$, followed by |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
428 |
the instructions for the else-branch, followed by the |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
429 |
after-the-else-branch label. Consider for example the |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
430 |
if-statement: |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
431 |
|
690 | 432 |
\begin{lstlisting}[mathescape,numbers=none,language=While] |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
433 |
if 1 = 1 then x := 2 else y := 3 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
434 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
435 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
436 |
\noindent |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
437 |
The generated code is as follows: |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
438 |
|
690 | 439 |
\begin{lstlisting}[language=JVMIS,mathescape,numbers=left] |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
440 |
ldc 1 |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
441 |
ldc 1 |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
442 |
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:
372
diff
changeset
|
443 |
ldc 2 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
444 |
istore 0 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
445 |
goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
446 |
L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
447 |
ldc 3 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
448 |
istore 1 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
449 |
L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
450 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
451 |
|
601 | 452 |
\begin{tikzpicture}[remember picture,overlay] |
453 |
\draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
454 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
455 |
\draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
601 | 456 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); |
457 |
\end{tikzpicture} |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
458 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
459 |
\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:
372
diff
changeset
|
460 |
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:
376
diff
changeset
|
461 |
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:
372
diff
changeset
|
462 |
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:
376
diff
changeset
|
463 |
environment $E$ is threaded through the recursive calls of |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
464 |
\textit{compile}. The function receives an environment $E$, |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
465 |
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:
376
diff
changeset
|
466 |
$E'$. This happens for example in the if-statement above |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
467 |
whenever the variable \code{x} has not been used before. |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
468 |
Similarly with the environment $E''$ for the second call to |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
469 |
\textit{compile}. $E''$ is also the environment that needs to |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
470 |
be returned as part of the answer. |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
471 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
472 |
The compilation of the while-loops, say |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
473 |
\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:
376
diff
changeset
|
474 |
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:
376
diff
changeset
|
475 |
and the control-flow needs to be as follows |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
476 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
477 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
478 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
479 |
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:
372
diff
changeset
|
480 |
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:
372
diff
changeset
|
481 |
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:
372
diff
changeset
|
482 |
\node (A0) [point, left=of A1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
483 |
\node (A1) [point] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
484 |
\node (b) [block, right=of A1] {code of $b$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
485 |
\node (A2) [point, right=of b] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
486 |
\node (cs1) [block, right=of A2] {code of $cs$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
487 |
\node (A3) [point, right=of cs1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
488 |
\node (A4) [point, right=of A3] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
489 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
490 |
\draw (A0) edge [->, black, line width=1mm] (b); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
491 |
\draw (b) edge [->, black, line width=1mm] (cs1); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
492 |
\draw (cs1) edge [->, black, line width=1mm] (A3); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
493 |
\draw (A3) edge [->,skip loop] (A1); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
494 |
\end{tikzpicture} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
495 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
496 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
497 |
\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:
372
diff
changeset
|
498 |
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:
372
diff
changeset
|
499 |
control flow. |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
500 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
501 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
502 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
503 |
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:
372
diff
changeset
|
504 |
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:
372
diff
changeset
|
505 |
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:
372
diff
changeset
|
506 |
\node (A0) [point, left=of A1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
507 |
\node (A1) [point] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
508 |
\node (b) [block, right=of A1] {code of $b$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
509 |
\node (A2) [point, right=of b] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
510 |
\node (cs1) [block, right=of A2] {code of $cs$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
511 |
\node (A3) [point, right=of cs1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
512 |
\node (A4) [point, right=of A3] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
513 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
514 |
\draw (A0) edge [->, black, line width=1mm] (b); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
515 |
\draw (b) edge [->, black, line width=1mm] (A2); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
516 |
\draw (A2) edge [skip loop] (A3); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
517 |
\draw (A3) edge [->, black, line width=1mm] (A4); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
518 |
\end{tikzpicture} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
519 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
520 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
521 |
\noindent Again we can use the \textit{compile}-function for |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
522 |
boolean expressions to insert the appropriate jump to the |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
523 |
end of the loop (label $L_{wend}$ below). |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
524 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
525 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
526 |
\begin{tabular}{lcl} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
527 |
$\textit{compile}(\pcode{while}\; b\; \pcode{do} \;cs, E)$ & $\dn$\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
528 |
\multicolumn{3}{l}{$\qquad L_{wbegin}\;$ (fresh label)}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
529 |
\multicolumn{3}{l}{$\qquad L_{wend}\;$ (fresh label)}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
530 |
\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:
372
diff
changeset
|
531 |
\multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
532 |
\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:
372
diff
changeset
|
533 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\ |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
534 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\; \text{goto}\;L_{wbegin}$}\\ |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
535 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_{wend}:, E')$}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
536 |
\end{tabular} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
537 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
538 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
539 |
\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:
376
diff
changeset
|
540 |
you can consider the while-loop |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
541 |
|
690 | 542 |
\begin{lstlisting}[mathescape,numbers=none,language=While] |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
543 |
while x <= 10 do x := x + 1 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
544 |
\end{lstlisting} |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
545 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
546 |
\noindent yielding the following code |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
547 |
|
690 | 548 |
\begin{lstlisting}[language=JVMIS,mathescape,numbers=left] |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
549 |
L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$ |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
550 |
iload 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
551 |
ldc 10 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
552 |
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:
376
diff
changeset
|
553 |
iload 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
554 |
ldc 1 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
555 |
iadd |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
556 |
istore 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
557 |
goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$ |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
558 |
L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$ |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
559 |
\end{lstlisting} |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
560 |
|
601 | 561 |
\begin{tikzpicture}[remember picture,overlay] |
562 |
\draw[->,very thick] (LA) edge [->,to path={-- ++(10mm,0mm) |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
563 |
-- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east); |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
564 |
\draw[->,very thick] (LC) edge [->,to path={-- ++(10mm,0mm) |
601 | 565 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east); |
566 |
\end{tikzpicture} |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
567 |
|
690 | 568 |
\noindent |
569 |
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:
376
diff
changeset
|
570 |
|
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
571 |
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:
376
diff
changeset
|
572 |
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:
372
diff
changeset
|
573 |
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:
373
diff
changeset
|
574 |
having to generate a lot of code for each |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
575 |
\pcode{write}-command, we use a separate helper-method and |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
576 |
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:
376
diff
changeset
|
577 |
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:
376
diff
changeset
|
578 |
follows. |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
579 |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
580 |
|
690 | 581 |
\begin{lstlisting}[language=JVMIS,numbers=left] |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
582 |
.method public static write(I)V |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
583 |
.limit locals 1 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
584 |
.limit stack 2 |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
585 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
586 |
iload 0 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
587 |
invokevirtual java/io/PrintStream/println(I)V |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
588 |
return |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
589 |
.end method |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
590 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
591 |
|
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
592 |
\noindent The first line marks the beginning of the method, |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
593 |
called \pcode{write}. It takes a single integer argument |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
594 |
indicated by the \pcode{(I)} and returns no result, indicated |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
595 |
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:
373
diff
changeset
|
596 |
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:
373
diff
changeset
|
597 |
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:
373
diff
changeset
|
598 |
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:
373
diff
changeset
|
599 |
\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:
373
diff
changeset
|
600 |
\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:
373
diff
changeset
|
601 |
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:
373
diff
changeset
|
602 |
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:
373
diff
changeset
|
603 |
\pcode{println} (from the class \pcode{java/io/PrintStream}). |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
604 |
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:
376
diff
changeset
|
605 |
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:
373
diff
changeset
|
606 |
\pcode{return}-instruction in the next line changes the |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
607 |
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:
373
diff
changeset
|
608 |
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:
376
diff
changeset
|
609 |
included in any code we generate. The helper-method |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
610 |
\pcode{write} can be invoked with the two instructions |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
611 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
612 |
\begin{lstlisting}[mathescape,language=JVMIS] |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
613 |
iload $E(x)$ |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
614 |
invokestatic XXX/XXX/write(I)V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
615 |
\end{lstlisting} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
616 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
617 |
\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:
376
diff
changeset
|
618 |
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:
376
diff
changeset
|
619 |
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:
376
diff
changeset
|
620 |
explained shortly). |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
621 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
622 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
623 |
\begin{figure}[t] |
690 | 624 |
\begin{lstlisting}[mathescape,language=JVMIS,numbers=left] |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
625 |
.class public XXX.XXX |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
626 |
.super java/lang/Object |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
627 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
628 |
.method public <init>()V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
629 |
aload_0 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
630 |
invokenonvirtual java/lang/Object/<init>()V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
631 |
return |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
632 |
.end method |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
633 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
634 |
.method public static main([Ljava/lang/String;)V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
635 |
.limit locals 200 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
636 |
.limit stack 200 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
637 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
638 |
$\textit{\ldots{}here comes the compiled code\ldots}$ |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
639 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
640 |
return |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
641 |
.end method |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
642 |
\end{lstlisting} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
643 |
\caption{Boilerplate code needed for running generated code.\label{boiler}} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
644 |
\end{figure} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
645 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
646 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
647 |
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:
373
diff
changeset
|
648 |
of (JVM assembly) instructions. Unfortunately, there is a bit |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
649 |
more boilerplate code needed before these instructions can be |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
650 |
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:
376
diff
changeset
|
651 |
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:
376
diff
changeset
|
652 |
other virtual machine or a machine language, then we would |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
653 |
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:
376
diff
changeset
|
654 |
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:
376
diff
changeset
|
655 |
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:
376
diff
changeset
|
656 |
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:
376
diff
changeset
|
657 |
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:
376
diff
changeset
|
658 |
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:
376
diff
changeset
|
659 |
be conservative default values that allow is to run some |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
660 |
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:
376
diff
changeset
|
661 |
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:
376
diff
changeset
|
662 |
stack and local variables. |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
663 |
|
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
664 |
To sum up, in Figure~\ref{test} is the complete code generated |
601 | 665 |
for the slightly nonsensical program |
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
666 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
667 |
\begin{lstlisting}[mathescape,language=While] |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
668 |
x := 1 + 2; |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
669 |
write x |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
670 |
\end{lstlisting} |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
671 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
672 |
\noindent Having this code at our disposal, we need the |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
673 |
assembler to translate the generated code into JVM bytecode (a |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
674 |
class file). This bytecode is understood by the JVM and can be |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
675 |
run by just invoking the \pcode{java}-program. |
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
676 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
677 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
678 |
\begin{figure}[p] |
383
a6a6bf32fade
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
377
diff
changeset
|
679 |
\lstinputlisting[language=JVMIS]{../progs/test-small.j} |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
680 |
\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:
376
diff
changeset
|
681 |
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:
383
diff
changeset
|
682 |
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:
374
diff
changeset
|
683 |
\end{figure} |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
684 |
|
690 | 685 |
\subsection*{Arrays} |
686 |
||
687 |
Maybe a useful addition to the While-language are arrays. This |
|
688 |
lets us generate more interesting While-programs by translating |
|
689 |
BF*** programs into equivalent While-programs. This means we |
|
690 |
want to support the following three constructions |
|
691 |
||
692 |
\begin{lstlisting}[mathescape,language=While] |
|
693 |
new arr[15000] |
|
694 |
x := 3 + arr[3 + y] |
|
695 |
arr[42 * n] := ... |
|
696 |
\end{lstlisting} |
|
697 |
||
698 |
\noindent |
|
699 |
The first one creates a new array with name \pcode{arr} that can |
|
700 |
hold a given number of integers. The second is referencing an array |
|
701 |
inside a arithmetic expression. Essentially we have to be able to |
|
702 |
look up the contents of an array at an index. Similarly we need to be |
|
703 |
able to update the content of an array (3rd line). The latter two |
|
704 |
constructions state that the index to an array can be given by |
|
705 |
an arithmetic expression. For creating a new array we need to generate |
|
706 |
the following JVM instructions: |
|
707 |
||
708 |
\begin{lstlisting}[mathescape,language=JVMIS] |
|
709 |
ldc number |
|
710 |
newarray int |
|
711 |
astore loc_var |
|
712 |
\end{lstlisting} |
|
713 |
||
714 |
\noindent |
|
715 |
First we need to put the dimension of the array onto the |
|
716 |
stack, then next instruction creates the array and last we |
|
717 |
need to store the array in a local variable (like ``simple'' variables). |
|
718 |
For looking up an element in an array we can use the following code |
|
719 |
||
720 |
\begin{lstlisting}[mathescape,language=JVMIS] |
|
721 |
aload loc_var |
|
722 |
index_aexp |
|
723 |
iaload |
|
724 |
\end{lstlisting} |
|
725 |
||
726 |
\noindent |
|
727 |
This first loads the ``pointer'' to the array onto the stack. Then we have the |
|
728 |
instructions corresponding to the index where we want to look up the array. The |
|
729 |
idea is that these instructions will leave a concrete number on the stack, which is |
|
730 |
the index. Finally we just need to load the corresponding element onto the stack. |
|
731 |
||
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
732 |
\end{document} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
733 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
734 |
%%% Local Variables: |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
735 |
%%% mode: latex |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
736 |
%%% TeX-master: t |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
737 |
%%% End: |