cws/cw05.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 19 Dec 2020 00:13:58 +0000
changeset 820 7fd1f611c21d
parent 752 c0bdd4ad69ca
child 821 f914b9476dc7
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}
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
     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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
     9
\section*{Coursework 5\footnote{\today}}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    11
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    12
751
4b208d81e002 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 750
diff changeset
    13
\noindent This coursework is worth 12\% and is due on \cwFIVE{} at
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    14
18:00. You are asked to implement a compiler targeting the LLVM-IR.
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    15
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
    16
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
    17
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
    18
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    19
\begin{itemize}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    20
\item \url{https://bit.ly/3rheZYr}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    21
\item \url{https://llvm.org/docs/LangRef.html}  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    22
\end{itemize}  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    23
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    24
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    25
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
    26
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
    27
you generated the LLVM-IR files, otherwise a mark of 0\% will be
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    28
awarded. You should use the lexer and parser from the previous
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    29
courseworks, but you need to make some modifications to them for the
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    30
`typed' fun-language. I will award up to 4\% if a lexer and parser are
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    31
implemented. At the end, please package everything(!) in a zip-file
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    32
that creates a directory with the name \texttt{YournameYourFamilyname}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    33
on my end.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    35
\subsection*{Disclaimer\alert}
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    36
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    37
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
    38
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
    39
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
    40
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
    41
CW~4.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    43
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    44
\subsection*{Task}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    45
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    46
The goal is to lex and parse the Mandelbrot program shown in
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    47
Figure~\ref{mand} and generate corresponding code for the
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    48
LLVM-IR. Unfortunately the calculations for the Mandelbrot set require
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    49
floating point arithmetic and therefore we cannot be as simple-minded
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    50
about types as we have been so far (remember the LLVM-IR is a
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    51
fully-typed language and needs to know the exact types of each
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    52
expression). The idea is to deal appropriately with three types,
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    53
namely \texttt{Int}, \texttt{Double} and \texttt{Void} (they are
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    54
represented in the LLVM-IR as \texttt{i32}, \texttt{double} and
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    55
\texttt{void}). You need to extend the lexer and parser accordingly in
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    56
order to deal with type annotations. The Fun-language includes global
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    57
constants, such as
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    58
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    59
\begin{lstlisting}[numbers=none]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    60
  val Ymin: Double = -1.3;
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    61
  val Maxiters: Int = 1000;
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    62
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    63
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    64
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    65
where you want to assume that they are `normal' identifiers, just
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    66
starting with a capital letter---all other identifiers should have
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    67
lower-case letters. Function definitions can take arguments of
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    68
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
    69
type, which can be \texttt{Void}, for example
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    70
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    71
\begin{lstlisting}[numbers=none]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    72
  def foo(n: Int, x: Double) : Double = ...
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    73
  def bar() : Void = ...
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    74
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    75
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    76
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    77
The idea is to record all typing information that is given
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    78
in the program, but then delay any further typing inference to
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    79
after the CPS-translation. That means the parser should
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    80
generate ASTs given by the Scala dataypes:
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    81
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    82
\begin{lstlisting}[numbers=none,language=Scala]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    83
abstract class Exp 
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    84
abstract class BExp  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    85
abstract class Decl 
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    86
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    87
case class Def(name: String, args: List[(String, String)],
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    88
               ty: String, body: Exp) extends Decl
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    89
case class Main(e: Exp) extends Decl
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    90
case class Const(name: String, v: Int) extends Decl
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    91
case class FConst(name: String, x: Float) extends Decl
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    92
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    93
case class Call(name: String, args: List[Exp]) extends Exp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    94
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    95
case class Var(s: String) extends Exp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    96
case class Num(i: Int) extends Exp    // integer numbers
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    97
case class FNum(i: Float) extends Exp // floating numbers
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    98
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    99
case class Sequence(e1: Exp, e2: Exp) extends Exp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   100
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   101
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   102
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   103
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   104
This datatype distinguishes whether the global constant is an integer
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   105
constant or floating constant. Also a function definition needs to
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   106
record the return type of the function, namely the argument
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   107
\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
   108
identifier names and types (\texttt{Int} or \texttt{Double}). The hard
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   109
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
   110
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
   111
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
   112
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   113
\begin{figure}[t]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   114
\lstinputlisting[language=Scala]{../progs/fun2/mand.fun}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   115
\caption{The Mandelbrot program in the `typed' Fun-language.\label{mand}}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   116
\end{figure}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   117
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   118
\begin{figure}[t]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   119
\includegraphics[scale=0.35]{../progs/fun2/out.png}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   120
\caption{Ascii output of the Mandelbrot program.\label{mand}}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   121
\end{figure}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   122
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   123
\subsection*{LLVM-IR}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   124
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   125
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
   126
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   127
\begin{itemize}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   128
\item \textbf{Global constants}: While global constants such as
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   129
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   130
\begin{lstlisting}[numbers=none]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   131
val Max : Int = 10;
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   132
\end{lstlisting}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   134
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   135
can be easily defined in the LLVM-IR as follows
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
\begin{lstlisting}[numbers=none]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   138
@Max = global i32 10
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   139
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   140
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   141
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   142
they cannot easily be referenced. If you want to use
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   143
this constant then you need to generate code such as
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   144
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   145
\begin{lstlisting}[numbers=none]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   146
%tmp_22 = load i32, i32* @Max
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   147
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   148
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   149
\noindent
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   150
first, which treats \texttt{@Max} as an Integer-pointer (type
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   151
\texttt{i32*}) that needs to be loaded into a local variable,
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   152
here \texttt{\%tmp\_22}.
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   153
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   154
\item \textbf{Void-Functions}: While integer and double functions
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   155
  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
   156
  temporary variable:
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{lstlisting}[numbers=none]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   159
   %tmp_23 = call i32 @sqr (i32 %n)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   160
  \end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   161
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   162
  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
   163
  called just as
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   164
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   165
  \begin{lstlisting}[numbers=none]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   166
  call void @print_int (i32 %tmp_23)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   167
\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   168
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   169
\item \textbf{Floating-Point Operations}: While integer operations
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   170
  are specified in the LLVM-IR as
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   171
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   172
  \begin{lstlisting}[numbers=none,language=Scala]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   173
  def compile_op(op: String) = op match {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   174
    case "+" => "add i32 "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   175
    case "*" => "mul i32 "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   176
    case "-" => "sub i32 "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   177
    case "==" => "icmp eq i32 "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   178
    case "<=" => "icmp sle i32 " // signed less or equal
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   179
    case "<"  => "icmp slt i32 " // signed less than
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   180
  }\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   181
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   182
  the corresponding operations on doubles are
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,language=Scala]
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   185
  def compile_dop(op: String) = op match {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   186
    case "+" => "fadd double "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   187
    case "*" => "fmul double "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   188
    case "-" => "fsub double "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   189
    case "==" => "fcmp oeq double "
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   190
    case "<=" => "fcmp ole double "   
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   191
    case "<"  => "fcmp olt double "   
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   192
  }\end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   193
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   194
\item \textbf{Typing}: In order to leave the CPS-translations
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   195
  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
   196
  K-intermediate-language. For this it is good to define
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   197
  the \texttt{KVar} constructor 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,language=Scala]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   200
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
   201
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   202
  where first a default type, for example \texttt{UNDEF}, is
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   203
  given. Then you need to define two typing functions
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   204
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   205
  \begin{lstlisting}[numbers=none,language=Scala]  
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   206
    def typ_val(v: KVal, ts: TyEnv) = ???
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   207
    def typ_exp(a: KExp, ts: TyEnv) = ???
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   208
  \end{lstlisting}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   209
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   210
  Both functions require a typing-environment that updates
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   211
  the information about what type each variable, operation
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   212
  and so on receives. Once the types are inferred, the
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   213
  LLVM-IR code can be generated. Since we are dealing only
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   214
  with simple first-order functions, nothing on the scale
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   215
  as the `Hindley-Milner' typing-algorithm is needed. I suggest
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   216
  to just look at what data is avaliable and generate all
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   217
  missing information by simple means.
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
\item \textbf{Build-In Functions}: The `prelude' comes
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   220
  with several build-in functions: \texttt{new\_line()},
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   221
  \texttt{skip}, \texttt{print\_int(n)}, \texttt{print\_space()}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   222
  and \texttt{print\_star()}.
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
   223
\end{itemize}  
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   224
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
%%% End: