Re: Proper Tail Recursive C++

Andy Armstrong <andy@wonderworks.co.uk>
27 Feb 1997 00:39:19 -0500

          From comp.compilers

Related articles
Proper Tail Recursive C++ jerpat@iastate.edu (Jerry) (1997-02-20)
Re: Proper Tail Recursive C++ sc@informatik.uni-jena.de (Sebastian Schmidt) (1997-02-22)
Re: Proper Tail Recursive C++ hbaker@netcom.com (1997-02-22)
Re: Proper Tail Recursive C++ will@ccs.neu.edu (William D Clinger) (1997-02-23)
Re: Proper Tail Recursive C++ fjh@mundook.cs.mu.OZ.AU (1997-02-23)
Re: Proper Tail Recursive C++ bothner@cygnus.com (1997-02-23)
Re: Proper Tail Recursive C++ andy@wonderworks.co.uk (Andy Armstrong) (1997-02-27)
Re: Proper Tail Recursive C++ gary@wheel.tiac.net (1997-03-01)
Re: Proper Tail Recursive C++ Wilco.Dijkstra@armltd.co.uk (Wilco Dijkstra) (1997-03-01)
Re: Proper Tail Recursive C++ hbaker@netcom.com (1997-03-05)
Re: Proper Tail Recursive C++ njl@cyberpass.net (1997-03-09)
Re: Proper Tail Recursive C++ hbaker@netcom.com (1997-03-13)
Re: Proper Tail Recursive C++ fjh@murlibobo.cs.mu.OZ.AU (1997-03-13)
[4 later articles]
| List of all articles for this month |

From: Andy Armstrong <andy@wonderworks.co.uk>
Newsgroups: comp.compilers
Date: 27 Feb 1997 00:39:19 -0500
Organization: WonderWorks
References: 97-02-111 97-02-131 97-02-141
Keywords: C++, optimize

William D Clinger <will@ccs.neu.edu> writes
>I think an example would help. If "properly tail recursive" has the
>meaning given to it by the functional programming community (e.g. the
>IEEE/ANSI Scheme standard), then a properly tail recursive C++
>compiler would have to translate the following program into machine
>code that terminates without overflowing the stack, heap, or any other
>storage segment, even on a tiny machine with much less than a megabyte
>of memory.


I've just tried this on Acorn's RISC OS C++ which is based on CFront +
Norcroft ARM C (which is good at optimizing TR).


Because of the "int A[1000]" in f() it fails to TR optimize the call to
g(A), and I can't immediately see how this could be optimised given that
each new invocation of f() needs a new A[]. Perhaps I'm missing
something.


For the record this compiler does generate nice tail call code for
functions which don't use any local storage. Here's what it does.


static void x1(int y)
{
      if (y > 0)
            x2(y-1);
}


static void x2(int y)
{
      if (y > 0)
            x1(y-1);
}


generates the following ARM code:


x1
                CMP a1,#0
                SUBGT a1,a1,#1
                BGT x2
                MOVS pc,lr


x2
                CMP a1,#0
                SUBGT a1,a1,#1
                BGT x1
                MOVS pc,lr


--
http://www.wonderworks.co.uk --> Free RISC OS software
Andy Armstrong, WonderWorks
--


Post a followup to this message

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