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