handouts/ho09.tex
author Christian Urban <urbanc@in.tum.de>
Tue, 28 Jan 2020 12:23:53 +0000
changeset 709 c112a6cb5e52
parent 704 6d9c960a2b26
child 722 14914b57e207
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}
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
     7
%%\usepackage{multicol}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
     9
%%\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}
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
\begin{document}
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
    12
\fnote{\copyright{} Christian Urban, King's College London, 2019}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
    15
\section*{Handout 9 (LLVM, SSA and CPS)}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    17
Reflecting on our two tiny compilers targetting the JVM, the code
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    18
generation part was actually not so hard, no? Pretty much just some
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    19
post-traversal of the abstract syntax tree, yes? One of the reasons for
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    20
this ease is that the JVM is a stack-based virtual machine and it is
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    21
therefore not hard to translate deeply-nested arithmetic expressions
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    22
into a sequence of instructions manipulating the stack. The problem is
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    23
that ``real'' CPUs, although supporting stack operations, are not really
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    24
designed to be \emph{stack machines}.  The design of CPUs is more like,
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    25
here is a chunk of memory---compiler, or better compiler writers, do
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    26
something with it. Consequently, modern compilers need to go the extra
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    27
mile in order to generate code that is much easier and faster to process
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    28
by CPUs. To make this all tractable for this module, we target the LLVM
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    29
Intermediate Language. In this way we can take advantage of the tools
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    30
coming with LLVM. For example we do not have to worry about things like
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    31
register allocations.\bigskip 
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    33
\noindent LLVM\footnote{\url{http://llvm.org}} is a beautiful example
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    34
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
    35
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
    36
Illinois at Urbana-Champaign. At the time the behemoth of compilers was
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    37
gcc with its myriad of front-ends for other languages (C++, Fortran,
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    38
Ada, Go, Objective-C, Pascal etc). The problem was that gcc morphed over
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    39
time into a monolithic gigantic piece of m\ldots ehm software, which you
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    40
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
    41
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
    42
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
    43
the original developers (I cannot remember the reason why Apple did not
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    44
want to use gcc, but maybe they were also just disgusted by its big
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    45
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
    46
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
    47
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
    48
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    49
We will target the LLVM Intermediate Language, or LLVM Intermediate
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    50
Representation (short LLVM-IR). The LLVM-IR looks very similar to the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    51
assembly language of Jasmin and Krakatau. It will also allow us to
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    52
benefit from the modular structure of the LLVM compiler and let for
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    53
example the compiler generate code for different CPUs, like X86 or ARM.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    54
That means we can be agnostic about where our code actually runs. We can
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    55
also be ignorant about optimising code and allocating memory
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    56
efficiently. 
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    58
However, what we have to do for LLVM is to generate code in \emph{Static
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    59
Single-Assignment} format (short SSA), because that is what the LLVM-IR
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    60
expects from us. A reason why LLVM uses the SSA format, rather than
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    61
JVM-like stack instructions, is that stack instructions are difficult to
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    62
optimise---you cannot just re-arrange instructions without messing about
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    63
with what is calculated on the stack. Also it is hard to find out if all
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    64
the calculations on the stack are actually necessary and not by chance
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    65
dead code. The JVM has for all these obstacles sophisticated machinery
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    66
to make such ``high-level'' code still run fast, but let's say that for
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    67
the sake of argument we do not want to rely on it. We want to generate
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    68
fast code ourselves. This means we have to work around the intricacies
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    69
of what instructions CPUs can actually process fast. This is what the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    70
SSA format is designed for.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    71
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    72
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    73
The main idea behind the SSA format is to use very simple variable
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    74
assignments where every variable is assigned only once. The assignments
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    75
also need to be primitive in the sense that they can be just simple
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    76
operations like addition, multiplication, jumps, comparisons and so on.
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    77
Say, we have an expression $((1 + a) + (3 + (b * 5)))$, then the
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    78
corresponding SSA format is 
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    79
 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    80
\begin{lstlisting}[language=LLVMIR,numbers=left]
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    81
let tmp0 = add 1 a in   
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    82
let tmp1 = mul b 5 in 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    83
let tmp2 = add 3 tmp1 in 
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    84
let tmp3 = add tmp0 tmp2 in tmp3 
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
    85
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    87
\noindent where every variable is used only once (we could not write
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
    88
\texttt{tmp1 = add 3 tmp1} in Line 3 for example).  There are
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    89
sophisticated algorithms for imperative languages, like C, that
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    90
efficiently transform a high-level program into SSA format. But we can
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    91
ignore them here. We want to compile a functional language and there
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    92
things get much more interesting than just sophisticated. We will need
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    93
to have a look at CPS translations, where the CPS stands for
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    94
Continuation-Passing-Style---basically black programming art or
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    95
abracadabra programming. So sit tight.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
    97
\subsection*{LLVM-IR}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
    99
Before we start, let's first have a look at the \emph{LLVM Intermediate
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   100
Representation} in more detail. The LLVM-IR is in between the frontends
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   101
and backends of the LLVM framework. It allows compilation of multiple
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   102
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
   103
the target independent optimisations are performed. 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   104
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   105
What is good about our toy Fun language is that it basically only
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   106
contains expressions (be they arithmetic expressions, boolean
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   107
expressions or if-expressions). The exception are function definitions.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   108
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
   109
LLVM-IR (this is similar to using JVM methods for functions in our
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   110
earlier compiler). For example the simple Fun program 
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   113
\begin{lstlisting}[language=Scala,numbers=none]
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   114
def sqr(x) = x * x
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   115
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   117
\noindent
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   118
can be compiled to the following LLVM-IR function:
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   120
\begin{lstlisting}[language=LLVM]
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   121
define i32 @sqr(i32 %x) {
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   122
   %tmp = mul i32 %x, %x
677
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   123
   ret i32 %tmp
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   124
}    
decfd8cf8180 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
   125
\end{lstlisting}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   127
\noindent First notice that all variable names, in this case \texttt{x}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   128
and \texttt{tmp}, are prefixed with \texttt{\%} in the LLVM-IR.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   129
Temporary variables can be named with an identifier, such as
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   130
\texttt{tmp}, or numbers. Function names, since they are ``global'',
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   131
need to be prefixed with @-symbol. Also, the LLVM-IR is a fully typed
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   132
language. The \texttt{i32} type stands for 32-bit integers. There are
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   133
also types for 64-bit integers (\texttt{i64}), chars (\texttt{i8}),
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   134
floats, arrays and even pointer types. In the code above, \texttt{sqr}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   135
takes an argument of type \texttt{i32} and produces a result of type
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   136
\texttt{i32} (the result type is in front of the function name, like in
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   137
C). Each arithmetic operation, for example addition and multiplication,
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   138
are also prefixed with the type they operate on. Obviously these types
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   139
need to match up\ldots{} but since we have in our programs only
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   140
integers, \texttt{i32} everywhere will do. We do not have to generate
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   141
any other types, but obviously this is a limitation in our Fun language.
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
 
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   143
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
   144
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
   145
kerfuffle we had to go through with boolean expressions and negating the
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   146
condition? In the LLVM-IR, branching  if-conditions is implemented
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   147
differently: there is a separate \texttt{br}-instruction as follows:
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   148
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   149
\begin{lstlisting}[language=LLVM]
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   150
br i1 %var, label %if_br, label %else_br
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   151
\end{lstlisting}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   152
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   153
\noindent
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   154
The type \texttt{i1} stands for booleans. If the variable is true, then
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   155
this instruction jumps to the if-branch, which needs an explicit label;
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   156
otherwise to the else-branch, again with its own label. This allows us
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   157
to keep the meaning of the boolean expression as is when compiling if's.
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   158
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
   159
\texttt{icmp}-instruction. This instruction is for integers (hence the
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   160
\texttt{i}) and takes the comparison operation as argument. For example
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   161
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   162
\begin{lstlisting}[language=LLVM]
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   163
icmp eq i32  %x, %y     ; for equal
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   164
icmp sle i32 %x, %y     ;   signed less or equal
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   165
icmp slt i32 %x, %y     ;   signed less than
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   166
icmp ult i32 %x, %y     ;   unsigned less than 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   167
\end{lstlisting}
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   168
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   169
\noindent
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   170
In some operations, the LLVM-IR distinguishes between signed and 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   171
unsigned representations of integers.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   172
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   173
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
   174
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
   175
instruction \texttt{call} and can also assign the result to 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   176
a variable. The syntax is as follows
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   177
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   178
\begin{lstlisting}[language=LLVM]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   179
%var = call i32 @foo(...args...)
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   180
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   181
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   182
\noindent
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   183
where the arguments can only be simple variables, not compound
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   184
expressions.
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   185
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   186
Conveniently, you can use the program \texttt{lli}, which comes with
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   187
LLVM, to interpret programs written in the LLVM-IR. So you can easily
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   188
check whether the code you produced actually works. To get a running
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   189
program that does something interesting you need to add some boilerplate
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   190
about printing out numbers and a main-function that is the entry point
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   191
for the program (see Figure~\ref{lli} for a complete listing). Again
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   192
this is very similar to the boilerplate we needed to add in our JVM
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   193
compiler. 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   194
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   195
You can generate a binary for the program in Figure~\ref{lli} by using
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   196
the \texttt{llc}-compiler and then \texttt{gcc}, whereby \texttt{llc} generates
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   197
an object file and \texttt{gcc} (that is clang) generates the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   198
executable binary:
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   199
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   200
\begin{lstlisting}[language=bash,numbers=none]
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   201
llc -filetype=obj sqr.ll
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   202
gcc sqr.o -o a.out
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   203
./a.out
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   204
> 25
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   205
\end{lstlisting}
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   206
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   207
\begin{figure}[t]\small 
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   208
\lstinputlisting[language=LLVM,numbers=left]{../progs/sqr.ll}
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   209
\caption{An LLVM-IR program for calculating the square function. It
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   210
calls this function in \texttt{@main} with the argument \texttt{5}. The
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   211
code for the \texttt{sqr} function is in Lines 13 -- 16. The main
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   212
function calls \texttt{sqr} and then prints out the result. The other
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   213
code is boilerplate for printing out integers.\label{lli}}
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   214
\end{figure}   
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   215
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   216
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   217
    
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   218
\subsection*{Our Own Intermediate Language}
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   219
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   220
Remember compilers have to solve the problem of bridging the gap between
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   221
``high-level'' programs and ``low-level'' hardware. If the gap is too
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   222
wide for one step, then a good strategy is to lay a stepping stone
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   223
somewhere in between. The LLVM-IR itself is such a stepping stone to
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   224
make the task of generating and optimising code easier. Like a real
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   225
compiler we will use our own stepping stone which I call the
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   226
\emph{K-language}. For what follows recall the various kinds of
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   227
expressions in the Fun language. For convenience the Scala code of the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   228
corresponding abstract syntax trees is shown on top of
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   229
Figure~\ref{absfun}. Below is the code for the abstract syntax trees in
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   230
the K-language. In K, here are two kinds of syntactic entities, namely
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   231
\emph{K-values} and \emph{K-expressions}. The central constructor of the
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   232
K-language is \texttt{KLet}. For this recall in SSA that arithmetic
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   233
expressions such as $((1 + a) + (3 + (b * 5)))$ need to be broken up
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   234
into smaller ``atomic'' steps, like so
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   235
 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   236
\begin{lstlisting}[language=LLVMIR,numbers=none]
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   237
let tmp0 = add 1 a in   
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   238
let tmp1 = mul b 5 in 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   239
let tmp2 = add 3 tmp1 in 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   240
let tmp3 = add tmp0 tmp2 in
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   241
  tmp3 
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   242
\end{lstlisting}
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   243
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   244
\noindent
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   245
Here \texttt{tmp3} will contain the result of what the whole expression
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   246
stands for. In each individual step we can only perform an ``atomic''
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   247
operation, like addition or multiplication of a number and a variable.
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   248
We are not allowed to have for example an if-condition on the right-hand
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   249
side of an equals. Such constraints are enforced upon us because of how
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   250
the SSA format works in the LLVM-IR. By having in \texttt{KLet} taking
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   251
first a string (standing for an intermediate result) and second a value,
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   252
we can fulfil this constraint ``by construction''---there is no way we
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   253
could write anything else than a value. 
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   254
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   255
To sum up, K-values are the atomic operations that can be on the
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   256
right-hand side of equal-signs. The K-language is restricted such that
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   257
it is easy to generate the SSA format for the LLVM-IR. 
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   258
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   259
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   260
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   261
\begin{figure}[p]\small
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   262
\begin{lstlisting}[language=Scala,numbers=none]
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   263
// Fun language (expressions)
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   264
abstract class Exp 
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   265
abstract class BExp 
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   266
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   267
case class Call(name: String, args: List[Exp]) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   268
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   269
case class Write(e: Exp) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   270
case class Var(s: String) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   271
case class Num(i: Int) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   272
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   273
case class Sequence(e1: Exp, e2: Exp) extends Exp
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   274
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp  
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   275
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   276
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   277
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   278
// K-language (K-expressions, K-values)
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   279
abstract class KExp
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   280
abstract class KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   281
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   282
case class KVar(s: String) extends KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   283
case class KNum(i: Int) extends KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   284
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   285
case class KCall(o: String, vrs: List[KVal]) extends KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   286
case class KWrite(v: KVal) extends KVal
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   287
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   288
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp
680
eecc4d5a2172 updated
Christian Urban <urbanc@in.tum.de>
parents: 679
diff changeset
   289
case class KLet(x: String, v: KVal, e: KExp) extends KExp
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   290
case class KReturn(v: KVal) extends KExp
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   291
\end{lstlisting}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   292
\caption{Abstract syntax trees for the Fun language.\label{absfun}}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   293
\end{figure}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   294
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   295
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   296
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   297
\subsection*{CPS-Translations}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   298
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   299
CPS stands for Continuation-Passing-Style. It is a kind of programming
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   300
technique often used in advanced functional programming. Before we delve
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   301
into the CPS-translation for our Fun language, let us look at 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   302
CPS-versions of some well-known functions. Consider
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   303
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   304
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   305
def fact(n: Int) : Int = 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   306
  if (n == 0) 1 else n * fact(n - 1) 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   307
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   308
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   309
\noindent 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   310
This is clearly the usual factorial function. But now consider the
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   311
following version of the factorial function:
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   312
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   313
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   314
def factC(n: Int, ret: Int => Int) : Int = 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   315
  if (n == 0) ret(1) 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   316
  else factC(n - 1, x => ret(n * x)) 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   317
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   318
factC(3, identity)
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   319
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   320
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   321
\noindent
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   322
This function is called with the number, in this case 3, and the
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   323
identity-function (which returns just its input). The recursive
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   324
calls are:
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   325
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   326
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   327
factC(2, x => identity(3 * x))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   328
factC(1, x => identity(3 * (2 * x)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   329
factC(0, x => identity(3 * (2 * (1 * x))))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   330
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   331
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   332
\noindent
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   333
Having reached 0, we get out of the recursion and apply 1 to the
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   334
continuation (see if-branch above). This gives
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   335
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   336
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   337
identity(3 * (2 * (1 * 1)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   338
= 3 * (2 * (1 * 1))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   339
= 6
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   340
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   341
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   342
\noindent
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   343
which is the expected result. If this looks somewhat familiar, then this
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   344
is not a 1000 miles off, because functions with continuations can be
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   345
seen as a kind of generalisation of tail-recursive functions. Anyway
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   346
notice how the continuations is ``stacked up'' during the recursion and
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   347
then ``unrolled'' when we apply 1 to the continuation. Interestingly, we
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   348
can do something similar to the Fibonacci function where in the traditional
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   349
version we have two recursive calls. Consider the following function
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   350
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   351
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   352
def fibC(n: Int, ret: Int => Int) : Int = 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   353
  if (n == 0 || n == 1) ret(1) 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   354
  else fibC(n - 1,
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   355
             r1 => fibC(n - 2,
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   356
               r2 => ret(r1 + r2)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   357
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   358
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   359
\noindent
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   360
Here the continuation is a nested function essentially wrapping up 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   361
the second recursive call. Let us check how the recursion unfolds
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   362
when called with 3 and the identity function:
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   363
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   364
\begin{lstlisting}[language=Scala, numbers=none]
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   365
fibC(3, id)
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   366
fibC(2, r1 => fibC(1, r2 => id(r1 + r2)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   367
fibC(1, r1 => 
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   368
   fibC(0, r2 => fibC(1, r2a => id((r1 + r2) + r2a))))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   369
fibC(0, r2 => fibC(1, r2a => id((1 + r2) + r2a)))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   370
fibC(1, r2a => id((1 + 1) + r2a))
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   371
id((1 + 1) + 1)
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   372
(1 + 1) + 1
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   373
3
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   374
\end{lstlisting}
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   375
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   376
Let us now come back to the CPS-translations for the Fun language. The
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   377
main difficulty of generating instructions in SSA format is that large
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   378
compound expressions need to be broken up into smaller pieces and
700
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   379
intermediate results need to be chained into later instructions. To do
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   380
this conveniently, CPS-translations have been developed. They use
52263ffd17b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   381
functions (``continuations'') to represent what is coming next in a
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   382
sequence of instructions. Continuations are functions of type
704
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   383
\code{KVal} to \code{KExp}. They can be seen as a sequence of
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   384
\code{KLet}s where there is a ``hole'' that needs to be filled. Consider
6d9c960a2b26 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   385
for example
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 677
diff changeset
   386
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   387
\begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   388
let tmp0 = add 1 a in   
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   389
let tmp1 = mul (*@$\Box$@*) 5 in 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   390
let tmp2 = add 3 tmp1 in 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   391
let tmp3 = add tmp0 tmp2 in
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   392
  tmp3 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   393
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   394
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   395
\noindent
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   396
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
   397
to be filled in before it becomes a ``proper'' \code{KExp}. When we 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   398
apply and argument to the continuation (remember they are functions)
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   399
we essentially fill something into the corresponding hole. The code
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   400
of the CPS-translation is 
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   401
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   402
\begin{lstlisting}[language=Scala,numbers=none]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   403
def CPS(e: Exp)(k: KVal => KExp) : KExp = 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   404
  e match { ... }
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   405
\end{lstlisting}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   406
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   407
\noindent 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   408
where \code{k} is the continuation and \code{e} is the expression 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   409
to be compiled. In case we have numbers or variables, we can just
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   410
apply the continuation like 
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   411
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   412
\begin{center}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   413
\code{k(KNum(n))} \qquad \code{k(KVar(x))}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   414
\end{center}
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   415
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   416
\noindent This would just fill in the $\Box$ in a \code{KLet}-expression.
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   417
More interesting is the case for an arithmetic expression.
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   418
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   419
\begin{lstlisting}[language=Scala,numbers=none]
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   420
case Aop(o, e1, e2) => {
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   421
    val z = Fresh("tmp")
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   422
    CPS(e1)(y1 => 
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   423
      CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   424
}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   425
\end{lstlisting}
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   426
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 700
diff changeset
   427
\noindent
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
\end{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
%%% Local Variables: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
%%% mode: latex
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
%%% TeX-master: t
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
%%% End: