author | Christian Urban <urbanc@in.tum.de> |
Tue, 25 Sep 2018 21:57:14 +0100 | |
changeset 562 | fa6d00968b0e |
parent 452 | b93f4d2aeee1 |
child 600 | d488a3e7b0ec |
permissions | -rw-r--r-- |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{../style} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs} |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
4 |
\usepackage{../grammar} |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
5 |
\usepackage{../graphics} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
6 |
|
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\begin{document} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
10 |
\section*{Handout 7 (Compilation)} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
12 |
The purpose of a compiler is to transform a program, a human |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
13 |
can read and write, into code the machine can run as fast as |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
14 |
possible. The fastest code would be machine code the CPU can |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
15 |
run directly, but it is often enough to improve the speed of a |
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
16 |
program by just targeting a virtual machine. This produces not |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
327
diff
changeset
|
17 |
the fastest possible code, but code that is fast enough and |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
18 |
has the advantage that the virtual machine takes care of |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
19 |
things a compiler would normally need to take care of (like |
452
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
20 |
explicit memory management). Why study compilers? John Regher |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
21 |
gives this answer in his compiler blog:\footnote{\url{http://blog.regehr.org/archives/1419}} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
|
452
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
23 |
\begin{quote}\it{}``We can start off with a couple of observations |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
24 |
about the role of compilers. First, hardware is getting weirder |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
25 |
rather than getting clocked faster: almost all processors are |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
26 |
multicores and it looks like there is increasing asymmetry in |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
27 |
resources across cores. Processors come with vector units, crypto |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
28 |
accelerators, bit twiddling instructions, and lots of features to |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
29 |
make virtualization and concurrency work. We have DSPs, GPUs, |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
30 |
big.little, and Xeon Phi. This is only scratching the |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
31 |
surface. Second, we’re getting tired of low-level languages and |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
32 |
their associated security disasters, we want to write new code, to |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
33 |
whatever extent possible, in safer, higher-level |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
34 |
languages. Compilers are caught right in the middle of these |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
35 |
opposing trends: one of their main jobs is to help bridge the large |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
36 |
and growing gap between increasingly high-level languages and |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
37 |
increasingly wacky platforms. It’s effectively a perpetual |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
38 |
employment act for solid compiler hackers.'' |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
39 |
\end{quote} |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
40 |
|
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
41 |
As a first example in this module we will implement a compiler for the |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
42 |
very simple While-language. It will generate code for the Java Virtual |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
43 |
Machine (JVM). This is a stack-based virtual machine, a fact which |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
44 |
will make it easy to generate code for arithmetic expressions. For |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
45 |
example for generating code for the expression $1 + 2$ we need to |
b93f4d2aeee1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
46 |
generate the following three instructions |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
47 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
48 |
\begin{lstlisting}[numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
49 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
50 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
51 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
52 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
53 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
54 |
\noindent The first instruction loads the constant $1$ onto |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
55 |
the stack, the next one $2$, the third instruction adds both |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
56 |
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
|
57 |
with the result $3$. For simplicity, we will throughout |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
58 |
consider only integer numbers and results. Therefore we can |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
59 |
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
|
60 |
\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
|
61 |
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
|
62 |
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
|
63 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
64 |
Recall our grammar for arithmetic expressions ($E$ is the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
65 |
starting symbol): |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
66 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
67 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
68 |
\begin{plstx}[rhs style=, margin=3cm] : \meta{E} ::= \meta{T} $+$ \meta{E} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
69 |
| \meta{T} $-$ \meta{E} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
70 |
| \meta{T}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
71 |
: \meta{T} ::= \meta{F} $*$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
72 |
| \meta{F} $\backslash$ \meta{T} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
73 |
| \meta{F}\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
74 |
: \meta{F} ::= ( \meta{E} ) |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
75 |
| \meta{Id} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
76 |
| \meta{Num}\\ \end{plstx} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
77 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
78 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
79 |
\noindent where \meta{Id} stands for variables and \meta{Num} |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
80 |
for numbers. For the moment let us omit variables from |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
81 |
arithmetic expressions. Our parser will take this grammar and |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
82 |
given an input produce abstract syntax trees. For example for |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
83 |
the expression $1 + ((2 * 3) + (4 - 3))$ it will produce the |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
84 |
following tree. |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
85 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
86 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
87 |
\begin{tikzpicture} \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]] \end{tikzpicture} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
88 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
89 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
90 |
\noindent To generate code for this expression, we need to |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
91 |
traverse this tree in post-order fashion and emit code for |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
92 |
each node---this traversal in post-order fashion will produce |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
93 |
code for a stack-machine (what the JVM is). Doing so for the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
94 |
tree above generates the instructions |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
95 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
96 |
\begin{lstlisting}[numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
97 |
ldc 1 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
98 |
ldc 2 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
99 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
100 |
imul |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
101 |
ldc 4 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
102 |
ldc 3 |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
103 |
isub |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
104 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
105 |
iadd |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
106 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
107 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
108 |
\noindent If we ``run'' these instructions, the result $8$ |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
109 |
will be on top of the stack (I leave this to you to verify; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
110 |
the meaning of each instruction should be clear). The result |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
111 |
being on the top of the stack will be a convention we always |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
112 |
observe in our compiler, that is the results of arithmetic |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
113 |
expressions will always be on top of the stack. Note, that a |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
114 |
different bracketing of the expression, for example $(1 + (2 * |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
115 |
3)) + (4 - 3)$, produces a different abstract syntax tree and |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
116 |
thus potentially also a different list of instructions. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
117 |
Generating code in this fashion is rather easy to implement: |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
118 |
it can be done with the following recursive |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
119 |
\textit{compile}-function, which takes the abstract syntax |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
120 |
tree as argument: |
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} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
123 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
124 |
$\textit{compile}(n)$ & $\dn$ & $\pcode{ldc}\; n$\\ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
125 |
$\textit{compile}(a_1 + a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
126 |
$\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
|
127 |
$\textit{compile}(a_1 - a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
128 |
$\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
|
129 |
$\textit{compile}(a_1 * a_2)$ & $\dn$ & |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
130 |
$\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
|
131 |
$\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
|
132 |
$\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
|
133 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
134 |
\end{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
135 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
136 |
However, our arithmetic expressions can also contain |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
137 |
variables. We will represent them as \emph{local variables} in |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
138 |
the JVM. Essentially, local variables are an array or pointers |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
139 |
to memory cells, containing in our case only integers. Looking |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
140 |
up a variable can be done with the instruction |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
141 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
142 |
\begin{lstlisting}[mathescape,numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
143 |
iload $index$ |
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 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
146 |
\noindent |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
can be done by the instruction |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
150 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
151 |
\begin{lstlisting}[mathescape,numbers=none] |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
152 |
istore $index$ |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
153 |
\end{lstlisting} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
154 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
155 |
\noindent Note that this also pops off the top of the stack. |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
156 |
One problem we have to overcome, however, is that local |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
157 |
variables are addressed, not by identifiers, but by numbers |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
158 |
(starting from $0$). Therefore our compiler needs to maintain |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
159 |
a kind of environment where variables are associated to |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
160 |
numbers. This association needs to be unique: if we muddle up |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
161 |
the numbers, then we essentially confuse variables and the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
162 |
consequence will usually be an erroneous result. Our extended |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
163 |
\textit{compile}-function for arithmetic expressions will |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
164 |
therefore take two arguments: the abstract syntax tree and the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
165 |
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
|
166 |
|
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
167 |
\begin{center} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
168 |
\begin{tabular}{lcl} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
169 |
$\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
|
170 |
$\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
|
171 |
$\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
|
172 |
$\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
|
173 |
$\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
|
174 |
$\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
|
175 |
$\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
|
176 |
$\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
|
177 |
$\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
|
178 |
$\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
|
179 |
\end{tabular} |
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
180 |
\end{center} |
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 In the last line we generate the code for variables |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
183 |
where $E(x)$ stands for looking up the environment to which |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
184 |
index the variable $x$ maps to. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
185 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
186 |
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
|
187 |
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
|
188 |
\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
|
189 |
let us first describe the compilation of statements of the |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
190 |
While-language. The clause for \pcode{skip} is trivial, since |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
191 |
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
|
192 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
193 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
194 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
195 |
$\textit{compile}(\pcode{skip}, E)$ & $\dn$ & $([], E)$\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
196 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
197 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
198 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
199 |
\noindent $[]$ is the empty list of instructions. Note that |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
environment for variables. The reason for the environment is |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
203 |
that assignments in the While-language might change the |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
204 |
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
|
205 |
time, we need to allocate a new index and if it has been used |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
206 |
before, we need to be able to retrieve the associated index. |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
207 |
This is reflected in the clause for compiling assignments: |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
208 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
209 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
210 |
\begin{tabular}{lcl} |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
211 |
$\textit{compile}(x := a, E)$ & $\dn$ & |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
212 |
$(\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
|
213 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
214 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
215 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
216 |
\noindent We first generate code for the right-hand side of |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
217 |
the assignment and then add an \pcode{istore}-instruction at |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
218 |
the end. By convention the result of the arithmetic expression |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
219 |
$a$ will be on top of the stack. After the \pcode{istore} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
220 |
instruction, the result will be stored in the index |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
221 |
corresponding to the variable $x$. If the variable $x$ has |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
222 |
been used before in the program, we just need to look up what |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
223 |
the index is and return the environment unchanged (that is in |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
224 |
this case $E' = E$). However, if this is the first encounter |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
225 |
of the variable $x$ in the program, then we have to augment |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
226 |
the environment and assign $x$ with the largest index in $E$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
227 |
plus one (that is $E' = E(x \mapsto largest\_index + 1)$). |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
228 |
That means for the assignment $x := x + 1$ we generate the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
229 |
following code |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
230 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
231 |
\begin{lstlisting}[mathescape,numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
232 |
iload $n_x$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
233 |
ldc 1 |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
234 |
iadd |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
235 |
istore $n_x$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
236 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
237 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
238 |
\noindent |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
239 |
where $n_x$ is the index for the variable $x$. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
240 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
241 |
More complicated is the code for \pcode{if}-statments, say |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
242 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
243 |
\begin{lstlisting}[mathescape,language={},numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
244 |
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
|
245 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
246 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
247 |
\noindent where $b$ is a boolean expression and the $cs_{1/2}$ |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
248 |
are the statements for each \pcode{if}-branch. Lets assume |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
249 |
we already generated code for $b$ and $cs_{1/2}$. Then in the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
250 |
true-case the control-flow of the program needs to be |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
251 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
252 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
253 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
254 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
\node (A1) [point] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
258 |
\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
|
259 |
\node (A2) [point, right=of b] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
260 |
\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
|
261 |
\node (A3) [point, right=of cs1] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
262 |
\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
|
263 |
\node (A4) [point, right=of cs2] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
264 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
265 |
\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
|
266 |
\draw (b) edge [->, black, line width=1mm] (cs1); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
267 |
\draw (cs1) edge [->, black, line width=1mm] (A3); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
268 |
\draw (A3) edge [->, black, skip loop] (A4); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
269 |
\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
|
270 |
\end{tikzpicture} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
271 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
272 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
273 |
\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
|
274 |
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
|
275 |
$cs_1$. After this however, we must not run the code for |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
276 |
$cs_2$, but always jump after the last instruction of $cs_2$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
277 |
(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
|
278 |
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
|
279 |
$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
|
280 |
\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
|
281 |
control-flow |
370
a65767fe5d71
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
282 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
283 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
284 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
285 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
286 |
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
|
287 |
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
|
288 |
\node (A1) [point] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
289 |
\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
|
290 |
\node (A2) [point, right=of b] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
291 |
\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
|
292 |
\node (A3) [point, right=of cs1] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
293 |
\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
|
294 |
\node (A4) [point, right=of cs2] {}; |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
295 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
296 |
\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
|
297 |
\draw (b) edge [->, black, line width=1mm] (A2); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
298 |
\draw (A2) edge [skip loop] (A3); |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
299 |
\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
|
300 |
\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
|
301 |
\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
|
302 |
\end{tikzpicture} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
303 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
304 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
305 |
\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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
code comes after the if-statement. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
310 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
like |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
319 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
320 |
\begin{lstlisting}[mathescape,numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
321 |
L: |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
322 |
$instr_1$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
323 |
$instr_2$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
324 |
$\vdots$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
325 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
326 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
327 |
\noindent where a label is indicated by a colon. |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
328 |
|
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
329 |
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
|
330 |
\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
|
331 |
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
|
332 |
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
|
333 |
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
|
334 |
for example, is as follows: |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
335 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
336 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
337 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
338 |
$\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
|
339 |
\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
|
340 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
341 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
342 |
|
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
343 |
\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
|
344 |
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
|
345 |
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
|
346 |
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
|
347 |
(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
|
348 |
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
|
349 |
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
|
350 |
(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
|
351 |
the instruction |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
352 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
353 |
\begin{lstlisting}[mathescape,numbers=none] |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
354 |
if_icmpne $lab$ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
355 |
\end{lstlisting} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
356 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
357 |
\noindent Other jump instructions for boolean operators are |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
358 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
359 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
360 |
\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
|
361 |
$\not=$ & $\Rightarrow$ & \pcode{if_icmpeq}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
362 |
$<$ & $\Rightarrow$ & \pcode{if_icmpge}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
363 |
$\le$ & $\Rightarrow$ & \pcode{if_icmpgt}\\ |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
364 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
365 |
\end{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
366 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
367 |
\noindent and so on. I leave it to you to extend the |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
368 |
\textit{compile}-function for the other boolean expressions. |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
369 |
Note that we need to jump whenever the boolean is \emph{not} |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
370 |
true, which means we have to ``negate'' the jump |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
371 |
condition---equals becomes not-equal, less becomes |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
372 |
greater-or-equal. If you do not like this design (it can be |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
373 |
the source of some nasty, hard-to-detect errors), you can also |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
374 |
change the layout of the code and first give the code for the |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
375 |
else-branch and then for the if-branch. However in the case |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
376 |
of while-loops this way of generating code still seems |
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
377 |
the most convenient. |
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 |
We are now ready to give the compile function for |
376
af65ffff9cdd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
375
diff
changeset
|
380 |
if-statments---remember this function returns for staments a |
372
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
381 |
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
|
382 |
|
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
383 |
\begin{center} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
384 |
\begin{tabular}{lcl} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
385 |
$\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
|
386 |
\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
|
387 |
\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
|
388 |
\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
|
389 |
\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
|
390 |
\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
|
391 |
\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
|
392 |
\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
|
393 |
\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
|
394 |
\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
|
395 |
\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
|
396 |
\end{tabular} |
d6af4b1239de
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
370
diff
changeset
|
397 |
\end{center} |
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
398 |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
399 |
\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
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
if-statement: |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
409 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
410 |
\begin{lstlisting}[mathescape,numbers=none,language={}] |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
411 |
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
|
412 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
413 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
414 |
\noindent |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
415 |
The generated code is as follows: |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
416 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
417 |
\begin{lstlisting}[mathescape,language={}] |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
418 |
ldc 1 |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
419 |
ldc 1 |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
420 |
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
|
421 |
ldc 2 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
422 |
istore 0 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
423 |
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
|
424 |
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
|
425 |
ldc 3 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
426 |
istore 1 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
427 |
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
|
428 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
429 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
430 |
\begin{tikzpicture}[remember picture,overlay] \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
431 |
-- ++(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
|
432 |
\draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
433 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); \end{tikzpicture} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
434 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
435 |
\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
|
436 |
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
|
437 |
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
|
438 |
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
|
439 |
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
|
440 |
\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
|
441 |
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
|
442 |
$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
|
443 |
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
|
444 |
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
|
445 |
\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
|
446 |
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
|
447 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
448 |
The compilation of the while-loops, say |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
449 |
\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
|
450 |
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
|
451 |
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
|
452 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
453 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
454 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
455 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
456 |
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
|
457 |
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
|
458 |
\node (A0) [point, left=of A1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
459 |
\node (A1) [point] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
460 |
\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
|
461 |
\node (A2) [point, right=of b] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
462 |
\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
|
463 |
\node (A3) [point, right=of cs1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
464 |
\node (A4) [point, right=of A3] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
465 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
466 |
\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
|
467 |
\draw (b) edge [->, black, line width=1mm] (cs1); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
468 |
\draw (cs1) edge [->, black, line width=1mm] (A3); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
469 |
\draw (A3) edge [->,skip loop] (A1); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
470 |
\end{tikzpicture} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
471 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
472 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
473 |
\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
|
474 |
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
|
475 |
control flow. |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
476 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
477 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
478 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
479 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
480 |
point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
481 |
skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
482 |
\node (A0) [point, left=of A1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
483 |
\node (A1) [point] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
484 |
\node (b) [block, right=of A1] {code of $b$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
485 |
\node (A2) [point, right=of b] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
486 |
\node (cs1) [block, right=of A2] {code of $cs$}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
487 |
\node (A3) [point, right=of cs1] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
488 |
\node (A4) [point, right=of A3] {}; |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
489 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
490 |
\draw (A0) edge [->, black, line width=1mm] (b); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
491 |
\draw (b) edge [->, black, line width=1mm] (A2); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
492 |
\draw (A2) edge [skip loop] (A3); |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
493 |
\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
|
494 |
\end{tikzpicture} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
495 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
496 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
497 |
\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
|
498 |
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
|
499 |
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
|
500 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
501 |
\begin{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
502 |
\begin{tabular}{lcl} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
503 |
$\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
|
504 |
\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
|
505 |
\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
|
506 |
\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
|
507 |
\multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\ |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
508 |
\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
|
509 |
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\ |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
510 |
\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
|
511 |
\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
|
512 |
\end{tabular} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
513 |
\end{center} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
514 |
|
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
515 |
\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
|
516 |
you can consider the while-loop |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
517 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
518 |
\begin{lstlisting}[mathescape,numbers=none,language={}] |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
519 |
while x <= 10 do x := x + 1 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
520 |
\end{lstlisting} |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
521 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
522 |
\noindent yielding the following code |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
523 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
524 |
\begin{lstlisting}[mathescape,language={}] |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
525 |
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
|
526 |
iload 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
527 |
ldc 10 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
528 |
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
|
529 |
iload 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
530 |
ldc 1 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
531 |
iadd |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
532 |
istore 0 |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
533 |
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
|
534 |
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
|
535 |
\end{lstlisting} |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
536 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
537 |
\begin{tikzpicture}[remember picture,overlay] \draw[->,very thick] (LA) edge [->,to path={-- ++(10mm,0mm) |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
538 |
-- ++(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
|
539 |
\draw[->,very thick] (LC) edge [->,to path={-- ++(10mm,0mm) |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
540 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east); \end{tikzpicture} |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
541 |
|
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
542 |
|
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
543 |
Next we need to consider the statement \pcode{write x}, which |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
544 |
can be used to print out the content of a variable. For this |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
545 |
we need to use a Java library function. In order to avoid |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
546 |
having to generate a lot of code for each |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
547 |
\pcode{write}-command, we use a separate helper-method and |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
548 |
just call this method with an argument (which needs to be |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
549 |
placed onto the stack). The code of the helper-method is as |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
550 |
follows. |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
551 |
|
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
552 |
|
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
553 |
\begin{lstlisting}[language=JVMIS] |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
554 |
.method public static write(I)V |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
555 |
.limit locals 1 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
556 |
.limit stack 2 |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
557 |
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
|
558 |
iload 0 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
559 |
invokevirtual java/io/PrintStream/println(I)V |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
560 |
return |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
561 |
.end method |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
562 |
\end{lstlisting} |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
372
diff
changeset
|
563 |
|
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
564 |
\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
|
565 |
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
|
566 |
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
|
567 |
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
|
568 |
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
|
569 |
two cells will be sufficient (Line 3). Line 4 instructs the |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
570 |
JVM to get the value of the field \pcode{out} of the class |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
571 |
\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
|
572 |
\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
|
573 |
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
|
574 |
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
|
575 |
\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
|
576 |
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
|
577 |
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
|
578 |
\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
|
579 |
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
|
580 |
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
|
581 |
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
|
582 |
\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
|
583 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
584 |
\begin{lstlisting}[mathescape,language=JVMIS] |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
585 |
iload $E(x)$ |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
586 |
invokestatic XXX/XXX/write(I)V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
587 |
\end{lstlisting} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
588 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
589 |
\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
|
590 |
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
|
591 |
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
|
592 |
explained shortly). |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
593 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
594 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
595 |
\begin{figure}[t] |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
596 |
\begin{lstlisting}[mathescape,language=JVMIS] |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
597 |
.class public XXX.XXX |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
598 |
.super java/lang/Object |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
599 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
600 |
.method public <init>()V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
601 |
aload_0 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
602 |
invokenonvirtual java/lang/Object/<init>()V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
603 |
return |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
604 |
.end method |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
605 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
606 |
.method public static main([Ljava/lang/String;)V |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
607 |
.limit locals 200 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
608 |
.limit stack 200 |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
609 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
610 |
$\textit{\ldots{}here comes the compiled code\ldots}$ |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
611 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
612 |
return |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
613 |
.end method |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
614 |
\end{lstlisting} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
615 |
\caption{Boilerplate code needed for running generated code.\label{boiler}} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
616 |
\end{figure} |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
617 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
618 |
|
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
619 |
By generating code for a While-program, we end up with a list |
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
620 |
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
|
621 |
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
|
622 |
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
|
623 |
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
|
624 |
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
|
625 |
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
|
626 |
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
|
627 |
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
|
628 |
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
|
629 |
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
|
630 |
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
|
631 |
be conservative default values that allow is to run some |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
632 |
simple While-programs. In a real compiler, we would of course |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
633 |
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
|
634 |
stack and local variables. |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
635 |
|
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
636 |
To sum up, in Figure~\ref{test} is the complete code generated |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
637 |
for the slightly non-sensical program |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
638 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
639 |
\begin{lstlisting}[mathescape,language=While] |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
640 |
x := 1 + 2; |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
641 |
write x |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
642 |
\end{lstlisting} |
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
643 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
644 |
\noindent Having this code at our disposal, we need the |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
645 |
assembler to translate the generated code into JVM bytecode (a |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
646 |
class file). This bytecode is understood by the JVM and can be |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
647 |
run by just invoking the \pcode{java}-program. |
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
648 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
649 |
|
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
650 |
\begin{figure}[p] |
383
a6a6bf32fade
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
377
diff
changeset
|
651 |
\lstinputlisting[language=JVMIS]{../progs/test-small.j} |
377
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
652 |
\caption{Generated code for a test program. This code can be |
a052a83f562e
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
376
diff
changeset
|
653 |
processed by an Java assembler producing a class-file, which |
394
2f9fe225ecc8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
383
diff
changeset
|
654 |
can be run by the {\tt{}java}-program.\label{test}} |
375
bf36664a3196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
655 |
\end{figure} |
374
0e25fb72d339
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
656 |
|
327
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
657 |
\end{document} |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
658 |
|
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
659 |
%%% Local Variables: |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
660 |
%%% mode: latex |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
661 |
%%% TeX-master: t |
9470cd124667
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
662 |
%%% End: |