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