comp.lang.ada
 help / color / mirror / Atom feed
From: Jacob Sparre Andersen <sparre@nbi.dk>
Subject: Re: Tail Recursion (Why it appears that Gnat 3.15p does support it)
Date: 14 Jan 2005 10:04:02 +0100
Date: 2005-01-14T08:42:15+00:00	[thread overview]
Message-ID: <rlsk6qgz8jh.fsf@jacob.crs4.it> (raw)
In-Reply-To: 1105653887.848989.268410@z14g2000cwz.googlegroups.com

Chad  R. Meiners wrote:

> 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?

Could it be related to the parameter passing rules in Ada?  And would
it really be faster?

If the arguments for Fib are not copied in the order you have written
them in the procedure specification and a single stack frame is
reused, the program will not behave correctly.  But alternating
between two stack frames should be safe enough.

Jacob
-- 
�By becoming continuous, war has fundamentally changed its character.
 In past ages, a war, almost by definition, was something that sooner
 or later came to an end, usually in unmistakable victory or defeat.�
                               -- Nineteen Eighty-Four, George Orwell
�I don't think you can win [the war on terror].�    -- George W. Bush





  parent reply	other threads:[~2005-01-14  9:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-13 22:04 Tail Recursion (Why it appears that Gnat 3.15p does support it) Chad  R. Meiners
2005-01-14  8:32 ` Duncan Sands
2005-01-14  9:04 ` Jacob Sparre Andersen [this message]
2005-01-14 16:06 ` wojtek
2005-01-15  5:08   ` Brian May
2005-01-18 20:59 ` Chad  R. Meiners
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox