handouts/ho08.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 03 Oct 2019 11:12:00 +0100
changeset 646 2abd285c66d1
parent 604 9e75249e96f2
child 693 605d971e98fd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
     1
% !TEX program = xelatex
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../style}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{../langs}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{../grammar}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\usepackage{../graphics}
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
     7
\usepackage{amssymb}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
405
30dd644ba71a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 383
diff changeset
     9
% modern compilers are different
30dd644ba71a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 383
diff changeset
    10
% https://channel9.msdn.com/Blogs/Seth-Juarez/Anders-Hejlsberg-on-Modern-Compiler-Construction
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    11
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
    12
%halting problem
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
    13
%https://dfilaretti.github.io/2017-04-30/halting-problem
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
    14
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
    15
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
\begin{document}
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    17
\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
    18
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
\section*{Handout 8 (A Functional Language)}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    22
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    23
The language we looked at in the previous lecture was rather
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
    24
primitive and the compiler rather crude---everything was
604
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
    25
essentially compiled 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
    26
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
    27
look at a slightly more comfortable language, which I call
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
    28
Fun-language, and a tiny-teeny bit more realistic compiler.
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    29
The Fun-language is a functional programming language. A small
604
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
    30
collection of programs we want to be able to write and compile
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    31
is as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
    33
\begin{lstlisting}[numbers=none]
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    34
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
    35
             else if n == 1 then 1 
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    36
             else fib(n - 1) + fib(n - 2);
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    38
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
    39
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    40
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
    41
                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
    42
                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
    43
                
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    44
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
    45
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    47
\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
    48
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
    49
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
    50
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
    51
expected to return an integer. The point of the Fun language
604
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
    52
is to compile 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
    53
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
    54
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
    55
methods.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    56
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    57
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
    58
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
    59
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
    60
as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    63
\begin{plstx}[rhs style=,margin=1.5cm]
600
d488a3e7b0ec updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
    64
: \meta{Exp} ::= \meta{Id} | \meta{Num} {\hspace{3.7cm}}
d488a3e7b0ec updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
    65
             |   \meta{Exp} + \meta{Exp} | ... | (\meta{Exp}) {\hspace{3.7cm}}
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    66
             |   \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
    67
             |   \code{write} \meta{Exp} {\hspace{5cm}}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    68
             |   \meta{Exp} ; \meta{Exp}  {\hspace{5cm}}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    69
             |   \textit{FunName} (\meta{Exp}, ..., \meta{Exp})\\
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    70
: \meta{BExp} ::= ...\\
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    71
: \meta{Decl} ::= \meta{Def} ; \meta{Decl}
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    72
             | \meta{Exp}\\
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    73
: \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
    74
\end{plstx}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    76
\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
    77
\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
    78
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
    79
\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
    80
again expressions, including other function calls. In
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    81
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
    82
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
    83
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
    84
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
    85
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
    86
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
    87
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
    88
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
    89
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
    90
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
\begin{lstlisting}[numbers=none]
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    92
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
    93
write(fact(5))
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    96
\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
    97
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
    98
can be represented as follows:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   101
{\small
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   102
\begin{lstlisting}[numbers=none]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   103
abstract class Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   104
abstract class BExp 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   105
abstract class Decl
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   107
case class Var(s: String) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   108
case class Num(i: Int) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   109
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
   110
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
   111
case class Write(e: Exp) extends Exp
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   112
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
   113
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
   114
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   115
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
   116
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   117
case class Def(name: String, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   118
               args: List[String], 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   119
               body: Exp) extends Decl
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   120
case class Main(e: Exp) extends Decl
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   121
\end{lstlisting}}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   123
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
   124
The compilation of arithmetic and boolean expressions is just
604
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
   125
like for the While-language and do not need any modification
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
   126
(recall that the \textit{compile}-function for boolean
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   127
expression takes a third argument for the label where the
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   128
control-flow should jump when the boolean expression is
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   129
\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
   130
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
   131
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
   132
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
   133
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
   134
(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
   135
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
   136
\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
   137
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
   138
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
   139
a \pcode{pop}-instruction is needed. 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   141
\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
   142
$\textrm{\textit{compile}}($exp1$)$
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   143
pop
383
a6a6bf32fade updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 382
diff changeset
   144
$\textrm{\textit{compile}}($exp2$)$
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   147
\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
   148
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
   149
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
   150
if the first result is just ``discarded''. 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   152
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
   153
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
   154
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
   155
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
   156
helper function implementing write, however, ``consumes'' the
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   157
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
   158
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
   159
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
   160
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   161
\begin{figure}[t]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   162
\begin{lstlisting}[language=JVMIS, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   163
                   xleftmargin=2mm, 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   164
                   numbers=none]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   165
.method public static write(I)V 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   166
   .limit locals 1
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   167
   .limit stack 2
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   168
   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
   169
   iload 0 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   170
   invokevirtual java/io/PrintStream/println(I)V 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   171
   return 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   172
.end method
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
\end{lstlisting}
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   174
\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
   175
\end{figure}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   178
\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
   179
$\textrm{\textit{compile}}($1+2$)$
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   180
dup
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
invokestatic XXX/XXX/write(I)V
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   184
\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
   185
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
   186
only allowed to be a single variable.
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   188
Most of the new code in the compiler for the Fun-language
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   189
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
   190
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
   191
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
   192
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   193
\begin{lstlisting}[numbers=none,mathescape]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   194
def fname (x1, ... , xn) = ...
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
\end{lstlisting}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   197
\noindent then we have to generate
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   199
\begin{lstlisting}[numbers=none,mathescape,language=JVMIS]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   200
.method public static fname (I...I)I
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   201
   .limit locals ??
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   202
   .limit stack ?? 
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   203
   ...
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   204
  ireturn
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   205
.method end  
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   206
\end{lstlisting}
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   208
\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
   209
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
   210
\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
   211
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
   212
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
   213
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
   214
\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
   215
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
   216
instructions \pcode{iload} and \pcode{istore}, respectively.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   217
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
   218
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
   219
(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
   220
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
   221
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
   222
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
   223
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
   224
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
   225
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   226
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
   227
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
   228
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
   229
call of the function requires. This can be relatively easily
604
9e75249e96f2 updated
Christian Urban <urbanc@in.tum.de>
parents: 603
diff changeset
   230
compiled by recursively analysing which instructions we 
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   231
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
   232
 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   233
\begin{center}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   234
\begin{tabular}{lcl}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   235
$\textit{estimate}(n)$ & $\dn$ & $1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   236
$\textit{estimate}(x)$ & $\dn$ & $1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   237
$\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
   238
$\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
   239
$\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
   240
$\textit{estimate}(b) +$\\ 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   241
& & $\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
   242
$\textit{estimate}(\pcode{write}(e))$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   243
$\textit{estimate}(e) + 1$\\
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   244
$\textit{estimate}(e_1 ; e_2)$ & $\dn$ & 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   245
$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
   246
$\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
   247
$\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
   248
$\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
   249
$\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
   250
\end{tabular}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   251
\end{center} 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   252
 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   253
\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
   254
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
   255
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
   256
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
   257
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
   258
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
   259
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
   260
in the stack.
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   261
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   262
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
   263
example, for the two functions:
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
\begin{lstlisting}[numbers=none]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   266
def suc(x) = x + 1;
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   267
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   268
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
   269
                else suc(add(x - 1, y));
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   270
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   271
603
155430aea517 updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   272
\noindent The successor function is a simple loading of the
381
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   273
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
   274
$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
   275
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
   276
the \pcode{suc}-function. See below:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   277
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   278
\begin{lstlisting}[language=JVMIS]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   279
.method public static suc(I)I 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   280
.limit locals 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   281
.limit stack 2
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   282
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   283
  ldc 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   284
  iadd
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   285
  ireturn
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   286
.end method 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   287
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   288
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   289
\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
   290
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
   291
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
   292
function. The code is as follows:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   293
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   294
\begin{lstlisting}[language=JVMIS]
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   295
.method public static add(II)I 
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   296
.limit locals 2
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   297
.limit stack 5
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   298
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   299
  ldc 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   300
  if_icmpne If_else
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   301
  iload 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   302
  goto If_end
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   303
If_else:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   304
  iload 0
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   305
  ldc 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   306
  isub
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   307
  iload 1
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   308
  invokestatic XXX/XXX/add(II)I
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   309
  invokestatic XXX/XXX/suc(I)I
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   310
If_end:
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   311
  ireturn
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   312
.end method
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   313
\end{lstlisting}
47eceea734c5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 380
diff changeset
   314
382
38e368dc9b10 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 381
diff changeset
   315
\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
   316
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
   317
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
   318
\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
   319
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
   320
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
   321
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
   322
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
   323
\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
   324
\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
   325
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
   326
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
   327
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
   328
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
   329
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
   330
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
   331
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
   332
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
   333
add-function.
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   334
 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
\end{document}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
%%% Local Variables: 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
%%% mode: latex  
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
%%% TeX-master: t
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
%%% End: