author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 03 Dec 2021 17:45:11 +0000 | |
changeset 853 | 568671822d52 |
parent 836 | a3418ee8c404 |
child 855 | 1c0a684567d7 |
permissions | -rw-r--r-- |
630 | 1 |
% !TEX program = xelatex |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\documentclass{article} |
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
3 |
\usepackage{../style} |
820 | 4 |
\usepackage{../graphics} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
214
diff
changeset
|
5 |
\usepackage{../langs} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
836 | 9 |
\section*{Coursework 5} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
722 | 11 |
|
12 |
||
836 | 13 |
\noindent This coursework is worth 25\% and is due on \cwFIVE{} at |
820 | 14 |
18:00. You are asked to implement a compiler targeting the LLVM-IR. |
15 |
Be careful that this CW needs some material about the LLVM-IR |
|
16 |
that has not been shown in the lectures and your own experiments |
|
17 |
might be required. You can find information about the LLVM-IR at |
|
18 |
||
19 |
\begin{itemize} |
|
20 |
\item \url{https://bit.ly/3rheZYr} |
|
21 |
\item \url{https://llvm.org/docs/LangRef.html} |
|
22 |
\end{itemize} |
|
23 |
||
24 |
\noindent |
|
25 |
You can do the implementation of your compiler in any programming |
|
748 | 26 |
language you like, but you need to submit the source code with which |
820 | 27 |
you generated the LLVM-IR files, otherwise a mark of 0\% will be |
853 | 28 |
awarded. You are asked to submit the code of your compiler, but also |
29 |
the generated \texttt{.ll} files. You should use the lexer and parser |
|
30 |
from the previous courseworks, but you need to make some modifications |
|
31 |
to them for the `typed' fun-language. I will award up to 5\% if a |
|
32 |
lexer and a parser are correctly implemented. At the end, please |
|
33 |
package everything(!) in a zip-file that creates a directory with the |
|
34 |
name |
|
35 |
||
36 |
\begin{center} |
|
37 |
\texttt{YournameYourFamilyname} |
|
38 |
\end{center} |
|
39 |
||
40 |
\noindent |
|
820 | 41 |
on my end. |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
750 | 43 |
\subsection*{Disclaimer\alert} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
44 |
|
750 | 45 |
It should be understood that the work you submit represents your own |
46 |
effort. You have not copied from anyone else. An exception is the |
|
47 |
Scala code I showed during the lectures or uploaded to KEATS, which |
|
751 | 48 |
you can both use. You can also use your own code from the CW~1 -- |
49 |
CW~4. |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
|
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
51 |
|
820 | 52 |
\subsection*{Task} |
53 |
||
853 | 54 |
The main goal is to lex and parse 4 Fun-programs, including the |
55 |
Mandelbrot program shown in Figure~\ref{mand}, and generate |
|
56 |
corresponding code for the LLVM-IR. Unfortunately the calculations for |
|
57 |
the Mandelbrot Set require floating point arithmetic and therefore we |
|
58 |
cannot be as simple-minded about types as we have been so far |
|
59 |
(remember the LLVM-IR is a fully-typed language and needs to know the |
|
60 |
exact types of each expression). The idea is to deal appropriately |
|
61 |
with three types, namely \texttt{Int}, \texttt{Double} and |
|
62 |
\texttt{Void} (they are represented in the LLVM-IR as \texttt{i32}, |
|
63 |
\texttt{double} and \texttt{void}). You need to extend the lexer and |
|
64 |
parser accordingly in order to deal with type annotations. The |
|
65 |
Fun-language includes global constants, such as |
|
820 | 66 |
|
67 |
\begin{lstlisting}[numbers=none] |
|
68 |
val Ymin: Double = -1.3; |
|
69 |
val Maxiters: Int = 1000; |
|
70 |
\end{lstlisting} |
|
71 |
||
72 |
\noindent |
|
73 |
where you want to assume that they are `normal' identifiers, just |
|
74 |
starting with a capital letter---all other identifiers should have |
|
75 |
lower-case letters. Function definitions can take arguments of |
|
76 |
type \texttt{Int} or \texttt{Double}, and need to specify a return |
|
77 |
type, which can be \texttt{Void}, for example |
|
78 |
||
79 |
\begin{lstlisting}[numbers=none] |
|
80 |
def foo(n: Int, x: Double) : Double = ... |
|
853 | 81 |
def id(n: Int) : Int = ... |
820 | 82 |
def bar() : Void = ... |
83 |
\end{lstlisting} |
|
84 |
||
85 |
\noindent |
|
86 |
The idea is to record all typing information that is given |
|
853 | 87 |
in the Fun-program, but then delay any further typing inference to |
820 | 88 |
after the CPS-translation. That means the parser should |
89 |
generate ASTs given by the Scala dataypes: |
|
90 |
||
91 |
\begin{lstlisting}[numbers=none,language=Scala] |
|
92 |
abstract class Exp |
|
93 |
abstract class BExp |
|
94 |
abstract class Decl |
|
95 |
||
96 |
case class Def(name: String, args: List[(String, String)], |
|
97 |
ty: String, body: Exp) extends Decl |
|
98 |
case class Main(e: Exp) extends Decl |
|
99 |
case class Const(name: String, v: Int) extends Decl |
|
100 |
case class FConst(name: String, x: Float) extends Decl |
|
101 |
||
102 |
case class Call(name: String, args: List[Exp]) extends Exp |
|
103 |
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp |
|
104 |
case class Var(s: String) extends Exp |
|
853 | 105 |
case class Num(i: Int) extends Exp // integer numbers |
106 |
case class FNum(i: Float) extends Exp // floating numbers |
|
107 |
case class ChConst(c: Int) extends Exp // char constant |
|
820 | 108 |
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
109 |
case class Sequence(e1: Exp, e2: Exp) extends Exp |
|
110 |
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
|
111 |
\end{lstlisting} |
|
112 |
||
113 |
\noindent |
|
114 |
This datatype distinguishes whether the global constant is an integer |
|
115 |
constant or floating constant. Also a function definition needs to |
|
116 |
record the return type of the function, namely the argument |
|
117 |
\texttt{ty} in \texttt{Def}, and the arguments consist of an pairs of |
|
118 |
identifier names and types (\texttt{Int} or \texttt{Double}). The hard |
|
119 |
part of the CW is to design the K-intermediate language and infer all |
|
120 |
necessary types in order to generate LLVM-IR code. You can check |
|
121 |
your LLVM-IR code by running it with the interpreter \texttt{lli}. |
|
122 |
||
123 |
\begin{figure}[t] |
|
124 |
\lstinputlisting[language=Scala]{../progs/fun2/mand.fun} |
|
125 |
\caption{The Mandelbrot program in the `typed' Fun-language.\label{mand}} |
|
126 |
\end{figure} |
|
127 |
||
128 |
\begin{figure}[t] |
|
129 |
\includegraphics[scale=0.35]{../progs/fun2/out.png} |
|
130 |
\caption{Ascii output of the Mandelbrot program.\label{mand}} |
|
131 |
\end{figure} |
|
132 |
||
853 | 133 |
Also note that the second version of the Mandelbrot program and also |
134 |
the Tower of Hanoi program uses character constants, like \texttt{'a'}, |
|
135 |
\texttt{'1'}, \texttt{'$\backslash$n'} and so on. When they are tokenised, |
|
136 |
such characters should be interpreted as the corresponding ASCII code (an |
|
137 |
integer), such that we can use them in calculations like \texttt{'a' + 10} |
|
138 |
where the result should be 107. As usual, the character \texttt{'$\backslash$n'} is the |
|
139 |
ASCII code 10. |
|
140 |
||
141 |
||
820 | 142 |
\subsection*{LLVM-IR} |
143 |
||
144 |
There are some subtleties in the LLVM-IR you need to be aware of: |
|
145 |
||
146 |
\begin{itemize} |
|
147 |
\item \textbf{Global constants}: While global constants such as |
|
148 |
||
149 |
\begin{lstlisting}[numbers=none] |
|
150 |
val Max : Int = 10; |
|
151 |
\end{lstlisting} |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
|
820 | 153 |
\noindent |
154 |
can be easily defined in the LLVM-IR as follows |
|
155 |
||
156 |
\begin{lstlisting}[numbers=none] |
|
157 |
@Max = global i32 10 |
|
158 |
\end{lstlisting} |
|
159 |
||
160 |
\noindent |
|
161 |
they cannot easily be referenced. If you want to use |
|
162 |
this constant then you need to generate code such as |
|
163 |
||
164 |
\begin{lstlisting}[numbers=none] |
|
165 |
%tmp_22 = load i32, i32* @Max |
|
166 |
\end{lstlisting} |
|
167 |
||
168 |
\noindent |
|
169 |
first, which treats \texttt{@Max} as an Integer-pointer (type |
|
170 |
\texttt{i32*}) that needs to be loaded into a local variable, |
|
171 |
here \texttt{\%tmp\_22}. |
|
172 |
||
173 |
\item \textbf{Void-Functions}: While integer and double functions |
|
174 |
can easily be called and their results can be allocated to a |
|
175 |
temporary variable: |
|
176 |
||
177 |
\begin{lstlisting}[numbers=none] |
|
178 |
%tmp_23 = call i32 @sqr (i32 %n) |
|
179 |
\end{lstlisting} |
|
180 |
||
181 |
void-functions cannot be allocated to a variable. They need to be |
|
182 |
called just as |
|
183 |
||
184 |
\begin{lstlisting}[numbers=none] |
|
185 |
call void @print_int (i32 %tmp_23) |
|
186 |
\end{lstlisting} |
|
187 |
||
188 |
\item \textbf{Floating-Point Operations}: While integer operations |
|
189 |
are specified in the LLVM-IR as |
|
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
190 |
|
820 | 191 |
\begin{lstlisting}[numbers=none,language=Scala] |
192 |
def compile_op(op: String) = op match { |
|
193 |
case "+" => "add i32 " |
|
194 |
case "*" => "mul i32 " |
|
195 |
case "-" => "sub i32 " |
|
196 |
case "==" => "icmp eq i32 " |
|
853 | 197 |
case "!=" => "icmp ne i32 " |
820 | 198 |
case "<=" => "icmp sle i32 " // signed less or equal |
199 |
case "<" => "icmp slt i32 " // signed less than |
|
200 |
}\end{lstlisting} |
|
201 |
||
202 |
the corresponding operations on doubles are |
|
203 |
||
204 |
\begin{lstlisting}[numbers=none,language=Scala] |
|
205 |
def compile_dop(op: String) = op match { |
|
206 |
case "+" => "fadd double " |
|
207 |
case "*" => "fmul double " |
|
208 |
case "-" => "fsub double " |
|
209 |
case "==" => "fcmp oeq double " |
|
853 | 210 |
case "!=" => "fcmp one double " |
820 | 211 |
case "<=" => "fcmp ole double " |
212 |
case "<" => "fcmp olt double " |
|
213 |
}\end{lstlisting} |
|
214 |
||
215 |
\item \textbf{Typing}: In order to leave the CPS-translations |
|
216 |
as is, it makes sense to defer the full type-inference to the |
|
217 |
K-intermediate-language. For this it is good to define |
|
218 |
the \texttt{KVar} constructor as |
|
219 |
||
220 |
\begin{lstlisting}[numbers=none,language=Scala] |
|
221 |
case class KVar(s: String, ty: Ty = "UNDEF") extends KVal\end{lstlisting} |
|
222 |
||
223 |
where first a default type, for example \texttt{UNDEF}, is |
|
224 |
given. Then you need to define two typing functions |
|
225 |
||
226 |
\begin{lstlisting}[numbers=none,language=Scala] |
|
227 |
def typ_val(v: KVal, ts: TyEnv) = ??? |
|
228 |
def typ_exp(a: KExp, ts: TyEnv) = ??? |
|
229 |
\end{lstlisting} |
|
230 |
||
231 |
Both functions require a typing-environment that updates |
|
232 |
the information about what type each variable, operation |
|
233 |
and so on receives. Once the types are inferred, the |
|
234 |
LLVM-IR code can be generated. Since we are dealing only |
|
235 |
with simple first-order functions, nothing on the scale |
|
236 |
as the `Hindley-Milner' typing-algorithm is needed. I suggest |
|
237 |
to just look at what data is avaliable and generate all |
|
836 | 238 |
missing information by ``simple means''\ldots rather than |
239 |
looking at the literature which solves the problem |
|
240 |
with much heavier machinery. |
|
820 | 241 |
|
242 |
\item \textbf{Build-In Functions}: The `prelude' comes |
|
243 |
with several build-in functions: \texttt{new\_line()}, |
|
853 | 244 |
\texttt{skip}, \texttt{print\_int(n)}, \texttt{print\_space()}, |
245 |
\texttt{print\_star()} and \texttt{print\_char(n)}. You can find the `prelude' for |
|
821 | 246 |
example in the file \texttt{sqr.ll}. |
820 | 247 |
\end{itemize} |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
248 |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
249 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
250 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
251 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
252 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
253 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
%%% End: |