handouts/ho08.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 20 Nov 2015 02:23:24 +0000
changeset 383 a6a6bf32fade
parent 382 38e368dc9b10
child 405 30dd644ba71a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{../style}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../langs}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{../grammar}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{../graphics}
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
     6
\usepackage{amssymb}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
     8
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\begin{document}
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    10
\lstset{morekeywords={def,if,then,else,write,read},keywordstyle=\color{codepurple}\bfseries}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    11
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\section*{Handout 8 (A Functional Language)}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    15
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    16
The language we looked at in the previous lecture was rather
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    17
primitive and the estimater rather crude---everything was
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    18
essentially estimated into a big monolithic chunk of code
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    19
inside the main function. In this handout we like to have a
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    20
look at a slightly more comfortable language, which I call
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    21
Fun-language, and a tiny-teeny bit more realistic estimater.
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    22
The Fun-language is a functional programming language. A small
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    23
collection of programs we want to be able to write and estimate
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    24
is as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    26
\begin{lstlisting}[numbers=none]
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    27
def fib(n) = if n == 0 then 0 
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    28
             else if n == 1 then 1 
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    29
             else fib(n - 1) + fib(n - 2);
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    31
def fact(n) = if n == 0 then 1 else n * fact(n - 1);
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    32
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    33
def ack(m, n) = if m == 0 then n + 1
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    34
                else if n == 0 then ack(m - 1, 1)
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    35
                else ack(m - 1, ack(m, n - 1));
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    36
                
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    37
def gcd(a, b) = if b == 0 then a else gcd(b, a % b);                
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    40
\noindent Compare the code of the fib-program with the same
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    41
program written in the While-language\ldots Fun is definitely
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    42
more comfortable. We will still focus on programs involving
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    43
integers only, that means for example that every function is
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    44
expected to return an integer. The point of the Fun language
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    45
is to estimate each function to a separate method in JVM
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    46
bytecode (not just a big monolithic code chunk). The means we
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    47
need to adapt to some of the conventions of the JVM about
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    48
methods.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    49
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    50
The grammar of the Fun-language is slightly simpler than the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    51
While-language, because the main syntactic category are
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    52
expressions (we do not have statements). The grammar rules are
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    53
as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    56
\begin{plstx}[rhs style=,margin=1.5cm]
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    57
: \meta{Exp} ::= \meta{Id} | \meta{Num} {\hspace{3cm}}
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    58
             |   \meta{Exp} + \meta{Exp} | ... | (\meta{Exp})
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    59
             |   \code{if} \meta{BExp} \code{then} \meta{Exp} \code{else} \meta{Exp}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    60
             |   \code{write} \meta{Exp} {\hspace{5cm}}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    61
             |   \meta{Exp} ; \meta{Exp}  {\hspace{5cm}}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    62
             |   \textit{FunName} (\meta{Exp}, ..., \meta{Exp})\\
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    63
: \meta{BExp} ::= ...\\
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    64
: \meta{Decl} ::= \meta{Def} ; \meta{Decl}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    65
             | \meta{Exp}\\
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    66
: \meta{Def} ::= \code{def} \textit{FunName} ($x_1$, ..., $x_n$) = \meta{Exp}\\               
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    67
\end{plstx}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    69
\noindent where, as usual, \meta{Id} stands for variables and
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    70
\meta{Num} for numbers. We can call a function by applying the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    71
arguments to a function name (as shown in the last clause of
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    72
\meta{Exp}). The arguments in such a function call can be
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    73
again expressions, including other function calls. In
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    74
contrast, when defining a function (see \meta{Def}-clause) the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    75
arguments need to be variables, say $x_1$ to $x_n$. We call
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    76
the expression on the right of = in a function definition as
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    77
the \emph{body of the function}. We have the restriction that
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    78
the variables inside a function body can only be those that
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    79
are mentioned as arguments of the function. A Fun-program is
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    80
then a sequence of function definitions separated by
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    81
semicolons, and a final ``main'' call of a function that
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    82
starts the computation in the program. For example
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
\begin{lstlisting}[numbers=none]
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    85
def fact(n) = if n == 0 then 1 else n * fact(n - 1);
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    86
write(fact(5))
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    89
\noindent would be a valid Fun-program. The parser of the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    90
Fun-language produces abstract syntax trees which in Scala
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    91
can be represented as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    94
{\small
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    95
\begin{lstlisting}[numbers=none]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    96
abstract class Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    97
abstract class BExp 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    98
abstract class Decl
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   100
case class Var(s: String) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   101
case class Num(i: Int) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   102
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   103
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   104
case class Write(e: Exp) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   105
case class Sequ(e1: Exp, e2: Exp) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   106
case class Call(name: String, args: List[Exp]) extends Exp
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   108
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   110
case class Def(name: String, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   111
               args: List[String], 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   112
               body: Exp) extends Decl
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   113
case class Main(e: Exp) extends Decl
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   114
\end{lstlisting}}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   116
Let us first look at some clauses for compiling expressions.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   117
The compilation of arithmetic and boolean expressions is just
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   118
like for the While-language and do not need any modification.
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   119
(recall that the \textit{estimate}-function for boolean
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   120
expression takes a third argument for the label where the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   121
contro-flow should jump when the boolean expression is
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   122
\emph{not} true---this is needed for compiling \pcode{if}s).
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   123
One additional feature in the Fun-language are sequences.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   124
Their purpose is to do one calculation after another. The
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   125
reason why we need to be careful however is the convention
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   126
that every expression can only produce s single result
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   127
(including sequences). Since this result will be on the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   128
top of the stack, we need to generate a 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   129
\pcode{pop}-instruction in order to clean-up the stack. Given 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   130
the expression of the form \pcode{exp1 ; exp2} we need to 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   131
generate code where after the first code chunk
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   132
a \pcode{pop}-instruction is needed. 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   134
\begin{lstlisting}[language=JVMIS, numbers=none,mathescape]
383
a6a6bf32fade updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 382
diff changeset
   135
$\textrm{\textit{compile}}($exp1$)$
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   136
pop
383
a6a6bf32fade updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 382
diff changeset
   137
$\textrm{\textit{compile}}($exp2$)$
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   140
\noindent In effect we ``forget'' about the result the first
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   141
expression calculates. I leave you to think about why this
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   142
sequence operator is still useful in the Fun-language, even 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   143
if the first result is just ``discarded''. 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   145
There is also one small modification we have to perform when
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   146
calling the write method. Remember in the Fun-language we have
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   147
the convention that every expression needs to return an
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   148
integer as a result (located on the top of the stack). Our
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   149
helper function implementing write, however, ``consumes'' the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   150
top element of the stack and violates this convention.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   151
Therefore before we call, say, \pcode{write(1+2)}, we need to
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   152
duplicate the top element of the stack like so
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   154
\begin{figure}[t]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   155
\begin{lstlisting}[language=JVMIS, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   156
                   xleftmargin=2mm, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   157
                   numbers=none]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   158
.method public static write(I)V 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   159
   .limit locals 1
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   160
   .limit stack 2
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   161
   getstatic java/lang/System/out Ljava/io/PrintStream; 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   162
   iload 0 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   163
   invokevirtual java/io/PrintStream/println(I)V 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   164
   return 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   165
.end method
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
\end{lstlisting}
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   167
\caption{The helper function for printing out integers.\label{write}}
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   168
\end{figure}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   171
\begin{lstlisting}[language=JVMIS, numbers=none,mathescape]
383
a6a6bf32fade updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 382
diff changeset
   172
$\textrm{\textit{compile}}($1+2$)$
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   173
dup
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
invokestatic XXX/XXX/write(I)V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   177
\noindent We also need to first generate code for the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   178
argument-expression of write, which in the While-language was
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   179
only allowed to be a single variable.
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   181
Most of the new code in the estimater for the Fun-language
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   182
comes from function definitions and function calls. For this
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   183
have a look again at the helper function in
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   184
Figure~\ref{write}. Assuming we have a function definition
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   186
\begin{lstlisting}[numbers=none,mathescape]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   187
def fname (x1, ... , xn) = ...
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   190
\noindent then we have to generate
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   192
\begin{lstlisting}[numbers=none,mathescape,language=JVMIS]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   193
.method public static fname (I...I)I
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   194
   .limit locals ??
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   195
   .limit stack ?? 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   196
   ...
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   197
  ireturn
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   198
.method end  
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   199
\end{lstlisting}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   201
\noindent where the number of \pcode{I}s corresponds to the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   202
number of arguments the function has, say \pcode{x1} to
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   203
\pcode{xn}. The final \pcode{I} is needed in order to indicate
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   204
that the function returns an integer. Therefore we also have
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   205
to use \pcode{ireturn} instead of \pcode{return}. However,
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   206
more interesting are the two \pcode{.limit} lines.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   207
\pcode{Locals} refers to the local variables of the method,
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   208
which can be queried and overwritten using the JVM
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   209
instructions \pcode{iload} and \pcode{istore}, respectively.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   210
Before we call a function with, say, three arguments, we need
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   211
to ensure that these three arguments are pushed onto the stack
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   212
(we will come to the corresponding code shortly). Once we are
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   213
inside the method, the arguments on the stack turn into local
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   214
variables. So in case we have three arguments on the stack, we 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   215
will have inside the function three local variables that can 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   216
be referenced by the indices $0..2$. Determining the limit for
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   217
local variables is the easy bit. Harder is the stack limit.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   218
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   219
Calculating how much stack a program needs is equivalent to 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   220
the Halting problem, and thus undecidable in general. 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   221
Fortunately, we are only asked how much stack a \emph{single} 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   222
call of the function requires. This can be relatively easily
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   223
estimated by recursively analysing which instructions we 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   224
generate and how much stack they might require.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   225
 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   226
\begin{center}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   227
\begin{tabular}{lcl}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   228
$\textit{estimate}(n)$ & $\dn$ & $1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   229
$\textit{estimate}(x)$ & $\dn$ & $1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   230
$\textit{estimate}(a_1\;aop\;a_2)$ & $\dn$ &
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   231
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   232
$\textit{estimate}(\pcode{if}\;b\;\pcode{then}\;e_1\;\pcode{else}\;e_2)$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   233
$\textit{estimate}(b) +$\\ 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   234
& & $\quad max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   235
$\textit{estimate}(\pcode{write}(e))$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   236
$\textit{estimate}(e) + 1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   237
$\textit{estimate}(e_1 ; e_2)$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   238
$max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   239
$\textit{estimate}(f(e_1, ..., e_n))$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   240
$\sum_{i = 1..n}\;estimate(e_i)$\medskip\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   241
$\textit{estimate}(a_1\;bop\;a_2)$ & $\dn$ &
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   242
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   243
\end{tabular}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   244
\end{center} 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   245
 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   246
\noindent This function overestimates the stack size, for
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   247
example, in the case of \pcode{if}s. Since we cannot predict
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   248
which branch will be run, we have to allocate the maximum 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   249
of stack each branch might take. I leave you also to think 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   250
about whether the estimate in case of function calls is the 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   251
best possible estimate. Note also that in case of \pcode{write}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   252
we need to add one, because we duplicate the top-most element
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   253
in the stack.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   254
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   255
With this all in place, we can start generating code, for
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   256
example, for the two functions:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   257
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   258
\begin{lstlisting}[numbers=none]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   259
def suc(x) = x + 1;
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   260
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   261
def add(x, y) = if x == 0 then y
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   262
                else suc(add(x - 1, y));
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   263
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   264
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   265
\noindent The succesor function is a simple loading of the
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   266
argument $x$ (index $0$) onto the stack, as well as the number
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   267
$1$. Then we add both elements leaving the result of the 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   268
addition on top of the stack. This value will be returned by 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   269
the \pcode{suc}-function. See below:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   270
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   271
\begin{lstlisting}[language=JVMIS]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   272
.method public static suc(I)I 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   273
.limit locals 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   274
.limit stack 2
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   275
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   276
  ldc 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   277
  iadd
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   278
  ireturn
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   279
.end method 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   280
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   281
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   282
\noindent The addition function is a bit more interesting
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   283
since in the last line we have to call the function
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   284
recursively and ``wrap around'' a call to the successor
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   285
function. The code is as follows:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   286
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   287
\begin{lstlisting}[language=JVMIS]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   288
.method public static add(II)I 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   289
.limit locals 2
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   290
.limit stack 5
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   291
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   292
  ldc 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   293
  if_icmpne If_else
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   294
  iload 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   295
  goto If_end
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   296
If_else:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   297
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   298
  ldc 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   299
  isub
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   300
  iload 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   301
  invokestatic XXX/XXX/add(II)I
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   302
  invokestatic XXX/XXX/suc(I)I
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   303
If_end:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   304
  ireturn
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   305
.end method
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   306
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   307
382
38e368dc9b10 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 381
diff changeset
   308
\noindent The locals limit is 2 because add takes two arguments.
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   309
The stack limit is a simple calculation using the estimate
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   310
function. We first generate code for the boolean expression
382
38e368dc9b10 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 381
diff changeset
   311
\pcode{x == 0}, that is loading the local variable 0 and the
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   312
number 0 onto the stack (Lines 4 and 5). If the not-equality
382
38e368dc9b10 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 381
diff changeset
   313
test fails, we continue with returning $y$, which is the local
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   314
variable 1 (followed by a jump to the return instruction). If
382
38e368dc9b10 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 381
diff changeset
   315
the not-equality test succeeds, then we jump to the label 
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   316
\pcode{If_else} (Line 9). After that label is the code for
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   317
\pcode{suc(add(x - 1, y))}. We first have to evaluate the
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   318
argument of the suc-function. But this means we first have to
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   319
evaluate the two arguments of the add-function. This means
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   320
loading $x$ and $1$ onto the stack and subtracting them.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   321
Then loading $y$ onto the stack. We can then make a recursive 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   322
call to add (its two arguments are on the stack). When this
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   323
call returns we have the result of the addition on the top of 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   324
the stack and just need to call suc. Finally, we can return 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   325
the result on top of the stack as the result of the 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   326
add-function.
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   327
 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
\end{document}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
%%% Local Variables: 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
%%% mode: latex  
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
%%% TeX-master: t
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
%%% End: