| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Thu, 26 Oct 2023 14:30:20 +0100 | |
| changeset 944 | 370cd97579a7 | 
| parent 939 | fb6ffb9b7304 | 
| child 957 | 03c5a8987141 | 
| permissions | -rw-r--r-- | 
| 701 | 1  | 
% !TEX program = xelatex  | 
| 744 | 2  | 
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
 | 
| 
309
 
640e4a05cd9b
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
215 
diff
changeset
 | 
3  | 
\usepackage{../slides}
 | 
| 
215
 
828303e8e4af
updated slides
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
207 
diff
changeset
 | 
4  | 
\usepackage{../langs}
 | 
| 609 | 5  | 
\usepackage{../data}
 | 
| 871 | 6  | 
\usepackage{../graphicss}
 | 
| 
379
 
fa2589ec0fae
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
312 
diff
changeset
 | 
7  | 
\usepackage{../grammar}
 | 
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
8  | 
\usepackage{soul}
 | 
| 610 | 9  | 
\usepackage{mathpartir}
 | 
| 701 | 10  | 
\usetikzlibrary{shapes,arrows,shadows}
 | 
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
11  | 
|
| 609 | 12  | 
% beamer stuff  | 
| 459 | 13  | 
\renewcommand{\slidecaption}{CFL 09, King's College London}
 | 
| 609 | 14  | 
\newcommand{\bl}[1]{\textcolor{blue}{#1}}       
 | 
15  | 
||
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
16  | 
|
| 
65
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
17  | 
\begin{document}
 | 
| 609 | 18  | 
|
| 
65
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
19  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
| 
309
 
640e4a05cd9b
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
215 
diff
changeset
 | 
20  | 
\begin{frame}[t]
 | 
| 
65
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
21  | 
\frametitle{%
 | 
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
22  | 
  \begin{tabular}{@ {}c@ {}}
 | 
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
23  | 
\\[-3mm]  | 
| 459 | 24  | 
\LARGE Compilers and \\[-2mm]  | 
| 901 | 25  | 
\LARGE Formal Languages\\[-3mm]  | 
| 
65
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
26  | 
  \end{tabular}}
 | 
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
27  | 
|
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
28  | 
\normalsize  | 
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
29  | 
  \begin{center}
 | 
| 
 
ade6af51402c
tuned
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
30  | 
  \begin{tabular}{ll}
 | 
| 939 | 31  | 
Email: & christian.urban at kcl.ac.uk\\  | 
32  | 
Office Hour: & Thurdays 15 -- 16\\  | 
|
| 901 | 33  | 
Location: & N7.07 (North Wing, Bush House)\\  | 
34  | 
Slides \& Progs: & KEATS\\  | 
|
35  | 
  Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\  
 | 
|
| 538 | 36  | 
  \end{tabular}
 | 
37  | 
  \end{center}
 | 
|
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
38  | 
|
| 744 | 39  | 
  \begin{center}
 | 
40  | 
    \begin{tikzpicture}
 | 
|
41  | 
\node[drop shadow,fill=white,inner sep=0pt]  | 
|
42  | 
      {\footnotesize\rowcolors{1}{capri!10}{white}
 | 
|
43  | 
        \begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline
 | 
|
44  | 
1 Introduction, Languages & 6 While-Language \\  | 
|
45  | 
2 Regular Expressions, Derivatives & 7 Compilation, JVM \\  | 
|
46  | 
3 Automata, Regular Languages & 8 Compiling Functional Languages \\  | 
|
47  | 
          4 Lexing, Tokenising               & \cellcolor{blue!50} 9 Optimisations \\
 | 
|
48  | 
5 Grammars, Parsing & 10 LLVM \\ \hline  | 
|
49  | 
        \end{tabular}%
 | 
|
50  | 
};  | 
|
51  | 
    \end{tikzpicture}
 | 
|
52  | 
  \end{center}
 | 
|
| 609 | 53  | 
\end{frame}
 | 
54  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
55  | 
||
| 701 | 56  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
57  | 
\begin{frame}[c,fragile]
 | 
|
| 702 | 58  | 
\frametitle{The Fun Language}
 | 
| 609 | 59  | 
|
| 701 | 60  | 
\footnotesize  | 
61  | 
\begin{textblock}{13}(0.9,3)
 | 
|
| 864 | 62  | 
\begin{lstlisting}[numbers=none]
 | 
| 701 | 63  | 
def fib(n) = if n == 0 then 0  | 
64  | 
else if n == 1 then 1  | 
|
65  | 
else fib(n - 1) + fib(n - 2);  | 
|
| 609 | 66  | 
|
| 701 | 67  | 
def fact(n) = if n == 0 then 1 else n * fact(n - 1);  | 
| 609 | 68  | 
|
| 701 | 69  | 
def ack(m, n) = if m == 0 then n + 1  | 
70  | 
else if n == 0 then ack(m - 1, 1)  | 
|
71  | 
else ack(m - 1, ack(m, n - 1));  | 
|
72  | 
||
73  | 
def gcd(a, b) = if b == 0 then a else gcd(b, a % b);  | 
|
74  | 
\end{lstlisting}
 | 
|
75  | 
\end{textblock}
 | 
|
| 609 | 76  | 
|
77  | 
\end{frame}
 | 
|
78  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 901 | 79  | 
|
| 853 | 80  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
| 901 | 81  | 
\begin{frame}[c, fragile]
 | 
82  | 
\frametitle{Stack Estimation}
 | 
|
83  | 
\small  | 
|
84  | 
\mbox{}\\[-15mm]
 | 
|
85  | 
||
86  | 
\bl{\begin{center}
 | 
|
87  | 
\begin{tabular}{@{\hspace{-4mm}}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
 | 
|
88  | 
$\textit{estimate}(n)$ & $\dn$ & $1$\\
 | 
|
89  | 
$\textit{estimate}(x)$ & $\dn$ & $1$\\
 | 
|
90  | 
$\textit{estimate}(a_1\;aop\;a_2)$ & $\dn$ &
 | 
|
91  | 
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\
 | 
|
92  | 
$\textit{estimate}(\pcode{if}\;b\;\pcode{then}\;e_1\;\pcode{else}\;e_2)$ & $\dn$ & 
 | 
|
93  | 
$\textit{estimate}(b) +$\\ 
 | 
|
94  | 
& & $\quad max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\
 | 
|
95  | 
$\textit{estimate}(\pcode{write}(e))$ & $\dn$ & 
 | 
|
96  | 
$\textit{estimate}(e) + 1$\\
 | 
|
97  | 
$\textit{estimate}(e_1 ; e_2)$ & $\dn$ & 
 | 
|
98  | 
$max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\
 | 
|
99  | 
$\textit{estimate}(f(e_1, ..., e_n))$ & $\dn$ & 
 | 
|
100  | 
$\sum_{i = 1..n}\;estimate(e_i)$\medskip\\
 | 
|
101  | 
$\textit{estimate}(a_1\;bop\;a_2)$ & $\dn$ &
 | 
|
102  | 
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\
 | 
|
103  | 
\end{tabular}
 | 
|
104  | 
\end{center}}
 | 
|
105  | 
||
106  | 
\end{frame}
 | 
|
107  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
108  | 
||
109  | 
||
110  | 
||
111  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
112  | 
||
113  | 
||
| 853 | 114  | 
\begin{frame}[c,fragile]
 | 
115  | 
\frametitle{\mbox{}\hspace{5cm}Factorial}
 | 
|
116  | 
||
117  | 
\begin{textblock}{7}(0,1.0)\footnotesize
 | 
|
118  | 
\begin{minipage}{6cm}
 | 
|
119  | 
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
 | 
|
120  | 
.method public static facT(II)I  | 
|
121  | 
.limit locals 2  | 
|
122  | 
.limit stack 6  | 
|
123  | 
iload 0  | 
|
124  | 
ldc 0  | 
|
125  | 
if_icmpne If_else_2  | 
|
126  | 
iload 1  | 
|
127  | 
goto If_end_3  | 
|
128  | 
If_else_2:  | 
|
129  | 
iload 0  | 
|
130  | 
ldc 1  | 
|
131  | 
isub  | 
|
132  | 
iload 0  | 
|
133  | 
iload 1  | 
|
134  | 
imul  | 
|
135  | 
invokestatic fact/fact/facT(II)I  | 
|
136  | 
If_end_3:  | 
|
137  | 
ireturn  | 
|
138  | 
.end method  | 
|
139  | 
\end{lstlisting}
 | 
|
140  | 
\end{minipage}
 | 
|
141  | 
\end{textblock}
 | 
|
142  | 
||
143  | 
\begin{textblock}{7}(6,7)
 | 
|
144  | 
\begin{bubble}[7cm]\small
 | 
|
145  | 
\begin{lstlisting}[language=Lisp,
 | 
|
146  | 
basicstyle=\ttfamily,  | 
|
147  | 
numbers=none,  | 
|
148  | 
                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
 | 
|
149  | 
def facT(n, acc) =  | 
|
150  | 
if n == 0 then acc  | 
|
151  | 
else facT(n - 1, n * acc);  | 
|
152  | 
\end{lstlisting}
 | 
|
153  | 
\end{bubble}
 | 
|
154  | 
\end{textblock}
 | 
|
155  | 
||
156  | 
\end{frame}
 | 
|
157  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
158  | 
||
159  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
160  | 
\begin{frame}[fragile]
 | 
|
161  | 
||
162  | 
\begin{textblock}{7}(1,-0.2)\footnotesize
 | 
|
163  | 
\begin{minipage}{6cm}
 | 
|
164  | 
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none, escapeinside={(*@}{@*)}]
 | 
|
165  | 
.method public static facT(II)I  | 
|
166  | 
.limit locals 2  | 
|
167  | 
.limit stack 6  | 
|
168  | 
(*@\hl{facT\_Start:} @*)
 | 
|
169  | 
iload 0  | 
|
170  | 
ldc 0  | 
|
171  | 
if_icmpne If_else_2  | 
|
172  | 
iload 1  | 
|
173  | 
goto If_end_3  | 
|
174  | 
If_else_2:  | 
|
175  | 
iload 0  | 
|
176  | 
ldc 1  | 
|
177  | 
isub  | 
|
178  | 
iload 0  | 
|
179  | 
iload 1  | 
|
180  | 
imul  | 
|
181  | 
  (*@\hl{istore 1} @*)
 | 
|
182  | 
  (*@\hl{istore 0} @*)
 | 
|
183  | 
  (*@\hl{goto facT\_Start} @*)
 | 
|
184  | 
If_end_3:  | 
|
185  | 
ireturn  | 
|
186  | 
.end method  | 
|
187  | 
\end{lstlisting}
 | 
|
188  | 
\end{minipage}
 | 
|
189  | 
\end{textblock}
 | 
|
190  | 
||
191  | 
\begin{textblock}{7}(6,7)
 | 
|
192  | 
\begin{bubble}[7cm]\small
 | 
|
193  | 
\begin{lstlisting}[language=Lisp,
 | 
|
194  | 
basicstyle=\ttfamily,  | 
|
195  | 
numbers=none,  | 
|
196  | 
                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
 | 
|
197  | 
def facT(n, acc) =  | 
|
198  | 
if n == 0 then acc  | 
|
199  | 
else facT(n - 1, n * acc);  | 
|
200  | 
\end{lstlisting}
 | 
|
201  | 
\end{bubble}
 | 
|
202  | 
\end{textblock}
 | 
|
203  | 
||
204  | 
\end{frame}
 | 
|
205  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
206  | 
||
207  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
208  | 
\begin{frame}[t, fragile]
 | 
|
209  | 
\frametitle{Tail Recursion}
 | 
|
210  | 
||
211  | 
A call to \texttt{f(args)} is usually compiled as\medskip
 | 
|
212  | 
||
213  | 
{\small\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
 | 
|
214  | 
args onto stack  | 
|
215  | 
invokestatic .../f  | 
|
216  | 
\end{lstlisting}}\pause
 | 
|
217  | 
||
218  | 
||
219  | 
A call is in tail position provided:\medskip  | 
|
220  | 
||
221  | 
{\small\begin{itemize}
 | 
|
222  | 
\item \texttt{if Bexp then \hl{Exp} else \hl{Exp}}
 | 
|
223  | 
\item \texttt{Exp ; \hl{Exp}}
 | 
|
224  | 
\item \texttt{Exp  op Exp}
 | 
|
225  | 
\end{itemize}}\medskip
 | 
|
226  | 
||
227  | 
then a call \texttt{f(args)} can be compiled as\medskip\small
 | 
|
228  | 
||
229  | 
\begin{lstlisting}[numbers=none]
 | 
|
230  | 
prepare environment  | 
|
231  | 
jump to start of function  | 
|
232  | 
\end{lstlisting}
 | 
|
233  | 
||
234  | 
\end{frame}
 | 
|
235  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
236  | 
||
237  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
238  | 
\begin{frame}[c, fragile]
 | 
|
239  | 
\frametitle{Tail Recursive Call}
 | 
|
240  | 
\footnotesize  | 
|
241  | 
||
242  | 
\begin{textblock}{13}(-0.3,3)
 | 
|
243  | 
\begin{lstlisting}[language=Scala, numbers=none]
 | 
|
244  | 
def compile_expT(a: Exp, env: Mem, name: String): Instrs =  | 
|
245  | 
...  | 
|
246  | 
case Call(n, args) => if (name == n)  | 
|
247  | 
  {
 | 
|
248  | 
val stores =  | 
|
249  | 
      args.zipWithIndex.map { case (x, y) => i"istore $y" } 
 | 
|
250  | 
||
251  | 
args.map(a => compile_expT(a, env, "")).mkString ++  | 
|
252  | 
stores.reverse.mkString ++  | 
|
253  | 
    i"goto ${n}_Start" 
 | 
|
254  | 
  } else {
 | 
|
255  | 
val is = "I" * args.length  | 
|
256  | 
args.map(a => compile_expT(a, env, "")).mkString ++  | 
|
257  | 
    i"invokestatic XXX/XXX/${n}(${is})I"
 | 
|
258  | 
}  | 
|
259  | 
\end{lstlisting}
 | 
|
260  | 
\end{textblock}
 | 
|
261  | 
||
262  | 
\end{frame}
 | 
|
263  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 609 | 264  | 
|
265  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
266  | 
\begin{frame}[c,fragile]
 | 
|
| 901 | 267  | 
\frametitle{Peephole Optimisations}
 | 
| 609 | 268  | 
|
| 901 | 269  | 
\begin{center}
 | 
270  | 
\begin{tabular}{ll}
 | 
|
271  | 
  \texttt{ldc}:  & \texttt{iconst\_0} \ldots \texttt{iconst\_5}\\
 | 
|
272  | 
                 & \texttt{bipush} $n$ where $-128 < n <= 128$\bigskip\\  
 | 
|
273  | 
  \texttt{iload}: &  \texttt{iload\_0} \ldots \texttt{iload\_3}\bigskip\\
 | 
|
274  | 
  \texttt{istore}: &  \texttt{istore\_0} \ldots \texttt{istore\_3}\\
 | 
|
275  | 
\end{tabular}
 | 
|
276  | 
\end{center}  
 | 
|
| 701 | 277  | 
|
278  | 
\end{frame}
 | 
|
279  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
280  | 
||
281  | 
||
282  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 901 | 283  | 
%\begin{frame}[c,fragile]
 | 
284  | 
%\frametitle{Factorial Funct.~on the JVM}
 | 
|
285  | 
%  | 
|
286  | 
%\begin{textblock}{7}(1,1.8)\footnotesize
 | 
|
287  | 
%\begin{minipage}{6cm}
 | 
|
288  | 
%\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
 | 
|
289  | 
%.method public static facT(II)I  | 
|
290  | 
%.limit locals 2  | 
|
291  | 
%.limit stack 6  | 
|
292  | 
% iload 0  | 
|
293  | 
% ldc 0  | 
|
294  | 
% if_icmpne If_else_2  | 
|
295  | 
% iload 1  | 
|
296  | 
% goto If_end_3  | 
|
297  | 
%If_else_2:  | 
|
298  | 
% iload 0  | 
|
299  | 
% ldc 1  | 
|
300  | 
% isub  | 
|
301  | 
% iload 0  | 
|
302  | 
% iload 1  | 
|
303  | 
% imul  | 
|
304  | 
% invokestatic fact/fact/facT(II)I  | 
|
305  | 
%If_end_3:  | 
|
306  | 
% ireturn  | 
|
307  | 
%.end method  | 
|
308  | 
%\end{lstlisting}
 | 
|
309  | 
%\end{minipage}
 | 
|
310  | 
%\end{textblock}
 | 
|
311  | 
%  | 
|
312  | 
%\begin{textblock}{7}(6,7)
 | 
|
313  | 
%\begin{bubble}[7cm]\small
 | 
|
314  | 
%\begin{lstlisting}[language=Lisp,
 | 
|
315  | 
% basicstyle=\ttfamily,  | 
|
316  | 
% numbers=none,  | 
|
317  | 
%                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
 | 
|
318  | 
%def facT(n, acc) =  | 
|
319  | 
% if n == 0 then acc  | 
|
320  | 
% else facT(n - 1, n * acc);  | 
|
321  | 
%\end{lstlisting}
 | 
|
322  | 
%\end{bubble}
 | 
|
323  | 
%\end{textblock}
 | 
|
324  | 
%  | 
|
325  | 
%\end{frame}
 | 
|
326  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
327  | 
||
328  | 
||
329  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 330  | 
\begin{frame}[fragile,c]
 | 
331  | 
\frametitle{LLVM}
 | 
|
| 609 | 332  | 
|
333  | 
\begin{itemize}
 | 
|
| 701 | 334  | 
\item Chris Lattner, Vikram Adve (started in 2000)  | 
335  | 
\item Apple hired Lattner in 2006  | 
|
336  | 
\item modular architecture, LLVM-IR  | 
|
337  | 
  \item \texttt{lli} and \texttt{llc} 
 | 
|
338  | 
\end{itemize}
 | 
|
| 609 | 339  | 
|
340  | 
\end{frame}
 | 
|
| 701 | 341  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
342  | 
||
343  | 
||
344  | 
||
345  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
346  | 
\tikzstyle{sensor}=[draw, fill=blue!20, text width=3.8em, line width=1mm,
 | 
|
347  | 
text centered, minimum height=2em,drop shadow]  | 
|
348  | 
\tikzstyle{ann} = [above, text width=4em, text centered]
 | 
|
349  | 
\tikzstyle{sc} = [sensor, text width=7em, fill=red!20, 
 | 
|
350  | 
minimum height=6em, rounded corners, drop shadow,line width=1mm]  | 
|
351  | 
||
352  | 
\begin{frame}[fragile,c]
 | 
|
353  | 
\frametitle{LLVM: Overview}
 | 
|
354  | 
||
355  | 
\begin{tikzpicture}
 | 
|
356  | 
% Validation Layer is the same except that there are a set of nodes and links which are added  | 
|
357  | 
||
358  | 
    \path (0,0) node (IR) [sc] {\textbf{LLVM-IR}\\ Optimisations};
 | 
|
359  | 
    \path (IR.west)+(-2.2,1.7) node (sou1) [sensor] {C++};
 | 
|
360  | 
    \path (IR.west)+(-2.2,0.5) node (sou2)[sensor] {C};
 | 
|
361  | 
    \path (IR.west)+(-2.2,-1.0) node (dots)[ann] {$\vdots$}; 
 | 
|
362  | 
    \path (IR.west)+(-2.2,-1.8) node (sou3)[sensor] {Haskell};    
 | 
|
363  | 
||
364  | 
    \path [draw,->,line width=1mm] (sou1.east) -- node [above] {} (IR.160);
 | 
|
365  | 
    \path [draw,->,line width=1mm] (sou2.east) -- node [above] {} (IR.180);
 | 
|
366  | 
    \path [draw,->,line width=1mm] (sou3.east) -- node [above] {} (IR.200);
 | 
|
367  | 
||
368  | 
    \path (IR.east)+(2.2,2.0)  node (tar1)[sensor] {x86};
 | 
|
369  | 
    \path (IR.east)+(2.2,0.8)  node (tar2)[sensor] {ARM};
 | 
|
370  | 
    \path (IR.east)+(2.2,-0.4) node (tar3)[sensor] {MIPS}; 
 | 
|
371  | 
    \path (IR.east)+(2.2,-1.6) node (tar4)[sensor] {RISC}; 
 | 
|
372  | 
    \path (IR.east)+(2.2,-2.8) node (tar5)[sensor] {Power PC};
 | 
|
373  | 
    \path (IR.east)+(2.2,-4.2) node (dots2)[ann] {$\vdots$};
 | 
|
374  | 
||
375  | 
    \path [draw,<-,line width=1mm] (tar1.west) -- node [above] {} (IR.10);
 | 
|
376  | 
    \path [draw,<-,line width=1mm] (tar2.west) -- node [above] {} (IR.5);
 | 
|
377  | 
    \path [draw,<-,line width=1mm] (tar3.west) -- node [above] {} (IR.0);
 | 
|
378  | 
    \path [draw,<-,line width=1mm] (tar4.west) -- node [above] {} (IR.-5);
 | 
|
379  | 
    \path [draw,<-,line width=1mm] (tar5.west) -- node [above] {} (IR.-10);
 | 
|
380  | 
||
381  | 
\end{tikzpicture}
 | 
|
382  | 
\end{frame}
 | 
|
383  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
384  | 
||
| 609 | 385  | 
|
386  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 387  | 
\begin{frame}[fragile,t]
 | 
388  | 
\frametitle{LLVM-IR}
 | 
|
389  | 
\small  | 
|
390  | 
||
391  | 
\begin{textblock}{7.7}(8,11.4)
 | 
|
392  | 
\begin{bubble}[5cm]\small
 | 
|
393  | 
\begin{lstlisting}[language=Lisp,
 | 
|
394  | 
numbers=none,  | 
|
395  | 
                   xleftmargin=1mm,linebackgroundcolor=\color{cream}]
 | 
|
396  | 
def fact(n) =  | 
|
397  | 
if n == 0 then 1  | 
|
398  | 
else n * fact(n - 1)  | 
|
399  | 
\end{lstlisting}
 | 
|
400  | 
\end{bubble}
 | 
|
401  | 
\end{textblock}
 | 
|
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
402  | 
|
| 701 | 403  | 
\begin{lstlisting}[language=LLVM,xleftmargin=-7mm]
 | 
404  | 
define i32 @fact (i32 %n) {
 | 
|
405  | 
%tmp_19 = icmp eq i32 %n, 0  | 
|
406  | 
br i1 %tmp_19, label %if_br_23, label %else_br_24  | 
|
407  | 
||
408  | 
if_br_23:  | 
|
409  | 
ret i32 1  | 
|
410  | 
||
411  | 
else_br_24:  | 
|
412  | 
%tmp_21 = sub i32 %n, 1  | 
|
413  | 
%tmp_22 = call i32 @fact (i32 %tmp_21)  | 
|
414  | 
%tmp_20 = mul i32 %n, %tmp_22  | 
|
415  | 
ret i32 %tmp_20  | 
|
416  | 
}  | 
|
417  | 
\end{lstlisting}
 | 
|
| 609 | 418  | 
|
419  | 
\end{frame}
 | 
|
| 701 | 420  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
421  | 
||
422  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
423  | 
\begin{frame}[fragile,c]
 | 
|
424  | 
\frametitle{LLVM Types}
 | 
|
425  | 
||
426  | 
\tt  | 
|
427  | 
\begin{center}
 | 
|
428  | 
\begin{tabular}{ll}
 | 
|
429  | 
boolean & i1 \\  | 
|
430  | 
byte & i8 \\  | 
|
431  | 
short & i16\\  | 
|
432  | 
char & i16\\  | 
|
433  | 
integer & i32\\  | 
|
434  | 
long & i64\\  | 
|
435  | 
float & float\\  | 
|
436  | 
double & double\\  | 
|
437  | 
*\_ & pointer to \\  | 
|
438  | 
**\_ & pointer to a pointer to\\  | 
|
439  | 
\mbox{}[\_]    & arrays of\\
 | 
|
440  | 
\end{tabular}
 | 
|
441  | 
\end{center}
 | 
|
442  | 
||
443  | 
\end{frame}
 | 
|
444  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
445  | 
||
446  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
447  | 
\begin{frame}[fragile,c]
 | 
|
| 702 | 448  | 
\frametitle{LLVM-IR Instructions}
 | 
| 701 | 449  | 
\small  | 
450  | 
||
| 702 | 451  | 
\begin{lstlisting}[language=LLVM,xleftmargin=0mm]
 | 
| 701 | 452  | 
br i1 %var, label %if_br, label %else_br  | 
453  | 
||
454  | 
icmp eq i32 %x, %y ; for equal  | 
|
455  | 
icmp sle i32 %x, %y ; signed less or equal  | 
|
456  | 
icmp slt i32 %x, %y ; signed less than  | 
|
457  | 
icmp ult i32 %x, %y ; unsigned less than  | 
|
458  | 
||
459  | 
%var = call i32 @foo(...args...)  | 
|
460  | 
\end{lstlisting}
 | 
|
461  | 
||
462  | 
\end{frame}
 | 
|
463  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
464  | 
||
465  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
466  | 
\begin{frame}[fragile,c]
 | 
|
467  | 
\frametitle{SSA Format}
 | 
|
468  | 
||
469  | 
\bl{$(1 + a) + (3 + (b * 5))$}\bigskip\bigskip
 | 
|
470  | 
||
471  | 
\begin{lstlisting}[language=LLVM]
 | 
|
| 702 | 472  | 
tmp0 = add 1 a  | 
473  | 
tmp1 = mul b 5  | 
|
474  | 
tmp2 = add 3 tmp1  | 
|
475  | 
tmp3 = add tmp0 tmp2  | 
|
476  | 
\end{lstlisting}\bigskip\bigskip
 | 
|
477  | 
||
478  | 
\hfill Static Single Assignment  | 
|
| 701 | 479  | 
|
480  | 
\end{frame}
 | 
|
481  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 609 | 482  | 
|
483  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 484  | 
\begin{frame}[fragile,c]
 | 
485  | 
\frametitle{Abstract Syntax Trees}
 | 
|
486  | 
\footnotesize  | 
|
487  | 
||
488  | 
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
 | 
|
489  | 
// Fun language (expressions)  | 
|
490  | 
abstract class Exp  | 
|
491  | 
abstract class BExp  | 
|
492  | 
||
493  | 
case class Call(name: String, args: List[Exp]) extends Exp  | 
|
494  | 
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp  | 
|
495  | 
case class Write(e: Exp) extends Exp  | 
|
496  | 
case class Var(s: String) extends Exp  | 
|
497  | 
case class Num(i: Int) extends Exp  | 
|
498  | 
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp  | 
|
499  | 
case class Sequence(e1: Exp, e2: Exp) extends Exp  | 
|
500  | 
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp  | 
|
501  | 
\end{lstlisting}
 | 
|
| 609 | 502  | 
|
| 701 | 503  | 
\end{frame}
 | 
504  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
505  | 
||
506  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
507  | 
\begin{frame}[fragile,c]
 | 
|
508  | 
\frametitle{K-(Intermediate)Language}
 | 
|
509  | 
\footnotesize  | 
|
| 609 | 510  | 
|
| 701 | 511  | 
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
 | 
512  | 
abstract class KExp  | 
|
513  | 
abstract class KVal  | 
|
514  | 
||
| 702 | 515  | 
// K-Values  | 
| 701 | 516  | 
case class KVar(s: String) extends KVal  | 
517  | 
case class KNum(i: Int) extends KVal  | 
|
518  | 
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal  | 
|
519  | 
case class KCall(o: String, vrs: List[KVal]) extends KVal  | 
|
520  | 
case class KWrite(v: KVal) extends KVal  | 
|
521  | 
||
| 702 | 522  | 
// K-Expressions  | 
| 701 | 523  | 
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp  | 
524  | 
case class KLet(x: String, v: KVal, e: KExp) extends KExp  | 
|
525  | 
case class KReturn(v: KVal) extends KExp  | 
|
| 609 | 526  | 
\end{lstlisting}
 | 
527  | 
||
| 701 | 528  | 
\end{frame}
 | 
529  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
530  | 
||
531  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
532  | 
\begin{frame}[fragile,c]
 | 
|
| 819 | 533  | 
\frametitle{KLet}
 | 
534  | 
||
535  | 
\begin{lstlisting}[language=LLVM]
 | 
|
536  | 
tmp0 = add 1 a  | 
|
537  | 
tmp1 = mul b 5  | 
|
538  | 
tmp2 = add 3 tmp1  | 
|
539  | 
tmp3 = add tmp0 tmp2  | 
|
540  | 
\end{lstlisting}
 | 
|
541  | 
||
542  | 
\begin{lstlisting}[language=LLVMIR]
 | 
|
543  | 
KLet tmp0 , add 1 a in  | 
|
544  | 
KLet tmp1 , mul b 5 in  | 
|
545  | 
KLet tmp2 , add 3 tmp1 in  | 
|
546  | 
KLet tmp3 , add tmp0 tmp2 in  | 
|
547  | 
...  | 
|
548  | 
\end{lstlisting}
 | 
|
549  | 
||
550  | 
\begin{lstlisting}[language=Scala,numbers=none]
 | 
|
551  | 
case class KLet(x: String, e1: KVal, e2: KExp)  | 
|
552  | 
\end{lstlisting}
 | 
|
553  | 
||
554  | 
\end{frame}
 | 
|
555  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
556  | 
||
557  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
558  | 
\begin{frame}[fragile,c]
 | 
|
559  | 
\frametitle{KLet}
 | 
|
560  | 
||
561  | 
\begin{lstlisting}[language=LLVM]
 | 
|
562  | 
tmp0 = add 1 a  | 
|
563  | 
tmp1 = mul b 5  | 
|
564  | 
tmp2 = add 3 tmp1  | 
|
565  | 
tmp3 = add tmp0 tmp2  | 
|
566  | 
\end{lstlisting}
 | 
|
567  | 
||
568  | 
\begin{lstlisting}[language=LLVMIR]
 | 
|
569  | 
let tmp0 = add 1 a in  | 
|
570  | 
let tmp1 = mul b 5 in  | 
|
571  | 
let tmp2 = add 3 tmp1 in  | 
|
572  | 
let tmp3 = add tmp0 tmp2 in  | 
|
573  | 
...  | 
|
574  | 
\end{lstlisting}
 | 
|
575  | 
||
576  | 
\begin{lstlisting}[language=Scala,numbers=none]
 | 
|
577  | 
case class KLet(x: String, e1: KVal, e2: KExp)  | 
|
578  | 
\end{lstlisting}
 | 
|
579  | 
||
580  | 
\end{frame}
 | 
|
581  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
582  | 
||
583  | 
||
584  | 
||
585  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
586  | 
\begin{frame}[fragile,c]
 | 
|
| 701 | 587  | 
\frametitle{CPS-Translation}
 | 
588  | 
\small  | 
|
589  | 
||
590  | 
\begin{lstlisting}[language=Scala,numbers=none]
 | 
|
591  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp =  | 
|
592  | 
  e match { ... }
 | 
|
593  | 
\end{lstlisting}
 | 
|
594  | 
\bigskip\bigskip  | 
|
595  | 
||
| 702 | 596  | 
the continuation \texttt{k} can be thought of:\medskip
 | 
597  | 
||
| 701 | 598  | 
\small  | 
599  | 
\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
 | 
|
600  | 
let tmp0 = add 1 a in  | 
|
601  | 
let tmp1 = mul (*@$\Box$@*) 5 in  | 
|
602  | 
let tmp2 = add 3 tmp1 in  | 
|
603  | 
let tmp3 = add tmp0 tmp2 in  | 
|
604  | 
KReturn tmp3  | 
|
605  | 
\end{lstlisting}
 | 
|
| 609 | 606  | 
|
607  | 
\end{frame}
 | 
|
| 701 | 608  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
609  | 
||
610  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
611  | 
\begin{frame}[fragile,c]
 | 
|
612  | 
\frametitle{CPS-Translation}
 | 
|
613  | 
\small  | 
|
614  | 
||
615  | 
\begin{lstlisting}[language=Scala,numbers=none]
 | 
|
616  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp =  | 
|
617  | 
  e match { 
 | 
|
618  | 
case Var(s) => k(KVar(s))  | 
|
619  | 
case Num(i) => k(KNum(i))  | 
|
620  | 
...  | 
|
621  | 
}  | 
|
622  | 
\end{lstlisting}
 | 
|
623  | 
\bigskip\bigskip  | 
|
624  | 
||
625  | 
\small  | 
|
626  | 
\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
 | 
|
627  | 
let tmp0 = add 1 a in  | 
|
628  | 
let tmp1 = mul (*@$\Box$@*) 5 in  | 
|
629  | 
let tmp2 = add 3 tmp1 in  | 
|
630  | 
let tmp3 = add tmp0 tmp2 in  | 
|
631  | 
KReturn tmp3  | 
|
632  | 
\end{lstlisting}
 | 
|
633  | 
||
634  | 
\end{frame}
 | 
|
635  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 609 | 636  | 
|
637  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 638  | 
\begin{frame}[fragile,c]
 | 
639  | 
\frametitle{CPS-Translation}
 | 
|
640  | 
\small  | 
|
| 609 | 641  | 
|
| 701 | 642  | 
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-5mm]
 | 
643  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp = e match { 
 | 
|
644  | 
  case Aop(o, e1, e2) => {
 | 
|
645  | 
    val z = Fresh("tmp")
 | 
|
646  | 
CPS(e1)(y1 =>  | 
|
647  | 
CPS(e2)(y2 =>  | 
|
648  | 
KLet(z, Kop(o, y1, y2), k(KVar(z)))))  | 
|
649  | 
} ...  | 
|
650  | 
}  | 
|
| 609 | 651  | 
\end{lstlisting}
 | 
| 702 | 652  | 
\mbox{}\\[-8mm]%
 | 
653  | 
%  | 
|
| 701 | 654  | 
\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
 | 
655  | 
...  | 
|
656  | 
let z = op (*@$\Box_{y_1}$@*) (*@$\Box_{y_2}$@*)
 | 
|
657  | 
let tmp0 = add 1 a in  | 
|
| 702 | 658  | 
let tmp1 = mul (*@$\Box\!\!\!\!\raisebox{0.6mm}{\alert{\texttt{z}}}$@*) 5 in 
 | 
| 701 | 659  | 
let tmp2 = add 3 tmp1 in  | 
660  | 
let tmp3 = add tmp0 tmp2 in  | 
|
661  | 
KReturn tmp3  | 
|
662  | 
\end{lstlisting}
 | 
|
| 609 | 663  | 
|
664  | 
\end{frame}
 | 
|
| 701 | 665  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
| 609 | 666  | 
|
667  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 668  | 
\begin{frame}[fragile,c]
 | 
669  | 
\frametitle{CPS-Translation}
 | 
|
670  | 
\small  | 
|
| 609 | 671  | 
|
| 701 | 672  | 
\begin{lstlisting}[language=Scala,numbers=none]
 | 
673  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp =  | 
|
674  | 
  e match { 
 | 
|
675  | 
case Sequence(e1, e2) =>  | 
|
676  | 
CPS(e1)(_ => CPS(e2)(y2 => k(y2)))  | 
|
677  | 
...  | 
|
678  | 
}  | 
|
679  | 
\end{lstlisting}
 | 
|
| 702 | 680  | 
\bigskip  | 
| 701 | 681  | 
|
682  | 
\small  | 
|
683  | 
\begin{lstlisting}[language=LLVMIR,numbers=none,xleftmargin=30mm,escapeinside={(*@}{@*)}]
 | 
|
684  | 
let tmp0 = add 1 a in  | 
|
685  | 
let tmp1 = mul (*@$\Box$@*) 5 in  | 
|
686  | 
let tmp2 = add 3 tmp1 in  | 
|
687  | 
let tmp3 = add tmp0 tmp2 in  | 
|
688  | 
KReturn tmp3  | 
|
689  | 
\end{lstlisting}
 | 
|
| 609 | 690  | 
|
691  | 
\end{frame}
 | 
|
| 701 | 692  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
| 
310
 
d384fe01d0e8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
309 
diff
changeset
 | 
693  | 
|
| 610 | 694  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
| 701 | 695  | 
\begin{frame}[fragile,c]
 | 
696  | 
\frametitle{CPS-Translation}
 | 
|
697  | 
\small  | 
|
698  | 
||
699  | 
\begin{lstlisting}[language=Scala,numbers=none,xleftmargin=-3mm]
 | 
|
700  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp =  | 
|
701  | 
  e match { 
 | 
|
702  | 
...  | 
|
703  | 
    case If(Bop(o, b1, b2), e1, e2) => {
 | 
|
704  | 
      val z = Fresh("tmp")
 | 
|
705  | 
CPS(b1)(y1 =>  | 
|
706  | 
CPS(b2)(y2 =>  | 
|
707  | 
KLet(z, Kop(o, y1, y2),  | 
|
708  | 
KIf(z, CPS(e1)(k), CPS(e2)(k)))))  | 
|
709  | 
}  | 
|
710  | 
...  | 
|
711  | 
}  | 
|
712  | 
\end{lstlisting}
 | 
|
713  | 
||
714  | 
\end{frame}
 | 
|
715  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
716  | 
||
| 702 | 717  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
718  | 
\begin{frame}[c,fragile]
 | 
|
719  | 
\frametitle{The Basic Language, 1980+}
 | 
|
720  | 
||
721  | 
\begin{lstlisting}[language={[Visual]Basic},numbers=none]
 | 
|
722  | 
5 LET S = 0  | 
|
723  | 
10 INPUT V  | 
|
724  | 
20 PRINT "Input number"  | 
|
725  | 
30 IF N = 0 THEN GOTO 99  | 
|
726  | 
40 FOR I = 1 TO N  | 
|
727  | 
45 LET S = S + V(I)  | 
|
728  | 
50 NEXT I  | 
|
729  | 
60 PRINT S/N  | 
|
730  | 
70 GOTO 5  | 
|
731  | 
99 END  | 
|
732  | 
\end{lstlisting}
 | 
|
733  | 
||
734  | 
\hfill ``Spaghetti Code''  | 
|
735  | 
\end{frame}
 | 
|
736  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
737  | 
||
738  | 
||
739  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
740  | 
\begin{frame}[c,fragile]
 | 
|
741  | 
\frametitle{Target Specific ASM}
 | 
|
742  | 
||
743  | 
\begin{lstlisting}[language={},numbers=none]
 | 
|
744  | 
llc -march=x86-64 fact.ll  | 
|
745  | 
llc -march=arm fact.ll  | 
|
746  | 
||
747  | 
||
748  | 
Intel: xorl %eax, %eax  | 
|
749  | 
ARM: mov pc, lr  | 
|
750  | 
\end{lstlisting}
 | 
|
751  | 
||
752  | 
\end{frame}
 | 
|
753  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 701 | 754  | 
|
755  | 
||
756  | 
||
757  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
| 610 | 758  | 
\begin{frame}[c]
 | 
759  | 
||
760  | 
\large\bf  | 
|
| 701 | 761  | 
Using a compiler, \\  | 
762  | 
how can you mount the\\  | 
|
763  | 
perfect attack against a system?  | 
|
| 610 | 764  | 
|
| 701 | 765  | 
\end{frame}
 | 
| 610 | 766  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
767  | 
||
768  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
769  | 
\mode<presentation>{
 | 
|
770  | 
\begin{frame}[c]
 | 
|
771  | 
||
772  | 
{\large\bf
 | 
|
773  | 
What is a \alert{perfect} attack?}\bigskip
 | 
|
774  | 
||
775  | 
\begin{enumerate}
 | 
|
776  | 
\item you can potentially completely take over a target system  | 
|
777  | 
\item your attack is (nearly) undetectable  | 
|
778  | 
\item the victim has (almost) no chance to recover  | 
|
779  | 
\end{enumerate}
 | 
|
780  | 
||
781  | 
\end{frame}}
 | 
|
782  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
783  | 
||
784  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
785  | 
\mode<presentation>{
 | 
|
786  | 
\begin{frame}[c]
 | 
|
787  | 
||
788  | 
||
789  | 
  \begin{center}
 | 
|
790  | 
  \begin{tikzpicture}[scale=1]
 | 
|
791  | 
||
792  | 
  \onslide<1->{
 | 
|
793  | 
  \node (A) at (0,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=17mm] {};
 | 
|
794  | 
  \node [below right] at (A.north west) {\footnotesize\begin{tabular}{@{}l@{}}
 | 
|
795  | 
  \only<1,2>{clean}\only<3->{\alert{hacked}}\\compiler\end{tabular}};}
 | 
|
796  | 
||
797  | 
||
798  | 
  \onslide<2->{
 | 
|
799  | 
  \node (B) at (-2,2)  [draw=black, rectangle, very thick, minimum height=10mm, minimum width=12mm] {};
 | 
|
800  | 
  \node [below right] at (B.north west) {\footnotesize\begin{tabular}{@{}l@{}}login\\(src)\end{tabular}};
 | 
|
801  | 
||
802  | 
  \node (C) at (2,2)  [draw=black, rectangle, very thick, minimum height=10mm, minimum width=12mm] {};
 | 
|
803  | 
  \node [below right] at (C.north west) {\footnotesize\begin{tabular}{@{}l@{}}login\\(bin)\end{tabular}};
 | 
|
804  | 
||
805  | 
\draw[->, line width=2mm] (B) -- (C);  | 
|
806  | 
}  | 
|
807  | 
||
808  | 
 \onslide<3->{\node [above left=-1.5mm] at (C.south east) {\footnotesize \alert{$\blacksquare$}};}
 | 
|
809  | 
||
810  | 
  \end{tikzpicture}
 | 
|
811  | 
  \end{center}
 | 
|
812  | 
||
813  | 
\end{frame}}
 | 
|
814  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
815  | 
||
816  | 
||
817  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
818  | 
\mode<presentation>{
 | 
|
819  | 
\begin{frame}[c]
 | 
|
820  | 
||
821  | 
  \begin{center}
 | 
|
822  | 
  \begin{tikzpicture}[scale=1]
 | 
|
823  | 
||
824  | 
  \onslide<1->{
 | 
|
825  | 
  \node (A) at (0,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
826  | 
  \node [below right] at (A.north west) {\small V0.01};
 | 
|
827  | 
  \node [below right] (A1) at (A.south west) {\small Scala};
 | 
|
828  | 
  \node [below right] (A1) at (A1.south west) {\small\textcolor{gray}{host language}};
 | 
|
829  | 
  \node [above right] at (A.north west) {my compiler (src)};}
 | 
|
830  | 
||
831  | 
  \onslide<2->{
 | 
|
832  | 
  \node (B) at (1.8,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
833  | 
  \node [below right] at (B.north west) {\small V0.02};
 | 
|
834  | 
  \node [below right] at (B.south west) {\small Scala};
 | 
|
835  | 
  \node at (3,0) {\ldots};
 | 
|
836  | 
||
837  | 
  \node (C) at (5,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
838  | 
  \node [below right] at (C.north west) {\small V1.00};
 | 
|
839  | 
  \node [below right] at (C.south west) {\small Scala};}
 | 
|
840  | 
||
841  | 
  \onslide<3->{
 | 
|
842  | 
  \node (D) at (6.8,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
843  | 
  \node [below right] at (D.north west) {\small V1.00};
 | 
|
844  | 
||
845  | 
  \node (E) at (6.8,2)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
846  | 
  \node [below right] at (E.north west) {\small V1.01};}
 | 
|
847  | 
||
848  | 
  \onslide<4->{
 | 
|
849  | 
  \node (F) at (8.6,0)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
850  | 
  \node [below right] at (F.north west) {\small V1.01};
 | 
|
851  | 
||
852  | 
  \node (G) at (8.6,2)  [draw=black, rectangle, very thick, minimum height=18mm, minimum width=14mm] {};
 | 
|
853  | 
  \node [below right] at (G.north west) {\small V1.02};
 | 
|
854  | 
  \node at (9.8,0) {\ldots};
 | 
|
855  | 
  \node at (9.8,2) {\ldots};
 | 
|
856  | 
  \node at (8,-2) {\textcolor{gray}{\begin{tabular}{@{}l@{}}no host language\\needed\end{tabular}}};
 | 
|
857  | 
}  | 
|
858  | 
||
859  | 
  \end{tikzpicture}
 | 
|
860  | 
  \end{center}
 | 
|
861  | 
||
862  | 
\end{frame}}
 | 
|
863  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
864  | 
||
865  | 
||
866  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
867  | 
  \mode<presentation>{
 | 
|
868  | 
  \begin{frame}<1-3>
 | 
|
869  | 
  \frametitle{\LARGE\begin{tabular}{c}Hacking Compilers 
 | 
|
870  | 
  \end{tabular}}
 | 
|
871  | 
||
872  | 
%Why is it so paramount to have a small trusted code base (TCB)?  | 
|
873  | 
\bigskip\bigskip  | 
|
874  | 
||
875  | 
  \begin{columns}
 | 
|
876  | 
  \begin{column}{2.7cm}
 | 
|
877  | 
  \begin{minipage}{2.5cm}%
 | 
|
878  | 
  \begin{tabular}{c@ {}}
 | 
|
879  | 
  \includegraphics[scale=0.2]{../pics/ken-thompson.jpg}\\[-1.8mm]
 | 
|
880  | 
\footnotesize Ken Thompson\\[-1.8mm]  | 
|
881  | 
\footnotesize Turing Award, 1983\\  | 
|
882  | 
  \end{tabular}
 | 
|
883  | 
  \end{minipage}
 | 
|
884  | 
  \end{column}
 | 
|
885  | 
  \begin{column}{9cm}
 | 
|
886  | 
  \begin{tabular}{l@ {\hspace{1mm}}p{8cm}}
 | 
|
887  | 
||
888  | 
& Ken Thompson showed how to hide a Trojan Horse in a  | 
|
889  | 
  compiler \textcolor{red}{without} leaving any traces in the source code.\\[2mm]
 | 
|
890  | 
||
891  | 
& No amount of source level verification will protect  | 
|
892  | 
you from such Thompson-hacks.\\[2mm]  | 
|
893  | 
||
894  | 
  \end{tabular}
 | 
|
895  | 
  \end{column}
 | 
|
896  | 
  \end{columns}
 | 
|
897  | 
||
898  | 
  \only<2>{
 | 
|
899  | 
  \begin{textblock}{6}(4,2)
 | 
|
900  | 
  \begin{tikzpicture}
 | 
|
901  | 
\draw (0,0) node[inner sep=3mm,fill=cream, ultra thick, draw=red, rounded corners=2mm]  | 
|
902  | 
  {\normalsize
 | 
|
903  | 
  \begin{minipage}{8cm}
 | 
|
904  | 
  \begin{quote}
 | 
|
905  | 
  \includegraphics[scale=0.05]{../pics/evil.png}
 | 
|
906  | 
  \begin{enumerate}
 | 
|
907  | 
\item[1)] Assume you ship the compiler as binary and also with sources.  | 
|
908  | 
\item[2)] Make the compiler aware when it compiles itself.  | 
|
909  | 
\item[3)] Add the Trojan horse.  | 
|
910  | 
\item[4)] Compile.  | 
|
911  | 
\item[5)] Delete Trojan horse from the sources of the compiler.  | 
|
912  | 
  \item[6)] Go on holiday for the rest of your life. ;o)\\[-7mm]\mbox{}
 | 
|
913  | 
  \end{enumerate}
 | 
|
914  | 
  \end{quote}
 | 
|
915  | 
  \end{minipage}};
 | 
|
916  | 
  \end{tikzpicture}
 | 
|
917  | 
  \end{textblock}}
 | 
|
918  | 
||
919  | 
  \end{frame}}
 | 
|
920  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
921  | 
||
| 701 | 922  | 
|
923  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
924  | 
  \begin{frame}[c]
 | 
|
925  | 
  \frametitle{Dijkstra on Testing}
 | 
|
926  | 
||
927  | 
  \begin{bubble}[10cm]
 | 
|
928  | 
``Program testing can be a very effective way to show the  | 
|
929  | 
presence of bugs, but it is hopelessly inadequate for showing  | 
|
930  | 
their absence.''  | 
|
931  | 
  \end{bubble}\bigskip
 | 
|
932  | 
||
933  | 
\small  | 
|
934  | 
What is good about compilers: the either seem to work,  | 
|
935  | 
or go horribly wrong (most of the time).  | 
|
936  | 
||
937  | 
  \end{frame}
 | 
|
938  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
939  | 
||
940  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
941  | 
\begin{frame}[c]
 | 
|
942  | 
\frametitle{\Large Proving Programs to be Correct}
 | 
|
943  | 
||
944  | 
\begin{bubble}[10cm]
 | 
|
945  | 
\small  | 
|
946  | 
{\bf Theorem:} There are infinitely many prime 
 | 
|
947  | 
numbers.\medskip\\  | 
|
948  | 
||
949  | 
{\bf Proof} \ldots\\
 | 
|
950  | 
\end{bubble}\bigskip
 | 
|
951  | 
||
952  | 
||
953  | 
similarly\bigskip  | 
|
954  | 
||
955  | 
\begin{bubble}[10cm]
 | 
|
956  | 
\small  | 
|
957  | 
{\bf Theorem:} The program is doing what 
 | 
|
958  | 
it is supposed to be doing.\medskip  | 
|
959  | 
||
960  | 
{\bf Long, long proof} \ldots\\
 | 
|
961  | 
\end{bubble}\bigskip\medskip
 | 
|
962  | 
||
963  | 
\small This can be a gigantic proof. The only hope is to have  | 
|
964  | 
help from the computer. `Program' is here to be understood to be  | 
|
965  | 
quite general (compiler, OS, \ldots).  | 
|
966  | 
||
967  | 
\end{frame}
 | 
|
968  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
969  | 
||
970  | 
||
971  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
972  | 
||
973  | 
\begin{frame}[c]
 | 
|
974  | 
\frametitle{Can This Be Done?}
 | 
|
975  | 
||
976  | 
\begin{itemize}
 | 
|
977  | 
\item in 2008, verification of a small C-compiler  | 
|
978  | 
\begin{itemize}
 | 
|
979  | 
\item ``if my input program has a certain behaviour, then the compiled machine code has the same behaviour''  | 
|
980  | 
\item is as good as \texttt{gcc -O1}, but much, much less buggy 
 | 
|
981  | 
\end{itemize}
 | 
|
982  | 
\end{itemize}
 | 
|
983  | 
||
984  | 
\begin{center}
 | 
|
985  | 
  \includegraphics[scale=0.12]{../pics/compcert.png}
 | 
|
986  | 
\end{center}
 | 
|
987  | 
||
988  | 
\end{frame}
 | 
|
989  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
990  | 
||
991  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
992  | 
\begin{frame}[t]
 | 
|
993  | 
\frametitle{Fuzzy Testing C-Compilers}
 | 
|
994  | 
||
995  | 
\begin{itemize}
 | 
|
996  | 
\item tested GCC, LLVM and others by randomly generating  | 
|
997  | 
C-programs  | 
|
998  | 
\item found more than 300 bugs in GCC and also  | 
|
999  | 
many in LLVM (some of them highest-level critical)\bigskip  | 
|
1000  | 
\item about CompCert:  | 
|
1001  | 
||
1002  | 
\begin{bubble}[10.7cm]\small ``The striking thing about our CompCert
 | 
|
1003  | 
results is that the middle-end bugs we found in all other  | 
|
1004  | 
compilers are absent. As of early 2011, the under-development  | 
|
1005  | 
version of CompCert is the only compiler we have tested for  | 
|
1006  | 
which Csmith cannot find wrong-code errors. This is not for  | 
|
1007  | 
lack of trying: we have devoted about six CPU-years to the  | 
|
1008  | 
task.''  | 
|
1009  | 
\end{bubble} 
 | 
|
1010  | 
\end{itemize}
 | 
|
1011  | 
||
1012  | 
\end{frame}
 | 
|
1013  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1014  | 
||
1015  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1016  | 
||
1017  | 
\begin{frame}[c]
 | 
|
1018  | 
\frametitle{Next Week}
 | 
|
1019  | 
||
1020  | 
\begin{itemize}
 | 
|
1021  | 
\item Revision Lecture\medskip  | 
|
1022  | 
\item How many  strings are in $\bl{L(a^*)}$?\pause\medskip
 | 
|
1023  | 
\item How many  strings are in $\bl{L((a + b)^*)}$?\\ Are there more than
 | 
|
1024  | 
  in $\bl{L(a^*)}$?
 | 
|
1025  | 
\end{itemize}
 | 
|
1026  | 
||
1027  | 
||
1028  | 
||
1029  | 
\end{frame}
 | 
|
1030  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1031  | 
||
| 822 | 1032  | 
\begin{frame}<1-20>[c]
 | 
1033  | 
\end{frame}
 | 
|
| 701 | 1034  | 
|
1035  | 
\end{document}
 | 
|
1036  | 
||
1037  | 
%%% Local Variables:  | 
|
1038  | 
%%% mode: latex  | 
|
1039  | 
%%% TeX-master: t  | 
|
1040  | 
%%% End:  | 
|
1041  | 
||
1042  | 
||
1043  | 
||
1044  | 
||
1045  | 
||
| 610 | 1046  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
1047  | 
  \begin{frame}[c]
 | 
|
1048  | 
||
1049  | 
  \begin{center}
 | 
|
1050  | 
  \includegraphics[scale=0.6]{../pics/bridge-limits.png}
 | 
|
1051  | 
  \end{center}
 | 
|
1052  | 
||
1053  | 
  \end{frame}
 | 
|
1054  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1055  | 
||
1056  | 
||
1057  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1058  | 
\begin{frame}[c]
 | 
|
1059  | 
\frametitle{Compilers \& Boeings 777}
 | 
|
1060  | 
||
1061  | 
First flight in 1994. They want to achieve triple redundancy in hardware  | 
|
1062  | 
faults.\bigskip  | 
|
1063  | 
||
1064  | 
They compile 1 Ada program to\medskip  | 
|
1065  | 
||
1066  | 
\begin{itemize}
 | 
|
1067  | 
\item Intel 80486  | 
|
1068  | 
\item Motorola 68040 (old Macintosh's)  | 
|
1069  | 
\item AMD 29050 (RISC chips used often in laser printers)  | 
|
1070  | 
\end{itemize}\medskip
 | 
|
1071  | 
||
1072  | 
using 3 independent compilers.\bigskip\pause  | 
|
1073  | 
||
1074  | 
\small Airbus uses C and static analysers. Recently started using CompCert.  | 
|
1075  | 
||
1076  | 
\end{frame}
 | 
|
1077  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1078  | 
||
1079  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1080  | 
\begin{frame}[c]
 | 
|
1081  | 
\frametitle{Goal}
 | 
|
1082  | 
||
1083  | 
Remember the Bridges example?  | 
|
1084  | 
||
1085  | 
\begin{itemize}
 | 
|
1086  | 
\item Can we look at our programs and somehow ensure  | 
|
1087  | 
they are bug free/correct?\pause\bigskip  | 
|
1088  | 
||
1089  | 
\item Very hard: Anything interesting about programs is equivalent  | 
|
1090  | 
to the Halting Problem, which is undecidable.\pause\bigskip  | 
|
1091  | 
||
1092  | 
\item \alert{Solution:} We avoid this ``minor'' obstacle by
 | 
|
1093  | 
being as close as possible of deciding the halting  | 
|
1094  | 
problem, without actually deciding the halting problem.  | 
|
1095  | 
\small$\quad\Rightarrow$ yes, no, don't know (static analysis)  | 
|
1096  | 
\end{itemize}
 | 
|
1097  | 
||
1098  | 
\end{frame}
 | 
|
1099  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1100  | 
||
1101  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1102  | 
  \begin{frame}[c]
 | 
|
1103  | 
  \frametitle{What is Static Analysis?}
 | 
|
1104  | 
||
1105  | 
  \begin{center}
 | 
|
1106  | 
  \includegraphics[scale=0.4]{../pics/state.png}
 | 
|
1107  | 
  \end{center}
 | 
|
1108  | 
||
1109  | 
  \begin{itemize}
 | 
|
1110  | 
\item depending on some initial input, a program  | 
|
1111  | 
(behaviour) will ``develop'' over time.  | 
|
1112  | 
  \end{itemize}
 | 
|
1113  | 
||
1114  | 
  \end{frame}
 | 
|
1115  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1116  | 
||
1117  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1118  | 
  \begin{frame}[c]
 | 
|
1119  | 
  \frametitle{What is Static Analysis?}
 | 
|
1120  | 
||
1121  | 
  \begin{center}
 | 
|
1122  | 
  \includegraphics[scale=0.4]{../pics/state2.png}
 | 
|
1123  | 
  \end{center}
 | 
|
1124  | 
||
1125  | 
  \end{frame}
 | 
|
1126  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1127  | 
||
1128  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1129  | 
  \begin{frame}[c]
 | 
|
1130  | 
  \frametitle{What is Static Analysis?}
 | 
|
1131  | 
||
1132  | 
  \begin{center}
 | 
|
1133  | 
  \includegraphics[scale=0.4]{../pics/state3.jpg}
 | 
|
1134  | 
  \end{center}
 | 
|
1135  | 
||
1136  | 
  \end{frame}
 | 
|
1137  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1138  | 
||
1139  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1140  | 
  \begin{frame}[c]
 | 
|
1141  | 
  \frametitle{What is Static Analysis?}
 | 
|
1142  | 
||
1143  | 
  \begin{center}
 | 
|
1144  | 
  \includegraphics[scale=0.4]{../pics/state4.jpg}
 | 
|
1145  | 
  \end{center}
 | 
|
1146  | 
||
1147  | 
  \begin{itemize}
 | 
|
1148  | 
\item to be avoided  | 
|
1149  | 
  \end{itemize}
 | 
|
1150  | 
||
1151  | 
  \end{frame}
 | 
|
1152  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1153  | 
||
1154  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1155  | 
  \begin{frame}[c]
 | 
|
1156  | 
  \frametitle{What is Static Analysis?}
 | 
|
1157  | 
||
1158  | 
  \begin{center}
 | 
|
1159  | 
  \includegraphics[scale=0.4]{../pics/state5.png}
 | 
|
1160  | 
  \end{center}
 | 
|
1161  | 
||
1162  | 
  \begin{itemize}
 | 
|
1163  | 
\item this needs more work  | 
|
1164  | 
  \end{itemize}
 | 
|
1165  | 
||
1166  | 
  \end{frame}
 | 
|
1167  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1168  | 
||
1169  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1170  | 
  \begin{frame}[c]
 | 
|
1171  | 
  \frametitle{What is Static Analysis?}
 | 
|
1172  | 
||
1173  | 
  \begin{center}
 | 
|
1174  | 
  \includegraphics[scale=0.4]{../pics/state6.png}
 | 
|
1175  | 
  \end{center}
 | 
|
1176  | 
||
1177  | 
  \end{frame}
 | 
|
1178  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1179  | 
||
1180  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1181  | 
  \begin{frame}[c,fragile]
 | 
|
1182  | 
    \frametitle{\Large\begin{tabular}{c}Concrete Example:\\[-1mm]
 | 
|
1183  | 
                  Are Vars Definitely Initialised?\end{tabular}}
 | 
|
1184  | 
||
1185  | 
Assuming \texttt{x} is initialised, what about \texttt{y}?\bigskip
 | 
|
1186  | 
||
1187  | 
Prog.~1:\\  | 
|
1188  | 
\begin{lstlisting}[numbers=none,
 | 
|
1189  | 
basicstyle=\ttfamily,  | 
|
1190  | 
language=While,xleftmargin=3mm]  | 
|
1191  | 
if x < 1 then y := x else y := x + 1;  | 
|
1192  | 
y := y + 1  | 
|
1193  | 
\end{lstlisting}\medskip     
 | 
|
1194  | 
||
1195  | 
Prog.~2:\\  | 
|
1196  | 
\begin{lstlisting}[numbers=none,
 | 
|
1197  | 
basicstyle=\ttfamily,  | 
|
1198  | 
language=While,xleftmargin=3mm]  | 
|
1199  | 
if x < x then y := y + 1 else y := x;  | 
|
1200  | 
y := y + 1  | 
|
1201  | 
\end{lstlisting}            
 | 
|
1202  | 
||
1203  | 
  \end{frame}
 | 
|
1204  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1205  | 
||
1206  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1207  | 
  \begin{frame}[c,fragile]
 | 
|
1208  | 
    \frametitle{\Large\begin{tabular}{c}Concrete Example:\\[-1mm]
 | 
|
1209  | 
                  Are Vars Definitely Initialised?\end{tabular}}
 | 
|
1210  | 
||
1211  | 
What should the rules be for deciding when a  | 
|
1212  | 
variable is initialised?\bigskip\pause  | 
|
1213  | 
||
1214  | 
\begin{itemize}
 | 
|
1215  | 
\item variable \texttt{x} is definitely initialized after
 | 
|
1216  | 
  \texttt{skip}\\
 | 
|
1217  | 
  iff \texttt{x} is definitely initialized before \texttt{skip}.
 | 
|
1218  | 
\end{itemize}
 | 
|
1219  | 
||
1220  | 
\end{frame}
 | 
|
1221  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1222  | 
||
1223  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1224  | 
  \begin{frame}[c,fragile]
 | 
|
1225  | 
%    \frametitle{\Large\begin{tabular}{c}Concrete Example:\\[-1mm]
 | 
|
1226  | 
%                  Are Vars Definitely Initialised?\end{tabular}}
 | 
|
1227  | 
||
1228  | 
$\bl{A}$ is the set of definitely defined variables:
 | 
|
1229  | 
||
1230  | 
\begin{center}
 | 
|
1231  | 
\begin{tabular}{c}
 | 
|
| 612 | 1232  | 
  \bl{\infer{\mbox{}}{A\triangleright\texttt{skip}\triangleright{}A}}\qquad
 | 
1233  | 
  \bl{\infer{vars(a) \subseteq A}{A\triangleright
 | 
|
1234  | 
  (\texttt{x\,:=\,a})\triangleright\{x\}\cup A}}
 | 
|
| 610 | 1235  | 
\medskip\\\pause  | 
1236  | 
||
| 612 | 1237  | 
  \bl{\infer{A_1\triangleright{}s_1\triangleright{}A_2
 | 
1238  | 
  \quad A_2\triangleright{}s_2\triangleright{}A_3}
 | 
|
1239  | 
  {A_1\triangleright{}(s_1 ; s_2)\triangleright{}A_3}}
 | 
|
| 610 | 1240  | 
\medskip\\\pause  | 
1241  | 
||
| 612 | 1242  | 
  \bl{\infer{vars(b)\subseteq A\quad A\triangleright{}s_1\triangleright{}A_1
 | 
1243  | 
  \quad A\triangleright{}s_2\triangleright{}A_2}
 | 
|
1244  | 
  {A\triangleright(\texttt{if}\;b\;\texttt{then}\;s_1\;\texttt{else}\;s_2)\triangleright{}A_1\cap A_2}}
 | 
|
| 610 | 1245  | 
\medskip\\\pause  | 
1246  | 
||
| 612 | 1247  | 
  \bl{\infer{vars(b)\subseteq A\quad A\triangleright{}s\triangleright{}A'}
 | 
1248  | 
  {A\triangleright(\texttt{while}\;b\;\texttt{do}\;s)\triangleright{}A}}\pause
 | 
|
| 610 | 1249  | 
\end{tabular}  
 | 
1250  | 
\end{center}
 | 
|
1251  | 
||
1252  | 
\hfill we start with $\bl{A = \{\}}$
 | 
|
1253  | 
\end{frame}
 | 
|
1254  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  | 
|
1255  |