slides/slides10.tex
changeset 223 e4b29b57f6a3
parent 215 828303e8e4af
child 224 70198792c2aa
equal deleted inserted replaced
222:b712519b41d3 223:e4b29b57f6a3
    16 \usetikzlibrary{positioning}
    16 \usetikzlibrary{positioning}
    17 \usetikzlibrary{calc}
    17 \usetikzlibrary{calc}
    18 \usetikzlibrary{plotmarks}
    18 \usetikzlibrary{plotmarks}
    19 \usepackage{graphicx} 
    19 \usepackage{graphicx} 
    20 \usepackage{pgfplots}
    20 \usepackage{pgfplots}
       
    21 \usepackage{soul}
    21 \usepackage{../langs}
    22 \usepackage{../langs}
    22 \usepackage{../data}
    23 \usepackage{../data}
    23 
    24 
       
    25 
       
    26 \tikzset{onslide/.code args={<#1>#2}{%
       
    27   \only<#1>{\pgfkeysalso{#2}} % \pgfkeysalso doesn't change the path
       
    28 }}
       
    29 
       
    30 \makeatletter
       
    31 \newenvironment<>{btHighlight}[1][]
       
    32 {\begin{onlyenv}#2\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
       
    33 {\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup\end{onlyenv}}
       
    34 
       
    35 \newcommand<>\btHL[1][]{%
       
    36   \only#2{\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv}%
       
    37 }
       
    38 \def\bt@HL@endenv{%
       
    39   \end{btHighlight}%   
       
    40   \egroup
       
    41 }
       
    42 \newcommand{\bt@HL@box}[2][]{%
       
    43   \tikz[#1]{%
       
    44     \pgfpathrectangle{\pgfpoint{1pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
       
    45     \pgfusepath{use as bounding box}%
       
    46     \node[anchor=base west, fill=orange!30,outer sep=0pt,inner xsep=1pt, inner ysep=0pt, rounded corners=3pt, minimum height=\ht\strutbox+1pt,#1]{\raisebox{1pt}{\strut}\strut\usebox{#2}};
       
    47   }%
       
    48 }
       
    49 \makeatother
    24 
    50 
    25 
    51 
    26 % beamer stuff 
    52 % beamer stuff 
    27 \renewcommand{\slidecaption}{AFL 10, King's College London, 4.~December 2013}
    53 \renewcommand{\slidecaption}{AFL 10, King's College London, 4.~December 2013}
    28 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    54 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    29 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    55 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    30 
       
    31 
       
    32 % The data files, written on the first run.
       
    33 \begin{filecontents}{compiled.data}
       
    34 %1 0.234146
       
    35 %5000 0.227539
       
    36 %10000 0.280748
       
    37 50000 1.087897
       
    38 100000 3.713165
       
    39 250000 21.6624545
       
    40 500000 85.872613
       
    41 750000 203.6408015
       
    42 1000000 345.736574
       
    43 \end{filecontents}
       
    44 
       
    45 \begin{filecontents}{interpreted.data}
       
    46 %1 0.00503
       
    47 200 1.005863
       
    48 400 7.8296765
       
    49 500 15.43106
       
    50 600 27.2321885
       
    51 800 65.249271
       
    52 1000 135.4493445
       
    53 1200 232.134097
       
    54 1400 382.527227
       
    55 \end{filecontents}
       
    56 
       
    57 \begin{filecontents}{interpreted2.data}
       
    58 %1 0.00503
       
    59 200 1.005863
       
    60 400 7.8296765
       
    61 600 27.2321885
       
    62 800 65.249271
       
    63 1000 135.4493445
       
    64 1200 232.134097
       
    65 1400 382.527227
       
    66 \end{filecontents}
       
    67 
       
    68 \begin{filecontents}{compiled2.data}
       
    69 200 0.222058
       
    70 400 0.215204
       
    71 600 0.202031
       
    72 800 0.21986
       
    73 1000 0.205934
       
    74 1200 0.1981615
       
    75 1400 0.207116
       
    76 \end{filecontents}
       
    77 
    56 
    78 \begin{document}
    57 \begin{document}
    79 
    58 
    80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    81 \mode<presentation>{
    60 \mode<presentation>{
   110 \end{frame}}
    89 \end{frame}}
   111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   112 
    91 
   113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   114 \mode<presentation>{
    93 \mode<presentation>{
   115 \begin{frame}[c]
    94 \begin{frame}[t]
   116 \frametitle{Revision: Proofs}
    95 \frametitle{Last Week}
   117 
    96 
   118 \begin{center}
    97 \begin{center}
   119 %%\includegraphics[scale=0.4]{river-stones.jpg}
    98 if \bl{$\varnothing$} does not occur in \bl{$r$} \;\;then\;\;\bl{$L(r) \not= \{\}$}
   120 \end{center}
    99 \end{center}
   121 
   100 
   122 \end{frame}}
   101 \noindent
   123 
   102 holds, or equivalently
   124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   103 
   125 \mode<presentation>{
   104 \begin{center}
   126 \begin{frame}[c]
   105 \bl{$L(r) = \{\}$} \;\;implies\;\; \bl{$\varnothing$} occurs in \bl{$r$}.\pause
   127 \frametitle{Subsets}
   106 \end{center}
   128 
   107 
   129 \Large
   108 \begin{center}
   130 \bl{$A \subseteq B$}\bigskip\bigskip\\
   109 \bl{\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
   131 
   110 $occurs(\varnothing)$      & $\dn$ & $true$\\
   132 \bl{$\forall e.\; e \in A \Rightarrow e \in B$}
   111 $occurs(\epsilon)$           & $\dn$ &  $f\!alse$\\
       
   112 $occurs (c)$                    & $\dn$ &  $f\!alse$\\
       
   113 $occurs (r_1 + r_2)$       & $\dn$ &  $occurs(r_1) \vee occurs(r_2)$\\ 
       
   114 $occurs (r_1 \cdot r_2)$ & $\dn$ &  $occurs(r_1) \vee occurs(r_2)$\\
       
   115 $occurs (r^*)$                & $\dn$ & $occurs(r)$ \\
       
   116 \end{tabular}}
       
   117 \end{center}
       
   118 
       
   119 \end{frame}}
       
   120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   121 
       
   122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   123 \begin{frame}[c,fragile]
       
   124 \frametitle{Functional Programming}
       
   125 
       
   126 \footnotesize
       
   127 \begin{textblock}{13}(0.9,3)
       
   128 \lstset{emph={def,if,then,else},emphstyle=\color{javapurple}}
       
   129 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   130 def fib(n) = if n == 0 then 0 
       
   131              else if n == 1 then 1 
       
   132              else fib(n - 1) + fib(n - 2);
       
   133 
       
   134 def fact(n) = if n == 0 then 1 else n * fact(n - 1);
       
   135 
       
   136 def ack(m, n) = if m == 0 then n + 1
       
   137                 else if n == 0 then ack(m - 1, 1)
       
   138                 else ack(m - 1, ack(m, n - 1));
       
   139                 
       
   140 def gcd(a, b) = if b == 0 then a else gcd(b, a % b);                
       
   141 \end{lstlisting}
       
   142 \end{textblock}
       
   143 
       
   144 \end{frame}
       
   145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   146 
       
   147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   148 \mode<presentation>{
       
   149 \begin{frame}[c]
       
   150 
       
   151 \begin{center}
       
   152 \bl{\begin{tabular}{@{}lcl@{}}
       
   153 \textit{Exp} & $\rightarrow$ &  \textit{Var} $|$ \textit{Num}\\
       
   154               & $|$ & \textit{Exp} \texttt{+} \textit{Exp} $|$ ... $|$ ( \textit{Exp} )\\
       
   155               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Exp} \;\texttt{else}\; \textit{Exp}\\
       
   156               & $|$ & \texttt{write}\;\textit{Exp}\\
       
   157               & $|$ & \textit{Exp}\;\texttt{;}\;\textit{Exp}\\
       
   158               & $|$ & \textit{FunName} \texttt{(}\textit{Exp},...,\textit{Exp}\texttt{)}\medskip\\
       
   159 \textit{BExp} & $\rightarrow$ & \ldots\medskip\\
       
   160 \textit{Decl} & $\rightarrow$ &  \textit{Def} \;\texttt{;}\; \textit{Decl}\\
       
   161               & $|$ & \textit{Exp}\medskip\\
       
   162 \textit{Def} & 
       
   163 $\rightarrow$ &  \texttt{def} \textit{FunName}\texttt{(}\textit{x}$_1$,..., \textit{x}$_n$\texttt{)} \texttt{=} \textit{Exp}\\
       
   164 \end{tabular}}
       
   165 \end{center}
       
   166 
       
   167 
       
   168 \end{frame}}
       
   169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   170 
       
   171 
       
   172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   173 \begin{frame}[c, fragile]
       
   174 \frametitle{Abstract Syntax Tree}
       
   175 
       
   176 \footnotesize
       
   177 \begin{textblock}{13}(0.3,2)
       
   178 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily, numbers=none]
       
   179 abstract class Exp
       
   180 abstract class BExp 
       
   181 abstract class Decl
       
   182 
       
   183 case class 
       
   184   Def(name: String, args: List[String], body: Exp) 
       
   185                                           extends Decl
       
   186 case class Main(e: Exp) extends Decl
       
   187 
       
   188 case class Call(name: String, args: List[Exp]) extends Exp
       
   189 case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
       
   190 case class Write(e: Exp) extends Exp
       
   191 case class Var(s: String) extends Exp
       
   192 case class Num(i: Int) extends Exp
       
   193 case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
       
   194 case class Sequ(e1: Exp, e2: Exp) extends Exp
       
   195 
       
   196 case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
       
   197 \end{lstlisting}
       
   198 \end{textblock}
       
   199 
       
   200 \end{frame}
       
   201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   202 
       
   203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   204 \begin{frame}[c, fragile]
       
   205 \frametitle{Mathematical Functions}
       
   206 
       
   207 Compilation of some mathematical functions:
       
   208 
       
   209 \begin{center}
       
   210 \begin{tabular}{lcl}
       
   211 \texttt{Aop("+", a1, a2)} & $\Rightarrow$ & \texttt{...iadd}\\
       
   212 \texttt{Aop("-", a1, a2)} & $\Rightarrow$ & \texttt{...isub}\\
       
   213 \texttt{Aop("*", a1, a2)} & $\Rightarrow$ & \texttt{...imul}\\
       
   214 \texttt{Aop("/", a1, a2)} & $\Rightarrow$ & \texttt{...idiv}\\
       
   215 \texttt{Aop("\%", a1, a2)} & $\Rightarrow$ & \texttt{...irem}\\
       
   216 \end{tabular}
       
   217 \end{center}
       
   218 
       
   219 \end{frame}
       
   220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   221 
       
   222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   223 \begin{frame}[c, fragile]
       
   224 \frametitle{Boolean Expressions}
       
   225 
       
   226 Compilation of boolean expressions:
       
   227 
       
   228 \begin{center}
       
   229 \begin{tikzpicture}[node distance=2mm and 4mm,
       
   230  block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm},
       
   231  point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red},
       
   232  skip loop/.style={red, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}]
       
   233 \node (A1) [point] {};
       
   234 \node (b) [block, right=of A1] {code of \bl{$b$}};
       
   235 \node (A2) [point, right=of b] {};
       
   236 \node (cs1) [block, right=of A2] {code of \bl{$cs_1$}};
       
   237 \node (A3) [point, right=of cs1] {};
       
   238 \node (cs2) [block, right=of A3] {code of \bl{$cs_2$}};
       
   239 \node (A4) [point, right=of cs2] {};
       
   240 
       
   241 \only<1>{
       
   242 \draw (A1) edge [->, red, line width=1mm] (b);
       
   243 \draw (b) edge [->, red, line width=1mm] (A2);
       
   244 \draw (A2) edge [skip loop] (A3);
       
   245 \draw (A3) edge [->, red, line width=1mm] (cs2);
       
   246 \draw (cs2) edge [->,red, line width=1mm] (A4);
       
   247 \node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}};}
       
   248 \end{tikzpicture}
       
   249 \end{center}
       
   250 
       
   251 \begin{center}
       
   252 \begin{tabular}{lcl}
       
   253 \texttt{Bop("==", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpne...}\\
       
   254 \texttt{Bop("!=", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpeq...}\\
       
   255 \texttt{Bop("<", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpge...}\\
       
   256 \texttt{Bop("<=", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpgt...}\\
       
   257 \end{tabular}
       
   258 \end{center}
       
   259 
       
   260 \end{frame}
       
   261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   262 
       
   263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   264 \begin{frame}[t, fragile]
       
   265 \frametitle{Sequences}
       
   266 
       
   267 Compiling \texttt{arg1 ; arg2}:
       
   268 
       
   269 
       
   270 \begin{textblock}{7}(2,5)\footnotesize
       
   271 \begin{minipage}{6cm}
       
   272 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   273 ...arg1...
       
   274 pop
       
   275 ...arg1...
       
   276 \end{lstlisting}
       
   277 \end{minipage}
       
   278 \end{textblock}
       
   279 
       
   280   
       
   281 \end{frame}
       
   282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   283 
       
   284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   285 \begin{frame}[t, fragile]
       
   286 \frametitle{Write}
       
   287 
       
   288 Compiling \texttt{write(arg)}:
       
   289 
       
   290 
       
   291 \begin{textblock}{7}(2,4)\footnotesize
       
   292 \begin{minipage}{6cm}
       
   293 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   294 ...arg...
       
   295 dup
       
   296 invokestatic XXX/XXX/write(I)V
       
   297 \end{lstlisting}
       
   298 \end{minipage}
       
   299 \end{textblock}
       
   300 
       
   301 
       
   302 
       
   303 \begin{textblock}{7}(2,8)\footnotesize
       
   304 \begin{minipage}{6cm}
       
   305 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily, numbers=none]
       
   306 case Write(a1) => {
       
   307     compile_exp(a1, env) ++
       
   308     List("dup\n",
       
   309          "invokestatic XXX/XXX/write(I)V\n")
       
   310   }
       
   311 \end{lstlisting}
       
   312 \end{minipage}
       
   313 \end{textblock}
       
   314   
       
   315 \end{frame}
       
   316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   317 
       
   318 
       
   319 
       
   320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   321 \begin{frame}[c, fragile]
       
   322 \frametitle{Functions}
       
   323 
       
   324 \begin{textblock}{7}(1,2)\footnotesize
       
   325 \begin{minipage}{6cm}
       
   326 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   327 .method public static write(I)V 
       
   328    .limit locals 5 
       
   329    .limit stack 5 
       
   330    iload 0 
       
   331    getstatic java/lang/System/out Ljava/io/PrintStream; 
       
   332    swap 
       
   333    invokevirtual java/io/PrintStream/println(I)V 
       
   334    return 
       
   335 .end method
       
   336 \end{lstlisting}
       
   337 \end{minipage}
       
   338 \end{textblock}
       
   339 
       
   340 
       
   341 \begin{textblock}{10}(1,9.8)
       
   342 \small We will need for definitions\\[-8mm]\mbox{}\footnotesize
       
   343 \begin{center}
       
   344 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   345 .method public static f (I...I)I
       
   346   .limit locals ??
       
   347   .limit stack ?? 
       
   348   ??
       
   349 .end method
       
   350 \end{lstlisting}
       
   351 \end{center}
       
   352 \end{textblock}
       
   353 
       
   354 \end{frame}
       
   355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   356 
       
   357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   358 \begin{frame}[c, fragile]
       
   359 \frametitle{Stack Estimation}
       
   360 \footnotesize
       
   361 \begin{center}
       
   362 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily, numbers=none]
       
   363 def max_stack_exp(e: Exp): Int = e match {
       
   364   case Call(_, args) => args.map(max_stack_exp).sum
       
   365   case If(a, e1, e2) => max_stack_bexp(a) + 
       
   366   	(List(max_stack_exp(e1), max_stack_exp(e1)).max)
       
   367   case Write(e) => max_stack_exp(e) + 1
       
   368   case Var(_) => 1
       
   369   case Num(_) => 1
       
   370   case Aop(_, a1, a2) => 
       
   371   	max_stack_exp(a1) + max_stack_exp(a2)
       
   372   case Sequ(e1, e2) => 
       
   373   	List(max_stack_exp(e1), max_stack_exp(e2)).max
       
   374 }
       
   375 
       
   376 def max_stack_bexp(e: BExp): Int = e match {
       
   377   case Bop(_, a1, a2) => 
       
   378   	max_stack_exp(a1) + max_stack_exp(a2)
       
   379 }
       
   380 \end{lstlisting}
       
   381 \end{center}
       
   382 
       
   383 \end{frame}
       
   384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   385 
       
   386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   387 \begin{frame}[fragile]
       
   388 \frametitle{Successor}
       
   389 
       
   390 \begin{textblock}{7}(1,2.5)\footnotesize
       
   391 \begin{minipage}{6cm}
       
   392 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   393 .method public static suc(I)I 
       
   394 .limit locals 1
       
   395 .limit stack
       
   396   iload 0
       
   397   ldc 1
       
   398   iadd
       
   399   ireturn
       
   400 .end method 
       
   401 \end{lstlisting}
       
   402 \end{minipage}
       
   403 \end{textblock}
       
   404 
       
   405 \begin{textblock}{7}(6,8)
       
   406 \begin{tikzpicture}\small
       
   407 \draw (0,0) node[inner sep=2mm,fill=cream, ultra thick, draw=red, rounded corners=2mm] 
       
   408 {\begin{minipage}{5cm}
       
   409 \begin{lstlisting}[language=Lisp,basicstyle=\ttfamily, numbers=none]
       
   410 def suc(x) = x + 1;
       
   411 \end{lstlisting}
       
   412 \end{minipage}};
       
   413 \end{tikzpicture}
       
   414 \end{textblock}
       
   415 \end{frame}
       
   416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   417 
       
   418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   419 \begin{frame}[fragile]
       
   420 \frametitle{Addition}
       
   421 
       
   422 \begin{textblock}{7}(1,1.5)\footnotesize
       
   423 \begin{minipage}{6cm}
       
   424 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   425 .method public static add(II)I 
       
   426 .limit locals 2
       
   427 .limit stack 4
       
   428   iload 0
       
   429   ldc 0
       
   430   if_icmpne If_else_2
       
   431   iload 1
       
   432   goto If_end_3
       
   433 If_else_2:
       
   434   iload 0
       
   435   ldc 1
       
   436   isub
       
   437   iload 1
       
   438   invokestatic defs/defs/add(II)I
       
   439   invokestatic defs/defs/suc(I)I
       
   440 If_end_3:
       
   441   ireturn
       
   442 .end method
       
   443 \end{lstlisting}
       
   444 \end{minipage}
       
   445 \end{textblock}
       
   446 
       
   447 \begin{textblock}{7}(6,6.2)
       
   448 \begin{tikzpicture}\small
       
   449 \draw (0,0) node[inner sep=2mm,fill=cream, ultra thick, draw=red, rounded corners=2mm] 
       
   450 {\begin{minipage}{7cm}
       
   451 \begin{lstlisting}[language=Lisp,basicstyle=\ttfamily, numbers=none]
       
   452 def add(x, y) = 
       
   453     if x == 0 then y 
       
   454     else suc(add(x - 1, y));
       
   455 \end{lstlisting}
       
   456 \end{minipage}};
       
   457 \end{tikzpicture}
       
   458 \end{textblock}
       
   459 \end{frame}
       
   460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   461 
       
   462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   463 \begin{frame}[fragile]
       
   464 \frametitle{Factorial}
       
   465 
       
   466 \begin{textblock}{7}(1,1.5)\footnotesize
       
   467 \begin{minipage}{6cm}
       
   468 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
   469 .method public static facT(II)I 
       
   470 .limit locals 2
       
   471 .limit stack 4
       
   472   iload 0
       
   473   ldc 0	
       
   474   if_icmpne If_else_2
       
   475   iload 1
       
   476   goto If_end_3
       
   477 If_else_2:
       
   478   iload 0
       
   479   ldc 1
       
   480   isub
       
   481   iload 0
       
   482   iload 1
       
   483   imul
       
   484   invokestatic fact/fact/facT(II)I
       
   485 If_end_3:
       
   486   ireturn
       
   487 .end method 
       
   488 \end{lstlisting}
       
   489 \end{minipage}
       
   490 \end{textblock}
       
   491 
       
   492 \begin{textblock}{7}(6,7)
       
   493 \begin{tikzpicture}\small
       
   494 \draw (0,0) node[inner sep=2mm,fill=cream, ultra thick, draw=red, rounded corners=2mm] 
       
   495 {\begin{minipage}{7cm}
       
   496 \begin{lstlisting}[language=Lisp,basicstyle=\ttfamily, numbers=none]
       
   497 def facT(n, acc) = 
       
   498   if n == 0 then acc 
       
   499   else facT(n - 1, n * acc);
       
   500 \end{lstlisting}
       
   501 \end{minipage}};
       
   502 \end{tikzpicture}
       
   503 \end{textblock}
       
   504 \end{frame}
       
   505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   506 
       
   507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   508 \begin{frame}[fragile]
       
   509 %\frametitle{Factorial}
       
   510 
       
   511 \begin{textblock}{7}(1,-0.2)\footnotesize
       
   512 \begin{minipage}{6cm}
       
   513 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none, escapeinside={(*@}{@*)}]
       
   514 .method public static facT(II)I 
       
   515 .limit locals 2
       
   516 .limit stack 4
       
   517 (*@\hl{facT\_Start:} @*)
       
   518   iload 0
       
   519   ldc 0
       
   520   if_icmpne If_else_2
       
   521   iload 1
       
   522   goto If_end_3
       
   523 If_else_2:
       
   524   iload 0
       
   525   ldc 1
       
   526   isub
       
   527   iload 0
       
   528   iload 1
       
   529   imul
       
   530   (*@\hl{istore 1} @*)
       
   531   (*@\hl{istore 0} @*)
       
   532   (*@\hl{goto facT\_Start} @*)
       
   533 If_end_3:
       
   534   ireturn
       
   535 .end method 
       
   536 \end{lstlisting}
       
   537 \end{minipage}
       
   538 \end{textblock}
       
   539 
       
   540 \begin{textblock}{7}(6,7)
       
   541 \begin{tikzpicture}\small
       
   542 \draw (0,0) node[inner sep=2mm,fill=cream, ultra thick, draw=red, rounded corners=2mm] 
       
   543 {\begin{minipage}{7cm}
       
   544 \begin{lstlisting}[language=Lisp,basicstyle=\ttfamily, numbers=none]
       
   545 def facT(n, acc) = 
       
   546   if n == 0 then acc 
       
   547   else facT(n - 1, n * acc);
       
   548 \end{lstlisting}
       
   549 \end{minipage}};
       
   550 \end{tikzpicture}
       
   551 \end{textblock}
       
   552 \end{frame}
       
   553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   554 
       
   555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   556 \begin{frame}[c, fragile]
       
   557 \frametitle{Tail Recursion}
       
   558 
       
   559 A call to \texttt{f(args)} is usually compiled as\medskip
       
   560 
       
   561 {\small\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   562   args onto stack
       
   563   invokestatic  .../f 
       
   564 \end{lstlisting}}\pause
       
   565 
       
   566 
       
   567 A call is in tail position provided:\medskip
       
   568 
       
   569 {\small\begin{itemize}
       
   570 \item \texttt{if Bexp then \hl{Exp} else \hl{Exp}}
       
   571 \item \texttt{Exp ; \hl{Exp}}
       
   572 \item \texttt{Exp  op Exp}
       
   573 \end{itemize}}\medskip
       
   574 
       
   575 then a call \texttt{f(args)} can be compiled as\medskip\small
       
   576 
       
   577 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   578   prepare environment
       
   579   jump to start of function
       
   580 \end{lstlisting}
       
   581 
       
   582 \end{frame}
       
   583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   584 
       
   585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   586 \begin{frame}[c, fragile]
       
   587 \frametitle{Tail Recursive Call}
       
   588 \footnotesize
       
   589 \begin{textblock}{13}(0.3,2)
       
   590 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily, numbers=none]
       
   591 def compile_expT(a: Exp, env: Mem, name: String): Instrs = 
       
   592   ...
       
   593   case Call(n, args) => if (name == n) 
       
   594   { 
       
   595     val stores = args.zipWithIndex.map 
       
   596        { case (x, y) => "istore " + y.toString + "\n" } 
       
   597     args.flatMap(a => compile_expT(a, env, "")) ++
       
   598     stores.reverse ++ 
       
   599     List ("goto " + n + "_Start\n") 
       
   600   } 
       
   601   else 
       
   602   {
       
   603     val is = "I" * args.length
       
   604     args.flatMap(a => compile_expT(a, env, "")) ++
       
   605     List ("invokestatic XXX/XXX/" + n + "(" + is + ")I\n")
       
   606   }
       
   607 \end{lstlisting}
       
   608 \end{textblock}
       
   609 
       
   610 \end{frame}
       
   611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   612 
       
   613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   614 \mode<presentation>{
       
   615 \begin{frame}[c]
       
   616 
       
   617 \Large\bf
       
   618 There are more problems, than there are programs.\bigskip\bigskip\pause\\
       
   619 
       
   620 There must be a problem for which there is no program.
   133 \end{frame}}
   621 \end{frame}}
   134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   135 
   623 
   136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   137 \mode<presentation>{
   625 \mode<presentation>{