slides/slides09.tex
changeset 853 568671822d52
parent 822 6b06aeb192ea
child 864 b5b1bc0a603b
equal deleted inserted replaced
852:8706b846a3e0 853:568671822d52
    73 \end{lstlisting}
    73 \end{lstlisting}
    74 \end{textblock}
    74 \end{textblock}
    75 
    75 
    76 \end{frame}
    76 \end{frame}
    77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
    78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    79 \begin{frame}[c,fragile]
       
    80 \frametitle{\mbox{}\hspace{5cm}Factorial}
       
    81 
       
    82 \begin{textblock}{7}(0,1.0)\footnotesize
       
    83 \begin{minipage}{6cm}
       
    84 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none]
       
    85 .method public static facT(II)I 
       
    86 .limit locals 2
       
    87 .limit stack 6
       
    88   iload 0
       
    89   ldc 0
       
    90   if_icmpne If_else_2
       
    91   iload 1
       
    92   goto If_end_3
       
    93 If_else_2:
       
    94   iload 0
       
    95   ldc 1
       
    96   isub
       
    97   iload 0
       
    98   iload 1
       
    99   imul
       
   100   invokestatic fact/fact/facT(II)I
       
   101 If_end_3:
       
   102   ireturn
       
   103 .end method 
       
   104 \end{lstlisting}
       
   105 \end{minipage}
       
   106 \end{textblock}
       
   107 
       
   108 \begin{textblock}{7}(6,7)
       
   109 \begin{bubble}[7cm]\small
       
   110 \begin{lstlisting}[language=Lisp,
       
   111                    basicstyle=\ttfamily, 
       
   112                    numbers=none,
       
   113                    xleftmargin=1mm,linebackgroundcolor=\color{cream}]
       
   114 def facT(n, acc) = 
       
   115   if n == 0 then acc 
       
   116   else facT(n - 1, n * acc);
       
   117 \end{lstlisting}
       
   118 \end{bubble}
       
   119 \end{textblock}
       
   120 
       
   121 \end{frame}
       
   122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   123 
       
   124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   125 \begin{frame}[fragile]
       
   126 
       
   127 \begin{textblock}{7}(1,-0.2)\footnotesize
       
   128 \begin{minipage}{6cm}
       
   129 \begin{lstlisting}[language=JVMIS,basicstyle=\ttfamily, numbers=none, escapeinside={(*@}{@*)}]
       
   130 .method public static facT(II)I 
       
   131 .limit locals 2
       
   132 .limit stack 6
       
   133 (*@\hl{facT\_Start:} @*)
       
   134   iload 0
       
   135   ldc 0
       
   136   if_icmpne If_else_2
       
   137   iload 1
       
   138   goto If_end_3
       
   139 If_else_2:
       
   140   iload 0
       
   141   ldc 1
       
   142   isub
       
   143   iload 0
       
   144   iload 1
       
   145   imul
       
   146   (*@\hl{istore 1} @*)
       
   147   (*@\hl{istore 0} @*)
       
   148   (*@\hl{goto facT\_Start} @*)
       
   149 If_end_3:
       
   150   ireturn
       
   151 .end method 
       
   152 \end{lstlisting}
       
   153 \end{minipage}
       
   154 \end{textblock}
       
   155 
       
   156 \begin{textblock}{7}(6,7)
       
   157 \begin{bubble}[7cm]\small
       
   158 \begin{lstlisting}[language=Lisp,
       
   159                    basicstyle=\ttfamily, 
       
   160                    numbers=none,
       
   161                    xleftmargin=1mm,linebackgroundcolor=\color{cream}]
       
   162 def facT(n, acc) = 
       
   163   if n == 0 then acc 
       
   164   else facT(n - 1, n * acc);
       
   165 \end{lstlisting}
       
   166 \end{bubble}
       
   167 \end{textblock}
       
   168 
       
   169 \end{frame}
       
   170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   171 
       
   172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   173 \begin{frame}[t, fragile]
       
   174 \frametitle{Tail Recursion}
       
   175 
       
   176 A call to \texttt{f(args)} is usually compiled as\medskip
       
   177 
       
   178 {\small\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   179   args onto stack
       
   180   invokestatic  .../f 
       
   181 \end{lstlisting}}\pause
       
   182 
       
   183 
       
   184 A call is in tail position provided:\medskip
       
   185 
       
   186 {\small\begin{itemize}
       
   187 \item \texttt{if Bexp then \hl{Exp} else \hl{Exp}}
       
   188 \item \texttt{Exp ; \hl{Exp}}
       
   189 \item \texttt{Exp  op Exp}
       
   190 \end{itemize}}\medskip
       
   191 
       
   192 then a call \texttt{f(args)} can be compiled as\medskip\small
       
   193 
       
   194 \begin{lstlisting}[numbers=none]
       
   195   prepare environment
       
   196   jump to start of function
       
   197 \end{lstlisting}
       
   198 
       
   199 \end{frame}
       
   200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   201 
       
   202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   203 \begin{frame}[c, fragile]
       
   204 \frametitle{Tail Recursive Call}
       
   205 \footnotesize
       
   206 
       
   207 \begin{textblock}{13}(-0.3,3)
       
   208 \begin{lstlisting}[language=Scala, numbers=none]
       
   209 def compile_expT(a: Exp, env: Mem, name: String): Instrs = 
       
   210   ...
       
   211   case Call(n, args) => if (name == n)
       
   212   {
       
   213     val stores =
       
   214       args.zipWithIndex.map { case (x, y) => i"istore $y" } 
       
   215 
       
   216     args.map(a => compile_expT(a, env, "")).mkString ++
       
   217     stores.reverse.mkString ++ 
       
   218     i"goto ${n}_Start" 
       
   219   } else {
       
   220     val is = "I" * args.length
       
   221     args.map(a => compile_expT(a, env, "")).mkString ++
       
   222     i"invokestatic XXX/XXX/${n}(${is})I"
       
   223   }
       
   224 \end{lstlisting}
       
   225 \end{textblock}
       
   226 
       
   227 \end{frame}
       
   228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    78 
   229 
    79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    80 \begin{frame}[c,fragile]
   231 \begin{frame}[c,fragile]
    81 \frametitle{Factorial Funct.~on the JVM}
   232 \frametitle{Factorial Funct.~on the JVM}
    82 
   233