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