equal
deleted
inserted
replaced
115 } |
115 } |
116 |
116 |
117 \item The JVM method given below is code for the following recursive function |
117 \item The JVM method given below is code for the following recursive function |
118 |
118 |
119 \begin{lstlisting}[numbers=none] |
119 \begin{lstlisting}[numbers=none] |
120 def add(x, y) = if x == 0 then y else add(x - 1, y + 1) |
120 def gcd(a, b) = if b == 0 then a else gcd(b, a \% b) |
121 \end{lstlisting} |
121 \end{lstlisting} |
122 |
122 |
123 Rewrite the JVM code such that the recursive call at the end is removed. |
123 Rewrite the JVM code such that the recursive call at the end is replaced by a tailcall to the beginning. |
124 |
124 |
125 \begin{lstlisting}[language=JVMIS,numbers=none] |
125 \begin{lstlisting}[language=JVMIS,numbers=none] |
126 .method public static add(II)I |
126 .method public static gcd(II)I |
127 .limit locals 2 |
127 .limit locals 2 |
128 .limit stack 7 |
128 .limit stack 6 |
129 iload 0 |
129 gcd_Start: |
130 ldc 0 |
130 iload 1 |
131 if_icmpne If_else_4 |
131 ldc 0 |
132 iload 1 |
132 if_icmpne If_else_20 |
133 goto If_end_5 |
133 iload 0 |
134 If_else_4: |
134 goto If_end_21 |
135 iload 0 |
135 If_else_20: |
136 ldc 1 |
136 iload 1 |
137 isub |
137 iload 0 |
138 iload 1 |
138 iload 1 |
139 ldc 1 |
139 irem |
140 iadd |
140 invokestatic defs/defs/gcd(II)I |
141 invokestatic defs/defs/add(II)I ;; recursive call |
141 If_end_21: |
142 If_end_5: |
142 ireturn |
143 ireturn |
|
144 .end method |
143 .end method |
145 \end{lstlisting} |
144 \end{lstlisting} |
146 |
145 |
147 \solution{ |
146 \solution{ |
148 \begin{lstlisting}[language=JVMIS,numbers=none] |
147 \begin{lstlisting}[language=JVMIS,numbers=none] |