45 time into a monolithic gigantic piece of m\ldots ehm software, which you |
45 time into a monolithic gigantic piece of m\ldots ehm software, which you |
46 could not mess about in an afternoon. In contrast, LLVM is designed to |
46 could not mess about in an afternoon. In contrast, LLVM is designed to |
47 be a modular suite of tools with which you can play around easily and |
47 be a modular suite of tools with which you can play around easily and |
48 try out something new. LLVM became a big player once Apple hired one of |
48 try out something new. LLVM became a big player once Apple hired one of |
49 the original developers (I cannot remember the reason why Apple did not |
49 the original developers (I cannot remember the reason why Apple did not |
50 want to use gcc, but maybe they were also just disgusted by its big |
50 want to use gcc, but maybe they were also just disgusted by gcc's big |
51 monolithic codebase). Anyway, LLVM is now the big player and gcc is more |
51 monolithic codebase). Anyway, LLVM is now the big player and gcc is more |
52 or less legacy. This does not mean that programming languages like C and |
52 or less legacy. This does not mean that programming languages like C and |
53 C++ are dying out any time soon---they are nicely supported by LLVM. |
53 C++ are dying out any time soon---they are nicely supported by LLVM. |
54 |
54 |
55 We will target the LLVM Intermediate Language, or LLVM Intermediate |
55 We will target the LLVM Intermediate Language, or LLVM Intermediate |
75 of what instructions CPUs can actually process fast. This is what the |
75 of what instructions CPUs can actually process fast. This is what the |
76 SSA format is designed for. |
76 SSA format is designed for. |
77 |
77 |
78 |
78 |
79 The main idea behind the SSA format is to use very simple variable |
79 The main idea behind the SSA format is to use very simple variable |
80 assignments where every variable is assigned only once. The assignments |
80 assignments where every tmp-variable is assigned only once. The assignments |
81 also need to be primitive in the sense that they can be just simple |
81 also need to be primitive in the sense that they can be just simple |
82 operations like addition, multiplication, jumps, comparisons and so on. |
82 operations like addition, multiplication, jumps, comparisons and so on. |
83 Say, we have an expression $((1 + a) + (3 + (b * 5)))$, then the |
83 Say, we have an expression $((1 + a) + (3 + (b * 5)))$, then the |
84 corresponding SSA format is |
84 corresponding SSA format is |
85 |
85 |
89 let tmp2 = add 3 tmp1 in |
89 let tmp2 = add 3 tmp1 in |
90 let tmp3 = add tmp0 tmp2 in tmp3 |
90 let tmp3 = add tmp0 tmp2 in tmp3 |
91 \end{lstlisting} |
91 \end{lstlisting} |
92 |
92 |
93 \noindent where every variable is used only once (we could not write |
93 \noindent where every variable is used only once (we could not write |
94 \texttt{tmp1 = add 3 tmp1} in Line 3 for example). There are |
94 \texttt{tmp1 = add 3 tmp1} in Line 3 for example). |
95 sophisticated algorithms for imperative languages, like C, that |
95 |
96 efficiently transform a high-level program into SSA format. But we can |
96 There are sophisticated algorithms for imperative languages, like C, |
97 ignore them here. We want to compile a functional language and there |
97 that efficiently transform a high-level program into SSA format. But |
98 things get much more interesting than just sophisticated. We will need |
98 we can ignore them here. We want to compile a functional language and |
99 to have a look at CPS translations, where the CPS stands for |
99 there things get much more interesting than just sophisticated. We |
|
100 will need to have a look at CPS translations, where the CPS stands for |
100 Continuation-Passing-Style---basically black programming art or |
101 Continuation-Passing-Style---basically black programming art or |
101 abracadabra programming. So sit tight. |
102 abracadabra programming. So sit tight. |
102 |
103 |
103 \subsection*{LLVM-IR} |
104 \subsection*{LLVM-IR} |
104 |
105 |
131 \end{lstlisting} |
132 \end{lstlisting} |
132 |
133 |
133 \noindent First notice that all variable names, in this case \texttt{x} |
134 \noindent First notice that all variable names, in this case \texttt{x} |
134 and \texttt{tmp}, are prefixed with \texttt{\%} in the LLVM-IR. |
135 and \texttt{tmp}, are prefixed with \texttt{\%} in the LLVM-IR. |
135 Temporary variables can be named with an identifier, such as |
136 Temporary variables can be named with an identifier, such as |
136 \texttt{tmp}, or numbers. Function names, since they are ``global'', |
137 \texttt{tmp}, or numbers. In contrast, function names, since they are ``global'', |
137 need to be prefixed with @-symbol. Also, the LLVM-IR is a fully typed |
138 need to be prefixed with an @-symbol. Also, the LLVM-IR is a fully typed |
138 language. The \texttt{i32} type stands for 32-bit integers. There are |
139 language. The \texttt{i32} type stands for 32-bit integers. There are |
139 also types for 64-bit integers (\texttt{i64}), chars (\texttt{i8}), |
140 also types for 64-bit integers (\texttt{i64}), chars (\texttt{i8}), |
140 floats, arrays and even pointer types. In the code above, \texttt{sqr} |
141 floats, arrays and even pointer types. In the code above, \texttt{sqr} |
141 takes an argument of type \texttt{i32} and produces a result of type |
142 takes an argument of type \texttt{i32} and produces a result of type |
142 \texttt{i32} (the result type is in front of the function name, like in |
143 \texttt{i32} (the result type is in front of the function name, like in |
143 C). Each arithmetic operation, for example addition and multiplication, |
144 C). Each arithmetic operation, for example addition and multiplication, |
144 are also prefixed with the type they operate on. Obviously these types |
145 are also prefixed with the type they operate on. Obviously these types |
145 need to match up\ldots{} but since we have in our programs only |
146 need to match up\ldots{} but since we have in our programs only |
146 integers, \texttt{i32} everywhere will do. We do not have to generate |
147 integers, for the moment \texttt{i32} everywhere will do. We do not have to generate |
147 any other types, but obviously this is a limitation in our Fun language. |
148 any other types, but obviously this is a limitation in our Fun language. |
148 |
149 |
149 There are a few interesting instructions in the LLVM-IR which are quite |
150 There are a few interesting instructions in the LLVM-IR which are quite |
150 different than what we have seen in the JVM. Can you remember the |
151 different than what we have seen in the JVM. Can you remember the |
151 kerfuffle we had to go through with boolean expressions and negating the |
152 kerfuffle we had to go through with boolean expressions and negating the |
157 \end{lstlisting} |
158 \end{lstlisting} |
158 |
159 |
159 \noindent |
160 \noindent |
160 The type \texttt{i1} stands for booleans. If the variable is true, then |
161 The type \texttt{i1} stands for booleans. If the variable is true, then |
161 this instruction jumps to the if-branch, which needs an explicit label; |
162 this instruction jumps to the if-branch, which needs an explicit label; |
162 otherwise to the else-branch, again with its own label. This allows us |
163 otherwise it jumps to the else-branch, again with its own label. This allows us |
163 to keep the meaning of the boolean expression as is when compiling if's. |
164 to keep the meaning of the boolean expression ``as is'' when compiling if's---thanks god no more negating the boolean. |
164 A value of type boolean is generated in the LLVM-IR by the |
165 A value of type boolean is generated in the LLVM-IR by the |
165 \texttt{icmp}-instruction. This instruction is for integers (hence the |
166 \texttt{icmp}-instruction. This instruction is for integers (hence the |
166 \texttt{i}) and takes the comparison operation as argument. For example |
167 \texttt{i}) and takes the comparison operation as argument. For example |
167 |
168 |
168 \begin{lstlisting}[language=LLVM] |
169 \begin{lstlisting}[language=LLVM] |
169 icmp eq i32 %x, %y ; for equal |
170 icmp eq i32 %x, %y ; for equal |
170 icmp sle i32 %x, %y ; signed less or equal |
171 icmp sle i32 %x, %y ; signed less or equal |
171 icmp slt i32 %x, %y ; signed less than |
172 icmp slt i32 %x, %y ; signed less than |
172 icmp ult i32 %x, %y ; unsigned less than |
173 icmp ult i32 %x, %y ; unsigned less than |
173 \end{lstlisting} |
174 \end{lstlisting} |
174 |
175 |
175 \noindent |
176 \noindent |
176 In some operations, the LLVM-IR distinguishes between signed and |
177 Note that in some operations the LLVM-IR distinguishes between signed and |
177 unsigned representations of integers. |
178 unsigned representations of integers. |
178 |
179 |
179 It is also easy to call another function in LLVM-IR: as can be |
180 It is also easy to call another function in LLVM-IR: as can be |
180 seen from Figure~\ref{lli} we can just call a function with the |
181 seen from Figure~\ref{lli} we can just call a function with the |
181 instruction \texttt{call} and can also assign the result to |
182 instruction \texttt{call} and can also assign the result to |
344 = 3 * (2 * (1 * 1)) |
345 = 3 * (2 * (1 * 1)) |
345 = 6 |
346 = 6 |
346 \end{lstlisting} |
347 \end{lstlisting} |
347 |
348 |
348 \noindent |
349 \noindent |
349 which is the expected result. If this looks somewhat familiar, then this |
350 which is the expected result. If this looks somewhat familiar to you, |
350 is not a 1000 miles off, because functions with continuations can be |
351 than this is because functions with continuations can be |
351 seen as a kind of generalisation of tail-recursive functions. Anyway |
352 seen as a kind of generalisation of tail-recursive functions. Anyway |
352 notice how the continuations is ``stacked up'' during the recursion and |
353 notice how the continuations is ``stacked up'' during the recursion and |
353 then ``unrolled'' when we apply 1 to the continuation. Interestingly, we |
354 then ``unrolled'' when we apply 1 to the continuation. Interestingly, we |
354 can do something similar to the Fibonacci function where in the traditional |
355 can do something similar to the Fibonacci function where in the traditional |
355 version we have two recursive calls. Consider the following function |
356 version we have two recursive calls. Consider the following function |
383 main difficulty of generating instructions in SSA format is that large |
384 main difficulty of generating instructions in SSA format is that large |
384 compound expressions need to be broken up into smaller pieces and |
385 compound expressions need to be broken up into smaller pieces and |
385 intermediate results need to be chained into later instructions. To do |
386 intermediate results need to be chained into later instructions. To do |
386 this conveniently, CPS-translations have been developed. They use |
387 this conveniently, CPS-translations have been developed. They use |
387 functions (``continuations'') to represent what is coming next in a |
388 functions (``continuations'') to represent what is coming next in a |
388 sequence of instructions. Continuations are functions of type |
389 sequence of instructions. In our case, continuations are functions of type |
389 \code{KVal} to \code{KExp}. They can be seen as a sequence of |
390 \code{KVal} to \code{KExp}. They can be seen as a sequence of |
390 \code{KLet}s where there is a ``hole'' that needs to be filled. Consider |
391 \code{KLet}s where there is a ``hole'' that needs to be filled. Consider |
391 for example |
392 for example |
392 |
393 |
393 \begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}] |
394 \begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}] |
399 \end{lstlisting} |
400 \end{lstlisting} |
400 |
401 |
401 \noindent |
402 \noindent |
402 where in the second line is a $\Box$ which still expects a \code{KVal} |
403 where in the second line is a $\Box$ which still expects a \code{KVal} |
403 to be filled in before it becomes a ``proper'' \code{KExp}. When we |
404 to be filled in before it becomes a ``proper'' \code{KExp}. When we |
404 apply and argument to the continuation (remember they are functions) |
405 apply an argument to the continuation (remember they are functions) |
405 we essentially fill something into the corresponding hole. The code |
406 we essentially fill something into the corresponding hole. The code |
406 of the CPS-translation is |
407 of the CPS-translation is |
407 |
408 |
408 \begin{lstlisting}[language=Scala,numbers=none] |
409 \begin{lstlisting}[language=Scala,numbers=none] |
409 def CPS(e: Exp)(k: KVal => KExp) : KExp = |
410 def CPS(e: Exp)(k: KVal => KExp) : KExp = |