89 \end{lstlisting} |
89 \end{lstlisting} |
90 |
90 |
91 \noindent The first instruction loads the constant $1$ onto the stack, |
91 \noindent The first instruction loads the constant $1$ onto the stack, |
92 the next one loads $2$, the third instruction adds both numbers together |
92 the next one loads $2$, the third instruction adds both numbers together |
93 replacing the top two elements of the stack with the result $3$. For |
93 replacing the top two elements of the stack with the result $3$. For |
94 simplicity, we will consider throughout only arithmetic involving |
94 simplicity, we will consider only arithmetic operations involving |
95 integer numbers. This means our main JVM instructions for arithmetic |
95 integer numbers. This means our main JVM instructions for arithmetic |
96 will be \instr{iadd}, \instr{isub}, \instr{imul}, \instr{idiv} and so on. |
96 will be \instr{iadd}, \instr{isub}, \instr{imul}, \instr{idiv} and so on. |
97 The \code{i} stands for integer instructions in the JVM (alternatives |
97 The \code{i} stands for integer instructions in the JVM (alternatives |
98 are \code{d} for doubles, \code{l} for longs and \code{f} for floats |
98 are \code{d} for doubles, \code{l} for longs and \code{f} for floats |
99 etc). |
99 etc). |
339 $cs_2$, but always jump to after the last instruction of $cs_2$ |
339 $cs_2$, but always jump to after the last instruction of $cs_2$ |
340 (the code for the \pcode{else}-branch). Note that this jump is |
340 (the code for the \pcode{else}-branch). Note that this jump is |
341 unconditional, meaning we always have to jump to the end of |
341 unconditional, meaning we always have to jump to the end of |
342 $cs_2$. The corresponding instruction of the JVM is |
342 $cs_2$. The corresponding instruction of the JVM is |
343 \instr{goto}. In case $b$ turns out to be false we need the |
343 \instr{goto}. In case $b$ turns out to be false we need the |
344 control-flow |
344 control-flow to be: |
345 |
345 |
346 \begin{center} |
346 \begin{center} |
347 \begin{tikzpicture}[node distance=2mm and 4mm,line cap=round, |
347 \begin{tikzpicture}[node distance=2mm and 4mm,line cap=round, |
348 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm, |
348 block/.style={rectangle, minimum size=1cm, draw=black, line width=1mm, |
349 top color=white,bottom color=black!20}, |
349 top color=white,bottom color=black!20}, |
474 the instructions for the else-branch, followed by the |
474 the instructions for the else-branch, followed by the |
475 after-the-else-branch label. Consider for example the |
475 after-the-else-branch label. Consider for example the |
476 if-statement: |
476 if-statement: |
477 |
477 |
478 \begin{lstlisting}[mathescape,numbers=none,language=While] |
478 \begin{lstlisting}[mathescape,numbers=none,language=While] |
479 if 1 = 1 then x := 2 else y := 3 |
479 if 1 == 1 then x := 2 else y := 3 |
480 \end{lstlisting} |
480 \end{lstlisting} |
481 |
481 |
482 \noindent |
482 \noindent |
483 The generated code is as follows: |
483 The generated code is as follows: |
484 |
484 |
501 \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
501 \draw[->,very thick] (C) edge [->,to path={-- ++(10mm,0mm) |
502 -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); |
502 -- ++(0mm,-17.3mm) |- (\tikztotarget)},line width=1mm] (D.east); |
503 \end{tikzpicture} |
503 \end{tikzpicture} |
504 |
504 |
505 \noindent The first three lines correspond to the the boolean |
505 \noindent The first three lines correspond to the the boolean |
506 expression $1 = 1$. The jump for when this boolean expression |
506 expression \texttt{1 == 1}. The jump for when this boolean expression |
507 is false is in Line~3. Lines 4-6 corresponds to the if-branch; |
507 is false is in Line~3. Lines 4-6 corresponds to the if-branch; |
508 the else-branch is in Lines 8 and 9. |
508 the else-branch is in Lines 8 and 9. |
509 |
509 |
510 Note carefully how the environment $E$ is threaded through the recursive |
510 Note carefully how the environment $E$ is threaded through the recursive |
511 calls of \textit{compile}. The function receives an environment $E$, but |
511 calls of \textit{compile}. The function receives an environment $E$, but |
640 \pcode{write}. It takes a single integer argument indicated by the |
640 \pcode{write}. It takes a single integer argument indicated by the |
641 \pcode{(I)} and returns no result, indicated by the \pcode{V} (for |
641 \pcode{(I)} and returns no result, indicated by the \pcode{V} (for |
642 void). Since the method has only one argument, we only need a single |
642 void). Since the method has only one argument, we only need a single |
643 local variable (Line~2) and a stack with two cells will be sufficient |
643 local variable (Line~2) and a stack with two cells will be sufficient |
644 (Line 3). Line 4 instructs the JVM to get the value of the member |
644 (Line 3). Line 4 instructs the JVM to get the value of the member |
645 \pcode{out} from the class \pcode{java/lang/System}. It expects the value |
645 \pcode{out} from the class \pcode{java/lang/System}. It expects the |
646 to be of type \pcode{java/io/PrintStream}. A reference to this value |
646 value to be of type \pcode{java/io/PrintStream}. A reference to this |
647 will be placed on the stack.\footnote{Note the syntax \texttt{L |
647 value will be placed on the stack.\footnote{Note the syntax |
648 \ldots{};} for the \texttt{PrintStream} type is not an typo. Somehow the |
648 \texttt{Ljava\ldots{};} for the \texttt{PrintStream} type is not an |
649 designers of Jasmin decided that this syntax is pleasing to the eye. So |
649 typo. Somehow the designers of Jasmin decided that this syntax is |
650 if you wanted to have strings in your Jasmin code, you would need to |
650 pleasing to the eye. So if you wanted to have strings in your Jasmin |
651 write \texttt{Ljava/lang/String;}\;. If you want arrays of one |
651 code, you would need to write \texttt{Ljava/lang/String;}\;. If you |
652 dimension, then use \texttt{[\ldots}; two dimensions, use |
652 want arrays of one dimension, then use \texttt{[\ldots}; two |
653 \texttt{[[\ldots} and so on. Looks all very ugly to my eyes.} Line~5 |
653 dimensions, use \texttt{[[\ldots} and so on. Looks all very ugly to |
654 copies the integer we want to print out onto the stack. In the line |
654 my eyes.} Line~5 copies the integer we want to print out onto the |
655 after that we call the method \pcode{println} (from the class |
655 stack. In the line after that we call the method \pcode{println} (from |
656 \pcode{java/io/PrintStream}). We want to print out an integer and do not |
656 the class \pcode{java/io/PrintStream}). We want to print out an |
657 expect anything back (that is why the type annotation is \pcode{(I)V}). |
657 integer and do not expect anything back (that is why the type |
658 The \pcode{return}-instruction in the next line changes the control-flow |
658 annotation is \pcode{(I)V}). The \pcode{return}-instruction in the |
659 back to the place from where \pcode{write} was called. This method needs |
659 next line changes the control-flow back to the place from where |
660 to be part of a header that is included in any code we generate. The |
660 \pcode{write} was called. This method needs to be part of a header |
661 helper-method \pcode{write} can be invoked with the two instructions |
661 that is included in any code we generate. The helper-method |
|
662 \pcode{write} can be invoked with the two instructions |
662 |
663 |
663 \begin{lstlisting}[mathescape,language=JVMIS] |
664 \begin{lstlisting}[mathescape,language=JVMIS] |
664 iload $E(x)$ |
665 iload $E(x)$ |
665 invokestatic XXX/XXX/write(I)V |
666 invokestatic XXX/XXX/write(I)V |
666 \end{lstlisting} |
667 \end{lstlisting} |
743 would allow us to generate more interesting WHILE-programs by |
744 would allow us to generate more interesting WHILE-programs by |
744 translating BF*** programs into equivalent WHILE-code. Therefore in this |
745 translating BF*** programs into equivalent WHILE-code. Therefore in this |
745 section let us have a look at how we can support the following three |
746 section let us have a look at how we can support the following three |
746 constructions |
747 constructions |
747 |
748 |
748 \begin{lstlisting}[mathescape,language=While] |
749 \begin{itemize} |
749 new(arr[15000]) |
750 \item \lstinline|new(arr[15000])| |
750 x := 3 + arr[3 + y] |
751 \item \lstinline|x := 3 + arr[3 + y]| |
751 arr[42 * n] := ... |
752 \item \lstinline|arr[42 * n] := ...| |
752 \end{lstlisting} |
753 \end{itemize} |
753 |
754 |
754 \noindent |
755 \noindent |
755 The first construct is for creating new arrays. In this instance the |
756 The first construct is for creating new arrays. In this instance the |
756 name of the array is \pcode{arr} and it can hold 15000 integers. We do |
757 name of the array is \pcode{arr} and it can hold 15000 integers. We do |
757 not support ``dynamic'' arrays, that is the size of our arrays will |
758 not support ``dynamic'' arrays, that is the size of our arrays will |