handouts/ho08.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 17 Nov 2015 15:03:08 +0000
changeset 378 e8ac05fe2630
child 379 fa2589ec0fae
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{../style}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../langs}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{../grammar}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{../graphics}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\begin{document}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\section*{Handout 8 (A Functional Language)}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
The purpose of a compiler is to transform a program, a human
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
can read and write, into code the machine can run as fast as
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
possible. The fastest code would be machine code the CPU can
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
run directly, but it is often enough to improve the speed of a
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
program by just targeting a virtual machine. This produces not
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
the fastest possible code, but code that is fast enough and
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
has the advantage that the virtual machine takes care of
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
things a compiler would normally need to take care of (like
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
explicit memory management).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
As a first example we will implement a compiler for the very
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
simple While-language. It will generate code for the Java
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
Virtual Machine (JVM). This is a stack-based virtual machine,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
a fact which will make it easy to generate code for arithmetic
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
expressions. For example for generating code for the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
expression $1 + 2$ we need to generate the following three
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
instructions
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
\begin{lstlisting}[numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
ldc 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
ldc 2
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
iadd 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
\noindent The first instruction loads the constant $1$ onto
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
the stack, the next one $2$, the third instruction adds both
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
numbers together replacing the top two elements of the stack
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
with the result $3$. For simplicity, we will throughout
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
consider only integer numbers and results. Therefore we can
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
use the JVM instructions \code{iadd}, \code{isub},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
\code{imul}, \code{idiv} and so on. The \code{i} stands for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
integer instructions in the JVM (alternatives are \code{d} for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
doubles, \code{l} for longs and \code{f} for floats).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
Recall our grammar for arithmetic expressions ($E$ is the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
starting symbol):
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
\begin{plstx}[rhs style=, margin=3cm]
: \meta{E} ::= \meta{T} $+$ \meta{E}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
         | \meta{T} $-$ \meta{E}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
         | \meta{T}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
: \meta{T} ::= \meta{F} $*$ \meta{T}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
          | \meta{F} $\backslash$ \meta{T}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
          | \meta{F}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
: \meta{F} ::= ( \meta{E} )
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
          | \meta{Id}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
          | \meta{Num}\\
\end{plstx}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
\noindent where \meta{Id} stands for variables and \meta{Num}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
for numbers. For the moment let us omit variables from
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
arithmetic expressions. Our parser will take this grammar and
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
given an input produce abstract syntax trees. For example for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
the expression $1 + ((2 * 3) + (4 - 3))$ it will produce the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
following tree.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
\begin{tikzpicture}
\Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]]
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
\noindent To generate code for this expression, we need to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
traverse this tree in post-order fashion and emit code for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
each node---this traversal in post-order fashion will produce
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
code for a stack-machine (what the JVM is). Doing so for the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
tree above generates the instructions
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
\begin{lstlisting}[numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
ldc 1 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
ldc 2 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
ldc 3 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
imul 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
ldc 4 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
ldc 3 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
isub 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
iadd 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
iadd
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
\noindent If we ``run'' these instructions, the result $8$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
will be on top of the stack (I leave this to you to verify;
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
the meaning of each instruction should be clear). The result
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
being on the top of the stack will be a convention we always
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
observe in our compiler, that is the results of arithmetic
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
expressions will always be on top of the stack. Note, that a
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
different bracketing of the expression, for example $(1 + (2 *
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
3)) + (4 - 3)$, produces a different abstract syntax tree and
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
thus potentially also a different list of instructions.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
Generating code in this fashion is rather easy to implement:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
it can be done with the following recursive
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
\textit{compile}-function, which takes the abstract syntax
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
tree as argument:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
$\textit{compile}(n)$ & $\dn$ & $\pcode{ldc}\; n$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
$\textit{compile}(a_1 + a_2)$ & $\dn$ &
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
$\textit{compile}(a_1) \;@\;\textit{compile}(a_2)\;@\; \pcode{iadd}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
$\textit{compile}(a_1 - a_2)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{isub}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
$\textit{compile}(a_1 * a_2)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{imul}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
$\textit{compile}(a_1 \backslash a_2)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
$\textit{compile}(a_1) \;@\; \textit{compile}(a_2)\;@\; \pcode{idiv}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
However, our arithmetic expressions can also contain
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
variables. We will represent them as \emph{local variables} in
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
the JVM. Essentially, local variables are an array or pointers
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
to memory cells, containing in our case only integers. Looking
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
up a variable can be done with the instruction
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
\begin{lstlisting}[mathescape,numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
iload $index$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
\noindent 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
which places the content of the local variable $index$ onto 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
the stack. Storing the top of the stack into a local variable 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
can be done by the instruction
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
\begin{lstlisting}[mathescape,numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
istore $index$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
\noindent Note that this also pops off the top of the stack.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
One problem we have to overcome, however, is that local
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
variables are addressed, not by identifiers, but by numbers
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
(starting from $0$). Therefore our compiler needs to maintain
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
a kind of environment where variables are associated to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
numbers. This association needs to be unique: if we muddle up
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
the numbers, then we essentially confuse variables and the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
consequence will usually be an erroneous result. Our extended
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
\textit{compile}-function for arithmetic expressions will
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
therefore take two arguments: the abstract syntax tree and the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
environment, $E$, that maps identifiers to index-numbers.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
$\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
$\textit{compile}(a_1 + a_2, E)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
$\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
$\textit{compile}(a_1 - a_2, E)$ & $\dn$ &
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
$\textit{compile}(a_1 * a_2, E)$ & $\dn$ &
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
$\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
$\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
$\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
\noindent In the last line we generate the code for variables
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
where $E(x)$ stands for looking up the environment to which
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
index the variable $x$ maps to.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
There is a similar \textit{compile}-function for boolean
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
expressions, but it includes a ``trick'' to do with
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
\pcode{if}- and \pcode{while}-statements. To explain the issue
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
let us first describe the compilation of statements of the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
While-language. The clause for \pcode{skip} is trivial, since
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
we do not have to generate any instruction
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
$\textit{compile}(\pcode{skip}, E)$ & $\dn$ & $([], E)$\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
\noindent $[]$ is the empty list of instructions. Note that
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
the \textit{compile}-function for statements returns a pair, a
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
list of instructions (in this case the empty list) and an
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
environment for variables. The reason for the environment is
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
that assignments in the While-language might change the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
environment---clearly if a variable is used for the first
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
time, we need to allocate a new index and if it has been used
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
before, we need to be able to retrieve the associated index.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
This is reflected in the clause for compiling assignments:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
$\textit{compile}(x := a, E)$ & $\dn$ & 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
$(\textit{compile}(a, E) \;@\;\pcode{istore}\;index, E')$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
\noindent We first generate code for the right-hand side of
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
the assignment and then add an \pcode{istore}-instruction at
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
the end. By convention the result of the arithmetic expression
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
$a$ will be on top of the stack. After the \pcode{istore}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
instruction, the result will be stored in the index
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
corresponding to the variable $x$. If the variable $x$ has
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
been used before in the program, we just need to look up what
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
the index is and return the environment unchanged (that is in
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
this case $E' = E$). However, if this is the first encounter 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
of the variable $x$ in the program, then we have to augment 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
the environment and assign $x$ with the largest index in $E$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
plus one (that is $E' = E(x \mapsto largest\_index + 1)$). 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
That means for the assignment $x := x + 1$ we generate the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
following code
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
\begin{lstlisting}[mathescape,numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
iload $n_x$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
ldc 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
iadd
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
istore $n_x$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
\noindent 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
where $n_x$ is the index for the variable $x$.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
More complicated is the code for \pcode{if}-statments, say
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
\begin{lstlisting}[mathescape,language={},numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
if $b$ then $cs_1$ else $cs_2$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
\noindent where $b$ is a boolean expression and the $cs_{1/2}$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
are the statements for each \pcode{if}-branch. Lets assume
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
we already generated code for $b$ and $cs_{1/2}$. Then in the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
true-case the control-flow of the program needs to be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
\begin{tikzpicture}[node distance=2mm and 4mm,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
 point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
 skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
\node (A1) [point] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
\node (b) [block, right=of A1] {code of $b$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
\node (A2) [point, right=of b] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
\node (cs1) [block, right=of A2] {code of $cs_1$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
\node (A3) [point, right=of cs1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
\node (cs2) [block, right=of A3] {code of $cs_2$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
\node (A4) [point, right=of cs2] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
\draw (A1) edge [->, black, line width=1mm] (b);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
\draw (b) edge [->, black, line width=1mm] (cs1);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
\draw (cs1) edge [->, black, line width=1mm] (A3);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
\draw (A3) edge [->, black, skip loop] (A4);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
\node [below=of cs2] {\raisebox{-5mm}{\small{}jump}};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
\noindent where we start with running the code for $b$; since
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
we are in the true case we continue with running the code for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
$cs_1$. After this however, we must not run the code for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
$cs_2$, but always jump after the last instruction of $cs_2$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
(the code for the \pcode{else}-branch). Note that this jump is
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
unconditional, meaning we always have to jump to the end of
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
$cs_2$. The corresponding instruction of the JVM is
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
\pcode{goto}. In case $b$ turns out to be false we need the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
control-flow
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
\begin{tikzpicture}[node distance=2mm and 4mm,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
 point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
 skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
\node (A1) [point] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
\node (b) [block, right=of A1] {code of $b$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
\node (A2) [point, right=of b] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
\node (cs1) [block, right=of A2] {code of $cs_1$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
\node (A3) [point, right=of cs1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
\node (cs2) [block, right=of A3] {code of $cs_2$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
\node (A4) [point, right=of cs2] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
\draw (A1) edge [->, black, line width=1mm] (b);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
\draw (b) edge [->, black, line width=1mm] (A2);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
\draw (A2) edge [skip loop] (A3);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
\draw (A3) edge [->, black, line width=1mm] (cs2);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
\draw (cs2) edge [->,black, line width=1mm] (A4);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
\node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
\noindent where we now need a conditional jump (if the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
if-condition is false) from the end of the code for the 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
boolean to the beginning of the instructions $cs_2$. Once we 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
are finished with running $cs_2$ we can continue with whatever
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
code comes after the if-statement.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
The \pcode{goto} and the conditional jumps need addresses to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
where the jump should go. Since we are generating assembly
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
code for the JVM, we do not actually have to give (numeric)
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
addresses, but can just attach (symbolic) labels to our code.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
These labels specify a target for a jump. Therefore the labels
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
need to be unique, as otherwise it would be ambiguous where a
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
jump should go to. A label, say \pcode{L}, is attached to code
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
like
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
\begin{lstlisting}[mathescape,numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
L:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
  $instr_1$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
  $instr_2$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
    $\vdots$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
\noindent where a label is indicated by a colon. 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
Recall the ``trick'' with compiling boolean expressions: the 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
\textit{compile}-function for boolean expressions takes three
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
arguments: an abstract syntax tree, an environment for 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
variable indices and also the label, $lab$, to where an conditional 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
jump needs to go. The clause for the expression $a_1 = a_2$, 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
for example, is as follows:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
$\textit{compile}(a_1 = a_2, E, lab)$ & $\dn$\\ 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
\multicolumn{3}{l}{$\qquad\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{if_icmpne}\;lab$}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
\noindent where we are first generating code for the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
subexpressions $a_1$ and $a_2$. This will mean after running
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
the corresponding code there will be two integers on top of
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
the stack. If they are equal, we do not have to do anything
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
(except for popping them off from the stack) and just continue
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
with the next instructions (see control-flow of ifs above).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
However if they are \emph{not} equal, then we need to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
(conditionally) jump to the label $lab$. This can be done with
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
the instruction
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
\begin{lstlisting}[mathescape,numbers=none]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
if_icmpne $lab$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
\noindent Other jump instructions for boolean operators are
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   341
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
\begin{tabular}{l@{\hspace{10mm}}c@{\hspace{10mm}}l}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
$\not=$ & $\Rightarrow$ & \pcode{if_icmpeq}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
$<$ & $\Rightarrow$ & \pcode{if_icmpge}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
$\le$ & $\Rightarrow$ & \pcode{if_icmpgt}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
\noindent and so on. I leave it to you to extend the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
\textit{compile}-function for the other boolean expressions.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
Note that we need to jump whenever the boolean is \emph{not}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
true, which means we have to ``negate'' the jump
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
condition---equals becomes not-equal, less becomes
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
greater-or-equal. If you do not like this design (it can be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
the source of some nasty, hard-to-detect errors), you can also
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
change the layout of the code and first give the code for the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
else-branch and then for the if-branch. However in the case
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
of while-loops this way of generating code still seems
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
the most convenient.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
We are now ready to give the compile function for 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
if-statments---remember this function returns for staments a 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
pair consisting of the code and an environment:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
$\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
\multicolumn{3}{l}{$\qquad L_\textit{ifelse}\;$ (fresh label)}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
\multicolumn{3}{l}{$\qquad L_\textit{ifend}\;$ (fresh label)}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
\multicolumn{3}{l}{$\qquad (is_1, E') = \textit{compile}(cs_1, E)$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
\multicolumn{3}{l}{$\qquad (is_2, E'') = \textit{compile}(cs_2, E')$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
\multicolumn{3}{l}{$\qquad(\textit{compile}(b, E, L_\textit{ifelse})$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   373
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is_1$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
\multicolumn{3}{l}{$\qquad\phantom{(}@\; \pcode{goto}\;L_\textit{ifend}$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifelse}:$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is_2$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_\textit{ifend}:, E'')$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
\noindent In the first two lines we generate two fresh labels
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
for the jump addresses (just before the else-branch and just
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
after). In the next two lines we generate the instructions for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
the two branches, $is_1$ and $is_2$. The final code will
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
be first the code for $b$ (including the label 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
just-before-the-else-branch), then the \pcode{goto} for after
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
the else-branch, the label $L_\textit{ifesle}$, followed by
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
the instructions for the else-branch, followed by the 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
after-the-else-branch label. Consider for example the 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
if-statement:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   392
\begin{lstlisting}[mathescape,numbers=none,language={}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
if 1 = 1 then x := 2 else y := 3
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   394
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
\noindent 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
The generated code is as follows:
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   399
\begin{lstlisting}[mathescape,language={}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
   ldc 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   401
   ldc 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   402
   if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   403
   ldc 2
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   404
   istore 0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
   goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   406
L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
   ldc 3
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
   istore 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
\begin{tikzpicture}[remember picture,overlay]
  \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   413
           -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   414
  \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
           -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east);
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
\noindent The first three lines correspond to the the boolean
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
expression $1 = 1$. The jump for when this boolean expression
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
is false is in Line~3. Lines 4-6 corresponds to the if-branch;
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
the else-branch is in Lines 8 and 9. Note carefully how the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
environment $E$ is threaded through the recursive calls of
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
\textit{compile}. The function receives an environment $E$,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
but it might extend it when compiling the if-branch, yielding
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   424
$E'$. This happens for example in the if-statement above
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   425
whenever the variable \code{x} has not been used before.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
Similarly with the environment $E''$ for the second call to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
\textit{compile}. $E''$ is also the environment that needs to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   428
be returned as part of the answer.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   429
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
The compilation of the while-loops, say 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
\pcode{while} $b$ \pcode{do} $cs$, is very similar. In case
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   432
the condition is true and we need to do another iteration, 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
and the control-flow needs to be as follows
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   434
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   436
\begin{tikzpicture}[node distance=2mm and 4mm,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   437
 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   438
 point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   439
 skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   440
\node (A0) [point, left=of A1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   441
\node (A1) [point] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   442
\node (b) [block, right=of A1] {code of $b$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   443
\node (A2) [point, right=of b] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   444
\node (cs1) [block, right=of A2] {code of $cs$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   445
\node (A3) [point, right=of cs1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
\node (A4) [point, right=of A3] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
\draw (A0) edge [->, black, line width=1mm] (b);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
\draw (b) edge [->, black, line width=1mm] (cs1);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
\draw (cs1) edge [->, black, line width=1mm] (A3);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
\draw (A3) edge [->,skip loop] (A1);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   453
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
\noindent Whereas if the condition is \emph{not} true, we
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
need to jump out of the loop, which gives the following
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
control flow.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   458
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   459
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
\begin{tikzpicture}[node distance=2mm and 4mm,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
 point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
 skip loop/.style={black, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
\node (A0) [point, left=of A1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   465
\node (A1) [point] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
\node (b) [block, right=of A1] {code of $b$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   467
\node (A2) [point, right=of b] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   468
\node (cs1) [block, right=of A2] {code of $cs$};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
\node (A3) [point, right=of cs1] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
\node (A4) [point, right=of A3] {};
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   472
\draw (A0) edge [->, black, line width=1mm] (b);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   473
\draw (b) edge [->, black, line width=1mm] (A2);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
\draw (A2) edge [skip loop] (A3);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
\draw (A3) edge [->, black, line width=1mm] (A4);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
\noindent Again we can use the \textit{compile}-function for
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
boolean expressions to insert the appropriate jump to the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   481
end of the loop (label $L_{wend}$ below).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   482
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   483
\begin{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   484
\begin{tabular}{lcl}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   485
$\textit{compile}(\pcode{while}\; b\; \pcode{do} \;cs, E)$ & $\dn$\\ 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
\multicolumn{3}{l}{$\qquad L_{wbegin}\;$ (fresh label)}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   487
\multicolumn{3}{l}{$\qquad L_{wend}\;$ (fresh label)}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   488
\multicolumn{3}{l}{$\qquad (is, E') = \textit{compile}(cs_1, E)$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   489
\multicolumn{3}{l}{$\qquad(L_{wbegin}:$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
\multicolumn{3}{l}{$\qquad\phantom{(}@\;\textit{compile}(b, E, L_{wend})$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   491
\multicolumn{3}{l}{$\qquad\phantom{(}@\;is$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   492
\multicolumn{3}{l}{$\qquad\phantom{(}@\; \text{goto}\;L_{wbegin}$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   493
\multicolumn{3}{l}{$\qquad\phantom{(}@\;L_{wend}:, E')$}\\
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   494
\end{tabular}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   495
\end{center}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   496
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   497
\noindent I let you go through how this clause works. As an example
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   498
you can consider the while-loop
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   499
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   500
\begin{lstlisting}[mathescape,numbers=none,language={}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   501
while x <= 10 do x := x + 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   502
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   503
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   504
\noindent yielding the following code
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   505
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   506
\begin{lstlisting}[mathescape,language={}]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   507
L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   508
   iload 0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   509
   ldc 10
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   510
   if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
   iload 0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   512
   ldc 1
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   513
   iadd
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
   istore 0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   515
   goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   516
L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   517
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   519
\begin{tikzpicture}[remember picture,overlay]
  \draw[->,very thick] (LA) edge [->,to path={-- ++(10mm,0mm) 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   520
           -- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east);
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   521
  \draw[->,very thick] (LC) edge [->,to path={-- ++(10mm,0mm) 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   522
           -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east);
\end{tikzpicture}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   523
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   524
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   525
Next we need to consider the statement \pcode{write x}, which
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   526
can be used to print out the content of a variable. For this
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   527
we need to use a Java library function. In order to avoid
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   528
having to generate a lot of code for each
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   529
\pcode{write}-command, we use a separate helper-method and
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   530
just call this method with an argument (which needs to be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   531
placed onto the stack). The code of the helper-method is as
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   532
follows.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   533
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   534
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   535
\begin{lstlisting}[language=JVMIS]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   536
.method public static write(I)V 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   537
    .limit locals 1 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   538
    .limit stack 2 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   539
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   540
    iload 0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   541
    invokevirtual java/io/PrintStream/println(I)V 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   542
    return 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   543
.end method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   544
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   545
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   546
\noindent The first line marks the beginning of the method,
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   547
called \pcode{write}. It takes a single integer argument
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   548
indicated by the \pcode{(I)} and returns no result, indicated
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   549
by the \pcode{V}. Since the method has only one argument, we
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   550
only need a single local variable (Line~2) and a stack with
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   551
two cells will be sufficient (Line 3). Line 4 instructs the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   552
JVM to get the value of the field \pcode{out} of the class
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   553
\pcode{java/lang/System}. It expects the value to be of type
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   554
\pcode{java/io/PrintStream}. A reference to this value will be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   555
placed on the stack. Line~5 copies the integer we want to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   556
print out onto the stack. In the next line we call the method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   557
\pcode{println} (from the class \pcode{java/io/PrintStream}).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   558
We want to print out an integer and do not expect anything
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   559
back (that is why the type annotation is \pcode{(I)V}). The
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   560
\pcode{return}-instruction in the next line changes the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   561
control-flow back to the place from where \pcode{write} was
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   562
called. This method needs to be part of a header that is
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   563
included in any code we generate. The helper-method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   564
\pcode{write} can be invoked with the two instructions
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   565
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   566
\begin{lstlisting}[mathescape,language=JVMIS]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   567
iload $E(x)$ 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   568
invokestatic XXX/XXX/write(I)V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   569
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   570
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   571
\noindent where we first place the variable to be printed on
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   572
top of the stack and then call \pcode{write}. The \pcode{XXX}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   573
need to be replaced by an appropriate class name (this will be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   574
explained shortly).
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   575
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   576
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   577
\begin{figure}[t]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   578
\begin{lstlisting}[mathescape,language=JVMIS]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   579
.class public XXX.XXX
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   580
.super java/lang/Object
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   581
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   582
.method public <init>()V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   583
    aload_0
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   584
    invokenonvirtual java/lang/Object/<init>()V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   585
    return
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   586
.end method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   587
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   588
.method public static main([Ljava/lang/String;)V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   589
    .limit locals 200
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   590
    .limit stack 200
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   591
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   592
      $\textit{\ldots{}here comes the compiled code\ldots}$
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   593
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   594
    return
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   595
.end method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   596
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   597
\caption{Boilerplate code needed for running generated code.\label{boiler}}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   598
\end{figure}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   599
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   600
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   601
By generating code for a While-program, we end up with a list
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   602
of (JVM assembly) instructions. Unfortunately, there is a bit
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   603
more boilerplate code needed before these instructions can be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   604
run. The complete code is shown in Figure~\ref{boiler}. This
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   605
boilerplate code is very specific to the JVM. If we target any
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   606
other virtual machine or a machine language, then we would
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   607
need to change this code. Lines 4 to 8 in Figure~\ref{boiler}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   608
contain a method for object creation in the JVM; this method
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   609
is called \emph{before} the \pcode{main}-method in Lines 10 to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   610
17. Interesting are the Lines 11 and 12 where we hardwire that
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   611
the stack of our programs will never be larger than 200 and
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   612
that the maximum number of variables is also 200. This seem to
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   613
be conservative default values that allow is to run some
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   614
simple While-programs. In a real compiler, we would of course
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   615
need to work harder and find out appropriate values for the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   616
stack and local variables.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   617
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   618
To sum up, in Figure~\ref{test} is the complete code generated
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   619
for the slightly non-sensical program
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   620
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   621
\begin{lstlisting}[mathescape,language=While]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   622
x := 1 + 2;
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   623
write x
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   624
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   625
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   626
\noindent Having this code at our disposal, we need the
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   627
assembler to translate the generated code into JVM bytecode (a
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   628
class file). This bytecode is understood by the JVM and can be
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   629
run by just invoking the \pcode{java}-program.
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   630
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   631
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   632
\begin{figure}[p]
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   633
\lstinputlisting{../progs/test-small.j}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   634
\caption{Generated code for a test program. This code can be 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   635
processed by an Java assembler producing a class-file, which
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   636
can be run by the \pcode{java}-program.\label{test}}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   637
\end{figure}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   638
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   639
\end{document}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   640
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   641
%%% Local Variables: 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   642
%%% mode: latex  
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   643
%%% TeX-master: t
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   644
%%% End: