| author | Christian Urban <christian.urban@kcl.ac.uk> |
| Fri, 13 Oct 2023 15:07:37 +0100 | |
| changeset 941 | 66adcae6c762 |
| parent 916 | 10f834eb0a9e |
| child 956 | ae9782e62bdd |
| permissions | -rw-r--r-- |
| 59 | 1 |
\documentclass{article}
|
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
2 |
\usepackage{../style}
|
| 876 | 3 |
\usepackage{../langs}
|
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
4 |
\usepackage{../graphics}
|
| 59 | 5 |
|
6 |
\begin{document}
|
|
7 |
||
|
75
898c25a4e399
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
60
diff
changeset
|
8 |
\section*{Homework 8}
|
| 59 | 9 |
|
| 916 | 10 |
%%\HEADER |
|
359
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
11 |
|
| 59 | 12 |
\begin{enumerate}
|
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
13 |
\item Write a program in the WHILE-language that calculates |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
14 |
the factorial function. |
|
77
49c0beef79a1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
15 |
|
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
16 |
\item What optimisations could a compiler perform when |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
17 |
compiling a WHILE-program? |
|
206
85b961f1eee9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
18 |
|
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
19 |
\item What is the main difference between the Java assembler |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
20 |
(as processed by Jasmin) and Java Byte Code? |
|
206
85b961f1eee9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
21 |
|
| 941 | 22 |
\item Remember symbolic labels in the Jasmin-assembler are meant to |
23 |
be used for jumps (like in loops or if-conditions). Assume |
|
24 |
you generated a Jasmin-file with some redundant |
|
25 |
labels, that is some labels are not used in your code for any jumps. For |
|
26 |
example \texttt{L\_begin} and \texttt{L\_end} are not used
|
|
27 |
in the fol,lowing code-snippet: |
|
28 |
||
29 |
\begin{lstlisting}[language=JVMIS,numbers=none]
|
|
30 |
L_begin: |
|
31 |
ldc 1 |
|
32 |
ldc 2 |
|
33 |
ldc 3 |
|
34 |
imul |
|
35 |
ldc 4 |
|
36 |
ldc 3 |
|
37 |
isub |
|
38 |
iadd |
|
39 |
iadd |
|
40 |
L_end: |
|
41 |
\end{lstlisting}
|
|
42 |
||
43 |
Do these redundant labels affect the size of the generated |
|
44 |
JVM-code? (Hint: What are the labels translated to by |
|
45 |
the Jasmin-assembler?). |
|
46 |
||
47 |
\solution{The answer is no. The reason is that assemblers
|
|
48 |
calculate for labels either relative or explicit adresses, |
|
49 |
which are then used in the JVM-byte-code. Relative addresses |
|
50 |
are like ``jump 10 bytes forward'' or ``12 bytes backward''. So |
|
51 |
additional labels do not increase the size of the generated code.} |
|
52 |
||
53 |
||
| 876 | 54 |
\item Consider the following Scala snippet. Are the two functions |
55 |
\texttt{is\_even} and \texttt{is\_odd} tail-recursive?
|
|
56 |
||
57 |
\begin{lstlisting}[numbers=none]
|
|
| 901 | 58 |
def is_even(n: Int) : Boolean = {
|
59 |
if (n == 0) true else is_odd(n - 1) |
|
| 876 | 60 |
} |
61 |
||
| 901 | 62 |
def is_odd(n: Int) : Boolean = {
|
| 876 | 63 |
if (n == 0) false |
| 901 | 64 |
else if (n == 1) true else is_even(n - 1) |
| 876 | 65 |
} |
66 |
\end{lstlisting}
|
|
67 |
||
68 |
Do they cause stack-overflows when compiled to the JVM (for example by Scala)? |
|
69 |
||
| 530 | 70 |
|
71 |
\item Explain what is meant by the terms lazy evaluation and eager |
|
| 577 | 72 |
evaluation. |
73 |
||
74 |
\item \POSTSCRIPT |
|
| 59 | 75 |
\end{enumerate}
|
76 |
||
77 |
\end{document}
|
|
78 |
||
79 |
%%% Local Variables: |
|
80 |
%%% mode: latex |
|
81 |
%%% TeX-master: t |
|
82 |
%%% End: |