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