handouts/ho07.tex
changeset 691 991849dfbcb1
parent 690 8d57433c7b5e
child 692 8c7ccdebcb89
equal deleted inserted replaced
690:8d57433c7b5e 691:991849dfbcb1
   682 can be run by the {\tt{}java}-program.\label{test}}
   682 can be run by the {\tt{}java}-program.\label{test}}
   683 \end{figure}
   683 \end{figure}
   684 
   684 
   685 \subsection*{Arrays}
   685 \subsection*{Arrays}
   686 
   686 
   687 Maybe a useful addition to the While-language are arrays. This 
   687 Maybe a useful addition to the While-language would be arrays. This
   688 lets us generate more interesting While-programs by translating
   688 would let us generate more interesting While-programs by translating
   689 BF*** programs into equivalent While-programs. This means we 
   689 BF*** programs into equivalent While-code. So in this section lets have
   690 want to support the following three constructions
   690 a look at how we can support the following three constructions
   691 
   691 
   692 \begin{lstlisting}[mathescape,language=While]
   692 \begin{lstlisting}[mathescape,language=While]
   693 new arr[15000]
   693 new arr[15000]
   694 x := 3 + arr[3 + y]
   694 x := 3 + arr[3 + y]
   695 arr[42 * n] := ...
   695 arr[42 * n] := ...
   696 \end{lstlisting}
   696 \end{lstlisting}
   697 
   697 
   698 \noindent
   698 \noindent
   699 The first one creates a new array with name \pcode{arr} that can 
   699 The first construct is for creating new arrays, in this instance the
   700 hold a given number of integers. The second is referencing an array
   700 name of the array is \pcode{arr} and it can hold 15000 integers. The
   701 inside a arithmetic expression. Essentially we have to be able to 
   701 second is for referencing an array cell inside an arithmetic
   702 look up the contents of an array at an index. Similarly we need to be
   702 expression---we need to be able to look up the contents of an array at
   703 able to update the content of an array (3rd line). The latter two
   703 an index determined by an arithmetic expression. Similarly, we need to
   704 constructions state that the index to an array can be given by 
   704 be able to update the content of an array at an calculated index---the
   705 an arithmetic expression. For creating a new array we need to generate
   705 3rd feature.  
   706 the following JVM instructions:
   706 
       
   707 For creating a new array we can generate the following three JVM
       
   708 instructions:
   707 
   709 
   708 \begin{lstlisting}[mathescape,language=JVMIS]
   710 \begin{lstlisting}[mathescape,language=JVMIS]
   709 ldc number 
   711 ldc number 
   710 newarray int 
   712 newarray int 
   711 astore loc_var
   713 astore loc_var
   712 \end{lstlisting}
   714 \end{lstlisting}
   713 
   715 
   714 \noindent
   716 \noindent
   715 First we need to put the dimension of the array onto the
   717 First we need to put the dimension of the array onto the stack. The next
   716 stack, then next instruction creates the array and last we
   718 instruction creates the array. With the last we can store the array as a
   717 need to store the array in a local variable (like ``simple'' variables).
   719 local variable (like the ``simple'' variables from the previous
   718 For looking up an element in an array we can use the following code
   720 section). The use of a local variable allows us to have multiple arrays
       
   721 in a While-program. For looking up an element in an array we can use the
       
   722 following JVM code
   719 
   723 
   720 \begin{lstlisting}[mathescape,language=JVMIS]
   724 \begin{lstlisting}[mathescape,language=JVMIS]
   721 aload loc_var 
   725 aload loc_var 
   722 index_aexp 
   726 index_aexp 
   723 iaload
   727 iaload
   724 \end{lstlisting}
   728 \end{lstlisting}
   725 
   729 
   726 \noindent
   730 \noindent
   727 This first loads the ``pointer'' to the array onto the stack. Then we have the
   731 The first instruction loads the ``pointer'' to the array onto the stack.
   728 instructions corresponding to the index where we want to look up the array. The
   732 Then we have some instructions corresponding to the index where we want
   729 idea is that these instructions will leave a concrete number on the stack, which is
   733 to look up the array. The idea is that these instructions will leave a
   730 the index. Finally we just need to load the corresponding element onto the stack.
   734 concrete number on the stack, which will be the index into the array we
       
   735 need. Finally we need to tell the JVM to load the corresponding element
       
   736 onto the stack. Updating an array at an index with a value is as
       
   737 follows.
       
   738 
       
   739 \begin{lstlisting}[mathescape,language=JVMIS]
       
   740 aload loc_var 
       
   741 index_aexp 
       
   742 value_aexp 
       
   743 iastore
       
   744 \end{lstlisting}
       
   745 
       
   746 \noindent
       
   747 Again the first instruction loads the ``pointer'' to the array onto the
       
   748 stack. Then we have some instructions corresponding to the index where
       
   749 we want to update the array. After that come the instructions for with what
       
   750 value we want to update the array. Finally the instruction for 
       
   751 updating the array.
       
   752 
       
   753 Next we need to update our grammar rules: it seems best to extend the 
       
   754 rule for factors in arithmetic expressions with a rule for looking up
       
   755 an array.
       
   756 
       
   757 \begin{plstx}[rhs style=, margin=3cm]
       
   758 : \meta{E} ::= \meta{T} $+$ \meta{E}
       
   759          | \meta{T} $-$ \meta{E}
       
   760          | \meta{T}\\
       
   761 : \meta{T} ::= \meta{F} $*$ \meta{T}
       
   762           | \meta{F} $\backslash$ \meta{T}
       
   763           | \meta{F}\\
       
   764 : \meta{F} ::= ( \meta{E} )
       
   765           | $\underbrace{\meta{Id}\,[\,\meta{E}\,]}_{new}$
       
   766           | \meta{Id}
       
   767           | \meta{Num}\\
       
   768 \end{plstx}
       
   769 
       
   770 \noindent
       
   771 There is no problem with left-recursion as the \meta{E} is ``protected''
       
   772 by an identifier and brackets. There are two new rules in statements,
       
   773 namely creation of an array and array assignment:
       
   774 
       
   775 \begin{plstx}[rhs style=, margin=2cm, one per line]
       
   776 : \meta{Stmt} ::=  \ldots
       
   777               | \texttt{new}\; \meta{Id}\,[\,\meta{Num}\,] 
       
   778               | \meta{Id}\,[\,\meta{E}\,]\,:=\,\meta{E}\\
       
   779 \end{plstx}
   731 
   780 
   732 \end{document}
   781 \end{document}
   733 
   782 
   734 %%% Local Variables: 
   783 %%% Local Variables: 
   735 %%% mode: latex  
   784 %%% mode: latex