comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: Function result
Date: 1997/06/27
Date: 1997-06-27T00:00:00+00:00	[thread overview]
Message-ID: <dewar.867410553@merv> (raw)
In-Reply-To: 5os5r8$4ud@netline.jpl.nasa.gov


Van Snyder says

<<This _works_ but it would be nice if you didn't need to trust the compiler
to optimize away the assignments to Result.

It doesn't seem so bad in Ada, with the explicit copy-out model of returning
function results, because you're only doubling the cost of returning the
function result.

The copy-out model, however, causes other headaches.  In particular, you
can't return a limited type, except by the "return by reference" kludge.

In Pascal and Fortran (at least) one returns a function result by assigning
to the result variable (default name = function name in Fortran 95).  The
Fortran standard is silent on _how_ function results are returned.  For
composite results (e.g. structures, arrays, character strings), most compilers
pass the address where the function result is to land as a "hidden argument."
In this way, the syntactic device of assigning to the function result
_really_is_ assigning directly to the function result -- no extra copying
involved.

By the way, this would eliminate the problem about returning a limited
type in Ada.
>>

Oh dear, this is very confused.

First, about trusting the compiler to optimize away the assignments to
Result. Most compilers in any case have only one copy of the epilog in
a function, and all return statements share it. For example, with GCC:

procedure f is

   b,c,d : Boolean;

   function g return boolean is
   begin
      if b then
         return c;
      else
         return d;
      end if;
   end;

   function h return boolean is
      result : Boolean;
   begin
      if b then
         result := c;
      else
         result := d;
      end if;
      return result;
   end;

begin
   null;
end;

The functions g and h generate identical code. It would be surprising to
anyone who knows something about compilers if this were NOT the case. There
is no doubling of costs anywhere, to guess that there might be is simply
based on some kind of inaccurate model of code generation.

Second, Ada does not require copy-out except for scalars, where in any case
it is more efficient (and typically used in most Fortran compilers for
example. It cannot be used in Pascal, but that often *damages* the efficiency
of compiled code in a Pascal compiler, since call by reference is less
efficient than call by copy for things that fit in registers. 

In fact Ada is very similar to Fortran when it comes to parameters that
do not fit in registers, it allows the compiler to pass either by reference
or by copy in/copy out. An Ada compiler will choose the most efficient
method, and typically any large argument will be passed by reference.

And yes, in the case where the result is fixed size (always true in Pascal
and Fortran, not, of course, always true in Ada), a standard technique is
for the caller to pass the address where the result is to be stored. Your
conception that this is not possible in Ada is just wrong (for example,
this is typically the way that GNAT returns results, and it is the way
anyone would expect fixed-length resluts to be returned)

Finally your comments on limited types show further misunderstanding. The
"difficulties" on returning limited types have nothing at all to do with
some kind of implementation problem (which as per above does not exist
anyway), but rather there is a fundamental semantic issue, which is that
limitedness is there to control copying (consider for example the case of
a self-referential access discriminant). Since copying must be controlled,
the semantics of returning a limited type must be dealt with very carefully
in the language -- this is purely a semantic issue, not an implementation
issue. I suggest reading the Ada 95 Rationale for a better understanding
of what limited types are all about.

As for the suggestions at the end of your message for the next version of
Ada, they are irrelevant, since they are based on misconceptions, and make
no sense in a context of understanding (a) the parameter passing rules
in Ada (b) the typical implemenmtation approaches (c) the semantics of
limited types. In particular, the suggestion of a syntactic device for
labeling the returned value seems entirely undesirable.





  parent reply	other threads:[~1997-06-27  0:00 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-11  0:00 GOTO considered necessary (reworked) Samuel Mize
1997-06-11  0:00 ` Bryce Bardin
1997-06-12  0:00 ` Michael F Brenner
1997-06-17  0:00   ` Robert Dewar
1997-06-17  0:00     ` Robert A Duff
1997-06-20  0:00       ` Robert Dewar
1997-06-21  0:00         ` Robert A Duff
1997-06-21  0:00           ` Robert Dewar
1997-06-12  0:00 ` Anonymous
1997-06-12  0:00   ` Robert Dewar
1997-06-12  0:00     ` John G. Volan
1997-06-13  0:00       ` Robert A Duff
1997-06-16  0:00         ` John G. Volan
1997-06-17  0:00           ` Robert A Duff
1997-06-25  0:00             ` Van Snyder
1997-06-17  0:00           ` Robert I. Eachus
1997-06-17  0:00           ` Robert Dewar
1997-06-17  0:00             ` Robert A Duff
1997-06-18  0:00               ` Spam Hater
1997-06-20  0:00               ` Robert Dewar
1997-06-21  0:00                 ` Robert A Duff
1997-06-21  0:00                   ` Robert Dewar
1997-06-20  0:00               ` Robert Dewar
1997-06-25  0:00               ` Wolfgang Gellerich
1997-06-25  0:00                 ` Samuel T. Harris
1997-06-25  0:00                 ` Michael F Brenner
1997-06-26  0:00                   ` Wolfgang Gellerich
1997-06-19  0:00             ` Karel Th�nissen
1997-06-19  0:00               ` Karel Th�nissen
1997-06-23  0:00               ` John G. Volan
1997-06-23  0:00                 ` Robert Dewar
1997-06-24  0:00                   ` Brian Rogoff
1997-06-25  0:00                   ` Featuritis not always bad (was re: GOTO considered necessary) Karel Th�nissen
1997-06-26  0:00                     ` Robert Dewar
1997-06-26  0:00                       ` Karel Th�nissen
1997-06-23  0:00                 ` GOTO considered necessary (reworked) Spam Hater
1997-06-25  0:00                 ` Karel Th�nissen
1997-06-23  0:00             ` John G. Volan
1997-07-21  0:00           ` Shmuel (Seymour J.) Metz
1997-06-12  0:00   ` John G. Volan
1997-06-16  0:00     ` Anonymous
1997-06-13  0:00 ` Robert A Duff
1997-06-14  0:00   ` Robert Dewar
1997-06-16  0:00     ` Spam Hater
1997-06-17  0:00       ` Robert Dewar
1997-06-17  0:00         ` Spam Hater
1997-06-16  0:00     ` Robert A Duff
1997-06-17  0:00       ` Spam Hater
1997-06-17  0:00         ` Robert A Duff
1997-06-19  0:00           ` Spam Hater
1997-06-17  0:00         ` Robert Dewar
1997-06-17  0:00           ` Spam Hater
1997-06-17  0:00           ` Robert A Duff
1997-06-19  0:00             ` John Herro
1997-06-25  0:00               ` Function result Van Snyder
1997-06-27  0:00                 ` Jon S Anthony
1997-06-27  0:00                 ` Robert Dewar [this message]
1997-06-20  0:00             ` GOTO considered necessary (reworked) Robert Dewar
1997-06-14  0:00   ` Samuel Mize
1997-06-14  0:00     ` Matthew Heaney
1997-06-14  0:00   ` Samuel Mize
1997-06-16  0:00 ` Anonymous
1997-06-16  0:00   ` Robert Dewar
replies disabled

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