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