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