coursework/cw04.tex
changeset 390 a1fe591a3df3
parent 373 b018234c9126
child 391 f352cb238c32
equal deleted inserted replaced
389:71c405056d3a 390:a1fe591a3df3
    10 \section*{Coursework 4 (Strand 1)}
    10 \section*{Coursework 4 (Strand 1)}
    11 
    11 
    12 \noindent This coursework is worth 10\% and is due on 11
    12 \noindent This coursework is worth 10\% and is due on 11
    13 December at 16:00. You are asked to implement a compiler for
    13 December at 16:00. You are asked to implement a compiler for
    14 the WHILE language that targets the assembler language
    14 the WHILE language that targets the assembler language
    15 provided by Jasmin or Krakatau. You can do the implementation
    15 provided by Jasmin or Krakatau (both have very similar
    16 in any programming language you like, but you need to submit
    16 syntax). You can do the implementation in any programming
    17 the source code with which you answered the questions,
    17 language you like, but you need to submit the source code with
    18 otherwise a mark of 0\% will be awarded. You should use the
    18 which you answered the questions, otherwise a mark of 0\% will
    19 lexer and parser from the previous courseworks. 
    19 be awarded. You should use the lexer and parser from the
       
    20 previous courseworks. 
    20 
    21 
    21 \subsection*{Disclaimer}
    22 \subsection*{Disclaimer}
    22 
    23 
    23 It should be understood that the work you submit represents
    24 It should be understood that the work you submit represents
    24 your own effort. You have not copied from anyone else. An
    25 your own effort. You have not copied from anyone else. An
    25 exception is the Scala code I showed during the lectures,
    26 exception is the Scala code I showed during the lectures,
    26 which you can use. You can also use your own code from the
    27 which you can use. You can also use your own code from the
    27 CW~1, CW~2 and CW~3.
    28 CW~1, CW~2 and CW~3.
    28 
    29 
    29 
    30 
    30 \subsection*{Assemblers}
    31 \subsection*{Jasmin Assembler}
    31 
    32 
    32 The Jasmin assembler is available from
    33 The Jasmin assembler is available from
    33 
    34 
    34 \begin{center}
    35 \begin{center}
    35 \url{http://jasmin.sourceforge.net}
    36 \url{http://jasmin.sourceforge.net}
    40 
    41 
    41 \begin{center}
    42 \begin{center}
    42 \url{http://jasmin.sourceforge.net/guide.html}
    43 \url{http://jasmin.sourceforge.net/guide.html}
    43 \end{center}
    44 \end{center}
    44 
    45 
    45 \noindent
    46 \noindent and also a description of some of the instructions
    46 and also a description of some of the instructions that the JVM understands
    47 that the JVM understands
    47 
    48 
    48 \begin{center}
    49 \begin{center}
    49 \url{http://jasmin.sourceforge.net/instructions.html}
    50 \url{http://jasmin.sourceforge.net/instructions.html}
    50 \end{center}
    51 \end{center}
    51 
    52 
    52 \noindent
    53 \noindent If you generated a correct assembler file for
    53 If you generated a correct assembler file for Jasmin, for example
    54 Jasmin, for example \texttt{loops.j}, you can use
    54 \texttt{loops.j}, you can use
       
    55 
    55 
    56 \begin{center}
    56 \begin{center}
    57 \texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
    57 \texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
    58 \end{center}
    58 \end{center}
    59 
    59 
    60 \noindent
    60 \noindent in order to translate it into Java Byte Code. The
    61 in order to translate it to Java byte code. The resulting class file can be
    61 resulting class file can be run with
    62 run with
       
    63 
    62 
    64 \begin{center}
    63 \begin{center}
    65 \texttt{java loops}
    64 \texttt{java loops}
    66 \end{center}
    65 \end{center}
    67 
    66 
    68 \noindent
    67 \noindent where you might need to give the correct path to the
    69 where you might need to give the correct path to the class file. 
    68 class file. For example:
    70 For example:
       
    71 
    69 
    72 \begin{center}
    70 \begin{center}
    73 \texttt{java -cp . loops/loops}
    71 \texttt{java -cp . loops/loops}
    74 \end{center}
    72 \end{center}
    75 
    73 
    76 \noindent 
    74 \noindent There are also other resources about Jasmin on the
    77 There are also other resources about Jasmin on the Internet, for example
    75 Internet, for example
    78 
    76 
    79 \begin{center}
    77 \begin{center}
    80 \url{http://www.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html}
    78 \small\url{http://www.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html}
    81 
    79 \end{center}
    82 and
    80 
    83 
    81 \noindent and
    84 \url{http://www.csc.villanova.edu/~tway/courses/csc8505/s2011/handouts/JVM%20and%20Jasmin.pdf}
    82 
    85 \end{center}
    83 \begin{center}
    86 
    84 \small\url{http://www.csc.villanova.edu/~tway/courses/csc8505/s2011/handouts/JVM%20and%20Jasmin.pdf}
    87 \noindent
    85 \end{center}
    88 You need to submit a document containing the answers for the two questions 
    86 
    89 below. You can do the implementation in any programming language you like, but you need 
    87 \subsection*{Krakatau Assembler}
    90 to submit the source code with which you answered the questions. Otherwise
    88 
    91 the submission will not be counted.  However, the coursework 
    89 The Krakatau assembler is available from
    92 will \emph{only} be judged according to the answers. You can submit your answers
    90 
    93 in a txt-file or as pdf.\bigskip
    91 \begin{center}
       
    92 \url{https://github.com/Storyyeller/Krakatau}
       
    93 \end{center}
       
    94 
       
    95 \noindent This assembler requires Python and a package called
       
    96 \pcode{ply} available from
       
    97 
       
    98 \begin{center}
       
    99 \url{https://pypi.python.org/pypi/ply}
       
   100 \end{center}
       
   101 
       
   102 \noindent This assembler is largely compatible with the Jasmin
       
   103 syntax---that means for the files we look are concerned with,
       
   104 it understands the same input syntax (no changes to your
       
   105 compiler need to be made). You can generate Java Byte Code by
       
   106 using 
       
   107 
       
   108 \begin{center}
       
   109 \texttt{python Krakatau-master/assemble.py loops.j}
       
   110 \end{center}
       
   111 
       
   112 \noindent where you may have to adapt the directory where
       
   113 Krakatau is installed (I just downloaded the zip file from
       
   114 Github and \pcode{Krakatau-master} was the directory where it
       
   115 was installed).
       
   116 
       
   117 
       
   118 %\noindent You need to submit a document containing the answers
       
   119 %for the two questions below. You can do the implementation in
       
   120 %any programming language you like, but you need to submit the
       
   121 %source code with which you answered the questions. Otherwise
       
   122 %the submission will not be counted. However, the coursework
       
   123 %will \emph{only} be judged according to the answers. You can
       
   124 %submit your answers in a txt-file or as pdf.\bigskip
    94 
   125 
    95 
   126 
    96 \subsection*{Question 1 (marked with 5\%)}
   127 \subsection*{Question 1 (marked with 5\%)}
    97 
   128 
    98 You need to lex and parse WHILE programs, and then generate Java Byte
   129 You need to lex and parse WHILE programs, and then generate
    99 Code instructions for the Jasmin assembler. As solution you need to
   130 Java Byte Code instructions for the Jasmin assembler (or
   100 submit the assembler instructions for the Fibonacci and Factorial
   131 Krakatau assembler). As solution you need to submit the
   101 programs. Both should be so modified that a user can input on the
   132 assembler instructions for the Fibonacci and Factorial
   102 console which Fibonacci number and which Factorial should
   133 programs. Both should be so modified that a user can input on
   103 calculated. The Fibonacci program is given in Figure~\ref{fibs}. You
   134 the console which Fibonacci number and which Factorial should
   104 can write your own program for calculating factorials.
   135 be calculated. The Fibonacci program is given in
       
   136 Figure~\ref{fibs}. You can write your own program for
       
   137 calculating factorials.
   105 
   138 
   106 \begin{figure}[t]
   139 \begin{figure}[t]
   107 \lstinputlisting[language=while]{../progs/fib.while}
   140 \lstinputlisting[language=while]{../progs/fib.while}
   108 \caption{The Fibonacci program in the WHILE language.\label{fibs}}
   141 \caption{The Fibonacci program in the WHILE language.\label{fibs}}
   109 \end{figure}
   142 \end{figure}
   110 
   143 
   111 \subsection*{Question 2 (marked with 4\%)}
   144 \subsection*{Question 2 (marked with 4\%)}
   112 
   145 
   113 Extend the syntax of you language so that it contains also \texttt{for}-loops, like
   146 Extend the syntax of your language so that it contains also
       
   147 \texttt{for}-loops, like
   114 
   148 
   115 \begin{center}
   149 \begin{center}
   116 \texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
   150 \texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
   117 \end{center}
   151 \end{center}
   118 
   152 
   119 \noindent
   153 \noindent The intended meaning is to first assign the variable
   120 The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic 
   154 \textit{Id} the value of the first arithmetic expression, then
   121 expression, then go through the loop, at the end increase the value of the variable by 1, 
   155 go through the loop, at the end increase the value of the
   122 and finally test wether the value is not less or equal to the value of the second
   156 variable by 1, and finally test wether the value is not less
   123 arithmetic expression. For example the following instance of a \texttt{for}-loop 
   157 or equal to the value of the second arithmetic expression (in
   124 is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
   158 case it is greater, you should leave the loop). For example
   125 
   159 the following instance of a \texttt{for}-loop is supposed to
   126 
   160 print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
   127 \begin{center}
   161 
   128 \begin{minipage}{6cm}
   162 
   129 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
   163 \begin{center}
       
   164 \begin{minipage}{12cm}
       
   165 \begin{lstlisting}[language=While, numbers=none]
   130 for i := 2 upto 4 do {
   166 for i := 2 upto 4 do {
   131     write i	
   167     write i	
   132 }
   168 }
   133 \end{lstlisting}
   169 \end{lstlisting}
   134 \end{minipage}
   170 \end{minipage}
   135 \end{center}
   171 \end{center}
   136 
   172 
   137 \noindent
   173 \noindent There are two ways how this can be implemented: one
   138 There are two ways how this can be implemented: one is to adapt the code generation 
   174 is to adapt the code generation part of the compiler and
   139 part of the compiler and generate specific code for \texttt{for}-loops; the other is to
   175 generate specific code for \texttt{for}-loops; the other is to
   140 translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using
   176 translate the abstract syntax tree of \texttt{for}-loops into
   141 existing language constructs. For example the loop above could be translated
   177 an abstract syntax tree using existing language constructs.
   142 to the following \texttt{while}-loop:
   178 For example the loop above could be translated to the
   143 
   179 following \texttt{while}-loop:
   144 \begin{center}
   180 
   145 \begin{minipage}{6cm}
   181 \begin{center}
   146 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
   182 \begin{minipage}{12cm}
       
   183 \begin{lstlisting}[language=While, numbers=none]
   147 i := 2;
   184 i := 2;
   148 while (i <= 4) do {
   185 while (i <= 4) do {
   149     write i;
   186     write i;
   150     i := i + 1;
   187     i := i + 1;
   151 }
   188 }
   153 \end{minipage}
   190 \end{minipage}
   154 \end{center}
   191 \end{center}
   155 
   192 
   156 \subsection*{Question 3 (marked with 1\%)}
   193 \subsection*{Question 3 (marked with 1\%)}
   157 
   194 
   158 \noindent
   195 \noindent In this question you are supposed to give the
   159 In this question you are supposed to give the assembler instructions for the
   196 assembler instructions for the program
   160 program
   197 
   161 
   198 \begin{center}
   162 \begin{center}
   199 \begin{minipage}{12cm}
   163 \begin{minipage}{6cm}
   200 \begin{lstlisting}[language=While, numbers=none]
   164 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
       
   165 for i := 1 upto 10 do {
   201 for i := 1 upto 10 do {
   166   for i := 1 upto 10 do {
   202   for i := 1 upto 10 do {
   167     write i
   203     write i
   168   }
   204   }
   169 } 
   205 } 
   172 \end{center}
   208 \end{center}
   173 
   209 
   174 \noindent 
   210 \noindent 
   175 Note that in this program the variable \pcode{i} is used
   211 Note that in this program the variable \pcode{i} is used
   176 twice. You need to make a decision how it should be compiled?
   212 twice. You need to make a decision how it should be compiled?
   177 Explain you decision and indicate what this program would
   213 Explain your decision and indicate what this program would
   178 print out.
   214 print out.
   179 
   215 
   180 \subsection*{Further Information}
   216 \subsection*{Further Information}
   181 
   217 
   182 The Java infrastructure unfortunately does not contain an assembler out-of-the-box
   218 The Java infrastructure unfortunately does not contain an
   183 (therefore
   219 assembler out-of-the-box (therefore you need to download the
   184 you need to download the additional package Jasmin---see above). But it does contain a 
   220 additional package Jasmin or Krakatau---see above). But it
   185 disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
   221 does contain a disassembler, called \texttt{javap}. A
   186 generates readable assembler code from Java byte code. Have a look at the
   222 dissembler does the ``opposite'' of an assembler: it generates
   187 following example: Compile using the usual Java compiler the simple Hello World 
   223 readable assembler code from Java Byte Code. Have a look at
   188 program below:
   224 the following example: Compile using the usual Java compiler
   189 
   225 the simple Hello World program below:
   190 \begin{center}
   226 
   191 \begin{minipage}{10cm}
   227 \begin{center}
       
   228 \begin{minipage}{12cm}
   192 \begin{lstlisting}[language=Java,numbers=none]
   229 \begin{lstlisting}[language=Java,numbers=none]
   193 class HelloWorld {
   230 class HelloWorld {
   194     public static void main(String[] args) {
   231     public static void main(String[] args) {
   195         System.out.println("Hello World!");
   232         System.out.println("Hello World!");
   196     }
   233     }
   201 
   238 
   202 \noindent
   239 \noindent
   203 You can use the command
   240 You can use the command
   204 
   241 
   205 \begin{center}
   242 \begin{center}
   206 \texttt{javap -v HelloWorld}
   243 \begin{minipage}{12cm}
   207 \end{center}
   244 \begin{lstlisting}[language={},numbers=none]
   208 
   245 javap -v HelloWorld
   209 \noindent to see the assembler instructions of the Java byte
   246 \end{lstlisting}
   210 code that has been generated for this program. You can compare
   247 \end{minipage}
       
   248 \end{center}
       
   249 
       
   250 \noindent to see the assembler instructions of the Java Byte
       
   251 Code that has been generated for this program. You can compare
   211 this with the code generated for the Scala version of Hello
   252 this with the code generated for the Scala version of Hello
   212 World.
   253 World.
   213 
   254 
   214 \begin{center}
   255 \begin{center}
   215 \begin{minipage}{10cm}
   256 \begin{minipage}{12cm}
   216 \begin{lstlisting}[language=Scala,numbers=none]
   257 \begin{lstlisting}[language=Scala,numbers=none]
   217 object HelloWorld {
   258 object HelloWorld {
   218    def main(args: Array[String]) {
   259    def main(args: Array[String]) {
   219       println("Hello World!")
   260       println("Hello World!")
   220   }
   261   }
   247 .end method
   288 .end method
   248 \end{lstlisting}
   289 \end{lstlisting}
   249 \end{minipage}
   290 \end{minipage}
   250 \end{center}
   291 \end{center}
   251 
   292 
   252 \noindent 
   293 \noindent This function will invoke Java's \texttt{println}
   253 This function will invoke Java's \texttt{println} function for integers. Then if you need
   294 function for integers. Then if you need to generate code for
   254 to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate
   295 \texttt{write x} where \texttt{x} is an integer variable, you
   255 
   296 can generate
   256 \begin{center}
   297 
   257 \begin{minipage}{8cm}
   298 \begin{center}
       
   299 \begin{minipage}{12cm}
   258 \begin{lstlisting}[language=JVMIS, numbers=none]
   300 \begin{lstlisting}[language=JVMIS, numbers=none]
   259 iload n 
   301 iload n 
   260 invokestatic XXX/XXX/write(I)V
   302 invokestatic XXX/XXX/write(I)V
   261 \end{lstlisting}
   303 \end{lstlisting}
   262 \end{minipage}
   304 \end{minipage}
   263 \end{center}
   305 \end{center}
   264 
   306 
   265 \noindent
   307 \noindent where \texttt{n} is the index where the value of the
   266 where \texttt{n} is the index where the value of the variable \texttt{x} is
   308 variable \texttt{x} is stored. The \texttt{XXX/XXX} needs to
   267 stored. The \texttt{XXX/XXX} needs to be replaced with the class name 
   309 be replaced with the class name which you use to generate the
   268 which you use to generate the code (for example \texttt{fib/fib} in case
   310 code (for example \texttt{fib/fib} in case of the Fibonacci
   269 of the Fibonacci numbers).
   311 numbers).
   270 
   312 
   271 Writing out a string is similar. The corresponding library function uses strings 
   313 Writing out a string is similar. The corresponding library
   272 instead of integers:
   314 function uses strings instead of integers:
   273 
   315 
   274 \begin{center}
   316 \begin{center}
   275 \begin{minipage}{12cm}
   317 \begin{minipage}{12cm}
   276 \begin{lstlisting}[language=JVMIS, numbers=none]
   318 \begin{lstlisting}[language=JVMIS, numbers=none]
   277 .method public static writes(Ljava/lang/String;)V
   319 .method public static writes(Ljava/lang/String;)V
   278    .limit stack 2
   320    .limit stack 2
   279    .limit locals 2
   321    .limit locals 1
   280    getstatic java/lang/System/out Ljava/io/PrintStream;
   322    getstatic java/lang/System/out Ljava/io/PrintStream;
   281    aload 0
   323    aload 0
   282    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
   324    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
   283    return
   325    return
   284 .end method
   326 .end method
   285 \end{lstlisting}
   327 \end{lstlisting}
   286 \end{minipage}
   328 \end{minipage}
   287 \end{center}
   329 \end{center}
   288 
   330 
   289 \noindent
   331 \noindent The code that needs to be generated for \code{write
   290 The code that needs to be generated for \code{write "some_string"} commands 
   332 "some_string"} commands is
   291 is
   333 
   292 
   334 \begin{center}
   293 \begin{center}
   335 \begin{minipage}{12cm}
   294 \begin{minipage}{8cm}
       
   295 \begin{lstlisting}[language=JVMIS,numbers=none]
   336 \begin{lstlisting}[language=JVMIS,numbers=none]
   296 ldc "some_string"
   337 ldc "some_string"
   297 invokestatic XXX/XXX/writes(Ljava/lang/String;)V
   338 invokestatic XXX/XXX/writes(Ljava/lang/String;)V
   298 \end{lstlisting}
   339 \end{lstlisting}
   299 \end{minipage}
   340 \end{minipage}
   300 \end{center}
   341 \end{center}
   301 
   342 
   302 \noindent
   343 \noindent Again you need to adjust the \texttt{XXX/XXX} part
   303 Again you need to adjust the \texttt{XXX/XXX} part in each call.
   344 in each call.
   304 
   345 
   305 The code for \texttt{read} is more complicated. The reason is that inputting a string
   346 The code for \texttt{read} is more complicated. The reason is
   306 will need to be transformed into an integer. The code in Figure~\ref{read} does this.
   347 that inputting a string will need to be transformed into an
   307 It can be called with
   348 integer. The code in Figure~\ref{read} does this. It can be
   308 
   349 called with
   309 \begin{center}
   350 
   310 \begin{minipage}{8cm}
   351 \begin{center}
       
   352 \begin{minipage}{12cm}
   311 \begin{lstlisting}[language=JVMIS,numbers=none]
   353 \begin{lstlisting}[language=JVMIS,numbers=none]
   312 invokestatic XXX/XXX/read()I 
   354 invokestatic XXX/XXX/read()I 
   313 istore n
   355 istore n
   314 \end{lstlisting}
   356 \end{lstlisting}
   315 \end{minipage}
   357 \end{minipage}