Re: Java Compilers and Tail Recursion

Mark Tillotson <>
29 Dec 1997 21:04:17 -0500

          From comp.compilers

Related articles
Java Compilers and Tail Recursion (Jason Karney) (1997-12-23)
Re: Java Compilers and Tail Recursion (Mark Tillotson) (1997-12-29)
Re: Java Compilers and Tail Recursion (Shri Borde) (1998-01-03)
Re: Java Compilers and Tail Recursion (David Chase) (1998-01-03)
| List of all articles for this month |

From: Mark Tillotson <>
Date: 29 Dec 1997 21:04:17 -0500
Organization: Harlequin Ltd, Longstanton House, Cambridge UK
References: 97-12-163
Keywords: Java, optimize

Jason Karney <> asked:
| Is the current crop of Java compilers smart enough to optimize tail
| recursion? I am specifically wondering about Symantec and Sun's JDK
| compilers.

Tail-recursion elimination is prohibited by the implementation of the
Java SecurityMangler. See definitions of getClassContext(),
classDepth(), inClass(), classLoaderDepth() etc. (section 20.17.* in
the language spec)

So the answer is you can't do it _and_ support the ClassLoader/
SecurityManager protocol. This is (in my opinion) because that
protocol is an inelegant hack.

It would not be at all difficult to do the elimination, it doesn't
even need a compiler to do it either, an interpreter can just check
for a suitable return bytecode after an invoke opcode (and you have to
check for being in a synchronized method too). It's a simple peephole
optimization, because the method-invocation and return byte codes are
at a high-enough level.

[ | | +44(0)1954 785433 ]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.