updated
authorChristian Urban <urbanc@in.tum.de>
Thu, 28 Nov 2019 08:18:57 +0000
changeset 701 681c36b2af27
parent 700 52263ffd17b9
child 702 39e21a33ffb0
updated
handouts/ho09.pdf
handouts/ho09.tex
progs/catastrophic.js
progs/catastrophic.py
progs/fun_llvm.scala
progs/sqr.ll
slides/slides08.tex
slides/slides09.pdf
slides/slides09.tex
Binary file handouts/ho09.pdf has changed
--- a/handouts/ho09.tex	Sun Nov 24 16:30:34 2019 +0000
+++ b/handouts/ho09.tex	Thu Nov 28 08:18:57 2019 +0000
@@ -138,13 +138,13 @@
 are also prefixed with the type they operate on. Obviously these types
 need to match up\ldots{} but since we have in our programs only
 integers, \texttt{i32} everywhere will do. We do not have to generate
-any other types, but obviously this is a limitation in our Fun-language.
+any other types, but obviously this is a limitation in our Fun language.
  
 There are a few interesting instructions in the LLVM-IR which are quite
-different than in the JVM. Can you remember the kerfuffle we had to go
-through with boolean expressions and negating the condition? In the
-LLVM-IR, branching  if-conditions is implemented differently: there
-is a separate \texttt{br}-instruction as follows:
+different than what we have seen in the JVM. Can you remember the
+kerfuffle we had to go through with boolean expressions and negating the
+condition? In the LLVM-IR, branching  if-conditions is implemented
+differently: there is a separate \texttt{br}-instruction as follows:
 
 \begin{lstlisting}[language=LLVM]
 br i1 %var, label %if_br, label %else_br
@@ -154,10 +154,10 @@
 The type \texttt{i1} stands for booleans. If the variable is true, then
 this instruction jumps to the if-branch, which needs an explicit label;
 otherwise to the else-branch, again with its own label. This allows us
-to keep the meaning of the boolean expression as is.  A value of type
-boolean is generated in the LLVM-IR by the \texttt{icmp}-instruction.
-This instruction is for integers (hence the \texttt{i}) and takes the
-comparison operation as argument. For example
+to keep the meaning of the boolean expression as is when compiling if's.
+A value of type boolean is generated in the LLVM-IR by the
+\texttt{icmp}-instruction. This instruction is for integers (hence the
+\texttt{i}) and takes the comparison operation as argument. For example
 
 \begin{lstlisting}[language=LLVM]
 icmp eq i32  %x, %y     ; for equal
@@ -170,11 +170,24 @@
 In some operations, the LLVM-IR distinguishes between signed and 
 unsigned representations of integers.
 
+It is also easy to call another function in LLVM-IR: as can be 
+seen from Figure~\ref{lli} we can just call a function with the 
+instruction \texttt{call} and can also assign the result to 
+a variable. The syntax is as follows
+
+\begin{lstlisting}[language=LLVM]
+%var = call i32 @foo(...args...)
+\end{lstlisting}
+
+\noindent
+where the arguments can only be simple variables, not compound
+expressions.
+
 Conveniently, you can use the program \texttt{lli}, which comes with
 LLVM, to interpret programs written in the LLVM-IR. So you can easily
 check whether the code you produced actually works. To get a running
 program that does something interesting you need to add some boilerplate
-about printing out numbers and a main-function that is the entrypoint
+about printing out numbers and a main-function that is the entry point
 for the program (see Figure~\ref{lli} for a complete listing). Again
 this is very similar to the boilerplate we needed to add in our JVM
 compiler. 
@@ -214,11 +227,11 @@
 expressions in the Fun language. For convenience the Scala code of the
 corresponding abstract syntax trees is shown on top of
 Figure~\ref{absfun}. Below is the code for the abstract syntax trees in
-the K-language. There are two kinds of syntactic entities, namely
+the K-language. In K, here are two kinds of syntactic entities, namely
 \emph{K-values} and \emph{K-expressions}. The central constructor of the
-K-language is \texttt{KLet}. For this recall that arithmetic expressions
-such as $((1 + a) + (3 + (b * 5)))$ need to be broken up into smaller
-``atomic'' steps, like so
+K-language is \texttt{KLet}. For this recall in SSA that arithmetic
+expressions such as $((1 + a) + (3 + (b * 5)))$ need to be broken up
+into smaller ``atomic'' steps, like so
  
 \begin{lstlisting}[language=LLVMIR,numbers=none]
 let tmp0 = add 1 a in   
@@ -247,7 +260,7 @@
 
 \begin{figure}[p]\small
 \begin{lstlisting}[language=Scala,numbers=none]
-// Fun-language (expressions)
+// Fun language (expressions)
 abstract class Exp 
 abstract class BExp 
 
@@ -288,12 +301,51 @@
 intermediate results need to be chained into later instructions. To do
 this conveniently, CPS-translations have been developed. They use
 functions (``continuations'') to represent what is coming next in a
-sequence of instructions.
+sequence of instructions. Continuations are functions of type
+\code{KVal} to \code{KExp}. They can be seen as a sequence of \code{KLet}s
+where there is a ``hole'' that needs to be filled. Consider for example
 
+\begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}]
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  tmp3 
+\end{lstlisting}
+
+\noindent
+where in the second line is a $\Box$ which still expects a \code{KVal}
+to be filled in before it becomes a ``proper'' \code{KExp}. When we 
+apply and argument to the continuation (remember they are functions)
+we essentially fill something into the corresponding hole. The code
+of the CPS-translation is 
 
+\begin{lstlisting}[language=Scala,numbers=none]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { ... }
+\end{lstlisting}
 
+\noindent 
+where \code{k} is the continuation and \code{e} is the expression 
+to be compiled. In case we have numbers or variables, we can just
+apply the continuation like 
 
+\begin{center}
+\code{k(KNum(n))} \qquad \code{k(KVar(x))}
+\end{center}
 
+\noindent This would just fill in the $\Box$ in a \code{KLet}-expression.
+More interesting is the case for an arithmetic expression.
+
+\begin{lstlisting}[language=Scala,numbers=none]
+case Aop(o, e1, e2) => {
+    val z = Fresh("tmp")
+    CPS(e1)(y1 => 
+      CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))
+}
+\end{lstlisting}
+
+\noindent
 \end{document}
 
 
--- a/progs/catastrophic.js	Sun Nov 24 16:30:34 2019 +0000
+++ b/progs/catastrophic.js	Thu Nov 28 08:18:57 2019 +0000
@@ -8,11 +8,11 @@
 //
 // call with:
 //
-//  $> node catastrophic.py 20
+//  $> node catastrophic.js 20
 //
 // call with timing as:
 //
-//  $> time node catastrophic.py 25
+//  $> time node catastrophic.js 25
 
 
 const args = process.argv[2]
--- a/progs/catastrophic.py	Sun Nov 24 16:30:34 2019 +0000
+++ b/progs/catastrophic.py	Thu Nov 28 08:18:57 2019 +0000
@@ -19,3 +19,4 @@
 m = re.match('(a*)*b' , s) 
 
 print s
+print m
--- a/progs/fun_llvm.scala	Sun Nov 24 16:30:34 2019 +0000
+++ b/progs/fun_llvm.scala	Thu Nov 28 08:18:57 2019 +0000
@@ -278,40 +278,3 @@
 
 
 
-/*
-LLVM notes
-
-Registers are places for data inside the CPU.
-+ up to 10 times faster access than to main memory 
-- expensive; typically just 32 of them in a 32-bit CPU
-
-High-level view of x86
-• Not a stack machine; no direct correspondence to operand stacks
-• Arithmetics, etc. is done with values in registers
-
-• Started as academic project at University of Illinois in 2002
-• Now a large open source project with many contributors and a growing user base
-
-Single Static Assignment (SSA) form
-• Only one assignment in the program text to each variable
-• But dynamically, this assignment can be executed many times
-• Many stores to a memory location are allowed
-• Also, Φ (phi) instructions can be used, in the beginning of a basic block
-• Value is one of the arguments, depending on from which block control came to this block
-• Register allocation tries to keep these variables in same real register
-
-Why SSA form?
-Many code optimizations can be done more efficiently
-
-Function definition form
- define t @name(t1 x1, t2 x2, ..., tn xn) {
- l1: block1
- l2: block2
- ... 
- lm : blockm
- }
-
-
-
-
-*/
\ No newline at end of file
--- a/progs/sqr.ll	Sun Nov 24 16:30:34 2019 +0000
+++ b/progs/sqr.ll	Thu Nov 28 08:18:57 2019 +0000
@@ -18,7 +18,7 @@
 ; main 
 define i32 @main() {
   %1 = call i32 @sqr(i32 5)
-  %2 = call i32 @printInt (i32 %1)
+  %2 = call i32 @printInt(i32 %1)
   ret i32 %1
 }
 
--- a/slides/slides08.tex	Sun Nov 24 16:30:34 2019 +0000
+++ b/slides/slides08.tex	Thu Nov 28 08:18:57 2019 +0000
@@ -556,7 +556,7 @@
 .limit locals 2
 .limit stack 6
   iload 0
-  ldc 0	
+  ldc 0
   if_icmpne If_else_2
   iload 1
   goto If_end_3
Binary file slides/slides09.pdf has changed
--- a/slides/slides09.tex	Sun Nov 24 16:30:34 2019 +0000
+++ b/slides/slides09.tex	Thu Nov 28 08:18:57 2019 +0000
@@ -1,3 +1,4 @@
+% !TEX program = xelatex
 \documentclass[dvipsnames,14pt,t]{beamer}
 \usepackage{../slides}
 \usepackage{../langs}
@@ -6,6 +7,7 @@
 \usepackage{../grammar}
 \usepackage{soul}
 \usepackage{mathpartir}
+\usetikzlibrary{shapes,arrows,shadows}
 
 % beamer stuff
 \renewcommand{\slidecaption}{CFL 09, King's College London}
@@ -26,147 +28,456 @@
   \normalsize
   \begin{center}
   \begin{tabular}{ll}
-  Email:  & christian.urban at kcl.ac.uk\\
-  Office: & N\liningnums{7.07} (North Wing, Bush House)\\
-  Slides: & KEATS (also homework is there)\\
+    Email:  & christian.urban at kcl.ac.uk\\
+    Office Hours: & Thursdays 12 -- 14\\
+    Location: & N7.07 (North Wing, Bush House)\\
+    Slides \& Progs: & KEATS (also homework is there)\\  
   \end{tabular}
   \end{center}
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
- 
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
-\frametitle{While Language}
-  
-\begin{center}
-\bl{\begin{tabular}{@{}lcl@{}}
-\\[-12mm]        
-\meta{Stmt} & $::=$ &  $\texttt{skip}$\\
-              & $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\
-              & $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\
-              & $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\
-              & $|$ & \texttt{read}\;\textit{Id}\\
-              & $|$ & \texttt{write}\;\textit{Id}\\
-              & $|$ & \texttt{write}\;\textit{String}\medskip\\
-\meta{Stmts} & $::=$ &  \meta{Stmt} \;\texttt{;}\; \meta{Stmts} $|$  \meta{Stmt}\medskip\\
-\meta{Block} & $::=$ &  \texttt{\{}\,\meta{Stmts}\,\texttt{\}} $|$ \meta{Stmt}\medskip\\
-\meta{AExp} & $::=$ & \ldots\\
-\meta{BExp} & $::=$ & \ldots\\
-\end{tabular}}
-\end{center}
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c,fragile]
+\frametitle{Functional Programming}
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c]
-\frametitle{\begin{tabular}{c}Fibonacci Numbers\end{tabular}}
+\footnotesize
+\begin{textblock}{13}(0.9,3)
+\begin{lstlisting}[]numbers=none]
+def fib(n) = if n == 0 then 0 
+             else if n == 1 then 1 
+             else fib(n - 1) + fib(n - 2);
 
-\mbox{}\\[-18mm]\mbox{}
+def fact(n) = if n == 0 then 1 else n * fact(n - 1);
 
-{\lstset{language=While}\fontsize{10}{12}\selectfont
-\texttt{\lstinputlisting{../progs/fib.while}}}
+def ack(m, n) = if m == 0 then n + 1
+                else if n == 0 then ack(m - 1, 1)
+                else ack(m - 1, ack(m, n - 1));
+                
+def gcd(a, b) = if b == 0 then a else gcd(b, a % b);                
+\end{lstlisting}
+\end{textblock}
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c,fragile]
-\frametitle{BF***}
+\frametitle{Factorial on the JVM}
 
-some big array, say \texttt{a}; 7 (8) instructions:
+\begin{textblock}{7}(1,1.8)\footnotesize
+\begin{minipage}{6cm}
+\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
+.method public static facT(II)I 
+.limit locals 2
+.limit stack 6
+  iload 0
+  ldc 0
+  if_icmpne If_else_2
+  iload 1
+  goto If_end_3
+If_else_2:
+  iload 0
+  ldc 1
+  isub
+  iload 0
+  iload 1
+  imul
+  invokestatic fact/fact/facT(II)I
+If_end_3:
+  ireturn
+.end method 
+\end{lstlisting}
+\end{minipage}
+\end{textblock}
+
+\begin{textblock}{7}(6,7)
+\begin{bubble}[7cm]\small
+\begin{lstlisting}[language=Lisp,
+                   basicstyle=\ttfamily, 
+                   numbers=none,
+                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
+def facT(n, acc) = 
+  if n == 0 then acc 
+  else facT(n - 1, n * acc);
+\end{lstlisting}
+\end{bubble}
+\end{textblock}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{LLVM}
 
 \begin{itemize}
-\item \texttt{>} move \texttt{ptr++}
-\item \texttt{<} move \texttt{ptr-{}-}
-\item \texttt{+} add \texttt{a[ptr]++}
-\item \texttt{-} subtract \texttt{a[ptr]-{}-}
-\item \texttt{.} print out \texttt{a[ptr]} as ASCII
-\item \texttt{[} if \texttt{a[ptr] == 0} jump just after the corresponding \texttt{]}; otherwise \texttt{ptr++}
-\item \texttt{]} if \texttt{a[ptr] != 0} jump just after the corresponding \texttt{[}; otherwise \texttt{ptr++}
-
-\end{itemize}  
+  \item Chris Lattner, Vikram Adve (started in 2000)
+  \item Apple hired Lattner in 2006
+  \item modular architecture, LLVM-IR
+  \item \texttt{lli} and \texttt{llc} 
+\end{itemize}
 
 \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\tikzstyle{sensor}=[draw, fill=blue!20, text width=3.8em, line width=1mm,
+    text centered, minimum height=2em,drop shadow]
+\tikzstyle{ann} = [above, text width=4em, text centered]
+\tikzstyle{sc} = [sensor, text width=7em, fill=red!20, 
+    minimum height=6em, rounded corners, drop shadow,line width=1mm]
+
+\begin{frame}[fragile,c]
+\frametitle{LLVM: Overview}
+
+\begin{tikzpicture}
+    % Validation Layer is the same except that there are a set of nodes and links which are added
+   
+    \path (0,0) node (IR) [sc] {\textbf{LLVM-IR}\\ Optimisations};
+    \path (IR.west)+(-2.2,1.7) node (sou1) [sensor] {C++};
+    \path (IR.west)+(-2.2,0.5) node (sou2)[sensor] {C};
+    \path (IR.west)+(-2.2,-1.0) node (dots)[ann] {$\vdots$}; 
+    \path (IR.west)+(-2.2,-1.8) node (sou3)[sensor] {Haskell};    
+
+    \path [draw,->,line width=1mm] (sou1.east) -- node [above] {} (IR.160);
+    \path [draw,->,line width=1mm] (sou2.east) -- node [above] {} (IR.180);
+    \path [draw,->,line width=1mm] (sou3.east) -- node [above] {} (IR.200);
+
+    \path (IR.east)+(2.2,2.0)  node (tar1)[sensor] {x86};
+    \path (IR.east)+(2.2,0.8)  node (tar2)[sensor] {ARM};
+    \path (IR.east)+(2.2,-0.4) node (tar3)[sensor] {MIPS}; 
+    \path (IR.east)+(2.2,-1.6) node (tar4)[sensor] {RISC}; 
+    \path (IR.east)+(2.2,-2.8) node (tar5)[sensor] {Power PC};
+    \path (IR.east)+(2.2,-4.2) node (dots2)[ann] {$\vdots$};
+
+    \path [draw,<-,line width=1mm] (tar1.west) -- node [above] {} (IR.10);
+    \path [draw,<-,line width=1mm] (tar2.west) -- node [above] {} (IR.5);
+    \path [draw,<-,line width=1mm] (tar3.west) -- node [above] {} (IR.0);
+    \path [draw,<-,line width=1mm] (tar4.west) -- node [above] {} (IR.-5);
+    \path [draw,<-,line width=1mm] (tar5.west) -- node [above] {} (IR.-10);
+    
+\end{tikzpicture}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{Arrays in While}
+\begin{frame}[fragile,t]
+\frametitle{LLVM-IR}
+\small
+
+\begin{textblock}{7.7}(8,11.4)
+\begin{bubble}[5cm]\small
+\begin{lstlisting}[language=Lisp,
+                   numbers=none,
+                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
+def fact(n) =
+  if n == 0 then 1 
+  else n * fact(n - 1)
+\end{lstlisting}
+\end{bubble}
+\end{textblock}
 
-\begin{itemize}
-\item \texttt{new arr[15000]}\medskip 
-\item \texttt{x := 3 + arr[3 + y]}\medskip 
-\item \texttt{arr[42 * n] := ...}
-\end{itemize}  
+\begin{lstlisting}[language=LLVM,xleftmargin=-7mm]
+define i32 @fact (i32 %n) {
+   %tmp_19 = icmp eq i32  %n, 0
+   br i1 %tmp_19, label %if_br_23, label %else_br_24
+
+if_br_23:
+   ret i32 1
+
+else_br_24:
+   %tmp_21 = sub i32  %n, 1
+   %tmp_22 = call i32 @fact (i32 %tmp_21)
+   %tmp_20 = mul i32  %n, %tmp_22
+   ret i32 %tmp_20
+}
+\end{lstlisting}
 
 \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{LLVM Types}
+
+\tt
+\begin{center}
+\begin{tabular}{ll}
+boolean & i1 \\
+byte    & i8 \\
+short   & i16\\
+char    & i16\\
+integer & i32\\
+long    & i64\\
+float   & float\\
+double  & double\\
+*\_      & pointer to \\
+**\_     & pointer to a pointer to\\
+\mbox{}[\_]    & arrays of\\
+\end{tabular}
+\end{center}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{LLVM Instructions}
+\small
+
+\begin{lstlisting}[language=LLVM]
+br i1 %var, label %if_br, label %else_br
+
+icmp eq i32  %x, %y     ; for equal
+icmp sle i32 %x, %y     ;   signed less or equal
+icmp slt i32 %x, %y     ;   signed less than
+icmp ult i32 %x, %y     ;   unsigned less than 
+
+%var = call i32 @foo(...args...)
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{SSA Format}
+
+\bl{$(1 + a) + (3 + (b * 5))$}\bigskip\bigskip
+
+\begin{lstlisting}[language=LLVM]
+let tmp0 = add 1 a in   
+let tmp1 = mul b 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  tmp3 
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{New Arrays}
+\begin{frame}[fragile,c]
+\frametitle{Abstract Syntax Trees}
+\footnotesize
+
+\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
+// Fun language (expressions)
+abstract class Exp 
+abstract class BExp 
+
+case class Call(name: String, args: List[Exp]) extends Exp
+case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
+case class Write(e: Exp) extends Exp
+case class Var(s: String) extends Exp
+case class Num(i: Int) extends Exp
+case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
+case class Sequence(e1: Exp, e2: Exp) extends Exp
+case class Bop(o: String, a1: Exp, a2: Exp) extends BExp  
+\end{lstlisting}
 
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  new arr[number]
-\end{lstlisting}\bigskip\bigskip
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{K-(Intermediate)Language}
+\footnotesize
 
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  ldc number
-  newarray int
-  astore loc_var
+\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
+abstract class KExp
+abstract class KVal
+
+case class KVar(s: String) extends KVal
+case class KNum(i: Int) extends KVal
+case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
+case class KCall(o: String, vrs: List[KVal]) extends KVal
+case class KWrite(v: KVal) extends KVal
+
+case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp
+case class KLet(x: String, v: KVal, e: KExp) extends KExp
+case class KReturn(v: KVal) extends KExp
 \end{lstlisting}
 
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
+
+\begin{lstlisting}[language=Scala,numbers=none]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { ... }
+\end{lstlisting}
+\bigskip\bigskip
+
+\small
+\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  KReturn tmp3 
+\end{lstlisting}
 
 \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
+
+\begin{lstlisting}[language=Scala,numbers=none]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { 
+     case Var(s) => k(KVar(s)) 
+     case Num(i) => k(KNum(i))
+     ...
+  }
+\end{lstlisting}
+\bigskip\bigskip
+
+\small
+\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  KReturn tmp3 
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{Array Update}
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
 
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  arr[...] := 
-\end{lstlisting}\bigskip\bigskip
-
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  aload loc_var
-  index_aexp
-  value_aexp
-  iastore
+\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-5mm]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = e match { 
+  case Aop(o, e1, e2) => {
+    val z = Fresh("tmp")
+    CPS(e1)(y1 => 
+      CPS(e2)(y2 => 
+               KLet(z, Kop(o, y1, y2), k(KVar(z)))))
+  } ...
+}
 \end{lstlisting}
 
+\small
+\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
+...
+let z = op (*@$\Box_{y_1}$@*) (*@$\Box_{y_2}$@*)
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box\!\!\!\!\raisebox{0.6mm}{\texttt{z}}$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  KReturn tmp3 
+\end{lstlisting}
 
 \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{Array Lookup in AExp}
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
 
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  ...arr[...]... 
-\end{lstlisting}\bigskip\bigskip
+\begin{lstlisting}[language=Scala,numbers=none]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { 
+    case Sequence(e1, e2) => 
+      CPS(e1)(_ => CPS(e2)(y2 => k(y2)))
+     ...
+  }
+\end{lstlisting}
+\bigskip\bigskip
 
-\begin{lstlisting}[mathescape,numbers=none,language=While]
-  aload loc_var
-  index_aexp
-  iaload
+\small
+\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  KReturn tmp3 
 \end{lstlisting}
 
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
+
+\begin{lstlisting}[language=Scala,numbers=none]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { 
+    ...
+    case Sequence(e1, e2) => 
+      CPS(e1)(_ => CPS(e2)(y2 => k(y2)))
+     ...
+  }
+\end{lstlisting}
+\bigskip\bigskip
+
+\small
+\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
+let tmp0 = add 1 a in   
+let tmp1 = mul (*@$\Box$@*) 5 in 
+let tmp2 = add 3 tmp1 in 
+let tmp3 = add tmp0 tmp2 in
+  KReturn tmp3 
+\end{lstlisting}
 
 \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\mode<presentation>{
+\begin{frame}[fragile,c]
+\frametitle{CPS-Translation}
+\small
+
+\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
+def CPS(e: Exp)(k: KVal => KExp) : KExp = 
+  e match { 
+    ...
+    case If(Bop(o, b1, b2), e1, e2) => {
+      val z = Fresh("tmp")
+      CPS(b1)(y1 => 
+        CPS(b2)(y2 => 
+          KLet(z, Kop(o, y1, y2), 
+                KIf(z, CPS(e1)(k), CPS(e2)(k)))))
+     }
+    ...
+  }
+\end{lstlisting}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c]
 
 \large\bf
-Using a compiler, \\how can you mount the\\ perfect attack against a system?
+Using a compiler, \\
+how can you mount the\\ 
+perfect attack against a system?
 
-\end{frame}}
+\end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -323,6 +634,129 @@
   \end{frame}}
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  \begin{frame}[c]
+  \frametitle{Dijkstra on Testing}
+  
+  \begin{bubble}[10cm]
+  ``Program testing can be a very effective way to show the
+  presence of bugs, but it is hopelessly inadequate for showing
+  their absence.''
+  \end{bubble}\bigskip
+  
+  \small
+  What is good about compilers: the either seem to work,
+  or go horribly wrong (most of the time).
+  
+  \end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c]
+\frametitle{\Large Proving Programs to be Correct}
+
+\begin{bubble}[10cm]
+\small
+{\bf Theorem:} There are infinitely many prime 
+numbers.\medskip\\
+
+{\bf Proof} \ldots\\
+\end{bubble}\bigskip
+
+
+similarly\bigskip
+
+\begin{bubble}[10cm]
+\small
+{\bf Theorem:} The program is doing what 
+it is supposed to be doing.\medskip
+
+{\bf Long, long proof} \ldots\\
+\end{bubble}\bigskip\medskip
+
+\small This can be a gigantic proof. The only hope is to have
+help from the computer. `Program' is here to be understood to be
+quite general (compiler, OS, \ldots).
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+
+\begin{frame}[c]
+\frametitle{Can This Be Done?}
+
+\begin{itemize}
+\item in 2008, verification of a small C-compiler
+\begin{itemize}
+\item ``if my input program has a certain behaviour, then the compiled machine code has the same behaviour''
+\item is as good as \texttt{gcc -O1}, but much, much less buggy 
+\end{itemize}
+\end{itemize}
+
+\begin{center}
+  \includegraphics[scale=0.12]{../pics/compcert.png}
+\end{center}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[t]
+\frametitle{Fuzzy Testing C-Compilers}
+
+\begin{itemize}
+\item tested GCC, LLVM and others by randomly generating 
+C-programs
+\item found more than 300 bugs in GCC and also
+many in LLVM (some of them highest-level critical)\bigskip
+\item about CompCert:
+
+\begin{bubble}[10.7cm]\small ``The striking thing about our CompCert
+results is that the middle-end bugs we found in all other
+compilers are absent. As of early 2011, the under-development
+version of CompCert is the only compiler we have tested for
+which Csmith cannot find wrong-code errors. This is not for
+lack of trying: we have devoted about six CPU-years to the
+task.'' 
+\end{bubble} 
+\end{itemize}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+
+\begin{frame}[c]
+\frametitle{Next Week}
+
+\begin{itemize}
+\item Revision Lecture\medskip
+\item How many  strings are in $\bl{L(a^*)}$?\pause\medskip
+\item How many  strings are in $\bl{L((a + b)^*)}$?\\ Are there more than
+  in $\bl{L(a^*)}$?
+\end{itemize}
+
+
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+
+
+
+\end{document}
+
+%%% Local Variables:  
+%%% mode: latex
+%%% TeX-master: t
+%%% End: 
+
+
+
+
+
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \begin{frame}[c]
 
@@ -533,122 +967,3 @@
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-  \begin{frame}[c]
-  \frametitle{Dijkstra on Testing}
-  
-  \begin{bubble}[10cm]
-  ``Program testing can be a very effective way to show the
-  presence of bugs, but it is hopelessly inadequate for showing
-  their absence.''
-  \end{bubble}\bigskip
-  
-  \small
-  What is good about compilers: the either seem to work,
-  or go horribly wrong (most of the time).
-  
-  \end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c]
-\frametitle{\Large Proving Programs to be Correct}
-
-\begin{bubble}[10cm]
-\small
-{\bf Theorem:} There are infinitely many prime 
-numbers.\medskip\\
-
-{\bf Proof} \ldots\\
-\end{bubble}\bigskip
-
-
-similarly\bigskip
-
-\begin{bubble}[10cm]
-\small
-{\bf Theorem:} The program is doing what 
-it is supposed to be doing.\medskip
-
-{\bf Long, long proof} \ldots\\
-\end{bubble}\bigskip\medskip
-
-\small This can be a gigantic proof. The only hope is to have
-help from the computer. `Program' is here to be understood to be
-quite general (compiler, OS, \ldots).
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
-\begin{frame}[c]
-\frametitle{Can This Be Done?}
-
-\begin{itemize}
-\item in 2008, verification of a small C-compiler
-\begin{itemize}
-\item ``if my input program has a certain behaviour, then the compiled machine code has the same behaviour''
-\item is as good as \texttt{gcc -O1}, but much, much less buggy 
-\end{itemize}
-\end{itemize}
-
-\begin{center}
-  \includegraphics[scale=0.12]{../pics/compcert.png}
-\end{center}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-\begin{frame}[t]
-\frametitle{Fuzzy Testing C-Compilers}
-
-\begin{itemize}
-\item tested GCC, LLVM and others by randomly generating 
-C-programs
-\item found more than 300 bugs in GCC and also
-many in LLVM (some of them highest-level critical)\bigskip
-\item about CompCert:
-
-\begin{bubble}[10.7cm]\small ``The striking thing about our CompCert
-results is that the middle-end bugs we found in all other
-compilers are absent. As of early 2011, the under-development
-version of CompCert is the only compiler we have tested for
-which Csmith cannot find wrong-code errors. This is not for
-lack of trying: we have devoted about six CPU-years to the
-task.'' 
-\end{bubble} 
-\end{itemize}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
-\begin{frame}[c]
-\frametitle{Next Week}
-
-\begin{itemize}
-\item Revision Lecture\medskip
-\item How many  strings are in $\bl{L(a^*)}$?\pause\medskip
-\item How many  strings are in $\bl{L((a + b)^*)}$?\\ Are there more than
-  in $\bl{L(a^*)}$?
-\end{itemize}
-
-
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
-
-
-\end{document}
-
-%%% Local Variables:  
-%%% mode: latex
-%%% TeX-master: t
-%%% End: 
-