slides/slides08.tex
changeset 813 059f970287d1
parent 744 99c5916d9a8f
child 814 1fbaa5f05516
equal deleted inserted replaced
812:2f9a0dcf61ae 813:059f970287d1
    60     \end{tikzpicture}
    60     \end{tikzpicture}
    61   \end{center}
    61   \end{center}
    62 \end{frame}
    62 \end{frame}
    63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    64 
    64 
    65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    65 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    66 \begin{frame}[c]
    66 % \begin{frame}[c]
    67 
    67 
    68 \begin{center}
    68 % \begin{center}
    69 \bl{\begin{tabular}{@{}lcl@{}}
    69 % \bl{\begin{tabular}{@{}lcl@{}}
    70 \meta{Stmt} & $::=$ &  $\texttt{skip}$\\
    70 % \meta{Stmt} & $::=$ &  $\texttt{skip}$\\
    71               & $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\
    71 %               & $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\
    72               & $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\
    72 %               & $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\
    73               & $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\
    73 %               & $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\
    74               & $|$ & \texttt{read}\;\textit{Id}\\
    74 %               & $|$ & \texttt{read}\;\textit{Id}\\
    75               & $|$ & \texttt{write}\;\textit{Id}\\
    75 %               & $|$ & \texttt{write}\;\textit{Id}\\
    76               & $|$ & \texttt{write}\;\textit{String}\medskip\\
    76 %               & $|$ & \texttt{write}\;\textit{String}\medskip\\
    77 \meta{Stmts} & $::=$ &  \meta{Stmt} \;\texttt{;}\; \meta{Stmts}\\
    77 % \meta{Stmts} & $::=$ &  \meta{Stmt} \;\texttt{;}\; \meta{Stmts}\\
    78               & $|$ & \meta{Stmt}\medskip\\
    78 %               & $|$ & \meta{Stmt}\medskip\\
    79 \meta{Block} & $::=$ &  \texttt{\{}\,\meta{Stmts}\,\texttt{\}}\\
    79 % \meta{Block} & $::=$ &  \texttt{\{}\,\meta{Stmts}\,\texttt{\}}\\
    80                 & $|$ & \meta{Stmt}\medskip\\
    80 %                 & $|$ & \meta{Stmt}\medskip\\
    81 \meta{AExp} & $::=$ & \ldots\\
    81 % \meta{AExp} & $::=$ & \ldots\\
    82 \meta{BExp} & $::=$ & \ldots\\
    82 % \meta{BExp} & $::=$ & \ldots\\
    83 \end{tabular}}
    83 % \end{tabular}}
    84 \end{center}
    84 % \end{center}
    85 \end{frame}
    85 % \end{frame}
    86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    86 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    87 
    87 
    88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    88 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    89 \begin{frame}[t,fragile]
    89 % \begin{frame}[t,fragile]
    90 \frametitle{Compiling AExps}
    90 % \frametitle{Compiling AExps}
    91 
    91 
    92 For example \bl{$1 + ((2 * 3) + (4 - 3))$}:\medskip
    92 % For example \bl{$1 + ((2 * 3) + (4 - 3))$}:\medskip
    93 
    93 
    94 \begin{columns}[T]
    94 % \begin{columns}[T]
    95 \begin{column}{.3\textwidth}
    95 % \begin{column}{.3\textwidth}
    96 \begin{center}
    96 % \begin{center}
    97 \bl{\begin{tikzpicture}
    97 % \bl{\begin{tikzpicture}
    98 \tikzset{level distance=12mm,sibling distance=4mm}
    98 % \tikzset{level distance=12mm,sibling distance=4mm}
    99 \tikzset{edge from parent/.style={draw,very thick}}
    99 % \tikzset{edge from parent/.style={draw,very thick}}
   100 \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]]
   100 % \Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]]
   101 \end{tikzpicture}}
   101 % \end{tikzpicture}}
   102 \end{center}
   102 % \end{center}
   103 \end{column}
   103 % \end{column}
   104 \begin{column}{.3\textwidth}
   104 % \begin{column}{.3\textwidth}
   105 \begin{lstlisting}[language=JVMIS,numbers=none]
   105 % \begin{lstlisting}[language=JVMIS,numbers=none]
   106 ldc 1 
   106 % ldc 1 
   107 ldc 2 
   107 % ldc 2 
   108 ldc 3 
   108 % ldc 3 
   109 imul 
   109 % imul 
   110 ldc 4 
   110 % ldc 4 
   111 ldc 3 
   111 % ldc 3 
   112 isub 
   112 % isub 
   113 iadd 
   113 % iadd 
   114 iadd
   114 % iadd
   115 \end{lstlisting}
   115 % \end{lstlisting}
   116 \end{column}
   116 % \end{column}
   117 \end{columns}\bigskip
   117 % \end{columns}\bigskip
   118 
   118 
   119 \small
   119 % \small
   120 Traverse tree in post-order \bl{$\Rightarrow$} code for 
   120 % Traverse tree in post-order \bl{$\Rightarrow$} code for 
   121 stack-machine
   121 % stack-machine
   122 
   122 
   123 \end{frame}
   123 % \end{frame}
   124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   124 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   125 
   125 
   126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   126 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   127 \begin{frame}[c,fragile]
   127 % \begin{frame}[c,fragile]
   128 \frametitle{Compiling AExps}
   128 % \frametitle{Compiling AExps}
   129 
   129 
   130 \bl{
   130 % \bl{
   131 \begin{center}
   131 % \begin{center}
   132 \begin{tabular}{lcl}
   132 % \begin{tabular}{lcl}
   133 $\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\smallskip\\
   133 % $\textit{compile}(n, E)$ & $\dn$ & $\pcode{ldc}\;n$\smallskip\\
   134 $\textit{compile}(a_1 + a_2, E)$ & $\dn$ \\
   134 % $\textit{compile}(a_1 + a_2, E)$ & $\dn$ \\
   135 \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$}\smallskip\\
   135 % \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\;\textit{compile}(a_2, E)\;@\; \pcode{iadd}$}\smallskip\\
   136 $\textit{compile}(a_1 - a_2, E)$ & $\dn$ \\
   136 % $\textit{compile}(a_1 - a_2, E)$ & $\dn$ \\
   137 \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$}\smallskip\\
   137 % \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{isub}$}\smallskip\\
   138 $\textit{compile}(a_1 * a_2, E)$ & $\dn$ \\
   138 % $\textit{compile}(a_1 * a_2, E)$ & $\dn$ \\
   139 \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$}\smallskip\\
   139 % \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{imul}$}\smallskip\\
   140 $\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$\\ 
   140 % $\textit{compile}(a_1 \backslash a_2, E)$ & $\dn$\\ 
   141 \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$}\smallskip\\
   141 % \multicolumn{3}{c}{$\qquad\textit{compile}(a_1, E) \;@\; \textit{compile}(a_2, E)\;@\; \pcode{idiv}$}\smallskip\\
   142 $\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\
   142 % $\textit{compile}(x, E)$ & $\dn$ & $\pcode{iload}\;E(x)$\\
   143 \end{tabular}
   143 % \end{tabular}
   144 \end{center}}
   144 % \end{center}}
   145 
   145 
   146 \end{frame}
   146 % \end{frame}
   147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   147 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   148 
   148 
   149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   149 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   150 \begin{frame}[c,fragile]
   150 % \begin{frame}[c,fragile]
   151 \frametitle{Compiling Ifs}
   151 % \frametitle{Compiling Ifs}
   152 
   152 
   153 For example
   153 % For example
   154 
   154 
   155 \begin{lstlisting}[mathescape,numbers=none,language=While]
   155 % \begin{lstlisting}[mathescape,numbers=none,language=While]
   156 if 1 = 1 then x := 2 else y := 3
   156 % if 1 = 1 then x := 2 else y := 3
   157 \end{lstlisting}
   157 % \end{lstlisting}
   158 
   158 
   159 
   159 
   160 \begin{center}
   160 % \begin{center}
   161 \begin{lstlisting}[mathescape,language=JVMIS,numbers=none]
   161 % \begin{lstlisting}[mathescape,language=JVMIS,numbers=none]
   162    ldc 1
   162 %    ldc 1
   163    ldc 1
   163 %    ldc 1
   164    if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$
   164 %    if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$
   165    ldc 2
   165 %    ldc 2
   166    istore 0
   166 %    istore 0
   167    goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$
   167 %    goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$
   168 L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$
   168 % L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$
   169    ldc 3
   169 %    ldc 3
   170    istore 1
   170 %    istore 1
   171 L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$
   171 % L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$
   172 \end{lstlisting}
   172 % \end{lstlisting}
   173 \end{center}
   173 % \end{center}
   174 
   174 
   175 \begin{tikzpicture}[remember picture,overlay]
   175 % \begin{tikzpicture}[remember picture,overlay]
   176   \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) 
   176 %   \draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) 
   177            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east);
   177 %            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east);
   178   \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) 
   178 %   \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) 
   179            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east);
   179 %            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east);
   180 \end{tikzpicture}
   180 % \end{tikzpicture}
   181 
   181 
   182 \end{frame}
   182 % \end{frame}
   183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   183 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   184 
   184 
   185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   185 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   186 \begin{frame}[c,fragile]
   186 % \begin{frame}[c,fragile]
   187 \frametitle{Compiling Whiles}
   187 % \frametitle{Compiling Whiles}
   188 
   188 
   189 For example
   189 % For example
   190 
   190 
   191 \begin{lstlisting}[mathescape,numbers=none,language=While]
   191 % \begin{lstlisting}[mathescape,numbers=none,language=While]
   192 while x <= 10 do x := x + 1
   192 % while x <= 10 do x := x + 1
   193 \end{lstlisting}
   193 % \end{lstlisting}
   194 
   194 
   195 
   195 
   196 \begin{center}
   196 % \begin{center}
   197 \begin{lstlisting}[mathescape,language=JVMIS,numbers=none]
   197 % \begin{lstlisting}[mathescape,language=JVMIS,numbers=none]
   198 L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$
   198 % L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$
   199    iload 0
   199 %    iload 0
   200    ldc 10
   200 %    ldc 10
   201    if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$
   201 %    if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$
   202    iload 0
   202 %    iload 0
   203    ldc 1
   203 %    ldc 1
   204    iadd
   204 %    iadd
   205    istore 0
   205 %    istore 0
   206    goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$
   206 %    goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$
   207 L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$
   207 % L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$
   208 \end{lstlisting}
   208 % \end{lstlisting}
   209 \end{center}
   209 % \end{center}
   210 
   210 
   211 \begin{tikzpicture}[remember picture,overlay]
   211 % \begin{tikzpicture}[remember picture,overlay]
   212   \draw[->,very thick] (LA) edge [->,to path={-- ++(12mm,0mm) 
   212 %   \draw[->,very thick] (LA) edge [->,to path={-- ++(12mm,0mm) 
   213            -- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east);
   213 %            -- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east);
   214   \draw[->,very thick] (LC) edge [->,to path={-- ++(12mm,0mm) 
   214 %   \draw[->,very thick] (LC) edge [->,to path={-- ++(12mm,0mm) 
   215            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east);
   215 %            -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east);
   216 \end{tikzpicture}
   216 % \end{tikzpicture}
   217 
   217 
   218 \end{frame}
   218 % \end{frame}
   219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   219 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   220 
   220 
   221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   221 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   222 \begin{frame}[c,fragile]
   222 % \begin{frame}[c,fragile]
   223 \frametitle{Compiling Writes}
   223 % \frametitle{Compiling Writes}
   224 
   224 
   225 \small
   225 % \small
   226 \begin{lstlisting}[language=JVMIS,mathescape,
   226 % \begin{lstlisting}[language=JVMIS,mathescape,
   227                    numbers=none,xleftmargin=-6mm]
   227 %                    numbers=none,xleftmargin=-6mm]
   228 .method public static write(I)V 
   228 % .method public static write(I)V 
   229   .limit locals 1 
   229 %   .limit locals 1 
   230   .limit stack 2 
   230 %   .limit stack 2 
   231   getstatic java/lang/System/out 
   231 %   getstatic java/lang/System/out 
   232                             Ljava/io/PrintStream; 
   232 %                             Ljava/io/PrintStream; 
   233   iload 0
   233 %   iload 0
   234   invokevirtual java/io/PrintStream/println(I)V 
   234 %   invokevirtual java/io/PrintStream/println(I)V 
   235   return 
   235 %   return 
   236 .end method
   236 % .end method
   237 
   237 
   238 
   238 
   239 
   239 
   240 iload $E(x)$ 
   240 % iload $E(x)$ 
   241 invokestatic XXX/XXX/write(I)V
   241 % invokestatic XXX/XXX/write(I)V
   242 \end{lstlisting}
   242 % \end{lstlisting}
   243 
   243 
   244 \end{frame}
   244 % \end{frame}
   245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   245 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   246 
   246 
   247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   247 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   248 \begin{frame}[c,fragile]
   248 % \begin{frame}[c,fragile]
   249 \frametitle{Compiling Main}
   249 % \frametitle{Compiling Main}
   250 
   250 
   251 \footnotesize
   251 % \footnotesize
   252 \begin{lstlisting}[language=JVMIS,mathescape,
   252 % \begin{lstlisting}[language=JVMIS,mathescape,
   253                    numbers=none,xleftmargin=-6mm]
   253 %                    numbers=none,xleftmargin=-6mm]
   254 .class public XXX.XXX
   254 % .class public XXX.XXX
   255 .super java/lang/Object
   255 % .super java/lang/Object
   256 
   256 
   257 .method public <init>()V
   257 % .method public <init>()V
   258     aload_0
   258 %     aload_0
   259     invokenonvirtual java/lang/Object/<init>()V
   259 %     invokenonvirtual java/lang/Object/<init>()V
   260     return
   260 %     return
   261 .end method
   261 % .end method
   262 
   262 
   263 .method public static main([Ljava/lang/String;)V
   263 % .method public static main([Ljava/lang/String;)V
   264     .limit locals 200
   264 %     .limit locals 200
   265     .limit stack 200
   265 %     .limit stack 200
   266 
   266 
   267       $\textit{\ldots{}here comes the compiled code\ldots}$
   267 %       $\textit{\ldots{}here comes the compiled code\ldots}$
   268 
   268 
   269     return
   269 %     return
   270 .end method
   270 % .end method
   271 \end{lstlisting}
   271 % \end{lstlisting}
   272 
   272 
   273 \end{frame}
   273 % \end{frame}
   274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   274 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   275 
   275 
   276 
   276 
   277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   278 \begin{frame}[c,fragile]
   278 \begin{frame}[c,fragile]
   279 \frametitle{Functional Programming}
   279 \frametitle{Functional Programming}