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