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