hws/hw08.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 28 Nov 2023 11:45:48 +0000
changeset 957 34b3aeb65fbe
parent 956 ae9782e62bdd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     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
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
     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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 60
diff changeset
     8
\section*{Homework 8}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
    10
%%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    11
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    14
  the factorial function.
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    15
957
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    16
%\begin{lstlisting}
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    17
%write "factorial: ";
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    18
%read n;
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    19
%minus1 := 1;
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    20
%while n > 0 do {
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    21
%minus1 := minus1 * n;
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    22
%n := n - 1
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    23
%};
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    24
%write "Result: ";
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    25
%write minus1 ;
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    26
%write "\n"
34b3aeb65fbe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    27
%\end{lstlisting}
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    28
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    29
\item What optimisations could a compiler perform when
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    30
  compiling a WHILE-program?
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    31
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    32
  \solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    33
    \begin{itemize}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    34
    \item peephole optimisations (more specific instructions)
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    35
    \item common sub-expression elimination %, for example
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    36
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    37
%\begin{lstlisting}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    38
%x := 3 + a
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    39
%y := 3 + a
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    40
%\end{lstlisting}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    41
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    42
    %can be optimised to
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    43
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    44
    %\begin{lstlisting}[numbers=none,language={}]
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    45
    %  z := 3 + a
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    46
    %  x := z
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    47
    %  y := z
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    48
    %\end{lstlisting}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    49
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    50
    \item constant folding / constant propagation (that is calculate the result of 3 + 4 already during
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    51
      compilation)
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    52
    \item tail-recursion optimisation cannot be applied to the WHILE language
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    53
      because there are no recursive functions
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    54
    \end{itemize}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    55
  }
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    56
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    57
\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
    58
      (as processed by Jasmin) and Java Byte Code?
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    59
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    60
      \solution{ The main difference is that the j-files have symbols
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    61
        for places where to jump, while class files have this resolved
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    62
        to concrete addresses (or relative jumps). That is what the
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    63
        assembler has to generate.  }
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    64
      
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    65
\item Remember symbolic labels in the Jasmin-assembler are meant to
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    66
      be used for jumps (like in loops or if-conditions). Assume
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    67
      you generated a Jasmin-file with some redundant
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    68
      labels, that is some labels are not used in your code for any jumps. For
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    69
      example \texttt{L\_begin} and \texttt{L\_end} are not used
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    70
      in the following code-snippet:
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    71
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    72
\begin{lstlisting}[language=JVMIS,numbers=none]
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    73
L_begin:        
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    74
ldc 1
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    75
ldc 2
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    76
ldc 3
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    77
imul
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    78
ldc 4
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    79
ldc 3
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    80
isub
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    81
iadd
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    82
iadd
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    83
L_end:
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    84
\end{lstlisting}
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    85
      
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    86
      Do these redundant labels affect the size of the generated
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    87
      JVM-code? (Hint: What are the labels translated to by
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    88
      the Jasmin-assembler?).      
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    89
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    90
      \solution{The answer is no. The reason is that assemblers
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    91
        calculate for labels either relative or explicit adresses,
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    92
        which are then used in the JVM-byte-code. Relative addresses
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    93
        are like ``jump 10 bytes forward'' or ``12 bytes backward''. So
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    94
      additional labels do not increase the size of the generated code.}
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    95
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    96
      
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    97
\item Consider the following Scala snippet. Are the two functions
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    98
\texttt{is\_even} and \texttt{is\_odd} tail-recursive?     
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    99
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   100
\begin{lstlisting}[numbers=none]
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
   101
def is_even(n: Int) : Boolean = {
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
   102
    if (n == 0) true else is_odd(n - 1)
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   103
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   104
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
   105
def is_odd(n: Int) : Boolean = {
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   106
    if (n == 0) false 
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
   107
    else if (n == 1) true else is_even(n - 1)
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   108
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   109
\end{lstlisting}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   110
      
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   111
Do they cause stack-overflows when compiled to the JVM (for example by Scala)?
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   112
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   113
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   114
Scala cannot generate jumps in between different methods (to which functions are compiled to). So cannot eliminate the tail-calls. Haskell for example can do this because it compiles the code in a big ``blob'' inside a main-method (similar to the WHILE language).
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   115
}  
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   116
530
cec95ad3a837 updated
cu
parents: 359
diff changeset
   117
cec95ad3a837 updated
cu
parents: 359
diff changeset
   118
\item Explain what is meant by the terms lazy evaluation and eager
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
   119
  evaluation.
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
   120
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   121
  \solution{ Lazy evaluation only evaluates expressions when they are
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   122
    needed and if they are needed twice, the results will be
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   123
    re-used. Eager evaluation immediately evaluates expressions, for
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   124
    example if they are arguments to function calls or allocated to
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   125
    variables.}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   126
    
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   127
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
   128
\item \POSTSCRIPT    
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
%%% End: