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