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