comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca>
Subject: Re: Where are returned values stored?
Date: Fri, 28 May 2004 16:33:06 -0400
Date: 2004-05-28T16:33:06-04:00	[thread overview]
Message-ID: <2lNtc.56154$tb4.2140194@news20.bellglobal.com> (raw)
In-Reply-To: <fgjcb0pnsbdcaadc3j365f3ld13m42aaf8@4ax.com>

 > Interesting to consider this with a recursive procedure, but yes, it
 > should work.
 >
 > JAF

Recursive procedures offer no real challenge, except that
if you passed a return value from a deeply nested call,
all the way back down, you could temporarily have
considerable "stack wastage". But there is no reason that
an address comparison couldn't be made and a copy forced
when a certain threshold of waste was reached. OTOH, if the
compiler knows the usage is very short lived, it might
as well just use it where it sits and "release" it after
(especially if no further function/procedure calls are
made, that will need use of the stack).

Calling Conventions:

If this indeed happens (on a given platform), I would think
that the calling convention used is +critical+. For example,
if you forced an Ada routine to use the C calling convention,
I would bet that on most platforms, that all return values
must be _copied_ or placed on the heap (temporarily). At least
this would be true for strings/arrays etc. where the size
was not known in advance.

This is because most platforms (I think), have C
programs pop off their stack frame as part of the
return "process" (I believe this to be true of
Wintel and Linux on Intel for example).

So the compiler might be forced to copy the return
value to some other location, or make some other
arrangement for non Ada calling conventions. You
obviously can't just re-extend the stack frame after
a return because of the possibility of UNIX process
signal calls or interrupt processing that
may overwrite the stack area of interest.

For this reason, I would expect that an Ada calling
convention (when it supports this "return value in
place" concept) probably has the called function Foo,
performing the shrink of its stack frame
as part of the return "processing"; but only enough
to keep the return value protected. The caller then
must finish the job by some prior arrangement known
to the compiler, after the return value is no longer
required.

Just keep in mind that my responses on this thread
are all conceptual/conjecture, and has no documented
factual basis in any existing implementation ;-)

Warren.

James Alan Farrell wrote:
> On Thu, 27 May 2004 13:05:06 -0400, "Warren W. Gay VE3WWG"
> <ve3wwg@cogeco.ca> wrote:
>>Conceptually, there is _no_ reason a compiler _must_ copy a
>>return value. Consider a function like:
>>
>>function Foo return String is
>>  I : Integer := 23;
>>  S : String(1..3) := "Bar";
>>  X : Natural := 0;
>>begin
>>  ...
>>  return S;
>>end;
>>
>>To return S, all the compiler needs to do is to extend the
>>caller's stack frame to claim all the storage up to and
>>including S (I and S assuming sequential assignment), at
>>the point when Foo returns (return effectively does
>>reclaim the storage used for X however).
>>
>>Then the calling code can reference S as if it were a
>>local variable.  The space used for I is
>>wasted at this point, but temporarily, who cares?
>>
>>Once the compiler knows that the caller's use of the returned
>>value S is no longer required, the stack frame can be shrunk
>>back to the original size that it had prior to calling Foo.
>>
>>Is this done? Is it done by GNAT? I have no idea, but I
>>suspect that this is done in some places, some of the time,
>>by some compilers. Perhaps, someone in this group
>>can confirm/debunk this idea.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg




  reply	other threads:[~2004-05-28 20:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-26 19:46 Where are returned values stored? (follow up to yesterday's question) James Alan Farrell
2004-05-26 20:44 ` Simon Wright
2004-05-27  7:51   ` Dmitry A. Kazakov
2004-05-27  9:39 ` Where are returned values stored? Marius Amado Alves
2004-05-27 17:05   ` Warren W. Gay VE3WWG
2004-05-27 20:24     ` James Alan Farrell
2004-05-28 20:33       ` Warren W. Gay VE3WWG [this message]
2004-05-29  7:03         ` Martin Krischik
2004-05-29 13:19           ` Larry Kilgallen
2004-05-30  7:10             ` Martin Krischik
2004-06-02  3:14         ` Robert I. Eachus
2004-05-30 21:17     ` Nick Roberts
2004-05-31 12:58       ` Warren W. Gay VE3WWG
2004-06-02  4:07         ` Robert I. Eachus
2004-06-02 12:42           ` Warren W. Gay VE3WWG
2004-06-03  2:00             ` Nick Roberts
2004-06-03  4:34             ` Robert I. Eachus
2004-06-03 16:06               ` Warren W. Gay VE3WWG
2004-06-03 16:13               ` Nick Roberts
2004-06-07  1:53                 ` Robert I. Eachus
2004-06-07 13:09                   ` Larry Kilgallen
2004-06-09  7:03                     ` Robert I. Eachus
2004-06-05 17:13             ` Simon Wright
2004-05-27 17:11   ` Martin Krischik
2004-05-27 17:07 ` Where are returned values stored? (follow up to yesterday's question) Martin Krischik
replies disabled

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