author | Christian Urban <urbanc@in.tum.de> |
Fri, 25 Oct 2019 14:55:31 +0100 | |
changeset 667 | 412556272333 |
parent 608 | 3db4970ad0aa |
child 686 | 05cfce0fdef7 |
permissions | -rw-r--r-- |
63 | 1 |
\documentclass[dvipsnames,14pt,t]{beamer} |
302
0fa7b4221745
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
2 |
\usepackage{../slides} |
215
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
188
diff
changeset
|
3 |
\usepackage{../langs} |
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
188
diff
changeset
|
4 |
\usepackage{../data} |
608 | 5 |
\usepackage{../graphics} |
6 |
\usepackage{../grammar} |
|
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
7 |
|
63 | 8 |
% beamer stuff |
451 | 9 |
\renewcommand{\slidecaption}{CFL 07, King's College London} |
63 | 10 |
\newcommand{\bl}[1]{\textcolor{blue}{#1}} |
11 |
||
12 |
\begin{document} |
|
13 |
||
14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
302
0fa7b4221745
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
15 |
\begin{frame}[t] |
63 | 16 |
\frametitle{% |
17 |
\begin{tabular}{@ {}c@ {}} |
|
18 |
\\[-3mm] |
|
451 | 19 |
\LARGE Compilers and \\[-2mm] |
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
20 |
\LARGE Formal Languages (7)\\[3mm] |
63 | 21 |
\end{tabular}} |
22 |
||
23 |
\normalsize |
|
24 |
\begin{center} |
|
25 |
\begin{tabular}{ll} |
|
26 |
Email: & christian.urban at kcl.ac.uk\\ |
|
600 | 27 |
Office: & N\liningnums{7.07} (North Wing, Bush House)\\ |
608 | 28 |
Slides: & KEATS (also homework is there)\\ |
63 | 29 |
\end{tabular} |
30 |
\end{center} |
|
31 |
||
302
0fa7b4221745
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
32 |
\end{frame} |
0fa7b4221745
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
33 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
63 | 34 |
|
608 | 35 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
36 |
\begin{frame}[c] |
|
37 |
\frametitle{Bird's Eye View} |
|
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
38 |
|
608 | 39 |
\begin{center} |
40 |
\begin{tikzpicture} |
|
41 |
\node (rexp) {\bl{\bf Lexer}}; |
|
42 |
\node (nfa) [right=of rexp] {\bl{\bf Parser}}; |
|
43 |
\node (dfa) [right=of nfa] |
|
44 |
{\bl{\begin{tabular}{c}\bf Machine Code/\\\bf Java Byte Code\end{tabular}}}; |
|
45 |
\path[->, red, line width=2mm] (rexp) edge node [above=4mm, black] {\begin{tabular}{c@{\hspace{9mm}}}token\\[-1mm] |
|
46 |
sequence\end{tabular}} (nfa); |
|
47 |
\path[->, red, line width=2mm] (nfa) edge node [above=4mm, black] {\begin{tabular}{c}parse\\[-1mm] tree\end{tabular}}(dfa); |
|
48 |
\end{tikzpicture}\\ |
|
49 |
\end{center} |
|
50 |
||
51 |
\end{frame} |
|
52 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
53 |
||
63 | 54 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
55 |
\begin{frame}[c] |
|
608 | 56 |
\begin{textblock}{10}(0.5,0.5) |
57 |
\LARGE |
|
58 |
\fontspec{Hoefler Text Black} |
|
59 |
\textcolor{ProcessBlue}{JVM\\ Code} |
|
60 |
\\[12mm] |
|
61 |
||
62 |
\normalsize |
|
63 |
Jasmin\\ |
|
64 |
Krakatau\\ |
|
65 |
ASM lib |
|
66 |
\end{textblock} |
|
67 |
||
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
68 |
|
608 | 69 |
\fontsize{8}{10}\selectfont |
70 |
%\footnotesize |
|
71 |
\mbox{}\\[-8mm]\mbox{} |
|
72 |
||
73 |
\begin{columns} |
|
74 |
\begin{column}{2cm} |
|
75 |
\lstinputlisting[numbers=none]{../progs/appHa.j} |
|
76 |
\end{column} |
|
77 |
||
78 |
\begin{column}{2cm} |
|
79 |
\lstinputlisting[numbers=none]{../progs/appHb.j} |
|
80 |
\end{column} |
|
81 |
||
82 |
\end{columns} |
|
63 | 83 |
|
608 | 84 |
\end{frame} |
85 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
86 |
||
87 |
||
88 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
89 |
\begin{frame}[t] |
|
90 |
||
63 | 91 |
\begin{center} |
608 | 92 |
\bl{\begin{tabular}{@{}lcl@{}} |
93 |
\\[-12mm] |
|
94 |
\meta{Stmt} & $::=$ & $\texttt{skip}$\\ |
|
95 |
& $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\ |
|
96 |
& $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\ |
|
97 |
& $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\ |
|
98 |
& $|$ & \texttt{read}\;\textit{Id}\\ |
|
99 |
& $|$ & \texttt{write}\;\textit{Id}\\ |
|
100 |
& $|$ & \texttt{write}\;\textit{String}\medskip\\ |
|
101 |
\meta{Stmts} & $::=$ & \meta{Stmt} \;\texttt{;}\; \meta{Stmts}\\ |
|
102 |
& $|$ & \meta{Stmt}\medskip\\ |
|
103 |
\meta{Block} & $::=$ & \texttt{\{}\,\meta{Stmts}\,\texttt{\}}\\ |
|
104 |
& $|$ & \meta{Stmt}\medskip\\ |
|
105 |
\meta{AExp} & $::=$ & \ldots\\ |
|
106 |
\meta{BExp} & $::=$ & \ldots\\ |
|
107 |
\end{tabular}} |
|
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
108 |
\end{center} |
608 | 109 |
\end{frame} |
110 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
111 |
|
608 | 112 |
|
113 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
114 |
\begin{frame}[c] |
|
115 |
\frametitle{\begin{tabular}{c}Fibonacci Numbers\end{tabular}} |
|
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
116 |
|
608 | 117 |
\mbox{}\\[-18mm]\mbox{} |
118 |
||
119 |
{\lstset{language=While}\fontsize{10}{12}\selectfont |
|
120 |
\texttt{\lstinputlisting{../progs/fib.while}}} |
|
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
121 |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
122 |
\end{frame} |
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
123 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
124 |
|
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
125 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
387
8aa406adfde0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
374
diff
changeset
|
126 |
\begin{frame}[c] |
608 | 127 |
\frametitle{Interpreter} |
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
128 |
|
63 | 129 |
\begin{center} |
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
130 |
\bl{\begin{tabular}{@{}lcl@{}} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
131 |
$\text{eval}(n, E)$ & $\dn$ & $n$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
132 |
$\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
133 |
$\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
134 |
$\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
135 |
$\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
136 |
$\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
137 |
$\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
138 |
$\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
139 |
\end{tabular}} |
63 | 140 |
\end{center} |
141 |
||
608 | 142 |
\end{frame} |
63 | 143 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
608 | 144 |
|
63 | 145 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
146 |
\begin{frame}[c] |
|
608 | 147 |
\frametitle{Interpreter (2)} |
63 | 148 |
|
149 |
\begin{center} |
|
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
150 |
\bl{\begin{tabular}{@{}lcl@{}} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
151 |
$\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
152 |
$\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
153 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
154 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\; |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
155 |
\text{eval}(cs_1,E)$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
156 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
157 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
158 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
159 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\; |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
160 |
\text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
161 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\ |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
162 |
$\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\ |
63 | 163 |
\end{tabular}} |
164 |
\end{center} |
|
165 |
||
608 | 166 |
\end{frame} |
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
167 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
168 |
|
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
169 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
170 |
\begin{frame}[c] |
608 | 171 |
\frametitle{Test Program} |
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
172 |
|
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
173 |
\mbox{}\\[-18mm]\mbox{} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
174 |
|
608 | 175 |
{\lstset{language=While}\fontsize{10}{12}\selectfont |
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
176 |
\texttt{\lstinputlisting{../progs/loops.while}}} |
63 | 177 |
|
608 | 178 |
\end{frame} |
63 | 179 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
180 |
||
181 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
608 | 182 |
\begin{frame}[c] |
183 |
\fontsize{7}{9}\selectfont |
|
184 |
||
185 |
||
186 |
\begin{columns} |
|
187 |
\begin{column}{7cm} |
|
188 |
\lstinputlisting[numbers=none]{../progs/appHa.j} |
|
189 |
\end{column} |
|
190 |
||
191 |
\begin{column}{7cm} |
|
192 |
\lstinputlisting[numbers=none]{../progs/appHb.j} |
|
193 |
\end{column} |
|
194 |
\end{columns} |
|
195 |
||
196 |
\end{frame} |
|
197 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
198 |
||
199 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
200 |
\begin{frame}[t] |
608 | 201 |
\frametitle{Interpreted Code} |
63 | 202 |
|
203 |
\begin{center} |
|
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
204 |
\begin{tikzpicture} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
205 |
\begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small] |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
206 |
\addplot+[smooth] file {interpreted.data}; |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
207 |
\end{axis} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
208 |
\end{tikzpicture} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
209 |
\end{center} |
63 | 210 |
|
608 | 211 |
\end{frame} |
63 | 212 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
213 |
||
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
214 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
215 |
\begin{frame}[c] |
608 | 216 |
\frametitle{Java Virtual Machine} |
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
217 |
|
188
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
218 |
\begin{itemize} |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
219 |
\item introduced in 1995 |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
220 |
\item is a stack-based VM (like Postscript, CLR of .Net) |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
221 |
\item contains a JIT compiler |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
222 |
\item many languages take advantage of JVM's infrastructure (JRE) |
12ef150273ce
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
187
diff
changeset
|
223 |
\item is garbage collected $\Rightarrow$ no buffer overflows |
608 | 224 |
\item some languages compiled to the JVM: Scala, Clojure\ldots |
225 |
\end{itemize} |
|
226 |
||
227 |
\end{frame} |
|
228 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
229 |
||
230 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
231 |
\begin{frame}[t,fragile] |
|
232 |
\frametitle{Compiling AExps} |
|
233 |
||
234 |
For example \liningnums{\bl{$1 + ((2 * 3) + (4 - 3))$}}:\medskip |
|
235 |
||
236 |
\begin{columns}[T] |
|
237 |
\begin{column}{.3\textwidth} |
|
238 |
\begin{center} |
|
239 |
\bl{\begin{tikzpicture} |
|
240 |
\tikzset{level distance=12mm,sibling distance=4mm} |
|
241 |
\tikzset{edge from parent/.style={draw,very thick}} |
|
242 |
\Tree [.$+$ [.$1$ ] [.$+$ [.$*$ $2$ $3$ ] [.$-$ $4$ $3$ ]]] |
|
243 |
\end{tikzpicture}} |
|
244 |
\end{center} |
|
245 |
\end{column} |
|
246 |
\begin{column}{.3\textwidth} |
|
247 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
|
248 |
ldc 1 |
|
249 |
ldc 2 |
|
250 |
ldc 3 |
|
251 |
imul |
|
252 |
ldc 4 |
|
253 |
ldc 3 |
|
254 |
isub |
|
255 |
iadd |
|
256 |
iadd |
|
257 |
\end{lstlisting} |
|
258 |
\end{column} |
|
259 |
\end{columns}\bigskip |
|
260 |
||
261 |
\small |
|
262 |
Traverse tree in post-order \bl{$\Rightarrow$} code for |
|
263 |
stack-machine |
|
264 |
||
265 |
\end{frame} |
|
266 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
267 |
||
268 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
269 |
\begin{frame}[t,fragile] |
|
270 |
\frametitle{Compiling AExps} |
|
271 |
||
272 |
\liningnums{\textbf{\Large\bl{1 + 2 + 3}}} |
|
273 |
||
274 |
\begin{lstlisting}[language=JVMIS,numbers=none,xleftmargin=6cm] |
|
275 |
ldc 1 |
|
276 |
ldc 2 |
|
277 |
iadd |
|
278 |
ldc 3 |
|
279 |
iadd |
|
280 |
\end{lstlisting} |
|
281 |
||
282 |
||
283 |
\end{frame} |
|
284 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
285 |
||
286 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
287 |
\begin{frame}[t,fragile] |
|
288 |
\frametitle{Compiling AExps} |
|
289 |
||
290 |
\liningnums{\textbf{\Large\bl{1 + (2 + 3)}}} |
|
291 |
||
292 |
\begin{lstlisting}[language=JVMIS,numbers=none,xleftmargin=6cm] |
|
293 |
ldc 1 |
|
294 |
ldc 2 |
|
295 |
ldc 3 |
|
296 |
iadd |
|
297 |
iadd |
|
298 |
\end{lstlisting} |
|
299 |
\bigskip\pause |
|
300 |
\vfill |
|
301 |
||
302 |
\textcolor{codepurple}{\textbf{\texttt{dadd}}}, |
|
303 |
\textcolor{codepurple}{\textbf{\texttt{fadd}}}, |
|
304 |
\textcolor{codepurple}{\textbf{\texttt{ladd}}}, \ldots |
|
305 |
||
306 |
\end{frame} |
|
307 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
308 |
||
309 |
||
310 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
311 |
\begin{frame}[t] |
|
312 |
\frametitle{Compiling AExps} |
|
313 |
||
314 |
\begin{center} |
|
315 |
\bl{\begin{tabular}{@{}lcl@{}} |
|
316 |
$\text{compile}(n)$ & $\dn$ & $\text{ldc}\;n$\\ |
|
317 |
$\text{compile}(a_1 + a_2)$ & $\dn$\\ |
|
318 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1) \;@\;\text{compile}(a_2)\;@\; \text{iadd}$}\smallskip\\ |
|
319 |
$\text{compile}(a_1 - a_2)$ & $\dn$\\ |
|
320 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1) \;@\; \text{compile}(a_2)\;@\; \text{isub}$}\smallskip\\ |
|
321 |
$\text{compile}(a_1 * a_2)$ & $\dn$\\ |
|
322 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1) \;@\; \text{compile}(a_2)\;@\; \text{imul}$}\smallskip\\ |
|
323 |
\end{tabular}} |
|
324 |
\end{center} |
|
325 |
||
326 |
\end{frame} |
|
327 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
328 |
||
329 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
330 |
\begin{frame}[t,fragile] |
|
331 |
\frametitle{\begin{tabular}{c}Compiling AExps\end{tabular}} |
|
332 |
||
333 |
\liningnums{\textbf{\Large\bl{1 $+$ 2 $*$ 3 $+$ (4 $-$ 3)}}} |
|
334 |
||
335 |
\begin{lstlisting}[language=JVMIS,numbers=none,xleftmargin=6cm] |
|
336 |
ldc 1 |
|
337 |
ldc 2 |
|
338 |
ldc 3 |
|
339 |
imul |
|
340 |
ldc 4 |
|
341 |
ldc 3 |
|
342 |
isub |
|
343 |
iadd |
|
344 |
iadd |
|
345 |
\end{lstlisting} |
|
346 |
||
347 |
\end{frame} |
|
348 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
349 |
||
350 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
351 |
\begin{frame}[c] |
|
352 |
\frametitle{Variables} |
|
353 |
\liningnums{\textbf{\Large\bl{x $:=$ 5 $+$ y $*$ 2}}}\bigskip\pause |
|
354 |
||
355 |
\begin{itemize} |
|
356 |
\item lookup: \bl{$\textcolor{codepurple}{\textbf{\texttt{iload}}}\; index$} |
|
357 |
\item store: \bl{$\textcolor{codepurple}{\textbf{\texttt{istore}}}\; index$} |
|
358 |
\end{itemize}\bigskip\pause |
|
359 |
||
360 |
while compilating we have to maintain a map between our identifiers and the |
|
361 |
Java bytecode indices |
|
362 |
||
363 |
\begin{center} |
|
364 |
\bl{$\text{compile}(a, E)$} |
|
365 |
\end{center} |
|
366 |
||
367 |
\end{frame} |
|
368 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
369 |
||
370 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
371 |
\begin{frame}[t] |
|
372 |
\frametitle{Compiling AExps} |
|
373 |
||
374 |
\begin{center} |
|
375 |
\bl{\begin{tabular}{@{}lcl@{}} |
|
376 |
$\text{compile}(n, E)$ & $\dn$ & $\text{ldc}\;n$\\ |
|
377 |
$\text{compile}(a_1 + a_2, E)$ & $\dn$\\ |
|
378 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1, E) \;@\;\text{compile}(a_2, E)\;@\; \text{iadd}$}\smallskip\\ |
|
379 |
$\text{compile}(a_1 - a_2, E)$ & $\dn$\\ |
|
380 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1, E) \;@\; \text{compile}(a_2, E)\;@\; \text{isub}$}\smallskip\\ |
|
381 |
$\text{compile}(a_1 * a_2, E)$ & $\dn$\\ |
|
382 |
\multicolumn{3}{l}{$\qquad\text{compile}(a_1, E) \;@\; \text{compile}(a_2, E)\;@\; \text{imul}$}\bigskip\\ |
|
383 |
$\text{compile}(x, E)$ & $\dn$ & $\text{iload}\;E(x)$\\ |
|
384 |
\end{tabular}} |
|
385 |
\end{center} |
|
386 |
||
387 |
\end{frame} |
|
388 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
389 |
||
390 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
391 |
\begin{frame}[c, fragile] |
|
392 |
\frametitle{Mathematical Functions} |
|
393 |
||
394 |
Compilation of some mathematical functions: |
|
395 |
||
396 |
\begin{center} |
|
397 |
\begin{tabular}{lcl} |
|
398 |
\texttt{Aop("+", a1, a2)} & $\Rightarrow$ & \texttt{...iadd}\\ |
|
399 |
\texttt{Aop("-", a1, a2)} & $\Rightarrow$ & \texttt{...isub}\\ |
|
400 |
\texttt{Aop("*", a1, a2)} & $\Rightarrow$ & \texttt{...imul}\\ |
|
401 |
\texttt{Aop("/", a1, a2)} & $\Rightarrow$ & \texttt{...idiv}\\ |
|
402 |
\texttt{Aop("\%", a1, a2)} & $\Rightarrow$ & \texttt{...irem}\\ |
|
403 |
\end{tabular} |
|
404 |
\end{center} |
|
405 |
||
406 |
\end{frame} |
|
407 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
408 |
||
409 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
410 |
\begin{frame}[c] |
|
411 |
\frametitle{Compiling Statements} |
|
412 |
||
413 |
We return a list of instructions and an environment for the |
|
414 |
variables |
|
415 |
||
416 |
\begin{center} |
|
417 |
\bl{\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
|
418 |
$\text{compile}(\text{skip}, E)$ & $\dn$ & $(\textit{Nil}, E)$\bigskip\\ |
|
419 |
$\text{compile}(x := a, E)$ & $\dn$\\ |
|
420 |
\multicolumn{3}{l}{$(\text{compile}(a, E) \;@\;\text{istore}\;index, E(x\mapsto index))$}\\ |
|
421 |
\end{tabular}} |
|
422 |
\end{center}\medskip |
|
423 |
||
424 |
where \bl{$index$} is \bl{$E(x)$} if it is already defined, |
|
425 |
or if it is not, then the largest index not yet seen |
|
426 |
||
427 |
\end{frame} |
|
428 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
429 |
||
430 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
431 |
\begin{frame}[c,fragile] |
|
432 |
\frametitle{Compiling Assignments} |
|
433 |
||
434 |
\liningnums{\textbf{\Large\bl{x $:=$ x $+$ 1}}} |
|
435 |
||
436 |
\begin{lstlisting}[language=JVMIS,numbers=none,xleftmargin=6cm] |
|
437 |
iload /*@\bl{$n_x$}@*/ |
|
438 |
ldc 1 |
|
439 |
iadd |
|
440 |
istore /*@\bl{$n_x$}@*/ |
|
441 |
\end{lstlisting}\medskip |
|
442 |
||
443 |
where \bl{$n_x$} is the index corresponding to the variable~\bl{$x$} |
|
444 |
||
445 |
\end{frame} |
|
446 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
447 |
||
448 |
||
449 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
450 |
\begin{frame}[t] |
|
451 |
\frametitle{Compiling Ifs} |
|
452 |
||
453 |
{\Large\bl{$\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2$}}\bigskip\bigskip |
|
454 |
||
455 |
\onslide<2->{Case }\only<2>{{\bf True}:}\only<3>{{\bf False}:} |
|
456 |
||
457 |
\begin{center} |
|
458 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
|
459 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
|
460 |
point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, |
|
461 |
skip loop/.style={red, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] |
|
462 |
\node (A1) [point] {}; |
|
463 |
\node (b) [block, right=of A1] {code of \bl{$b$}}; |
|
464 |
\node (A2) [point, right=of b] {}; |
|
465 |
\node (cs1) [block, right=of A2] {code of \bl{$cs_1$}}; |
|
466 |
\node (A3) [point, right=of cs1] {}; |
|
467 |
\node (cs2) [block, right=of A3] {code of \bl{$cs_2$}}; |
|
468 |
\node (A4) [point, right=of cs2] {}; |
|
469 |
||
470 |
\only<2>{ |
|
471 |
\draw (A1) edge [->, red, line width=1mm] (b); |
|
472 |
\draw (b) edge [->, red, line width=1mm] (cs1); |
|
473 |
\draw (cs1) edge [->, red, line width=1mm] (A3); |
|
474 |
\draw (A3) edge [->,skip loop] (A4); |
|
475 |
\node [below=of cs2] {\raisebox{-5mm}{\small{}jump}};} |
|
476 |
\only<3>{ |
|
477 |
\draw (A1) edge [->, red, line width=1mm] (b); |
|
478 |
\draw (b) edge [->, red, line width=1mm] (A2); |
|
479 |
\draw (A2) edge [skip loop] (A3); |
|
480 |
\draw (A3) edge [->, red, line width=1mm] (cs2); |
|
481 |
\draw (cs2) edge [->,red, line width=1mm] (A4); |
|
482 |
\node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}};} |
|
483 |
\end{tikzpicture} |
|
484 |
\end{center} |
|
485 |
||
486 |
\end{frame} |
|
487 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
488 |
||
489 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
490 |
\begin{frame}[t,fragile] |
|
491 |
\frametitle{Conditional Jumps} |
|
492 |
||
493 |
\begin{minipage}{1.1\textwidth} |
|
494 |
\begin{itemize} |
|
495 |
\item \textcolor{codepurple}{\textbf{\texttt{if\_icmpeq}}} \bl{$label$} |
|
496 |
if two ints are equal, then jump\medskip |
|
497 |
\item \textcolor{codepurple}{\textbf{\texttt{if\_icmpne}}} \bl{$label$} |
|
498 |
if two ints aren't equal, then jump\medskip |
|
499 |
\item \textcolor{codepurple}{\textbf{\texttt{if\_icmpge}}} \bl{$label$} |
|
500 |
if one int is greater or equal then another, then jump |
|
501 |
\item[]\ldots |
|
502 |
\end{itemize} |
|
503 |
\end{minipage}\pause |
|
504 |
||
505 |
\begin{lstlisting}[language=JVMIS,numbers=none,xleftmargin=2cm] |
|
506 |
/*@\bl{$L_1$:}@*/ |
|
507 |
if_icmpeq /*@\bl{$L_2$}@*/ |
|
508 |
iload 1 |
|
509 |
ldc 1 |
|
510 |
iadd |
|
511 |
if_icmpeq /*@\bl{$L_1$}@*/ |
|
512 |
/*@\bl{$L_2$:}@*/ |
|
513 |
\end{lstlisting} |
|
514 |
||
515 |
||
516 |
\begin{textblock}{3.5}(11,12) |
|
517 |
\only<3>{labels must be unique} |
|
518 |
\end{textblock} |
|
519 |
\end{frame} |
|
520 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
521 |
||
522 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
523 |
\begin{frame}[c,fragile] |
|
524 |
\frametitle{Compiling Ifs} |
|
525 |
||
526 |
For example |
|
527 |
||
528 |
\begin{lstlisting}[mathescape,numbers=none,language=While] |
|
529 |
if 1 = 1 then x := 2 else y := 3 |
|
530 |
\end{lstlisting} |
|
531 |
||
532 |
||
533 |
\begin{center} |
|
534 |
\begin{lstlisting}[mathescape,language=JVMIS,numbers=none] |
|
535 |
ldc 1 |
|
536 |
ldc 1 |
|
537 |
if_icmpne L_ifelse $\quad\tikz[remember picture] \node (C) {\mbox{}};$ |
|
538 |
ldc 2 |
|
539 |
istore 0 |
|
540 |
goto L_ifend $\quad\tikz[remember picture] \node (A) {\mbox{}};$ |
|
541 |
L_ifelse: $\quad\tikz[remember picture] \node[] (D) {\mbox{}};$ |
|
542 |
ldc 3 |
|
543 |
istore 1 |
|
544 |
L_ifend: $\quad\tikz[remember picture] \node[] (B) {\mbox{}};$ |
|
545 |
\end{lstlisting} |
|
546 |
\end{center} |
|
547 |
||
548 |
\begin{tikzpicture}[remember picture,overlay] |
|
549 |
\draw[->,very thick] (A) edge [->,to path={-- ++(10mm,0mm) |
|
550 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (B.east); |
|
551 |
\draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
|
552 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); |
|
553 |
\end{tikzpicture} |
|
554 |
||
555 |
\end{frame} |
|
556 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
557 |
||
558 |
||
559 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
560 |
\begin{frame}[t] |
|
561 |
\frametitle{Compiling BExps} |
|
562 |
||
563 |
{\Large\bl{$a_1 = a_2$}} |
|
564 |
||
565 |
\begin{center} |
|
566 |
\bl{\begin{tabular}{lcl} |
|
567 |
$\text{compile}(a_1 = a_2, E, lab)$ & $\dn$\\ |
|
568 |
\multicolumn{3}{l}{$\quad\text{compile}(a_1, E) \;@\;\text{compile}(a_2, E)\;@\; \text{if\_icmpne}\;lab$} |
|
569 |
\end{tabular}} |
|
570 |
\end{center} |
|
571 |
||
572 |
\end{frame} |
|
573 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
574 |
||
575 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
576 |
\begin{frame}[c, fragile] |
|
577 |
\frametitle{Boolean Expressions} |
|
578 |
||
579 |
Compilation of boolean expressions: |
|
580 |
||
581 |
\begin{center} |
|
582 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
|
583 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
|
584 |
point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, |
|
585 |
skip loop/.style={red, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] |
|
586 |
\node (A1) [point] {}; |
|
587 |
\node (b) [block, right=of A1] {code of \bl{$b$}}; |
|
588 |
\node (A2) [point, right=of b] {}; |
|
589 |
\node (cs1) [block, right=of A2] {code of \bl{$cs_1$}}; |
|
590 |
\node (A3) [point, right=of cs1] {}; |
|
591 |
\node (cs2) [block, right=of A3] {code of \bl{$cs_2$}}; |
|
592 |
\node (A4) [point, right=of cs2] {}; |
|
593 |
||
594 |
\only<1>{ |
|
595 |
\draw (A1) edge [->, red, line width=1mm] (b); |
|
596 |
\draw (b) edge [->, red, line width=1mm] (A2); |
|
597 |
\draw (A2) edge [skip loop] (A3); |
|
598 |
\draw (A3) edge [->, red, line width=1mm] (cs2); |
|
599 |
\draw (cs2) edge [->,red, line width=1mm] (A4); |
|
600 |
\node [below=of cs1] {\raisebox{-5mm}{\small{}conditional jump}};} |
|
601 |
\end{tikzpicture} |
|
602 |
\end{center} |
|
603 |
||
604 |
\begin{center} |
|
605 |
\begin{tabular}{lcl} |
|
606 |
\texttt{Bop("==", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpne...}\\ |
|
607 |
\texttt{Bop("!=", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpeq...}\\ |
|
608 |
\texttt{Bop("<", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpge...}\\ |
|
609 |
\texttt{Bop("<=", a1, a2)} & $\Rightarrow$ & \texttt{...if\_icmpgt...}\\ |
|
610 |
\end{tabular} |
|
611 |
\end{center} |
|
612 |
||
613 |
\end{frame} |
|
614 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
615 |
||
616 |
||
617 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
618 |
\begin{frame}[t] |
|
619 |
\frametitle{Compiling Ifs} |
|
620 |
||
621 |
{\Large\bl{if $b$ then $cs_1$ else $cs_2$}} |
|
622 |
||
623 |
\begin{center} |
|
624 |
\bl{\begin{tabular}{lcl} |
|
625 |
$\text{compile}(\text{if}\;b\;\text{then}\; cs_1\;\text{else}\; cs_2, E)$ & $\dn$\\ |
|
626 |
\multicolumn{3}{l}{$\quad l_{ifelse}\;$ \textcolor{black}{(fresh label)}}\\ |
|
627 |
\multicolumn{3}{l}{$\quad l_{ifend}\;$ \textcolor{black}{(fresh label)}}\\ |
|
628 |
\multicolumn{3}{l}{$\quad (is_1, E') = \text{compile}(cs_1, E)$}\\ |
|
629 |
\multicolumn{3}{l}{$\quad (is_2, E'') = \text{compile}(cs_2, E')$}\\ |
|
630 |
\multicolumn{3}{l}{$\quad(\text{compile}(b, E, l_{ifelse})$}\\ |
|
631 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;is_1$}\\ |
|
632 |
\multicolumn{3}{l}{$\quad\phantom{(}@\; \text{goto}\;l_{ifend}$}\\ |
|
633 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;l_{ifelse}:$}\\ |
|
634 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;is_2$}\\ |
|
635 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;l_{ifend}:, E'')$}\\ |
|
636 |
\end{tabular}} |
|
637 |
\end{center} |
|
638 |
||
639 |
\end{frame} |
|
640 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
641 |
||
642 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
643 |
\begin{frame}[t] |
|
644 |
\frametitle{Compiling Whiles} |
|
645 |
||
646 |
{\Large\bl{$\text{while}\;b\;\text{do}\;cs$}}\bigskip\bigskip |
|
647 |
||
648 |
\onslide<2->{Case }\only<2>{{\bf True}:}\only<3>{{\bf False}:} |
|
649 |
||
650 |
\begin{center} |
|
651 |
\begin{tikzpicture}[node distance=2mm and 4mm, |
|
652 |
block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm}, |
|
653 |
point/.style={rectangle, inner sep=0mm, minimum size=0mm, fill=red}, |
|
654 |
skip loop/.style={red, line width=1mm, to path={-- ++(0,-10mm) -| (\tikztotarget)}}] |
|
655 |
\node (A0) [point, left=of A1] {}; |
|
656 |
\node (A1) [point] {}; |
|
657 |
\node (b) [block, right=of A1] {code of \bl{$b$}}; |
|
658 |
\node (A2) [point, right=of b] {}; |
|
659 |
\node (cs1) [block, right=of A2] {code of \bl{$cs$}}; |
|
660 |
\node (A3) [point, right=of cs1] {}; |
|
661 |
\node (A4) [point, right=of A3] {}; |
|
662 |
||
663 |
\only<2>{ |
|
664 |
\draw (A0) edge [->, red, line width=1mm] (b); |
|
665 |
\draw (b) edge [->, red, line width=1mm] (cs1); |
|
666 |
\draw (cs1) edge [->, red, line width=1mm] (A3); |
|
667 |
\draw (A3) edge [->,skip loop] (A1);} |
|
668 |
\only<3>{ |
|
669 |
\draw (A0) edge [->, red, line width=1mm] (b); |
|
670 |
\draw (b) edge [->, red, line width=1mm] (A2); |
|
671 |
\draw (A2) edge [skip loop] (A3); |
|
672 |
\draw (A3) edge [->, red, line width=1mm] (A4);} |
|
673 |
\end{tikzpicture} |
|
674 |
\end{center} |
|
675 |
||
676 |
\end{frame} |
|
677 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
678 |
||
679 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
680 |
\begin{frame}[t] |
|
681 |
\frametitle{Compiling Whiles} |
|
682 |
||
683 |
{\Large\bl{while $b$ do $cs$}} |
|
684 |
||
685 |
\begin{center} |
|
686 |
\bl{\begin{tabular}{lcl} |
|
687 |
$\text{compile}(\text{while}\; b\; \text{do} \;cs, E)$ & $\dn$\\ |
|
688 |
\multicolumn{3}{l}{$\quad l_{wbegin}\;$ \textcolor{black}{(fresh label)}}\\ |
|
689 |
\multicolumn{3}{l}{$\quad l_{wend}\;$ \textcolor{black}{(fresh label)}}\\ |
|
690 |
\multicolumn{3}{l}{$\quad (is, E') = \text{compile}(cs_1, E)$}\\ |
|
691 |
\multicolumn{3}{l}{$\quad(l_{wbegin}:$}\\ |
|
692 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;\text{compile}(b, E, l_{wend})$}\\ |
|
693 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;is$}\\ |
|
694 |
\multicolumn{3}{l}{$\quad\phantom{(}@\; \text{goto}\;l_{wbegin}$}\\ |
|
695 |
\multicolumn{3}{l}{$\quad\phantom{(}@\;l_{wend}:, E')$}\\ |
|
696 |
\end{tabular}} |
|
697 |
\end{center} |
|
698 |
||
699 |
\end{frame} |
|
700 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
701 |
||
702 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
703 |
\begin{frame}[c,fragile] |
|
704 |
\frametitle{Compiling Whiles} |
|
705 |
||
706 |
For example |
|
707 |
||
708 |
\begin{lstlisting}[mathescape,numbers=none,language=While] |
|
709 |
while x <= 10 do x := x + 1 |
|
710 |
\end{lstlisting} |
|
711 |
||
712 |
||
713 |
\begin{center} |
|
714 |
\begin{lstlisting}[mathescape,language=JVMIS,numbers=none] |
|
715 |
L_wbegin: $\quad\tikz[remember picture] \node[] (LB) {\mbox{}};$ |
|
716 |
iload 0 |
|
717 |
ldc 10 |
|
718 |
if_icmpgt L_wend $\quad\tikz[remember picture] \node (LC) {\mbox{}};$ |
|
719 |
iload 0 |
|
720 |
ldc 1 |
|
721 |
iadd |
|
722 |
istore 0 |
|
723 |
goto L_wbegin $\quad\tikz[remember picture] \node (LA) {\mbox{}};$ |
|
724 |
L_wend: $\quad\tikz[remember picture] \node[] (LD) {\mbox{}};$ |
|
725 |
\end{lstlisting} |
|
726 |
\end{center} |
|
727 |
||
728 |
\begin{tikzpicture}[remember picture,overlay] |
|
729 |
\draw[->,very thick] (LA) edge [->,to path={-- ++(12mm,0mm) |
|
730 |
-- ++(0mm,17.3mm) |- (\tikztotarget)},line width=1mm] (LB.east); |
|
731 |
\draw[->,very thick] (LC) edge [->,to path={-- ++(12mm,0mm) |
|
732 |
-- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (LD.east); |
|
733 |
\end{tikzpicture} |
|
734 |
||
735 |
\end{frame} |
|
736 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
737 |
||
738 |
||
739 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
740 |
\begin{frame}[c,fragile] |
|
741 |
\frametitle{Compiling Writes} |
|
742 |
||
743 |
\small |
|
744 |
\begin{lstlisting}[language=JVMIS,mathescape, |
|
745 |
numbers=none,xleftmargin=-6mm] |
|
746 |
.method public static write(I)V |
|
747 |
.limit locals 1 |
|
748 |
.limit stack 2 |
|
749 |
getstatic java/lang/System/out |
|
750 |
Ljava/io/PrintStream; |
|
751 |
iload 0 |
|
752 |
invokevirtual java/io/PrintStream/println(I)V |
|
753 |
return |
|
754 |
.end method |
|
755 |
||
756 |
||
757 |
||
758 |
iload $E(x)$ |
|
759 |
invokestatic XXX/XXX/write(I)V |
|
760 |
\end{lstlisting} |
|
761 |
||
762 |
\end{frame} |
|
763 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
764 |
||
765 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
766 |
\begin{frame}[c,fragile] |
|
767 |
\frametitle{Compiling Main} |
|
768 |
||
769 |
\footnotesize |
|
770 |
\begin{lstlisting}[language=JVMIS,mathescape, |
|
771 |
numbers=none,xleftmargin=-6mm] |
|
772 |
.class public XXX.XXX |
|
773 |
.super java/lang/Object |
|
774 |
||
775 |
.method public <init>()V |
|
776 |
aload_0 |
|
777 |
invokenonvirtual java/lang/Object/<init>()V |
|
778 |
return |
|
779 |
.end method |
|
780 |
||
781 |
.method public static main([Ljava/lang/String;)V |
|
782 |
.limit locals 200 |
|
783 |
.limit stack 200 |
|
784 |
||
785 |
$\textit{\ldots{}here comes the compiled code\ldots}$ |
|
786 |
||
787 |
return |
|
788 |
.end method |
|
789 |
\end{lstlisting} |
|
790 |
||
791 |
\end{frame} |
|
792 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
793 |
||
794 |
||
795 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
796 |
\mode<presentation>{ |
|
797 |
\begin{frame}[c] |
|
798 |
\frametitle{\begin{tabular}{c}Next Compiler Phases\end{tabular}} |
|
799 |
||
800 |
\begin{itemize} |
|
801 |
\item assembly $\Rightarrow$ byte code (class file) |
|
802 |
\item labels $\Rightarrow$ absolute or relative jumps\bigskip\bigskip |
|
803 |
\item \texttt{javap} is a disassembler for class files |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
804 |
\end{itemize} |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
805 |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
806 |
\end{frame}} |
608 | 807 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
808 |
||
809 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
810 |
\mode<presentation>{ |
|
811 |
\begin{frame}[t] |
|
812 |
\frametitle{\begin{tabular}{c}Compiled Code\end{tabular}} |
|
813 |
||
814 |
\begin{center} |
|
815 |
\begin{tikzpicture} |
|
816 |
\begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small] |
|
817 |
\addplot+[smooth] file {compiled.data}; |
|
818 |
\end{axis} |
|
819 |
\end{tikzpicture} |
|
820 |
\end{center} |
|
821 |
||
822 |
\end{frame}} |
|
823 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
824 |
||
825 |
||
826 |
||
827 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
828 |
\mode<presentation>{ |
|
829 |
\begin{frame}[t] |
|
830 |
\frametitle{\begin{tabular}{c}Compiler vs.~Interpreter\end{tabular}} |
|
831 |
||
832 |
\begin{center} |
|
833 |
\begin{tikzpicture} |
|
834 |
\begin{axis}[axis x line=bottom, axis y line=left, ylabel=secs, |
|
835 |
xlabel=n, |
|
836 |
enlargelimits=0.05, |
|
837 |
ybar interval=0.7, legend style=small] |
|
838 |
\addplot file {interpreted2.data}; |
|
839 |
\addplot file {compiled2.data}; |
|
840 |
%\legend{interpreted, compiled} |
|
841 |
\end{axis} |
|
842 |
\end{tikzpicture} |
|
843 |
\end{center} |
|
844 |
||
845 |
\end{frame}} |
|
846 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
847 |
||
848 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
849 |
\mode<presentation>{ |
|
850 |
\begin{frame}[c] |
|
851 |
\frametitle{Backend} |
|
852 |
||
853 |
\begin{center} |
|
854 |
\begin{tikzpicture} |
|
855 |
\node (rexp) {\bl{\bf Lexer}}; |
|
856 |
\node (nfa) [right=of rexp] {\bl{\bf Parser}}; |
|
857 |
\node (dfa) [right=of nfa] {\bl{\begin{tabular}{c}\bf Optimizations\end{tabular}}}; |
|
858 |
\path[->, red, line width=2mm] (rexp) edge node [above=4mm, black] {\begin{tabular}{c@{\hspace{9mm}}}token\\[-1mm] |
|
859 |
sequence\end{tabular}} (nfa); |
|
860 |
\node (final) [below=of dfa] {\bl{\begin{tabular}{c}\bf Machine Code/\\\bf Byte Code\end{tabular}}}; |
|
861 |
\path[->, red, line width=2mm] (nfa) edge node [above=4mm, black] {\begin{tabular}{c}parse\\[-1mm] tree |
|
862 |
\end{tabular}}(dfa); |
|
863 |
\path[->, red, line width=2mm] (dfa) edge (final); |
|
864 |
\end{tikzpicture}\\ |
|
865 |
\end{center} |
|
866 |
||
867 |
\end{frame}} |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
868 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
302
diff
changeset
|
869 |
|
608 | 870 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
871 |
\mode<presentation>{ |
|
872 |
\begin{frame}[t] |
|
873 |
\frametitle{\begin{tabular}{c}What is Next\end{tabular}} |
|
874 |
||
875 |
\begin{itemize} |
|
876 |
\item register spilling |
|
877 |
\item dead code removal |
|
878 |
\item loop optimisations |
|
879 |
\item instruction selection |
|
880 |
\item type checking |
|
881 |
\item concurrency |
|
882 |
\item fuzzy testing |
|
883 |
\item verification\bigskip\\ |
|
884 |
||
885 |
\item GCC, LLVM, tracing JITs |
|
886 |
\end{itemize} |
|
887 |
||
888 |
\end{frame}} |
|
889 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
890 |
||
184
2e9134d25a2b
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
891 |
|
63 | 892 |
\end{document} |
893 |
||
894 |
%%% Local Variables: |
|
895 |
%%% mode: latex |
|
896 |
%%% TeX-master: t |
|
897 |
%%% End: |
|
898 |