Tail Recursion (Why it appears that Gnat 3.15p does support it)

From: Chad R. Meiners (chad.rmeiners_at_gmail.com)
Date: 01/13/05


Date: 13 Jan 2005 14:04:47 -0800

I have noticed that Gnat 3.15p does not optimize the tail recursion in
the following program.

with Ada.Text_IO;
use Ada.Text_IO;

procedure Tail is

type Int is mod 2**64;

procedure Fib(First, Second : Int) is
Next : constant Int := First + Second;
begin
Put(Int'Image(Next) & ", ");
Fib(Second,Next);
end Fib;

begin
Put("1, 1, ");
Fib(1,1);
end Tail;

Does anyone know why GNAT (or any other compiler) does not always reuse
stack frames for all subroutines that appear right before a return? Is
there some requirement that prevents such naive optimizations?
Incidently, if anyone knows of a compiler that does optimize the tail
recursion for this program please let me know.
Thank you,
Chad R. Meiners



Relevant Pages