\documentclass{article}
\usepackage{../style}
\usepackage{../graphics}
\usepackage{../langs}
\usepackage{../grammar}
\begin{document}
\section*{Homework 9}
%\HEADER
\begin{enumerate}
\item Describe what is meant by \emph{eliminating tail
recursion}? When can this optimization be applied and
why is it of benefit?
\solution{ Tail-call optimisation replaces a recursive call (in
tail-call position) by a jump to the beginning of a function.
In this way a recursion is replaced by a loop. This saves stack
space because no new stack space needs to be allocated when a
function is called recursively.
Tail-call optimisation can be applied when the recursive call is
the last instruction that is run in the function.}
\item A programming language has arithmetic expression. For an
arithmetic expression the compiler of this language produces the
following snippet of JVM code.
\begin{lstlisting}[language=JVMIS,numbers=none]
ldc 1
ldc 2
ldc 3
imul
ldc 4
ldc 3
isub
iadd
iadd
\end{lstlisting}
Give the arithmetic expression that produced this code. Make sure
you give all necessary parentheses.
\solution{
$1 + ((2 * 3) + (4 - 3))$
}
\item Describe what the following JVM instructions do!
\begin{lstlisting}[language=JVMIS2,numbers=none]
ldc 3
iload 3
istore 1
ifeq label
if_icmpge label
\end{lstlisting}
\solution{
(1) load the constant 3 onto the stack. (2) load the 4th local variable onto the stack. (3) store the top of the stack into the 2nd local variable (deleting the top element from the stack) (4) tests whether the top of the stack is equal to zero (if yes, then jump to label; delete top of the stack) (5) compares the top 2 elements of the stack whether they are greater or equal (if yes jump to label; delete two topmost elements from the stack)
}
\item What does the following JVM function calculate?
\begin{lstlisting}[language=JVMIS2,numbers=none]
.method public static bar(I)I
.limit locals 1
.limit stack 9
iload 0
ldc 0
if_icmpne If_else_8
ldc 0
goto If_end_9
If_else_8:
iload 0
ldc 1
if_icmpne If_else_10
ldc 1
goto If_end_11
If_else_10:
iload 0
ldc 1
isub
invokestatic bar(I)I
iload 0
ldc 2
isub
invokestatic bar(I)I
iadd
If_end_11:
If_end_9:
ireturn
.end method
\end{lstlisting}
\solution{ Fibonacci function..students should be able to read what the instructions do on the stack).}
\item What does the following LLVM function calculate? Give the
corresponding arithmetic expression .
\begin{lstlisting}[language=LLVM,numbers=none]
define i32 @foo(i32 %a, i32 %b) {
%1 = mul i32 %a, %a
%2 = mul i32 %a, 2
%3 = mul i32 %2, %b
%4 = add i32 %1, %3
%5 = mul i32 %b, %b
%6 = add i32 %5, %4
ret i32 %6
}
\end{lstlisting}
\solution{ $a^2+a*2*b + b^2$
}
\item As an optimisation technique, a compiler might want to detect
`dead code' and not generate anything for this code. Why does this
optimisation technique have the potential of speeding up the
run-time of a program? (Hint: On what kind of CPUs are programs run
nowadays?)
\solution{ Modern CPUs use predictive branching (guessing which
code-branch is run) and use the cache extensively...any code that
isn't in the program helps with guessing the right branch and does
not occupy anything in the cache. So in effect the code will run
faster. }
\item In an earlier question, we analysed the advantages of having a lexer-phase
before running the parser (having a lexer is definitely a good thing to have). But you
might wonder if a lexer can also be implemented by a parser and some simple
grammar rules. Consider for example:
\begin{plstx}[margin=1cm]
: \meta{S\/} ::= (\meta{Kw\/}\mid \meta{Id\/}\mid \meta{Ws\/}) \cdot \meta{S\/} \;\mid\; \epsilon\\
: \meta{Kw\/} ::= \texttt{if} \mid \texttt{then} \mid \ldots\\
: \meta{Id\/} ::= (\texttt{a} \mid\ldots\mid \texttt{z}) \cdot \meta{Id\/} \;\mid\; \epsilon\\
: \meta{Ws\/} ::= \ldots\\
\end{plstx}
What is wrong with implementing a lexer in this way?
\solution { There is no problem in terms of which strings are
matched (the grammar can be defined such that it matches exactly
the same strings. However, CFG do not obey the POSIX rules,
meaning they cannot implement ``how regular expressions matc a
string'' (for example longest match rule; rule priority). }
\item What is the difference between a parse tree and an abstract
syntax tree? Give some simple examples for each of them.
\solution { Parse-trees follow the grammar rules, therefore the
inner nodes correspond to the non-terminal symbols in CFGs. ASTs
represent the tree-structure of the programs. }
\item What are the two main features of code in
static single assignment form (SSA)?
\solution{
Variables are only assigned once and all operations are
primitive (in the sense of simple arithmetic operations,
function calls and so on).
}
%\item Give a description of how the Brzozowski matcher works.
% The description should be coherent and logical.
%\item Give a description of how a compiler for the While-language can
% be implemented. You should assume you are producing code for the JVM.
% The description should be coherent and logical.
\item \POSTSCRIPT
% \item It is true (I confirmed it) that
%
% \begin{center} if $\varnothing$ does not occur in $r$
% \;\;then\;\;$L(r) \not= \{\}$
% \end{center}
%
% \noindent
% holds, or equivalently
%
% \begin{center}
% $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$.
% \end{center}
%
% \noindent
% You can prove either version by induction on $r$. The best way to
% make more formal what is meant by `$\varnothing$ occurs in $r$', you can define
% the following function:
%
% \begin{center}
% \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
% $occurs(\varnothing)$ & $\dn$ & $true$\\
% $occurs(\epsilon)$ & $\dn$ & $f\!alse$\\
% $occurs (c)$ & $\dn$ & $f\!alse$\\
% $occurs (r_1 + r_2)$ & $\dn$ & $occurs(r_1) \vee occurs(r_2)$\\
% $occurs (r_1 \cdot r_2)$ & $\dn$ & $occurs(r_1) \vee occurs(r_2)$\\
% $occurs (r^*)$ & $\dn$ & $occurs(r)$ \\
% \end{tabular}
% \end{center}
%
% \noindent
% Now you can prove
%
% \begin{center}
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
% \end{center}
%
% \noindent
% The interesting cases are $r_1 + r_2$ and $r^*$.
% The other direction is not true, that is if $occurs(r)$ then $L(r) = \{\}$. A counter example
% is $\varnothing + a$: although $\varnothing$ occurs in this regular expression, the corresponding
% language is not empty. The obvious extension to include the not-regular expression, $\sim r$,
% also leads to an incorrect statement. Suppose we add the clause
%
% \begin{center}
% \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
% $occurs(\sim r)$ & $\dn$ & $occurs(r)$
% \end{tabular}
% \end{center}
%
% \noindent
% to the definition above, then it will not be true that
%
% \begin{center}
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
% \end{center}
%
% \noindent
% Assume the alphabet contains just $a$ and $b$, find a counter example to this
% property.
\end{enumerate}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: