handouts/ho09.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 05 Jun 2023 10:50:36 +0100
changeset 912 e32802acf952
parent 909 a04efdd5e7a3
child 913 eef6a56c185a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
     1
% !TEX program = xelatex
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage{../style}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
\usepackage{../langs}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\usepackage{../graphics}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\usepackage{../grammar}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\begin{document}
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
     9
\fnote{\copyright{} Christian Urban, King's College London, 2019, 2023}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    12
% CPS translations
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    13
% https://felleisen.org/matthias/4400-s20/lecture17.html
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    14
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    15
%% pattern matching resources
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    16
%% https://www.reddit.com/r/ProgrammingLanguages/comments/g1vno3/beginner_resources_for_compiling_pattern_matching/
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 704
diff changeset
    17
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
    18
\section*{Handout 9 (LLVM, SSA and CPS)}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    20
Reflecting on our two tiny compilers targetting the JVM, the code
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    21
generation part was actually not so hard, no? Pretty much just some
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    22
post-traversal of the abstract syntax tree, yes? One of the reasons
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    23
for this ease is that the JVM is a stack-based virtual machine and it
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    24
is therefore not hard to translate deeply-nested arithmetic
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    25
expressions into a sequence of instructions manipulating the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    26
stack. The problem is that ``real'' CPUs, although supporting stack
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    27
operations, are not really designed to be \emph{stack machines}.  The
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    28
design of CPUs is more like: Here are some instructions and a chunk of
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    29
memory---compiler, or better compiler writers, do something with
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    30
them. Consequently, modern compilers need to go the extra mile in
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    31
order to generate code that is much easier and faster to process by
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    32
actual CPUs, rather than running code on virtual machines that
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    33
introduce an additional layer of indirection. To make this all
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    34
tractable for this module, we target the LLVM Intermediate
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    35
Language. In this way we can take advantage of the tools coming with
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    36
LLVM.\footnote{\url{http://llvm.org}} For example we do not have to
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    37
worry about things like register allocations. By using the LLVM-IR,
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    38
however, we also have to pay price in the sense that generating code
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    39
gets harder\ldots{}unfor\-tunately.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    41
\subsection*{LLVM and the LLVM-IR}
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    42
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    43
\noindent LLVM is a beautiful example
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    44
that projects from Academia can make a difference in the World. LLVM
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    45
started in 2000 as a project by two researchers at the  University of
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    46
Illinois at Urbana-Champaign. At the time the behemoth of compilers was
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    47
gcc with its myriad of front-ends for different programming languages (C++, Fortran,
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    48
Ada, Go, Objective-C, Pascal etc). The problem was that gcc morphed over
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    49
time into a monolithic gigantic piece of m\ldots ehm complicated
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    50
software, which you
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    51
could not mess about in an afternoon. In contrast, LLVM is designed to
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    52
be a modular suite of tools with which you can play around easily and
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    53
try out something new. LLVM became a big player once Apple hired one of
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    54
the original developers (I cannot remember the reason why Apple did not
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    55
want to use gcc, but maybe they were also just disgusted by gcc's big
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    56
monolithic codebase). Anyway, LLVM is now the big player and gcc is more
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    57
or less legacy. This does not mean that programming languages like C and
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    58
C++ are dying out any time soon---they are nicely supported by LLVM.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    60
We will target the LLVM Intermediate Language, or LLVM Intermediate
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    61
Representation (short LLVM-IR). The LLVM-IR looks very similar to the
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    62
assembly language of Jasmin and Krakatau. Targetting LLVM-IR will also
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    63
allow us to benefit from the modular structure of the LLVM compiler
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    64
and we can let the compiler generate code for different CPUs, for
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    65
example X86 or ARM.  That means we can be agnostic about where our
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    66
code is actually going to run.\footnote{Anybody want to try to run our
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    67
  programs on Android phones?}  We can also be somewhat ignorant about
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    68
optimising our code and about allocating memory efficiently---the LLVM
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    69
tools will take care of all this.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    71
However, what we have to do in order to make LLVM to play ball is to
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    72
generate code in \emph{Static Single-Assignment} format (short SSA). A
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    73
reason why LLVM uses the SSA-format, rather than JVM-like stack
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    74
instructions, is that stack instructions are difficult to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    75
optimise---you cannot just re-arrange instructions without messing
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    76
about with what is calculated on the stack. Also it is hard to find
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    77
out if all the calculations on the stack are actually necessary and
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    78
not by chance dead code. The JVM has for all these obstacles
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    79
sophisticated machinery to make such ``high-level'' code still run
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    80
fast, but let's say that for the sake of argument we do not want to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    81
rely on it. We want to generate fast code ourselves. This means we
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    82
have to work around the intricacies of what instructions CPUs can
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
    83
actually process fast. This is what the SSA format is designed for.
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    84
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    85
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    86
The main idea behind the SSA-format is to have sequences of very
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    87
simple variable assignments where every (tmp)variable is assigned only
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    88
once. The assignments need to be simple in the sense that they can be
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    89
just be primitive operations like addition, multiplication, jumps,
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    90
comparisons, function calls and so on.  Say, we have an expression
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    91
$((1 + a) + (foo(3 + 2) + (b * 5)))$, then the corresponding sequence
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
    92
of assignments in SSA-format are:
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    93
 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    94
\begin{lstlisting}[language=LLVMIR,numbers=left]
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    95
let tmp0 = add 1 a in
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    96
let tmp1 = add 3 2 in  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    97
let tmp2 = call foo(tmp1)  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    98
let tmp3 = mul b 5 in 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    99
let tmp4 = add tmp2 tmp3 in 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   100
let tmp5 = add tmp0 tmp4 in
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   101
  return tmp5 
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   102
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   104
\noindent where every tmpX-variable is used only once (we could, for
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   105
example, not write \texttt{tmp1 = add tmp2 tmp3} in Line 5 even if
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   106
this would not change the overall result). At the end we have a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   107
return-instruction wich contains the final result of the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   108
expression. As can be seen, the task we have to solve is to take apart
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   109
compound expressions as shown above and transfrom them into a sequence
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   110
of simple assignments. Note that this for example means we have to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   111
fix the order in which the expression is calculated. 
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   112
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   113
There are sophisticated algorithms for imperative languages, like C,
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   114
that efficiently transform high-level programs into SSA-format. But
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   115
we can ignore them here. We want to compile a functional language and
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   116
there things get much more interesting than just sophisticated. We
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   117
will need to have a look at CPS translations, where the CPS stands for
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   118
\emph{Continuation-Passing-Style}---basically black programming art or
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   119
abracadabra programming. So sit tight.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   121
\subsection*{LLVM-IR}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   123
Before we start, let's however first have a look at the \emph{LLVM Intermediate
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   124
Representation} in more detail. The LLVM-IR is in between the frontends
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   125
and backends of the LLVM framework. It allows compilation of multiple
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   126
source languages to multiple targets. It is also the place where most of
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   127
the target independent optimisations are performed. 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   128
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   129
What is good about our toy Fun-language is that it basically only
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   130
contains expressions (be they arithmetic expressions, boolean
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   131
expressions or if-expressions). The exception are function definitions.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   132
Luckily, for them we can use the mechanism of defining functions in the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   133
LLVM-IR (this is similar to using JVM methods for functions in our
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   134
earlier compiler). For example the simple Fun-program 
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   137
\begin{lstlisting}[language=Scala,numbers=none]
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   138
def sqr(x) = x * x
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   139
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   141
\noindent
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   142
can be compiled to the following LLVM-IR function:
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   144
\begin{lstlisting}[language=LLVM]
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   145
define i32 @sqr(i32 %x) {
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   146
   %tmp = mul i32 %x, %x
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   147
   ret i32 %tmp
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   148
}    
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   149
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   151
\noindent First notice that all ``local'' variable names, in this case
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   152
\texttt{x} and \texttt{tmp}, are prefixed with \texttt{\%} in the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   153
LLVM-IR.  Temporary variables can be named with an identifier, such as
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   154
\texttt{tmp}, or numbers. In contrast, function names, since they are
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   155
``global'', need to be prefixed with an @-symbol. Also, the LLVM-IR is
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   156
a fully typed language. The \texttt{i32} type stands for 32-bit
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   157
integers. There are also types for 64-bit integers (\texttt{i64}),
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   158
chars (\texttt{i8}), floats, arrays and even pointer types. In the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   159
code above, \texttt{sqr} takes an argument of type \texttt{i32} and
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   160
produces a result of type \texttt{i32} (the result type is in front of
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   161
the function name, like in C). Each arithmetic operation, for example
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   162
addition and multiplication, are also prefixed with the type they
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   163
operate on. Obviously these types need to match up\ldots{} but since
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   164
we have in our programs only integers, for the moment \texttt{i32}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   165
everywhere will do. We do not have to generate any other types, but
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   166
obviously this is a limitation in our Fun-language (and which we
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   167
are going to lift in the final CW).
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
 
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   169
There are a few interesting instructions in the LLVM-IR which are quite
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   170
different than what we have seen in the JVM. Can you remember the
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   171
kerfuffle we had to go through with boolean expressions and negating the
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   172
condition? In the LLVM-IR, branching  if-conditions are implemented
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   173
differently: there is a separate \texttt{br}-instruction as follows:
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   174
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   175
\begin{lstlisting}[language=LLVM]
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   176
br i1 %var, label %if_br, label %else_br
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   177
\end{lstlisting}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   178
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   179
\noindent
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   180
The type \texttt{i1} stands for booleans. If the variable is true,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   181
then this instruction jumps to the if-branch, which needs an explicit
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   182
label; otherwise it jumps to the else-branch, again with its own
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   183
label. This allows us to keep the meaning of the boolean expression
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   184
``as is'' when compiling if's---thanks god no more negating of
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   185
booleans.
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   186
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   187
A value of type boolean is generated in the LLVM-IR by the
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   188
\texttt{icmp}-instruction. This instruction is for integers (hence the
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   189
\texttt{i}) and takes the comparison operation as argument. For example
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   190
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   191
\begin{lstlisting}[language=LLVM]
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   192
icmp eq  i32 %x, %y     ; for equal
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   193
icmp sle i32 %x, %y     ; signed less or equal
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   194
icmp slt i32 %x, %y     ; signed less than
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   195
icmp ult i32 %x, %y     ; unsigned less than 
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   196
\end{lstlisting}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   197
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   198
\noindent
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   199
Note that in some operations the LLVM-IR distinguishes between signed and 
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   200
unsigned representations of integers. I assume you know what this means,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   201
otherwise just ask.
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   202
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   203
It is also easy to call another function in LLVM-IR: as can be 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   204
seen from Figure~\ref{lli} we can just call a function with the 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   205
instruction \texttt{call} and can also assign the result to 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   206
a variable. The syntax is as follows
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   207
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   208
\begin{lstlisting}[language=LLVM]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   209
%var = call i32 @foo(...args...)
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   210
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   211
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   212
\noindent
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   213
where the arguments can only be simple variables and numbers, but not compound
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   214
expressions.
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   215
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   216
Conveniently, you can use the program \texttt{lli}, which comes with
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   217
LLVM, to interpret programs written in the LLVM-IR. Type on the command line
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   218
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   219
\begin{lstlisting}[language=bash,numbers=none]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   220
lli sqr.ll
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   221
\end{lstlisting}
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   222
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   223
\noindent
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   224
and you can see the output of the pragram generated. 
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   225
This means you can easily check whether the code you produced actually
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   226
works. To get a running program that does something interesting you
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   227
need to add some boilerplate about printing out numbers and a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   228
main-function that is the entry point for the program (see
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   229
Figure~\ref{lli} for a complete listing of the square function). Again
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   230
this is very similar to the boilerplate we needed to add in our JVM
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   231
compiler.
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   232
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   233
You can generate a binary for the program in Figure~\ref{lli} by using
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   234
the \texttt{llc}-compiler and then \texttt{gcc}/\texttt{clang}, whereby \texttt{llc} generates
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   235
an object file and \texttt{gcc} (that is actually \texttt{clang} on my Mac) generates the
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   236
executable binary:
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   237
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   238
\begin{lstlisting}[language=bash,numbers=none]
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   239
llc -filetype=obj sqr.ll
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   240
gcc sqr.o -o a.out
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   241
./a.out
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   242
> 25
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   243
\end{lstlisting}
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   244
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   245
\begin{figure}[t]\small 
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   246
\lstinputlisting[language=LLVM,numbers=left]{../progs/sqr.ll}
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   247
\caption{An LLVM-IR program for calculating the square function. It
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   248
  calls the \texttt{sqr}-function in \texttt{@main} with the argument \texttt{5}
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   249
  (Line 20). The
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   250
  code for the \texttt{sqr}-function is in Lines 13 -- 16. The main-function
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   251
  stores
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   252
  the result of sqr in the variable called \texttt{\%1} and then
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   253
  prints out the contents of this variable in Line 21. The other
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   254
  code is boilerplate for printing out integers.\label{lli}}
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   255
\end{figure}   
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   256
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   257
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   258
    
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   259
\subsection*{Our Own Intermediate Language}
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   260
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   261
Let's get back to our compiler: Remember compilers have to solve the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   262
problem of bridging the gap between ``high-level'' programs and
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   263
``low-level'' hardware. If the gap is too wide for one step, then a
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   264
good strategy is to lay a stepping stone somewhere in between. The
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   265
LLVM-IR itself is such a stepping stone to make the task of generating
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   266
and optimising code easier. Like a real compiler we will use our own
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   267
stepping stone which I call the \emph{K-language}.
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   268
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   269
\begin{center}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   270
  \begin{tikzpicture}[scale=0.9,font=\bf,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   271
                      node/.style={
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   272
                      rectangle,rounded corners=3mm,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   273
                      ultra thick,draw=black!50,minimum height=16mm, 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   274
                      minimum width=20mm,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   275
                      top color=white,bottom color=black!20}]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   276
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   277
  %\node (0) at (-3,0) {};  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   278
  \node (A) at (0.0,0) [node,text width=1.6cm,text centered,label=above:{\small\it{}source}] {Fun-Language};
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   279
  \node (B) at (3.5,0) [node,text width=1.6cm,text centered,label=above:{\small\it{}stepping stone}] {K-Language};
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   280
  \node (C) at (7.0,0) [node,text width=1.6cm,text centered,label=above:{\small\it{}target}] {LLVM-IR};
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   281
 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   282
  \draw [->,line width=2.5mm] (A) -- node [above,pos=0.35] {} (B); 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   283
  \draw [->,line width=2.5mm] (B) -- node [above,pos=0.35] {} (C); 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   284
  \end{tikzpicture}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   285
  \end{center}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   286
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   287
  \noindent
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   288
  To see why we need a stepping stone for generating code in SSA-format,
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   289
  considder again arithmetic expressions such as
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   290
  $((1 + a) + (3 + (b * 5)))$. They need to be broken up into smaller
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   291
  ``atomic'' steps, like so
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   292
 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   293
\begin{lstlisting}[language=LLVMIR,numbers=none]
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   294
let tmp0 = add 1 a in   
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   295
let tmp1 = mul b 5 in 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   296
let tmp2 = add 3 tmp1 in 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   297
let tmp3 = add tmp0 tmp2 in
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   298
  return tmp3 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   299
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   300
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   301
\noindent
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   302
Here \texttt{tmp3} will contain the result of what the whole
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   303
expression stands for. In each individual step we can only perform an
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   304
``atomic'' or ``trival'' operation, like addition or multiplication of
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   305
a number or a variable.  We are not allowed to have for example a
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   306
nested addition or an if-condition on the right-hand side of an
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   307
assignment. Such constraints are forced upon us because of how the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   308
SSA-format works in the LLVM-IR. To simplify matters we represent
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   309
assignments with two kinds of syntactic entities, namely
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   310
\emph{K-values} and \emph{K-expressions}. K-values are all ``things''
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   311
that can appear on the right-hand side of an equal. Say we have the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   312
following definition for K-values:
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   313
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   314
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   315
enum KVal {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   316
  case KVar(s: String)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   317
  case KNum(n: Int)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   318
  case KAop(op: String, v1: KVal, v2: KVal)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   319
  case KCall(fname: String, args: List[KVal])
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   320
}
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   321
\end{lstlisting}
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   322
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   323
\noindent
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   324
where a K-value can be a variable, a number or a ``trivial'' binary
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   325
operation, such as addition or multiplication. The two arguments of a
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   326
binary operation need to be K-values as well. Finally, we have
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   327
function calls, but again each argument of the function call needs to
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   328
be a K-value.  One constraint we need to be careful about is that the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   329
arguments of the binary operations and function calls can only be
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   330
variables or numbers. For example
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   331
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   332
\begin{lstlisting}[language=Scala,numbers=none]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   333
KAop("+", KAop("*", KNum(1), KNum(2)), KNum(3))
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   334
KCall("foo", List(KAop("+", KNum(4), KNum(5)))  
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   335
\end{lstlisting}
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   336
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   337
\noindent
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   338
while perfect instances of K-values according to the types, are not
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   339
allowed in the LLVM-IR. To encode this constraint into the type-system
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   340
of Scala, however, would make things overly complicated---to satisfy
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   341
this constraint is therefore on us. For the moment this will be all
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   342
K-values we are considdering. Later on, we will have some more for our
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   343
Fun-language.
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   344
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   345
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   346
Our K-expressions will encode the \texttt{let} and the \texttt{return}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   347
from the SSA-format (again for the moment we only consider these two
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   348
constructors---later on we will have if-branches as well).
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   349
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   350
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   351
enum KExp {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   352
  case KLet(x: String, v: KVal, e: KExp)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   353
  case KReturn(v: KVal)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   354
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   355
\end{lstlisting}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   356
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   357
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   358
By having in \texttt{KLet} taking first a string (standing for a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   359
tmp-variable) and second a value, we can fulfil the SSA constraint in
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   360
assignments ``by con\-struction''---there is no way we could write
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   361
anything else than a K-value.  Note that the third argument of a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   362
\texttt{KLet} is again a K-expression, meaning either another
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   363
\texttt{KLet} or a \texttt{KReturn}. In this way we can construct a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   364
sequence of computations and indicate what is the final result of the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   365
computations.  According to the SSA-format, we also have to ensure
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   366
that all intermediary computations are given (fresh) names---we will
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   367
use an (ugly) counter for this.
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   368
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   369
To sum up, K-values are the atomic operations that can be on the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   370
right-hand side of assignemnts. The K-language is restricted such that
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   371
it is easy to generate the SSA-format for the LLVM-IR. What remains to
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   372
be done is a translation of our Fun-language into the K-language. The
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   373
main difficulty is that we need to order the computation---in the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   374
K-language we only have linear sequence of instructions. Before we
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   375
explain this, we have a look at some programs in CPS-style.
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   376
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   377
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   378
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   379
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   380
\subsection*{CPS-Translations}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   381
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   382
CPS stands for \emph{Continuation-Passing-Style}. It is a kind of
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   383
programming technique often used in advanced functional programming
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   384
and in particular in compilers. Before we delve into the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   385
CPS-translation for our Fun-language compiler, let us look at
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   386
CPS-versions of some well-known functions. Consider
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   387
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   388
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   389
def fact(n: Int) : Int = 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   390
  if (n == 0) 1 else n * fact(n - 1) 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   391
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   392
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   393
\noindent 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   394
This is clearly the usual factorial function. But now consider the
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   395
following slight variant of the factorial function:
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   396
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   397
\begin{lstlisting}[language=Scala, numbers=left]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   398
def factC(n: Int, k: Int => Int) : Int = 
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   399
  if (n == 0) k(1) 
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   400
  else factC(n - 1, x => k(n * x)) 
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   401
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   402
factC(3, id)
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   403
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   404
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   405
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   406
The function is is Lines 1--3. The function call is in Line 5.  We
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   407
call this function with a number, in this case 3, and the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   408
identity-function (which returns just its input). The \texttt{k} is
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   409
the continuation in this function. The recursive calls are:
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   410
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   411
\begin{lstlisting}[language=Scala, numbers=none]
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   412
factC(2, x => id(3 * x))
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   413
factC(1, x => id(3 * (2 * x)))
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   414
factC(0, x => id(3 * (2 * (1 * x))))
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   415
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   416
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   417
\noindent
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   418
Having reached 0, we get out of the recursion and apply 1 to the
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   419
continuation (see if-branch above in Line 2). This gives
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   420
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   421
\begin{lstlisting}[language=Scala, numbers=none]
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   422
id(3 * (2 * (1 * 1)))
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   423
= 3 * (2 * (1 * 1))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   424
= 6
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   425
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   426
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   427
\noindent
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   428
which is the expected result. If this looks somewhat familiar to you,
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   429
than this is because functions with continuations can be seen as a
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   430
kind of generalisation of tail-recursive functions.  So far, however,
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   431
we did not look at this generalisation in earnest.  Anyway notice how
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   432
the continuations is ``stacked up'' during the recursion and then
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   433
``unrolled'' when we apply 1 to the continuation. Interestingly, we
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   434
can do something similar to the Fibonacci function where in the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   435
traditional version we have two recursive calls. Consider the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   436
following function
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   437
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   438
\begin{lstlisting}[language=Scala, numbers=left]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   439
def fibC(n: Int, k: Int => Int) : Int = 
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   440
  if (n == 0 || n == 1) k(1) 
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   441
  else fibC(n - 1,
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   442
             r1 => fibC(n - 2,
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   443
               r2 => k(r1 + r2)))
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   444
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   445
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   446
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   447
Here the continuation (Lines 4--5) is a nested function essentially wrapping up 
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   448
the second recursive call plus the original continuation. Let us check how the recursion unfolds
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   449
when called with 3 and the identity function:
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   450
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   451
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   452
fibC(3, id)
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   453
fibC(2, r1 => fibC(1, r2 => id(r1 + r2)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   454
fibC(1, r1 => 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   455
   fibC(0, r2 => fibC(1, r2a => id((r1 + r2) + r2a))))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   456
fibC(0, r2 => fibC(1, r2a => id((1 + r2) + r2a)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   457
fibC(1, r2a => id((1 + 1) + r2a))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   458
id((1 + 1) + 1)
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   459
(1 + 1) + 1
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   460
3
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   461
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   462
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   463
\noindent
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   464
The point of this section is that you should be playing around with these
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   465
simple definitions and make sure they calculate the expected result.
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   466
In the next step, you should understand \emph{how} these functions
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   467
calculate the result with the continuations. Now change the initial
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   468
continuation which you call the function to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   469
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   470
\begin{lstlisting}[language=Scala, numbers=none]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   471
r => { println("The result plus 1 is:") ; r + 1 }
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   472
\end{lstlisting}
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   473
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   474
\noindent
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   475
Does this still calculate the expected result?
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   476
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   477
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   478
\section*{Worked Example}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   479
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   480
Let us now come back to the CPS-translations for the Fun-language.
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   481
Though we will start with a simpler subset containing only numbers,
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   482
arithmetic expressions and function calls.  The main difficulty of
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   483
generating instructions in SSA-format is that large compound
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   484
expressions need to be broken up into smaller pieces and intermediate
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   485
results need to be chained into later instructions. To do this
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   486
conveniently, we use the CPS-translation mechanism. What continuations
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   487
essentially solve is the following problem: Given an expressions
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   488
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   489
\begin{equation}\label{exp}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   490
(1 + 2) * (3 + 4)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   491
\end{equation}  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   492
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   493
\noindent
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   494
which of the subexpressions should be calculated first? We just
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   495
arbitrarily going to decide that the calculation will be from left to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   496
right. Other languages make different choices---C famously is right to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   497
left. In our case this means we have to tear the expression shown in
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   498
\eqref{exp} apart as follows:
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   499
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   500
\[
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   501
(1 + 2) \qquad\text{and}\qquad  \Box * (3 + 4)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   502
\]  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   503
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   504
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   505
The first subexpression can be easily calculated and will give us a result,
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   506
but the second one is not really an expression that makes sense. It
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   507
will only make sense as the next step in the computation when we
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   508
fill-in the result of $1+2$ into the ``hole'' indicated by
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   509
$\Box$. Such incomplete expressions can be represented in Scala as
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   510
functions
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   511
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   512
\begin{lstlisting}[language=Scala, numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   513
r => r * (3 + 4)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   514
\end{lstlisting}  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   515
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   516
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   517
where \texttt{r} will represent any result that has been computed
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   518
earlier. By the way, in lambda-calculus notation we would write
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   519
$\lambda r.\, r * (3 + 4)$ for the same function. To sum up, we use
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   520
functions (``continuations'') to represent what is coming next in a
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   521
sequence of instructions. In our case, continuations are functions of
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   522
type \code{KVal} to \code{KExp}. They can be seen as a sequence of
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   523
\code{KLet}s where there is a ``hole'' that needs to be
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   524
filled. Consider for example
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   525
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   526
\begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   527
let tmp0 = add 1 a in   
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   528
let tmp1 = mul (*@$\Box$@*) 5 in 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   529
let tmp2 = add 3 tmp1 in 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   530
let tmp3 = add tmp0 tmp2 in
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   531
  return tmp3 
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   532
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   533
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   534
\noindent
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   535
where in the second line is a $\Box$ which still expects a \code{KVal}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   536
to be filled in before it becomes a ``proper'' \code{KExp}. When we 
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   537
apply an argument to the continuation (remember they are functions)
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   538
we essentially fill something into the corresponding hole.
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   539
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   540
Lets look at some concrete code. To simplify matters, 
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   541
suppose our source language consists just of three kinds of expressions
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   542
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   543
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   544
enum Expr {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   545
    case Num(n: Int)
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   546
    case Aop(op: String, e1: Expr, e2: Expr)
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   547
    case Call(fname: String, args: List[Expr])
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   548
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   549
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   550
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   551
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   552
This allows us to generate SSA-instructions for expressions like
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   553
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   554
\[
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   555
1 + foo(bar(4 * 7), 3, id(12))  
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   556
\]
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   557
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   558
\noindent
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   559
The code of the CPS-translation
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   560
that generates these instructions is then of the form
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   561
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   562
\begin{lstlisting}[language=Scala,numbers=none]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   563
def CPS(e: Exp)(k: KVal => KExp) : KExp = 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   564
  e match { ... }
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   565
\end{lstlisting}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   566
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   567
\noindent 
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   568
where \code{k} is the continuation and \code{e} is the expression to
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   569
be compiled. The result of the function is a K-expression, which later
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   570
can be compiled into LLVM-IR code.
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   571
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   572
In case we have numbers, then we can just pass them in CPS-translation
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   573
to the continuations because numbers need not be further teared apart
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   574
as they are already primitive. Passing the number to the continuation
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   575
means we apply the continuation like
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   576
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   577
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   578
  case Num(i) => k(KNum(i))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   579
\end{lstlisting}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   580
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   581
\noindent This would fill in the $\Box$ in a \code{KLet}-expression.
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   582
Since \texttt{k} is a function from \texttt{KVar} to \texttt{KExp} we
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   583
also optain the correct type for CPS, namely \texttt{KExp}.  There is
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   584
no need to create a temporary variable for simple numbers.  More
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   585
interesting is the case for arithmetic operations.
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   586
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   587
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=0mm]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   588
case Aop(op, e1, e2) => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   589
  val z = Fresh("tmp")
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   590
  CPS(e1)(r1 => 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   591
    CPS(e2)(r2 => KLet(z, KAop(op, r1, r2), k(KVar(z)))))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   592
}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   593
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   594
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   595
\noindent
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   596
What we essentially have to do in this case is the following: compile
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   597
the subexpressions \texttt{e1} and \texttt{e2}. They will produce some
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   598
result that is stored in two temporary variables (assuming they are more
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   599
complicated than just numbers). We need to use these two temporary
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   600
variables and feed them into a new assignment of the form
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   601
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   602
\begin{lstlisting}[language=LLVMIR,numbers=none,escapeinside={(*@}{@*)}]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   603
let z = op (*@$\Box_\texttt{r1}$@*) (*@$\Box_\texttt{r2}$@*) in
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   604
...
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   605
\end{lstlisting}
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   606
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   607
\noindent
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   608
Note that this assignment has two ``holes'', one for the left
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   609
subexpression and one for the right subexpression. We can fill both
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   610
holes by calling the CPS function twice---this is a bit of the black
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   611
art in CPS. We can use the second call of CPS as the continuation
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   612
of the first call. The reason is that
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   613
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   614
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=0mm]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   615
r1 => CPS(e2)(r2 => KLet(z, KAop(op, r1, r2), k(KVar(z))))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   616
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   617
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   618
\noindent is a function from \code{KVal} to \code{KExp}, which we need
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   619
as type for the continuation. Once we created the assignment with the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   620
fresh temporary variable \texttt{z}, we need to ``communicate'' that
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   621
the result of the computation of the arithmetic expression is stored
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   622
in \texttt{z} to the computations that follow. In this way we apply
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   623
the continuation \texttt{k} with this new variable (essentially we are
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   624
plugging in a hole further down the line).  Hope this makes sense.
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   625
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   626
The last case we need to consider in our small expression language are
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   627
function calls. For them remember each argument of the function
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   628
call can in SSA-format only be a variable or a number.
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   629
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   630
\begin{lstlisting}[language=Scala,numbers=left,xleftmargin=0mm]
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   631
case Call(fname, args) => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   632
  def aux(args: List[Expr], vs: List[KVal]): KExp = args match {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   633
       case Nil => {
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   634
         val z = Fresh("tmp")
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   635
         KLet(z, KCall(fname, vs), k(KVar(z)))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   636
       }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   637
       case a::as => CPS(a)(r => aux(as, vs ::: List(r)))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   638
  }
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   639
  aux(args, Nil)  
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   640
}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   641
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   642
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   643
\noindent
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   644
For this case we introduce an auxiliary function that compiles first all
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   645
function arguments---remember in our source language we can have calls
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   646
such as $foo(3 + 4, g(3))$ where we first have to create temporary
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   647
variables (and computations) for each argument. Therefore \texttt{aux}
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   648
analyses the argument list from left to right. In case there is an
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   649
argument \texttt{a} on the front of the list (the case \texttt{a::as}
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   650
in Line 7), we call CPS recursively for the corresponding
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   651
subexpression. The temporary variable containing the result for this
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   652
argument we add to the end of the K-values we have already analysed
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   653
before. Again very conveniently we can use the recursive call to
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   654
\texttt{aux} as the continuation for the computations that
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   655
follow. When we reach the end of the argument list (the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   656
\texttt{Nil}-case in Lines 3--6), then we collect all the K-values
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   657
\texttt{v1} to \texttt{vn} and call the function in the K-language
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   658
like so
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   659
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   660
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   661
\begin{lstlisting}[language=LLVMIR,numbers=none,escapeinside={(*@}{@*)}]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   662
let z = call foo(v1,...,vn) in
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   663
...
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   664
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   665
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   666
\noindent
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   667
Again we need to communicate the result of the function call, namely the
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   668
fresh temporary variable \texttt{z}, to the rest of the computation. Therefore
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   669
we apply the continuation \texttt{k} with this variable.
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   670
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   671
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   672
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   673
\begin{figure}[p]\small
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   674
  \lstinputlisting[numbers=left,firstline=1,lastline=24]{../progs/fun/simple-cps.sc}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   675
  \hspace{10mm}...
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   676
  \lstinputlisting[numbers=left,firstline=32,firstnumber=32,lastline=51]{../progs/fun/simple-cps.sc}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   677
\caption{CPS-translation for a simple expression language.\label{cps}}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   678
\end{figure}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   679
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   680
The last question we need to answer in the code (see Figure~\ref{cps})
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   681
is how we start the CPS-translation function, or more precisely with
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   682
which continuation we should start CPS? It turns out that this initial
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   683
continuation will be the last one that is called. What should be the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   684
last step in the computation? Yes, we need to return the temporary
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   685
variable where the last result is stored (if it is a simple number,
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   686
then we can just return this number). Therefore we call CPS with the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   687
code
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   688
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   689
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   690
def CPSi(e: Expr) : KExp = CPS(e)(KReturn(_))
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   691
\end{lstlisting}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   692
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   693
\noindent
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   694
where we give the function \code{KReturn(_)} as the continuation. With
912
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   695
this we completed the translation of simple expressions into our
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   696
K-language. Play around with some more expressions and see how the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   697
CPS-translation generates the correct code. I know this is not easy to
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   698
follow code when you see it the first time.  Figure~\ref{absfun} gives
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   699
the complete datatypes for the ASTs of the Fun-language and the
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   700
K-values and K-expressions for the K-language. The complete
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   701
CPS-translation you can find in the code.
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   702
e32802acf952 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 909
diff changeset
   703
\section*{Next Steps}
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   704
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   705
Having obtained a K-expression, it is relatively straightforward to
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   706
generate a valid program for the LLVM-IR. We leave this to the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   707
attentive reader. What else can we do?  Well it should be relatively
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   708
easy to apply some common optimisations to the K-expressions. One
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   709
optimisations is called constant folding---for example if we have an
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   710
expression $3 + 4$ then we can replace it by just $5$ instead of
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   711
generating code to compute $5$. Now this information needs to be
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   712
propagated to the next computation step to see whether any further
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   713
constant foldings are possible. Another useful technique is common
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   714
subexpression elimination, meaning if you have twice a calculation of
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   715
a function $foo(a)$, then we want to call it only once and store the
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   716
result in a temporary variable that can be used instead of the second,
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   717
or third, call to $foo(a)$. Again I leave this to the attentive reader
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   718
to work out and implement.
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   719
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   720
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   721
\begin{figure}[p]\small
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   722
\begin{lstlisting}[language=Scala,numbers=none]
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   723
// Fun language (expressions)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   724
abstract class Exp 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   725
abstract class BExp 
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   726
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   727
case class Call(name: String, args: List[Exp]) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   728
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   729
case class Write(e: Exp) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   730
case class Var(s: String) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   731
case class Num(i: Int) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   732
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   733
case class Sequence(e1: Exp, e2: Exp) extends Exp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   734
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp  
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   735
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   736
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   737
// K-language (K-expressions, K-values)
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   738
abstract class KExp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   739
abstract class KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   740
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   741
case class KVar(s: String) extends KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   742
case class KNum(i: Int) extends KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   743
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   744
case class KCall(o: String, vrs: List[KVal]) extends KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   745
case class KWrite(v: KVal) extends KVal
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   746
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   747
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   748
case class KLet(x: String, v: KVal, e: KExp) extends KExp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   749
case class KReturn(v: KVal) extends KExp
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   750
\end{lstlisting}
909
a04efdd5e7a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 908
diff changeset
   751
\caption{Abstract syntax trees for the Fun-language and the K-language.\label{absfun}}
908
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   752
\end{figure}
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   753
0138618eff73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   754
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   755
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
   756
\noindent
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
\end{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
%%% Local Variables: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
%%% mode: latex
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
%%% TeX-master: t
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
%%% End: