handouts/ho08.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 18 Nov 2015 01:53:01 +0000
changeset 380 1e88390e81aa
parent 379 fa2589ec0fae
child 381 47eceea734c5
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{../style}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../langs}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{../grammar}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{../graphics}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
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
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    17
primitive and the compiler rather crude---everything was
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    18
essentially compiled into a big monolithic chunk of code
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
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    21
Fun-language, and a tiny-teeny bit more realistic compiler.
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
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    23
collection of programs we want to be able to write and compile
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
379
fa2589ec0fae updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 378
diff changeset
    26
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
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
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
    45
is to compile each function to a separate method in JVM
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.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   119
(recall that the \textit{compile}-function for boolean
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]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   135
$\textrm{\textit{compile}}($exp1$)$
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   136
pop
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
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]
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   172
$\textrm{\textit{compile}}($1+2$)$
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
380
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   181
Most of the new code in the compiler for the Fun-language
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,
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   206
more interesting are the two \pcode{.limit} lines. Locals
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   207
refers to the local variables of the method, which can be
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   208
queried and overwritten using \pcode{iload} and
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   209
\pcode{istore}, respectively.
1e88390e81aa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 379
diff changeset
   210
 
378
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
\end{document}
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
%%% Local Variables: 
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
%%% mode: latex  
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
%%% TeX-master: t
e8ac05fe2630 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
%%% End: