author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 02 Oct 2023 23:10:56 +0100 | |
changeset 936 | 0b5f06539a84 |
parent 899 | 569a3adb6e01 |
child 940 | 46eee459a999 |
permissions | -rw-r--r-- |
697 | 1 |
% !TEX program = xelatex |
744 | 2 |
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer} |
3 |
%%\usepackage{xcolor} |
|
306
fecffce112fa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
4 |
\usepackage{../slides} |
215
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
197
diff
changeset
|
5 |
\usepackage{../langs} |
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
197
diff
changeset
|
6 |
\usepackage{../data} |
871 | 7 |
\usepackage{../graphicss} |
600 | 8 |
\usepackage{../grammar} |
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
9 |
|
608 | 10 |
\usepackage[most]{tcolorbox} |
11 |
||
12 |
\newtcbox{\hl}[1][]{% |
|
13 |
size=fbox, |
|
14 |
tcbox raise base, nobeforeafter, |
|
15 |
enhanced, colframe=gray, |
|
16 |
colback=gray!30, boxrule=1pt, |
|
17 |
fontupper=\ttfamily, |
|
18 |
#1} |
|
19 |
||
20 |
\lstset{morekeywords={def,if,then,else,write,read},keywordstyle=\color{codepurple}\bfseries} |
|
21 |
||
22 |
% beamer stuff |
|
23 |
\renewcommand{\slidecaption}{CFL 09, King's College London} |
|
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
\newcommand{\bl}[1]{\textcolor{blue}{#1}} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
|
608 | 26 |
|
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
\begin{document} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
899 | 29 |
|
30 |
||
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
306
fecffce112fa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
32 |
\begin{frame}[t] |
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
\frametitle{% |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
\begin{tabular}{@ {}c@ {}} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
\\[-3mm] |
459 | 36 |
\LARGE Compilers and \\[-2mm] |
899 | 37 |
\LARGE Formal Languages\\[-3mm] |
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
\end{tabular}} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
899 | 40 |
\normalsize |
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
\begin{center} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
\begin{tabular}{ll} |
899 | 43 |
Email: & christian.urban at kcl.ac.uk\\ |
44 |
Office Hour: & Fridays 11 -- 12\\ |
|
45 |
Location: & N7.07 (North Wing, Bush House)\\ |
|
46 |
Slides \& Progs: & KEATS\\ |
|
47 |
Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ |
|
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
\end{tabular} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
\end{center} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
|
744 | 51 |
\begin{center} |
52 |
\begin{tikzpicture} |
|
53 |
\node[drop shadow,fill=white,inner sep=0pt] |
|
54 |
{\footnotesize\rowcolors{1}{capri!10}{white} |
|
55 |
\begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline |
|
56 |
1 Introduction, Languages & 6 While-Language \\ |
|
57 |
2 Regular Expressions, Derivatives & 7 Compilation, JVM \\ |
|
58 |
3 Automata, Regular Languages & \cellcolor{blue!50} 8 Compiling Functional Languages \\ |
|
59 |
4 Lexing, Tokenising & 9 Optimisations \\ |
|
60 |
5 Grammars, Parsing & 10 LLVM \\ \hline |
|
61 |
\end{tabular}% |
|
62 |
}; |
|
63 |
\end{tikzpicture} |
|
64 |
\end{center} |
|
306
fecffce112fa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
65 |
\end{frame} |
608 | 66 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
65
diff
changeset
|
67 |
|
899 | 68 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
69 |
\begin{frame}[t,fragile] |
|
70 |
%%\frametitle{CW2} |
|
71 |
||
72 |
\small |
|
73 |
\begin{textblock}{13}(-0.5,1) |
|
74 |
\begin{lstlisting}[numbers=none] |
|
75 |
def mkeps(r: Rexp) : Val = r match { |
|
76 |
... |
|
77 |
case PLUS(r) => Stars(List(mkeps(r))) |
|
78 |
case OPTIONAL(r) => Stars(Nil) |
|
79 |
case NTIMES(r, n) => Stars(List.fill(n)(mkeps(r))) |
|
80 |
} |
|
81 |
||
82 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
83 |
... |
|
84 |
case (RANGE(_), Empty) => Chr(c) |
|
85 |
case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
86 |
case (OPTIONAL(r), v) => Stars(inj(r, c, v)::Nil) |
|
87 |
case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
88 |
} |
|
89 |
\end{lstlisting} |
|
90 |
\end{textblock} |
|
91 |
||
92 |
||
93 |
\begin{textblock}{5}(3,13.5) |
|
94 |
\bl{$r^? \dn r^{\{0..1\}}$} |
|
95 |
\end{textblock} |
|
96 |
||
97 |
\end{frame} |
|
98 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
99 |
||
100 |
||
101 |
||
813 | 102 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
103 |
% \begin{frame}[c] |
|
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
65
diff
changeset
|
104 |
|
813 | 105 |
% \begin{center} |
106 |
% \bl{\begin{tabular}{@{}lcl@{}} |
|
107 |
% \meta{Stmt} & $::=$ & $\texttt{skip}$\\ |
|
108 |
% & $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\ |
|
109 |
% & $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\ |
|
110 |
% & $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\ |
|
111 |
% & $|$ & \texttt{read}\;\textit{Id}\\ |
|
112 |
% & $|$ & \texttt{write}\;\textit{Id}\\ |
|
113 |
% & $|$ & \texttt{write}\;\textit{String}\medskip\\ |
|
114 |
% \meta{Stmts} & $::=$ & \meta{Stmt} \;\texttt{;}\; \meta{Stmts}\\ |
|
115 |
% & $|$ & \meta{Stmt}\medskip\\ |
|
116 |
% \meta{Block} & $::=$ & \texttt{\{}\,\meta{Stmts}\,\texttt{\}}\\ |
|
117 |
% & $|$ & \meta{Stmt}\medskip\\ |
|
118 |
% \meta{AExp} & $::=$ & \ldots\\ |
|
119 |
% \meta{BExp} & $::=$ & \ldots\\ |
|
120 |
% \end{tabular}} |
|
121 |
% \end{center} |
|
122 |
% \end{frame} |
|
123 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
67
diff
changeset
|
124 |
|
813 | 125 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
126 |
% \begin{frame}[t,fragile] |
|
127 |
% \frametitle{Compiling AExps} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
128 |
|
813 | 129 |
% For example \bl{$1 + ((2 * 3) + (4 - 3))$}:\medskip |
608 | 130 |
|
813 | 131 |
% \begin{columns}[T] |
132 |
% \begin{column}{.3\textwidth} |
|
133 |
% \begin{center} |
|
134 |
% \bl{\begin{tikzpicture} |
|
135 |
% \tikzset{level distance=12mm,sibling distance=4mm} |
|
136 |
% \tikzset{edge from parent/.style={draw,very thick}} |
|
137 |
% \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]] |
|
138 |
% \end{tikzpicture}} |
|
139 |
% \end{center} |
|
140 |
% \end{column} |
|
141 |
% \begin{column}{.3\textwidth} |
|
142 |
% \begin{lstlisting}[language=JVMIS,numbers=none] |
|
143 |
% ldc 1 |
|
144 |
% ldc 2 |
|
145 |
% ldc 3 |
|
146 |
% imul |
|
147 |
% ldc 4 |
|
148 |
% ldc 3 |
|
149 |
% isub |
|
150 |
% iadd |
|
151 |
% iadd |
|
152 |
% \end{lstlisting} |
|
153 |
% \end{column} |
|
154 |
% \end{columns}\bigskip |
|
535 | 155 |
|
813 | 156 |
% \small |
157 |
% Traverse tree in post-order \bl{$\Rightarrow$} code for |
|
158 |
% stack-machine |
|
535 | 159 |
|
813 | 160 |
% \end{frame} |
161 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
162 |
|
813 | 163 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
164 |
% \begin{frame}[c,fragile] |
|
165 |
% \frametitle{Compiling AExps} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
166 |
|
813 | 167 |
% \bl{ |
168 |
% \begin{center} |
|
169 |
% \begin{tabular}{lcl} |
|
170 |
% $\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\smallskip\\ |
|
171 |
% $\textit{compile}(a_1 + a_2, E)$ & $\dn$ \\ |
|
172 |
% \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$}\smallskip\\ |
|
173 |
% $\textit{compile}(a_1 - a_2, E)$ & $\dn$ \\ |
|
174 |
% \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$}\smallskip\\ |
|
175 |
% $\textit{compile}(a_1 * a_2, E)$ & $\dn$ \\ |
|
176 |
% \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$}\smallskip\\ |
|
177 |
% $\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$\\ |
|
178 |
% \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$}\smallskip\\ |
|
179 |
% $\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\ |
|
180 |
% \end{tabular} |
|
181 |
% \end{center}} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
182 |
|
813 | 183 |
% \end{frame} |
184 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
185 |
|
813 | 186 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
187 |
% \begin{frame}[c,fragile] |
|
188 |
% \frametitle{Compiling Ifs} |
|
534 | 189 |
|
813 | 190 |
% For example |
534 | 191 |
|
813 | 192 |
% \begin{lstlisting}[mathescape,numbers=none,language=While] |
193 |
% if 1 = 1 then x := 2 else y := 3 |
|
194 |
% \end{lstlisting} |
|
534 | 195 |
|
196 |
||
813 | 197 |
% \begin{center} |
198 |
% \begin{lstlisting}[mathescape,language=JVMIS,numbers=none] |
|
199 |
% ldc 1 |
|
200 |
% ldc 1 |
|
201 |
% if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$ |
|
202 |
% ldc 2 |
|
203 |
% istore 0 |
|
204 |
% goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$ |
|
205 |
% L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$ |
|
206 |
% ldc 3 |
|
207 |
% istore 1 |
|
208 |
% L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$ |
|
209 |
% \end{lstlisting} |
|
210 |
% \end{center} |
|
534 | 211 |
|
813 | 212 |
% \begin{tikzpicture}[remember picture,overlay] |
213 |
% \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) |
|
214 |
% -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east); |
|
215 |
% \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
|
216 |
% -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); |
|
217 |
% \end{tikzpicture} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
218 |
|
813 | 219 |
% \end{frame} |
220 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
221 |
|
813 | 222 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
223 |
% \begin{frame}[c,fragile] |
|
224 |
% \frametitle{Compiling Whiles} |
|
534 | 225 |
|
813 | 226 |
% For example |
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
227 |
|
813 | 228 |
% \begin{lstlisting}[mathescape,numbers=none,language=While] |
229 |
% while x <= 10 do x := x + 1 |
|
230 |
% \end{lstlisting} |
|
534 | 231 |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
232 |
|
813 | 233 |
% \begin{center} |
234 |
% \begin{lstlisting}[mathescape,language=JVMIS,numbers=none] |
|
235 |
% L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$ |
|
236 |
% iload 0 |
|
237 |
% ldc 10 |
|
238 |
% if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$ |
|
239 |
% iload 0 |
|
240 |
% ldc 1 |
|
241 |
% iadd |
|
242 |
% istore 0 |
|
243 |
% goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$ |
|
244 |
% L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$ |
|
245 |
% \end{lstlisting} |
|
246 |
% \end{center} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
247 |
|
813 | 248 |
% \begin{tikzpicture}[remember picture,overlay] |
249 |
% \draw[->,very thick] (LA) edge [->,to path={-- ++(12mm,0mm) |
|
250 |
% -- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east); |
|
251 |
% \draw[->,very thick] (LC) edge [->,to path={-- ++(12mm,0mm) |
|
252 |
% -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east); |
|
253 |
% \end{tikzpicture} |
|
534 | 254 |
|
813 | 255 |
% \end{frame} |
256 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
257 |
|
813 | 258 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
259 |
% \begin{frame}[c,fragile] |
|
260 |
% \frametitle{Compiling Writes} |
|
534 | 261 |
|
813 | 262 |
% \small |
263 |
% \begin{lstlisting}[language=JVMIS,mathescape, |
|
264 |
% numbers=none,xleftmargin=-6mm] |
|
265 |
% .method public static write(I)V |
|
266 |
% .limit locals 1 |
|
267 |
% .limit stack 2 |
|
268 |
% getstatic java/lang/System/out |
|
269 |
% Ljava/io/PrintStream; |
|
270 |
% iload 0 |
|
271 |
% invokevirtual java/io/PrintStream/println(I)V |
|
272 |
% return |
|
273 |
% .end method |
|
534 | 274 |
|
275 |
||
276 |
||
813 | 277 |
% iload $E(x)$ |
278 |
% invokestatic XXX/XXX/write(I)V |
|
279 |
% \end{lstlisting} |
|
534 | 280 |
|
813 | 281 |
% \end{frame} |
282 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
534 | 283 |
|
813 | 284 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
285 |
% \begin{frame}[c,fragile] |
|
286 |
% \frametitle{Compiling Main} |
|
534 | 287 |
|
813 | 288 |
% \footnotesize |
289 |
% \begin{lstlisting}[language=JVMIS,mathescape, |
|
290 |
% numbers=none,xleftmargin=-6mm] |
|
291 |
% .class public XXX.XXX |
|
292 |
% .super java/lang/Object |
|
534 | 293 |
|
813 | 294 |
% .method public <init>()V |
295 |
% aload_0 |
|
296 |
% invokenonvirtual java/lang/Object/<init>()V |
|
297 |
% return |
|
298 |
% .end method |
|
534 | 299 |
|
813 | 300 |
% .method public static main([Ljava/lang/String;)V |
301 |
% .limit locals 200 |
|
302 |
% .limit stack 200 |
|
534 | 303 |
|
813 | 304 |
% $\textit{\ldots{}here comes the compiled code\ldots}$ |
534 | 305 |
|
813 | 306 |
% return |
307 |
% .end method |
|
308 |
% \end{lstlisting} |
|
534 | 309 |
|
813 | 310 |
% \end{frame} |
311 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
534 | 312 |
|
313 |
||
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
314 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 315 |
\begin{frame}[c,fragile] |
316 |
\frametitle{Functional Programming} |
|
317 |
||
318 |
\footnotesize |
|
319 |
\begin{textblock}{13}(0.9,3) |
|
320 |
\begin{lstlisting}[]numbers=none] |
|
321 |
def fib(n) = if n == 0 then 0 |
|
322 |
else if n == 1 then 1 |
|
323 |
else fib(n - 1) + fib(n - 2); |
|
324 |
||
325 |
def fact(n) = if n == 0 then 1 else n * fact(n - 1); |
|
326 |
||
327 |
def ack(m, n) = if m == 0 then n + 1 |
|
328 |
else if n == 0 then ack(m - 1, 1) |
|
329 |
else ack(m - 1, ack(m, n - 1)); |
|
330 |
||
331 |
def gcd(a, b) = if b == 0 then a else gcd(b, a % b); |
|
332 |
\end{lstlisting} |
|
333 |
\end{textblock} |
|
334 |
||
335 |
\end{frame} |
|
336 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
337 |
|
608 | 338 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
339 |
\begin{frame}[c] |
|
697 | 340 |
\frametitle{Fun-Grammar} |
608 | 341 |
\bl{ |
342 |
\begin{plstx}[rhs style=] |
|
697 | 343 |
: \meta{Exp} ::= \meta{Var} | \meta{Num}{\hspace{4cm}} |
608 | 344 |
| \meta{Exp} + \meta{Exp} | ... | (\meta{Exp}) |
345 |
| \code{if} \meta{BExp} \code{then} \meta{Exp} \code{else} \meta{Exp} |
|
697 | 346 |
| \code{write} \meta{Exp} {\hspace{5cm}} |
608 | 347 |
| \meta{Exp} ; \meta{Exp} |
348 |
| \textit{FunName} (\meta{Exp}, ... , \meta{Exp})\\ |
|
349 |
: \meta{BExp} ::= ...\\ |
|
350 |
: \meta{Def} ::= \code{def} \textit{FunName} ($\hspace{0.4mm}x_1$, ... , $x_n$) = \meta{Exp}\\ |
|
351 |
: \meta{Prog} ::= \meta{Def} ; \meta{Prog} |
|
352 |
| \meta{Exp} ; \meta{Prog} |
|
353 |
| \meta{Exp}\\ |
|
354 |
\end{plstx}} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
355 |
|
608 | 356 |
% Almost what is implemented - Exp ; Exp is left-recursive |
357 |
% |
|
358 |
\end{frame} |
|
359 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
360 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
361 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 362 |
\begin{frame}[c, fragile] |
363 |
\frametitle{Abstract Syntax Trees} |
|
364 |
||
365 |
\footnotesize |
|
366 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm] |
|
367 |
abstract class Exp |
|
368 |
abstract class BExp |
|
369 |
abstract class Decl |
|
370 |
||
371 |
case class Var(s: String) extends Exp |
|
372 |
case class Num(i: Int) extends Exp |
|
373 |
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
|
374 |
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp |
|
375 |
case class Write(e: Exp) extends Exp |
|
376 |
case class Sequ(e1: Exp, e2: Exp) extends Exp |
|
377 |
case class Call(name: String, args: List[Exp]) extends Exp |
|
378 |
||
379 |
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
|
380 |
||
381 |
case class Def(name: String, |
|
382 |
args: List[String], |
|
383 |
body: Exp) extends Decl |
|
384 |
case class Main(e: Exp) extends Decl |
|
385 |
\end{lstlisting} |
|
386 |
||
387 |
\end{frame} |
|
388 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
389 |
||
390 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
391 |
\begin{frame}[c, fragile] |
|
697 | 392 |
\frametitle{Ideas} |
608 | 393 |
|
697 | 394 |
Use separate JVM methods for each Fun-function.\bigskip |
395 |
||
396 |
Compile \bl{\texttt{exp}}s such that the result of the expression |
|
608 | 397 |
is on top of the stack.\bigskip |
398 |
||
399 |
\begin{itemize} |
|
400 |
\item \texttt{\color{codepurple}{write}}\texttt{(1 + 2)} |
|
401 |
\item \texttt{1 + 2; 3 + 4} |
|
402 |
\end{itemize} |
|
403 |
||
404 |
\end{frame} |
|
405 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
406 |
|
608 | 407 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
408 |
\begin{frame}[c, fragile] |
|
409 |
\frametitle{Sequences} |
|
410 |
||
411 |
Compiling \texttt{exp1 ; exp2}:\bigskip |
|
412 |
||
413 |
||
414 |
\begin{lstlisting}[mathescape,language=JVMIS, numbers=none] |
|
415 |
$\textit{compile}$(exp1) |
|
416 |
pop |
|
417 |
$\textit{compile}$(exp2) |
|
418 |
\end{lstlisting} |
|
419 |
||
420 |
\end{frame} |
|
421 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
422 |
||
423 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
424 |
\begin{frame}[t, fragile] |
|
425 |
\frametitle{Write} |
|
426 |
||
427 |
Compiling call to \texttt{write(1+2)}:\bigskip |
|
428 |
||
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
429 |
|
608 | 430 |
\begin{lstlisting}[mathescape,language=JVMIS, numbers=none] |
431 |
$\textit{compile}$(1+2) |
|
432 |
dup |
|
433 |
invokestatic XXX/XXX/write(I)V |
|
434 |
\end{lstlisting}\bigskip |
|
435 |
||
436 |
\small |
|
437 |
needs the helper method |
|
438 |
||
439 |
\footnotesize |
|
440 |
\begin{lstlisting}[language=JVMIS, |
|
441 |
xleftmargin=2mm, |
|
442 |
numbers=none] |
|
443 |
.method public static write(I)V |
|
444 |
.limit locals 1 |
|
445 |
.limit stack 2 |
|
446 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
447 |
iload 0 |
|
448 |
invokevirtual java/io/PrintStream/println(I)V |
|
449 |
return |
|
450 |
.end method |
|
451 |
\end{lstlisting} |
|
452 |
\end{frame} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
453 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
454 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
455 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
456 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
457 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 458 |
\begin{frame}[t, fragile] |
459 |
\frametitle{Function Definitions} |
|
460 |
||
461 |
\footnotesize |
|
462 |
\begin{lstlisting}[language=JVMIS, |
|
463 |
xleftmargin=2mm, |
|
464 |
numbers=none] |
|
465 |
.method public static write(I)V |
|
466 |
.limit locals 1 |
|
467 |
.limit stack 2 |
|
468 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
469 |
iload 0 |
|
470 |
invokevirtual java/io/PrintStream/println(I)V |
|
471 |
return |
|
472 |
.end method |
|
473 |
\end{lstlisting}\bigskip |
|
474 |
||
697 | 475 |
\small We will need methods for definitions like\footnotesize\medskip |
608 | 476 |
|
477 |
\begin{lstlisting}[language=JVMIS, |
|
478 |
xleftmargin=2mm, |
|
479 |
numbers=none] |
|
480 |
def fname (x1, ... , xn) = ... |
|
481 |
||
482 |
.method public static fname (I...I)I |
|
483 |
.limit locals ?? |
|
484 |
.limit stack ?? |
|
485 |
?? |
|
486 |
.end method |
|
487 |
\end{lstlisting} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
488 |
|
608 | 489 |
\end{frame} |
490 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
491 |
||
492 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
493 |
\begin{frame}[c, fragile] |
|
494 |
\frametitle{Stack Estimation} |
|
495 |
\small |
|
496 |
\mbox{}\\[-15mm] |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
497 |
|
608 | 498 |
\bl{\begin{center} |
499 |
\begin{tabular}{@{\hspace{-4mm}}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}} |
|
500 |
$\textit{estimate}(n)$ & $\dn$ & $1$\\ |
|
501 |
$\textit{estimate}(x)$ & $\dn$ & $1$\\ |
|
502 |
$\textit{estimate}(a_1\;aop\;a_2)$ & $\dn$ & |
|
503 |
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\ |
|
504 |
$\textit{estimate}(\pcode{if}\;b\;\pcode{then}\;e_1\;\pcode{else}\;e_2)$ & $\dn$ & |
|
505 |
$\textit{estimate}(b) +$\\ |
|
506 |
& & $\quad max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\ |
|
507 |
$\textit{estimate}(\pcode{write}(e))$ & $\dn$ & |
|
508 |
$\textit{estimate}(e) + 1$\\ |
|
509 |
$\textit{estimate}(e_1 ; e_2)$ & $\dn$ & |
|
510 |
$max(\textit{estimate}(e_1), \textit{estimate}(e_2))$\\ |
|
511 |
$\textit{estimate}(f(e_1, ..., e_n))$ & $\dn$ & |
|
512 |
$\sum_{i = 1..n}\;estimate(e_i)$\medskip\\ |
|
513 |
$\textit{estimate}(a_1\;bop\;a_2)$ & $\dn$ & |
|
514 |
$\textit{estimate}(a_1) + \textit{estimate}(a_2)$\\ |
|
515 |
\end{tabular} |
|
516 |
\end{center}} |
|
517 |
||
518 |
\end{frame} |
|
519 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
520 |
|
197
6622bd256029
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
196
diff
changeset
|
521 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 522 |
\begin{frame}[fragile] |
523 |
\frametitle{Successor Function} |
|
524 |
||
525 |
\begin{textblock}{7}(1,2.5)\footnotesize |
|
526 |
\begin{minipage}{6cm} |
|
527 |
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none] |
|
528 |
.method public static suc(I)I |
|
529 |
.limit locals 1 |
|
530 |
.limit stack 2 |
|
531 |
iload 0 |
|
532 |
ldc 1 |
|
533 |
iadd |
|
534 |
ireturn |
|
535 |
.end method |
|
536 |
\end{lstlisting} |
|
537 |
\end{minipage} |
|
538 |
\end{textblock} |
|
539 |
||
540 |
\begin{textblock}{7}(6,8) |
|
541 |
\begin{bubble}[5cm]\small |
|
542 |
\begin{lstlisting}[language=Lisp, |
|
543 |
basicstyle=\ttfamily, |
|
544 |
numbers=none, |
|
545 |
xleftmargin=1mm,linebackgroundcolor=\color{cream}] |
|
546 |
def suc(x) = x + 1; |
|
547 |
\end{lstlisting} |
|
548 |
\end{bubble} |
|
549 |
\end{textblock} |
|
550 |
||
551 |
\end{frame} |
|
552 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
553 |
||
554 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
555 |
\begin{frame}[fragile] |
|
556 |
\frametitle{Addition Function} |
|
557 |
||
558 |
\begin{textblock}{7}(1,1.9)\footnotesize |
|
559 |
\begin{minipage}{6cm} |
|
560 |
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none] |
|
561 |
.method public static add(II)I |
|
562 |
.limit locals 2 |
|
563 |
.limit stack 5 |
|
564 |
iload 0 |
|
565 |
ldc 0 |
|
566 |
if_icmpne If_else |
|
567 |
iload 1 |
|
568 |
goto If_end |
|
569 |
If_else: |
|
570 |
iload 0 |
|
571 |
ldc 1 |
|
572 |
isub |
|
573 |
iload 1 |
|
574 |
invokestatic XXX/XXX/add(II)I |
|
575 |
invokestatic XXX/XXX/suc(I)I |
|
576 |
If_end: |
|
577 |
ireturn |
|
578 |
.end method |
|
579 |
\end{lstlisting} |
|
580 |
\end{minipage} |
|
581 |
\end{textblock} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
582 |
|
608 | 583 |
\begin{textblock}{7}(6,6.6) |
584 |
\begin{bubble}[7cm]\small |
|
585 |
\begin{lstlisting}[language=Lisp, |
|
586 |
basicstyle=\ttfamily, |
|
587 |
numbers=none, |
|
588 |
xleftmargin=1mm,linebackgroundcolor=\color{cream}] |
|
589 |
def add(x, y) = |
|
590 |
if x == 0 then y |
|
591 |
else suc(add(x - 1, y)); |
|
592 |
\end{lstlisting} |
|
593 |
\end{bubble} |
|
594 |
\end{textblock} |
|
595 |
||
596 |
\end{frame} |
|
597 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
598 |
||
599 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
600 |
\begin{frame}[c,fragile] |
|
814 | 601 |
\frametitle{\mbox{}\hspace{5cm}Factorial} |
197
6622bd256029
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
196
diff
changeset
|
602 |
|
814 | 603 |
\begin{textblock}{7}(0,1.0)\footnotesize |
608 | 604 |
\begin{minipage}{6cm} |
605 |
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none] |
|
606 |
.method public static facT(II)I |
|
607 |
.limit locals 2 |
|
608 |
.limit stack 6 |
|
609 |
iload 0 |
|
701 | 610 |
ldc 0 |
608 | 611 |
if_icmpne If_else_2 |
612 |
iload 1 |
|
613 |
goto If_end_3 |
|
614 |
If_else_2: |
|
615 |
iload 0 |
|
616 |
ldc 1 |
|
617 |
isub |
|
618 |
iload 0 |
|
619 |
iload 1 |
|
620 |
imul |
|
621 |
invokestatic fact/fact/facT(II)I |
|
622 |
If_end_3: |
|
623 |
ireturn |
|
624 |
.end method |
|
625 |
\end{lstlisting} |
|
626 |
\end{minipage} |
|
627 |
\end{textblock} |
|
628 |
||
629 |
\begin{textblock}{7}(6,7) |
|
630 |
\begin{bubble}[7cm]\small |
|
631 |
\begin{lstlisting}[language=Lisp, |
|
632 |
basicstyle=\ttfamily, |
|
633 |
numbers=none, |
|
634 |
xleftmargin=1mm,linebackgroundcolor=\color{cream}] |
|
635 |
def facT(n, acc) = |
|
636 |
if n == 0 then acc |
|
637 |
else facT(n - 1, n * acc); |
|
638 |
\end{lstlisting} |
|
639 |
\end{bubble} |
|
640 |
\end{textblock} |
|
641 |
||
642 |
\end{frame} |
|
197
6622bd256029
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
196
diff
changeset
|
643 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
644 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
645 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 646 |
\begin{frame}[fragile] |
647 |
||
648 |
\begin{textblock}{7}(1,-0.2)\footnotesize |
|
649 |
\begin{minipage}{6cm} |
|
650 |
\begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none, escapeinside={(*@}{@*)}] |
|
651 |
.method public static facT(II)I |
|
652 |
.limit locals 2 |
|
653 |
.limit stack 6 |
|
654 |
(*@\hl{facT\_Start:} @*) |
|
655 |
iload 0 |
|
656 |
ldc 0 |
|
657 |
if_icmpne If_else_2 |
|
658 |
iload 1 |
|
659 |
goto If_end_3 |
|
660 |
If_else_2: |
|
661 |
iload 0 |
|
662 |
ldc 1 |
|
663 |
isub |
|
664 |
iload 0 |
|
665 |
iload 1 |
|
666 |
imul |
|
667 |
(*@\hl{istore 1} @*) |
|
668 |
(*@\hl{istore 0} @*) |
|
669 |
(*@\hl{goto facT\_Start} @*) |
|
670 |
If_end_3: |
|
671 |
ireturn |
|
672 |
.end method |
|
673 |
\end{lstlisting} |
|
674 |
\end{minipage} |
|
675 |
\end{textblock} |
|
676 |
||
677 |
\begin{textblock}{7}(6,7) |
|
678 |
\begin{bubble}[7cm]\small |
|
679 |
\begin{lstlisting}[language=Lisp, |
|
680 |
basicstyle=\ttfamily, |
|
681 |
numbers=none, |
|
682 |
xleftmargin=1mm,linebackgroundcolor=\color{cream}] |
|
683 |
def facT(n, acc) = |
|
684 |
if n == 0 then acc |
|
685 |
else facT(n - 1, n * acc); |
|
686 |
\end{lstlisting} |
|
687 |
\end{bubble} |
|
688 |
\end{textblock} |
|
689 |
||
690 |
\end{frame} |
|
691 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
692 |
||
693 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
694 |
\begin{frame}[t, fragile] |
|
695 |
\frametitle{Tail Recursion} |
|
696 |
||
697 |
A call to \texttt{f(args)} is usually compiled as\medskip |
|
698 |
||
699 |
{\small\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
|
700 |
args onto stack |
|
701 |
invokestatic .../f |
|
702 |
\end{lstlisting}}\pause |
|
703 |
||
704 |
||
705 |
A call is in tail position provided:\medskip |
|
706 |
||
707 |
{\small\begin{itemize} |
|
708 |
\item \texttt{if Bexp then \hl{Exp} else \hl{Exp}} |
|
709 |
\item \texttt{Exp ; \hl{Exp}} |
|
710 |
\item \texttt{Exp op Exp} |
|
711 |
\end{itemize}}\medskip |
|
712 |
||
713 |
then a call \texttt{f(args)} can be compiled as\medskip\small |
|
714 |
||
697 | 715 |
\begin{lstlisting}[numbers=none] |
608 | 716 |
prepare environment |
717 |
jump to start of function |
|
718 |
\end{lstlisting} |
|
719 |
||
720 |
\end{frame} |
|
721 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
722 |
||
723 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
724 |
\begin{frame}[c, fragile] |
|
725 |
\frametitle{Tail Recursive Call} |
|
726 |
\footnotesize |
|
727 |
||
697 | 728 |
\begin{textblock}{13}(-0.3,3) |
729 |
\begin{lstlisting}[language=Scala, numbers=none] |
|
608 | 730 |
def compile_expT(a: Exp, env: Mem, name: String): Instrs = |
731 |
... |
|
697 | 732 |
case Call(n, args) => if (name == n) |
608 | 733 |
{ |
697 | 734 |
val stores = |
735 |
args.zipWithIndex.map { case (x, y) => i"istore $y" } |
|
736 |
||
737 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
|
738 |
stores.reverse.mkString ++ |
|
739 |
i"goto ${n}_Start" |
|
740 |
} else { |
|
608 | 741 |
val is = "I" * args.length |
697 | 742 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
743 |
i"invokestatic XXX/XXX/${n}(${is})I" |
|
608 | 744 |
} |
745 |
\end{lstlisting} |
|
746 |
\end{textblock} |
|
747 |
||
748 |
\end{frame} |
|
749 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
750 |
||
751 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
752 |
\begin{frame}[c] |
|
753 |
\frametitle{Dijkstra on Testing} |
|
754 |
||
755 |
\begin{bubble}[10cm] |
|
756 |
``Program testing can be a very effective way to show the |
|
757 |
presence of bugs, but it is hopelessly inadequate for showing |
|
758 |
their absence.'' |
|
759 |
\end{bubble}\bigskip |
|
760 |
||
761 |
\small |
|
762 |
What is good about compilers: the either seem to work, |
|
763 |
or go horribly wrong (most of the time). |
|
764 |
||
765 |
\end{frame} |
|
766 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
767 |
||
768 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
769 |
\begin{frame}[c] |
|
770 |
\frametitle{\Large Proving Programs to be Correct} |
|
771 |
||
772 |
\begin{bubble}[10cm] |
|
773 |
\small |
|
774 |
{\bf Theorem:} There are infinitely many prime |
|
775 |
numbers.\medskip\\ |
|
776 |
||
777 |
{\bf Proof} \ldots\\ |
|
778 |
\end{bubble}\bigskip |
|
779 |
||
780 |
||
781 |
similarly\bigskip |
|
782 |
||
783 |
\begin{bubble}[10cm] |
|
784 |
\small |
|
785 |
{\bf Theorem:} The program is doing what |
|
786 |
it is supposed to be doing.\medskip |
|
787 |
||
788 |
{\bf Long, long proof} \ldots\\ |
|
789 |
\end{bubble}\bigskip\medskip |
|
790 |
||
791 |
\small This can be a gigantic proof. The only hope is to have |
|
792 |
help from the computer. `Program' is here to be understood to be |
|
793 |
quite general (compiler, OS, \ldots). |
|
794 |
||
795 |
\end{frame} |
|
796 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
797 |
||
798 |
||
799 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
800 |
\begin{frame}[c] |
|
801 |
\frametitle{Can This Be Done?} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
802 |
|
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
803 |
\begin{itemize} |
608 | 804 |
\item in 2008, verification of a small C-compiler |
805 |
\begin{itemize} |
|
806 |
\item ``if my input program has a certain behaviour, then the compiled machine code has the same behaviour'' |
|
807 |
\item is as good as \texttt{gcc -O1}, but much, much less buggy |
|
808 |
\end{itemize} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
809 |
\end{itemize} |
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
810 |
|
608 | 811 |
\begin{center} |
812 |
\includegraphics[scale=0.12]{../pics/compcert.png} |
|
813 |
\end{center} |
|
814 |
||
815 |
\end{frame} |
|
816 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
817 |
||
818 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
819 |
\begin{frame}[c] |
|
820 |
\frametitle{Fuzzy Testing C-Compilers} |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
821 |
|
608 | 822 |
\begin{itemize} |
823 |
\item tested GCC, LLVM and others by randomly generating |
|
824 |
C-programs |
|
825 |
\item found more than 300 bugs in GCC and also |
|
826 |
many in LLVM (some of them highest-level critical)\bigskip |
|
827 |
\item about CompCert: |
|
828 |
||
829 |
\begin{bubble}[10cm]\small ``The striking thing about our CompCert |
|
830 |
results is that the middle-end bugs we found in all other |
|
831 |
compilers are absent. As of early 2011, the under-development |
|
832 |
version of CompCert is the only compiler we have tested for |
|
833 |
which Csmith cannot find wrong-code errors. This is not for |
|
834 |
lack of trying: we have devoted about six CPU-years to the |
|
835 |
task.'' |
|
836 |
\end{bubble} |
|
837 |
\end{itemize} |
|
838 |
||
839 |
\end{frame} |
|
840 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196
7182786d9c68
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
841 |
|
697 | 842 |
|
817 | 843 |
\begin{frame}<1-13> |
844 |
\end{frame} |
|
697 | 845 |
|
65
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
846 |
\end{document} |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
847 |
|
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
848 |
%%% Local Variables: |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
849 |
%%% mode: latex |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
850 |
%%% TeX-master: t |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
851 |
%%% End: |
ade6af51402c
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
852 |