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