On 3 August 2010 04:16, Dan Kersten <dkers...@gmail.com> wrote: > Why can't the clojure bytecode compiler hand-perform this like > functional languages do when compiling to native code?
Because bytecode attempting to manipulate the stack and jump around (unrestricted goto-like) in other ways than through the usual JVM method call mechanisms would not pass verification (the first step of the bytecode loading process on the JVM). > Is it to keep > the clojure compiler fast (for dynamic runtime compilation), since > performing tail call optimisation presumably requires a bunch of extra > checks and more complex code generation? Perhaps this could be done on > AOT compilation? TCO adds no complexity at all when the generated object code handles its own stack, subroutine calling conventions etc. A compiler targeting native code has the option of simply not storing a new return address on the stack when compiling a tail call (if the arguments to the current subrouting where passed in registers; if they were passed in a stack frame, it can simply be popped, with the return address saved and reused for the tail call). On the JVM it is impossible, because the generated object code (JVM bytecode) is not permitted to do this sort of thing. Interestingly, [Erjang][1] (a port of Erlang to the JVM) apparently performs TCO while claiming to stay "reasonably fast". The gimmick involved is apparently a particularly smart implementation of trampolining. Read more about it [here][2]. I have a hunch that this is a no-go for Clojure (partly because Clojure tends to insist on staying close to the platform -- which has significant benefits -- and partly because I'm not sure if this kind of thing wouldn't disagree with Clojure's extremely dynamic nature... I haven't thought this through that well, though, so maybe this is nonsense). At any rate, it is interesting. Sincerely, Michał [1] git://github.com/krestenkrab/erjang [2] http://wiki.github.com/krestenkrab/erjang/how-erjang-compiles-tail-recursion -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en